Search in sources :

Example 1 with WrongArgumentType

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

the class LinearProgramming method numericEval.

@Override
public IExpr numericEval(final IAST ast, EvalEngine engine) {
    Validate.checkSize(ast, 4);
    try {
        if (ast.arg1().isList() && ast.arg2().isList() && ast.arg3().isList()) {
            double[] arg1D = Expr2Object.toDoubleVector((IAST) ast.arg1());
            LinearObjectiveFunction f = new LinearObjectiveFunction(arg1D, 0);
            Collection<LinearConstraint> constraints = new ArrayList<LinearConstraint>();
            IAST arg2 = (IAST) ast.arg2();
            IAST arg3 = (IAST) ast.arg3();
            if (arg2.size() != arg3.size()) {
                return F.NIL;
            }
            double[] arg2D;
            double[] arg3D;
            for (int i = 1; i < arg2.size(); i++) {
                arg2D = Expr2Object.toDoubleVector((IAST) arg2.get(i));
                if (arg2.get(i).isList()) {
                    if (arg3.get(i).isList()) {
                        arg3D = Expr2Object.toDoubleVector((IAST) arg3.get(i));
                        if (arg3D.length >= 2) {
                            double val = arg3D[1];
                            if (val == 0.0) {
                                constraints.add(new LinearConstraint(arg2D, Relationship.EQ, arg3D[0]));
                            } else if (val < 0.0) {
                                constraints.add(new LinearConstraint(arg2D, Relationship.LEQ, arg3D[0]));
                            } else if (val > 0.0) {
                                constraints.add(new LinearConstraint(arg2D, Relationship.GEQ, arg3D[0]));
                            }
                        } else if (arg3D.length == 1) {
                            constraints.add(new LinearConstraint(arg2D, Relationship.GEQ, arg3D[0]));
                        }
                    } else {
                        ISignedNumber sn = arg3.get(i).evalSignedNumber();
                        if (sn != null) {
                            constraints.add(new LinearConstraint(arg2D, Relationship.GEQ, sn.doubleValue()));
                        } else {
                            throw new WrongArgumentType(arg3, arg3.get(i), i, "Numeric vector or number expected!");
                        }
                    }
                } else {
                    throw new WrongArgumentType(ast, ast.get(i), i, "Numeric vector expected!");
                }
            }
            SimplexSolver solver = new SimplexSolver();
            // PointValuePair solution = solver.optimize(f, constraints, GoalType.MINIMIZE, true);
            PointValuePair solution = solver.optimize(f, new LinearConstraintSet(constraints), GoalType.MINIMIZE, new NonNegativeConstraint(true), PivotSelectionRule.BLAND);
            double[] values = solution.getPointRef();
            return F.List(values);
        }
    } catch (MathIllegalStateException oe) {
        throw new WrappedException(oe);
    // if (Config.SHOW_STACKTRACE) {
    // oe.printStackTrace();
    // }
    }
    return F.NIL;
}
Also used : WrappedException(org.matheclipse.core.eval.exception.WrappedException) LinearConstraintSet(org.hipparchus.optim.linear.LinearConstraintSet) NonNegativeConstraint(org.hipparchus.optim.linear.NonNegativeConstraint) LinearObjectiveFunction(org.hipparchus.optim.linear.LinearObjectiveFunction) ArrayList(java.util.ArrayList) LinearConstraint(org.hipparchus.optim.linear.LinearConstraint) MathIllegalStateException(org.hipparchus.exception.MathIllegalStateException) LinearConstraint(org.hipparchus.optim.linear.LinearConstraint) NonNegativeConstraint(org.hipparchus.optim.linear.NonNegativeConstraint) PointValuePair(org.hipparchus.optim.PointValuePair) ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) WrongArgumentType(org.matheclipse.core.eval.exception.WrongArgumentType) SimplexSolver(org.hipparchus.optim.linear.SimplexSolver) IAST(org.matheclipse.core.interfaces.IAST)

