Search in sources :

Example 21 with IInteger

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

the class BigIntegerSym method divisors.

/**
	 * Return the divisors of this integer number.
	 * 
	 * <pre>
	 * divisors(24) ==> {1,2,3,4,6,8,12,24}
	 * </pre>
	 */
@Override
public IAST divisors() {
    if (isOne() || isMinusOne()) {
        return F.List(F.C1);
    }
    Set<IInteger> set = new TreeSet<IInteger>();
    final IAST primeFactorsList = factorize(F.List());
    int len = primeFactorsList.size() - 1;
    // build the k-subsets from the primeFactorsList
    for (int k = 1; k < len; k++) {
        final KSubsetsList iter = Subsets.createKSubsets(primeFactorsList, k, F.List(), 1);
        for (IAST subset : iter) {
            if (subset == null) {
                break;
            }
            // create the product of all integers in the k-subset
            IInteger factor = F.C1;
            for (int j = 1; j < subset.size(); j++) {
                factor = factor.multiply((IInteger) subset.get(j));
            }
            // add this divisor to the set collection
            set.add(factor);
        }
    }
    // build the final divisors list from the tree set
    final IAST resultList = List(F.C1);
    for (IInteger entry : set) {
        resultList.append(entry);
    }
    resultList.append(this);
    return resultList;
}
Also used : TreeSet(java.util.TreeSet) IInteger(org.matheclipse.core.interfaces.IInteger) IAST(org.matheclipse.core.interfaces.IAST) KSubsetsList(org.matheclipse.core.builtin.Combinatoric.Subsets.KSubsetsList)

Example 22 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 23 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 24 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 25 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)

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