Search in sources :

Example 6 with GenPolynomial

use of edu.jas.poly.GenPolynomial in project symja_android_library by axkr.

the class Algebra method partialFractionDecompositionRational.

/**
	 * Returns an AST with head <code>Plus</code>, which contains the partial fraction decomposition of the numerator
	 * and denominator parts.
	 * 
	 * @param pf
	 *            partial fraction generator
	 * @param parts
	 * @param variable
	 *            a variable
	 * @return <code>F.NIL</code> if the partial fraction decomposition wasn't constructed
	 */
public static IExpr partialFractionDecompositionRational(IPartialFractionGenerator pf, IExpr[] parts, ISymbol variable) {
    try {
        IAST variableList = F.List(variable);
        IExpr exprNumerator = F.evalExpandAll(parts[0]);
        IExpr exprDenominator = F.evalExpandAll(parts[1]);
        ASTRange r = new ASTRange(variableList, 1);
        List<IExpr> varList = r;
        String[] varListStr = new String[1];
        varListStr[0] = variableList.arg1().toString();
        JASConvert<BigRational> jas = new JASConvert<BigRational>(varList, BigRational.ZERO);
        GenPolynomial<BigRational> numerator = jas.expr2JAS(exprNumerator, false);
        GenPolynomial<BigRational> denominator = jas.expr2JAS(exprDenominator, false);
        // get factors
        FactorAbstract<BigRational> factorAbstract = FactorFactory.getImplementation(BigRational.ZERO);
        SortedMap<GenPolynomial<BigRational>, Long> sfactors = factorAbstract.baseFactors(denominator);
        List<GenPolynomial<BigRational>> D = new ArrayList<GenPolynomial<BigRational>>(sfactors.keySet());
        SquarefreeAbstract<BigRational> sqf = SquarefreeFactory.getImplementation(BigRational.ZERO);
        List<List<GenPolynomial<BigRational>>> Ai = sqf.basePartialFraction(numerator, sfactors);
        if (Ai.size() > 0) {
            // IAST result = F.Plus();
            pf.allocPlus(Ai.size() * 2);
            pf.setJAS(jas);
            if (!Ai.get(0).get(0).isZERO()) {
                pf.addNonFractionalPart(Ai.get(0).get(0));
            }
            for (int i = 1; i < Ai.size(); i++) {
                List<GenPolynomial<BigRational>> list = Ai.get(i);
                int j = 0;
                for (GenPolynomial<BigRational> genPolynomial : list) {
                    if (!genPolynomial.isZERO()) {
                        GenPolynomial<BigRational> Di_1 = D.get(i - 1);
                        pf.addSinglePartialFraction(genPolynomial, Di_1, j);
                    }
                    j++;
                }
            }
            return pf.getResult();
        }
    } catch (JASConversionException e) {
        if (Config.DEBUG) {
            e.printStackTrace();
        }
    }
    return F.NIL;
}
Also used : ASTRange(org.matheclipse.core.expression.ASTRange) GenPolynomial(edu.jas.poly.GenPolynomial) BigRational(edu.jas.arith.BigRational) ArrayList(java.util.ArrayList) JASConversionException(org.matheclipse.core.eval.exception.JASConversionException) ModLong(edu.jas.arith.ModLong) JASConvert(org.matheclipse.core.convert.JASConvert) List(org.matheclipse.core.expression.F.List) List(java.util.List) ArrayList(java.util.ArrayList) IAST(org.matheclipse.core.interfaces.IAST) JASIExpr(org.matheclipse.core.convert.JASIExpr) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 7 with GenPolynomial

use of edu.jas.poly.GenPolynomial in project symja_android_library by axkr.

the class Algebra method factorComplex.

