Search in sources :

Example 31 with ASTNode

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

the class Parser method getPart.

/**
	 * Get a <i>part [[..]]</i> of an expression <code>{a,b,c}[[2]]</code> &rarr; <code>b</code>
	 * 
	 */
private ASTNode getPart() throws SyntaxError {
    ASTNode temp = getFactor();
    if (fToken != TT_PARTOPEN) {
        return temp;
    }
    FunctionNode function = null;
    do {
        if (function == null) {
            function = fFactory.createFunction(fFactory.createSymbol(IConstantOperators.Part), temp);
        } else {
            function = fFactory.createFunction(fFactory.createSymbol(IConstantOperators.Part), function);
        }
        fRecursionDepth++;
        try {
            do {
                getNextToken();
                if (fToken == TT_ARGUMENTS_CLOSE) {
                    if (fInputString.length() > fCurrentPosition && fInputString.charAt(fCurrentPosition) == ']') {
                        throwSyntaxError("Statement (i.e. index) expected in [[ ]].");
                    }
                }
                function.add(parseExpression());
            } while (fToken == TT_COMMA);
            if (fToken == TT_ARGUMENTS_CLOSE) {
                // scanner-step begin: (instead of getNextToken() call):
                if (fInputString.length() > fCurrentPosition) {
                    if (fInputString.charAt(fCurrentPosition) == ']') {
                        fCurrentPosition++;
                        fToken = TT_PARTCLOSE;
                    }
                }
            // scanner-step end
            }
            if (fToken != TT_PARTCLOSE) {
                throwSyntaxError("']]' expected.");
            }
        } finally {
            fRecursionDepth--;
        }
        getNextToken();
    } while (fToken == TT_PARTOPEN);
    return parseArguments(function);
}
Also used : ASTNode(org.matheclipse.parser.client.ast.ASTNode) FunctionNode(org.matheclipse.parser.client.ast.FunctionNode)

Example 32 with ASTNode

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

the class DoubleEvaluator method optimizeFunction.

/**
	 * Optimize an already parsed in <code>functionNode</code> into an
	 * <code>ASTNode</code>.
	 * 
	 * @param functionNode
	 * @return
	 * 
	 */
public ASTNode optimizeFunction(final FunctionNode functionNode) {
    if (functionNode.size() > 0) {
        boolean doubleOnly = true;
        ASTNode node;
        for (int i = 1; i < functionNode.size(); i++) {
            node = functionNode.getNode(i);
            if (node instanceof NumberNode) {
                functionNode.set(i, new DoubleNode(((NumberNode) functionNode.getNode(i)).doubleValue()));
            } else if (functionNode.getNode(i) instanceof FunctionNode) {
                ASTNode optNode = optimizeFunction((FunctionNode) functionNode.getNode(i));
                if (!(optNode instanceof DoubleNode)) {
                    doubleOnly = false;
                }
                functionNode.set(i, optNode);
            } else if (node instanceof SymbolNode) {
                Double dbl = SYMBOL_DOUBLE_MAP.get(node.toString());
                if (dbl != null) {
                    functionNode.set(i, new DoubleNode(dbl.doubleValue()));
                } else {
                    doubleOnly = false;
                }
            } else {
                doubleOnly = false;
            }
        }
        if (doubleOnly) {
            try {
                return new DoubleNode(evaluateFunction(functionNode));
            } catch (Exception e) {
            }
        }
    }
    return functionNode;
}
Also used : SymbolNode(org.matheclipse.parser.client.ast.SymbolNode) NumberNode(org.matheclipse.parser.client.ast.NumberNode) ASTNode(org.matheclipse.parser.client.ast.ASTNode) FunctionNode(org.matheclipse.parser.client.ast.FunctionNode) ArithmeticMathException(org.matheclipse.parser.client.math.ArithmeticMathException)

Example 33 with ASTNode

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

the class Get method loadPackage.

/**
	 * Load a package from the given reader
	 * 
	 * @param engine
	 * @param is
	 * @return the last evaluated expression result
	 */
