Search in sources :

Example 11 with ISignedNumber

use of org.matheclipse.core.interfaces.ISignedNumber in project symja_android_library by axkr.

the class EasterSunday method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    Validate.checkSize(ast, 2);
    try {
        IExpr arg1 = ast.arg1();
        ISignedNumber signedNumber = arg1.evalSignedNumber();
        if (signedNumber != null) {
            int y = signedNumber.toInt();
            // "Anonymous Gregorian algorithm", see
            // https://en.wikipedia.org/wiki/Computus
            int a = y % 19;
            int b = y / 100;
            int c = y % 100;
            int d = b / 4;
            int e = b % 4;
            int f = (b + 8) / 25;
            int g = (b - f + 1) / 3;
            int h = (19 * a + b - d - g + 15) % 30;
            int i = c / 4;
            int k = c % 4;
            int l = (32 + 2 * e + 2 * i - h - k) % 7;
            int m = (a + 11 * h + 22 * l) / 451;
            int month = (h + l - 7 * m + 114) / 31;
            int day = ((h + l - 7 * m + 114) % 31) + 1;
            return F.List(F.integer(y), F.integer(month), F.integer(day));
        }
    } catch (ArithmeticException ae) {
    // toInt() method may throw ArithmeticException
    }
    return F.NIL;
}
Also used : ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 12 with ISignedNumber

use of org.matheclipse.core.interfaces.ISignedNumber in project symja_android_library by axkr.

the class FindRoot method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    Validate.checkRange(ast, 3);
    ISymbol method = Newton;
    int maxIterations = 100;
    if (ast.size() >= 4) {
        final Options options = new Options(ast.topHead(), ast, 3, engine);
        IExpr optionMaxIterations = options.getOption("MaxIterations");
        if (optionMaxIterations.isSignedNumber()) {
            maxIterations = ((ISignedNumber) optionMaxIterations).toInt();
        }
        IExpr optionMethod = options.getOption("Method");
        if (optionMethod.isSymbol()) {
            method = ((ISymbol) optionMethod);
        } else {
            if (ast.arg3().isSymbol()) {
                method = (ISymbol) ast.arg3();
            }
        }
    }
    if ((ast.arg2().isList())) {
        IAST list = (IAST) ast.arg2();
        IExpr function = ast.arg1();
        if (list.size() >= 3 && list.arg1().isSymbol()) {
            if (function.isAST(F.Equal, 3)) {
                function = F.Plus(((IAST) function).arg1(), F.Negate(((IAST) function).arg2()));
            }
            ISignedNumber min = list.arg2().evalSignedNumber();
            ISignedNumber max = null;
            if (list.size() > 3) {
                max = list.arg3().evalSignedNumber();
            }
            if (min != null) {
                return F.List(F.Rule(list.arg1(), Num.valueOf(findRoot(method, maxIterations, list, min, max, function, engine))));
            }
        }
    }
    return F.NIL;
}
Also used : Options(org.matheclipse.core.eval.util.Options) ISymbol(org.matheclipse.core.interfaces.ISymbol) ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

Example 13 with ISignedNumber

use of org.matheclipse.core.interfaces.ISignedNumber in project symja_android_library by axkr.

the class UnitStep method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    int size = ast.size();
    if (size > 1) {
        for (int i = 1; i < size; i++) {
            IExpr expr = ast.get(i);
            ISignedNumber temp = expr.evalSignedNumber();
            if (temp != null) {
                if (temp.sign() < 0) {
                    return F.C0;
                } else {
                    continue;
                }
            } else {
                if (expr.isNegativeInfinity()) {
                    return F.C0;
                }
                if (expr.isInfinity()) {
                    continue;
                }
                if (expr.isInterval1()) {
                    IExpr l = expr.lower();
                    IExpr u = expr.upper();
                    if (l.isSignedNumber() && u.isSignedNumber()) {
                        ISignedNumber min = (ISignedNumber) l;
                        ISignedNumber max = (ISignedNumber) u;
                        if (min.sign() < 0) {
                            if (max.sign() < 0) {
                                return F.Interval(F.List(F.C0, F.C0));
                            } else {
                                if (size == 2) {
                                    return F.Interval(F.List(F.C0, F.C1));
                                }
                            }
                        } else {
                            if (max.sign() < 0) {
                                if (size == 2) {
                                    return F.Interval(F.List(F.C1, F.C0));
                                }
                            } else {
                                if (size == 2) {
                                    return F.Interval(F.List(F.C1, F.C1));
                                }
                                continue;
                            }
                        }
                    }
                }
            }
            return F.NIL;
        }
    }
    return F.C1;
}
Also used : ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 14 with ISignedNumber

