Search in sources :

Example 56 with IInteger

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

the class TeXFormFactory method convert.

public void convert(final StringBuffer buf, final Object o, final int precedence) {
    if (o instanceof IExpr) {
        IExpr expr = (IExpr) o;
        String str = CONSTANT_EXPRS.get(expr);
        if (str != null) {
            buf.append(str);
            return;
        }
    }
    if (o instanceof IAST) {
        final IAST f = ((IAST) o);
        IExpr h = f.head();
        if (h.isSymbol()) {
            IConverter converter = reflection(((ISymbol) h).getSymbolName());
            if ((converter != null) && (converter.convert(buf, f, precedence))) {
                return;
            }
        }
        convertAST(buf, f);
        return;
    }
    if (o instanceof IInteger) {
        convertInteger(buf, (IInteger) o, precedence);
        return;
    }
    if (o instanceof IFraction) {
        convertFraction(buf, (IFraction) o, precedence);
        return;
    }
    if (o instanceof INum) {
        convertDouble(buf, (INum) o, precedence);
        return;
    }
    if (o instanceof IComplexNum) {
        convertDoubleComplex(buf, (IComplexNum) o, precedence);
        return;
    }
    if (o instanceof IComplex) {
        convertComplex(buf, (IComplex) o, precedence);
        return;
    }
    if (o instanceof ISymbol) {
        convertSymbol(buf, (ISymbol) o);
        return;
    }
    if (o instanceof BigFraction) {
        convertFraction(buf, (BigFraction) o, precedence);
        return;
    }
    convertString(buf, o.toString());
}
Also used : IFraction(org.matheclipse.core.interfaces.IFraction) IComplex(org.matheclipse.core.interfaces.IComplex) ISymbol(org.matheclipse.core.interfaces.ISymbol) BigFraction(org.hipparchus.fraction.BigFraction) IInteger(org.matheclipse.core.interfaces.IInteger) IComplexNum(org.matheclipse.core.interfaces.IComplexNum) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) INum(org.matheclipse.core.interfaces.INum)

Example 57 with IInteger

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

the class ContinuedFraction method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    Validate.checkRange(ast, 2, 3);
    IExpr arg1 = ast.arg1();
    int maxIterations = Integer.MAX_VALUE;
    if (ast.isAST2() && ast.arg2().isInteger()) {
        maxIterations = Validate.checkIntType(ast, 2);
    }
    if (arg1 instanceof INum) {
        arg1 = F.fraction(((INum) arg1).getRealPart());
    } else if (arg1.isAST() || arg1.isSymbol() && arg1.isNumericFunction()) {
        IExpr num = engine.evalN(arg1);
        if (num instanceof INum) {
            arg1 = F.fraction(((INum) num).getRealPart());
        }
    }
    if (arg1.isRational()) {
        IRational rat = (IRational) arg1;
        IAST continuedFractionList;
        if (rat.getDenominator().isOne()) {
            continuedFractionList = F.List(rat.getNumerator());
        } else if (rat.getNumerator().isOne()) {
            continuedFractionList = F.ListAlloc(2);
            continuedFractionList.append(F.C0);
            continuedFractionList.append(rat.getDenominator());
        } else {
            IFraction temp = F.fraction(rat.getNumerator(), rat.getDenominator());
            IInteger quotient;
            IInteger remainder;
            continuedFractionList = F.ListAlloc(10);
            while (temp.getDenominator().compareInt(1) > 0 && (0 < maxIterations--)) {
                quotient = temp.getNumerator().div(temp.getDenominator());
                remainder = temp.getNumerator().mod(temp.getDenominator());
                continuedFractionList.append(quotient);
                temp = F.fraction(temp.getDenominator(), remainder);
                if (temp.getDenominator().isOne()) {
                    continuedFractionList.append(temp.getNumerator());
                }
            }
        }
        return continuedFractionList;
    }
    return F.NIL;
}
Also used : IFraction(org.matheclipse.core.interfaces.IFraction) IInteger(org.matheclipse.core.interfaces.IInteger) IRational(org.matheclipse.core.interfaces.IRational) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) INum(org.matheclipse.core.interfaces.INum)

Example 58 with IInteger

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

the class Derivative method evaluate.

