Search in sources :

Example 46 with IASTAppendable

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

the class F method timesOrderless.

private static IASTMutable timesOrderless(Predicate<IExpr> t, final IExpr a1, final IExpr a2) {
    final boolean test1 = t.test(a1);
    final boolean test2 = t.test(a2);
    if (test1 || test2) {
        int size = test1 ? a1.size() : 1;
        size += test2 ? a2.size() : 1;
        IASTAppendable result = ast(Times, size);
        if (test1) {
            result.appendArgs((IAST) a1);
        } else {
            result.append(a1);
        }
        if (test2) {
            result.appendArgs((IAST) a2);
        } else {
            result.append(a2);
        }
        EvalAttributes.sort(result);
        return result;
    }
    if (a1.compareTo(a2) > 0) {
        // swap arguments
        return new B2.Times(a2, a1);
    }
    return new B2.Times(a1, a2);
}
Also used : IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable)

Example 47 with IASTAppendable

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

the class F method ast.

/**
 * @param head the header expression of the function. If the ast represents a function like <code>
 *     f[x,y], Sin[x],...</code>, the <code>head</code> will be an instance of type ISymbol.
 * @param collection the collection which holds the elements which should be appended
 * @param initialCapacity the additional capacity added to the collections size (i.e. number of
 *        arguments without the header element) of the list.
 * @return
 */
public static IASTAppendable ast(final IExpr head, Collection<? extends IExpr> collection, int initialCapacity) {
    IASTAppendable result = ast(head, collection.size() + initialCapacity);
    result.appendAll(collection);
    return result;
}
Also used : IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable)

Example 48 with IASTAppendable

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

the class F method plusOrderless.

private static IASTMutable plusOrderless(Predicate<IExpr> t, final IExpr a1, final IExpr a2) {
    final boolean test1 = t.test(a1);
    final boolean test2 = t.test(a2);
    if (test1 || test2) {
        int size = test1 ? a1.size() : 1;
        size += test2 ? a2.size() : 1;
        IASTAppendable result = ast(Plus, size);
        if (test1) {
            result.appendArgs((IAST) a1);
        } else {
            result.append(a1);
        }
        if (test2) {
            result.appendArgs((IAST) a2);
        } else {
            result.append(a2);
        }
        EvalAttributes.sort(result);
        return result;
    }
    if (a1.compareTo(a2) > 0) {
        // swap arguments
        return new B2.Plus(a2, a1);
    }
    return new B2.Plus(a1, a2);
}
Also used : IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable)

Example 49 with IASTAppendable

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

the class ExpressionJSONConvert method importExpressionJSONRecursive.

public static IExpr importExpressionJSONRecursive(JsonNode node) {
    if (node instanceof ArrayNode) {
        ArrayNode arrayNode = (ArrayNode) node;
        Iterator<JsonNode> iter = arrayNode.elements();
        IASTAppendable ast;
        if (iter.hasNext()) {
            JsonNode next = iter.next();
            IExpr temp = importExpressionJSONRecursive(next);
            if (temp.isPresent()) {
                ast = F.ast(temp, arrayNode.size() - 1);
                while (iter.hasNext()) {
                    next = iter.next();
                    temp = importExpressionJSONRecursive(next);
                    if (temp.isPresent()) {
                        ast.append(temp);
                    }
                }
                return ast;
            }
        }
        return F.NIL;
    } 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 = importExpressionJSONRecursive(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) {
            String symbolName = valueNode.textValue();
            if (symbolName.length() > 1 && symbolName.charAt(0) == '\'' && symbolName.charAt(symbolName.length() - 1) == '\'') {
                return F.$str(symbolName.substring(1, symbolName.length() - 1));
            }
            if (Scanner.isIdentifier(symbolName)) {
                return F.symbol(symbolName);
            }
            return F.$str(symbolName);
        }
        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 50 with IASTAppendable

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

the class JASIExpr method exprPoly2Expr.

/**
 * Converts a <a href="http://krum.rz.uni-mannheim.de/jas/">JAS</a> polynomial to a MathEclipse
 * AST with head <code>Plus</code>
 *
 * @param poly a JAS polynomial
 * @param variable
 * @return
 * @throws ArithmeticException
 * @throws ClassCastException
 */
public IExpr exprPoly2Expr(final GenPolynomial<IExpr> poly, IExpr variable) {
    if (poly.length() == 0) {
        return F.C0;
    }
    IASTAppendable result = F.PlusAlloc(poly.length());
    for (Monomial<IExpr> monomial : poly) {
        IExpr 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) ExpVector(edu.jas.poly.ExpVector) IExpr(org.matheclipse.core.interfaces.IExpr)

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