Search in sources :

Example 1 with Apint

use of org.apfloat.Apint in project symja_android_library by axkr.

the class AST2Expr method convertNode.

/**
 * Converts a parsed ASTNode expression into a Symja IExpr expression
 *
 * @param node the parsed ASTNode
 * @return the Symja expression
 */
private IExpr convertNode(ASTNode node) {
    if (node == null) {
        return null;
    }
    if (node instanceof FunctionNode) {
        final FunctionNode functionNode = (FunctionNode) node;
        int size = functionNode.size();
        IASTMutable ast;
        switch(size) {
            case 1:
                ast = F.headAST0(convertNode(functionNode.get(0)));
                break;
            case 2:
                ast = F.unaryAST1(convertNode(functionNode.get(0)), convertNode(functionNode.get(1)));
                break;
            case 3:
                ast = F.binaryAST2(convertNode(functionNode.get(0)), convertNode(functionNode.get(1)), convertNode(functionNode.get(2)));
                break;
            case 4:
                ast = F.ternaryAST3(convertNode(functionNode.get(0)), convertNode(functionNode.get(1)), convertNode(functionNode.get(2)), convertNode(functionNode.get(3)));
                break;
            default:
                IASTAppendable appendableAST = F.ast(convertNode(functionNode.get(0)), functionNode.size());
                for (int i = 1; i < functionNode.size(); i++) {
                    appendableAST.append(convertNode(functionNode.get(i)));
                }
                ast = appendableAST;
        }
        int functionID = ast.headID();
        if (functionID > ID.UNKNOWN) {
            IExpr temp = evaluateOnInput(functionID, ast, functionNode);
            if (temp.isPresent()) {
                return temp;
            }
        }
        return ast;
    }
    if (node instanceof SymbolNode) {
        String nodeStr = node.getString();
        return convertSymbol(nodeStr);
    }
    // PatternNode
    if (node instanceof Pattern3Node) {
        final Pattern3Node p3n = (Pattern3Node) node;
        SymbolNode sn = p3n.getSymbol();
        return F.$ps((ISymbol) convertNode(sn), convertNode(p3n.getConstraint()), p3n.isDefault(), true);
    }
    if (node instanceof Pattern2Node) {
        final Pattern2Node p2n = (Pattern2Node) node;
        SymbolNode sn = p2n.getSymbol();
        return F.$ps((ISymbol) convertNode(sn), convertNode(p2n.getConstraint()), p2n.isDefault(), false);
    }
    if (node instanceof PatternNode) {
        final PatternNode pn = (PatternNode) node;
        SymbolNode sn = pn.getSymbol();
        if (sn == null) {
            return F.$b(convertNode(pn.getConstraint()), pn.isDefault());
        }
        ASTNode defaultValue = pn.getDefaultValue();
        if (defaultValue != null) {
            return F.Optional(F.$p((ISymbol) convertNode(pn.getSymbol()), convertNode(pn.getConstraint())), convertNode(defaultValue));
        }
        return F.$p((ISymbol) convertNode(pn.getSymbol()), convertNode(pn.getConstraint()), pn.isDefault());
    }
    if (node instanceof IntegerNode) {
        final IntegerNode integerNode = (IntegerNode) node;
        final String iStr = integerNode.getString();
        if (iStr != null) {
            return F.ZZ(iStr, integerNode.getNumberFormat());
        }
        return F.ZZ(integerNode.getIntValue());
    }
    if (node instanceof FractionNode) {
        FractionNode fr = (FractionNode) node;
        IInteger numerator = (IInteger) convertNode(fr.getNumerator());
        IInteger denominator = (IInteger) convertNode(fr.getDenominator());
        if (denominator.isZero()) {
            return F.Rational(fr.isSign() ? numerator.negate() : numerator, denominator);
        }
        if (denominator.isOne()) {
            return fr.isSign() ? numerator.negate() : numerator;
        }
        // return F.Rational(fr.isSign() ? numerator.negate() : numerator, denominator);
        return F.fraction(fr.isSign() ? numerator.negate() : numerator, 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.symbol(node.toString());
}
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) IASTMutable(org.matheclipse.core.interfaces.IASTMutable) FractionNode(org.matheclipse.parser.client.ast.FractionNode) Apint(org.apfloat.Apint) Apfloat(org.apfloat.Apfloat) SymbolNode(org.matheclipse.parser.client.ast.SymbolNode) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) PatternNode(org.matheclipse.parser.client.ast.PatternNode) IInteger(org.matheclipse.core.interfaces.IInteger) ASTNode(org.matheclipse.parser.client.ast.ASTNode) StringNode(org.matheclipse.parser.client.ast.StringNode) IExpr(org.matheclipse.core.interfaces.IExpr) Pattern3Node(org.matheclipse.parser.client.ast.Pattern3Node)

Example 2 with Apint

use of org.apfloat.Apint in project symja_android_library by axkr.

the class ExprParser method getReal.

private static INum getReal(String str) {
    int index = str.indexOf("*^");
    int fExponent = 1;
    String fFloatStr = str;
    if (index > 0) {
        fFloatStr = str.substring(0, index);
        fExponent = Integer.parseInt(str.substring(index + 2));
    }
    if (fFloatStr.length() > 15) {
        int precision = fFloatStr.length();
        Apfloat apfloatValue = new Apfloat(fFloatStr, precision);
        if (fExponent != 1) {
            // value * 10 ^ exponent
            return F.num(apfloatValue.multiply(ApfloatMath.pow(new Apfloat(10, precision), new Apint(fExponent))));
        }
        return F.num(apfloatValue);
    }
    double fDouble = Double.parseDouble(fFloatStr);
    if (fExponent != 1) {
        // value * 10 ^ exponent
        fDouble = fDouble * Math.pow(10, fExponent);
    }
    return new NumStr(fFloatStr, fExponent);
}
Also used : NumStr(org.matheclipse.core.expression.NumStr) Apint(org.apfloat.Apint) Apint(org.apfloat.Apint) Apfloat(org.apfloat.Apfloat)

Example 3 with Apint

use of org.apfloat.Apint 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)

Aggregations

Apfloat (org.apfloat.Apfloat)3 Apint (org.apfloat.Apint)3 IExpr (org.matheclipse.core.interfaces.IExpr)2 IInteger (org.matheclipse.core.interfaces.IInteger)2 ISymbol (org.matheclipse.core.interfaces.ISymbol)2 ASTNode (org.matheclipse.parser.client.ast.ASTNode)2 FloatNode (org.matheclipse.parser.client.ast.FloatNode)2 FractionNode (org.matheclipse.parser.client.ast.FractionNode)2 FunctionNode (org.matheclipse.parser.client.ast.FunctionNode)2 IntegerNode (org.matheclipse.parser.client.ast.IntegerNode)2 Pattern2Node (org.matheclipse.parser.client.ast.Pattern2Node)2 Pattern3Node (org.matheclipse.parser.client.ast.Pattern3Node)2 PatternNode (org.matheclipse.parser.client.ast.PatternNode)2 StringNode (org.matheclipse.parser.client.ast.StringNode)2 SymbolNode (org.matheclipse.parser.client.ast.SymbolNode)2 DoubleNode (org.matheclipse.parser.client.eval.DoubleNode)2 WrongArgumentType (org.matheclipse.core.eval.exception.WrongArgumentType)1 NumStr (org.matheclipse.core.expression.NumStr)1 IAST (org.matheclipse.core.interfaces.IAST)1 IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)1