Search in sources :

Example 26 with IInteger

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

the class AbstractIntegerSym method pow.

/** {@inheritDoc} */
@Override
public final IInteger pow(final long exponent) throws ArithmeticException {
    if (exponent < 0L) {
        throw new ArithmeticException("Negative exponent");
    }
    if (exponent == 0L) {
        if (!this.isZero()) {
            return F.C1;
        }
        throw new ArithmeticException("Indeterminate: 0^0");
    } else if (exponent == 1L) {
        return this;
    }
    if (isOne()) {
        return F.C1;
    }
    if (isMinusOne()) {
        if ((exponent & 1L) == 1L) {
            // isOdd(exponent) ?
            return F.CN1;
        } else {
            return F.C1;
        }
    }
    long exp = exponent;
    long b2pow = 0;
    while ((exp & 1) == 0L) {
        b2pow++;
        exp >>= 1;
    }
    IInteger r = this;
    IInteger x = r;
    while ((exp >>= 1) > 0L) {
        x = x.multiply(x);
        if ((exp & 1) != 0) {
            r = r.multiply(x);
        }
    }
    while (b2pow-- > 0L) {
        r = r.multiply(r);
    }
    return r;
}
Also used : IInteger(org.matheclipse.core.interfaces.IInteger)

Example 27 with IInteger

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

the class AbstractIntegerSym method factorInteger.

/** {@inheritDoc} */
@Override
public IAST factorInteger() {
    IInteger factor;
    IInteger last = F.CN2;
    int count = 0;
    final IAST iFactors = factorize(F.ListAlloc(10));
    final IAST list = List();
    IAST subList = null;
    for (int i = 1; i < iFactors.size(); i++) {
        factor = (IInteger) iFactors.get(i);
        if (!last.equals(factor)) {
            if (subList != null) {
                subList.append(AbstractIntegerSym.valueOf(count));
                list.append(subList);
            }
            count = 0;
            subList = F.ListAlloc(2);
            subList.append(factor);
        }
        count++;
        last = factor;
    }
    if (subList != null) {
        subList.append(AbstractIntegerSym.valueOf(count));
        list.append(subList);
    }
    return list;
}
Also used : IInteger(org.matheclipse.core.interfaces.IInteger) IAST(org.matheclipse.core.interfaces.IAST)

Example 28 with IInteger

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

