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;
}
Aggregations