Search in sources :

Example 86 with ISymbol

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;
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 87 with ISymbol

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;
}
Also used : OpenIntToSet(org.matheclipse.core.eval.util.OpenIntToSet) Set(java.util.Set) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) ISymbol(org.matheclipse.core.interfaces.ISymbol) ArrayList(java.util.ArrayList) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr) HashSet(java.util.HashSet)

Example 88 with ISymbol

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;
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) IPatternObject(org.matheclipse.core.interfaces.IPatternObject) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 89 with ISymbol

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++));
    }
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol)

Example 90 with ISymbol

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);
}
Also used : SimpsonIntegrator(org.hipparchus.analysis.integration.SimpsonIntegrator) UnaryNumerical(org.matheclipse.core.generic.UnaryNumerical) ISymbol(org.matheclipse.core.interfaces.ISymbol) UnivariateFunction(org.hipparchus.analysis.UnivariateFunction) UnivariateIntegrator(org.hipparchus.analysis.integration.UnivariateIntegrator) RombergIntegrator(org.hipparchus.analysis.integration.RombergIntegrator) EvalEngine(org.matheclipse.core.eval.EvalEngine) IExpr(org.matheclipse.core.interfaces.IExpr) GaussIntegratorFactory(org.hipparchus.analysis.integration.gauss.GaussIntegratorFactory) TrapezoidIntegrator(org.hipparchus.analysis.integration.TrapezoidIntegrator) GaussIntegrator(org.hipparchus.analysis.integration.gauss.GaussIntegrator)

Aggregations

ISymbol (org.matheclipse.core.interfaces.ISymbol)117 IExpr (org.matheclipse.core.interfaces.IExpr)79 IAST (org.matheclipse.core.interfaces.IAST)76 IInteger (org.matheclipse.core.interfaces.IInteger)18 WrongArgumentType (org.matheclipse.core.eval.exception.WrongArgumentType)12 INum (org.matheclipse.core.interfaces.INum)10 ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)10 IFraction (org.matheclipse.core.interfaces.IFraction)9 IBuiltInSymbol (org.matheclipse.core.interfaces.IBuiltInSymbol)8 IComplex (org.matheclipse.core.interfaces.IComplex)7 IComplexNum (org.matheclipse.core.interfaces.IComplexNum)7 ArrayList (java.util.ArrayList)6 IPatternObject (org.matheclipse.core.interfaces.IPatternObject)6 HashSet (java.util.HashSet)5 EvalEngine (org.matheclipse.core.eval.EvalEngine)5 Num (org.matheclipse.core.expression.Num)5 GenPolynomial (edu.jas.poly.GenPolynomial)4 Options (org.matheclipse.core.eval.util.Options)4 ExprPolynomialRing (org.matheclipse.core.polynomials.ExprPolynomialRing)4 ExpVector (edu.jas.poly.ExpVector)3