Search in sources :

Example 1 with Int2IntRBTreeMap

use of it.unimi.dsi.fastutil.ints.Int2IntRBTreeMap in project symja_android_library by axkr.

the class AbstractFractionSym method factorSmallPrimes.

@Override
public IAST factorSmallPrimes(int numerator, int root) {
    IInteger b = numerator();
    boolean isNegative = false;
    if (complexSign() < 0) {
        b = b.negate();
        isNegative = true;
    }
    if (numerator != 1) {
        b = b.powerRational(numerator);
    }
    IInteger d = denominator();
    if (numerator != 1) {
        d = d.powerRational(numerator);
    }
    Int2IntMap bMap = new Int2IntRBTreeMap();
    BigInteger number = b.toBigNumerator();
    IAST bAST = AbstractIntegerSym.factorBigInteger(number, isNegative, numerator, root, bMap);
    Int2IntMap dMap = new Int2IntRBTreeMap();
    number = d.toBigNumerator();
    IAST dAST = AbstractIntegerSym.factorBigInteger(number, false, numerator, root, dMap);
    if (bAST.isPresent()) {
        if (dAST.isPresent()) {
            return F.Times(bAST, F.Power(dAST, F.CN1));
        }
        return F.Times(bAST, F.Power(denominator(), F.QQ(-numerator, root)));
    } else if (dAST.isPresent()) {
        return F.Times(F.Power(numerator(), F.QQ(numerator, root)), F.Power(dAST, F.CN1));
    }
    return F.NIL;
}
Also used : Int2IntRBTreeMap(it.unimi.dsi.fastutil.ints.Int2IntRBTreeMap) IInteger(org.matheclipse.core.interfaces.IInteger) BigInteger(java.math.BigInteger) IAST(org.matheclipse.core.interfaces.IAST) Int2IntMap(it.unimi.dsi.fastutil.ints.Int2IntMap)

Example 2 with Int2IntRBTreeMap

use of it.unimi.dsi.fastutil.ints.Int2IntRBTreeMap in project symja_android_library by axkr.

the class AbstractIntegerSym method factorSmallPrimes.

/**
 * {@inheritDoc}
 */
@Override
public IAST factorSmallPrimes(int numerator, int root) {
    // SortedMap<Integer, Integer> map = new TreeMap<Integer, Integer>();
    Int2IntMap map = new Int2IntRBTreeMap();
    IInteger b = this;
    boolean isNegative = false;
    if (complexSign() < 0) {
        b = b.negate();
        isNegative = true;
    }
    if (numerator != 1) {
        b = b.powerRational(numerator);
    }
    if (b.isLT(F.C8)) {
        return F.NIL;
    }
    BigInteger number = b.toBigNumerator();
    return factorBigInteger(number, isNegative, numerator, root, map);
}
Also used : Int2IntRBTreeMap(it.unimi.dsi.fastutil.ints.Int2IntRBTreeMap) IInteger(org.matheclipse.core.interfaces.IInteger) BigInteger(java.math.BigInteger) Int2IntMap(it.unimi.dsi.fastutil.ints.Int2IntMap)

Example 3 with Int2IntRBTreeMap

use of it.unimi.dsi.fastutil.ints.Int2IntRBTreeMap in project symja_android_library by axkr.

the class Primality method factorIInteger.