public static IExpr loadPackage(final EvalEngine engine, final Reader is) {
    final BufferedReader r = new BufferedReader(is);
    Context packageContext = null;
    try {
        final List<ASTNode> node = parseReader(r, engine);
        IExpr temp;
        int i = 0;
        AST2Expr ast2Expr = AST2Expr.CONST;
        if (engine.isRelaxedSyntax()) {
            ast2Expr = AST2Expr.CONST_LC;
        }
        IExpr result = F.Null;
        while (i < node.size()) {
            temp = ast2Expr.convert(node.get(i++), engine);
            if (temp.isAST()) {
                IAST ast = (IAST) temp;
                IExpr head = temp.head();
                if (head.equals(F.BeginPackage) && ast.size() >= 2) {
                    String contextName = Validate.checkContextName(ast, 1);
                    packageContext = new Context(contextName);
                    ISymbol endSymbol = F.EndPackage;
                    for (int j = 2; j < ast.size(); j++) {
                        FileReader reader = new FileReader(ast.get(j).toString());
                        Get.loadPackage(engine, reader);
                        reader.close();
                    }
                    i = addContextToPath(new ContextPath(packageContext), node, i, engine, endSymbol);
                    continue;
                } else if (head.equals(F.Begin) && ast.size() >= 2) {
                    String contextName = Validate.checkContextName(ast, 1);
                    ISymbol endSymbol = F.End;
                    i = addContextToPath(new ContextPath(contextName), node, i, engine, endSymbol);
                    continue;
                }
            }
            result = engine.evaluate(temp);
        }
        return result;
    } catch (final Exception e) {
        e.printStackTrace();
    } finally {
        if (packageContext != null) {
            engine.getContextPath().add(packageContext);
        }
        try {
            r.close();
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return F.Null;
}
Also used : Context(org.matheclipse.core.expression.Context) ISymbol(org.matheclipse.core.interfaces.ISymbol) IOException(java.io.IOException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) AST2Expr(org.matheclipse.core.convert.AST2Expr) ContextPath(org.matheclipse.core.expression.ContextPath) BufferedReader(java.io.BufferedReader) ASTNode(org.matheclipse.parser.client.ast.ASTNode) FileReader(java.io.FileReader) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

Example 34 with ASTNode

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

the class MathUtils method getFunctionVal.

public static double getFunctionVal(String f, String v, String x) {
    // StringBuilder command = new StringBuilder();
    // command.append("ReplaceAll(");
    // command.append(f);
    // command.append(",");
    // command.append(v);
    // command.append("-> (");
    // command.append(x);
    // command.append(")");
    // command.append(")");
    // String result = evaluate(command.toString(), "N");
    //
    // EvalDouble dEval = new EvalDouble(true);
    // return dEval.evaluate(result);
    // Variable var = new Variable(v);
    // Expression fun,val;
    // Parser parser = new Parser(Parser.STANDARD_FUNCTIONS |
    // Parser.OPTIONAL_PARENS
    // | Parser.OPTIONAL_STARS | Parser.OPTIONAL_SPACES
    // | Parser.BRACES | Parser.BRACKETS| Parser.BOOLEANS);
    // parser.add(var);
    // setUpParser(parser);
    EvalDouble parser = new EvalDouble(true);
    String var = v;
    ASTNode fun, val;
    parser.defineVariable(var);
    try {
        fun = parser.parse(f);
    } catch (MathException e) {
        // + e.getMessage(), e.context);
        throw e;
    }
    try {
        val = parser.parse(x);
    } catch (MathException e) {
        // e.getMessage(), e.context);
        throw e;
    }
    // var.setVal(val.getVal());
    parser.defineVariable(var, parser.evaluateNode(val));
    return parser.evaluateNode(fun);
}
Also used : MathException(org.matheclipse.parser.client.math.MathException) ASTNode(org.matheclipse.parser.client.ast.ASTNode)

Example 35 with ASTNode

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

the class MathUtils method getFunctionVal.

public static String getFunctionVal(String fun, String[] var, String resp, String[] vals) throws MathException {
    // return evaluate(command.toString(), null);
    try {
        EvalDouble parParser = new EvalDouble(true);
        double[] values = new double[vals.length];
        for (int i = 0; i < vals.length; i++) {
            values[i] = parParser.evaluate(vals[i]);
        }
        String respVar = null;
        for (int i = 0; i < var.length; i++) {
            if (var[i].equals(resp)) {
                respVar = resp;
                // parParser.add(respVar);
                // respVar.setVal(values[i]);
                parParser.defineVariable(respVar, values[i]);
            } else {
                String temp = var[i];
                parParser.defineVariable(temp, values[i]);
            }
        }
        if (respVar != null) {
            try {
                ASTNode f = parParser.parse(fun);
                return parParser.evaluateNode(f) + "";
            } catch (MathException e) {
                // e.getMessage(), e.context);
                throw e;
            }
        }
    } catch (MathException e) {
        // ParserContext(resp, 0, null));
        throw e;
    }
    throw new MathException("MathUtils:getFunctionVal - cannot compute function values");
}
Also used : MathException(org.matheclipse.parser.client.math.MathException) ASTNode(org.matheclipse.parser.client.ast.ASTNode)

Aggregations

ASTNode (org.matheclipse.parser.client.ast.ASTNode)56 Parser (org.matheclipse.parser.client.Parser)32 IExpr (org.matheclipse.core.interfaces.IExpr)10 FunctionNode (org.matheclipse.parser.client.ast.FunctionNode)8 MathException (org.matheclipse.parser.client.math.MathException)6 PatternMatcher (org.matheclipse.core.patternmatching.PatternMatcher)5 SymbolNode (org.matheclipse.parser.client.ast.SymbolNode)5 IAST (org.matheclipse.core.interfaces.IAST)4 IOException (java.io.IOException)3 AST2Expr (org.matheclipse.core.convert.AST2Expr)3 NumberNode (org.matheclipse.parser.client.ast.NumberNode)3 FileReader (java.io.FileReader)2 ISymbol (org.matheclipse.core.interfaces.ISymbol)2 ArithmeticMathException (org.matheclipse.parser.client.math.ArithmeticMathException)2 InfixOperator (org.matheclipse.parser.client.operator.InfixOperator)2 PostfixOperator (org.matheclipse.parser.client.operator.PostfixOperator)2 PrefixOperator (org.matheclipse.parser.client.operator.PrefixOperator)2 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1