Search in sources :

Example 1 with Complex

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

the class JASIExpr method complexPoly2Expr.

/**
	 * Convert a JAS complex polynomial to <code>IExpr</code>.
	 * 
	 * @param poly
	 * @return
	 * @throws ArithmeticException
	 * @throws ClassCastException
	 */
public IExpr complexPoly2Expr(final GenPolynomial<Complex<BigRational>> poly) throws ArithmeticException, ClassCastException {
    if (poly.length() == 0) {
        return F.C0;
    }
    IAST result = F.Plus();
    for (Monomial<Complex<BigRational>> monomial : poly) {
        Complex<BigRational> coeff = monomial.coefficient();
        ExpVector exp = monomial.exponent();
        BigRational re = coeff.getRe();
        BigRational im = coeff.getIm();
        IAST monomTimes = F.Times(F.complex(F.fraction(re.numerator(), re.denominator()), F.fraction(im.numerator(), im.denominator())));
        long lExp;
        for (int i = 0; i < exp.length(); i++) {
            lExp = exp.getVal(i);
            if (lExp != 0) {
                monomTimes.append(F.Power(fVariables.get(i), F.integer(lExp)));
            }
        }
        if (monomTimes.isAST1()) {
            result.append(monomTimes.arg1());
        } else {
            result.append(monomTimes);
        }
    }
    if (result.isAST1()) {
        return result.arg1();
    } else {
        return result;
    }
}
Also used : BigRational(edu.jas.arith.BigRational) ExpVector(edu.jas.poly.ExpVector) IAST(org.matheclipse.core.interfaces.IAST) Complex(edu.jas.poly.Complex)

Example 2 with Complex

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

the class JASConvert method complexPoly2Expr.

/**
	 * Convert a JAS complex polynomial to <code>IExpr</code>.
	 * 
	 * @param poly
	 * @return
	 * @throws ArithmeticException
	 * @throws ClassCastException
	 */
public IExpr complexPoly2Expr(final GenPolynomial<Complex<BigRational>> poly) throws ArithmeticException, ClassCastException {
    if (poly.length() == 0) {
        return F.C0;
    }
    IAST result = F.Plus();
    for (Monomial<Complex<BigRational>> monomial : poly) {
        Complex<BigRational> coeff = monomial.coefficient();
        ExpVector exp = monomial.exponent();
        IAST monomTimes = F.Times();
        monomialToExpr(coeff, exp, monomTimes);
        result.append(monomTimes.getOneIdentity(F.C1));
    }
    return result.getOneIdentity(F.C0);
}
Also used : BigRational(edu.jas.arith.BigRational) ExpVector(edu.jas.poly.ExpVector) IAST(org.matheclipse.core.interfaces.IAST) IComplex(org.matheclipse.core.interfaces.IComplex) Complex(edu.jas.poly.Complex)

Example 3 with Complex

use of edu.jas.poly.Complex 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 4 with Complex

use of edu.jas.poly.Complex 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 5 with Complex

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

the class JASConvert method fraction2Poly.

private GenPolynomial<C> 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);
    if (fRingFactory instanceof ComplexRing) {
        ComplexRing ring = (ComplexRing) fRingFactory;
        Complex<BigRational> c = new Complex<BigRational>(ring, r);
        return new GenPolynomial(fPolyFactory, c);
    } else {
        return new GenPolynomial(fPolyFactory, r);
    }
}
Also used : GenPolynomial(edu.jas.poly.GenPolynomial) BigRational(edu.jas.arith.BigRational) ComplexRing(edu.jas.poly.ComplexRing) BigInteger(java.math.BigInteger) IComplex(org.matheclipse.core.interfaces.IComplex) Complex(edu.jas.poly.Complex)

Aggregations

BigRational (edu.jas.arith.BigRational)5 Complex (edu.jas.poly.Complex)5 IAST (org.matheclipse.core.interfaces.IAST)4 ComplexRing (edu.jas.poly.ComplexRing)3 IComplex (org.matheclipse.core.interfaces.IComplex)3 ExpVector (edu.jas.poly.ExpVector)2 GenPolynomial (edu.jas.poly.GenPolynomial)2 BigInteger (edu.jas.arith.BigInteger)1 ModLong (edu.jas.arith.ModLong)1 GenPolynomialRing (edu.jas.poly.GenPolynomialRing)1 TermOrder (edu.jas.poly.TermOrder)1 ComplexRootsSturm (edu.jas.root.ComplexRootsSturm)1 InvalidBoundaryException (edu.jas.root.InvalidBoundaryException)1 Rectangle (edu.jas.root.Rectangle)1 FactorComplex (edu.jas.ufd.FactorComplex)1 BigInteger (java.math.BigInteger)1 SortedMap (java.util.SortedMap)1 JASConvert (org.matheclipse.core.convert.JASConvert)1 VariablesSet (org.matheclipse.core.convert.VariablesSet)1 JASConversionException (org.matheclipse.core.eval.exception.JASConversionException)1