Search in sources :

Example 1 with IntegerSym

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;
}
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 2 with IntegerSym

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);
}
Also used : IntegerSym(org.matheclipse.core.expression.IntegerSym) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 3 with IntegerSym

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

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