Search in sources :

Example 1 with SyntaxError

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

the class Console method main.

public static void main(final String[] args) {
    // console.getDefaultSystemRulesFilename(),
    F.initSymbols(null, null, true);
    // null, false);
    Console console;
    try {
        console = new Console();
    } catch (final SyntaxError e1) {
        e1.printStackTrace();
        return;
    }
    String inputExpression = null;
    String trimmedInput = null;
    console.setArgs(args);
    final File file = console.getFile();
    if (file != null) {
        try {
            final BufferedReader f = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
            final StringBuffer buff = new StringBuffer(1024);
            String line;
            while ((line = f.readLine()) != null) {
                buff.append(line);
                buff.append('\n');
            }
            f.close();
            inputExpression = buff.toString();
            System.out.println("In [" + COUNTER + "]: " + inputExpression);
            console.resultPrinter(inputExpression);
            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) == '?') {
                    String name = trimmedInput.substring(1);
                    IAST list = Names.getNamesByPrefix(name);
                    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());
                    } else if (list.size() == 1 && (name.equals("D") || name.equals("E") || name.equals("I") || name.equals("N"))) {
                        printDocumentation(name);
                    }
                    continue;
                }
                String postfix = Scanner.balanceCode(inputExpression);
                if (postfix != null && postfix.length() > 0) {
                    System.err.println("Automatically closing brackets: " + postfix);
                    inputExpression = inputExpression + postfix;
                }
                System.out.println("In [" + COUNTER + "]: " + inputExpression);
                if (console.fPrettyPrinter) {
                    console.prettyPrinter(inputExpression);
                } else {
                    console.resultPrinter(inputExpression);
                }
                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 : InputStreamReader(java.io.InputStreamReader) SyntaxError(org.matheclipse.parser.client.SyntaxError) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) IAST(org.matheclipse.core.interfaces.IAST) File(java.io.File) FileInputStream(java.io.FileInputStream) MathException(org.matheclipse.parser.client.math.MathException) IOException(java.io.IOException)

Example 2 with SyntaxError

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

the class SymjaInterpreter method interpreter.

/**
	 * Evaluate the expression assigned to this interpreter.
	 * 
	 * @param function
	 *            <code>null</code> if you like to evaluate in symbolic mode;
	 *            &quot;N&quot; if you like to evaluate in numeric mode
	 * @return
	 */
public String interpreter(String 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
        if (function != null) {
            evalStr = function + "(" + codeString + ")";
        }
        expr = p.parse(evalStr);
    } catch (SyntaxError e1) {
        try {
            ExprParser p = new ExprParser(engine);
            // throws SyntaxError exception, if syntax isn't valid
            if (function != null) {
                evalStr = function + "[" + codeString + "]";
            }
            expr = p.parse(evalStr);
        } catch (Exception e2) {
            outStream.println(e2.getMessage());
            return "";
        }
    }
    IExpr result;
    final StringBuilder buf = new StringBuilder();
    try {
        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 ex) {
        Validate.printException(buf, ex);
    } catch (final StackOverflowError soe) {
        Validate.printException(buf, soe);
    } catch (final OutOfMemoryError oome) {
        Validate.printException(buf, oome);
    }
    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)

Example 3 with SyntaxError

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

the class MMAConsole method interpreter.

/**
	 * Evaluates the given string-expression and returns the result in
	 * <code>OutputForm</code>
	 * 
	 * @param inputExpression
	 * @return
	 */
public String interpreter(final String inputExpression) {
    IExpr result;
    final StringWriter buf = new StringWriter();
    try {
        if (fSeconds <= 0) {
            result = fEvaluator.evaluate(inputExpression);
        } else {
            result = fEvaluator.evaluateWithTimeout(inputExpression, fSeconds, TimeUnit.SECONDS, true);
        }
        if (result != null) {
            if (result.equals(F.Null)) {
                return "";
            }
            return result.toString();
        }
    } catch (final SyntaxError se) {
        String msg = se.getMessage();
        System.err.println(msg);
    } 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);
    } catch (final OutOfMemoryError e) {
        Validate.printException(buf, e);
    } catch (final StackOverflowError e) {
        Validate.printException(buf, e);
    }
    return buf.toString();
}
Also used : StringWriter(java.io.StringWriter) 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) IOException(java.io.IOException)

Example 4 with SyntaxError

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

the class Console method interpreter.

/**
	 * Evaluates the given string-expression and returns the result in <code>OutputForm</code>
	 * 
	 * @param inputExpression
	 * @return
	 */
public String interpreter(final String inputExpression) {
    IExpr result;
    final StringWriter buf = new StringWriter();
    try {
        if (fSeconds <= 0) {
            result = fEvaluator.evaluate(inputExpression);
        } else {
            result = fEvaluator.evaluateWithTimeout(inputExpression, fSeconds, TimeUnit.SECONDS, true);
        }
        if (result != null) {
            if (result.equals(F.Null)) {
                return "";
            }
            StringBuilder strBuffer = new StringBuilder();
            fOutputFactory.convert(strBuffer, result);
            return strBuffer.toString();
        }
    } catch (final SyntaxError se) {
        String msg = se.getMessage();
        System.err.println();
        System.err.println(msg);
        return "";
    } catch (final RuntimeException re) {
        Throwable me = re.getCause();
        if (me instanceof MathException) {
            Validate.printException(buf, me);
        } else {
            Validate.printException(buf, re);
        }
        return "";
    } catch (final Exception e) {
        Validate.printException(buf, e);
        return "";
    } catch (final OutOfMemoryError e) {
        Validate.printException(buf, e);
        return "";
    } catch (final StackOverflowError e) {
        Validate.printException(buf, e);
        return "";
    }
    return buf.toString();
}
Also used : StringWriter(java.io.StringWriter) 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) IOException(java.io.IOException)

Example 5 with SyntaxError

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

the class PolynomialExample method main.

public static void main(String[] args) {
    try {
        ExprEvaluator util = new ExprEvaluator();
        IExpr expr = util.evaluate("x^2+y+a*x+b*y+c");
        System.out.println(expr.toString());
        final IAST variables = F.List(F.x, F.y);
        ExprPolynomialRing ring = new ExprPolynomialRing(ExprRingFactory.CONST, variables, variables.size() - 1, ExprTermOrderByName.Lexicographic, false);
        ExprPolynomial poly = ring.create(expr);
        System.out.println(poly.toString());
        // x degree
        System.out.println(poly.degree(0));
        // y degree
        System.out.println(poly.degree(1));
        // show internal structure:
        System.out.println(poly.coefficientRules());
        System.out.println();
        for (ExprMonomial monomial : poly) {
            System.out.println(monomial.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 : ExprPolynomialRing(org.matheclipse.core.polynomials.ExprPolynomialRing) 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) ExprMonomial(org.matheclipse.core.polynomials.ExprMonomial) ExprPolynomial(org.matheclipse.core.polynomials.ExprPolynomial) MathException(org.matheclipse.parser.client.math.MathException)

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