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