use of org.matheclipse.core.interfaces.IASTAppendable in project symja_android_library by axkr.
the class TeXParser method mfrac.
private IExpr mfrac(NodeList list) {
IASTAppendable frac = F.TimesAlloc(2);
if (list.getLength() > 0) {
Node temp = list.item(0);
frac.append(toExpr(temp));
if (1 < list.getLength()) {
temp = list.item(1);
frac.append(F.Power(toExpr(temp), -1));
} else {
throw new AbortException();
}
}
if (frac.isTimes() && frac.first().isSymbol() && frac.size() == 3 && frac.second().isPowerReciprocal()) {
ISymbol d = (ISymbol) frac.first();
if (d.getSymbolName().equals("d")) {
IExpr dDenominator = frac.second().first();
if (dDenominator.isSymbol()) {
String str = ((ISymbol) dDenominator).getSymbolName();
if (str.startsWith("d")) {
str = str.substring(1);
return F.Function(F.D(F.Slot1, F.$s(str)));
}
}
}
}
return frac;
}
use of org.matheclipse.core.interfaces.IASTAppendable in project symja_android_library by axkr.
the class GaussianInteger method factorize.
public static IAST factorize(BigInteger re, BigInteger im, IExpr num) {
GaussianInteger g = new GaussianInteger();
SortedMap<ComplexSym, Integer> complexMap = new TreeMap<ComplexSym, Integer>();
g.gaussianFactorization2(re, im, complexMap);
IASTAppendable list = F.ListAlloc(complexMap.size() + 1);
IExpr factor = F.C1;
IASTAppendable ast = F.TimesAlloc(complexMap.size());
for (Map.Entry<ComplexSym, Integer> entry : complexMap.entrySet()) {
ComplexSym key = entry.getKey();
int i = entry.getValue();
if (i == 1) {
ast.append(key);
} else {
IInteger is = F.ZZ(i);
ast.append(F.Power(key, is));
}
}
factor = F.eval(F.Divide(num, ast));
if (!factor.isOne()) {
list.append(F.list(factor, F.C1));
}
for (Map.Entry<ComplexSym, Integer> entry : complexMap.entrySet()) {
ComplexSym key = entry.getKey();
IInteger is = F.ZZ(entry.getValue());
list.append(F.list(key, is));
}
return list;
}
use of org.matheclipse.core.interfaces.IASTAppendable in project symja_android_library by axkr.
the class UtilityFunctionCtors method evalRubiDistPlus.
/**
* Rule 1:
*
* <pre>
* Dist[u_,v_,x_]+Dist[w_,v_,x_] :=
* If[EqQ[u+w,0],
* 0,
* Dist[u+w,v,x]]
* </pre>
*
* Rule 2:
*
* <pre>
* Dist[u_,v_,x_]-Dist[w_,v_,x_] :=
* If[EqQ[u-w,0],
* 0,
* Dist[u-w,v,x]]
* </pre>
*
* @param astPlus
* @param engine TODO
* @return
*/
public static IExpr evalRubiDistPlus(IAST astPlus, EvalEngine engine) {
for (int i = 1; i < astPlus.size() - 1; i++) {
IExpr arg1 = astPlus.get(i);
if (arg1.isAST(Dist) && arg1.size() == 4) {
// dist1 = Dist[u_,v_,x_]
// (IAST)
IAST dist1 = engine.evalArgs((IAST) arg1, ISymbol.NOATTRIBUTE).orElse((IAST) arg1);
// arg1;
IExpr v = dist1.arg2();
IExpr x = dist1.arg3();
for (int j = i + 1; j < astPlus.size(); j++) {
IExpr arg2 = astPlus.get(j);
if (arg2.isAST(Dist) && arg2.size() == 4 && arg2.getAt(2).equals(v) && arg2.getAt(3).equals(x)) {
// dist2=Dist[w_,v_,x_]
// (IAST)
IAST dist2 = engine.evalArgs((IAST) arg2, ISymbol.NOATTRIBUTE).orElse((IAST) arg2);
// arg2;
IASTAppendable result = astPlus.removeAtClone(j);
result.remove(i);
// Dist /: Dist[u_,v_,x_]+Dist[w_,v_,x_] :=
// If[EqQ[u+w,0],
// 0,
// Dist[u+w,v,x]]
IExpr p = F.Plus(dist1.arg1(), dist2.arg1());
result.append(F.If(EqQ(p, F.C0), F.C0, Dist(p, v, x)));
return result;
}
if (arg2.isTimes2() && arg2.first().isMinusOne() && arg2.second().isAST(Dist)) {
// -1 * Dist[w_,v_,x_]
IAST dist2 = engine.evalArgs((IAST) arg2.second(), ISymbol.NOATTRIBUTE).orElse(// (IAST) arg2.second();
(IAST) arg2.second());
if (dist2.size() == 4 && dist2.getAt(2).equals(v) && dist2.getAt(3).equals(x)) {
IASTAppendable result = astPlus.removeAtClone(j);
result.remove(i);
// Dist /: Dist[u_,v_,x_]-Dist[w_,v_,x_] :=
// If[EqQ[u-w,0],
// 0,
// Dist[u-w,v,x]]
IExpr p = F.Subtract(dist1.arg1(), dist2.arg1());
result.append(F.If(EqQ(p, F.C0), F.C0, Dist(p, v, x)));
return result;
}
}
}
} else if (arg1.isTimes2() && arg1.first().isMinusOne() && arg1.second().isAST(Dist)) {
// -1 * Dist[w_,v_,x_]
IAST dist1 = // (IAST)
engine.evalArgs((IAST) arg1.second(), ISymbol.NOATTRIBUTE).orElse((IAST) arg1.second());
// arg1.second();
IExpr v = dist1.arg2();
IExpr x = dist1.arg3();
for (int j = i + 1; j < astPlus.size(); j++) {
IExpr arg2 = astPlus.get(j);
if (arg2.isAST(Dist) && arg2.size() == 4 && arg2.getAt(2).equals(v) && arg2.getAt(3).equals(x)) {
// dist2 = Dist[u_,v_,x_]
// (IAST)
IAST dist2 = engine.evalArgs((IAST) arg2, ISymbol.NOATTRIBUTE).orElse((IAST) arg2);
// arg2;
IASTAppendable result = astPlus.removeAtClone(j);
result.remove(i);
// Dist /: Dist[u_,v_,x_]-Dist[w_,v_,x_] :=
// If[EqQ[u-w,0],
// 0,
// Dist[u-w,v,x]]
IExpr p = F.Subtract(dist2.arg1(), dist1.arg1());
result.append(F.If(EqQ(p, F.C0), F.C0, Dist(p, v, x)));
return result;
}
}
}
}
return F.NIL;
}
use of org.matheclipse.core.interfaces.IASTAppendable in project symja_android_library by axkr.
the class Java8TestCase method testStream001.
public void testStream001() {
IAST ast = List(C10, a, b, c, d, e);
IASTAppendable result = F.ListAlloc(ast.argSize() + 7);
// Consumer<IExpr> action = (IExpr x) -> System.out.println(x);
ast.stream().forEach(x -> result.append(x));
ast.stream(0, 7).forEach(x -> result.append(x));
assertEquals("{10,a,b,c,d,e,List,10,a,b,c,d,e}", result.toString());
}
use of org.matheclipse.core.interfaces.IASTAppendable in project symja_android_library by axkr.
the class Java8TestCase method testGrouping.
public void testGrouping() {
IAST ast = (IAST) F.List.of(10, 11, 12, 13, 14, 15, 16, 17, 18, 19);
IASTAppendable prototype = F.ListAlloc(10);
prototype.append(F.C0);
prototype.append(F.C0);
prototype.append(F.C0);
// append numbers grouped by prime to the prototype
java.util.Map<Boolean, IASTAppendable> exprByPrime = ast.stream().collect(//
Collectors.groupingBy(//
F.PrimeQ::ofQ, IASTAppendable.toAST(prototype)));
assertEquals("{false={0,0,0,10,12,14,15,16,18}, true={0,0,0,11,13,17,19}}", exprByPrime.toString());
exprByPrime = //
ast.stream().collect(//
Collectors.groupingBy(//
F.PrimeQ::ofQ, //
Collectors.mapping(//
x -> x.greater(F.ZZ(15)), IASTAppendable.toAST(F.List, 10))));
assertEquals("{false={False,False,False,False,True,True}, true={False,False,True,True}}", exprByPrime.toString());
java.util.Map<Boolean, Double> averageInt = //
ast.stream().collect(//
Collectors.groupingBy(//
F.PrimeQ::ofQ, //
Collectors.averagingInt(IExpr::toIntDefault)));
assertEquals("{false=14.166666666666666, true=15.0}", averageInt.toString());
}
Aggregations