Search in sources :

Example 1 with GenPolynomial

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

the class GroebnerBasis method computeGroebnerBasis.

/**
	 * 
	 * @param listOfPolynomials
	 *            a list of polynomials
	 * @param listOfVariables
	 *            a list of variable symbols
	 * @param termOrder
	 *            the term order
	 * @return <code>F.NIL</code> if
	 *         <code>stopUnevaluatedOnPolynomialConversionError==true</code> and
	 *         one of the polynomials in <code>listOfPolynomials</code> are not
	 *         convertible to JAS polynomials
	 */
private static IAST computeGroebnerBasis(IAST listOfPolynomials, IAST listOfVariables, TermOrder termOrder) {
    List<ISymbol> varList = new ArrayList<ISymbol>(listOfVariables.size() - 1);
    String[] pvars = new String[listOfVariables.size() - 1];
    for (int i = 1; i < listOfVariables.size(); i++) {
        if (!listOfVariables.get(i).isSymbol()) {
            return F.NIL;
        }
        varList.add((ISymbol) listOfVariables.get(i));
        pvars[i - 1] = ((ISymbol) listOfVariables.get(i)).toString();
    }
    List<GenPolynomial<BigRational>> polyList = new ArrayList<GenPolynomial<BigRational>>(listOfPolynomials.size() - 1);
    JASConvert<BigRational> jas = new JASConvert<BigRational>(varList, BigRational.ZERO, termOrder);
    for (int i = 1; i < listOfPolynomials.size(); i++) {
        IExpr expr = F.evalExpandAll(listOfPolynomials.get(i));
        try {
            GenPolynomial<BigRational> poly = jas.expr2JAS(expr, false);
            polyList.add(poly);
        } catch (JASConversionException e) {
            return F.NIL;
        }
    }
    if (polyList.size() == 0) {
        return F.NIL;
    }
    GroebnerBasePartial<BigRational> gbp = new GroebnerBasePartial<BigRational>();
    OptimizedPolynomialList<BigRational> opl = gbp.partialGB(polyList, pvars);
    List<GenPolynomial<BigRational>> list = OrderedPolynomialList.sort(opl.list);
    IAST resultList = F.List();
    for (GenPolynomial<BigRational> p : list) {
        // convert rational to integer coefficients and add
        // polynomial to result list
        resultList.append(jas.integerPoly2Expr((GenPolynomial<BigInteger>) jas.factorTerms(p)[2]));
    }
    return resultList;
}
Also used : GenPolynomial(edu.jas.poly.GenPolynomial) ISymbol(org.matheclipse.core.interfaces.ISymbol) BigRational(edu.jas.arith.BigRational) ArrayList(java.util.ArrayList) JASConversionException(org.matheclipse.core.eval.exception.JASConversionException) GroebnerBasePartial(edu.jas.gbufd.GroebnerBasePartial) JASConvert(org.matheclipse.core.convert.JASConvert) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

Example 2 with GenPolynomial

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

the class JASConvert method integral2Expr.

/**
	 * Convert a jas <code>Integral</code> into a matheclipse expression
	 * 
	 * @param integral
	 *            the JAS Integral
	 * @return
	 */
public IAST integral2Expr(Integral<BigRational> integral) {
    IAST sum = F.Plus();
    GenPolynomial<BigRational> pol = integral.pol;
    List<GenPolynomial<BigRational>> rational = integral.rational;
    List<LogIntegral<BigRational>> logarithm = integral.logarithm;
    if (!pol.isZERO()) {
        sum.append(rationalPoly2Expr(pol));
    }
    if (rational.size() != 0) {
        int i = 0;
        while (i < rational.size()) {
            sum.append(F.Times(rationalPoly2Expr(rational.get(i++)), F.Power(rationalPoly2Expr(rational.get(i++)), F.CN1)));
        }
    }
    if (logarithm.size() != 0) {
        for (LogIntegral<BigRational> pf : logarithm) {
            sum.append(logIntegral2Expr(pf));
        }
    }
    return sum;
}
Also used : GenPolynomial(edu.jas.poly.GenPolynomial) BigRational(edu.jas.arith.BigRational) IAST(org.matheclipse.core.interfaces.IAST) LogIntegral(edu.jas.integrate.LogIntegral)

Example 3 with GenPolynomial

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

the class JASConvert method logIntegral2Expr.

/**
	 * Convert a jas <code>LogIntegral</code> into a matheclipse expression
	 * 
	 * @param logIntegral
	 *            the JAS LogIntegral
	 * @return
	 */
