Search in sources :

Example 6 with JASConvert

use of org.matheclipse.core.convert.JASConvert in project symja_android_library by axkr.

the class GroebnerBasis method solveGroebnerBasis.

/**
	 * Used in <code>Solve()</code> function to reduce the polynomial list of
	 * equations.
	 * 
	 * @param listOfPolynomials
	 *            a list of polynomials
	 * @param listOfVariables
	 *            a list of variable symbols
	 * @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
	 */
public static IAST solveGroebnerBasis(IAST listOfPolynomials, IAST listOfVariables) {
    List<ISymbol> varList = new ArrayList<ISymbol>(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));
    }
    IAST rest = F.List();
    List<GenPolynomial<BigRational>> polyList = new ArrayList<GenPolynomial<BigRational>>(listOfPolynomials.size() - 1);
    TermOrder termOrder = TermOrderByName.IGRLEX;
    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) {
            rest.append(expr);
        }
    }
    if (polyList.size() == 0) {
        return F.NIL;
    }
    GroebnerBaseAbstract<BigRational> engine = GBAlgorithmBuilder.<BigRational>polynomialRing(jas.getPolynomialRingFactory()).fractionFree().syzygyPairlist().build();
    List<GenPolynomial<BigRational>> opl = engine.GB(polyList);
    IAST resultList = F.List();
    // polynomial to result list
    for (GenPolynomial<BigRational> p : opl) {
        resultList.append(jas.integerPoly2Expr((GenPolynomial<BigInteger>) jas.factorTerms(p)[2]));
    }
    for (int i = 1; i < rest.size(); i++) {
        resultList.append(rest.get(i));
    }
    return resultList;
}
Also used : GenPolynomial(edu.jas.poly.GenPolynomial) TermOrder(edu.jas.poly.TermOrder) ISymbol(org.matheclipse.core.interfaces.ISymbol) BigRational(edu.jas.arith.BigRational) ArrayList(java.util.ArrayList) JASConversionException(org.matheclipse.core.eval.exception.JASConversionException) JASConvert(org.matheclipse.core.convert.JASConvert) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 7 with JASConvert

use of org.matheclipse.core.convert.JASConvert in project symja_android_library by axkr.

the class Algebra method cancelGCD.

/**
 * Calculate the 3 elements result array
 *
 * <pre>
 * [
 *   commonFactor,
 *   numeratorPolynomial.divide(gcd(numeratorPolynomial, denominatorPolynomial)),
 *   denominatorPolynomial.divide(gcd(numeratorPolynomial, denominatorPolynomial))
 * ]
 * </pre>
 *
 * for the given expressions <code>numeratorPolynomial</code> and <code>denominatorPolynomial
 * </code>.
 *
 * @param numerator an expression which should be converted to JAS polynomial (using
 *        substitutions)
 * @param denominator a expression which could be converted to JAS polynomial (using
 *        substitutions)
 * @return <code>null</code> if the expressions couldn't be converted to JAS polynomials or gcd
 *         equals 1
 * @throws JASConversionException
 */
