Search in sources :

Example 21 with IRational

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

the class TeXFormFactory method convertComplex.

public void convertComplex(final StringBuilder buf, final IComplex c, final int precedence) {
    if (c.isImaginaryUnit()) {
        buf.append("i ");
        return;
    }
    if (c.isNegativeImaginaryUnit()) {
        if (precedence > plusPrec) {
            buf.append("\\left( ");
        }
        buf.append(" - i ");
        if (precedence > plusPrec) {
            buf.append("\\right) ");
        }
        return;
    }
    if (precedence > plusPrec) {
        buf.append("\\left( ");
    }
    IRational re = c.getRealPart();
    IRational im = c.getImaginaryPart();
    if (!re.isZero()) {
        convertInternal(buf, re, 0);
        if (im.compareInt(0) >= 0) {
            buf.append(" + ");
        } else {
            buf.append(" - ");
            im = im.negate();
        }
    }
    convertInternal(buf, im, 0);
    // InvisibleTimes
    buf.append("\\,");
    buf.append("i ");
    if (precedence > plusPrec) {
        buf.append("\\right) ");
    }
}
Also used : IRational(org.matheclipse.core.interfaces.IRational)

Example 22 with IRational

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

the class Algebra method fractionalPartsRational.

/**
 * Split the expression into numerator and denominator parts, by separating positive and negative
 * powers.
 *
 * @param arg
 * @return the numerator and denominator expression
 */
public static IExpr[] fractionalPartsRational(final IExpr arg) {
    if (arg.isFraction()) {
        IFraction fr = (IFraction) arg;
        IExpr[] parts = new IExpr[2];
        parts[0] = fr.numerator();
        parts[1] = fr.denominator();
        return parts;
    } else if (arg.isComplex()) {
        IRational re = ((IComplex) arg).getRealPart();
        IRational im = ((IComplex) arg).getImaginaryPart();
        if (re.isFraction() || im.isFraction()) {
            IExpr[] parts = new IExpr[2];
            parts[0] = re.numerator().times(im.denominator()).add(im.numerator().times(re.denominator()).times(F.CI));
            parts[1] = re.denominator().times(im.denominator());
            return parts;
        }
        return null;
    }
    return fractionalParts(arg, false);
}
Also used : IFraction(org.matheclipse.core.interfaces.IFraction) IRational(org.matheclipse.core.interfaces.IRational) JASIExpr(org.matheclipse.core.convert.JASIExpr) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 23 with IRational

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

the class Algebra method factorTermsPlus.

/**
 * Factor out a rational number which may be a factor in every sub-expression of <code>plus
 * </code>.
 *
 * @param plusAST
 * @param engine
 * @return {@link F#NIL} if the factor couldn't be found
 */
/* package private */
static IExpr factorTermsPlus(IAST plusAST, EvalEngine engine) {
    IRational gcd1 = null;
    if (plusAST.arg1().isRational()) {
        gcd1 = (IRational) plusAST.arg1();
    } else if (plusAST.arg1().isTimes() && plusAST.arg1().first().isRational()) {
        gcd1 = (IRational) plusAST.arg1().first();
    }
    if (gcd1 == null) {
        return F.NIL;
    }
    for (int i = 2; i < plusAST.size(); i++) {
        IRational gcd2 = null;
        if (plusAST.get(i).isRational()) {
            gcd2 = (IRational) plusAST.get(i);
        } else if (plusAST.get(i).isTimes() && plusAST.get(i).first().isRational()) {
            gcd2 = (IRational) plusAST.get(i).first();
        }
        if (gcd2 == null) {
            return F.NIL;
        }
        final IExpr gcd12 = engine.evaluate(F.GCD(gcd1, gcd2));
        if (gcd12.isRational() && !gcd12.isOne()) {
            gcd1 = (IRational) gcd12;
        } else {
            return F.NIL;
        }
    }
    if (gcd1.isMinusOne()) {
        return F.NIL;
    }
    return engine.evaluate(F.Times(gcd1, F.Expand(F.Times(gcd1.inverse(), plusAST))));
}
Also used : IRational(org.matheclipse.core.interfaces.IRational) JASIExpr(org.matheclipse.core.convert.JASIExpr) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 24 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 symbol the variable symbol
 * @param engine the evaluation engine
 * @return the iterator
 */