@Override
public IAST factorIInteger(IInteger b) {
    if (b.isZero()) {
        return F.CListC0;
    } else if (b.isOne()) {
        return F.CListC1;
    } else if (b.isMinusOne()) {
        return F.CListCN1;
    }
    if (b instanceof IntegerSym) {
        int intValue = b.intValue();
        return AbstractIntegerSym.factorizeLong(intValue);
    }
    boolean negative = false;
    if (b.complexSign() < 0) {
        b = b.negate();
        negative = true;
    }
    BigInteger big = b.toBigNumerator();
    try {
        long longValue = big.longValueExact();
        if (longValue < PrimeInteger.BETA) {
            return AbstractIntegerSym.factorizeLong(longValue);
        }
    } catch (ArithmeticException aex) {
    // go on with big integers
    }
    Int2IntMap map = new Int2IntRBTreeMap();
    // SortedMap<Integer, Integer> map = new TreeMap<Integer, Integer>();
    BigInteger rest = countPrimes32749(big, map);
    int allocSize = 1;
    for (Int2IntMap.Entry entry : map.int2IntEntrySet()) {
        allocSize += entry.getIntValue();
    }
    IASTAppendable result = F.ListAlloc(allocSize);
    if (negative) {
        result.append(F.CN1);
    }
    for (Int2IntMap.Entry entry : map.int2IntEntrySet()) {
        int key = entry.getIntKey();
        IInteger is = AbstractIntegerSym.valueOf(key);
        for (int i = 0; i < entry.getIntValue(); i++) {
            result.append(is);
        }
    }
    if (rest.equals(BigInteger.ONE)) {
        return result;
    }
    if (rest.isProbablePrime(IInteger.PRIME_CERTAINTY)) {
        result.append(AbstractIntegerSym.valueOf(rest));
        return result;
    }
    // b = AbstractIntegerSym.valueOf(rest);
    // SortedMap<BigInteger, Integer> bigMap = new TreeMap<BigInteger, Integer>();
    SortedMap<BigInteger, Integer> bigMap = factorInteger(rest);
    for (Map.Entry<BigInteger, Integer> entry : bigMap.entrySet()) {
        BigInteger key = entry.getKey();
        IInteger is = AbstractIntegerSym.valueOf(key);
        for (int i = 0; i < entry.getValue(); i++) {
            result.append(is);
        }
    }
    return result;
}
Also used : IInteger(org.matheclipse.core.interfaces.IInteger) BigInteger(java.math.BigInteger) PrimeInteger(edu.jas.arith.PrimeInteger) Int2IntRBTreeMap(it.unimi.dsi.fastutil.ints.Int2IntRBTreeMap) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IInteger(org.matheclipse.core.interfaces.IInteger) AbstractIntegerSym(org.matheclipse.core.expression.AbstractIntegerSym) IntegerSym(org.matheclipse.core.expression.IntegerSym) BigInteger(java.math.BigInteger) Int2IntMap(it.unimi.dsi.fastutil.ints.Int2IntMap) Map(java.util.Map) OpenIntToIExprHashMap(org.matheclipse.core.eval.util.OpenIntToIExprHashMap) Int2IntRBTreeMap(it.unimi.dsi.fastutil.ints.Int2IntRBTreeMap) SortedMap(java.util.SortedMap) Int2IntMap(it.unimi.dsi.fastutil.ints.Int2IntMap)

Example 4 with Int2IntRBTreeMap

use of it.unimi.dsi.fastutil.ints.Int2IntRBTreeMap in project symja_android_library by axkr.

the class Primality method factorInteger.

/**
 * Decomposes the argument <code>n</code> into small prime factors. The result is stored in the
 * <code>
 * map</code>.
 *
 * @param b
 * @param map3 of all BigInteger primes and their associated exponents
 * @see org.matheclipse.gpl.numbertheory.BigIntegerPrimality
 */
@Override
public void factorInteger(BigInteger b, SortedMultiset<BigInteger> map3) {
    // factor only small primes
    Int2IntMap map = new Int2IntRBTreeMap();
    boolean isNegative = false;
    if (b.signum() < 0) {
        b = b.negate();
        isNegative = true;
    }
    BigInteger rest = Primality.countPrimes32749(b, map);
    if (isNegative) {
        map3.put(BigInteger.valueOf(-1), 1);
    }
    ObjectSet<it.unimi.dsi.fastutil.ints.Int2IntMap.Entry> entrySet = map.int2IntEntrySet();
    for (Entry<Integer, Integer> entry : entrySet) {
        map3.put(BigInteger.valueOf(entry.getKey()), entry.getValue());
    }
    if (!rest.equals(BigInteger.ONE)) {
        // map3.put(rest, 1);
        pollardRhoFactors(rest, map3);
    }
}
Also used : IInteger(org.matheclipse.core.interfaces.IInteger) BigInteger(java.math.BigInteger) PrimeInteger(edu.jas.arith.PrimeInteger) Entry(java.util.Map.Entry) Int2IntRBTreeMap(it.unimi.dsi.fastutil.ints.Int2IntRBTreeMap) BigInteger(java.math.BigInteger) Int2IntMap(it.unimi.dsi.fastutil.ints.Int2IntMap)

