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;
}
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);
}
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;
}
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);
}
}
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;
}
Aggregations