Search in sources :

Example 11 with IFraction

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);
}
Also used : IFraction(org.matheclipse.core.interfaces.IFraction) BigFraction(org.hipparchus.fraction.BigFraction) IInteger(org.matheclipse.core.interfaces.IInteger) BigInteger(java.math.BigInteger)

Example 12 with IFraction

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());
}
Also used : IFraction(org.matheclipse.core.interfaces.IFraction) BigInteger(java.math.BigInteger)

Example 13 with IFraction

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;
}
Also used : IFraction(org.matheclipse.core.interfaces.IFraction)

Example 14 with IFraction

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);
}
Also used : IFraction(org.matheclipse.core.interfaces.IFraction) JASIExpr(org.matheclipse.core.convert.JASIExpr) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 15 with IFraction

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);
}
Also used : IFraction(org.matheclipse.core.interfaces.IFraction) BigInteger(java.math.BigInteger)

Aggregations

IFraction (org.matheclipse.core.interfaces.IFraction)29 IInteger (org.matheclipse.core.interfaces.IInteger)16 IExpr (org.matheclipse.core.interfaces.IExpr)14 IAST (org.matheclipse.core.interfaces.IAST)11 ISymbol (org.matheclipse.core.interfaces.ISymbol)9 BigInteger (java.math.BigInteger)8 INum (org.matheclipse.core.interfaces.INum)7 IComplex (org.matheclipse.core.interfaces.IComplex)6 IComplexNum (org.matheclipse.core.interfaces.IComplexNum)6 GenPolynomial (edu.jas.poly.GenPolynomial)3 WrongArgumentType (org.matheclipse.core.eval.exception.WrongArgumentType)3 ExpVector (edu.jas.poly.ExpVector)2 BigFraction (org.hipparchus.fraction.BigFraction)2 JASIExpr (org.matheclipse.core.convert.JASIExpr)2 ApfloatNum (org.matheclipse.core.expression.ApfloatNum)2 IRational (org.matheclipse.core.interfaces.IRational)2 BigInteger (edu.jas.arith.BigInteger)1 BigRational (edu.jas.arith.BigRational)1 ModLong (edu.jas.arith.ModLong)1 ApcomplexNum (org.matheclipse.core.expression.ApcomplexNum)1