use of org.matheclipse.core.interfaces.IFraction in project symja_android_library by axkr.
the class BigFractionSym method compareTo.
@Override
public int compareTo(IExpr expr) {
if (expr instanceof IFraction) {
BigInteger valthis = toBigNumerator().multiply(((IFraction) expr).toBigDenominator());
BigInteger valo = ((IFraction) expr).toBigNumerator().multiply(toBigDenominator());
return valthis.compareTo(valo);
}
if (expr instanceof IInteger) {
return fFraction.compareTo(new BigFraction(((IInteger) expr).toBigNumerator(), BigInteger.ONE));
}
if (expr instanceof Num) {
double d = fFraction.doubleValue() - ((Num) expr).getRealPart();
if (d < 0.0) {
return -1;
}
if (d > 0.0) {
return 1;
}
}
return super.compareTo(expr);
}
use of org.matheclipse.core.interfaces.IFraction in project symja_android_library by axkr.
the class FractionSym method multiply.
@Override
public IRational multiply(IRational parm1) {
if (isZero() || parm1.isZero()) {
return F.C0;
}
if (parm1.isOne()) {
return this;
}
if (parm1.isMinusOne()) {
return this.negate();
}
if (parm1 instanceof IFraction) {
return mul((IFraction) parm1);
}
if (parm1 instanceof IntegerSym) {
IntegerSym is = (IntegerSym) parm1;
long newnum = (long) fNumerator * (long) is.fIntValue;
return valueOf(newnum, fDenominator);
}
BigIntegerSym p1 = (BigIntegerSym) parm1;
BigInteger newnum = toBigNumerator().multiply(p1.toBigNumerator());
return valueOf(newnum, toBigDenominator());
}
use of org.matheclipse.core.interfaces.IFraction in project symja_android_library by axkr.
the class AbstractFractionSym method pow.
/** {@inheritDoc} */
@Override
public final IFraction pow(final long n) throws ArithmeticException {
if (n == 0L) {
if (!this.isZero()) {
return AbstractFractionSym.ONE;
}
throw new ArithmeticException("Indeterminate: 0^0");
} else if (n == 1L) {
return this;
} else if (n == -1L) {
return inverse();
}
long exp = n;
if (n < 0) {
exp *= -1;
}
int b2pow = 0;
while ((exp & 1) == 0) {
b2pow++;
exp >>= 1;
}
IFraction r = this;
IFraction x = r;
while ((exp >>= 1) > 0) {
x = x.mul(x);
if ((exp & 1) != 0) {
r = r.mul(x);
}
}
while (b2pow-- > 0) {
r = r.mul(r);
}
if (n < 0) {
return r.inverse();
}
return r;
}
use of org.matheclipse.core.interfaces.IFraction in project symja_android_library by axkr.
the class Algebra method fractionalPartsRational.
/**
* Split the expression into numerator and denominator parts, by separating positive and negative powers.
*
* @param arg
* @return the numerator and denominator expression
*/
public static IExpr[] fractionalPartsRational(final IExpr arg) {
if (arg.isFraction()) {
IFraction fr = (IFraction) arg;
IExpr[] parts = new IExpr[2];
parts[0] = fr.getNumerator();
parts[1] = fr.getDenominator();
return parts;
}
return fractionalParts(arg, false);
}
use of org.matheclipse.core.interfaces.IFraction in project symja_android_library by axkr.
the class AbstractFractionSym method mul.
/**
* Return a new rational representing <code>this * other</code>.
*
* @param other
* Rational to multiply.
* @return Product of <code>this</code> and <code>other</code>.
*/
@Override
public IFraction mul(IFraction other) {
if (other.isOne()) {
return this;
}
if (other.isZero()) {
return other;
}
if (other.isMinusOne()) {
return ((IFraction) this).negate();
}
BigInteger newnum = toBigNumerator().multiply(other.toBigNumerator());
BigInteger newdenom = toBigDenominator().multiply(other.toBigDenominator());
return valueOf(newnum, newdenom);
}
Aggregations