Search in sources :

Example 11 with MathException

use of org.matheclipse.parser.client.math.MathException in project symja_android_library by axkr.

the class SolveSO39753012 method main.

public static void main(String[] args) {
    try {
        ExprEvaluator util = new ExprEvaluator();
        IExpr eq = F.Equal(F.Plus(F.a, F.b), F.c);
        IExpr eq1 = eq.replaceAll(F.Rule(F.a, F.integer(1)));
        eq1 = eq1.replaceAll(F.Rule(F.b, F.integer(2)));
        // Solve(1+2==c, c)
        IExpr result = util.evaluate(F.Solve(eq1, F.c));
        // print: {{c->3}}
        System.out.println(result.toString());
        IExpr eq2 = eq.replaceAll(F.Rule(F.a, F.integer(1)));
        eq2 = eq2.replaceAll(F.Rule(F.c, F.integer(3)));
        // Solve(1+b==3, b)
        result = util.evaluate(F.Solve(eq2, F.b));
        // print: {{b->2}}
        System.out.println(result.toString());
    } catch (SyntaxError e) {
        // catch Symja parser errors here
        System.out.println(e.getMessage());
    } catch (MathException me) {
        // catch Symja math errors here
        System.out.println(me.getMessage());
    } catch (Exception e) {
        e.printStackTrace();
    } catch (final StackOverflowError soe) {
        System.out.println(soe.getMessage());
    } catch (final OutOfMemoryError oome) {
        System.out.println(oome.getMessage());
    }
}
Also used : ExprEvaluator(org.matheclipse.core.eval.ExprEvaluator) SyntaxError(org.matheclipse.parser.client.SyntaxError) MathException(org.matheclipse.parser.client.math.MathException) IExpr(org.matheclipse.core.interfaces.IExpr) MathException(org.matheclipse.parser.client.math.MathException)

Example 12 with MathException

use of org.matheclipse.parser.client.math.MathException in project symja_android_library by axkr.

the class SolveSO43024172 method main.

public static void main(String[] args) {
    try {
        ExprEvaluator util = new ExprEvaluator();
        IExpr expr = util.evaluate("(x1)^2+4*(x2)^2-2*x1-4*x2");
        System.out.println(expr.toString());
        // determine the variables used in the expression
        IAST variableList = VariablesSet.getVariables(expr);
        System.out.println(variableList.toString());
        IExpr a = util.evaluate("a");
        IExpr x1 = util.evaluate("x1");
        IExpr x2 = util.evaluate("x2");
        IExpr x1Substitute = util.evaluate("5+a");
        IExpr x2Substitute = util.evaluate("2");
        IAST astRules = F.List(F.Rule(x1, x1Substitute), F.Rule(x2, x2Substitute));
        // {x1->5+a,x2->2}
        System.out.println(astRules.toString());
        IExpr replacedExpr = expr.replaceAll(astRules);
        // -2*(5+a)+(5+a)^2+(-4)*2+4*2^2
        System.out.println(replacedExpr.toString());
        // 8+(5+a)^2-2*(5+a)
        replacedExpr = util.evaluate(replacedExpr);
        System.out.println(replacedExpr.toString());
        // replacedExpr = util.evaluate(F.ExpandAll(replacedExpr));
        // 23+8*a+a^2
        // System.out.println(replacedExpr.toString());
        IExpr derivedExpr = util.evaluate(F.D(replacedExpr, a));
        // -2+2*(5+a)
        System.out.println(derivedExpr.toString());
        IExpr equation = F.Equal(derivedExpr, F.C0);
        // -2+2*(5+a)==0
        System.out.println(equation.toString());
        IExpr solvedEquation = util.evaluate(F.Solve(equation, a));
        // {{a->-4}}
        System.out.println(solvedEquation.toString());
    } catch (SyntaxError e) {
        // catch Symja parser errors here
        System.out.println(e.getMessage());
    } catch (MathException me) {
        // catch Symja math errors here
        System.out.println(me.getMessage());
    } catch (Exception e) {
        e.printStackTrace();
    } catch (final StackOverflowError soe) {
        System.out.println(soe.getMessage());
    } catch (final OutOfMemoryError oome) {
        System.out.println(oome.getMessage());
    }
}
Also used : ExprEvaluator(org.matheclipse.core.eval.ExprEvaluator) SyntaxError(org.matheclipse.parser.client.SyntaxError) MathException(org.matheclipse.parser.client.math.MathException) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) MathException(org.matheclipse.parser.client.math.MathException)

Example 13 with MathException

use of org.matheclipse.parser.client.math.MathException in project symja_android_library by axkr.

the class EvalDoubleTestCase method testInterpolatingFunction.

public void testInterpolatingFunction() {
    try {
        DoubleEvaluator engine = new DoubleEvaluator();
        engine.setCallbackFunction(CoreCallbackFunction.CONST);
        engine.defineVariable("x", new DoubleVariable(3.0));
        double d = engine.evaluate("InterpolatingFunction[{{0, 0}, {1, 1}, {2, 3}, {3, 4}, {4, 3}, {5, 0}}][x]");
        assertEquals(Double.toString(d), "4.0");
    } catch (MathException e) {
        e.printStackTrace();
        assertEquals("EvalDouble#evaluateFunction(FunctionNode) not possible for: aTest(1.0)", e.getMessage());
    }
}
Also used : DoubleEvaluator(org.matheclipse.parser.client.eval.DoubleEvaluator) MathException(org.matheclipse.parser.client.math.MathException) DoubleVariable(org.matheclipse.parser.client.eval.DoubleVariable)

