Search in sources :

Example 1 with JASConversionException

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

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

the class Algebra method partialFractionDecompositionRational.

/**
	 * Returns an AST with head <code>Plus</code>, which contains the partial fraction decomposition of the numerator
	 * and denominator parts.
	 * 
	 * @param pf
	 *            partial fraction generator
	 * @param parts
	 * @param variable
	 *            a variable
	 * @return <code>F.NIL</code> if the partial fraction decomposition wasn't constructed
	 */
public static IExpr partialFractionDecompositionRational(IPartialFractionGenerator pf, IExpr[] parts, ISymbol variable) {
    try {
        IAST variableList = F.List(variable);
        IExpr exprNumerator = F.evalExpandAll(parts[0]);
        IExpr exprDenominator = F.evalExpandAll(parts[1]);
        ASTRange r = new ASTRange(variableList, 1);
        List<IExpr> varList = r;
        String[] varListStr = new String[1];
        varListStr[0] = variableList.arg1().toString();
        JASConvert<BigRational> jas = new JASConvert<BigRational>(varList, BigRational.ZERO);
        GenPolynomial<BigRational> numerator = jas.expr2JAS(exprNumerator, false);
        GenPolynomial<BigRational> denominator = jas.expr2JAS(exprDenominator, false);
        // get factors
        FactorAbstract<BigRational> factorAbstract = FactorFactory.getImplementation(BigRational.ZERO);
        SortedMap<GenPolynomial<BigRational>, Long> sfactors = factorAbstract.baseFactors(denominator);
        List<GenPolynomial<BigRational>> D = new ArrayList<GenPolynomial<BigRational>>(sfactors.keySet());
        SquarefreeAbstract<BigRational> sqf = SquarefreeFactory.getImplementation(BigRational.ZERO);
        List<List<GenPolynomial<BigRational>>> Ai = sqf.basePartialFraction(numerator, sfactors);
        if (Ai.size() > 0) {
            // IAST result = F.Plus();
            pf.allocPlus(Ai.size() * 2);
            pf.setJAS(jas);
            if (!Ai.get(0).get(0).isZERO()) {
                pf.addNonFractionalPart(Ai.get(0).get(0));
            }
            for (int i = 1; i < Ai.size(); i++) {
                List<GenPolynomial<BigRational>> list = Ai.get(i);
                int j = 0;
                for (GenPolynomial<BigRational> genPolynomial : list) {
                    if (!genPolynomial.isZERO()) {
                        GenPolynomial<BigRational> Di_1 = D.get(i - 1);
                        pf.addSinglePartialFraction(genPolynomial, Di_1, j);
                    }
                    j++;
                }
            }
            return pf.getResult();
        }
    } catch (JASConversionException e) {
        if (Config.DEBUG) {
            e.printStackTrace();
        }
    }
    return F.NIL;
}
Also used : ASTRange(org.matheclipse.core.expression.ASTRange) GenPolynomial(edu.jas.poly.GenPolynomial) BigRational(edu.jas.arith.BigRational) ArrayList(java.util.ArrayList) JASConversionException(org.matheclipse.core.eval.exception.JASConversionException) ModLong(edu.jas.arith.ModLong) JASConvert(org.matheclipse.core.convert.JASConvert) List(org.matheclipse.core.expression.F.List) List(java.util.List) ArrayList(java.util.ArrayList) IAST(org.matheclipse.core.interfaces.IAST) JASIExpr(org.matheclipse.core.convert.JASIExpr) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 3 with JASConversionException

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

the class Roots method rootsOfVariable.

/**
	 * 
	 * @param expr
	 * @param denominator
	 * @param variables
	 * @param numericSolutions
	 * @param engine
	 * @return <code>F.NIL</code> if no evaluation was possible.
	 */