public static IIterator<IExpr> create(final IAST list, final ISymbol symbol, final EvalEngine engine) {
    EvalEngine evalEngine = engine;
    IExpr lowerLimit;
    IExpr upperLimit;
    IExpr step;
    ISymbol variable;
    boolean fNumericMode;
    if (symbol != null && (!symbol.isVariable() || symbol.isProtected())) {
        // Cannot assign to raw object `1`.
        throw new ArgumentTypeException(IOFunctions.getMessage("setraw", F.list(symbol), EvalEngine.get()));
    }
    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 = symbol;
                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(symbol, 1, iUpperLimit, 1);
                    } catch (ArithmeticException ae) {
                    // 
                    }
                } else if (upperLimit.isRational()) {
                    try {
                        return new RationalIterator(symbol, F.C1, (IRational) upperLimit, F.C1);
                    } catch (ArithmeticException ae) {
                    // 
                    }
                } else if (upperLimit.isQuantity()) {
                    return new QuantityIterator(symbol, (IQuantity) upperLimit);
                } else if (upperLimit.isReal()) {
                    return new ISignedNumberIterator(variable, F.C1, (ISignedNumber) upperLimit, F.C1);
                }
                break;
            case 3:
                lowerLimit = evalEngine.evalWithoutNumericReset(list.arg1());
                upperLimit = evalEngine.evalWithoutNumericReset(list.arg2());
                step = F.C1;
                variable = symbol;
                if (upperLimit.isList()) {
                    if (variable != null) {
                        if (!variable.isVariable() || variable.isProtected()) {
                            // Cannot assign to raw object `1`.
                            throw new ArgumentTypeException(IOFunctions.getMessage("setraw", F.list(variable), EvalEngine.get()));
                        }
                    } else {
                        // Raw object `1` cannot be used as an iterator.
                        throw new ArgumentTypeException(IOFunctions.getMessage("itraw", F.list(list.arg1()), EvalEngine.get()));
                    }
                    return new ExprListIterator(variable, (IAST) upperLimit, evalEngine);
                }
                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(symbol, iLowerLimit, iUpperLimit, 1);
                    } catch (ArithmeticException ae) {
                    // 
                    }
                } else if (lowerLimit.isRational() && upperLimit.isRational()) {
                    try {
                        return new RationalIterator(symbol, (IRational) lowerLimit, (IRational) upperLimit, F.C1);
                    } catch (ArithmeticException ae) {
                    // 
                    }
                } else if (lowerLimit.isQuantity() && upperLimit.isQuantity()) {
                    return new QuantityIterator(symbol, (IQuantity) lowerLimit, (IQuantity) upperLimit);
                } else if (lowerLimit.isReal() && upperLimit.isReal()) {
                    return new ISignedNumberIterator(variable, (ISignedNumber) lowerLimit, (ISignedNumber) upperLimit, F.C1);
                }
                break;
            case 4:
                lowerLimit = evalEngine.evalWithoutNumericReset(list.arg1());
                upperLimit = evalEngine.evalWithoutNumericReset(list.arg2());
                step = evalEngine.evalWithoutNumericReset(list.arg3());
                variable = symbol;
                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(symbol, iLowerLimit, iUpperLimit, iStep);
                    } catch (ArithmeticException ae) {
                    // 
                    }
                } else if (lowerLimit.isRational() && upperLimit.isRational() && step.isRational()) {
                    try {
                        return new RationalIterator(symbol, (IRational) lowerLimit, (IRational) upperLimit, (IRational) step);
                    } catch (ArithmeticException ae) {
                    // 
                    }
                } else if (lowerLimit.isQuantity() && upperLimit.isQuantity() && step.isQuantity()) {
                    return new QuantityIterator(symbol, (IQuantity) lowerLimit, (IQuantity) upperLimit, (IQuantity) step);
                } else if (lowerLimit.isReal() && upperLimit.isReal() && step.isReal()) {
                    return new ISignedNumberIterator(variable, (ISignedNumber) lowerLimit, (ISignedNumber) upperLimit, (ISignedNumber) step);
                }
                break;
            default:
                lowerLimit = null;
                upperLimit = null;
                step = null;
                variable = null;
        }
        return new ExprIterator(variable, lowerLimit, upperLimit, step, fNumericMode, evalEngine);
    } catch (LimitException le) {
        throw le;
    } catch (ArgumentTypeException atex) {
        throw atex;
    } catch (RuntimeException rex) {
        throw new ClassCastException();
    } finally {
        evalEngine.setNumericMode(localNumericMode);
    }
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) Num(org.matheclipse.core.expression.Num) INum(org.matheclipse.core.interfaces.INum) 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) LimitException(org.matheclipse.core.eval.exception.LimitException) ArgumentTypeException(org.matheclipse.core.eval.exception.ArgumentTypeException)

Example 25 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 position the position of the list in the argument sequence, for printing an error if
 *        list cannot be converted into an iterator form
 * @param engine the evaluation engine
 * @return the iterator
 */
