Search in sources :

Example 11 with AbortException

use of org.matheclipse.core.eval.exception.AbortException in project symja_android_library by axkr.

the class TeXParser method identifier.

/**
 * Create an identifier from multiple <code>&lt;mi&gt;</code> expressions.
 *
 * @param list
 * @param position
 * @return
 */
private ISymbol identifier(NodeList list, int[] position) {
    StringBuilder buf = new StringBuilder();
    boolean evaled = false;
    while (position[0] < list.getLength()) {
        Node temp = list.item(position[0]);
        if (temp.getNodeName().equals("mi")) {
            position[0]++;
            buf.append(temp.getTextContent());
            evaled = true;
        } else {
            break;
        }
    }
    if (evaled) {
        return F.$s(buf.toString());
    }
    throw new AbortException();
}
Also used : Node(org.w3c.dom.Node) AbortException(org.matheclipse.core.eval.exception.AbortException)

Example 12 with AbortException

use of org.matheclipse.core.eval.exception.AbortException in project symja_android_library by axkr.

the class TeXParser method integrate.

/**
 * Get the position there the <code>d</code> of the <code>dx</code> or <code>dx/expr</code> in the
 * integral definition starts and ends and create an <code>F.Integrate(...,x)</code> or <code>
 * F.Integrate()...,{x,a,b})</code> expression.
 *
 * @param parentList
 * @param position
 * @param dummySymbol
 * @param symbolOrList
 * @return
 */
private IExpr integrate(NodeList parentList, int[] position, ISymbol dummySymbol, IExpr symbolOrList) {
    ISymbol x = null;
    IExpr dxValue = F.C1;
    int dxStart = -1;
    int dxEnd = -1;
    int[] dxPosition1 = new int[] { position[0] };
    while (dxPosition1[0] < parentList.getLength()) {
        Node nd = parentList.item(dxPosition1[0]++);
        if (// 
        nd.getNodeName().equals("mi") && nd.getTextContent().equals("d")) {
            if (dxPosition1[0] < parentList.getLength()) {
                nd = parentList.item(dxPosition1[0]);
                if (nd.getNodeName().equals("mi")) {
                    dxStart = dxPosition1[0];
                    ISymbol x1 = identifier(parentList, dxPosition1);
                    dxEnd = dxPosition1[0];
                    x = x1;
                    break;
                }
            }
        } else if (nd.getNodeName().equals("mfrac")) {
            IExpr frac = mfrac(nd.getChildNodes());
            if (frac.isTimes() && frac.first().isSymbol()) {
                ISymbol d = (ISymbol) frac.first();
                String dStr = d.getSymbolName();
                if (dStr.startsWith("d")) {
                    // dx/x
                    dxStart = dxPosition1[0];
                    dxEnd = dxPosition1[0];
                    x = F.$s(dStr.substring(1));
                    dxValue = frac.second();
                    break;
                }
            }
        }
    }
    if (x == null) {
        throw new AbortException();
    }
    dxStart--;
    if (dxStart > position[0]) {
        IExpr arg1 = convert(parentList, position, dxStart, null, 0);
        position[0] = dxEnd;
        arg1 = F.subs(arg1, dummySymbol, x);
        symbolOrList = F.subs(symbolOrList, dummySymbol, x);
        return F.binaryAST2(S.Integrate, arg1, symbolOrList);
    } else if (dxStart == position[0]) {
        position[0] = dxEnd;
        symbolOrList = F.subs(symbolOrList, dummySymbol, x);
        return F.binaryAST2(S.Integrate, dxValue, symbolOrList);
    }
    throw new AbortException();
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) Node(org.w3c.dom.Node) IExpr(org.matheclipse.core.interfaces.IExpr) AbortException(org.matheclipse.core.eval.exception.AbortException)

Example 13 with AbortException

use of org.matheclipse.core.eval.exception.AbortException in project symja_android_library by axkr.

the class TeXParser method msup.

private IExpr msup(NodeList list) {
    if (list.getLength() == 2) {
        Node arg1 = list.item(0);
        Node arg2 = list.item(1);
        return power(arg1, arg2);
    }
    throw new AbortException();
}
Also used : Node(org.w3c.dom.Node) AbortException(org.matheclipse.core.eval.exception.AbortException)

Example 14 with AbortException

