use of org.matheclipse.core.interfaces.IRational in project symja_android_library by axkr.
the class ComplexSym method normalize.
@Override
public INumber normalize() {
if (fImaginary.isZero()) {
if (fReal instanceof IFraction) {
if (fReal.getDenominator().isOne()) {
return fReal.getNumerator();
}
if (fReal.getNumerator().isZero()) {
return F.C0;
}
}
return fReal;
}
boolean evaled = false;
IRational newRe = fReal;
IRational newIm = fImaginary;
if (fReal instanceof IFraction) {
if (fReal.getDenominator().isOne()) {
newRe = fReal.getNumerator();
evaled = true;
}
if (fReal.getNumerator().isZero()) {
newRe = F.C0;
evaled = true;
}
}
if (fImaginary instanceof IFraction && fImaginary.getDenominator().isOne()) {
newIm = fImaginary.getNumerator();
evaled = true;
}
return evaled ? valueOf(newRe, newIm) : this;
}
use of org.matheclipse.core.interfaces.IRational in project symja_android_library by axkr.
the class F method complexNum.
public static IComplexNum complexNum(final IComplex value) {
final IRational realFraction = value.getRealPart();
final IRational imagFraction = value.getImaginaryPart();
final EvalEngine engine = EvalEngine.get();
if (engine.isApfloat()) {
return ApcomplexNum.valueOf(realFraction.toBigNumerator(), realFraction.toBigDenominator(), imagFraction.toBigNumerator(), imagFraction.toBigDenominator(), engine.getNumericPrecision());
}
// double precision complex number
double nr = realFraction.getNumerator().doubleValue();
double dr = realFraction.getDenominator().doubleValue();
double ni = imagFraction.getNumerator().doubleValue();
double di = imagFraction.getDenominator().doubleValue();
return complexNum(nr / dr, ni / di);
}
use of org.matheclipse.core.interfaces.IRational in project symja_android_library by axkr.
the class TeXFormFactory method convertComplex.
public void convertComplex(final StringBuffer buf, final IComplex c, final int precedence) {
if (c.isImaginaryUnit()) {
buf.append("i ");
return;
}
if (precedence > plusPrec) {
buf.append("\\left( ");
}
IRational re = c.getRealPart();
IRational im = c.getImaginaryPart();
if (!re.isZero()) {
convert(buf, c.getRealPart(), 0);
if (im.compareInt(0) >= 0) {
buf.append(" + ");
} else {
buf.append(" - ");
im = im.negate();
}
}
convert(buf, im, 0);
// InvisibleTimes
buf.append("\\,");
buf.append("i ");
if (precedence > plusPrec) {
buf.append("\\right) ");
}
}
use of org.matheclipse.core.interfaces.IRational 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