Search in sources :

Example 1 with NoEvalException

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

the class Solve method solveEquations.

/**
 * @param termsEqualZeroList the list of expressions, 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 static IASTMutable solveEquations(IASTMutable termsEqualZeroList, IAST inequationsList, IAST variables, int maximumNumberOfResults, EvalEngine engine) {
    try {
        IASTMutable list = PolynomialFunctions.solveGroebnerBasis(termsEqualZeroList, variables);
        if (list.isPresent()) {
            termsEqualZeroList = list;
        }
    } catch (JASConversionException e) {
        LOGGER.debug("Solve.solveEquations() failed", e);
    }
    // rewrite some special expressions
    for (int i = 1; i < termsEqualZeroList.size(); i++) {
        IExpr equationTerm = termsEqualZeroList.get(i);
        if (equationTerm.isPlus()) {
            IExpr eq = S.Equal.of(equationTerm, F.C0);
            if (eq.isEqual()) {
                IExpr arg1 = eq.first();
                if (arg1.isPlus2()) {
                    if (arg1.first().isSqrtExpr() && arg1.second().isSqrtExpr()) {
                        // Sqrt() + Sqrt() == constant
                        termsEqualZeroList.set(i, S.Subtract.of(S.Expand.of(F.Sqr(arg1.second())), S.Expand.of(F.Sqr(F.Subtract(eq.second(), arg1.first())))));
                    }
                }
            }
        }
    }
    ExprAnalyzer exprAnalyzer;
    ArrayList<ExprAnalyzer> analyzerList = new ArrayList<ExprAnalyzer>();
    IsWrongSolveExpression predicate = new IsWrongSolveExpression();
    // collect linear and univariate polynomial equations:
    for (IExpr expr : termsEqualZeroList) {
        if (expr.has(predicate, true)) {
            LOGGER.log(engine.getLogLevel(), "Solve: the system contains the wrong object: {}", predicate.getWrongExpr());
            throw new NoEvalException();
        }
        exprAnalyzer = new ExprAnalyzer(expr, variables, engine);
        exprAnalyzer.simplifyAndAnalyze();
        analyzerList.add(exprAnalyzer);
    }
    IASTAppendable matrix = F.ListAlloc();
    IASTAppendable vector = F.ListAlloc();
    try {
        IASTAppendable resultList = F.ListAlloc();
        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);
            if (augmentedMatrix != null) {
                IAST subSolutionList = LinearAlgebra.rowReduced2RulesList(augmentedMatrix, variables, resultList, engine);
                return solveInequations((IASTMutable) subSolutionList, inequationsList, maximumNumberOfResults, engine);
            }
            return F.NIL;
        }
        return solveInequations(resultList, inequationsList, maximumNumberOfResults, engine);
    // return sortASTArguments(resultList);
    } catch (NoSolution e) {
        if (e.getType() == NoSolution.WRONG_SOLUTION) {
            return F.ListAlloc();
        }
        return F.NIL;
    }
}
Also used : ArrayList(java.util.ArrayList) IASTMutable(org.matheclipse.core.interfaces.IASTMutable) JASConversionException(org.matheclipse.core.eval.exception.JASConversionException) NoEvalException(org.matheclipse.core.eval.exception.NoEvalException) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

Aggregations

ArrayList (java.util.ArrayList)1 JASConversionException (org.matheclipse.core.eval.exception.JASConversionException)1 NoEvalException (org.matheclipse.core.eval.exception.NoEvalException)1 IAST (org.matheclipse.core.interfaces.IAST)1 IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)1 IASTMutable (org.matheclipse.core.interfaces.IASTMutable)1 IExpr (org.matheclipse.core.interfaces.IExpr)1