use of org.matheclipse.core.interfaces.IFraction in project symja_android_library by axkr.
the class ASCIIPrettyPrinter3 method convertNumber.
/**
*
* @param number
* @param caller
* @return <code>true</code> if the number is not equal <code>-1</code> or <code>1</code>
*/
private boolean convertNumber(ISignedNumber number, boolean caller) {
boolean negative = false;
if (number.isNegative()) {
number = number.negate();
negative = true;
}
if (caller == PLUS_CALL) {
if (negative) {
print(" - ");
} else {
print(" + ");
}
if (!number.isOne()) {
if (number.isFraction()) {
IFraction frac = (IFraction) number;
printFraction(frac.toBigNumerator().toString(), frac.toBigDenominator().toString());
} else {
convert(number, 0, NO_PLUS_CALL);
}
return true;
}
} else {
if (number.isFraction()) {
if (negative) {
if (fractionPrinted) {
print(" - ");
} else {
print("- ");
}
}
IFraction frac = (IFraction) number;
printFraction(frac.toBigNumerator().toString(), frac.toBigDenominator().toString());
} else {
if (negative) {
print("-");
}
print(number.toString());
}
return true;
}
return false;
}
use of org.matheclipse.core.interfaces.IFraction in project symja_android_library by axkr.
the class Power method convert.
/**
* Converts a given function into the corresponding MathML output
*
* @param buf
* StringBuffer for MathML output
* @param f
* The math function which should be converted to MathML
*/
@Override
public boolean convert(final StringBuffer buf, final IAST f, final int precedence) {
if (f.size() != 3) {
return false;
}
IExpr arg1 = f.arg1();
IExpr arg2 = f.arg2();
IExpr exp = arg2;
IExpr den = F.C1;
int useMROOT = 0;
if (arg2.isFraction()) {
IFraction f2 = ((IFraction) arg2);
if (f2.isPositive()) {
exp = f2.getNumerator();
if (f2.equals(F.C1D2)) {
fFactory.tagStart(buf, "msqrt");
useMROOT = 1;
} else {
den = f2.getDenominator();
fFactory.tagStart(buf, "mroot");
useMROOT = 2;
}
}
}
if (useMROOT > 0 && exp.isOne()) {
fFactory.convert(buf, arg1, fPrecedence);
} else {
fFactory.tagStart(buf, "msup");
precedenceOpen(buf, precedence);
fFactory.convert(buf, arg1, fPrecedence);
fFactory.convert(buf, exp, fPrecedence);
precedenceClose(buf, precedence);
fFactory.tagEnd(buf, "msup");
}
if (useMROOT == 1) {
fFactory.tagEnd(buf, "msqrt");
} else if (useMROOT == 2) {
fFactory.convert(buf, den, fPrecedence);
fFactory.tagEnd(buf, "mroot");
}
return true;
}
use of org.matheclipse.core.interfaces.IFraction 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());
}
use of org.matheclipse.core.interfaces.IFraction 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