Search in sources :

Example 91 with IASTAppendable

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;
}
Also used : IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) ISymbol(org.matheclipse.core.interfaces.ISymbol) Node(org.w3c.dom.Node) IExpr(org.matheclipse.core.interfaces.IExpr) AbortException(org.matheclipse.core.eval.exception.AbortException)

Example 92 with IASTAppendable

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;
}
Also used : BigInteger(java.math.BigInteger) IInteger(org.matheclipse.core.interfaces.IInteger) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) ComplexSym(org.matheclipse.core.expression.ComplexSym) IInteger(org.matheclipse.core.interfaces.IInteger) IExpr(org.matheclipse.core.interfaces.IExpr) TreeMap(java.util.TreeMap) TreeMap(java.util.TreeMap) Map(java.util.Map) SortedMap(java.util.SortedMap)

Example 93 with IASTAppendable

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;
}
Also used : IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

Example 94 with IASTAppendable

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());
}
Also used : IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IAST(org.matheclipse.core.interfaces.IAST)

Example 95 with IASTAppendable

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());
}
Also used : IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) F(org.matheclipse.core.expression.F) IAST(org.matheclipse.core.interfaces.IAST) Collectors(java.util.stream.Collectors) S.d(org.matheclipse.core.expression.S.d) S.c(org.matheclipse.core.expression.S.c) S.e(org.matheclipse.core.expression.S.e) IExpr(org.matheclipse.core.interfaces.IExpr) List(org.matheclipse.core.expression.F.List) IntSummaryStatistics(java.util.IntSummaryStatistics) Comparator(java.util.Comparator) C10(org.matheclipse.core.expression.F.C10) S.b(org.matheclipse.core.expression.S.b) S.a(org.matheclipse.core.expression.S.a) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) F(org.matheclipse.core.expression.F) IAST(org.matheclipse.core.interfaces.IAST) 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