Search in sources :

Example 1 with IRational

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

the class ComplexSym method pow.

@Override
public IComplex pow(final int parm1) {
    int temp = parm1;
    if ((parm1 == 0) && fReal.isZero() && fImaginary.isZero()) {
        throw new java.lang.ArithmeticException();
    }
    if (parm1 == 1) {
        return this;
    }
    IComplex res = ONE;
    if (parm1 < 0) {
        temp *= -1;
        for (int i = 0; i < temp; i++) {
            res = res.multiply(this);
        }
        final IRational d = res.getRealPart().multiply(res.getRealPart()).add(res.getImaginaryPart().multiply(res.getImaginaryPart()));
        return ComplexSym.valueOf(res.getRealPart().divideBy(d), res.getImaginaryPart().negate().divideBy(d));
    }
    for (int i = 0; i < temp; i++) {
        res = res.multiply(this);
    }
    return res;
}
Also used : IComplex(org.matheclipse.core.interfaces.IComplex) IRational(org.matheclipse.core.interfaces.IRational)

Example 2 with IRational

use of org.matheclipse.core.interfaces.IRational 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 3 with IRational

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

the class PatternMatcher method matchAST.

protected boolean matchAST(final IAST lhsPatternAST, final IExpr lhsEvalExpr, StackMatcher stackMatcher) {
    // "+lhsEvalExpr.toString());
    if (lhsPatternAST.isAST(F.PatternTest, 3)) {
        if (matchExpr(lhsPatternAST.arg1(), lhsEvalExpr, stackMatcher)) {
            return fPatternMap.isPatternTest(lhsPatternAST.arg1(), lhsPatternAST.arg2());
        }
        return false;
    }
    if (lhsPatternAST.isAST(F.Except, 2, 3)) {
        if (lhsPatternAST.isAST2()) {
            return !matchExpr(lhsPatternAST.arg1(), lhsEvalExpr, stackMatcher) && matchExpr(lhsPatternAST.arg2(), lhsEvalExpr, stackMatcher);
        } else {
            return !matchExpr(lhsPatternAST.arg1(), lhsEvalExpr, stackMatcher);
        }
    }
    if (lhsEvalExpr instanceof IAST) {
        if (!lhsPatternAST.isPatternExpr() && lhsPatternAST.equals(lhsEvalExpr)) {
            return stackMatcher.matchRest();
        }
        final IAST lhsEvalAST = (IAST) lhsEvalExpr;
        final ISymbol sym = lhsPatternAST.topHead();
        if (lhsPatternAST.size() <= lhsEvalAST.size()) {
            if ((lhsPatternAST.isFlatAST()) && sym.equals(lhsEvalAST.topHead()) && !(lhsPatternAST.isOrderlessAST() && lhsPatternAST.size() == lhsEvalAST.size())) {
                if (!matchExpr(lhsPatternAST.head(), lhsEvalAST.head())) {
                    return false;
                }
                return matchFlatAndFlatOrderlessAST(sym, lhsPatternAST, lhsEvalAST, stackMatcher);
            }
            if (lhsPatternAST.size() < lhsEvalAST.size()) {
                if (lhsPatternAST.isEvalFlagOn(IAST.CONTAINS_PATTERN_SEQUENCE)) {
                    if (!matchExpr(lhsPatternAST.head(), lhsEvalAST.head())) {
                        return false;
                    }
                    int lastPosition = lhsPatternAST.size() - 1;
                    if (lhsPatternAST.get(lastPosition).isAST(F.PatternTest, 3)) {
                        IAST patternTest = (IAST) lhsPatternAST.get(lastPosition);
                        if (patternTest.arg1().isPatternSequence()) {
                            // TODO only the special case, where the last
                            // element is
                            // a pattern sequence, is handled here
                            IAST seq = F.Sequence();
                            seq.appendAll(lhsEvalAST, lastPosition, lhsEvalAST.size());
                            if (((IPatternSequence) patternTest.arg1()).matchPatternSequence(seq, fPatternMap)) {
                                if (matchAST(lhsPatternAST.copyUntil(lastPosition), lhsEvalAST.copyUntil(lastPosition), stackMatcher)) {
                                    return fPatternMap.isPatternTest(patternTest.arg1(), patternTest.arg2());
                                }
                                return false;
                            }
                        }
                    }
                    if (lhsPatternAST.get(lastPosition).isPatternSequence()) {
                        // TODO only the special case, where the last
                        // element is
                        // a pattern sequence, is handled here
                        IAST seq = F.Sequence();
                        seq.appendAll(lhsEvalAST.range(), lastPosition, lhsEvalAST.size());
                        if (((IPatternSequence) lhsPatternAST.get(lastPosition)).matchPatternSequence(seq, fPatternMap)) {
                            return matchAST(lhsPatternAST.copyUntil(lastPosition), lhsEvalAST.copyUntil(lastPosition), stackMatcher);
                        }
                    }
                }
                return false;
            }
        }
        if (lhsPatternAST.size() != lhsEvalAST.size()) {
            return false;
        }
        IExpr e1 = lhsPatternAST.head();
        IExpr e2 = lhsEvalAST.head();
        if (e1.isSymbol() && e2.isSymbol()) {
            if (!e1.equals(e2)) {
                return false;
            }
        } else {
            // TODO create correct stack-matcher for the following call:
            if (!matchExpr(lhsPatternAST.head(), lhsEvalAST.head())) {
                return false;
            }
        }
        if (lhsPatternAST.isOrderlessAST()) {
            // only "pure Orderless" and "FlatOrderless with same size()"
            // will be handled here:
            OrderlessStepVisitor visitor = new OrderlessStepVisitor(sym, lhsPatternAST, lhsEvalAST, stackMatcher, fPatternMap, (sym.hasOneIdentityAttribute()) || // matching
            (lhsPatternAST.size() == lhsEvalAST.size() && !sym.hasFlatAttribute()));
            MultisetPartitionsIterator iter = new MultisetPartitionsIterator(visitor, lhsPatternAST.size() - 1);
            return !iter.execute();
        }
        return matchASTSequence(lhsPatternAST, lhsEvalAST, 0, stackMatcher);
    }
    if (lhsPatternAST.isAST(F.Rational, 3) && lhsEvalExpr.isRational()) {
        IRational numer = ((IRational) lhsEvalExpr).getNumerator();
        IRational denom = ((IRational) lhsEvalExpr).getDenominator();
        if (matchExpr(lhsPatternAST.arg1(), numer) && matchExpr(lhsPatternAST.arg2(), denom)) {
            return true;
        }
    } else if (lhsPatternAST.isAST(F.Complex, 3) && lhsEvalExpr.isNumber()) {
        ISignedNumber re = ((INumber) lhsEvalExpr).re();
        ISignedNumber im = ((INumber) lhsEvalExpr).im();
        if (matchExpr(lhsPatternAST.arg1(), re) && matchExpr(lhsPatternAST.arg2(), im)) {
            return true;
        }
    }
    return false;
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) MultisetPartitionsIterator(org.matheclipse.combinatoric.MultisetPartitionsIterator) IRational(org.matheclipse.core.interfaces.IRational) IPatternSequence(org.matheclipse.core.interfaces.IPatternSequence) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 4 with IRational

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

