use of org.matheclipse.core.eval.EvalEngine in project symja_android_library by axkr.
the class MainTestCase method testSystem400.
public void testSystem400() {
EvalEngine engine = EvalEngine.get();
IExpr exprNumerator = engine.parse("8+12*x+20*x^2+12*x^3+8*x^4+3*x^5");
IExpr exprDenominator = engine.parse("8*x+12*x^3+6*x^5+x^7");
IExpr[] result = Algebra.cancelGCD(exprNumerator, exprDenominator);
assertEquals(result[0].toString(), "1");
assertEquals(result[1].toString(), "4+6*x+8*x^2+3*x^3");
}
use of org.matheclipse.core.eval.EvalEngine in project symja_android_library by axkr.
the class MMAFormTestCase method testWLForm006.
public void testWLForm006() {
EvalEngine engine = new EvalEngine("", 256, 256, System.out, System.err, true);
ExprParser parser = new ExprParser(engine, true);
IExpr expr = parser.parse("a+i*b^2+k*c^3+d");
assertEquals(expr.toMMA(), "a + i*b^2 + k*c^3 + d");
}
use of org.matheclipse.core.eval.EvalEngine in project symja_android_library by axkr.
the class PatternMatcher method matchFlatOrderless.
/**
* Match <code>Flat</code> and <code>Orderless</code> LHS pattern expressions. It's assumed that
* the headers of the expressions already matched.
*
* @param sym
* @param lhsPattern
* @param lhsEval
* @param engine
* @param stackMatcher
* @return
*/
private boolean matchFlatOrderless(final ISymbol sym, IAST lhsPattern, IAST lhsEval, EvalEngine engine, StackMatcher stackMatcher) {
if (lhsPattern.isAST1()) {
return matchExpr(lhsPattern.arg1(), lhsEval, engine, stackMatcher);
}
IAST lhsPatternAST = lhsPattern;
IAST lhsEvalAST = lhsEval;
// removeOrderless already called a level up
boolean matched = false;
IExpr[] patternValues = fPatternMap.copyPattern();
if (lhsPatternAST.size() <= 2) {
try {
if (lhsPatternAST.isAST1()) {
matched = matchExpr(lhsPatternAST.arg1(), lhsEvalAST, engine, stackMatcher);
return matched;
}
if (lhsPatternAST.isEmpty() && lhsEvalAST.size() > 1) {
matched = false;
return matched;
}
matched = stackMatcher.matchRest();
return matched;
} finally {
if (!matched) {
fPatternMap.resetPattern(patternValues);
}
}
}
lhsPattern = lhsPatternAST;
lhsEval = lhsEvalAST;
final IAST lhsPatternFinal = lhsPattern;
final IAST lhsEvalFinal = lhsEval;
for (int i = 1; i < lhsPatternFinal.size(); i++) {
IExpr patternArg = lhsPatternFinal.get(i);
if (!(patternArg instanceof IPatternObject)) {
final int index = i;
IAST reduced = lhsPatternFinal.splice(index);
boolean evaled = false;
for (int k = 1; k < lhsEvalFinal.size(); k++) {
try {
IExpr evalArg = lhsEvalFinal.get(k);
if (!(patternArg.head() instanceof IPatternObject)) {
if (patternArg.isASTOrAssociation()) {
if ((((IAST) patternArg).getEvalFlags() & IAST.CONTAINS_DEFAULT_PATTERN) == IAST.CONTAINS_DEFAULT_PATTERN) {
continue;
}
}
if (patternArg.head().equals(evalArg.head()) && patternArg.isFree(x -> x.isOrderlessAST(), true)) {
evaled = true;
matched = matchExpr(patternArg, evalArg, engine, stackMatcher);
}
if (matched) {
matched = matchFlatAndFlatOrderless(sym, reduced, lhsEvalFinal.removeAtCopy(k), engine, stackMatcher);
if (matched) {
return true;
}
}
}
} finally {
if (!matched) {
fPatternMap.resetPattern(patternValues);
}
}
}
if (evaled) {
return false;
}
}
}
FlatOrderlessStepVisitor visitor = new FlatOrderlessStepVisitor(sym, lhsPatternFinal, lhsEvalFinal, stackMatcher, fPatternMap, sym.hasFlatAttribute());
MultisetPartitionsIterator iter = new MultisetPartitionsIterator(visitor, lhsPattern.argSize());
return !iter.execute();
}
use of org.matheclipse.core.eval.EvalEngine in project symja_android_library by axkr.
the class MatrixD method evaluate.
/**
* For the referenced formula numbers (XX) see:
* <a href="https://archive.org/details/imm3274/">Internet Archive - The Matrix Cookbook</a>
*/
@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
IOFunctions.printExperimental(S.MatrixD);
if (ast.size() < 3) {
return F.NIL;
}
final IExpr fx = ast.arg1();
if (fx.isIndeterminate()) {
return S.Indeterminate;
}
if (ast.size() > 3) {
// reduce arguments by folding MatrixD[fxy, x, y] to MatrixD[ MatrixD[fxy, x], y] ...
return ast.foldLeft((x, y) -> engine.evaluateNIL(F.MatrixD(x, y)), fx, 2);
}
IExpr x = ast.arg2();
if (!(x.isVariable() || x.isList())) {
// `1` is not a valid variable.
return IOFunctions.printMessage(ast.topHead(), "ivar", F.list(x), engine);
}
if (fx.isList()) {
IAST list = (IAST) fx;
// thread over first list
return list.mapThreadEvaled(engine, F.ListAlloc(list.size()), ast, 1);
}
if (x.isList()) {
// MatrixD[fx_, {...}]
IAST xList = (IAST) x;
if (xList.isAST1() && xList.arg1().isListOfLists()) {
IAST subList = (IAST) xList.arg1();
IASTAppendable result = F.ListAlloc(subList.size());
result.appendArgs(subList.size(), i -> F.MatrixD(fx, F.list(subList.get(i))));
return result;
} else if (xList.isAST1() && xList.arg1().isList()) {
IAST subList = (IAST) xList.arg1();
return subList.mapLeft(F.ListAlloc(), (a, b) -> engine.evaluateNIL(F.MatrixD(a, b)), fx);
} else if (xList.isAST2()) {
if (xList.arg1().isList()) {
x = F.list(xList.arg1());
} else {
x = xList.arg1();
}
IExpr arg2 = xList.arg2();
int n = arg2.toIntDefault();
if (n >= 0) {
IExpr temp = fx;
for (int i = 0; i < n; i++) {
temp = S.MatrixD.ofNIL(engine, temp, x);
if (!temp.isPresent()) {
return F.NIL;
}
}
return temp;
}
if (arg2.isFree(num -> num.isNumber(), false)) {
if (fx.equals(x)) {
return S.$SingleEntryMatrix;
}
if (fx.isAST()) {
final IAST function = (IAST) fx;
if (function.isPlus()) {
// (35)
return function.mapThread(F.MatrixD(F.Slot1, xList), 1);
}
}
return F.NIL;
}
if (!x.isVariable()) {
// `1` is not a valid variable.
return IOFunctions.printMessage(ast.topHead(), "ivar", F.list(x), engine);
}
if (arg2.isAST()) {
return F.NIL;
}
// symbolic expression or a non-negative integer.
return IOFunctions.printMessage(ast.topHead(), "dvar", F.list(xList), engine);
}
return F.NIL;
}
if (!x.isVariable()) {
// `1` is not a valid variable.
return IOFunctions.printMessage(ast.topHead(), "ivar", F.list(x), engine);
}
return binaryMatrixD(fx, x);
}
use of org.matheclipse.core.eval.EvalEngine in project symja_android_library by axkr.
the class TrigToExp method evaluate.
/**
* Exponential definitions for trigonometric functions
*
* <p>
* See <a href=
* "http://en.wikipedia.org/wiki/List_of_trigonometric_identities#Exponential_definitions"> List
* of trigonometric identities - Exponential definitions</a>,<br>
* <a href="http://en.wikipedia.org/wiki/Hyperbolic_function">Hyperbolic function</a>
*/
@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
IExpr temp = StructureFunctions.threadLogicEquationOperators(ast.arg1(), ast, 1);
if (temp.isPresent()) {
return temp;
}
IExpr arg1 = ast.arg1();
Function<IExpr, IExpr> fun = x -> {
IExpr t = x.rewrite(ID.Exp);
if (!t.isPresent()) {
return x.rewrite(ID.Log);
}
return t.rewrite(ID.Log).orElse(t);
};
VisitorReplaceAllDFS dfs = new VisitorReplaceAllDFS(fun, 1);
return arg1.accept(dfs).orElse(arg1);
}
Aggregations