Search in sources :

Example 6 with JASConversionException

use of org.matheclipse.core.eval.exception.JASConversionException in project symja_android_library by axkr.

the class Roots method rootsOfQuadraticExprPolynomial.

/**
	 * Solve a polynomial with degree <= 2.
	 * 
	 * @param expr
	 * @param varList
	 * @return <code>F.NIL</code> if no evaluation was possible.
	 */
private static IAST rootsOfQuadraticExprPolynomial(final IExpr expr, IAST varList) {
    IAST result = F.NIL;
    try {
        // try to generate a common expression polynomial
        ExprPolynomialRing ring = new ExprPolynomialRing(ExprRingFactory.CONST, varList);
        ExprPolynomial ePoly = ring.create(expr, false, false);
        ePoly = ePoly.multiplyByMinimumNegativeExponents();
        result = rootsOfQuadraticPolynomial(ePoly);
        if (result.isPresent() && expr.isNumericMode()) {
            for (int i = 1; i < result.size(); i++) {
                result.set(i, F.chopExpr(result.get(i), Config.DEFAULT_ROOTS_CHOP_DELTA));
            }
        }
    } catch (JASConversionException e2) {
        if (Config.SHOW_STACKTRACE) {
            e2.printStackTrace();
        }
    }
    return result;
}
Also used : ExprPolynomialRing(org.matheclipse.core.polynomials.ExprPolynomialRing) IAST(org.matheclipse.core.interfaces.IAST) JASConversionException(org.matheclipse.core.eval.exception.JASConversionException) ExprPolynomial(org.matheclipse.core.polynomials.ExprPolynomial)

Example 7 with JASConversionException

use of org.matheclipse.core.eval.exception.JASConversionException in project symja_android_library by axkr.

the class Roots method rootsOfExprPolynomial.

private static IAST rootsOfExprPolynomial(final IExpr expr, IAST varList, boolean rootsOfQuartic) {
    IAST result = F.NIL;
    try {
        // try to generate a common expression polynomial
        ExprPolynomialRing ring = new ExprPolynomialRing(ExprRingFactory.CONST, varList);
        ExprPolynomial ePoly = ring.create(expr, false, false);
        ePoly = ePoly.multiplyByMinimumNegativeExponents();
        if (ePoly.degree(0) >= 3) {
            result = unitPolynomial(ePoly.degree(0), ePoly);
            if (result.isPresent()) {
                result = QuarticSolver.createSet(result);
                return result;
            }
        }
        if (!rootsOfQuartic && ePoly.degree(0) > 2) {
            return F.NIL;
        }
        result = rootsOfQuarticPolynomial(ePoly);
        if (result.isPresent()) {
            if (expr.isNumericMode()) {
                for (int i = 1; i < result.size(); i++) {
                    result.set(i, F.chopExpr(result.get(i), Config.DEFAULT_ROOTS_CHOP_DELTA));
                }
            }
            return result;
        }
    } catch (JASConversionException e2) {
        if (Config.SHOW_STACKTRACE) {
            e2.printStackTrace();
        }
    }
    return F.NIL;
}
Also used : ExprPolynomialRing(org.matheclipse.core.polynomials.ExprPolynomialRing) IAST(org.matheclipse.core.interfaces.IAST) JASConversionException(org.matheclipse.core.eval.exception.JASConversionException) ExprPolynomial(org.matheclipse.core.polynomials.ExprPolynomial)

Example 8 with JASConversionException

use of org.matheclipse.core.eval.exception.JASConversionException in project symja_android_library by axkr.

the class MonomialList method evaluate.

@Override
public IExpr evaluate(final IAST ast, final EvalEngine engine) {
    Validate.checkRange(ast, 2, 5);
    IExpr expr = F.evalExpandAll(ast.arg1());
    VariablesSet eVar;
    IAST symbolList = F.List();
    List<IExpr> varList;
    if (ast.isAST1()) {
        // extract all variables from the polynomial expression
        eVar = new VariablesSet(ast.arg1());
        eVar.appendToList(symbolList.args());
        varList = eVar.getArrayList();
    } else {
        symbolList = Validate.checkSymbolOrSymbolList(ast, 2);
        varList = new ArrayList<IExpr>(symbolList.size() - 1);
        for (int i = 1; i < symbolList.size(); i++) {
            varList.add(symbolList.get(i));
        }
    }
    TermOrder termOrder = TermOrderByName.Lexicographic;
    try {
        if (ast.size() > 3) {
            if (ast.arg3() instanceof IStringX) {
                // NegativeLexicographic
                String orderStr = ast.arg3().toString();
                termOrder = Options.getMonomialOrder(orderStr, termOrder);
            }
            final Options options = new Options(ast.topHead(), ast, 2, engine);
            IExpr option = options.getOption("Modulus");
            if (option.isSignedNumber()) {
                return monomialListModulus(expr, varList, termOrder, option);
            }
        }
        if (USE_JAS_POLYNOMIAL) {
            return monomialList(expr, varList, termOrder);
        } else {
            ExprPolynomialRing ring = new ExprPolynomialRing(symbolList, new ExprTermOrder(termOrder.getEvord()));
            ExprPolynomial poly = ring.create(expr);
            return poly.monomialList();
        }
    } catch (JASConversionException jce) {
        // toInt() conversion failed
        if (Config.DEBUG) {
            jce.printStackTrace();
        }
    }
    return F.NIL;
}
Also used : Options(org.matheclipse.core.eval.util.Options) TermOrder(edu.jas.poly.TermOrder) ExprTermOrder(org.matheclipse.core.polynomials.ExprTermOrder) VariablesSet(org.matheclipse.core.convert.VariablesSet) JASConversionException(org.matheclipse.core.eval.exception.JASConversionException) ExprPolynomialRing(org.matheclipse.core.polynomials.ExprPolynomialRing) ExprTermOrder(org.matheclipse.core.polynomials.ExprTermOrder) JASIExpr(org.matheclipse.core.convert.JASIExpr) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) IStringX(org.matheclipse.core.interfaces.IStringX) ExprPolynomial(org.matheclipse.core.polynomials.ExprPolynomial)