use of org.matheclipse.core.interfaces.ISignedNumber in project symja_android_library by axkr.

the class Iterator method create.

/**
	 * Iterator specification for functions like <code>Table()</code> or
	 * <code>Sum()</code> or <code>Product()</code>
	 * 
	 * @param list
	 *            a list representing an iterator specification
	 * @param engine
	 *            the evaluation engine
	 * @return the iterator
	 */
public static IIterator<IExpr> create(final IAST list, final EvalEngine engine) {
    EvalEngine evalEngine = engine;
    IExpr lowerLimit;
    IExpr upperLimit;
    IExpr step;
    ISymbol variable;
    boolean fNumericMode;
    // fNumericMode = evalEngine.isNumericMode() ||
    // list.isMember(Predicates.isNumeric(), false);
    boolean localNumericMode = evalEngine.isNumericMode();
    try {
        if (list.hasNumericArgument()) {
            evalEngine.setNumericMode(true);
        }
        fNumericMode = evalEngine.isNumericMode();
        switch(list.size()) {
            case 2:
                lowerLimit = F.C1;
                upperLimit = evalEngine.evalWithoutNumericReset(list.arg1());
                step = F.C1;
                variable = null;
                if (upperLimit instanceof Num) {
                    return new DoubleIterator(variable, 1.0, ((INum) upperLimit).doubleValue(), 1.0);
                }
                if (upperLimit.isInteger()) {
                    try {
                        int iUpperLimit = ((IInteger) upperLimit).toInt();
                        return new IntIterator(variable, 1, iUpperLimit, 1);
                    } catch (ArithmeticException ae) {
                    //
                    }
                } else if (upperLimit.isRational()) {
                    try {
                        return new RationalIterator(variable, F.C1, (IRational) upperLimit, F.C1);
                    } catch (ArithmeticException ae) {
                    //
                    }
                } else if (upperLimit.isSignedNumber()) {
                    return new ISignedNumberIterator(variable, F.C1, (ISignedNumber) upperLimit, F.C1);
                }
                break;
            case 3:
                lowerLimit = F.C1;
                upperLimit = evalEngine.evalWithoutNumericReset(list.arg2());
                step = F.C1;
                if (list.arg1() instanceof ISymbol) {
                    variable = (ISymbol) list.arg1();
                } else {
                    variable = null;
                }
                if (upperLimit instanceof Num) {
                    return new DoubleIterator(variable, 1.0, ((INum) upperLimit).doubleValue(), 1.0);
                }
                if (upperLimit.isInteger()) {
                    try {
                        int iUpperLimit = ((IInteger) upperLimit).toInt();
                        return new IntIterator(variable, 1, iUpperLimit, 1);
                    } catch (ArithmeticException ae) {
                    //
                    }
                } else if (upperLimit.isRational()) {
                    try {
                        return new RationalIterator(variable, F.C1, (IRational) upperLimit, F.C1);
                    } catch (ArithmeticException ae) {
                    //
                    }
                } else if (upperLimit.isSignedNumber()) {
                    return new ISignedNumberIterator(variable, F.C1, (ISignedNumber) upperLimit, F.C1);
                }
                break;
            case 4:
                lowerLimit = evalEngine.evalWithoutNumericReset(list.arg2());
                upperLimit = evalEngine.evalWithoutNumericReset(list.arg3());
                step = F.C1;
                if (list.arg1().isSymbol()) {
                    variable = (ISymbol) list.arg1();
                } else {
                    variable = null;
                }
                if (lowerLimit instanceof Num && upperLimit instanceof Num) {
                    return new DoubleIterator(variable, ((INum) lowerLimit).doubleValue(), ((INum) upperLimit).doubleValue(), 1.0);
                }
                if (lowerLimit.isInteger() && upperLimit.isInteger()) {
                    try {
                        int iLowerLimit = ((IInteger) lowerLimit).toInt();
                        int iUpperLimit = ((IInteger) upperLimit).toInt();
                        return new IntIterator(variable, iLowerLimit, iUpperLimit, 1);
                    } catch (ArithmeticException ae) {
                    //
                    }
                } else if (lowerLimit.isRational() && upperLimit.isRational()) {
                    try {
                        return new RationalIterator(variable, (IRational) lowerLimit, (IRational) upperLimit, F.C1);
                    } catch (ArithmeticException ae) {
                    //
                    }
                } else if (lowerLimit.isSignedNumber() && upperLimit.isSignedNumber()) {
                    ISignedNumber iLowerLimit = (ISignedNumber) lowerLimit;
                    ISignedNumber iUpperLimit = (ISignedNumber) upperLimit;
                    return new ISignedNumberIterator(variable, iLowerLimit, iUpperLimit, F.C1);
                }
                break;
            case 5:
                lowerLimit = evalEngine.evalWithoutNumericReset(list.arg2());
                upperLimit = evalEngine.evalWithoutNumericReset(list.arg3());
                step = evalEngine.evalWithoutNumericReset(list.arg4());
                if (list.arg1() instanceof ISymbol) {
                    variable = (ISymbol) list.arg1();
                } else {
                    variable = null;
                }
                if (lowerLimit instanceof Num && upperLimit instanceof Num && step instanceof Num) {
                    return new DoubleIterator(variable, ((INum) lowerLimit).doubleValue(), ((INum) upperLimit).doubleValue(), ((INum) step).doubleValue());
                }
                if (lowerLimit.isInteger() && upperLimit.isInteger() && step.isInteger()) {
                    try {
                        int iLowerLimit = ((IInteger) lowerLimit).toInt();
                        int iUpperLimit = ((IInteger) upperLimit).toInt();
                        int iStep = ((IInteger) step).toInt();
                        return new IntIterator(variable, iLowerLimit, iUpperLimit, iStep);
                    } catch (ArithmeticException ae) {
                    //
                    }
                } else if (lowerLimit.isRational() && upperLimit.isRational() && step.isRational()) {
                    try {
                        return new RationalIterator(variable, (IRational) lowerLimit, (IRational) upperLimit, (IRational) step);
                    } catch (ArithmeticException ae) {
                    //
                    }
                } else if (lowerLimit.isSignedNumber() && upperLimit.isSignedNumber() && step.isSignedNumber()) {
                    return new ISignedNumberIterator(variable, (ISignedNumber) lowerLimit, (ISignedNumber) upperLimit, (ISignedNumber) step);
                }
                break;
            default:
                lowerLimit = null;
                upperLimit = null;
                step = null;
                variable = null;
        }
        return new ExprIterator(variable, evalEngine, lowerLimit, upperLimit, step, fNumericMode);
    } finally {
        evalEngine.setNumericMode(localNumericMode);
    }
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) INum(org.matheclipse.core.interfaces.INum) Num(org.matheclipse.core.expression.Num) ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) IInteger(org.matheclipse.core.interfaces.IInteger) EvalEngine(org.matheclipse.core.eval.EvalEngine) IRational(org.matheclipse.core.interfaces.IRational) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 15 with ISignedNumber

