use of org.matheclipse.core.interfaces.IAST 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;
}
use of org.matheclipse.core.interfaces.IAST in project symja_android_library by axkr.
the class BinaryApply method apply.
public IExpr apply(final IExpr firstArg, final IExpr secondArg) {
final IAST ast = fConstant.apply(secondArg);
ast.append(firstArg);
return ast;
}
use of org.matheclipse.core.interfaces.IAST in project symja_android_library by axkr.
the class BinaryEval method apply.
/**
* Return the evaluation of an binary AST object by settings it's first
* argument to <code>firstArg</code> and it's second argument to
* <code>secondArg</code>
*
*/
public IExpr apply(final IExpr firstArg, final IExpr secondArg) {
final IAST ast = fAST.clone();
ast.append(firstArg);
ast.append(secondArg);
return fEngine.evaluate(ast);
}
use of org.matheclipse.core.interfaces.IAST in project symja_android_library by axkr.
the class Functors method rules.
/**
* Create a functor from the given rules. If <code>astRules</code> is a
* <code>List[]</code> object, the elements of the list are taken as the
* rules of the form <code>Rule[lhs, rhs]</code>, otherwise the
* <code>astRules</code> itself is taken as the <code>Rule[lhs, rhs]</code>.
*
* @param astRules
* @return
* @throws WrongArgumentType
*/
public static Function<IExpr, IExpr> rules(@Nonnull IAST astRules) throws WrongArgumentType {
final Map<IExpr, IExpr> equalRules;
List<PatternMatcherAndEvaluator> matchers = new ArrayList<PatternMatcherAndEvaluator>();
if (astRules.isList()) {
// assuming multiple rules in a list
IAST rule;
int size = astRules.size() - 1;
if (size <= 5) {
equalRules = new OpenFixedSizeMap<IExpr, IExpr>(size * 3 - 1);
} else {
equalRules = new HashMap<IExpr, IExpr>();
}
for (final IExpr expr : astRules) {
if (expr.isRuleAST()) {
rule = (IAST) expr;
addRuleToCollection(equalRules, matchers, rule);
} else {
throw new WrongArgumentType(astRules, astRules, -1, "Rule expression (x->y) expected: ");
}
}
} else {
if (astRules.isRuleAST()) {
equalRules = new OpenFixedSizeMap<IExpr, IExpr>(3);
addRuleToCollection(equalRules, matchers, astRules);
} else {
throw new WrongArgumentType(astRules, astRules, -1, "Rule expression (x->y) expected: ");
}
}
if (matchers.size() > 0) {
return new RulesPatternFunctor(equalRules, matchers);
}
return rules(equalRules);
}
use of org.matheclipse.core.interfaces.IAST in project symja_android_library by axkr.
the class Functors method rules.
/**
* Create a functor from the given rules. All strings in
* <code>strRules</code> are parsed in internal rules form.
*
* @param strRules
* array of rules of the form "<code>x->y</code>"
* @return
* @throws WrongArgumentType
*/
public static Function<IExpr, IExpr> rules(@Nonnull String[] strRules) throws WrongArgumentType {
IAST astRules = F.ListAlloc(strRules.length);
ExprParser parser = new ExprParser(EvalEngine.get());
// final Parser parser = new Parser();
final EvalEngine engine = EvalEngine.get();
for (String str : strRules) {
IExpr expr = parser.parse(str);
// final ASTNode parsedAST = parser.parse(str);
// IExpr expr = AST2Expr.CONST.convert(parsedAST, engine);
expr = engine.evaluate(expr);
astRules.append(expr);
}
return rules(astRules);
}
Aggregations