Example 2 with WrongArgumentType

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

the class Limit method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    Validate.checkRange(ast, 3, 4);
    if (!ast.arg2().isRuleAST()) {
        throw new WrongArgumentType(ast, ast.arg2(), 2, "Limit: rule definition expected!");
    }
    IAST rule = (IAST) ast.arg2();
    if (!(rule.arg1().isSymbol())) {
        throw new WrongArgumentType(ast, ast.arg1(), 2, "Limit: variable symbol for rule definition expected!");
    }
    // no direction as default
    int direction = DIRECTION_AUTOMATIC;
    if (ast.isAST3()) {
        final Options options = new Options(ast.topHead(), ast, 2, engine);
        IExpr option = options.getOption("Direction");
        if (option.isPresent()) {
            if (option.isOne()) {
                direction = DIRECTION_FROM_SMALLER_VALUES;
            } else if (option.isMinusOne()) {
                direction = DIRECTION_FROM_LARGER_VALUES;
            } else if (option.equals(F.Automatic)) {
                direction = DIRECTION_AUTOMATIC;
            } else {
                throw new WrongArgumentType(ast, ast.arg2(), 2, "Limit: direction option expected!");
            }
        } else {
            throw new WrongArgumentType(ast, ast.arg2(), 2, "Limit: direction option expected!");
        }
    }
    ISymbol symbol = (ISymbol) rule.arg1();
    IExpr limit = null;
    if (rule.isFreeAt(2, symbol)) {
        limit = rule.arg2();
    } else {
        throw new WrongArgumentType(ast, ast.arg2(), 2, "Limit: limit value contains variable symbol for rule definition!");
    }
    if (ast.isAST2() && direction == DIRECTION_AUTOMATIC) {
        // check if there's a direction specific rule available in the rule database
        IExpr temp = engine.evalLoop(F.Limit(ast.arg1(), ast.arg2(), F.Rule(F.Direction, F.CN1)));
        if (temp.isPresent()) {
            return temp;
        }
    }
    LimitData data = new LimitData(symbol, limit, rule, direction);
    return evalLimit(ast.arg1(), data, true);
}
Also used : Options(org.matheclipse.core.eval.util.Options) ISymbol(org.matheclipse.core.interfaces.ISymbol) WrongArgumentType(org.matheclipse.core.eval.exception.WrongArgumentType) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 3 with WrongArgumentType

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

the class Interval method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    Validate.checkRange(ast, 2);
    if (ast.isInterval1()) {
        IAST list = (IAST) ast.arg1();
        try {
            ISignedNumber min = (ISignedNumber) list.arg1();
            ISignedNumber max = (ISignedNumber) list.arg2();
            if (min.greaterThan(max).isTrue()) {
                throw new WrongArgumentType(ast, ast.get(1), 1, "Min > Mac in interval");
            }
        } catch (ClassCastException cca) {
        // do nothing
        }
    }
    return F.NIL;
}
Also used : ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) WrongArgumentType(org.matheclipse.core.eval.exception.WrongArgumentType) IAST(org.matheclipse.core.interfaces.IAST)

Example 4 with WrongArgumentType

use of org.matheclipse.core.eval.exception.WrongArgumentType 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 5 with WrongArgumentType

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

the class ExprParser method convertN.

private IExpr convertN(final IAST function) {
    try {
        int precision = Validate.checkIntType(function.arg2());
        if (EvalEngine.isApfloat(precision)) {
            NVisitorExpr nve = new NVisitorExpr(precision);
            IExpr temp = function.arg1().accept(nve);
            if (temp.isPresent()) {
                function.set(1, temp);
            }
        }
    } catch (WrongArgumentType wat) {
    }
    return function;
}
Also used : WrongArgumentType(org.matheclipse.core.eval.exception.WrongArgumentType) IExpr(org.matheclipse.core.interfaces.IExpr) Apint(org.apfloat.Apint)

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