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);
}
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;
}
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);
}
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;
}
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();
}
Aggregations