use of org.matheclipse.core.eval.exception.LimitException in project symja_android_library by axkr.
the class PolynomialFunctions method coefficientList.
public static IAST coefficientList(IExpr expr, IAST listOfVariables) {
try {
ExprPolynomialRing ring = new ExprPolynomialRing(listOfVariables);
ExprPolynomial poly = ring.create(expr, true, false, true);
if (poly.isZero()) {
return F.CEmptyList;
}
return poly.coefficientList();
} catch (LimitException le) {
throw le;
} catch (RuntimeException ex) {
// org.matheclipse.core.polynomials.longexponent.ExprPolynomialRing.create()
LOGGER.debug("PolynomialFunctions.coefficientList() failed", ex);
}
if (listOfVariables.argSize() > 0) {
return F.Nest(S.List, expr, listOfVariables.argSize());
}
return F.NIL;
}
use of org.matheclipse.core.eval.exception.LimitException in project symja_android_library by axkr.
the class Solve method of.
/**
* @param ast the <code>Solve(...)</code> ast
* @param numeric if true, try to find a numerically solution
* @param engine
* @return
*/
public static IExpr of(final IAST ast, boolean numeric, EvalEngine engine) {
boolean[] isNumeric = new boolean[] { numeric };
try {
if (ast.arg1().isEmptyList()) {
return F.list(F.CEmptyList);
}
IAST userDefinedVariables = Validate.checkIsVariableOrVariableList(ast, 2, ast.topHead(), engine);
if (userDefinedVariables.isPresent()) {
IAST equationVariables = VariablesSet.getVariables(ast.arg1());
if (userDefinedVariables.isEmpty()) {
userDefinedVariables = equationVariables;
}
ISymbol domain = S.Complexes;
if (ast.isAST3()) {
if (!ast.arg3().isSymbol()) {
LOGGER.log(engine.getLogLevel(), "{}: domain definition expected at position 3 instead of {}", ast.topHead(), ast.arg3());
return F.NIL;
}
domain = (ISymbol) ast.arg3();
if (domain.equals(S.Booleans)) {
return BooleanFunctions.solveInstances(ast.arg1(), userDefinedVariables, Integer.MAX_VALUE);
}
if (domain.equals(S.Integers)) {
return solveIntegers(ast, equationVariables, userDefinedVariables, Integer.MAX_VALUE, engine);
}
if (!domain.equals(S.Reals) && !domain.equals(S.Complexes)) {
Level level = engine.getLogLevel();
LOGGER.log(level, "{}: domain definition expected at position 3 instead of {}", ast.topHead(), domain);
return F.NIL;
}
}
IAssumptions oldAssumptions = engine.getAssumptions();
try {
IAssumptions assum = setVariablesReals(userDefinedVariables, domain);
if (assum != null) {
engine.setAssumptions(assum);
}
IAST termsList = Validate.checkEquationsAndInequations(ast, 1);
IASTMutable[] lists = SolveUtils.filterSolveLists(termsList, F.NIL, isNumeric);
boolean numericFlag = isNumeric[0] || numeric;
if (lists[2].isPresent()) {
IExpr result = solveNumeric(lists[2], numericFlag, engine);
if (!result.isPresent()) {
return IOFunctions.printMessage(ast.topHead(), "nsmet", F.list(ast.topHead()), engine);
}
return checkDomain(result, domain);
}
IASTMutable termsEqualZeroList = lists[0];
IExpr result = solveRecursive(termsEqualZeroList, lists[1], numericFlag, userDefinedVariables, engine);
if (!result.isPresent()) {
return IOFunctions.printMessage(ast.topHead(), "nsmet", F.list(ast.topHead()), engine);
}
return checkDomain(result, domain);
} finally {
engine.setAssumptions(oldAssumptions);
}
}
} catch (ValidateException ve) {
return IOFunctions.printMessage(S.Solve, ve, engine);
} catch (LimitException e) {
LOGGER.log(engine.getLogLevel(), S.Solve, e);
} catch (RuntimeException rex) {
LOGGER.debug("Solve.of() failed() failed", rex);
}
return F.NIL;
}
use of org.matheclipse.core.eval.exception.LimitException in project symja_android_library by axkr.
the class Solve method solveTimesEquationsRecursively.
/**
* Analyze the <code>termsEqualZeroList</code> if it contains a <code>Times[..., ,...]</code>
* expression. If true, set the factors equal to <code>0</code> and solve the equations
* recursively.
*
* @param termsEqualZeroList the list of expressions, which should equal <code>0</code>
* @param numericFlag
* @param variables the variables for which the equations should be solved
* @param engine the evaluation engine
* @return
*/
private static IASTMutable solveTimesEquationsRecursively(IASTMutable termsEqualZeroList, IAST inequationsList, boolean numericFlag, IAST variables, boolean multipleValues, EvalEngine engine) {
try {
IASTMutable resultList = solveEquations(termsEqualZeroList, inequationsList, variables, 0, engine);
if (resultList.isPresent() && !resultList.isEmpty()) {
return resultList;
}
Set<IExpr> subSolutionSet = new TreeSet<IExpr>();
for (int i = 1; i < termsEqualZeroList.size(); i++) {
IExpr termEQZero = termsEqualZeroList.get(i);
if (termEQZero.isTimes()) {
solveTimesAST((IAST) termEQZero, termsEqualZeroList, inequationsList, numericFlag, variables, multipleValues, engine, subSolutionSet, i);
} else {
if (termEQZero.isAST()) {
// try factoring
termEQZero = S.Factor.of(engine, termEQZero);
if (termEQZero.isTimes()) {
solveTimesAST((IAST) termEQZero, termsEqualZeroList, inequationsList, numericFlag, variables, multipleValues, engine, subSolutionSet, i);
}
}
}
}
if (subSolutionSet.size() > 0) {
return F.ListAlloc(subSolutionSet);
}
return resultList;
} catch (LimitException le) {
LOGGER.debug("Solve.solveTimesEquationsRecursively() failed", le);
throw le;
} catch (RuntimeException rex) {
LOGGER.debug("Solve.solveTimesEquationsRecursively() failed", rex);
if (Config.SHOW_STACKTRACE) {
rex.printStackTrace();
}
}
return F.NIL;
}
Aggregations