use of org.matheclipse.core.eval.exception.AbortException in project symja_android_library by axkr.

the class TeXParser method convert.

private IExpr convert(NodeList list, int[] position, int end, IExpr lhs, int precedence) {
    final int listSize = list.getLength();
    if (end > 1) {
        if (lhs == null) {
            Node lhsNode = list.item(position[0]++);
            String name = lhsNode.getNodeName();
            if (name.equals("mo")) {
                String text = lhsNode.getTextContent();
                PrefixOperator operator = PREFIX_OPERATOR_MAP.get(text);
                if (operator != null) {
                    int currPrec = operator.getPrecedence();
                    IExpr x = convert(list, position, end, null, currPrec);
                    lhs = operator.createFunction(x);
                }
            }
            if (lhs == null) {
                lhs = toHeadExpr(lhsNode, list, position, precedence);
                if (position[0] >= listSize) {
                    return lhs;
                }
            }
            int attribute = ISymbol.NOATTRIBUTE;
            if (lhs.isSymbol()) {
                attribute = ((ISymbol) lhs).getAttributes();
            }
            if ((attribute & ISymbol.CONSTANT) != ISymbol.CONSTANT) {
                if (// 
                (lhs.isFunction() || lhs.isSymbol() || lhs.isDerivative() != null) && position[0] < listSize) {
                    boolean isNumericFunction = ((attribute & ISymbol.NUMERICFUNCTION) == ISymbol.NUMERICFUNCTION);
                    Node arg2 = list.item(position[0]);
                    if (arg2.getNodeName().equals("mfenced")) {
                        position[0]++;
                        int[] position2 = new int[] { 0 };
                        NodeList childNodes = arg2.getChildNodes();
                        IExpr args = convertArgs(childNodes, position2);
                        if (args.isSequence()) {
                            ((IASTMutable) args).set(0, lhs);
                            return args;
                        }
                        lhs = F.unaryAST1(lhs, args);
                        if (position[0] == listSize) {
                            return lhs;
                        }
                    } else if (isNumericFunction || (lhs.isBuiltInSymbol() && !(lhs instanceof BuiltInDummy)) || lhs.isFunction()) {
                        if (lhs.equals(S.Integrate)) {
                            ISymbol test = F.Dummy("test");
                            return integrate(list, position, test, test);
                        }
                        IExpr args = convert(list, position, end, null, 0);
                        if (args.isSequence()) {
                            ((IASTMutable) args).set(0, lhs);
                            return args;
                        }
                        if (lhs.isFunction() && lhs.size() == 2) {
                            IExpr temp = Lambda.replaceSlots(lhs.first(), F.list(args));
                            if (temp.isPresent()) {
                                lhs = temp;
                            }
                        } else {
                            lhs = F.unaryAST1(lhs, args);
                        }
                        if (position[0] == listSize) {
                            return lhs;
                        }
                    }
                }
            }
        }
        IExpr result = lhs;
        int currPrec = 0;
        while (position[0] < end) {
            Node op = list.item(position[0]);
            String name = op.getNodeName();
            if (name.equals("mo")) {
                String text = op.getTextContent();
                if (SHOW_UNICODE) {
                    LOGGER.info("mo: {} - {}", () -> text, () -> toUnicodeString(text, "UTF-8"));
                }
                BinaryOperator binaryOperator = BINARY_OPERATOR_MAP.get(text);
                if (binaryOperator != null) {
                    currPrec = binaryOperator.getPrecedence();
                    if (precedence >= currPrec) {
                        return result;
                    }
                    position[0]++;
                    IExpr rhs = convert(list, position, end, null, currPrec);
                    result = binaryOperator.createFunction(result, rhs);
                    continue;
                } else {
                    PostfixOperator postfixOperator = POSTFIX_OPERATOR_MAP.get(text);
                    if (postfixOperator != null) {
                        currPrec = postfixOperator.getPrecedence();
                        if (precedence >= currPrec) {
                            return result;
                        }
                        result = postfixOperator.createFunction(lhs);
                        position[0]++;
                        continue;
                    }
                }
                throw new AbortException();
            } else if (name.equals("mspace")) {
                position[0]++;
                continue;
            }
            // try to build a Times(...) expression
            currPrec = Precedence.TIMES;
            IExpr rhs = convert(list, position, end, null, currPrec);
            // invisible times?
            result = F.Times(lhs, rhs);
        }
        if (result.isPresent() && position[0] >= end) {
            return result;
        }
    }
    return convertArgs(list, position);
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) IASTMutable(org.matheclipse.core.interfaces.IASTMutable) BuiltInDummy(org.matheclipse.core.expression.BuiltInDummy) IExpr(org.matheclipse.core.interfaces.IExpr) AbortException(org.matheclipse.core.eval.exception.AbortException)