the class BigFractionSym method compareTo.

@Override
public int compareTo(IExpr expr) {
    if (expr instanceof IRational) {
        if (expr instanceof IFraction) {
            BigInteger valthis = toBigNumerator().multiply(((IFraction) expr).toBigDenominator());
            BigInteger valo = ((IFraction) expr).toBigNumerator().multiply(toBigDenominator());
            return valthis.compareTo(valo);
        }
        if (expr instanceof IInteger) {
            return fFraction.compareTo(new BigFraction(((IInteger) expr).toBigNumerator(), BigInteger.ONE));
        }
    }
    if (expr.isReal()) {
        return Double.compare(fFraction.doubleValue(), ((ISignedNumber) expr).doubleValue());
    }
    return -1;
// return super.compareTo(expr);
}
Also used : IFraction(org.matheclipse.core.interfaces.IFraction) BigFraction(org.hipparchus.fraction.BigFraction) IInteger(org.matheclipse.core.interfaces.IInteger) IRational(org.matheclipse.core.interfaces.IRational) BigInteger(java.math.BigInteger)

Example 5 with IRational

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

the class AbstractFunctionEvaluator method peelOff.

/**
 * Split plusAST into two parts, a "rest" and a multiple of Pi/2. This assumes plusAST to be an
 * Plus() expression. The multiple of Pi/2 returned in the second position is always a IRational
 * number.
 *
 * @param plusAST
 * @param engine
 * @return <code>F.NIL</code> if no multiple is found.
 */
public static IAST peelOff(final IAST plusAST, final EvalEngine engine) {
    IRational k = null;
    for (int i = 1; i < plusAST.size(); i++) {
        IExpr temp = plusAST.get(i);
        if (temp.equals(S.Pi)) {
            k = F.C1;
            break;
        }
        if (temp.isTimes2()) {
            if (temp.first().isRational() && temp.second().equals(S.Pi)) {
                k = (IRational) temp.first();
                break;
            }
        }
    }
    if (k != null) {
        IASTMutable result = F.binaryAST2(S.List, plusAST, F.C0);
        IExpr m1 = F.Times(k.mod(F.C1D2), S.Pi);
        IExpr m2 = S.Subtract.of(engine, F.Times(k, S.Pi), m1);
        result.set(1, S.Subtract.of(plusAST, m2));
        result.set(2, m2);
        return result;
    }
    return F.NIL;
}
Also used : IRational(org.matheclipse.core.interfaces.IRational) IExpr(org.matheclipse.core.interfaces.IExpr) IASTMutable(org.matheclipse.core.interfaces.IASTMutable)

Aggregations

IRational (org.matheclipse.core.interfaces.IRational)30 IExpr (org.matheclipse.core.interfaces.IExpr)17 IInteger (org.matheclipse.core.interfaces.IInteger)11 ISymbol (org.matheclipse.core.interfaces.ISymbol)8 IAST (org.matheclipse.core.interfaces.IAST)6 IComplex (org.matheclipse.core.interfaces.IComplex)6 EvalEngine (org.matheclipse.core.eval.EvalEngine)5 IASTMutable (org.matheclipse.core.interfaces.IASTMutable)5 IFraction (org.matheclipse.core.interfaces.IFraction)4 INum (org.matheclipse.core.interfaces.INum)4 ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)4 Num (org.matheclipse.core.expression.Num)3 IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)3 BigRational (edu.jas.arith.BigRational)2 GenPolynomial (edu.jas.poly.GenPolynomial)2 BigInteger (java.math.BigInteger)2 JASIExpr (org.matheclipse.core.convert.JASIExpr)2 ArgumentTypeException (org.matheclipse.core.eval.exception.ArgumentTypeException)2 LimitException (org.matheclipse.core.eval.exception.LimitException)2 Complex (edu.jas.poly.Complex)1