public IAST logIntegral2Expr(LogIntegral<BigRational> logIntegral) {
    IAST plus = F.Plus();
    List<BigRational> cfactors = logIntegral.cfactors;
    List<GenPolynomial<BigRational>> cdenom = logIntegral.cdenom;
    List<AlgebraicNumber<BigRational>> afactors = logIntegral.afactors;
    List<GenPolynomial<AlgebraicNumber<BigRational>>> adenom = logIntegral.adenom;
    if (cfactors.size() > 0) {
        for (int i = 0; i < cfactors.size(); i++) {
            BigRational cp = cfactors.get(i);
            GenPolynomial<BigRational> p = cdenom.get(i);
            plus.append(F.Times(F.fraction(cp.numerator(), cp.denominator()), F.Log(rationalPoly2Expr(p))));
        }
    }
    // TODO implement this conversion for AlgebraicNumbers...
    if (afactors.size() > 0) {
        for (int i = 0; i < afactors.size(); i++) {
            AlgebraicNumber<BigRational> ap = afactors.get(i);
            AlgebraicNumberRing<BigRational> ar = ap.factory();
            GenPolynomial<AlgebraicNumber<BigRational>> p = adenom.get(i);
            if (p.degree(0) < ar.modul.degree(0) && ar.modul.degree(0) > 2) {
            }
            GenPolynomial<BigRational> v = ap.getVal();
            IAST times = F.Times();
            if (p.degree(0) < ar.modul.degree(0) && ar.modul.degree(0) > 2) {
                IAST rootOf = F.ast(F.RootOf);
                rootOf.append(rationalPoly2Expr(ar.modul));
                times.append(rootOf);
                throw new UnsupportedOperationException("JASConvert#logIntegral2Expr()");
            }
            times.append(rationalPoly2Expr(v));
            times.append(F.Log(polyAlgebraicNumber2Expr(p)));
            plus.append(times);
        }
    }
    return plus;
}
Also used : GenPolynomial(edu.jas.poly.GenPolynomial) BigRational(edu.jas.arith.BigRational) AlgebraicNumber(edu.jas.poly.AlgebraicNumber) IAST(org.matheclipse.core.interfaces.IAST)

Example 4 with GenPolynomial

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

the class JASIExpr method expr2IExprPoly.

private GenPolynomial<IExpr> expr2IExprPoly(final IExpr exprPoly) throws ArithmeticException, ClassCastException {
    if (exprPoly instanceof IAST) {
        final IAST ast = (IAST) exprPoly;
        GenPolynomial<IExpr> result = fPolyFactory.getZERO();
        GenPolynomial<IExpr> p = fPolyFactory.getZERO();
        if (ast.isPlus()) {
            IExpr expr = ast.arg1();
            result = expr2IExprPoly(expr);
            for (int i = 2; i < ast.size(); i++) {
                expr = ast.get(i);
                p = expr2IExprPoly(expr);
                result = result.sum(p);
            }
            return result;
        } else if (ast.isTimes()) {
            IExpr expr = ast.arg1();
            result = expr2IExprPoly(expr);
            for (int i = 2; i < ast.size(); i++) {
                expr = ast.get(i);
                p = expr2IExprPoly(expr);
                result = result.multiply(p);
            }
            return result;
        } else if (ast.isPower()) {
            final IExpr expr = ast.arg1();
            if (expr instanceof ISymbol) {
                ExpVector leer = fPolyFactory.evzero;
                int ix = leer.indexVar(expr.toString(), fPolyFactory.getVars());
                if (ix >= 0) {
                    int exponent = -1;
                    try {
                        exponent = Validate.checkPowerExponent(ast);
                    } catch (WrongArgumentType e) {
                    //
                    }
                    if (exponent < 0) {
                        throw new ArithmeticException("JASConvert:expr2Poly - invalid exponent: " + ast.arg2().toString());
                    }
                    ExpVector e = ExpVector.create(fVariables.size(), ix, exponent);
                    return fPolyFactory.getONE().multiply(e);
                }
            }
        } else if (fNumericFunction) {
            if (ast.isNumericFunction()) {
                return new GenPolynomial<IExpr>(fPolyFactory, ast);
            }
        }
    } else if (exprPoly instanceof ISymbol) {
        ExpVector leer = fPolyFactory.evzero;
        int ix = leer.indexVar(exprPoly.toString(), fPolyFactory.getVars());
        if (ix >= 0) {
            ExpVector e = ExpVector.create(fVariables.size(), ix, 1L);
            return fPolyFactory.getONE().multiply(e);
        }
        if (fNumericFunction) {
            if (exprPoly.isNumericFunction()) {
                return new GenPolynomial<IExpr>(fPolyFactory, exprPoly);
            }
            throw new ClassCastException(exprPoly.toString());
        } else {
            return new GenPolynomial<IExpr>(fPolyFactory, exprPoly);
        }
    } else if (exprPoly instanceof IInteger) {
        return new GenPolynomial<IExpr>(fPolyFactory, exprPoly);
    } else if (exprPoly instanceof IFraction) {
        return new GenPolynomial<IExpr>(fPolyFactory, exprPoly);
    }
    if (exprPoly.isFree(t -> fVariables.contains(t), true)) {
        return new GenPolynomial<IExpr>(fPolyFactory, exprPoly);
    } else {
        for (int i = 0; i < fVariables.size(); i++) {
            if (fVariables.get(i).equals(exprPoly)) {
                ExpVector e = ExpVector.create(fVariables.size(), i, 1L);
                return fPolyFactory.getONE().multiply(e);
            }
        }
    }
    throw new ClassCastException(exprPoly.toString());
}
Also used : IFraction(org.matheclipse.core.interfaces.IFraction) GenPolynomial(edu.jas.poly.GenPolynomial) ISymbol(org.matheclipse.core.interfaces.ISymbol) WrongArgumentType(org.matheclipse.core.eval.exception.WrongArgumentType) IInteger(org.matheclipse.core.interfaces.IInteger) ExpVector(edu.jas.poly.ExpVector) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 5 with GenPolynomial

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