Example 14 with MathException

use of org.matheclipse.parser.client.math.MathException in project symja_android_library by axkr.

the class Programming method rememberVariables.

/**
	 * Remember which local variable names (appended with the module counter) we use in the given
	 * <code>variablesMap</code>.
	 * 
	 * @param variablesList
	 *            initializer variables list from the <code>Module</code> function
	 * @param varAppend
	 *            the module counter string which aer appended to the variable names.
	 * @param variablesMap
	 *            the resulting module variables map
	 * @param engine
	 *            the evaluation engine
	 */
private static void rememberVariables(IAST variablesList, final String varAppend, final java.util.Map<ISymbol, ISymbol> variablesMap, final EvalEngine engine) {
    ISymbol oldSymbol;
    ISymbol newSymbol;
    for (int i = 1; i < variablesList.size(); i++) {
        if (variablesList.get(i).isSymbol()) {
            oldSymbol = (ISymbol) variablesList.get(i);
            newSymbol = F.userSymbol(oldSymbol.toString() + varAppend, engine);
            variablesMap.put(oldSymbol, newSymbol);
            newSymbol.pushLocalVariable();
        } else {
            if (variablesList.get(i).isAST(F.Set, 3)) {
                final IAST setFun = (IAST) variablesList.get(i);
                if (setFun.arg1().isSymbol()) {
                    oldSymbol = (ISymbol) setFun.arg1();
                    newSymbol = F.userSymbol(oldSymbol.toString() + varAppend, engine);
                    variablesMap.put(oldSymbol, newSymbol);
                    IExpr rightHandSide = setFun.arg2();
                    try {
                        rightHandSide = engine.evaluate(rightHandSide);
                    } catch (MathException me) {
                        if (Config.DEBUG) {
                            me.printStackTrace();
                        }
                    }
                    newSymbol.pushLocalVariable(rightHandSide);
                }
            }
        }
    }
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) MathException(org.matheclipse.parser.client.math.MathException) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 15 with MathException

use of org.matheclipse.parser.client.math.MathException in project symja_android_library by axkr.

the class SymjaInterpreter method interpreter.

/**
	 * Parse the <code>codeString</code> into an <code>IExpr</code> and if
	 * <code>function</code> unequals <code>null</code>, replace all occurences
	 * of slot <code>#</code> in the function with the parsed expression. After
	 * that evaluate the given expression.
	 * 
	 * @param function
	 * @return
	 */
public String interpreter(IAST function) {
    String evalStr = codeString;
    IExpr expr;
    EvalEngine engine = EvalEngine.get();
    try {
        ExprParser p = new ExprParser(engine, true);
        // throws SyntaxError exception, if syntax isn't valid
        expr = p.parse(evalStr);
    } catch (SyntaxError e1) {
        try {
            ExprParser p = new ExprParser(engine);
            // throws SyntaxError exception, if syntax isn't valid
            expr = p.parse(evalStr);
        } catch (Exception e2) {
            outStream.println(e2.getMessage());
            return "";
        }
    }
    IExpr result;
    final StringBuilder buf = new StringBuilder();
    try {
        if (function != null) {
            expr = function.replaceAll(F.Rule(F.Slot1, expr));
        }
        if (expr.isPresent()) {
            result = evaluate(expr);
            if (result.isPresent()) {
                if (result.equals(F.Null)) {
                    return buf.toString();
                }
                OutputFormFactory.get(true).convert(buf, result);
            }
            return buf.toString();
        }
    } catch (final RuntimeException re) {
        Throwable me = re.getCause();
        if (me instanceof MathException) {
            Validate.printException(buf, me);
        } else {
            Validate.printException(buf, re);
        }
    } catch (final Exception e) {
        Validate.printException(buf, e);
    }
    return buf.toString();
}
Also used : SyntaxError(org.matheclipse.parser.client.SyntaxError) MathException(org.matheclipse.parser.client.math.MathException) IExpr(org.matheclipse.core.interfaces.IExpr) ExprParser(org.matheclipse.core.parser.ExprParser) MathException(org.matheclipse.parser.client.math.MathException)

Aggregations

MathException (org.matheclipse.parser.client.math.MathException)23 IExpr (org.matheclipse.core.interfaces.IExpr)16 SyntaxError (org.matheclipse.parser.client.SyntaxError)11 ExprEvaluator (org.matheclipse.core.eval.ExprEvaluator)6 IAST (org.matheclipse.core.interfaces.IAST)6 ASTNode (org.matheclipse.parser.client.ast.ASTNode)6 IOException (java.io.IOException)4 StringWriter (java.io.StringWriter)4 ISymbol (org.matheclipse.core.interfaces.ISymbol)2 ExprParser (org.matheclipse.core.parser.ExprParser)2 DoubleEvaluator (org.matheclipse.parser.client.eval.DoubleEvaluator)2 SimpleTimeLimiter (com.google.common.util.concurrent.SimpleTimeLimiter)1 TimeLimiter (com.google.common.util.concurrent.TimeLimiter)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Bindings (javax.script.Bindings)1 ScriptException (javax.script.ScriptException)1 AST2Expr (org.matheclipse.core.convert.AST2Expr)1 ASCIIPrettyPrinter3 (org.matheclipse.core.form.output.ASCIIPrettyPrinter3)1 ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)1