Search in sources :

Example 11 with EvalEngine

use of org.matheclipse.core.eval.EvalEngine in project symja_android_library by axkr.

the class AbstractAST method isValue.

/** {@inheritDoc} */
@Override
public final boolean isValue() {
    EvalEngine engine = EvalEngine.get();
    ISymbol symbol = topHead();
    IExpr result = engine.evalAttributes(symbol, this);
    if (result.isPresent()) {
        if (result.isAST(symbol)) {
            return engine.evalRules(symbol, (IAST) result).isPresent();
        }
        return false;
    }
    return engine.evalRules(symbol, this).isPresent();
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) EvalEngine(org.matheclipse.core.eval.EvalEngine) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

Example 12 with EvalEngine

use of org.matheclipse.core.eval.EvalEngine in project symja_android_library by axkr.

the class PatternMatcher method checkCondition.

/**
	 * Check if the condition for this pattern matcher evaluates to <code>true</code>.
	 */
public boolean checkCondition() {
    if (fPatternCondition != null) {
        final EvalEngine engine = EvalEngine.get();
        boolean traceMode = false;
        try {
            traceMode = engine.isTraceMode();
            engine.setTraceMode(false);
            final IExpr substConditon = fPatternMap.substituteSymbols(fPatternCondition);
            if (engine.evalTrue(substConditon)) {
                return checkRHSCondition(engine);
            }
            return false;
        } finally {
            engine.setTraceMode(traceMode);
        }
    }
    return true;
}
Also used : EvalEngine(org.matheclipse.core.eval.EvalEngine) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 13 with EvalEngine

use of org.matheclipse.core.eval.EvalEngine in project symja_android_library by axkr.

the class F method expand.

/**
	 * Apply <code>Expand()</code> to the given expression if it's an <code>IAST</code>. If expanding wasn't possible
	 * this method returns the given argument.
	 * 
	 * @param a
	 *            the expression which should be evaluated
	 * @param expandNegativePowers
	 *            TODO
	 * @param distributePlus
	 *            TODO
	 * @return the evaluated expression
	 * @see EvalEngine#evaluate(IExpr)
	 */
public static IExpr expand(IExpr a, boolean expandNegativePowers, boolean distributePlus) {
    if (a.isAST()) {
        EvalEngine engine = EvalEngine.get();
        IAST ast = engine.evalFlatOrderlessAttributesRecursive((IAST) a);
        if (!ast.isPresent()) {
            ast = (IAST) a;
        }
        return Algebra.expand(ast, null, expandNegativePowers, distributePlus).orElse(a);
    }
    return a;
}
Also used : EvalEngine(org.matheclipse.core.eval.EvalEngine) IAST(org.matheclipse.core.interfaces.IAST)

Example 14 with EvalEngine

use of org.matheclipse.core.eval.EvalEngine in project symja_android_library by axkr.

the class F method num.

public static INum num(final IFraction value) {
    EvalEngine engine = EvalEngine.get();
    if (engine.isApfloat()) {
        return ApfloatNum.valueOf(value.toBigNumerator(), value.toBigDenominator(), engine.getNumericPrecision());
    }
    final double n = value.toBigNumerator().doubleValue();
    final double d = value.toBigDenominator().doubleValue();
    return num(n / d);
}
Also used : EvalEngine(org.matheclipse.core.eval.EvalEngine)

Example 15 with EvalEngine

use of org.matheclipse.core.eval.EvalEngine in project symja_android_library by axkr.

the class Iterator method create.

/**
	 * Iterator specification for functions like <code>Table()</code> or
	 * <code>Sum()</code> or <code>Product()</code>
	 * 
	 * @param list
	 *            a list representing an iterator specification
	 * @param symbol
	 *            the variable symbol
	 * @param engine
	 *            the evaluation engine
	 * @return the iterator
	 */