public static IExpr[] cancelGCD(final IExpr numerator, final IExpr denominator) throws JASConversionException {
    try {
        if (denominator.isInteger() && numerator.isPlus()) {
            IExpr[] result = Cancel.cancelPlusIntegerGCD((IAST) numerator, (IInteger) denominator);
            if (result != null) {
                return result;
            }
        }
        VariablesSet eVar = new VariablesSet(numerator);
        eVar.addVarList(denominator);
        if (eVar.size() == 0) {
            return null;
        }
        IAST vars = eVar.getVarList();
        PolynomialHomogenization substitutions = new PolynomialHomogenization(vars, EvalEngine.get());
        IExpr[] subst = substitutions.replaceForward(numerator, denominator);
        IExpr numeratorPolynomial = subst[0];
        IExpr denominatorPolynomial = subst[1];
        if (substitutions.size() > 0) {
            eVar.clear();
            eVar.addAll(substitutions.substitutedVariablesSet());
            vars = eVar.getVarList();
        }
        try {
            ExprPolynomialRing ring = new ExprPolynomialRing(vars);
            ExprPolynomial pol1 = ring.create(numeratorPolynomial);
            ExprPolynomial pol2 = ring.create(denominatorPolynomial);
            List<IExpr> varList = eVar.getVarList().copyTo();
            JASIExpr jas = new JASIExpr(varList, true);
            GenPolynomial<IExpr> p1 = jas.expr2IExprJAS(pol1);
            GenPolynomial<IExpr> p2 = jas.expr2IExprJAS(pol2);
            GreatestCommonDivisor<IExpr> engine;
            engine = GCDFactory.getImplementation(ExprRingFactory.CONST);
            GenPolynomial<IExpr> gcd = engine.gcd(p1, p2);
            IExpr[] result = new IExpr[3];
            if (gcd.isONE()) {
                return null;
            // result[0] = jas.exprPoly2Expr(gcd);
            // result[1] = jas.exprPoly2Expr(p1);
            // result[2] = jas.exprPoly2Expr(p2);
            } else {
                result[0] = F.C1;
                result[1] = F.eval(jas.exprPoly2Expr(p1.divide(gcd)));
                result[2] = F.eval(jas.exprPoly2Expr(p2.divide(gcd)));
            }
            result[0] = substitutions.replaceBackward(result[0]);
            result[1] = substitutions.replaceBackward(result[1]);
            result[2] = substitutions.replaceBackward(result[2]);
            return result;
        } catch (RuntimeException rex) {
        }
        List<IExpr> varList = eVar.getVarList().copyTo();
        ComplexRing<BigRational> cfac = new ComplexRing<BigRational>(BigRational.ZERO);
        JASConvert<Complex<BigRational>> jas = new JASConvert<Complex<BigRational>>(varList, cfac);
        GenPolynomial<Complex<BigRational>> p1 = jas.expr2JAS(numeratorPolynomial, false);
        GenPolynomial<Complex<BigRational>> p2 = jas.expr2JAS(denominatorPolynomial, false);
        GreatestCommonDivisor<Complex<BigRational>> engine;
        engine = GCDFactory.getImplementation(cfac);
        GenPolynomial<Complex<BigRational>> gcd;
        // if (numeratorPolynomial.isSymbol()||denominatorPolynomial.isSymbol() ) {
        // gcd = jas.expr2IExprJAS(F.C1);
        // }else {
        gcd = engine.gcd(p1, p2);
        // }
        IExpr[] result = new IExpr[3];
        if (gcd.isONE()) {
            return null;
        // result[0] = jas.complexPoly2Expr(gcd);
        // result[1] = jas.complexPoly2Expr(p1);
        // result[2] = jas.complexPoly2Expr(p2);
        } else {
            result[0] = F.C1;
            result[1] = F.eval(jas.complexPoly2Expr(p1.divide(gcd)));
            result[2] = F.eval(jas.complexPoly2Expr(p2.divide(gcd)));
        }
        result[0] = substitutions.replaceBackward(result[0]);
        result[1] = substitutions.replaceBackward(result[1]);
        result[2] = substitutions.replaceBackward(result[2]);
        return result;
    } catch (RuntimeException e) {
        LOGGER.debug("Algebra.cancelGCD() failed", e);
    }
    return null;
}
Also used : PolynomialHomogenization(org.matheclipse.core.polynomials.PolynomialHomogenization) BigRational(edu.jas.arith.BigRational) VariablesSet(org.matheclipse.core.convert.VariablesSet) IComplex(org.matheclipse.core.interfaces.IComplex) FactorComplex(edu.jas.ufd.FactorComplex) Complex(edu.jas.poly.Complex) ExprPolynomialRing(org.matheclipse.core.polynomials.longexponent.ExprPolynomialRing) JASIExpr(org.matheclipse.core.convert.JASIExpr) ComplexRing(edu.jas.poly.ComplexRing) JASConvert(org.matheclipse.core.convert.JASConvert) JASIExpr(org.matheclipse.core.convert.JASIExpr) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) ExprPolynomial(org.matheclipse.core.polynomials.longexponent.ExprPolynomial)

