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();
}
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;
}
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;
}
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);
}
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);
}
}
Aggregations