public static IAST factorComplex(GenPolynomial<BigRational> polyRat, JASConvert<BigRational> jas, List<IExpr> varList, ISymbol head, boolean noGCDLCM) {
    TermOrder termOrder = TermOrderByName.Lexicographic;
    // Object[] objects = jas.factorTerms(polyRat);
    String[] vars = new String[varList.size()];
    for (int i = 0; i < varList.size(); i++) {
        vars[i] = varList.get(i).toString();
    }
    Object[] objects = JASConvert.rationalFromRationalCoefficientsFactor(new GenPolynomialRing<BigRational>(BigRational.ZERO, varList.size(), termOrder, vars), polyRat);
    java.math.BigInteger gcd = (java.math.BigInteger) objects[0];
    java.math.BigInteger lcm = (java.math.BigInteger) objects[1];
    GenPolynomial<BigRational> poly = (GenPolynomial<BigRational>) objects[2];
    ComplexRing<BigRational> cfac = new ComplexRing<BigRational>(BigRational.ZERO);
    GenPolynomialRing<Complex<BigRational>> cpfac = new GenPolynomialRing<Complex<BigRational>>(cfac, 1, termOrder);
    GenPolynomial<Complex<BigRational>> a = PolyUtil.complexFromAny(cpfac, poly);
    FactorComplex<BigRational> factorAbstract = new FactorComplex<BigRational>(cfac);
    SortedMap<GenPolynomial<Complex<BigRational>>, Long> map = factorAbstract.factors(a);
    IAST result = F.ast(head);
    if (!noGCDLCM) {
        if (!gcd.equals(java.math.BigInteger.ONE) || !lcm.equals(java.math.BigInteger.ONE)) {
            result.append(F.fraction(gcd, lcm));
        }
    }
    GenPolynomial<Complex<BigRational>> temp;
    for (SortedMap.Entry<GenPolynomial<Complex<BigRational>>, Long> entry : map.entrySet()) {
        if (entry.getKey().isONE() && entry.getValue().equals(1L)) {
            continue;
        }
        temp = entry.getKey();
        result.append(F.Power(jas.complexPoly2Expr(entry.getKey()), F.integer(entry.getValue())));
    }
    return result;
}
Also used : TermOrder(edu.jas.poly.TermOrder) ExprTermOrder(org.matheclipse.core.polynomials.ExprTermOrder) GenPolynomial(edu.jas.poly.GenPolynomial) BigRational(edu.jas.arith.BigRational) IComplex(org.matheclipse.core.interfaces.IComplex) FactorComplex(edu.jas.ufd.FactorComplex) Complex(edu.jas.poly.Complex) IAST(org.matheclipse.core.interfaces.IAST) FactorComplex(edu.jas.ufd.FactorComplex) GenPolynomialRing(edu.jas.poly.GenPolynomialRing) ComplexRing(edu.jas.poly.ComplexRing) SortedMap(java.util.SortedMap) ModLong(edu.jas.arith.ModLong) BigInteger(edu.jas.arith.BigInteger)

Example 8 with GenPolynomial

use of edu.jas.poly.GenPolynomial in project symja_android_library by axkr.

the class PartialFractionIntegrateGenerator method addSinglePartialFraction.

