Search in sources :

Example 51 with IASTAppendable

use of org.matheclipse.core.interfaces.IASTAppendable in project symja_android_library by axkr.

the class JASIExpr method complexPoly2Expr.

/**
 * Convert a JAS complex polynomial to <code>IExpr</code>.
 *
 * @param poly
 * @return
 * @throws ArithmeticException
 * @throws ClassCastException
 */
public IExpr complexPoly2Expr(final GenPolynomial<Complex<BigRational>> poly) throws ArithmeticException, ClassCastException {
    if (poly.length() == 0) {
        return F.C0;
    }
    IASTAppendable result = F.PlusAlloc(poly.length());
    for (Monomial<Complex<BigRational>> monomial : poly) {
        Complex<BigRational> coeff = monomial.coefficient();
        ExpVector exp = monomial.exponent();
        BigRational re = coeff.getRe();
        BigRational im = coeff.getIm();
        IASTAppendable monomTimes = F.Times(F.complex(F.fraction(re.numerator(), re.denominator()), F.fraction(im.numerator(), im.denominator())));
        long lExp;
        for (int i = 0; i < exp.length(); i++) {
            lExp = exp.getVal(i);
            if (lExp != 0) {
                monomTimes.append(F.Power(fVariables.get(i), F.ZZ(lExp)));
            }
        }
        if (monomTimes.isAST1()) {
            result.append(monomTimes.arg1());
        } else {
            result.append(monomTimes);
        }
    }
    if (result.isAST1()) {
        return result.arg1();
    } else {
        return result;
    }
}
Also used : IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) BigRational(edu.jas.arith.BigRational) ExpVector(edu.jas.poly.ExpVector) Complex(edu.jas.poly.Complex)

Example 52 with IASTAppendable

use of org.matheclipse.core.interfaces.IASTAppendable in project symja_android_library by axkr.

the class JSONConvert method importJSONRecursive.

public static IExpr importJSONRecursive(JsonNode node) {
    if (node instanceof ArrayNode) {
        ArrayNode arrayNode = (ArrayNode) node;
        Iterator<JsonNode> iter = arrayNode.elements();
        IASTAppendable list = F.ListAlloc(arrayNode.size());
        while (iter.hasNext()) {
            JsonNode next = iter.next();
            IExpr temp = importJSONRecursive(next);
            if (temp.isPresent()) {
                list.append(temp);
            }
        }
        return list;
    } else if (node instanceof ObjectNode) {
        IASTAppendable list = F.ListAlloc();
        ObjectNode objectNode = (ObjectNode) node;
        Iterator<Entry<String, JsonNode>> iter = objectNode.fields();
        while (iter.hasNext()) {
            Entry<String, JsonNode> next = iter.next();
            IExpr temp = importJSONRecursive(next.getValue());
            if (temp.isPresent()) {
                list.append(F.Rule(F.$str(next.getKey()), temp));
            }
        }
        return list;
    } else if (node instanceof ValueNode) {
        ValueNode valueNode = (ValueNode) node;
        if (valueNode instanceof NumericNode) {
            if (valueNode instanceof DoubleNode) {
                return F.num(valueNode.doubleValue());
            } else if (valueNode instanceof FloatNode) {
                return F.num(valueNode.doubleValue());
            } else if (valueNode instanceof IntNode) {
                return F.ZZ(valueNode.intValue());
            } else if (valueNode instanceof LongNode) {
                return F.ZZ(valueNode.longValue());
            } else if (valueNode instanceof ShortNode) {
                return F.ZZ(valueNode.intValue());
            } else if (valueNode instanceof BigIntegerNode) {
                return F.ZZ(valueNode.bigIntegerValue());
            } else if (valueNode instanceof DecimalNode) {
                return F.num(new Apfloat(valueNode.decimalValue()));
            }
        }
        if (valueNode instanceof BooleanNode) {
            return valueNode.booleanValue() ? S.True : S.False;
        } else if (valueNode instanceof NullNode) {
            return S.Null;
        } else if (valueNode instanceof TextNode) {
            return F.$str(valueNode.textValue());
        }
        return F.$str(valueNode.toString());
    }
    return F.NIL;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) FloatNode(com.fasterxml.jackson.databind.node.FloatNode) DoubleNode(com.fasterxml.jackson.databind.node.DoubleNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) BigIntegerNode(com.fasterxml.jackson.databind.node.BigIntegerNode) TextNode(com.fasterxml.jackson.databind.node.TextNode) NumericNode(com.fasterxml.jackson.databind.node.NumericNode) LongNode(com.fasterxml.jackson.databind.node.LongNode) BooleanNode(com.fasterxml.jackson.databind.node.BooleanNode) Apfloat(org.apfloat.Apfloat) ShortNode(com.fasterxml.jackson.databind.node.ShortNode) Entry(java.util.Map.Entry) IntNode(com.fasterxml.jackson.databind.node.IntNode) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) Iterator(java.util.Iterator) ValueNode(com.fasterxml.jackson.databind.node.ValueNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) IExpr(org.matheclipse.core.interfaces.IExpr) DecimalNode(com.fasterxml.jackson.databind.node.DecimalNode) NullNode(com.fasterxml.jackson.databind.node.NullNode)

Example 53 with IASTAppendable

use of org.matheclipse.core.interfaces.IASTAppendable in project symja_android_library by axkr.

the class Object2Expr method convertList.

/**
 * @param lst
 * @param parseString if <code>true</code> and <code>obj instanceof String</code> parse the string
 *        as a Symja expression
 * @param javaObject if <code>true</code> return a wrapper instanceof {@link JavaObjectExpr} if no
 *        other conversion was found
 * @return
 */
