Search in sources :

Example 11 with FunctionNode

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

the class InfixOperator method createFunction.

public ASTNode createFunction(final IParserFactory factory, final ASTNode lhs, final ASTNode rhs) {
    if (fOperatorString.equals("//")) {
        //   lhs // rhs ==> rhs[lhs]
        FunctionNode function = factory.createAST(rhs);
        function.add(lhs);
        return function;
    }
    return factory.createFunction(factory.createSymbol(getFunctionName()), lhs, rhs);
}
Also used : FunctionNode(org.matheclipse.parser.client.ast.FunctionNode)

Example 12 with FunctionNode

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

the class AST2Expr method convertNode.

/**
	 * Converts a parsed ASTNode expression into an IExpr expression
	 * 
	 * @param engine
	 *            TODO
	 */
private IExpr convertNode(ASTNode node, EvalEngine engine) throws ConversionException {
    if (node == null) {
        return null;
    }
    if (node instanceof FunctionNode) {
        final FunctionNode functionNode = (FunctionNode) node;
        int size = functionNode.size();
        IAST ast;
        switch(size) {
            case 1:
                ast = F.headAST0(convertNode(functionNode.get(0), engine));
                break;
            case 2:
                ast = F.unaryAST1(convertNode(functionNode.get(0), engine), convertNode(functionNode.get(1), engine));
                break;
            case 3:
                ast = F.binaryAST2(convertNode(functionNode.get(0), engine), convertNode(functionNode.get(1), engine), convertNode(functionNode.get(2), engine));
                break;
            case 4:
                ast = F.ternaryAST3(convertNode(functionNode.get(0), engine), convertNode(functionNode.get(1), engine), convertNode(functionNode.get(2), engine), convertNode(functionNode.get(3), engine));
                break;
            default:
                ast = F.ast(convertNode(functionNode.get(0), engine), functionNode.size(), false);
                for (int i = 1; i < functionNode.size(); i++) {
                    ast.append(convertNode(functionNode.get(i), engine));
                }
        }
        IExpr head = ast.head();
        if (ast.isAST(F.N, 3)) {
            try {
                int precision = Validate.checkIntType(ast.arg2());
                if (EvalEngine.isApfloat(precision)) {
                    fPrecision = precision;
                    ast.set(1, convertNode(functionNode.get(1), engine));
                }
                return ast;
            } catch (WrongArgumentType wat) {
            }
        } else if (ast.isAST(F.Sqrt, 2)) {
            // rewrite from input: Sqrt(x) => Power(x, 1/2)
            return F.Power(ast.arg1(), F.C1D2);
        } else if (ast.isAST(F.Exp, 2)) {
            // rewrite from input: Exp(x) => E^x
            return F.Power(F.E, ast.arg1());
        } else if (ast.isPower() && ast.arg1().isPower() && ast.arg2().isMinusOne()) {
            IAST arg1 = (IAST) ast.arg1();
            if (arg1.arg2().isNumber()) {
                // Power(x, - <number>)
                return F.Power(arg1.arg1(), ((INumber) arg1.arg2()).negate());
            }
        } else if (ast.isASTSizeGE(F.GreaterEqual, 3)) {
            ISymbol compareHead = F.Greater;
            return rewriteLessGreaterAST(ast, compareHead);
        } else if (ast.isASTSizeGE(F.Greater, 3)) {
            ISymbol compareHead = F.GreaterEqual;
            return rewriteLessGreaterAST(ast, compareHead);
        } else if (ast.isASTSizeGE(F.LessEqual, 3)) {
            ISymbol compareHead = F.Less;
            return rewriteLessGreaterAST(ast, compareHead);
        } else if (ast.isASTSizeGE(F.Less, 3)) {
            ISymbol compareHead = F.LessEqual;
            return rewriteLessGreaterAST(ast, compareHead);
        } else if (head.equals(F.PatternHead)) {
            final IExpr expr = Pattern.CONST.evaluate(ast, engine);
            if (expr.isPresent()) {
                return expr;
            }
        } else if (head.equals(F.BlankHead)) {
            final IExpr expr = Blank.CONST.evaluate(ast, engine);
            if (expr.isPresent()) {
                return expr;
            }
        } else if (head.equals(F.Complex)) {
            final IExpr expr = Arithmetic.CONST_COMPLEX.evaluate(ast, engine);
            if (expr.isPresent()) {
                return expr;
            }
        } else if (head.equals(F.Rational)) {
            final IExpr expr = Arithmetic.CONST_RATIONAL.evaluate(ast, engine);
            if (expr.isPresent()) {
                return expr;
            }
        }
        return ast;
    }
    if (node instanceof SymbolNode) {
        String nodeStr = node.getString();
        return convertSymbol(nodeStr, engine);
    }
    // PatternNode
    if (node instanceof Pattern3Node) {
        final Pattern3Node p3n = (Pattern3Node) node;
        SymbolNode sn = p3n.getSymbol();
        return F.$ps((ISymbol) convertNode(sn, engine), convertNode(p3n.getConstraint(), engine), p3n.isDefault(), true);
    }
    if (node instanceof Pattern2Node) {
        final Pattern2Node p2n = (Pattern2Node) node;
        SymbolNode sn = p2n.getSymbol();
        return F.$ps((ISymbol) convertNode(sn, engine), convertNode(p2n.getConstraint(), engine), p2n.isDefault(), false);
    }
    if (node instanceof PatternNode) {
        final PatternNode pn = (PatternNode) node;
        SymbolNode sn = pn.getSymbol();
        if (sn == null) {
            // TODO ,
            return F.$b(convertNode(pn.getConstraint(), engine));
        // p2n.isDefault());
        }
        ASTNode defaultValue = pn.getDefaultValue();
        if (defaultValue != null) {
            return F.$p((ISymbol) convertNode(pn.getSymbol(), engine), convertNode(pn.getConstraint(), engine), convertNode(defaultValue, engine));
        }
        return F.$p((ISymbol) convertNode(pn.getSymbol(), engine), convertNode(pn.getConstraint(), engine), pn.isDefault());
    }
    if (node instanceof IntegerNode) {
        final IntegerNode integerNode = (IntegerNode) node;
        final String iStr = integerNode.getString();
        if (iStr != null) {
            return F.integer(iStr, integerNode.getNumberFormat());
        }
        return F.integer(integerNode.getIntValue());
    }
    if (node instanceof FractionNode) {
        FractionNode fr = (FractionNode) node;
        IInteger numerator = (IInteger) convertNode(fr.getNumerator(), engine);
        IInteger denominator = (IInteger) convertNode(fr.getDenominator(), engine);
        if (denominator.isZero()) {
            return F.Rational(fr.isSign() ? numerator.negate() : numerator, denominator);
        }
        return F.Rational(fr.isSign() ? numerator.negate() : numerator, denominator);
    // return F.fraction(numerator, fr.isSign() ? (IInteger) denominator.negate() : denominator);
    }
    if (node instanceof StringNode) {
        return F.$str(node.getString());
    }
    if (node instanceof FloatNode) {
        String nStr = node.getString();
        String floatStr = nStr;
        int index = nStr.indexOf("*^");
        int exponent = 1;
        if (index > 0) {
            floatStr = nStr.substring(0, index);
            exponent = Integer.parseInt(nStr.substring(index + 2));
        }
        if (EvalEngine.isApfloat(fPrecision)) {
            Apfloat apfloatValue = new Apfloat(floatStr, fPrecision);
            if (exponent != 1) {
                // value * 10 ^ exponent
                return F.num(apfloatValue.multiply(ApfloatMath.pow(new Apint(10), new Apint(exponent))));
            }
            return F.num(apfloatValue);
        }
        double doubleValue = Double.parseDouble(floatStr);
        if (exponent != 1) {
            // value * 10 ^ exponent
            return F.num(doubleValue * Math.pow(10, exponent));
        }
        return F.num(doubleValue);
    }
    if (node instanceof DoubleNode) {
        return F.num(((DoubleNode) node).doubleValue());
    }
    return F.userSymbol(node.toString(), engine);
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) IntegerNode(org.matheclipse.parser.client.ast.IntegerNode) FunctionNode(org.matheclipse.parser.client.ast.FunctionNode) Pattern2Node(org.matheclipse.parser.client.ast.Pattern2Node) FloatNode(org.matheclipse.parser.client.ast.FloatNode) Apint(org.apfloat.Apint) DoubleNode(org.matheclipse.parser.client.eval.DoubleNode) FractionNode(org.matheclipse.parser.client.ast.FractionNode) Apint(org.apfloat.Apint) Apfloat(org.apfloat.Apfloat) SymbolNode(org.matheclipse.parser.client.ast.SymbolNode) WrongArgumentType(org.matheclipse.core.eval.exception.WrongArgumentType) PatternNode(org.matheclipse.parser.client.ast.PatternNode) INumber(org.matheclipse.core.interfaces.INumber) IInteger(org.matheclipse.core.interfaces.IInteger) ASTNode(org.matheclipse.parser.client.ast.ASTNode) StringNode(org.matheclipse.parser.client.ast.StringNode) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr) Pattern3Node(org.matheclipse.parser.client.ast.Pattern3Node)