Example 8 with JASConvert

use of org.matheclipse.core.convert.JASConvert in project symja_android_library by axkr.

the class PolynomialFunctions method solveGroebnerBasis.

/**
 * Used in <code>Solve()</code> function to reduce the polynomial list of equations.
 *
 * @param listOfPolynomials a list of polynomials
 * @param listOfVariables a list of variable symbols
 * @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
 */
public static IASTAppendable solveGroebnerBasis(IAST listOfPolynomials, IAST listOfVariables) {
    List<IExpr> varList = new ArrayList<IExpr>(listOfVariables.argSize());
    for (int i = 1; i < listOfVariables.size(); i++) {
        // if (!listOfVariables.get(i).isSymbol() ) {
        // return F.NIL;
        // }
        varList.add(listOfVariables.get(i));
    }
    List<GenPolynomial<BigRational>> polyList = new ArrayList<GenPolynomial<BigRational>>(listOfPolynomials.argSize());
    TermOrder termOrder = TermOrderByName.IGRLEX;
    JASConvert<BigRational> jas = new JASConvert<BigRational>(varList, BigRational.ZERO, termOrder);
    IASTAppendable rest = F.ListAlloc(8);
    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) {
            rest.append(expr);
        }
    }
    if (polyList.size() == 0) {
        return F.NIL;
    }
    GroebnerBaseAbstract<BigRational> engine = GBAlgorithmBuilder.<BigRational>polynomialRing(jas.getPolynomialRingFactory()).fractionFree().syzygyPairlist().build();
    List<GenPolynomial<BigRational>> opl = engine.GB(polyList);
    IASTAppendable resultList = F.ListAlloc(opl.size() + rest.size());
    // polynomial to result list
    for (GenPolynomial<BigRational> p : opl) {
        resultList.append(jas.integerPoly2Expr((GenPolynomial<edu.jas.arith.BigInteger>) jas.factorTerms(p)[2]));
    }
    resultList.appendArgs(rest);
    return resultList;
}
Also used : GenPolynomial(edu.jas.poly.GenPolynomial) TermOrder(edu.jas.poly.TermOrder) ExprTermOrder(org.matheclipse.core.polynomials.longexponent.ExprTermOrder) BigRational(edu.jas.arith.BigRational) ArrayList(java.util.ArrayList) JASConversionException(org.matheclipse.core.eval.exception.JASConversionException) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) JASConvert(org.matheclipse.core.convert.JASConvert) JASIExpr(org.matheclipse.core.convert.JASIExpr) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 9 with JASConvert

use of org.matheclipse.core.convert.JASConvert in project symja_android_library by axkr.

the class RootsFunctions method rootsOfVariable.

/**
 * @param expr
 * @param denominator
 * @param variables
 * @param numericSolutions
 * @param engine
 * @return <code>F.NIL</code> if no evaluation was possible.
 */
