use of org.matheclipse.core.interfaces.ISymbol in project symja_android_library by axkr.
the class PatternMap method getValue.
/**
* Return the matched value for the given pattern object
*
* @param pExpr
* @return <code>null</code> if no matched expression exists
*/
public IExpr getValue(@Nonnull IPatternObject pattern) {
ISymbol sym = pattern.getSymbol();
if (sym != null) {
return val(sym);
}
IExpr temp = pattern;
int indx = get(temp);
return indx >= 0 ? fPatternValuesArray[indx] : null;
}
use of org.matheclipse.core.interfaces.ISymbol in project symja_android_library by axkr.
the class RulesData method definition.
public List<IAST> definition() {
ArrayList<IAST> definitionList = new ArrayList<IAST>();
Iterator<IExpr> iter;
IExpr key;
PatternMatcherEquals pmEquals;
IAST ast;
ISymbol setSymbol;
IExpr condition;
PatternMatcherAndEvaluator pmEvaluator;
if (fEqualUpRules != null && fEqualUpRules.size() > 0) {
iter = fEqualUpRules.keySet().iterator();
while (iter.hasNext()) {
key = iter.next();
pmEquals = fEqualUpRules.get(key);
setSymbol = pmEquals.getSetSymbol();
// ast = F.ast(setSymbol);
// ast.add(key);
// ast.add(pmEquals.getRHS());
// definitionList.add(ast);
definitionList.add(F.binaryAST2(setSymbol, key, pmEquals.getRHS()));
}
}
if (fSimplePatternUpRules != null && fSimplePatternUpRules.size() > 0) {
Iterator<IPatternMatcher> listIter;
Set<IPatternMatcher>[] setArr = fSimplePatternUpRules.getValues();
for (int i = 0; i < setArr.length; i++) {
if (setArr[i] != null) {
listIter = setArr[i].iterator();
IPatternMatcher elem;
while (listIter.hasNext()) {
elem = listIter.next();
if (elem instanceof PatternMatcherAndEvaluator) {
pmEvaluator = (PatternMatcherAndEvaluator) elem;
setSymbol = pmEvaluator.getSetSymbol();
condition = pmEvaluator.getCondition();
if (condition != null) {
// ast = F.ast(setSymbol);
// ast.add(pmEvaluator.getLHS());
// ast.add(F.Condition(pmEvaluator.getRHS(),
// condition));
// definitionList.add(ast);
definitionList.add(F.binaryAST2(setSymbol, pmEvaluator.getLHS(), F.Condition(pmEvaluator.getRHS(), condition)));
} else {
// ast = F.ast(setSymbol);
// ast.add(pmEvaluator.getLHS());
// ast.add(pmEvaluator.getRHS());
// definitionList.add(ast);
definitionList.add(F.binaryAST2(setSymbol, pmEvaluator.getLHS(), pmEvaluator.getRHS()));
}
}
// if (elem instanceof PatternMatcherAndInvoker) {
// don't show internal methods associated with a pattern
// }
}
}
}
}
if (fEqualDownRules != null && fEqualDownRules.size() > 0) {
iter = fEqualDownRules.keySet().iterator();
while (iter.hasNext()) {
key = iter.next();
pmEquals = fEqualDownRules.get(key);
ast = pmEquals.getAsAST();
definitionList.add(ast);
}
}
if (fSimplePatternDownRules != null && fSimplePatternDownRules.size() > 0) {
Iterator<IPatternMatcher> listIter;
Set<IPatternMatcher>[] setArr = fSimplePatternDownRules.getValues();
for (int i = 0; i < setArr.length; i++) {
if (setArr[i] != null) {
listIter = setArr[i].iterator();
IPatternMatcher elem;
while (listIter.hasNext()) {
elem = listIter.next();
if (elem instanceof PatternMatcherAndEvaluator) {
pmEvaluator = (PatternMatcherAndEvaluator) elem;
ast = pmEvaluator.getAsAST();
definitionList.add(ast);
}
}
}
}
}
if (fSimpleOrderlesPatternDownRules != null && fSimpleOrderlesPatternDownRules.size() > 0) {
Iterator<IPatternMatcher> listIter;
Set<IPatternMatcher>[] setArr = fSimpleOrderlesPatternDownRules.getValues();
for (int i = 0; i < setArr.length; i++) {
if (setArr[i] != null) {
listIter = setArr[i].iterator();
IPatternMatcher elem;
Set<PatternMatcherAndEvaluator> set = new HashSet<PatternMatcherAndEvaluator>();
while (listIter.hasNext()) {
elem = listIter.next();
if (elem instanceof PatternMatcherAndEvaluator) {
pmEvaluator = (PatternMatcherAndEvaluator) elem;
if (!set.contains(pmEvaluator)) {
set.add(pmEvaluator);
ast = pmEvaluator.getAsAST();
definitionList.add(ast);
}
}
}
}
}
}
if (fPatternDownRules != null && fPatternDownRules.size() > 0) {
IPatternMatcher[] list = fPatternDownRules.toArray(new IPatternMatcher[0]);
for (int i = 0; i < list.length; i++) {
if (list[i] instanceof PatternMatcherAndEvaluator) {
pmEvaluator = (PatternMatcherAndEvaluator) list[i];
ast = pmEvaluator.getAsAST();
definitionList.add(ast);
}
}
}
return definitionList;
}
use of org.matheclipse.core.interfaces.ISymbol in project symja_android_library by axkr.
the class PatternMap method substitutePatternOrSymbols.
/**
* Substitute all patterns and symbols in the given expression with the
* current value of the corresponding internal pattern values arrays
*
* @param lhsPatternExpr
* left-hand-side expression which may containe pattern objects
*
* @return
*/
protected IExpr substitutePatternOrSymbols(final IExpr lhsPatternExpr) {
if (fPatternValuesArray != null) {
IExpr result = lhsPatternExpr.replaceAll((IExpr input) -> {
if (input instanceof IPatternObject) {
IPatternObject patternObject = (IPatternObject) input;
ISymbol sym = patternObject.getSymbol();
if (sym != null) {
for (int i = 0; i < fSymbolsArray.length; i++) {
if (sym == fSymbolsArray[i]) {
return fPatternValuesArray[i] != null ? fPatternValuesArray[i] : F.NIL;
}
}
} else {
for (int i = 0; i < fSymbolsArray.length; i++) {
if (patternObject == fSymbolsArray[i]) {
return fPatternValuesArray[i] != null ? fPatternValuesArray[i] : F.NIL;
}
}
}
}
return F.NIL;
});
if (result != null) {
return result;
}
}
return lhsPatternExpr;
}
use of org.matheclipse.core.interfaces.ISymbol in project symja_android_library by axkr.
the class PatternMap method addPattern.
/**
* Set the index of <code>fPatternSymbolsArray</code> where the
* <code>pattern</code> stores it's assigned value during pattern matching.
*
* @param pattern
* @param patternIndexMap
*/
public void addPattern(Map<IExpr, Integer> patternIndexMap, IPatternObject pattern) {
fRuleWithoutPattern = false;
ISymbol sym = pattern.getSymbol();
if (sym != null) {
Integer i = patternIndexMap.get(sym);
if (i != null) {
return;
}
patternIndexMap.put(sym, Integer.valueOf(fPatternCounter++));
} else {
patternIndexMap.put(pattern, Integer.valueOf(fPatternCounter++));
}
}
use of org.matheclipse.core.interfaces.ISymbol in project symja_android_library by axkr.
the class NIntegrate method integrate.
/**
* Integrate a function numerically.
*
* @param method
* the following methods are possible: LegendreGauss, Simpson,
* Romberg, Trapezoid
* @param list
* a list of the form <code>{x, lowerBound, upperBound}</code>,
* where <code>lowerBound</code> and <code>upperBound</code> are
* numbers which could be converted to a Java double value.
* @param min
* Lower bound of the integration interval.
* @param max
* Upper bound of the integration interval.
* @param function
* the function which should be integrated.
* @param maxPoints
* maximum number of points
* @param maxIterations
* maximum number of iterations
* @return
* @throws MathIllegalStateException
*/
public static double integrate(String method, IAST list, double min, double max, IExpr function, int maxPoints, int maxIterations) throws MathIllegalStateException {
GaussIntegratorFactory factory = new GaussIntegratorFactory();
ISymbol xVar = (ISymbol) list.arg1();
final EvalEngine engine = EvalEngine.get();
IExpr tempFunction = F.eval(function);
UnivariateFunction f = new UnaryNumerical(tempFunction, xVar, engine);
UnivariateIntegrator integrator;
if ("Simpson".equalsIgnoreCase(method)) {
integrator = new SimpsonIntegrator();
} else if ("Romberg".equalsIgnoreCase(method)) {
integrator = new RombergIntegrator();
} else if ("Trapezoid".equalsIgnoreCase(method)) {
integrator = new TrapezoidIntegrator();
} else {
// default: LegendreGauss
GaussIntegrator integ = factory.legendre(maxPoints, min, max);
return integ.integrate(f);
}
return integrator.integrate(maxIterations, f, min, max);
}
Aggregations