Search in sources :

Example 6 with SyntaxError

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

the class SolveExample method main.

public static void main(String[] args) {
    try {
        ExprEvaluator util = new ExprEvaluator();
        IExpr result = util.evaluate("Solve(2*x==5 + 4*x,x)");
        // print: {{x->-5/2}}
        System.out.println(result.toString());
        result = util.evaluate("Roots(2*x==5+4*x, x)");
        // print: x==-5/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 7 with SyntaxError

use of org.matheclipse.parser.client.SyntaxError 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 8 with SyntaxError

use of org.matheclipse.parser.client.SyntaxError 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 9 with SyntaxError

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

the class EvalUtilities method eval.

/**
	 * Evaluate the <code>inputExpression</code> and return the resulting
	 * expression. <br/>
	 * The parser first tries (independently from the settings in the
	 * <code>evalEngine</code>) to parse the expression in the &quot;relaxed
	 * mode&quot; (i.e. &quot;common math expression syntax&quot; with
	 * parentheses for function arguments) and if that results in a
	 * <code>SyntaxError</code> exception it tries to parse in the
	 * &quot;stronger mode&quot; (i.e. with square brackets for function
	 * arguments). <br />
	 * <b>Note</B> that after the second parser step this method may also throw
	 * a <code>SyntaxError</code> exception.
	 * 
	 * @param inputExpression
	 *            the expression which should be evaluated.
	 * @param evalEngine
	 *            the evaluation engine which should be used
	 * @return <code>F.NIL</code>, if the inputExpression is <code>null</code>
	 * @throw org.matheclipse.parser.client.SyntaxError
	 * @throw org.matheclipse.parser.client.math.MathException
	 */
public static IExpr eval(final String inputExpression, final EvalEngine evalEngine) throws MathException {
    if (inputExpression != null) {
        EvalEngine.set(evalEngine);
        boolean SIMPLE_SYNTAX = true;
        IExpr parsedExpression;
        try {
            ExprParser parser = new ExprParser(evalEngine, SIMPLE_SYNTAX);
            parsedExpression = parser.parse(inputExpression);
        } catch (SyntaxError se1) {
            try {
                SIMPLE_SYNTAX = false;
                ExprParser parser = new ExprParser(evalEngine, SIMPLE_SYNTAX);
                parsedExpression = parser.parse(inputExpression);
            } catch (SyntaxError se2) {
                throw se1;
            }
        }
        if (parsedExpression != null) {
            F.join();
            evalEngine.reset();
            IExpr temp = evalEngine.evaluate(parsedExpression);
            evalEngine.addOut(temp);
            return temp;
        }
    }
    return F.NIL;
}
Also used : SyntaxError(org.matheclipse.parser.client.SyntaxError) IExpr(org.matheclipse.core.interfaces.IExpr) ExprParser(org.matheclipse.core.parser.ExprParser)

Example 10 with SyntaxError

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

the class MMAConsole method main.

public static void main(final String[] args) {
    // distinguish between lower- and uppercase identifiers
    Config.PARSER_USE_LOWERCASE_SYMBOLS = false;
    // console.getDefaultSystemRulesFilename(),
    F.initSymbols(null, null, true);
    // null, false);
    printUsage();
    MMAConsole console;
    try {
        console = new MMAConsole();
    } catch (final SyntaxError e1) {
        e1.printStackTrace();
        return;
    }
    String inputExpression = null;
    String trimmedInput = null;
    String outputExpression = null;
    console.setArgs(args);
    final File file = console.getFile();
    if (file != null) {
        try {
            final BufferedReader f = new BufferedReader(new FileReader(file));
            final StringBuffer buff = new StringBuffer(1024);
            String line;
            while ((line = f.readLine()) != null) {
                buff.append(line);
                buff.append('\n');
            }
            f.close();
            inputExpression = buff.toString();
            outputExpression = console.interpreter(inputExpression);
            System.out.println("In [" + COUNTER + "]: " + inputExpression);
            if (outputExpression.length() > 0) {
                System.out.println("Out[" + COUNTER + "]: " + outputExpression);
            }
            COUNTER++;
        } catch (final IOException ioe) {
            final String msg = "Cannot read from the specified file. " + "Make sure the path exists and you have read permission.";
            System.out.println(msg);
            return;
        }
    }
    while (true) {
        try {
            inputExpression = console.readString(System.out, ">>> ");
            if (inputExpression != null) {
                trimmedInput = inputExpression.trim();
                if ((inputExpression.length() >= 4) && inputExpression.toLowerCase(Locale.ENGLISH).substring(0, 4).equals("exit")) {
                    System.out.println("Closing Symja console... bye.");
                    System.exit(0);
                } else if ((inputExpression.length() >= 10) && inputExpression.toLowerCase(Locale.ENGLISH).substring(0, 10).equals("timeoutoff")) {
                    System.out.println("Disabling timeout for evaluation");
                    console.fSeconds = -1;
                    continue;
                } else if ((inputExpression.length() >= 9) && inputExpression.toLowerCase(Locale.ENGLISH).substring(0, 9).equals("timeouton")) {
                    System.out.println("Enabling timeout for evaluation to 60 seconds.");
                    console.fSeconds = 60;
                    continue;
                } else if (trimmedInput.length() > 1 && trimmedInput.charAt(0) == '?') {
                    IAST list = Names.getNamesByPrefix(trimmedInput.substring(1));
                    for (int i = 1; i < list.size(); i++) {
                        System.out.print(list.get(i).toString());
                        if (i != list.size() - 1) {
                            System.out.print(", ");
                        }
                    }
                    System.out.println();
                    if (list.size() == 2) {
                        printDocumentation(list.get(1).toString());
                    }
                    continue;
                }
                outputExpression = console.interpreter(inputExpression);
                System.out.println("In [" + COUNTER + "]: " + inputExpression);
                if (outputExpression.length() > 0) {
                    System.out.println("Out[" + COUNTER + "]: " + outputExpression);
                }
                COUNTER++;
            }
        // } catch (final MathRuntimeException mre) {
        // Throwable me = mre.getCause();
        // System.out.println(me.getMessage());
        } catch (final Exception e) {
            System.out.println(e.getMessage());
        }
    }
}
Also used : SyntaxError(org.matheclipse.parser.client.SyntaxError) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) IOException(java.io.IOException) IAST(org.matheclipse.core.interfaces.IAST) File(java.io.File) MathException(org.matheclipse.parser.client.math.MathException) IOException(java.io.IOException)

Aggregations

SyntaxError (org.matheclipse.parser.client.SyntaxError)15 IExpr (org.matheclipse.core.interfaces.IExpr)13 MathException (org.matheclipse.parser.client.math.MathException)13 ExprEvaluator (org.matheclipse.core.eval.ExprEvaluator)6 IOException (java.io.IOException)5 IAST (org.matheclipse.core.interfaces.IAST)5 StringWriter (java.io.StringWriter)3 ExprParser (org.matheclipse.core.parser.ExprParser)3 BufferedReader (java.io.BufferedReader)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)1 FileReader (java.io.FileReader)1 InputStreamReader (java.io.InputStreamReader)1 ASCIIPrettyPrinter3 (org.matheclipse.core.form.output.ASCIIPrettyPrinter3)1 ExprMonomial (org.matheclipse.core.polynomials.ExprMonomial)1 ExprPolynomial (org.matheclipse.core.polynomials.ExprPolynomial)1 ExprPolynomialRing (org.matheclipse.core.polynomials.ExprPolynomialRing)1