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 "list of rules list" 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;
}
}
Aggregations