the class IntegerSym 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 = F.ListAlloc(set.size() + 1);
    resultList.append(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 29 with IInteger

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

the class AbstractIntegerSym method egcd.

// /** {@inheritDoc} */
// @Override
// public IInteger gcd(final IInteger that) {
// return gcd( that);
// }
/**
	 * IntegerSym extended greatest common divisor.
	 * 
	 * @param that
	 *            if that is of type IntegerSym calculate the extended GCD otherwise call <code>super#egcd(IExpr)</code>
	 * 
	 * @return [ gcd(this,S), a, b ] with a*this + b*S = gcd(this,S).
	 */
@Override
public IExpr[] egcd(IExpr that) {
    if (that instanceof IInteger) {
        BigInteger S = ((IInteger) that).toBigNumerator();
        IInteger[] result = new IInteger[3];
        result[0] = null;
        result[1] = F.C1;
        result[2] = F.C1;
        if (that.isZero()) {
            result[0] = this;
            return result;
        }
        if (this.isZero()) {
            result[0] = (BigIntegerSym) that;
            return result;
        }
        BigInteger[] qr;
        BigInteger q = toBigNumerator();
        BigInteger r = S;
        BigInteger c1 = BigInteger.ONE;
        BigInteger d1 = BigInteger.ZERO;
        BigInteger c2 = BigInteger.ZERO;
        BigInteger d2 = BigInteger.ONE;
        BigInteger x1;
        BigInteger x2;
        while (!r.equals(BigInteger.ZERO)) {
            qr = q.divideAndRemainder(r);
            q = qr[0];
            x1 = c1.subtract(q.multiply(d1));
            x2 = c2.subtract(q.multiply(d2));
            c1 = d1;
            c2 = d2;
            d1 = x1;
            d2 = x2;
            q = r;
            r = qr[1];
        }
        if (q.signum() < 0) {
            q = q.negate();
            c1 = c1.negate();
            c2 = c2.negate();
        }
        result[0] = valueOf(q);
        result[1] = valueOf(c1);
        result[2] = valueOf(c2);
        return result;
    }
    return IInteger.super.egcd(that);
}
Also used : IInteger(org.matheclipse.core.interfaces.IInteger) BigInteger(java.math.BigInteger)

Example 30 with IInteger

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

the class Product method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    Validate.checkRange(ast, 3);
    IExpr arg1 = ast.arg1();
    if (arg1.isAST()) {
        arg1 = F.expand(arg1, false, false);
        if (!arg1.isPresent()) {
            arg1 = ast.arg1();
        }
    }
    if (arg1.isTimes()) {
        IAST prod = ast.setAtCopy(1, null);
        return ((IAST) arg1).mapThread(prod, 1);
    }
    IExpr temp = evaluateTableThrow(ast, Times(), Times(), engine);
    if (temp.isPresent()) {
        return temp;
    }
    if (arg1.isPower()) {
        IExpr powArg2 = arg1.getAt(2);
        boolean flag = true;
        // Prod( i^a, {i,from,to},... )
        for (int i = 2; i < ast.size(); i++) {
            IIterator iterator = Iterator.create((IAST) ast.get(i), engine);
            if (iterator.isValidVariable() && powArg2.isFree(iterator.getVariable())) {
                continue;
            }
            flag = false;
            break;
        }
        if (flag) {
            IAST prod = ast.copy();
            prod.set(1, arg1.getAt(1));
            return F.Power(prod, powArg2);
        }
    }
    IExpr argN = ast.get(ast.size() - 1);
    if (ast.size() >= 3 && argN.isList()) {
        IIterator iterator = Iterator.create((IAST) argN, engine);
        if (iterator.isValidVariable()) {
            if (iterator.getLowerLimit().isInteger() && iterator.getUpperLimit().isSymbol() && iterator.getStep().isOne()) {
                final ISymbol var = iterator.getVariable();
                final IInteger from = (IInteger) iterator.getLowerLimit();
                final ISymbol to = (ISymbol) iterator.getUpperLimit();
                if (arg1.isPower()) {
                    IExpr powArg1 = arg1.getAt(1);
                    IExpr powArg2 = arg1.getAt(2);
                    if (powArg1.isFree(var)) {
                        if (iterator.getLowerLimit().isOne()) {
                            if (powArg2.equals(var)) {
                                // Prod( a^i, ..., {i,from,to} )
                                if (ast.isAST2()) {
                                    return F.Power(powArg1, Times(C1D2, to, Plus(C1, to)));
                                }
                                IAST result = ast.clone();
                                result.remove(ast.size() - 1);
                                result.set(1, F.Power(powArg1, Times(C1D2, to, Plus(C1, to))));
                                return result;
                            }
                        }
                    }
                }
                if (arg1.isFree(var)) {
                    if (ast.isAST2()) {
                        if (from.isOne()) {
                            return F.Power(ast.arg1(), to);
                        }
                        if (from.isZero()) {
                            return F.Power(ast.arg1(), Plus(to, C1));
                        }
                    } else {
                        IAST result = ast.clone();
                        result.remove(ast.size() - 1);
                        if (from.isOne()) {
                            result.set(1, F.Power(ast.arg1(), to));
                            return result;
                        }
                        if (from.isZero()) {
                            result.set(1, F.Power(ast.arg1(), Plus(to, C1)));
                            return result;
                        }
                    }
                }
            }
        }
        IAST resultList = Times();
        temp = evaluateLast(ast.arg1(), iterator, resultList, C1);
        if (!temp.isPresent() || temp.equals(resultList)) {
            return F.NIL;
        }
        if (ast.isAST2()) {
            return temp;
        } else {
            IAST result = ast.clone();
            result.remove(ast.size() - 1);
            result.set(1, temp);
            return result;
        }
    }
    return F.NIL;
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) IInteger(org.matheclipse.core.interfaces.IInteger) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) IIterator(org.matheclipse.core.generic.interfaces.IIterator)

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