Search in sources :

Example 41 with IAST

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;
}
Also used : FrobeniusSolver(org.matheclipse.core.frobenius.FrobeniusSolver) IInteger(org.matheclipse.core.interfaces.IInteger) IAST(org.matheclipse.core.interfaces.IAST)

Example 42 with IAST

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;
}
Also used : IAST(org.matheclipse.core.interfaces.IAST)

Example 43 with IAST

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);
}
Also used : IAST(org.matheclipse.core.interfaces.IAST)

Example 44 with IAST

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);
}
Also used : WrongArgumentType(org.matheclipse.core.eval.exception.WrongArgumentType) ArrayList(java.util.ArrayList) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) PatternMatcherAndEvaluator(org.matheclipse.core.patternmatching.PatternMatcherAndEvaluator)

Example 45 with IAST

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 &quot;<code>x-&gt;y</code>&quot;
	 * @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);
}
Also used : EvalEngine(org.matheclipse.core.eval.EvalEngine) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr) ExprParser(org.matheclipse.core.parser.ExprParser)

Aggregations

IAST (org.matheclipse.core.interfaces.IAST)413 IExpr (org.matheclipse.core.interfaces.IExpr)248 ISymbol (org.matheclipse.core.interfaces.ISymbol)76 IInteger (org.matheclipse.core.interfaces.IInteger)34 WrongArgumentType (org.matheclipse.core.eval.exception.WrongArgumentType)30 ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)22 ExpVector (edu.jas.poly.ExpVector)15 ArrayList (java.util.ArrayList)14 BigRational (edu.jas.arith.BigRational)13 JASIExpr (org.matheclipse.core.convert.JASIExpr)13 VariablesSet (org.matheclipse.core.convert.VariablesSet)13 INum (org.matheclipse.core.interfaces.INum)13 ExprPolynomial (org.matheclipse.core.polynomials.ExprPolynomial)12 GenPolynomial (edu.jas.poly.GenPolynomial)11 JASConversionException (org.matheclipse.core.eval.exception.JASConversionException)11 IFraction (org.matheclipse.core.interfaces.IFraction)11 INumber (org.matheclipse.core.interfaces.INumber)11 IComplex (org.matheclipse.core.interfaces.IComplex)10 ModLong (edu.jas.arith.ModLong)9 ExprPolynomialRing (org.matheclipse.core.polynomials.ExprPolynomialRing)9