use of org.matheclipse.core.expression.IntegerSym 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 org.matheclipse.core.expression.IntegerSym in project symja_android_library by axkr.
the class Validate method checkIntType.
/**
* Check the {@code ast}s argument, if it's a Java {@code int} value in the range [ {@code
* startValue}, Integer.MAX_VALUE]
*
* @param ast
* @param position the arguments position in the {@code ast}
* @param startValue
* @return
*/
public static int checkIntType(IAST ast, int position, int startValue) {
final IExpr arg = ast.get(position);
if (arg instanceof IntegerSym) {
// IntegerSym always fits into an int number
int result = ((IntegerSym) arg).toInt();
if (startValue > result) {
// Machine-sized integer expected at position `2` in `1`.
String str = IOFunctions.getMessage("intm", F.list(ast, F.ZZ(position)), EvalEngine.get());
throw new ArgumentTypeException(str);
}
return result;
}
if (arg.isReal()) {
int result = arg.toIntDefault();
if (result == Integer.MIN_VALUE || startValue > result) {
// Machine-sized integer expected at position `2` in `1`.
String str = IOFunctions.getMessage("intm", F.list(ast, F.ZZ(position)), EvalEngine.get());
throw new ArgumentTypeException(str);
}
return result;
}
// Machine-sized integer expected at position `2` in `1`.
String str = IOFunctions.getMessage("intm", F.list(ast, F.ZZ(position)), EvalEngine.get());
throw new ArgumentTypeException(str);
}
use of org.matheclipse.core.expression.IntegerSym 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