@Override
public IExpr evaluate(IAST ast, EvalEngine engine) {
    if (ast.isAST1()) {
        IAST[] derivativeAST = ast.isDerivative();
        if (derivativeAST != null) {
            IAST derivativeHead = derivativeAST[0];
            boolean isZero = true;
            for (int i = 1; i < derivativeHead.size(); i++) {
                if (!derivativeHead.get(i).isZero()) {
                    isZero = false;
                    break;
                }
            }
            if (isZero) {
                if (derivativeAST[2] == null) {
                    return derivativeAST[1].arg1();
                }
            }
            if (derivativeHead.size() == 2) {
                IExpr head = derivativeHead.arg1();
                if (head.isInteger()) {
                    IAST functions = derivativeAST[1];
                    if (functions.size() == 2) {
                        try {
                            int n = ((IInteger) head).toInt();
                            if (n >= 1) {
                                IAST fullDerivative = derivativeAST[2];
                                return evaluateDArg1IfPossible(n, derivativeHead, functions, fullDerivative, engine);
                            }
                        } catch (ArithmeticException ae) {
                        // toInt() may throw ArithmeticException
                        }
                        return F.NIL;
                    }
                }
            }
            if (ast.head().isAST(F.Derivative, 2)) {
                // Derivative(n)
                IAST head = (IAST) ast.head();
                if (head.arg1().isInteger()) {
                    try {
                        int n = ((IInteger) head.arg1()).toInt();
                        IExpr arg1 = ast.arg1();
                        if (n >= 0) {
                            if (arg1.isSymbol()) {
                                ISymbol symbol = (ISymbol) arg1;
                            // return derivative(n, symbol, engine);
                            } else {
                                if (arg1.isFunction()) {
                                    return derivative(n, (IAST) arg1, engine);
                                }
                            }
                        }
                    } catch (ArithmeticException ae) {
                    }
                }
                return F.NIL;
            }
            if (ast.head().isAST(F.Derivative, 3)) {
                // Derivative(n, m)
                IAST head = (IAST) ast.head();
                if (head.arg1().isInteger() && head.arg2().isInteger()) {
                    try {
                        int n = ((IInteger) head.arg1()).toInt();
                        int m = ((IInteger) head.arg2()).toInt();
                        IExpr arg1 = ast.arg1();
                        if (n >= 0 && m >= 0) {
                            if (arg1.isSymbol()) {
                                ISymbol symbol = (ISymbol) arg1;
                                return derivative(n, m, symbol, engine);
                            }
                        }
                    } catch (ArithmeticException ae) {
                    }
                }
                return F.NIL;
            }
        }
    }
    return F.NIL;
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) IInteger(org.matheclipse.core.interfaces.IInteger) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 59 with IInteger

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

the class TrigExpand method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    Validate.checkSize(ast, 2);
    IExpr temp = ast.arg1();
    IExpr result = temp;
    while (temp.isPresent()) {
        result = evalExpandAll(temp);
        temp = F.NIL;
        if (result.isAST1()) {
            if (result.getAt(1).isPlus()) {
                if (result.isSin()) {
                    temp = expandSinPlus((IAST) result.getAt(1), 1);
                } else if (result.isCos()) {
                    temp = expandCosPlus((IAST) result.getAt(1), 1);
                }
            } else if (result.getAt(1).isTimes()) {
                IAST timesAST = (IAST) result.getAt(1);
                if (timesAST.arg1().isInteger()) {
                    IInteger n = (IInteger) timesAST.arg1();
                    if (n.compareInt(0) > 0) {
                        IExpr theta = timesAST.removeAtClone(1).getOneIdentity(F.C1);
                        if (result.isSin()) {
                            // Sin(n*theta)
                            return Sum(Times(Times(Times(Power(CN1, Times(Plus(F.i, CN1), C1D2)), Binomial(n, F.i)), Power(Cos(theta), Plus(n, Times(CN1, F.i)))), Power(Sin(theta), F.i)), List(F.i, C1, n, C2));
                        } else if (result.isCos()) {
                            // Cos(n*theta)
                            return Sum(Times(Times(Times(Power(CN1, Times(F.i, C1D2)), Binomial(n, F.i)), Power(Cos(theta), Plus(n, Times(CN1, F.i)))), Power(Sin(theta), F.i)), List(F.i, C0, n, C2));
                        }
                    }
                }
            }
        }
        if (temp.isPresent()) {
            result = temp;
        }
    }
    return result;
}
Also used : IInteger(org.matheclipse.core.interfaces.IInteger) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

Aggregations

IInteger (org.matheclipse.core.interfaces.IInteger)59 IAST (org.matheclipse.core.interfaces.IAST)34 IExpr (org.matheclipse.core.interfaces.IExpr)29 ISymbol (org.matheclipse.core.interfaces.ISymbol)18 IFraction (org.matheclipse.core.interfaces.IFraction)16 BigInteger (java.math.BigInteger)11 INum (org.matheclipse.core.interfaces.INum)10 IComplex (org.matheclipse.core.interfaces.IComplex)7 IComplexNum (org.matheclipse.core.interfaces.IComplexNum)7 WrongArgumentType (org.matheclipse.core.eval.exception.WrongArgumentType)6 ExpVector (edu.jas.poly.ExpVector)5 INumber (org.matheclipse.core.interfaces.INumber)4 ModLong (edu.jas.arith.ModLong)3 GenPolynomial (edu.jas.poly.GenPolynomial)3 Num (org.matheclipse.core.expression.Num)3 IRational (org.matheclipse.core.interfaces.IRational)3 ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)3 TreeSet (java.util.TreeSet)2 BigFraction (org.hipparchus.fraction.BigFraction)2 KSubsetsList (org.matheclipse.core.builtin.Combinatoric.Subsets.KSubsetsList)2