Example 15 with AbortException

use of org.matheclipse.core.eval.exception.AbortException 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 trimmedInput a trimmed input string
 * @return
 */
/* package private */
String interpreter(final String trimmedInput) {
    IExpr result;
    final StringWriter buf = new StringWriter();
    try {
        if (trimmedInput.length() > 1 && trimmedInput.charAt(0) == '?') {
            IExpr doc = Documentation.findDocumentation(trimmedInput);
            return printResult(doc);
        }
        if (fSeconds <= 0) {
            result = fEvaluator.eval(trimmedInput);
        } else {
            result = fEvaluator.evaluateWithTimeout(trimmedInput, fSeconds, TimeUnit.SECONDS, true, new EvalControlledCallable(fEvaluator.getEvalEngine()));
        }
        if (result != null) {
            return printResult(result);
        }
    } catch (final AbortException re) {
        return printResult(S.$Aborted);
    } catch (final FailedException re) {
        return printResult(S.$Failed);
    } catch (final SyntaxError se) {
        String msg = se.getMessage();
        stderr.println(msg);
        // stderr.println();
        stderr.flush();
        return "";
    } catch (final RuntimeException re) {
        Throwable me = re.getCause();
        if (me instanceof MathException) {
            Validate.printException(buf, me);
        } else {
            Validate.printException(buf, re);
        }
        stderr.println(buf.toString());
        stderr.flush();
        return "";
    } catch (final Exception | OutOfMemoryError | StackOverflowError e) {
        Validate.printException(buf, e);
        stderr.println(buf.toString());
        stderr.flush();
        return "";
    }
    return buf.toString();
}
Also used : MathException(org.matheclipse.parser.client.math.MathException) AbortException(org.matheclipse.core.eval.exception.AbortException) FailedException(org.matheclipse.core.eval.exception.FailedException) ReturnException(org.matheclipse.core.eval.exception.ReturnException) IOException(java.io.IOException) StringWriter(java.io.StringWriter) SyntaxError(org.matheclipse.parser.client.SyntaxError) FailedException(org.matheclipse.core.eval.exception.FailedException) MathException(org.matheclipse.parser.client.math.MathException) EvalControlledCallable(org.matheclipse.core.eval.EvalControlledCallable) IExpr(org.matheclipse.core.interfaces.IExpr) AbortException(org.matheclipse.core.eval.exception.AbortException)

Aggregations

AbortException (org.matheclipse.core.eval.exception.AbortException)16 IExpr (org.matheclipse.core.interfaces.IExpr)14 FailedException (org.matheclipse.core.eval.exception.FailedException)8 Node (org.w3c.dom.Node)8 MathException (org.matheclipse.parser.client.math.MathException)7 StringWriter (java.io.StringWriter)6 ISymbol (org.matheclipse.core.interfaces.ISymbol)6 SyntaxError (org.matheclipse.parser.client.SyntaxError)6 EvalControlledCallable (org.matheclipse.core.eval.EvalControlledCallable)5 IOException (java.io.IOException)4 EvalEngine (org.matheclipse.core.eval.EvalEngine)2 ReturnException (org.matheclipse.core.eval.exception.ReturnException)2 ScriptException (javax.script.ScriptException)1 ServletException (javax.servlet.ServletException)1 Level (org.apache.logging.log4j.Level)1 ApfloatRuntimeException (org.apfloat.ApfloatRuntimeException)1 ExprEvaluator (org.matheclipse.core.eval.ExprEvaluator)1 RecursionLimitExceeded (org.matheclipse.core.eval.exception.RecursionLimitExceeded)1 BuiltInDummy (org.matheclipse.core.expression.BuiltInDummy)1 GraphExpr (org.matheclipse.core.expression.data.GraphExpr)1