the class JASModInteger method expr2IExprPoly.

private GenPolynomial<ModLong> expr2IExprPoly(final IExpr exprPoly) throws ArithmeticException, ClassCastException {
    if (exprPoly instanceof IAST) {
        final IAST ast = (IAST) exprPoly;
        GenPolynomial<ModLong> result = fPolyFactory.getZERO();
        GenPolynomial<ModLong> p = fPolyFactory.getZERO();
        if (ast.isPlus()) {
            IExpr expr = ast.arg1();
            result = expr2IExprPoly(expr);
            for (int i = 2; i < ast.size(); i++) {
                expr = ast.get(i);
                p = expr2IExprPoly(expr);
                result = result.sum(p);
            }
            return result;
        } else if (ast.isTimes()) {
            IExpr expr = ast.arg1();
            result = expr2IExprPoly(expr);
            for (int i = 2; i < ast.size(); i++) {
                expr = ast.get(i);
                p = expr2IExprPoly(expr);
                result = result.multiply(p);
            }
            return result;
        } else if (ast.isPower()) {
            final IExpr expr = ast.arg1();
            for (int i = 0; i < fVariables.size(); i++) {
                if (fVariables.get(i).equals(expr)) {
                    int exponent = -1;
                    try {
                        exponent = Validate.checkPowerExponent(ast);
                    } catch (WrongArgumentType e) {
                    //
                    }
                    if (exponent < 0) {
                        throw new ArithmeticException("JASConvert:expr2Poly - invalid exponent: " + ast.arg2().toString());
                    }
                    ExpVector e = ExpVector.create(fVariables.size(), i, exponent);
                    return fPolyFactory.getONE().multiply(e);
                }
            }
        }
    } else if (exprPoly instanceof ISymbol) {
        for (int i = 0; i < fVariables.size(); i++) {
            if (fVariables.get(i).equals(exprPoly)) {
                ExpVector e = ExpVector.create(fVariables.size(), i, 1L);
                return fPolyFactory.getONE().multiply(e);
            }
        }
        return new GenPolynomial(fPolyFactory, exprPoly);
    } else if (exprPoly instanceof IInteger) {
        return fPolyFactory.fromInteger((java.math.BigInteger) ((IInteger) exprPoly).asType(java.math.BigInteger.class));
    } else if (exprPoly instanceof IFraction) {
        return fraction2Poly((IFraction) exprPoly);
    }
    if (exprPoly.isFree(t -> fVariables.contains(t), true)) {
        return new GenPolynomial(fPolyFactory, exprPoly);
    } else {
        for (int i = 0; i < fVariables.size(); i++) {
            if (fVariables.get(i).equals(exprPoly)) {
                ExpVector e = ExpVector.create(fVariables.size(), i, 1L);
                return fPolyFactory.getONE().multiply(e);
            }
        }
    }
    throw new ClassCastException(exprPoly.toString());
}
Also used : IFraction(org.matheclipse.core.interfaces.IFraction) GenPolynomial(edu.jas.poly.GenPolynomial) ISymbol(org.matheclipse.core.interfaces.ISymbol) ModLong(edu.jas.arith.ModLong) WrongArgumentType(org.matheclipse.core.eval.exception.WrongArgumentType) IInteger(org.matheclipse.core.interfaces.IInteger) ExpVector(edu.jas.poly.ExpVector) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr)

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