Example 5 with Int2IntRBTreeMap

use of it.unimi.dsi.fastutil.ints.Int2IntRBTreeMap in project symja_android_library by axkr.

the class BigIntegerPrimality method factorIInteger.

@Override
public IAST factorIInteger(IInteger b) {
    if (b.isZero()) {
        return F.CListC0;
    } else if (b.isOne()) {
        return F.CListC1;
    } else if (b.isMinusOne()) {
        return F.CListCN1;
    }
    if (b instanceof IntegerSym) {
        int intValue = b.intValue();
        return AbstractIntegerSym.factorizeLong(intValue);
    }
    boolean negative = false;
    if (b.complexSign() < 0) {
        b = b.negate();
        negative = true;
    }
    BigInteger big = b.toBigNumerator();
    try {
        long longValue = big.longValueExact();
        if (longValue < PrimeInteger.BETA) {
            return AbstractIntegerSym.factorizeLong(longValue);
        }
    } catch (ArithmeticException aex) {
    // go on with big integers
    }
    Int2IntMap map = new Int2IntRBTreeMap();
    // SortedMap<Integer, Integer> map = new TreeMap<Integer, Integer>();
    BigInteger rest = Primality.countPrimes32749(big, map);
    int allocSize = 1;
    for (Int2IntMap.Entry entry : map.int2IntEntrySet()) {
        allocSize += entry.getIntValue();
    }
    IASTAppendable result = F.ListAlloc(allocSize);
    if (negative) {
        result.append(F.CN1);
    }
    for (Int2IntMap.Entry entry : map.int2IntEntrySet()) {
        int key = entry.getIntKey();
        IInteger is = AbstractIntegerSym.valueOf(key);
        for (int i = 0; i < entry.getIntValue(); i++) {
            result.append(is);
        }
    }
    if (rest.equals(BigInteger.ONE)) {
        return result;
    }
    if (rest.isProbablePrime(IInteger.PRIME_CERTAINTY)) {
        result.append(AbstractIntegerSym.valueOf(rest));
        return result;
    }
    SortedMap<BigInteger, Integer> bigMap = factorInteger(rest);
    for (Map.Entry<BigInteger, Integer> entry : bigMap.entrySet()) {
        BigInteger key = entry.getKey();
        IInteger is = AbstractIntegerSym.valueOf(key);
        for (int i = 0; i < entry.getValue(); i++) {
            result.append(is);
        }
    }
    return result;
}
Also used : IInteger(org.matheclipse.core.interfaces.IInteger) BigInteger(java.math.BigInteger) PrimeInteger(edu.jas.arith.PrimeInteger) Int2IntRBTreeMap(it.unimi.dsi.fastutil.ints.Int2IntRBTreeMap) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IInteger(org.matheclipse.core.interfaces.IInteger) AbstractIntegerSym(org.matheclipse.core.expression.AbstractIntegerSym) IntegerSym(org.matheclipse.core.expression.IntegerSym) BigInteger(java.math.BigInteger) Int2IntMap(it.unimi.dsi.fastutil.ints.Int2IntMap) Int2IntRBTreeMap(it.unimi.dsi.fastutil.ints.Int2IntRBTreeMap) Map(java.util.Map) SortedMap(java.util.SortedMap) Int2IntMap(it.unimi.dsi.fastutil.ints.Int2IntMap)

Aggregations

Int2IntMap (it.unimi.dsi.fastutil.ints.Int2IntMap)5 Int2IntRBTreeMap (it.unimi.dsi.fastutil.ints.Int2IntRBTreeMap)5 BigInteger (java.math.BigInteger)5 IInteger (org.matheclipse.core.interfaces.IInteger)5 PrimeInteger (edu.jas.arith.PrimeInteger)3 Map (java.util.Map)2 SortedMap (java.util.SortedMap)2 AbstractIntegerSym (org.matheclipse.core.expression.AbstractIntegerSym)2 IntegerSym (org.matheclipse.core.expression.IntegerSym)2 IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)2 Entry (java.util.Map.Entry)1 OpenIntToIExprHashMap (org.matheclipse.core.eval.util.OpenIntToIExprHashMap)1 IAST (org.matheclipse.core.interfaces.IAST)1