public static IIterator<IExpr> create(final IAST list, int position, 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();
        int iterationLimit = evalEngine.getIterationLimit();
        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();
                        if (iUpperLimit > iterationLimit && iterationLimit > 0) {
                            IterationLimitExceeded.throwIt(iUpperLimit, upperLimit);
                        }
                        return new IntIterator(variable, 1, iUpperLimit, 1);
                    } catch (ArithmeticException ae) {
                    // 
                    }
                } else if (upperLimit.isRational()) {
                    try {
                        int iUpperLimit = ((IRational) upperLimit).floor().toInt();
                        if (iUpperLimit > iterationLimit && iterationLimit > 0) {
                            IterationLimitExceeded.throwIt(iUpperLimit, upperLimit);
                        }
                        return new RationalIterator(variable, F.C1, (IRational) upperLimit, F.C1);
                    } catch (ArithmeticException ae) {
                    // 
                    }
                } else if (upperLimit.isQuantity()) {
                    return new QuantityIterator(variable, (IQuantity) upperLimit);
                } else if (upperLimit.isReal()) {
                    return new ISignedNumberIterator(variable, F.C1, (ISignedNumber) upperLimit, F.C1);
                }
                if (!list.arg1().isVariable()) {
                    throw new ArgumentTypeException(IOFunctions.getMessage("vloc", F.list(list.arg1()), EvalEngine.get()));
                }
                break;
            case 3:
                lowerLimit = F.C1;
                upperLimit = evalEngine.evalWithoutNumericReset(list.arg2());
                step = F.C1;
                if (list.arg1() instanceof ISymbol) {
                    ISymbol sym = (ISymbol) list.arg1();
                    if (!sym.isVariable() || sym.isProtected()) {
                        // Cannot assign to raw object `1`.
                        throw new ArgumentTypeException(IOFunctions.getMessage("setraw", F.list(sym), EvalEngine.get()));
                    }
                    variable = sym;
                } else {
                    // Raw object `1` cannot be used as an iterator.
                    throw new ArgumentTypeException(IOFunctions.getMessage("itraw", F.list(list.arg1()), EvalEngine.get()));
                }
                if (upperLimit.isList()) {
                    return new ExprListIterator(variable, (IAST) upperLimit, evalEngine);
                }
                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.isQuantity()) {
                    return new QuantityIterator(variable, (IQuantity) upperLimit);
                } else if (upperLimit.isReal()) {
                    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()) {
                    ISymbol sym = (ISymbol) list.arg1();
                    if (!sym.isVariable() || sym.isProtected()) {
                        // Cannot assign to raw object `1`.
                        throw new ArgumentTypeException(IOFunctions.getMessage("setraw", F.list(sym), EvalEngine.get()));
                    }
                    variable = sym;
                } else {
                    // Raw object `1` cannot be used as an iterator.
                    throw new ArgumentTypeException(IOFunctions.getMessage("itraw", F.list(list.arg1()), EvalEngine.get()));
                }
                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.isQuantity() && upperLimit.isQuantity()) {
                    return new QuantityIterator(variable, (IQuantity) lowerLimit, (IQuantity) upperLimit);
                } else if (lowerLimit.isReal() && upperLimit.isReal()) {
                    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) {
                    ISymbol sym = (ISymbol) list.arg1();
                    if (!sym.isVariable() || sym.isProtected()) {
                        // Cannot assign to raw object `1`.
                        throw new ArgumentTypeException(IOFunctions.getMessage("setraw", F.list(sym), EvalEngine.get()));
                    }
                    variable = sym;
                } else {
                    // Raw object `1` cannot be used as an iterator.
                    throw new ArgumentTypeException(IOFunctions.getMessage("itraw", F.list(list.arg1()), EvalEngine.get()));
                }
                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.isQuantity() && upperLimit.isQuantity() && step.isQuantity()) {
                    return new QuantityIterator(variable, (IQuantity) lowerLimit, (IQuantity) upperLimit, (IQuantity) step);
                } else if (lowerLimit.isReal() && upperLimit.isReal() && step.isReal()) {
                    return new ISignedNumberIterator(variable, (ISignedNumber) lowerLimit, (ISignedNumber) upperLimit, (ISignedNumber) step);
                }
                break;
            default:
                // Argument `1` at position `2` does not have the correct form for an iterator.
                String str = IOFunctions.getMessage("itform", F.list(list, F.ZZ(position)), EvalEngine.get());
                throw new ArgumentTypeException(str);
        }
        return new ExprIterator(variable, lowerLimit, upperLimit, step, fNumericMode, evalEngine);
    } catch (LimitException le) {
        throw le;
    } catch (ArgumentTypeException atex) {
        throw atex;
    } catch (RuntimeException rex) {
        // Argument `1` at position `2` does not have the correct form for an iterator.
        String str = IOFunctions.getMessage("itform", F.list(list, F.ZZ(position)), EvalEngine.get());
        throw new ArgumentTypeException(str);
    } finally {
        evalEngine.setNumericMode(localNumericMode);
    }
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) Num(org.matheclipse.core.expression.Num) INum(org.matheclipse.core.interfaces.INum) 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) LimitException(org.matheclipse.core.eval.exception.LimitException) ArgumentTypeException(org.matheclipse.core.eval.exception.ArgumentTypeException)

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