@Override
public void addSinglePartialFraction(GenPolynomial<BigRational> genPolynomial, GenPolynomial<BigRational> Di_1, int j) {
    if (!genPolynomial.isZERO()) {
        BigRational[] numer = new BigRational[3];
        BigRational[] denom = new BigRational[3];
        IExpr temp;
        boolean isDegreeLE2 = Di_1.degree() <= 2;
        if (isDegreeLE2 && j == 1L) {
            Object[] objects = jas.factorTerms(genPolynomial);
            java.math.BigInteger gcd = (java.math.BigInteger) objects[0];
            java.math.BigInteger lcm = (java.math.BigInteger) objects[1];
            GenPolynomial<edu.jas.arith.BigInteger> genPolynomial2 = ((GenPolynomial<edu.jas.arith.BigInteger>) objects[2]).multiply(edu.jas.arith.BigInteger.valueOf(gcd));
            if (genPolynomial2.isONE()) {
                GenPolynomial<BigRational> newDi_1 = Di_1.multiply(BigRational.valueOf(lcm));
                isQuadratic(newDi_1, denom);
                IFraction a = F.fraction(denom[2].numerator(), denom[2].denominator());
                IFraction b = F.fraction(denom[1].numerator(), denom[1].denominator());
                IFraction c = F.fraction(denom[0].numerator(), denom[0].denominator());
                if (a.isZero()) {
                    // JavaForm[Log[b*x+c]/b]
                    result.append(Times(Log(Plus(c, Times(b, x))), Power(b, CN1)));
                } else {
                    // compute b^2-4*a*c from
                    // (a*x^2+b*x+c)
                    BigRational cmp = denom[1].multiply(denom[1]).subtract(BigRational.valueOf(4L).multiply(denom[2]).multiply(denom[0]));
                    int cmpTo = cmp.compareTo(BigRational.ZERO);
                    // (2*a*x+b)
                    IExpr ax2Plusb = F.Plus(F.Times(F.C2, a, x), b);
                    if (cmpTo == 0) {
                        // (-2) / (2*a*x+b)
                        result.append(F.Times(F.integer(-2L), F.Power(ax2Plusb, F.CN1)));
                    } else if (cmpTo > 0) {
                        // (b^2-4ac)^(1/2)
                        temp = F.eval(F.Power(F.Subtract(F.Sqr(b), F.Times(F.C4, a, c)), F.C1D2));
                        result.append(F.Times(F.Power(temp, F.CN1), F.Log(F.Times(F.Subtract(ax2Plusb, temp), Power(F.Plus(ax2Plusb, temp), F.CN1)))));
                    } else {
                        // (4ac-b^2)^(1/2)
                        temp = F.eval(F.Power(F.Subtract(F.Times(F.C4, a, c), F.Sqr(b)), F.CN1D2));
                        result.append(F.Times(F.C2, temp, F.ArcTan(Times(ax2Plusb, temp))));
                    }
                }
            } else {
                // (B*A*x) / (q*p*x)
                isQuadratic(genPolynomial, numer);
                IFraction A = F.fraction(numer[1].numerator(), numer[1].denominator());
                IFraction B = F.fraction(numer[0].numerator(), numer[0].denominator());
                isQuadratic(Di_1, denom);
                IFraction p = F.fraction(denom[1].numerator(), denom[1].denominator());
                IFraction q = F.fraction(denom[0].numerator(), denom[0].denominator());
                if (A.isZero() && !p.isZero()) {
                    // JavaForm[B*Log[p*x+q]/p]
                    if (q.isNegative()) {
                        temp = Times(B, Log(Plus(q.negate(), Times(p.negate(), x))), Power(p, CN1));
                    } else {
                        temp = Times(B, Log(Plus(q, Times(p, x))), Power(p, CN1));
                    }
                } else {
                    // JavaForm[A/2*Log[x^2+p*x+q]+(2*B-A*p)/(4*q-p^2)^(1/2)*ArcTan[(2*x+p)/(4*q-p^2)^(1/2)]]
                    temp = Plus(Times(C1D2, A, Log(Plus(q, Times(p, x), Power(x, C2)))), Times(ArcTan(Times(Plus(p, Times(C2, x)), Power(Plus(Times(CN1, Power(p, C2)), Times(C4, q)), CN1D2))), Plus(Times(C2, B), Times(CN1, A, p)), Power(Plus(Times(CN1, Power(p, C2)), Times(C4, q)), CN1D2)));
                }
                result.append(F.eval(temp));
            }
        } else if (isDegreeLE2 && j > 1L) {
            isQuadratic(genPolynomial, numer);
            IFraction A = F.fraction(numer[1].numerator(), numer[1].denominator());
            IFraction B = F.fraction(numer[0].numerator(), numer[0].denominator());
            isQuadratic(Di_1, denom);
            IFraction a = F.fraction(denom[2].numerator(), denom[2].denominator());
            IFraction b = F.fraction(denom[1].numerator(), denom[1].denominator());
            IFraction c = F.fraction(denom[0].numerator(), denom[0].denominator());
            IInteger k = F.integer(j);
            if (A.isZero()) {
                // JavaForm[B*((2*a*x+b)/((k-1)*(4*a*c-b^2)*(a*x^2+b*x+c)^(k-1))+
                // (4*k*a-6*a)/((k-1)*(4*a*c-b^2))*Integrate[(a*x^2+b*x+c)^(-k+1),x])]
                temp = Times(B, Plus(Times(Integrate(Power(Plus(c, Times(b, x), Times(a, Power(x, C2))), Plus(C1, Times(CN1, k))), x), Plus(Times(F.integer(-6L), a), Times(C4, a, k)), Power(Plus(CN1, k), CN1), Power(Plus(Times(CN1, Power(b, C2)), Times(C4, a, c)), CN1)), Times(Plus(b, Times(C2, a, x)), Power(Plus(CN1, k), CN1), Power(Plus(Times(CN1, Power(b, C2)), Times(C4, a, c)), CN1), Power(Plus(c, Times(b, x), Times(a, Power(x, C2))), Times(CN1, Plus(CN1, k))))));
            } else {
                // JavaForm[(-A)/(2*a*(k-1)*(a*x^2+b*x+c)^(k-1))+(B-A*b/(2*a))*Integrate[(a*x^2+b*x+c)^(-k),x]]
                temp = Plus(Times(Integrate(Power(Plus(c, Times(b, x), Times(a, Power(x, C2))), Times(CN1, k)), x), Plus(B, Times(CN1D2, A, Power(a, CN1), b))), Times(CN1D2, A, Power(a, CN1), Power(Plus(CN1, k), CN1), Power(Plus(c, Times(b, x), Times(a, Power(x, C2))), Times(CN1, Plus(CN1, k)))));
            }
            result.append(F.eval(temp));
        } else {
            // ElementaryIntegration<BigRational> ei = new
            // ElementaryIntegration<BigRational>(BigRational.ZERO);
            // Integral<BigRational> integral= ei.integrate(genPolynomial,
            // Di_1);
            temp = F.eval(F.Times(jas.rationalPoly2Expr(genPolynomial), F.Power(jas.rationalPoly2Expr(Di_1), F.integer(j * (-1L)))));
            if (!temp.isZero()) {
                if (temp.isAST()) {
                    ((IAST) temp).addEvalFlags(IAST.IS_DECOMPOSED_PARTIAL_FRACTION);
                }
                result.append(F.Integrate(temp, x));
            }
        }
    }
}
Also used : IFraction(org.matheclipse.core.interfaces.IFraction) GenPolynomial(edu.jas.poly.GenPolynomial) BigRational(edu.jas.arith.BigRational) IInteger(org.matheclipse.core.interfaces.IInteger) BigInteger(edu.jas.arith.BigInteger) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 9 with GenPolynomial

