Search in sources :

Example 6 with WrongArgumentType

use of org.matheclipse.core.eval.exception.WrongArgumentType in project symja_android_library by axkr.

the class Replace method replaceExpr.

private static IExpr replaceExpr(final IAST ast, IExpr arg1, IExpr rules, final EvalEngine engine) {
    if (rules.isListOfLists()) {
        IAST result = F.List();
        for (IExpr list : (IAST) rules) {
            IAST subList = (IAST) list;
            IExpr temp = F.NIL;
            for (IExpr element : subList) {
                if (element.isRuleAST()) {
                    IAST rule = (IAST) element;
                    Function<IExpr, IExpr> function = Functors.rules(rule);
                    temp = function.apply(arg1);
                    if (temp.isPresent()) {
                        break;
                    }
                } else {
                    WrongArgumentType wat = new WrongArgumentType(ast, ast, -1, "Rule expression (x->y) expected: ");
                    throw wat;
                }
            }
            if (temp.isPresent()) {
                result.append(temp);
            } else {
                result.append(arg1);
            }
        }
        return result;
    } else if (rules.isList()) {
        for (IExpr element : (IAST) rules) {
            if (element.isRuleAST()) {
                IAST rule = (IAST) element;
                Function<IExpr, IExpr> function = Functors.rules(rule);
                IExpr temp = function.apply(arg1);
                if (temp.isPresent()) {
                    return temp;
                }
            } else {
                WrongArgumentType wat = new WrongArgumentType(ast, ast, -1, "Rule expression (x->y) expected: ");
                throw wat;
            }
        }
        return arg1;
    }
    if (rules.isRuleAST()) {
        return replaceRule(arg1, (IAST) rules);
    } else {
        WrongArgumentType wat = new WrongArgumentType(ast, ast, -1, "Rule expression (x->y) expected: ");
        engine.printMessage(wat.getMessage());
    }
    return F.NIL;
}
Also used : Function(java.util.function.Function) WrongArgumentType(org.matheclipse.core.eval.exception.WrongArgumentType) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 7 with WrongArgumentType

use of org.matheclipse.core.eval.exception.WrongArgumentType in project symja_android_library by axkr.

the class Replace method replaceExprWithLevelSpecification.

private static IExpr replaceExprWithLevelSpecification(final IAST ast, IExpr arg1, IExpr rules, IExpr exprLevelSpecification, EvalEngine engine) {
    // use replaceFunction#setRule() method to set the current rules which
    // are initialized with null
    ReplaceFunction replaceFunction = new ReplaceFunction(ast, null, engine);
    VisitorLevelSpecification level = new VisitorLevelSpecification(replaceFunction, exprLevelSpecification, false);
    if (rules.isListOfLists()) {
        IAST result = F.List();
        for (IExpr list : (IAST) rules) {
            IExpr temp = F.NIL;
            IAST subList = (IAST) list;
            for (IExpr element : subList) {
                if (element.isRuleAST()) {
                    IAST rule = (IAST) element;
                    replaceFunction.setRule(rule);
                    temp = arg1.accept(level);
                    if (temp.isPresent()) {
                        break;
                    }
                } else {
                    WrongArgumentType wat = new WrongArgumentType(ast, ast, -1, "Rule expression (x->y) expected: ");
                    throw wat;
                }
            }
            result.append(temp.orElse(arg1));
        }
        return result;
    }
    replaceFunction.setRule(rules);
    return arg1.accept(level).orElse(arg1);
}
Also used : WrongArgumentType(org.matheclipse.core.eval.exception.WrongArgumentType) VisitorLevelSpecification(org.matheclipse.core.visit.VisitorLevelSpecification) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 8 with WrongArgumentType

use of org.matheclipse.core.eval.exception.WrongArgumentType in project symja_android_library by axkr.

the class ReplaceList method replaceExpr.

private static IAST replaceExpr(final IAST ast, IExpr arg1, IExpr rules, IAST result, int maxNumberOfResults, final EvalEngine engine) {
    if (rules.isList()) {
        for (IExpr element : (IAST) rules) {
            if (element.isRuleAST()) {
                IAST rule = (IAST) element;
                Function<IExpr, IExpr> function = Functors.rules(rule);
                IExpr temp = function.apply(arg1);
                if (temp.isPresent()) {
                    if (maxNumberOfResults <= result.size()) {
                        return result;
                    }
                    result.append(temp);
                }
            } else {
                WrongArgumentType wat = new WrongArgumentType(ast, ast, -1, "Rule expression (x->y) expected: ");
                throw wat;
            }
        }
        return result;
    }
    if (rules.isRuleAST()) {
        Function<IExpr, IExpr> function = Functors.rules((IAST) rules);
        IExpr temp = function.apply(arg1);
        if (temp.isPresent()) {
            if (maxNumberOfResults <= result.size()) {
                return result;
            }
            result.append(temp);
        }
    } else {
        WrongArgumentType wat = new WrongArgumentType(ast, ast, -1, "Rule expression (x->y) expected: ");
        engine.printMessage(wat.getMessage());
    }
    return result;
}
Also used : WrongArgumentType(org.matheclipse.core.eval.exception.WrongArgumentType) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