protected static IAST rootsOfVariable(final IExpr expr, final IExpr denominator, final IAST variables, boolean numericSolutions, EvalEngine engine) {
    IAST result = F.NIL;
    ASTRange r = new ASTRange(variables, 1);
    List<IExpr> varList = r;
    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;
        }
        // }
        result = F.List();
        IAST factorRational = Algebra.factorRational(polyRat, jas, varList, F.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) {
                        result.append(F.chopExpr(engine.evalN(quarticResultList.get(j)), Config.DEFAULT_ROOTS_CHOP_DELTA));
                    } else {
                        result.append(quarticResultList.get(j));
                    }
                }
            } else {
                polyRat = jas.expr2JAS(temp, numericSolutions);
                IAST factorComplex = Algebra.factorComplex(polyRat, jas, varList, F.List, true);
                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) {
                                result.append(F.chopExpr(engine.evalN(quarticResultList.get(j)), Config.DEFAULT_ROOTS_CHOP_DELTA));
                            } else {
                                result.append(quarticResultList.get(j));
                            }
                        }
                    } else {
                        double[] coefficients = CoefficientList.coefficientList(temp, (ISymbol) variables.arg1());
                        if (coefficients == null) {
                            return F.NIL;
                        }
                        IAST resultList = findRoots(coefficients);
                        // true);
                        if (resultList.size() > 0) {
                            result.appendArgs(resultList);
                        }
                    }
                }
            }
        }
        result = QuarticSolver.createSet(result);
        return result;
    } catch (JASConversionException e) {
        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;
            while (i < result.size()) {
                IExpr temp = denominator.replaceAll(F.Rule(variables.arg1(), result.get(i)));
                if (temp.isPresent() && engine.evaluate(temp).isZero()) {
                    result.remove(i);
                    continue;
                }
                i++;
            }
        }
        return result;
    }
    return F.NIL;
}
Also used : ASTRange(org.matheclipse.core.expression.ASTRange) BigRational(edu.jas.arith.BigRational) JASConvert(org.matheclipse.core.convert.JASConvert) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr) JASConversionException(org.matheclipse.core.eval.exception.JASConversionException)

Example 4 with JASConversionException

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

the class Solve method solveEquations.

/**
	 * 
	 * @param termsEqualZeroList
	 *            the list of expressions extracted form the given equations, which should equal <code>0</code>
	 * @param variables
	 *            the variables for which the equations should be solved
	 * @param maximumNumberOfResults
	 *            the maximum number of results which should be returned
	 * @param engine
	 *            the evaluation engine
	 * @return a &quot;list of rules list&quot; which solves the equations, or an empty list if no solution exists, or
	 *         <code>F.NIL</code> if the equations are not solvable by this algorithm.
	 */
protected IAST solveEquations(IAST termsEqualZeroList, IAST variables, int maximumNumberOfResults, EvalEngine engine) {
    try {
        IAST list = GroebnerBasis.solveGroebnerBasis(termsEqualZeroList, variables);
        if (list.isPresent()) {
            termsEqualZeroList = list;
        }
    } catch (JASConversionException e) {
        if (Config.SHOW_STACKTRACE) {
            e.printStackTrace();
        }
    }
    ExprAnalyzer exprAnalyzer;
    ArrayList<ExprAnalyzer> analyzerList = new ArrayList<ExprAnalyzer>();
    IsWrongSolveExpression predicate = new IsWrongSolveExpression();
    // collect linear and univariate polynomial equations:
    for (IExpr expr : termsEqualZeroList) {
        if (expr.isMember(predicate, true)) {
            engine.printMessage("Solve: the system contains the wrong object: " + predicate.getWrongExpr().toString());
            return F.NIL;
        }
        exprAnalyzer = new ExprAnalyzer(expr, variables, engine);
        exprAnalyzer.simplifyAndAnalyze();
        analyzerList.add(exprAnalyzer);
    }
    IAST matrix = F.List();
    IAST vector = F.List();
    try {
        IAST resultList = F.List();
        resultList = analyzeSublist(analyzerList, variables, resultList, maximumNumberOfResults, matrix, vector, engine);
        if (vector.size() > 1) {
            // solve a linear equation <code>matrix.x == vector</code>
            FieldMatrix<IExpr> augmentedMatrix = Convert.list2Matrix(matrix, vector);
            return LinearAlgebra.rowReduced2RulesList(augmentedMatrix, variables, resultList, engine);
        }
        return sortASTArguments(resultList);
    } catch (NoSolution e) {
        if (e.getType() == NoSolution.WRONG_SOLUTION) {
            return F.List();
        }
        return F.NIL;
    }
}
Also used : ArrayList(java.util.ArrayList) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr) JASConversionException(org.matheclipse.core.eval.exception.JASConversionException)

Example 5 with JASConversionException

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

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