Example 9 with JASConversionException

use of org.matheclipse.core.eval.exception.JASConversionException in project symja_android_library by axkr.

the class CoefficientRules method evaluate.

@Override
public IExpr evaluate(final IAST ast, final EvalEngine engine) {
    Validate.checkRange(ast, 2, 5);
    IExpr expr = F.evalExpandAll(ast.arg1());
    VariablesSet eVar;
    IAST symbolList = F.List();
    List<IExpr> varList;
    if (ast.isAST1()) {
        // extract all variables from the polynomial expression
        eVar = new VariablesSet(ast.arg1());
        eVar.appendToList(symbolList.args());
        varList = eVar.getArrayList();
    } else {
        symbolList = Validate.checkSymbolOrSymbolList(ast, 2);
        varList = new ArrayList<IExpr>(symbolList.size() - 1);
        for (int i = 1; i < symbolList.size(); i++) {
            varList.add((ISymbol) symbolList.get(i));
        }
    }
    TermOrder termOrder = TermOrderByName.Lexicographic;
    try {
        if (ast.size() > 3) {
            if (ast.arg3() instanceof IStringX) {
                String orderStr = ast.arg3().toString();
                termOrder = Options.getMonomialOrder(orderStr, termOrder);
            }
            final Options options = new Options(ast.topHead(), ast, 2, engine);
            IExpr option = options.getOption("Modulus");
            if (option.isSignedNumber()) {
                return coefficientRulesModulus(expr, varList, termOrder, option);
            }
        }
        if (MonomialList.USE_JAS_POLYNOMIAL) {
            return coefficientRules(expr, varList, termOrder);
        } else {
            ExprPolynomialRing ring = new ExprPolynomialRing(symbolList, new ExprTermOrder(termOrder.getEvord()));
            ExprPolynomial poly = ring.create(expr);
            return poly.coefficientRules();
        }
    } catch (JASConversionException jce) {
        // toInt() conversion failed
        if (Config.DEBUG) {
            jce.printStackTrace();
        }
    }
    return F.NIL;
}
Also used : Options(org.matheclipse.core.eval.util.Options) TermOrder(edu.jas.poly.TermOrder) ExprTermOrder(org.matheclipse.core.polynomials.ExprTermOrder) VariablesSet(org.matheclipse.core.convert.VariablesSet) JASConversionException(org.matheclipse.core.eval.exception.JASConversionException) ExprPolynomialRing(org.matheclipse.core.polynomials.ExprPolynomialRing) ExprTermOrder(org.matheclipse.core.polynomials.ExprTermOrder) JASIExpr(org.matheclipse.core.convert.JASIExpr) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) IStringX(org.matheclipse.core.interfaces.IStringX) ExprPolynomial(org.matheclipse.core.polynomials.ExprPolynomial)

Example 10 with JASConversionException

use of org.matheclipse.core.eval.exception.JASConversionException 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)

Aggregations

JASConversionException (org.matheclipse.core.eval.exception.JASConversionException)10 IAST (org.matheclipse.core.interfaces.IAST)10 IExpr (org.matheclipse.core.interfaces.IExpr)8 BigRational (edu.jas.arith.BigRational)5 JASConvert (org.matheclipse.core.convert.JASConvert)5 ArrayList (java.util.ArrayList)4 ExprPolynomial (org.matheclipse.core.polynomials.ExprPolynomial)4 ExprPolynomialRing (org.matheclipse.core.polynomials.ExprPolynomialRing)4 GenPolynomial (edu.jas.poly.GenPolynomial)3 TermOrder (edu.jas.poly.TermOrder)3 JASIExpr (org.matheclipse.core.convert.JASIExpr)3 VariablesSet (org.matheclipse.core.convert.VariablesSet)3 ASTRange (org.matheclipse.core.expression.ASTRange)3 Options (org.matheclipse.core.eval.util.Options)2 IStringX (org.matheclipse.core.interfaces.IStringX)2 ISymbol (org.matheclipse.core.interfaces.ISymbol)2 ExprTermOrder (org.matheclipse.core.polynomials.ExprTermOrder)2 ModLong (edu.jas.arith.ModLong)1 GroebnerBasePartial (edu.jas.gbufd.GroebnerBasePartial)1 Complex (edu.jas.poly.Complex)1