/** * Be cautious with this method, no new internal rational is created * * @param numerator * @return */ protected static FractionSym newInstance(final BigFraction rational) { FractionSym r = new FractionSym(); r.fRational = rational; return r; }
@Override public IRational add(IRational parm1) { if (parm1.isZero()) { return this; } if (parm1 instanceof IFraction) { return add((IFraction) parm1); } if (parm1 instanceof IntegerSym) { IntegerSym is = (IntegerSym) parm1; long newnum = fNumerator + (long) fDenominator * (long) is.fIntValue; return valueOf(newnum, fDenominator); } BigInteger newnum = toBigNumerator().add(toBigDenominator().multiply(parm1.toBigNumerator())); return valueOf(newnum, toBigDenominator()); }
/** * Return a new rational representing <code>this * other</code>. * * @param other big integer to multiply. * @return Product of <code>this</code> and <code>other</code>. */ public IFraction mul(BigInteger other) { if (other.bitLength() <= 31) { int oint = other.intValue(); if (oint == 1) return this; if (oint == -1) return this.negate(); long newnum = (long) fNumerator * oint; return valueOf(newnum, fDenominator); } if (this.isOne()) { return valueOf(other, BigInteger.ONE); } if (this.isMinusOne()) { return valueOf(other.negate(), BigInteger.ONE); } return valueOf(toBigNumerator().multiply(other), toBigDenominator()); }
/** {@inheritDoc} */ public ISignedNumber minus(ISignedNumber that) { if (that instanceof FractionSym) { return this.add((FractionSym) that.negate()); } if (isZero()) { return that.negate(); } if (that instanceof IntegerSym) { return this.minus(valueOf(((IntegerSym) that).fInteger)); } return Num.valueOf(doubleValue() - that.doubleValue()); }
@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()); }
/** * Create a "fractional" number * * @param numerator * numerator of the fractional number * @param denominator * denumerator of the fractional number * @return IFraction */ public static IFraction fraction(final IInteger numerator, final IInteger denominator) { return FractionSym.valueOf(numerator, denominator); }
/** * 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 (this.isOne()) { return other; } if (other.isMinusOne()) { return this.negate(); } if (this.isMinusOne()) { return other.negate(); } if (other instanceof BigFractionSym) { return other.mul(this); } FractionSym fs = (FractionSym) other; long newnum = (long) fNumerator * fs.fNumerator; long newdenom = (long) fDenominator * fs.fDenominator; return valueOf(newnum, newdenom); }
if (isZero()) { return other; return valueOf((long) fNumerator + fs.fNumerator, fDenominator); long newdenom = denomgcd * otherdenomgcd; long newnum = otherdenomgcd * fNumerator + (long) fDenominator * (long) fs.fNumerator; return valueOf(newnum, newdenom); long newdenom = denomgcd * fs.fDenominator; long newnum = otherdenomgcd * fNumerator + denomgcd * fs.fNumerator; return valueOf(newnum, newdenom);
/** * Return a new rational representing <code>this / other</code>. * * @param other Rational to divide. * @return Quotient of <code>this</code> and <code>other</code>. */ @Override public IFraction div(IFraction other) { if (other instanceof BigFractionSym) { return ((BigFractionSym) other).idiv(this); } FractionSym fs = (FractionSym) other; if (fs.fDenominator == 1) { if (fs.fNumerator == 1) { return this; } if (fs.fNumerator == -1) { return this.negate(); } } long newnum = (long) fNumerator * fs.fDenominator; long newdenom = (long) fDenominator * fs.fNumerator; // +-inf : -c = -+inf if (newdenom == 0 && fs.fNumerator < 0) newnum = -newnum; return valueOf(newnum, newdenom); }
public ISignedNumber minus(ISignedNumber that) { if (that instanceof IntegerSym) { return this.add((IntegerSym) that.negate()); } if (isZero()) { return that.negate(); } if (that instanceof FractionSym) { return FractionSym.valueOf(fInteger).minus(that); } return Num.valueOf(fInteger.doubleValue() - that.doubleValue()); }
@Override public int compareTo(IExpr expr) { if (expr instanceof IRational) { if (expr instanceof FractionSym) { FractionSym temp = (FractionSym) expr; if (temp.fDenominator == fDenominator) { return fNumerator < temp.fNumerator ? -1 : fNumerator == temp.fNumerator ? 0 : 1; } long valt = (long) fNumerator * (long) temp.fDenominator; long valo = (long) fDenominator * (long) temp.fNumerator; return valt < valo ? -1 : valt == valo ? 0 : 1; } if (expr instanceof AbstractIntegerSym) { return compareTo(new BigFractionSym(((AbstractIntegerSym) expr).toBigNumerator(), BigInteger.ONE)); } if (expr instanceof BigFractionSym) { return -expr.compareTo(this); } } if (expr.isReal()) { return Double.compare(doubleValue(), ((ISignedNumber) expr).doubleValue()); } return -1; // return super.compareTo(expr); }
/** {@inheritDoc} */ @Override public IExpr inc() { return add(F.C1); }
/** {@inheritDoc} */ @Override public IRational normalize() { if (fDenominator == 1) { return F.integer(fNumerator); } if (isZero()) { return F.C0; } return this; }
/** {@inheritDoc} */ @Override public IExpr evaluate(EvalEngine engine) { if (engine.isNumericMode()) { return F.num(this); } if (getBigDenominator().equals(BigInteger.ONE)) { return F.integer(getBigNumerator()); } return null; }
@Override public IExpr gcd(IExpr that) { if (that instanceof FractionSym) { return gcd((FractionSym) that); } return super.gcd(that); }
/** * Compares this expression with the specified expression for order. Returns a * negative integer, zero, or a positive integer as this expression is * canonical less than, equal to, or greater than the specified expression. */ public int compareTo(final IExpr obj) { if (obj instanceof FractionSym) { return fRational.compareTo(((FractionSym) obj).fRational); } if (obj instanceof IntegerSym) { return fRational.compareTo(new BigFraction(((IntegerSym) obj).fInteger, BigInteger.ONE)); } return (hierarchy() - (obj).hierarchy()); }
/** * Returns the fractional part of the rational, i.e. the number this.sub(this.floor()). * * @return Next smaller integer of <code>this</code>. */ @Override public IFraction fractionalPart() { if (fDenominator == 1) { return AbstractFractionSym.ZERO; } return valueOf(fNumerator % fDenominator, fDenominator); }