Example 13 with FunctionNode

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

the class CoreCallbackFunction method evaluate.

@Override
public double evaluate(DoubleEvaluator doubleEngine, FunctionNode functionNode, double[] args) {
    ASTNode node = functionNode.getNode(0);
    if (node instanceof SymbolNode) {
        AST2Expr ast2Expr = new AST2Expr();
        IExpr head = ast2Expr.convert(node);
        IAST fun = F.ast(head);
        for (int i = 0; i < args.length; i++) {
            fun.append(F.num(args[i]));
        }
        final IExpr result = F.evaln(fun);
        if (result.isSignedNumber()) {
            return ((ISignedNumber) result).doubleValue();
        }
    } else if (node instanceof FunctionNode) {
        AST2Expr ast2Expr = new AST2Expr();
        IExpr head = ast2Expr.convert(node);
        IAST fun = F.ast(head);
        for (int i = 0; i < args.length; i++) {
            fun.append(F.num(args[i]));
        }
        final IExpr result = F.evaln(fun);
        if (result.isSignedNumber()) {
            return ((ISignedNumber) result).doubleValue();
        }
    }
    throw new MathException("CoreCallbackFunction#evaluate() not possible for: " + functionNode.toString());
}
Also used : SymbolNode(org.matheclipse.parser.client.ast.SymbolNode) ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) MathException(org.matheclipse.parser.client.math.MathException) ASTNode(org.matheclipse.parser.client.ast.ASTNode) FunctionNode(org.matheclipse.parser.client.ast.FunctionNode) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) AST2Expr(org.matheclipse.core.convert.AST2Expr)

