Search in sources :

Example 16 with Apfloat

use of org.apfloat.Apfloat 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 17 with Apfloat

use of org.apfloat.Apfloat 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 18 with Apfloat

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

the class ExpressionJSONConvert method exportExpressionJSON.

/**
 * Export an expression to <code>ExpressionJSON</code> format.
 *
 * @param expr
 * @return
 * @throws IOException
 * @throws JsonGenerationException
 * @throws JsonMappingException
 */
public static JsonNode exportExpressionJSON(IExpr expr) throws IOException, JsonGenerationException, JsonMappingException {
    if (expr.isASTOrAssociation()) {
        IAST ast = (IAST) expr;
        ArrayNode temp = JSON_OBJECT_MAPPER.createArrayNode();
        temp.add(ast.head().toString());
        for (int i = 1; i < ast.size(); i++) {
            IExpr arg = ast.getRule(i);
            if (arg.isComplexNumeric()) {
                IComplexNum complexNum = (IComplexNum) arg;
                ArrayNode complexJson = JSON_OBJECT_MAPPER.createArrayNode();
                complexJson.add("Complex");
                complexJson.add(complexNum.reDoubleValue());
                complexJson.add(complexNum.imDoubleValue());
                temp.add(complexJson);
            } else if (arg instanceof Num) {
                temp.add(((Num) arg).doubleValue());
            } else if (arg instanceof ApfloatNum) {
                Apfloat apfloatValue = ((ApfloatNum) arg).apfloatValue();
                if (apfloatValue.precision() > 20L) {
                    temp.add(apfloatValue.toString());
                } else {
                    temp.add(apfloatValue.doubleValue());
                }
            } else if (arg.isNumber() || arg.isSymbol()) {
                temp.add(arg.toString());
            } else if (arg.isString()) {
                temp.add("'" + arg.toString() + "'");
            } else {
                temp.add(exportExpressionJSON(arg));
            }
        }
        return temp;
    }
    ArrayNode temp = JSON_OBJECT_MAPPER.createArrayNode();
    temp.add(temp.toString());
    return temp;
}
Also used : IComplexNum(org.matheclipse.core.interfaces.IComplexNum) Num(org.matheclipse.core.expression.Num) IComplexNum(org.matheclipse.core.interfaces.IComplexNum) ApfloatNum(org.matheclipse.core.expression.ApfloatNum) IAST(org.matheclipse.core.interfaces.IAST) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) IExpr(org.matheclipse.core.interfaces.IExpr) ApfloatNum(org.matheclipse.core.expression.ApfloatNum) Apfloat(org.apfloat.Apfloat)

Example 19 with Apfloat

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

the class IntervalSym method normalizeArgument.

/**
 * If the argument is a list of 2 elements, try sorting the elements. If the argument is not a
 * list return a new <code>{argOfIntervalList, argOfIntervalList]</code>
 *
 * @param arg
 * @param engine
 * @return
 */
private static IAST normalizeArgument(final IExpr arg, final EvalEngine engine) {
    if (arg.isList()) {
        if (arg.size() == 3) {
            IAST list = (IAST) arg;
            IExpr arg1 = list.arg1();
            IExpr arg2 = list.arg2();
            if (arg1.isReal() && arg2.isReal()) {
                if (arg1.greaterThan(arg2).isTrue()) {
                    return F.list(arg2, arg1);
                }
                return F.NIL;
            }
            IExpr min = arg1.isNumber() ? arg1 : engine.evaluate(arg1);
            IExpr max = arg2.isNumber() ? arg2 : engine.evaluate(arg2);
            if (min.isRealResult() && max.isRealResult()) {
                if (min.greaterThan(max).isTrue()) {
                    return F.list(max, min);
                }
            }
            return F.NIL;
        }
        // The expression `1` is not a valid interval.
        String str = IOFunctions.getMessage("nvld", F.list(arg), engine);
        throw new ArgumentTypeException(str);
    }
    if (arg instanceof INum) {
        if (arg instanceof ApfloatNum) {
            Apfloat apfloat = ((ApfloatNum) arg).fApfloat;
            Apfloat[] values = interval(apfloat);
            return // 
            F.list(// 
            F.num(values[0]), F.num(values[1]));
        }
        double value = ((ISignedNumber) arg).doubleValue();
        return // 
        F.list(// 
        F.num(Math.nextDown(value)), F.num(Math.nextUp(value)));
    }
    return F.list(arg, arg);
}
Also used : ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr) Apfloat(org.apfloat.Apfloat) ArgumentTypeException(org.matheclipse.core.eval.exception.ArgumentTypeException) INum(org.matheclipse.core.interfaces.INum)

Example 20 with Apfloat

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

the class ApfloatJUnit method testApfloatDivide.

public void testApfloatDivide() {
    EvalEngine.setApfloat(new FixedPrecisionApfloatHelper(30));
    IComplexNum cnum1 = F.complexNum(new Apfloat(Long.MIN_VALUE), new Apfloat(Long.MAX_VALUE));
    INum cnum2 = F.num(new Apfloat(Long.MIN_VALUE));
    IExpr value = S.Divide.of(cnum2, cnum1);
    assertEquals(// 
    value.toString(), "(5.00000000000000000054210108624*10^-1, 4.99999999999999999999999999999*10^-1)");
}
Also used : FixedPrecisionApfloatHelper(org.apfloat.FixedPrecisionApfloatHelper) IComplexNum(org.matheclipse.core.interfaces.IComplexNum) IExpr(org.matheclipse.core.interfaces.IExpr) Apfloat(org.apfloat.Apfloat) INum(org.matheclipse.core.interfaces.INum)

Aggregations

Apfloat (org.apfloat.Apfloat)29 IExpr (org.matheclipse.core.interfaces.IExpr)8 FixedPrecisionApfloatHelper (org.apfloat.FixedPrecisionApfloatHelper)7 Apcomplex (org.apfloat.Apcomplex)4 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)3 Apint (org.apfloat.Apint)3 IAST (org.matheclipse.core.interfaces.IAST)3 IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)3 IComplexNum (org.matheclipse.core.interfaces.IComplexNum)3 IInteger (org.matheclipse.core.interfaces.IInteger)3 INum (org.matheclipse.core.interfaces.INum)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 BigIntegerNode (com.fasterxml.jackson.databind.node.BigIntegerNode)2 BooleanNode (com.fasterxml.jackson.databind.node.BooleanNode)2 DecimalNode (com.fasterxml.jackson.databind.node.DecimalNode)2 DoubleNode (com.fasterxml.jackson.databind.node.DoubleNode)2 FloatNode (com.fasterxml.jackson.databind.node.FloatNode)2 IntNode (com.fasterxml.jackson.databind.node.IntNode)2 LongNode (com.fasterxml.jackson.databind.node.LongNode)2 NullNode (com.fasterxml.jackson.databind.node.NullNode)2