Search in sources :

Example 11 with BigRational

use of edu.jas.arith.BigRational 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 12 with BigRational

use of edu.jas.arith.BigRational 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 13 with BigRational

use of edu.jas.arith.BigRational in project symja_android_library by axkr.

the class RootIntervals method croots.

/**
	 * Complex numeric roots intervals.
	 * 
	 * @param ast
	 * @return
	 */
public static IAST croots(final IExpr arg, boolean numeric) {
    try {
        VariablesSet eVar = new VariablesSet(arg);
        if (!eVar.isSize(1)) {
            // only possible for univariate polynomials
            return F.NIL;
        }
        IExpr expr = F.evalExpandAll(arg);
        ASTRange r = new ASTRange(eVar.getVarList(), 1);
        List<IExpr> varList = r;
        ComplexRing<BigRational> cfac = new ComplexRing<BigRational>(new BigRational(1));
        ComplexRootsAbstract<BigRational> cr = new ComplexRootsSturm<BigRational>(cfac);
        JASConvert<Complex<BigRational>> jas = new JASConvert<Complex<BigRational>>(varList, cfac);
        GenPolynomial<Complex<BigRational>> poly = jas.numericExpr2JAS(expr);
        Squarefree<Complex<BigRational>> engine = SquarefreeFactory.<Complex<BigRational>>getImplementation(cfac);
        poly = engine.squarefreePart(poly);
        List<Rectangle<BigRational>> roots = cr.complexRoots(poly);
        BigRational len = new BigRational(1, 100000L);
        IAST resultList = F.List();
        if (numeric) {
            for (Rectangle<BigRational> root : roots) {
                Rectangle<BigRational> refine = cr.complexRootRefinement(root, poly, len);
                resultList.append(JASConvert.jas2Numeric(refine.getCenter(), Config.DEFAULT_ROOTS_CHOP_DELTA));
            }
        } else {
            IAST rectangleList;
            for (Rectangle<BigRational> root : roots) {
                rectangleList = F.List();
                Rectangle<BigRational> refine = cr.complexRootRefinement(root, poly, len);
                rectangleList.append(JASConvert.jas2Complex(refine.getNW()));
                rectangleList.append(JASConvert.jas2Complex(refine.getSW()));
                rectangleList.append(JASConvert.jas2Complex(refine.getSE()));
                rectangleList.append(JASConvert.jas2Complex(refine.getNE()));
                resultList.append(rectangleList);
            // System.out.println("refine = " + refine);
            }
        }
        return resultList;
    } catch (InvalidBoundaryException e) {
        if (Config.SHOW_STACKTRACE) {
            e.printStackTrace();
        }
    } catch (JASConversionException e) {
        if (Config.SHOW_STACKTRACE) {
            e.printStackTrace();
        }
    }
    return F.NIL;
}
Also used : ASTRange(org.matheclipse.core.expression.ASTRange) InvalidBoundaryException(edu.jas.root.InvalidBoundaryException) BigRational(edu.jas.arith.BigRational) ComplexRootsSturm(edu.jas.root.ComplexRootsSturm) Rectangle(edu.jas.root.Rectangle) VariablesSet(org.matheclipse.core.convert.VariablesSet) JASConversionException(org.matheclipse.core.eval.exception.JASConversionException) Complex(edu.jas.poly.Complex) ComplexRing(edu.jas.poly.ComplexRing) JASConvert(org.matheclipse.core.convert.JASConvert) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

Example 14 with BigRational

use of edu.jas.arith.BigRational in project symja_android_library by axkr.

the class Integrate method isQuadratic.

/**
	 * Check if the polynomial has maximum degree 2 in 1 variable and return the
	 * coefficients.
	 * 
	 * @param poly
	 * @return <code>false</code> if the polynomials degree > 2 and number of
	 *         variables <> 1
	 */
public static boolean isQuadratic(GenPolynomial<BigRational> poly, BigRational[] result) {
    if (poly.degree() <= 2 && poly.numberOfVariables() == 1) {
        result[0] = BigRational.ZERO;
        result[1] = BigRational.ZERO;
        result[2] = BigRational.ZERO;
        for (Monomial<BigRational> monomial : poly) {
            BigRational coeff = monomial.coefficient();
            ExpVector exp = monomial.exponent();
            for (int i = 0; i < exp.length(); i++) {
                result[(int) exp.getVal(i)] = coeff;
            }
        }
        return true;
    }
    return false;
}
Also used : BigRational(edu.jas.arith.BigRational) ExpVector(edu.jas.poly.ExpVector)

Example 15 with BigRational

use of edu.jas.arith.BigRational in project symja_android_library by axkr.

the class JASModInteger method fraction2Poly.

private GenPolynomial<ModLong> fraction2Poly(final IFraction exprPoly) {
    // .toJavaBigInteger();
    BigInteger n = exprPoly.toBigNumerator();
    // .toJavaBigInteger();
    BigInteger d = exprPoly.toBigDenominator();
    BigRational nr = new BigRational(n);
    BigRational dr = new BigRational(d);
    BigRational r = nr.divide(dr);
    return new GenPolynomial(fPolyFactory, r);
}
Also used : GenPolynomial(edu.jas.poly.GenPolynomial) BigRational(edu.jas.arith.BigRational) BigInteger(java.math.BigInteger)

Aggregations

BigRational (edu.jas.arith.BigRational)20 IAST (org.matheclipse.core.interfaces.IAST)13 GenPolynomial (edu.jas.poly.GenPolynomial)9 ExpVector (edu.jas.poly.ExpVector)6 IExpr (org.matheclipse.core.interfaces.IExpr)6 Complex (edu.jas.poly.Complex)5 JASConvert (org.matheclipse.core.convert.JASConvert)5 JASConversionException (org.matheclipse.core.eval.exception.JASConversionException)5 ComplexRing (edu.jas.poly.ComplexRing)3 BigInteger (java.math.BigInteger)3 ArrayList (java.util.ArrayList)3 ASTRange (org.matheclipse.core.expression.ASTRange)3 IComplex (org.matheclipse.core.interfaces.IComplex)3 BigInteger (edu.jas.arith.BigInteger)2 ModLong (edu.jas.arith.ModLong)2 LogIntegral (edu.jas.integrate.LogIntegral)2 AlgebraicNumber (edu.jas.poly.AlgebraicNumber)2 TermOrder (edu.jas.poly.TermOrder)2 SortedMap (java.util.SortedMap)2 ISymbol (org.matheclipse.core.interfaces.ISymbol)2