Search in sources :

Example 11 with IInteger

use of org.matheclipse.core.interfaces.IInteger in project symja_android_library by axkr.

the class BigIntegerSym method eulerPhi.

public static BigInteger eulerPhi(BigInteger value) throws ArithmeticException {
    if (value.equals(BigInteger.ZERO)) {
        return BigInteger.ZERO;
    }
    if (value.equals(BigInteger.ONE)) {
        return BigInteger.ONE;
    }
    IAST ast = AbstractIntegerSym.valueOf(value).factorInteger();
    IInteger phi = AbstractIntegerSym.valueOf(1);
    for (int i = 1; i < ast.size(); i++) {
        IAST element = (IAST) ast.get(i);
        IInteger q = (IInteger) element.arg1();
        int c = ((IInteger) element.arg2()).toInt();
        if (c == 1) {
            phi = phi.multiply(q.subtract(AbstractIntegerSym.valueOf(1)));
        } else {
            phi = phi.multiply(q.subtract(AbstractIntegerSym.valueOf(1)).multiply(q.pow(c - 1)));
        }
    }
    return phi.toBigNumerator();
}
Also used : IInteger(org.matheclipse.core.interfaces.IInteger) IAST(org.matheclipse.core.interfaces.IAST)

Example 12 with IInteger

use of org.matheclipse.core.interfaces.IInteger in project symja_android_library by axkr.

the class BigIntegerSym method exponent.

/**
	 * Get the highest exponent of <code>base</code> that divides <code>this</code>
	 * 
	 * @return the exponent
	 */
@Override
public IExpr exponent(IInteger base) {
    IInteger b = this;
    if (sign() < 0) {
        b = b.negate();
    } else if (b.isZero()) {
        return F.CInfinity;
    } else if (b.isOne()) {
        return F.C0;
    }
    if (b.equals(base)) {
        return F.C1;
    }
    BigInteger rest = Primality.countExponent(b.toBigNumerator(), base.toBigNumerator());
    return valueOf(rest);
}
Also used : IInteger(org.matheclipse.core.interfaces.IInteger) BigInteger(java.math.BigInteger)

Example 13 with IInteger

use of org.matheclipse.core.interfaces.IInteger 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 14 with IInteger

use of org.matheclipse.core.interfaces.IInteger in project symja_android_library by axkr.

the class AbstractIntegerSym method factorize.

/**
	 * Get all prime factors of this integer
	 * 
	 * @param result
	 *            add the prime factors to this result list
	 * @return
	 */
public IAST factorize(IAST result) {
    IInteger b = this;
    if (sign() < 0) {
        b = b.negate();
        result.append(F.CN1);
    } else if (b.isZero()) {
        result.append(F.C0);
        return result;
    } else if (b.isOne()) {
        result.append(F.C1);
        return result;
    }
    if (b instanceof IntegerSym) {
        Map<Long, Integer> map = PrimeInteger.factors(b.longValue());
        for (Map.Entry<Long, Integer> entry : map.entrySet()) {
            long key = entry.getKey();
            IInteger is = valueOf(key);
            for (int i = 0; i < entry.getValue(); i++) {
                result.append(is);
            }
        }
        return result;
    }
    SortedMap<Integer, Integer> map = new TreeMap<Integer, Integer>();
    BigInteger rest = Primality.countPrimes32749(b.toBigNumerator(), map);
    for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
        int key = entry.getKey();
        AbstractIntegerSym is = valueOf(key);
        for (int i = 0; i < entry.getValue(); i++) {
            result.append(is);
        }
    }
    if (rest.equals(BigInteger.ONE)) {
        return result;
    }
    if (rest.isProbablePrime(PRIME_CERTAINTY)) {
        result.append(valueOf(rest));
        return result;
    }
    b = valueOf(rest);
    SortedMap<BigInteger, Integer> bigMap = new TreeMap<BigInteger, Integer>();
    Primality.factorInteger(rest, bigMap);
    for (Map.Entry<BigInteger, Integer> entry : bigMap.entrySet()) {
        BigInteger key = entry.getKey();
        IInteger is = valueOf(key);
        for (int i = 0; i < entry.getValue(); i++) {
            result.append(is);
        }
    }
    return result;
}
Also used : TreeMap(java.util.TreeMap) IInteger(org.matheclipse.core.interfaces.IInteger) BigInteger(java.math.BigInteger) PrimeInteger(edu.jas.arith.PrimeInteger) IInteger(org.matheclipse.core.interfaces.IInteger) IVisitorLong(org.matheclipse.core.visit.IVisitorLong) BigInteger(java.math.BigInteger) TreeMap(java.util.TreeMap) Map(java.util.Map) SortedMap(java.util.SortedMap)

Example 15 with IInteger

use of org.matheclipse.core.interfaces.IInteger in project symja_android_library by axkr.

the class AbstractIntegerSym method pow.

/** {@inheritDoc} */
@Override
public final IInteger pow(final long exponent) throws ArithmeticException {
    if (exponent < 0L) {
        throw new ArithmeticException("Negative exponent");
    }
    if (exponent == 0L) {
        if (!this.isZero()) {
            return F.C1;
        }
        throw new ArithmeticException("Indeterminate: 0^0");
    } else if (exponent == 1L) {
        return this;
    }
    if (isOne()) {
        return F.C1;
    }
    if (isMinusOne()) {
        if ((exponent & 1L) == 1L) {
            // isOdd(exponent) ?
            return F.CN1;
        } else {
            return F.C1;
        }
    }
    long exp = exponent;
    long b2pow = 0;
    while ((exp & 1) == 0L) {
        b2pow++;
        exp >>= 1;
    }
    IInteger r = this;
    IInteger x = r;
    while ((exp >>= 1) > 0L) {
        x = x.multiply(x);
        if ((exp & 1) != 0) {
            r = r.multiply(x);
        }
    }
    while (b2pow-- > 0L) {
        r = r.multiply(r);
    }
    return r;
}
Also used : IInteger(org.matheclipse.core.interfaces.IInteger)

Aggregations

IInteger (org.matheclipse.core.interfaces.IInteger)59 IAST (org.matheclipse.core.interfaces.IAST)34 IExpr (org.matheclipse.core.interfaces.IExpr)29 ISymbol (org.matheclipse.core.interfaces.ISymbol)18 IFraction (org.matheclipse.core.interfaces.IFraction)16 BigInteger (java.math.BigInteger)11 INum (org.matheclipse.core.interfaces.INum)10 IComplex (org.matheclipse.core.interfaces.IComplex)7 IComplexNum (org.matheclipse.core.interfaces.IComplexNum)7 WrongArgumentType (org.matheclipse.core.eval.exception.WrongArgumentType)6 ExpVector (edu.jas.poly.ExpVector)5 INumber (org.matheclipse.core.interfaces.INumber)4 ModLong (edu.jas.arith.ModLong)3 GenPolynomial (edu.jas.poly.GenPolynomial)3 Num (org.matheclipse.core.expression.Num)3 IRational (org.matheclipse.core.interfaces.IRational)3 ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)3 TreeSet (java.util.TreeSet)2 BigFraction (org.hipparchus.fraction.BigFraction)2 KSubsetsList (org.matheclipse.core.builtin.Combinatoric.Subsets.KSubsetsList)2