Search in sources :

Example 16 with INum

use of org.matheclipse.core.interfaces.INum 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)

Aggregations

INum (org.matheclipse.core.interfaces.INum)16 IExpr (org.matheclipse.core.interfaces.IExpr)15 IAST (org.matheclipse.core.interfaces.IAST)12 IInteger (org.matheclipse.core.interfaces.IInteger)9 ISymbol (org.matheclipse.core.interfaces.ISymbol)9 IComplexNum (org.matheclipse.core.interfaces.IComplexNum)7 IFraction (org.matheclipse.core.interfaces.IFraction)6 IComplex (org.matheclipse.core.interfaces.IComplex)4 IRational (org.matheclipse.core.interfaces.IRational)3 EvalEngine (org.matheclipse.core.eval.EvalEngine)2 ApfloatNum (org.matheclipse.core.expression.ApfloatNum)2 Num (org.matheclipse.core.expression.Num)2 BigFraction (org.hipparchus.fraction.BigFraction)1 OpenIntToDoubleHashMap (org.hipparchus.util.OpenIntToDoubleHashMap)1 WrongArgumentType (org.matheclipse.core.eval.exception.WrongArgumentType)1 ApcomplexNum (org.matheclipse.core.expression.ApcomplexNum)1 ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)1