Example 14 with FunctionNode

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

the class DoubleEvaluator method evaluate.

/**
	 * Parse the given <code>expression String</code> and evaluate it to a
	 * double value
	 * 
	 * @param expression
	 * @return
	 * @throws SyntaxError
	 */
public double evaluate(String expression) {
    Parser p;
    if (fRelaxedSyntax) {
        p = new Parser(ASTNodeFactory.RELAXED_STYLE_FACTORY, true);
    } else {
        p = new Parser(ASTNodeFactory.MMA_STYLE_FACTORY, false);
    }
    fNode = p.parse(expression);
    if (fNode instanceof FunctionNode) {
        fNode = optimizeFunction((FunctionNode) fNode);
    }
    return evaluateNode(fNode);
}
Also used : FunctionNode(org.matheclipse.parser.client.ast.FunctionNode) Parser(org.matheclipse.parser.client.Parser)

Example 15 with FunctionNode

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

the class Parser method parseDerivative.

private ASTNode parseDerivative(ASTNode expr) {
    int derivativeCounter = 1;
    getNextToken();
    while (fToken == TT_DERIVATIVE) {
        derivativeCounter++;
        getNextToken();
    }
    FunctionNode head = fFactory.createFunction(DERIVATIVE, new IntegerNode(derivativeCounter));
    FunctionNode deriv = fFactory.createAST(head);
    deriv.add(expr);
    expr = parseArguments(deriv);
    return expr;
}
Also used : IntegerNode(org.matheclipse.parser.client.ast.IntegerNode) FunctionNode(org.matheclipse.parser.client.ast.FunctionNode)

Aggregations

FunctionNode (org.matheclipse.parser.client.ast.FunctionNode)18 ASTNode (org.matheclipse.parser.client.ast.ASTNode)8 SymbolNode (org.matheclipse.parser.client.ast.SymbolNode)6 NumberNode (org.matheclipse.parser.client.ast.NumberNode)3 IAST (org.matheclipse.core.interfaces.IAST)2 IExpr (org.matheclipse.core.interfaces.IExpr)2 Parser (org.matheclipse.parser.client.Parser)2 IntegerNode (org.matheclipse.parser.client.ast.IntegerNode)2 ArithmeticMathException (org.matheclipse.parser.client.math.ArithmeticMathException)2 PrefixOperator (org.matheclipse.parser.client.operator.PrefixOperator)2 Apfloat (org.apfloat.Apfloat)1 Apint (org.apfloat.Apint)1 AST2Expr (org.matheclipse.core.convert.AST2Expr)1 WrongArgumentType (org.matheclipse.core.eval.exception.WrongArgumentType)1 IInteger (org.matheclipse.core.interfaces.IInteger)1 INumber (org.matheclipse.core.interfaces.INumber)1 ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)1 ISymbol (org.matheclipse.core.interfaces.ISymbol)1 FloatNode (org.matheclipse.parser.client.ast.FloatNode)1 FractionNode (org.matheclipse.parser.client.ast.FractionNode)1