use of org.matheclipse.core.interfaces.ISignedNumber in project symja_android_library by axkr.

the class Assumptions method isPositive.

@Override
public boolean isPositive(IExpr expr) {
    ISignedNumber num;
    SignedNumberRelations gla = valueMap.get(expr);
    if (gla != null) {
        boolean result = false;
        num = gla.getGreater();
        if (num != null) {
            if (!num.isZero()) {
                if (!num.isGreaterThan(F.C0)) {
                    return false;
                }
            }
            result = true;
        }
        if (!result) {
            num = gla.getGreaterEqual();
            if (num != null) {
                if (!num.isGreaterThan(F.C0)) {
                    return false;
                }
                result = true;
            }
        }
        return result;
    }
    return false;
}
Also used : ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber)

Aggregations

ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)49 IExpr (org.matheclipse.core.interfaces.IExpr)31 IAST (org.matheclipse.core.interfaces.IAST)22 ISymbol (org.matheclipse.core.interfaces.ISymbol)10 WrongArgumentType (org.matheclipse.core.eval.exception.WrongArgumentType)7 LinearConstraint (org.hipparchus.optim.linear.LinearConstraint)3 Num (org.matheclipse.core.expression.Num)3 IInteger (org.matheclipse.core.interfaces.IInteger)3 IntegerDistribution (org.hipparchus.distribution.IntegerDistribution)2 RealDistribution (org.hipparchus.distribution.RealDistribution)2 MathIllegalStateException (org.hipparchus.exception.MathIllegalStateException)2 LinearObjectiveFunction (org.hipparchus.optim.linear.LinearObjectiveFunction)2 WrappedException (org.matheclipse.core.eval.exception.WrappedException)2 Options (org.matheclipse.core.eval.util.Options)2 ComplexNum (org.matheclipse.core.expression.ComplexNum)2 IComplex (org.matheclipse.core.interfaces.IComplex)2 IComplexNum (org.matheclipse.core.interfaces.IComplexNum)2 INum (org.matheclipse.core.interfaces.INum)2 IRational (org.matheclipse.core.interfaces.IRational)2 ArrayList (java.util.ArrayList)1