public static IIterator<IExpr> create(final IAST list, final ISymbol symbol, final EvalEngine engine) {
    EvalEngine evalEngine = engine;
    IExpr lowerLimit;
    IExpr upperLimit;
    IExpr step;
    ISymbol variable;
    boolean fNumericMode;
    boolean localNumericMode = evalEngine.isNumericMode();
    try {
        if (list.hasNumericArgument()) {
            evalEngine.setNumericMode(true);
        }
        fNumericMode = evalEngine.isNumericMode();
        switch(list.size()) {
            case 2:
                lowerLimit = F.C1;
                upperLimit = evalEngine.evalWithoutNumericReset(list.arg1());
                step = F.C1;
                variable = symbol;
                if (upperLimit instanceof Num) {
                    return new DoubleIterator(variable, 1.0, ((INum) upperLimit).doubleValue(), 1.0);
                }
                if (upperLimit.isInteger()) {
                    try {
                        int iUpperLimit = ((IInteger) upperLimit).toInt();
                        return new IntIterator(symbol, 1, iUpperLimit, 1);
                    } catch (ArithmeticException ae) {
                    //
                    }
                } else if (upperLimit.isRational()) {
                    try {
                        return new RationalIterator(symbol, F.C1, (IRational) upperLimit, F.C1);
                    } catch (ArithmeticException ae) {
                    //
                    }
                } else if (upperLimit.isSignedNumber()) {
                    return new ISignedNumberIterator(variable, F.C1, (ISignedNumber) upperLimit, F.C1);
                }
                break;
            case 3:
                lowerLimit = evalEngine.evalWithoutNumericReset(list.arg1());
                upperLimit = evalEngine.evalWithoutNumericReset(list.arg2());
                step = F.C1;
                variable = symbol;
                if (lowerLimit instanceof Num && upperLimit instanceof Num) {
                    return new DoubleIterator(variable, ((INum) lowerLimit).doubleValue(), ((INum) upperLimit).doubleValue(), 1.0);
                }
                if (lowerLimit.isInteger() && upperLimit.isInteger()) {
                    try {
                        int iLowerLimit = ((IInteger) lowerLimit).toInt();
                        int iUpperLimit = ((IInteger) upperLimit).toInt();
                        return new IntIterator(symbol, iLowerLimit, iUpperLimit, 1);
                    } catch (ArithmeticException ae) {
                    //
                    }
                } else if (lowerLimit.isRational() && upperLimit.isRational()) {
                    try {
                        return new RationalIterator(symbol, (IRational) lowerLimit, (IRational) upperLimit, F.C1);
                    } catch (ArithmeticException ae) {
                    //
                    }
                } else if (lowerLimit.isSignedNumber() && upperLimit.isSignedNumber()) {
                    return new ISignedNumberIterator(variable, (ISignedNumber) lowerLimit, (ISignedNumber) upperLimit, F.C1);
                }
                break;
            case 4:
                lowerLimit = evalEngine.evalWithoutNumericReset(list.arg1());
                upperLimit = evalEngine.evalWithoutNumericReset(list.arg2());
                step = evalEngine.evalWithoutNumericReset(list.arg3());
                variable = symbol;
                if (lowerLimit instanceof Num && upperLimit instanceof Num && step instanceof Num) {
                    return new DoubleIterator(variable, ((INum) lowerLimit).doubleValue(), ((INum) upperLimit).doubleValue(), ((INum) step).doubleValue());
                }
                if (lowerLimit.isInteger() && upperLimit.isInteger() && step.isInteger()) {
                    try {
                        int iLowerLimit = ((IInteger) lowerLimit).toInt();
                        int iUpperLimit = ((IInteger) upperLimit).toInt();
                        int iStep = ((IInteger) step).toInt();
                        return new IntIterator(symbol, iLowerLimit, iUpperLimit, iStep);
                    } catch (ArithmeticException ae) {
                    //
                    }
                } else if (lowerLimit.isRational() && upperLimit.isRational() && step.isRational()) {
                    try {
                        return new RationalIterator(symbol, (IRational) lowerLimit, (IRational) upperLimit, (IRational) step);
                    } catch (ArithmeticException ae) {
                    //
                    }
                } else if (lowerLimit.isSignedNumber() && upperLimit.isSignedNumber() && step.isSignedNumber()) {
                    return new ISignedNumberIterator(variable, (ISignedNumber) lowerLimit, (ISignedNumber) upperLimit, (ISignedNumber) step);
                }
                break;
            default:
                lowerLimit = null;
                upperLimit = null;
                step = null;
                variable = null;
        }
        return new ExprIterator(variable, evalEngine, lowerLimit, upperLimit, step, fNumericMode);
    } finally {
        evalEngine.setNumericMode(localNumericMode);
    }
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) INum(org.matheclipse.core.interfaces.INum) Num(org.matheclipse.core.expression.Num) IInteger(org.matheclipse.core.interfaces.IInteger) EvalEngine(org.matheclipse.core.eval.EvalEngine) IRational(org.matheclipse.core.interfaces.IRational) IExpr(org.matheclipse.core.interfaces.IExpr)

Aggregations

EvalEngine (org.matheclipse.core.eval.EvalEngine)32 IExpr (org.matheclipse.core.interfaces.IExpr)15 IAST (org.matheclipse.core.interfaces.IAST)5 ISymbol (org.matheclipse.core.interfaces.ISymbol)5 IRational (org.matheclipse.core.interfaces.IRational)3 IOException (java.io.IOException)2 RecursionLimitExceeded (org.matheclipse.core.eval.exception.RecursionLimitExceeded)2 Num (org.matheclipse.core.expression.Num)2 IInteger (org.matheclipse.core.interfaces.IInteger)2 INum (org.matheclipse.core.interfaces.INum)2 Parser (org.matheclipse.parser.client.Parser)2 File (java.io.File)1 InputStream (java.io.InputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 PrintWriter (java.io.PrintWriter)1 Apint (org.apfloat.Apint)1 UnivariateFunction (org.hipparchus.analysis.UnivariateFunction)1 RombergIntegrator (org.hipparchus.analysis.integration.RombergIntegrator)1 SimpsonIntegrator (org.hipparchus.analysis.integration.SimpsonIntegrator)1 TrapezoidIntegrator (org.hipparchus.analysis.integration.TrapezoidIntegrator)1