Search in sources :

Example 56 with IASTAppendable

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

the class JASConvert method integral2Expr.

/**
 * Convert a jas <code>Integral</code> into a matheclipse expression
 *
 * @param integral the JAS Integral
 * @return
 */
public IAST integral2Expr(Integral<BigRational> integral) {
    GenPolynomial<BigRational> pol = integral.pol;
    List<GenPolynomial<BigRational>> rational = integral.rational;
    List<LogIntegral<BigRational>> logarithm = integral.logarithm;
    IASTAppendable sum = F.PlusAlloc(rational.size() + logarithm.size());
    if (!pol.isZERO()) {
        sum.append(rationalPoly2Expr(pol, false));
    }
    if (rational.size() != 0) {
        int i = 0;
        while (i < rational.size()) {
            sum.append(F.Times(rationalPoly2Expr(rational.get(i++), false), F.Power(rationalPoly2Expr(rational.get(i++), false), F.CN1)));
        }
    }
    if (logarithm.size() != 0) {
        for (LogIntegral<BigRational> pf : logarithm) {
            sum.append(logIntegral2Expr(pf));
        }
    }
    return sum;
}
Also used : GenPolynomial(edu.jas.poly.GenPolynomial) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) BigRational(edu.jas.arith.BigRational) LogIntegral(edu.jas.integrate.LogIntegral)

Example 57 with IASTAppendable

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

the class JASModInteger method modLongPoly2Expr.

public IExpr modLongPoly2Expr(final GenPolynomial<ModLong> poly) throws ArithmeticException, ClassCastException {
    if (poly.length() == 0) {
        return F.Plus(F.C0);
    }
    IASTAppendable result = F.PlusAlloc(poly.length());
    for (Monomial<ModLong> monomial : poly) {
        ModLong coeff = monomial.coefficient();
        ExpVector exp = monomial.exponent();
        IInteger coeffValue = F.ZZ(coeff.getVal());
        IASTAppendable monomTimes = F.TimesAlloc(exp.length() + 1);
        monomialToExpr(coeffValue, exp, monomTimes);
        result.append(monomTimes.oneIdentity1());
    }
    return result.oneIdentity0();
}
Also used : ModLong(edu.jas.arith.ModLong) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IInteger(org.matheclipse.core.interfaces.IInteger) ExpVector(edu.jas.poly.ExpVector)

Example 58 with IASTAppendable

use of org.matheclipse.core.interfaces.IASTAppendable 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 59 with IASTAppendable

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

the class ElementData method setUp.

@Override
public void setUp(final ISymbol newSymbol) {
    // 1: "AtomicNumber"
    // 2: "Abbreviation"
    // 3: "StandardName"
    // 4: "Name"
    // 5: "Block"
    // 6: "Group"
    // 7: "Period"
    // 8: "Series"
    // 9: "AtomicWeight"
    // 10: "DiscoveryYear"
    // 11: "LiquidDensity"
    // 12: "Density"
    // 13: "AbsoluteMeltingPoint"
    // 14: "MeltingPoint"
    // 15: "AbsoluteBoilingPoint"
    // 16: "BoilingPoint"
    // 17: "SpecificHeat"
    // 18: "FusionHeat"
    // 19: "VaporizationHeat"
    // 20: "ElectroNegativity"
    // 21: "CrustAbundance"
    // 22: "MohsHardness"
    // 23: "VickersHardness"
    // 24: "BrinellHardness"
    // 25: "AtomicRadius"
    // 26: "VanDerWaalsRadius"
    // 27: "CovalentRadius"
    // 28: "IonizationEnergies"
    // 29: "ElectronAffinity"
    // 30: "ThermalConductivity"
    // 31: "YoungModulus"
    // 32: "PoissonRatio"
    // 33: "BulkModulus"
    // 34: "ShearModulus"
    // 35: "ElectronConfiguration"
    // 36: "ElectronConfigurationString"
    // 37: "ElectronShellConfiguration"
    IAST[] list = ElementData1.ELEMENTS;
    for (int i = 0; i < list.length; i++) {
        MAP_NUMBER_NAME.put(list[i].arg1(), list[i].arg3());
        IASTAppendable subList = F.ListAlloc(list[i].size());
        for (int j = 1; j < list[i].size(); j++) {
            subList.append(list[i].get(j));
        }
        MAP_NAME_DATA.put(list[i].arg1(), subList);
        MAP_NAME_DATA.put(list[i].arg2(), subList);
        MAP_NAME_DATA.put(list[i].arg3(), subList);
    }
    list = ElementData2.ELEMENTS;
    for (int i = 0; i < list.length; i++) {
        MAP_NUMBER_NAME.put(list[i].arg1(), list[i].arg3());
        IASTAppendable subList = F.ListAlloc(list[i].size());
        for (int j = 1; j < list[i].size(); j++) {
            subList.append(list[i].get(j));
        }
        MAP_NAME_DATA.put(list[i].arg1(), subList);
        MAP_NAME_DATA.put(list[i].arg2(), subList);
        MAP_NAME_DATA.put(list[i].arg3(), subList);
    }
}
Also used : IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IAST(org.matheclipse.core.interfaces.IAST)

Example 60 with IASTAppendable

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

the class Convert method complexVector2List.

/**
 * Convert a RealVector to a IAST list.
 *
 * @param vector
 * @param vectorFormat set flag for isVector() method
 * @return <code>F.NIL</code> if no conversion was possible
 */
public static IASTAppendable complexVector2List(final FieldVector<Complex> vector, boolean vectorFormat) {
    if (vector == null) {
        return F.NIL;
    }
    final int rowSize = vector.getDimension();
    final IASTAppendable out = F.ListAlloc(rowSize);
    for (int i = 0; i < rowSize; i++) {
        out.append(F.complexNum(vector.getEntry(i)));
    }
    out.addEvalFlags(IAST.IS_VECTOR);
    return out;
}
Also used : 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