use of org.matheclipse.core.frobenius.FrobeniusSolver in project symja_android_library by axkr.
the class FrobeniusSolve method evaluate.
/** {@inheritDoc} */
@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
Validate.checkRange(ast, 3, 4);
if (ast.arg1().isList()) {
IAST list = ast.getAST(1);
try {
IInteger[][] equations = new IInteger[1][list.size()];
// format looks like: { { 12, 16, 20, 27, 123 } };
for (int i = 1; i < list.size(); i++) {
equations[0][i - 1] = (IInteger) list.get(i);
}
equations[0][list.size() - 1] = (IInteger) ast.arg2();
// all solutions
int numberOfSolutions = -1;
if (ast.size() == 4) {
numberOfSolutions = ((ISignedNumber) ast.arg3()).toInt();
}
FrobeniusSolver solver = new FrobeniusSolver(equations);
IInteger[] solution;
IAST result = F.List();
if (numberOfSolutions < 0) {
while ((solution = solver.take()) != null) {
result.append(Lists.asList(solution));
}
} else {
while ((solution = solver.take()) != null) {
if (--numberOfSolutions < 0) {
break;
}
result.append(Lists.asList(solution));
}
}
return result;
} catch (RuntimeException e) {
if (Config.SHOW_STACKTRACE) {
e.printStackTrace();
}
}
}
return null;
}
Aggregations