public static IAST rootsOfVariable(final IExpr expr, final IExpr denominator, final IAST variables, boolean numericSolutions, EvalEngine engine) {
    IASTMutable result = F.NIL;
    // ASTRange r = new ASTRange(variables, 1);
    // List<IExpr> varList = r;
    List<IExpr> varList = variables.copyTo();
    try {
        IExpr temp;
        IAST list = rootsOfQuadraticExprPolynomial(expr, variables);
        if (list.isPresent()) {
            return list;
        }
        JASConvert<BigRational> jas = new JASConvert<BigRational>(varList, BigRational.ZERO);
        GenPolynomial<BigRational> polyRat = jas.expr2JAS(expr, numericSolutions);
        // if (polyRat.degree(0) <= 2) {
        result = rootsOfExprPolynomial(expr, variables, false);
        if (result.isPresent()) {
            return result;
        }
        // }
        IASTAppendable newResult = F.ListAlloc(8);
        IAST factorRational = Algebra.factorRational(polyRat, jas, S.List);
        for (int i = 1; i < factorRational.size(); i++) {
            temp = F.evalExpand(factorRational.get(i));
            IAST quarticResultList = QuarticSolver.solve(temp, variables.arg1());
            if (quarticResultList.isPresent()) {
                for (int j = 1; j < quarticResultList.size(); j++) {
                    if (numericSolutions) {
                        newResult.append(F.chopExpr(engine.evalN(quarticResultList.get(j)), Config.DEFAULT_ROOTS_CHOP_DELTA));
                    } else {
                        newResult.append(quarticResultList.get(j));
                    }
                }
            } else {
                polyRat = jas.expr2JAS(temp, numericSolutions);
                IAST factorComplex = Algebra.factorRational(polyRat, jas, S.List);
                for (int k = 1; k < factorComplex.size(); k++) {
                    temp = F.evalExpand(factorComplex.get(k));
                    quarticResultList = QuarticSolver.solve(temp, variables.arg1());
                    if (quarticResultList.isPresent()) {
                        for (int j = 1; j < quarticResultList.size(); j++) {
                            if (numericSolutions) {
                                newResult.append(F.chopExpr(engine.evalN(quarticResultList.get(j)), Config.DEFAULT_ROOTS_CHOP_DELTA));
                            } else {
                                newResult.append(quarticResultList.get(j));
                            }
                        }
                    } else {
                        double[] coefficients = coefficients(temp, (ISymbol) variables.arg1());
                        if (coefficients == null) {
                            return F.NIL;
                        }
                        IAST resultList = findRoots(coefficients);
                        // true);
                        if (resultList.size() > 0) {
                            newResult.appendArgs(resultList);
                        }
                    }
                }
            }
        }
        newResult = QuarticSolver.createSet(newResult);
        return newResult;
    } catch (RuntimeException rex) {
        // JAS or "findRoots" may throw RuntimeExceptions
        result = rootsOfExprPolynomial(expr, variables, true);
    }
    if (result.isPresent()) {
        if (!denominator.isNumber()) {
            // eliminate roots from the result list, which occur in the
            // denominator
            int i = 1;
            IASTAppendable appendable = F.NIL;
            while (i < result.size()) {
                IExpr temp = denominator.replaceAll(F.Rule(variables.arg1(), result.get(i)));
                if (temp.isPresent() && engine.evaluate(temp).isZero()) {
                    if (!appendable.isPresent()) {
                        appendable = result.removeAtClone(i);
                        continue;
                    }
                    appendable.remove(i);
                    continue;
                }
                i++;
            }
        }
        return result;
    }
    return F.NIL;
}
Also used : IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) BigRational(edu.jas.arith.BigRational) JASConvert(org.matheclipse.core.convert.JASConvert) IASTMutable(org.matheclipse.core.interfaces.IASTMutable) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

Aggregations

BigRational (edu.jas.arith.BigRational)9 JASConvert (org.matheclipse.core.convert.JASConvert)9 IExpr (org.matheclipse.core.interfaces.IExpr)9 IAST (org.matheclipse.core.interfaces.IAST)7 JASConversionException (org.matheclipse.core.eval.exception.JASConversionException)6 GenPolynomial (edu.jas.poly.GenPolynomial)5 ArrayList (java.util.ArrayList)5 JASIExpr (org.matheclipse.core.convert.JASIExpr)4 ASTRange (org.matheclipse.core.expression.ASTRange)3 ModLong (edu.jas.arith.ModLong)2 Complex (edu.jas.poly.Complex)2 ComplexRing (edu.jas.poly.ComplexRing)2 TermOrder (edu.jas.poly.TermOrder)2 List (java.util.List)2 VariablesSet (org.matheclipse.core.convert.VariablesSet)2 IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)2 ISymbol (org.matheclipse.core.interfaces.ISymbol)2 GroebnerBasePartial (edu.jas.gbufd.GroebnerBasePartial)1 ComplexRootsSturm (edu.jas.root.ComplexRootsSturm)1 InvalidBoundaryException (edu.jas.root.InvalidBoundaryException)1