Example 9 with WrongArgumentType

use of org.matheclipse.core.eval.exception.WrongArgumentType in project symja_android_library by axkr.

the class Resultant method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    Validate.checkSize(ast, 4);
    // TODO allow multinomials
    IExpr arg3 = Validate.checkSymbolType(ast, 3);
    ISymbol x = (ISymbol) arg3;
    IExpr a = F.evalExpandAll(ast.arg1());
    IExpr b = F.evalExpandAll(ast.arg2());
    ExprPolynomialRing ring = new ExprPolynomialRing(F.List(x));
    try {
        // check if a is a polynomial otherwise check ArithmeticException,
        // ClassCastException
        ring.create(a);
    } catch (RuntimeException ex) {
        throw new WrongArgumentType(ast, a, 1, "Polynomial expected!");
    }
    try {
        // check if b is a polynomial otherwise check ArithmeticException,
        // ClassCastException
        ring.create(b);
        return F.Together(resultant(a, b, x, engine));
    } catch (RuntimeException ex) {
        throw new WrongArgumentType(ast, b, 2, "Polynomial expected!");
    }
}
Also used : ExprPolynomialRing(org.matheclipse.core.polynomials.ExprPolynomialRing) ISymbol(org.matheclipse.core.interfaces.ISymbol) WrongArgumentType(org.matheclipse.core.eval.exception.WrongArgumentType) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 10 with WrongArgumentType

use of org.matheclipse.core.eval.exception.WrongArgumentType in project symja_android_library by axkr.

the class Solve method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    Validate.checkRange(ast, 3, 4);
    IAST variables = Validate.checkIsVariableOrVariableList(ast, 2, engine);
    if (variables == null) {
        return F.NIL;
    }
    IExpr domain = F.Complexes;
    if (ast.isAST3()) {
        domain = ast.arg3();
        if (domain.equals(F.Booleans)) {
            IAST resultList = F.List();
            booleansSolve(ast.arg1(), variables, 0, 1, resultList);
            return resultList;
        }
        if (domain.equals(F.Integers)) {
            IAST equationsAndInequations = Validate.checkEquationsAndInequations(ast, 1);
            try {
                IAST resultList = Solve.integerSolve(equationsAndInequations, variables);
                EvalAttributes.sort(resultList);
                return resultList;
            } catch (RuntimeException rex) {
                if (Config.SHOW_STACKTRACE) {
                    rex.printStackTrace();
                }
                engine.printMessage("Integer solution not found: " + rex.getMessage());
            }
            return F.NIL;
        }
        if (!domain.equals(F.Reals) && !domain.equals(F.Complexes)) {
            throw new WrongArgumentType(ast, ast.arg3(), 3, "Booleans or Integers expected!");
        }
    }
    IAST termsEqualZeroList = Validate.checkEquations(ast, 1);
    IAST temp = solveTimesEquationsRecursively(termsEqualZeroList, variables, engine);
    if (temp.isPresent()) {
        return temp;
    }
    temp = solveEquations(termsEqualZeroList, variables, 0, engine);
    if (temp.isPresent()) {
        // }
        return temp;
    }
    return F.NIL;
// return solvePlusEquationsSimplified(termsEqualZeroList,
// variables,engine);
}
Also used : WrongArgumentType(org.matheclipse.core.eval.exception.WrongArgumentType) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr)

Aggregations

WrongArgumentType (org.matheclipse.core.eval.exception.WrongArgumentType)37 IAST (org.matheclipse.core.interfaces.IAST)30 IExpr (org.matheclipse.core.interfaces.IExpr)29 ISymbol (org.matheclipse.core.interfaces.ISymbol)12 ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)7 IInteger (org.matheclipse.core.interfaces.IInteger)6 ExprPolynomialRing (org.matheclipse.core.polynomials.ExprPolynomialRing)5 ExprPolynomial (org.matheclipse.core.polynomials.ExprPolynomial)4 ExpVector (edu.jas.poly.ExpVector)3 LinearConstraint (org.hipparchus.optim.linear.LinearConstraint)3 IFraction (org.matheclipse.core.interfaces.IFraction)3 ModLong (edu.jas.arith.ModLong)2 GenPolynomial (edu.jas.poly.GenPolynomial)2 ArrayList (java.util.ArrayList)2 Apint (org.apfloat.Apint)2 PrettyPrint (edu.jas.kern.PrettyPrint)1 Function (java.util.function.Function)1 IntVariable (jp.ac.kobe_u.cs.cream.IntVariable)1 Apfloat (org.apfloat.Apfloat)1 MathIllegalStateException (org.hipparchus.exception.MathIllegalStateException)1