use of org.matheclipse.core.interfaces.IInteger in project symja_android_library by axkr.
the class BigIntegerSym method multiply.
@Override
public IRational multiply(IRational parm1) {
if (parm1.isZero()) {
return F.C0;
}
if (parm1.isOne()) {
return this;
}
if (parm1.isMinusOne()) {
return this.negate();
}
if (parm1 instanceof IFraction) {
return ((IFraction) parm1).multiply(this);
}
IInteger p1 = (IInteger) parm1;
BigInteger newnum = toBigNumerator().multiply(p1.toBigNumerator());
return valueOf(newnum);
}
use of org.matheclipse.core.interfaces.IInteger in project symja_android_library by axkr.
the class BigFractionSym method multiply.
@Override
public IRational multiply(IRational parm1) {
if (parm1.isOne()) {
return this;
}
if (parm1.isZero()) {
return parm1;
}
if (parm1.isMinusOne()) {
return this.negate();
}
if (parm1 instanceof IFraction) {
return mul((IFraction) parm1);
}
IInteger p1 = (IInteger) parm1;
BigInteger newnum = toBigNumerator().multiply(p1.toBigNumerator());
return valueOf(newnum, toBigDenominator());
}
use of org.matheclipse.core.interfaces.IInteger in project symja_android_library by axkr.
the class Coefficient method evaluate.
@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
Validate.checkRange(ast, 3, 4);
IExpr arg2 = ast.arg2();
// list of variable expressions extracted from the second argument
IAST listOfVariables = null;
// array of corresponding exponents for the list of variables
long[] exponents = null;
if (arg2.isTimes()) {
// Times(x, y^a,...)
IAST arg2AST = (IAST) arg2;
VariablesSet eVar = new VariablesSet(arg2AST);
listOfVariables = eVar.getVarList();
exponents = new long[listOfVariables.size() - 1];
for (int i = 0; i < exponents.length; i++) {
exponents[i] = 0L;
}
for (int i = 1; i < arg2AST.size(); i++) {
long value = 1L;
IExpr a1 = arg2AST.get(i);
if (arg2AST.get(i).isPower() && arg2AST.get(i).getAt(2).isInteger()) {
a1 = arg2AST.get(i).getAt(1);
IInteger ii = (IInteger) arg2AST.get(i).getAt(2);
try {
value = ii.toLong();
} catch (ArithmeticException ae) {
return F.NIL;
}
}
if (!setExponent(listOfVariables, a1, exponents, value)) {
return F.NIL;
}
}
} else {
listOfVariables = F.List();
listOfVariables.append(arg2);
exponents = new long[1];
exponents[0] = 1;
}
try {
long n = 1;
if (ast.isAST3()) {
if (ast.arg3().isNegativeInfinity()) {
return F.C0;
}
n = Validate.checkLongType(ast.arg3());
for (int i = 0; i < exponents.length; i++) {
exponents[i] *= n;
}
}
ExpVectorLong expArr = new ExpVectorLong(exponents);
IExpr expr = F.evalExpandAll(ast.arg1());
ExprPolynomialRing ring = new ExprPolynomialRing(ExprRingFactory.CONST, listOfVariables, listOfVariables.size() - 1);
ExprPolynomial poly = ring.create(expr, true, true);
return poly.coefficient(expArr);
} catch (RuntimeException ae) {
if (Config.DEBUG) {
ae.printStackTrace();
}
return F.C0;
}
}
use of org.matheclipse.core.interfaces.IInteger in project symja_android_library by axkr.
the class Solve method integerVariable.
public static IntVariable integerVariable(Network net, TreeMap<ISymbol, IntVariable> map, IExpr expr) throws ArithmeticException {
if (expr instanceof ISymbol) {
return map.get(expr);
}
if (expr instanceof IInteger) {
// throws ArithmeticException
int value = ((IInteger) expr).toInt();
return new IntVariable(net, value);
}
if (expr.isPlus()) {
IAST ast = (IAST) expr;
IntVariable result = integerVariable(net, map, ast.arg1());
for (int i = 2; i < ast.size(); i++) {
result = result.add(integerVariable(net, map, ast.get(i)));
}
return result;
} else if (expr.isTimes()) {
IAST ast = (IAST) expr;
IntVariable result = integerVariable(net, map, ast.arg1());
for (int i = 2; i < ast.size(); i++) {
result = result.multiply(integerVariable(net, map, ast.get(i)));
}
return result;
} else if (expr.isPower()) {
IAST ast = (IAST) expr;
if (ast.arg2().isInteger()) {
int value = ((IInteger) ast.arg2()).toInt();
if (value > 0) {
IntVariable result = integerVariable(net, map, ast.arg1());
for (int i = 1; i < value; i++) {
result = result.multiply(integerVariable(net, map, ast.arg1()));
}
return result;
}
}
} else if (expr.isASTSizeGE(F.Max, 3)) {
IAST ast = (IAST) expr;
IntVariable result = integerVariable(net, map, ast.arg1());
for (int i = 2; i < ast.size(); i++) {
result = result.max(integerVariable(net, map, ast.get(i)));
}
return result;
} else if (expr.isASTSizeGE(F.Min, 3)) {
IAST ast = (IAST) expr;
IntVariable result = integerVariable(net, map, ast.arg1());
for (int i = 2; i < ast.size(); i++) {
result = result.min(integerVariable(net, map, ast.get(i)));
}
return result;
} else if (expr.isAbs()) {
IAST ast = (IAST) expr;
return integerVariable(net, map, ast.arg1()).abs();
} else if (expr.isAST(F.Sign, 2)) {
IAST ast = (IAST) expr;
return integerVariable(net, map, ast.arg1()).sign();
}
throw new WrongArgumentType(expr, "for Solve(..., Integers)");
}
use of org.matheclipse.core.interfaces.IInteger in project symja_android_library by axkr.
the class PowerMod method evaluate.
@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
Validate.checkSize(ast, 4);
for (int i = 1; i < ast.size(); i++) {
if (!(ast.get(i).isInteger())) {
return F.NIL;
}
}
IInteger arg1 = (IInteger) ast.get(1);
IInteger arg2 = (IInteger) ast.get(2);
IInteger arg3 = (IInteger) ast.get(3);
try {
if (arg2.isMinusOne()) {
return arg1.modInverse(arg3);
}
return arg1.modPow(arg2, arg3);
} catch (ArithmeticException ae) {
engine.printMessage("PowerMod: " + ae.getMessage());
}
return F.NIL;
}
Aggregations