use of edu.jas.poly.GenPolynomial in project symja_android_library by axkr.

the class Algebra method factorRational.

public static IAST factorRational(GenPolynomial<BigRational> polyRat, JASConvert<BigRational> jas, List<IExpr> varList, ISymbol head) {
    Object[] objects = jas.factorTerms(polyRat);
    GenPolynomial<edu.jas.arith.BigInteger> poly = (GenPolynomial<edu.jas.arith.BigInteger>) objects[2];
    FactorAbstract<edu.jas.arith.BigInteger> factorAbstract = FactorFactory.getImplementation(edu.jas.arith.BigInteger.ONE);
    SortedMap<GenPolynomial<edu.jas.arith.BigInteger>, Long> map;
    map = factorAbstract.factors(poly);
    IAST result = F.ast(head);
    java.math.BigInteger gcd = (java.math.BigInteger) objects[0];
    java.math.BigInteger lcm = (java.math.BigInteger) objects[1];
    if (!gcd.equals(java.math.BigInteger.ONE) || !lcm.equals(java.math.BigInteger.ONE)) {
        result.append(F.fraction(gcd, lcm));
    }
    for (SortedMap.Entry<GenPolynomial<edu.jas.arith.BigInteger>, Long> entry : map.entrySet()) {
        if (entry.getKey().isONE() && entry.getValue().equals(1L)) {
            continue;
        }
        if (entry.getValue() == 1L) {
            result.append(jas.integerPoly2Expr(entry.getKey()));
        } else {
            result.append(F.Power(jas.integerPoly2Expr(entry.getKey()), F.integer(entry.getValue())));
        }
    }
    return result;
}
Also used : GenPolynomial(edu.jas.poly.GenPolynomial) SortedMap(java.util.SortedMap) ModLong(edu.jas.arith.ModLong) BigInteger(edu.jas.arith.BigInteger) IAST(org.matheclipse.core.interfaces.IAST)

Example 10 with GenPolynomial

use of edu.jas.poly.GenPolynomial in project symja_android_library by axkr.

the class Algebra method factorModulus.

public static IAST factorModulus(JASModInteger jas, ModLongRing modIntegerRing, GenPolynomial<ModLong> poly, boolean factorSquareFree) {
    FactorAbstract<ModLong> factorAbstract = FactorFactory.getImplementation(modIntegerRing);
    SortedMap<GenPolynomial<ModLong>, Long> map;
    if (factorSquareFree) {
        map = factorAbstract.squarefreeFactors(poly);
    } else {
        map = factorAbstract.factors(poly);
    }
    IAST result = F.Times();
    for (SortedMap.Entry<GenPolynomial<ModLong>, Long> entry : map.entrySet()) {
        GenPolynomial<ModLong> singleFactor = entry.getKey();
        Long val = entry.getValue();
        result.append(F.Power(jas.modLongPoly2Expr(singleFactor), F.integer(val)));
    }
    return result;
}
Also used : ModLong(edu.jas.arith.ModLong) GenPolynomial(edu.jas.poly.GenPolynomial) SortedMap(java.util.SortedMap) ModLong(edu.jas.arith.ModLong) IAST(org.matheclipse.core.interfaces.IAST)

Aggregations

GenPolynomial (edu.jas.poly.GenPolynomial)14 IAST (org.matheclipse.core.interfaces.IAST)11 BigRational (edu.jas.arith.BigRational)9 IExpr (org.matheclipse.core.interfaces.IExpr)7 ModLong (edu.jas.arith.ModLong)5 ISymbol (org.matheclipse.core.interfaces.ISymbol)4 BigInteger (edu.jas.arith.BigInteger)3 ArrayList (java.util.ArrayList)3 SortedMap (java.util.SortedMap)3 JASConvert (org.matheclipse.core.convert.JASConvert)3 JASConversionException (org.matheclipse.core.eval.exception.JASConversionException)3 IFraction (org.matheclipse.core.interfaces.IFraction)3 IInteger (org.matheclipse.core.interfaces.IInteger)3 Complex (edu.jas.poly.Complex)2 ComplexRing (edu.jas.poly.ComplexRing)2 ExpVector (edu.jas.poly.ExpVector)2 TermOrder (edu.jas.poly.TermOrder)2 BigInteger (java.math.BigInteger)2 WrongArgumentType (org.matheclipse.core.eval.exception.WrongArgumentType)2 IComplex (org.matheclipse.core.interfaces.IComplex)2