public static IExpr convertList(java.util.Collection<?> lst, boolean parseString, boolean javaObject) {
    if (lst.size() == 0) {
        return List();
    } else {
        int size = lst.size();
        IASTAppendable list = F.ast(S.List, size);
        for (Object element : lst) {
            list.append(convert(element, parseString, javaObject));
        }
        return list;
    }
}
Also used : IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable)

Example 54 with IASTAppendable

use of org.matheclipse.core.interfaces.IASTAppendable in project symja_android_library by axkr.

the class JASConvert method complexPoly2Expr.

/**
 * Convert a JAS complex-rational polynomial to <code>IExpr</code>.
 *
 * @param poly
 * @return
 * @throws ArithmeticException
 * @throws JASConversionException
 */
public IExpr complexPoly2Expr(final GenPolynomial<Complex<BigRational>> poly) throws ArithmeticException, JASConversionException {
    if (poly.length() == 0) {
        return F.C0;
    }
    IASTAppendable result = F.PlusAlloc(poly.length());
    for (Monomial<Complex<BigRational>> monomial : poly) {
        Complex<BigRational> coeff = monomial.coefficient();
        ExpVector exp = monomial.exponent();
        IASTAppendable monomTimes = F.TimesAlloc(exp.length() + 1);
        monomialToExpr(coeff, exp, monomTimes);
        result.append(monomTimes.oneIdentity1());
    }
    return result.oneIdentity0();
}
Also used : IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) BigRational(edu.jas.arith.BigRational) ExpVector(edu.jas.poly.ExpVector) IComplex(org.matheclipse.core.interfaces.IComplex) Complex(edu.jas.poly.Complex)

Example 55 with IASTAppendable

use of org.matheclipse.core.interfaces.IASTAppendable in project symja_android_library by axkr.

the class JASConvert method logIntegral2Expr.

/**
 * Convert a jas <code>LogIntegral</code> into a matheclipse expression
 *
 * @param logIntegral the JAS LogIntegral
 * @return
 */
public IAST logIntegral2Expr(LogIntegral<BigRational> logIntegral) {
    List<BigRational> cfactors = logIntegral.cfactors;
    List<GenPolynomial<BigRational>> cdenom = logIntegral.cdenom;
    List<AlgebraicNumber<BigRational>> afactors = logIntegral.afactors;
    List<GenPolynomial<AlgebraicNumber<BigRational>>> adenom = logIntegral.adenom;
    IASTAppendable plus = F.PlusAlloc(cfactors.size() + afactors.size());
    if (cfactors.size() > 0) {
        for (int i = 0; i < cfactors.size(); i++) {
            BigRational cp = cfactors.get(i);
            GenPolynomial<BigRational> p = cdenom.get(i);
            plus.append(F.Times(F.fraction(cp.numerator(), cp.denominator()), F.Log(rationalPoly2Expr(p, false))));
        }
    }
    // TODO implement this conversion for AlgebraicNumbers...
    if (afactors.size() > 0) {
        for (int i = 0; i < afactors.size(); i++) {
            AlgebraicNumber<BigRational> ap = afactors.get(i);
            AlgebraicNumberRing<BigRational> ar = ap.factory();
            GenPolynomial<AlgebraicNumber<BigRational>> p = adenom.get(i);
            if (p.degree(0) < ar.modul.degree(0) && ar.modul.degree(0) > 2) {
            }
            GenPolynomial<BigRational> v = ap.getVal();
            IASTAppendable times = F.TimesAlloc(2);
            if (p.degree(0) < ar.modul.degree(0) && ar.modul.degree(0) > 2) {
                IASTAppendable rootOf = F.ast(S.RootOf);
                rootOf.append(rationalPoly2Expr(ar.modul, false));
                times.append(rootOf);
                throw new UnsupportedOperationException("JASConvert#logIntegral2Expr()");
            }
            times.append(rationalPoly2Expr(v, false));
            times.append(F.Log(polyAlgebraicNumber2Expr(p)));
            plus.append(times);
        }
    }
    return plus;
}
Also used : GenPolynomial(edu.jas.poly.GenPolynomial) BigRational(edu.jas.arith.BigRational) AlgebraicNumber(edu.jas.poly.AlgebraicNumber) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable)

Aggregations

IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)363 IExpr (org.matheclipse.core.interfaces.IExpr)219 IAST (org.matheclipse.core.interfaces.IAST)130 ISymbol (org.matheclipse.core.interfaces.ISymbol)36 IASTMutable (org.matheclipse.core.interfaces.IASTMutable)30 IInteger (org.matheclipse.core.interfaces.IInteger)29 Map (java.util.Map)28 EvalEngine (org.matheclipse.core.eval.EvalEngine)20 PrettyPrint (edu.jas.kern.PrettyPrint)13 SortedMap (java.util.SortedMap)13 ArrayList (java.util.ArrayList)12 F (org.matheclipse.core.expression.F)12 BigRational (edu.jas.arith.BigRational)10 LogManager (org.apache.logging.log4j.LogManager)10 Logger (org.apache.logging.log4j.Logger)10 ExpVector (edu.jas.poly.ExpVector)9 HashMap (java.util.HashMap)9 IBuiltInSymbol (org.matheclipse.core.interfaces.IBuiltInSymbol)8 IStringX (org.matheclipse.core.interfaces.IStringX)8 ASTNode (org.matheclipse.parser.client.ast.ASTNode)8