use of org.matheclipse.core.interfaces.IInteger in project symja_android_library by axkr.
the class ExtendedGCD method extendedGCD.
public static BigInteger extendedGCD(final IAST ast, BigInteger[] subBezouts) {
BigInteger factor;
BigInteger gcd = ((IInteger) ast.arg1()).toBigNumerator();
Object[] stepResult = extendedGCD(((IInteger) ast.arg2()).toBigNumerator(), gcd);
gcd = (BigInteger) stepResult[0];
subBezouts[0] = ((BigInteger[]) stepResult[1])[0];
subBezouts[1] = ((BigInteger[]) stepResult[1])[1];
for (int i = 3; i < ast.size(); i++) {
stepResult = extendedGCD(((IInteger) ast.get(i)).toBigNumerator(), gcd);
gcd = (BigInteger) stepResult[0];
factor = ((BigInteger[]) stepResult[1])[0];
for (int j = 0; j < i - 1; j++) {
subBezouts[j] = subBezouts[j].multiply(factor);
}
subBezouts[i - 1] = ((BigInteger[]) stepResult[1])[1];
}
return gcd;
}
use of org.matheclipse.core.interfaces.IInteger in project symja_android_library by axkr.
the class NumberTheory method multinomial.
/**
*
* @param indices
* the non-negative coefficients
* @param n
* the sum of the non-negative coefficients
* @return
*/
public static IInteger multinomial(final int[] indices, final int n) {
IInteger bn = AbstractIntegerSym.valueOf(n);
IInteger result = factorial(bn);
for (int i = 0; i < indices.length; i++) {
if (indices[i] != 0) {
result = result.div(factorial(AbstractIntegerSym.valueOf(indices[i])));
}
}
return result;
}
use of org.matheclipse.core.interfaces.IInteger in project symja_android_library by axkr.
the class Iterator method create.
/**
* Iterator specification for functions like <code>Table()</code> or
* <code>Sum()</code> or <code>Product()</code>
*
* @param list
* a list representing an iterator specification
* @param engine
* the evaluation engine
* @return the iterator
*/
public static IIterator<IExpr> create(final IAST list, final EvalEngine engine) {
EvalEngine evalEngine = engine;
IExpr lowerLimit;
IExpr upperLimit;
IExpr step;
ISymbol variable;
boolean fNumericMode;
// fNumericMode = evalEngine.isNumericMode() ||
// list.isMember(Predicates.isNumeric(), false);
boolean localNumericMode = evalEngine.isNumericMode();
try {
if (list.hasNumericArgument()) {
evalEngine.setNumericMode(true);
}
fNumericMode = evalEngine.isNumericMode();
switch(list.size()) {
case 2:
lowerLimit = F.C1;
upperLimit = evalEngine.evalWithoutNumericReset(list.arg1());
step = F.C1;
variable = null;
if (upperLimit instanceof Num) {
return new DoubleIterator(variable, 1.0, ((INum) upperLimit).doubleValue(), 1.0);
}
if (upperLimit.isInteger()) {
try {
int iUpperLimit = ((IInteger) upperLimit).toInt();
return new IntIterator(variable, 1, iUpperLimit, 1);
} catch (ArithmeticException ae) {
//
}
} else if (upperLimit.isRational()) {
try {
return new RationalIterator(variable, F.C1, (IRational) upperLimit, F.C1);
} catch (ArithmeticException ae) {
//
}
} else if (upperLimit.isSignedNumber()) {
return new ISignedNumberIterator(variable, F.C1, (ISignedNumber) upperLimit, F.C1);
}
break;
case 3:
lowerLimit = F.C1;
upperLimit = evalEngine.evalWithoutNumericReset(list.arg2());
step = F.C1;
if (list.arg1() instanceof ISymbol) {
variable = (ISymbol) list.arg1();
} else {
variable = null;
}
if (upperLimit instanceof Num) {
return new DoubleIterator(variable, 1.0, ((INum) upperLimit).doubleValue(), 1.0);
}
if (upperLimit.isInteger()) {
try {
int iUpperLimit = ((IInteger) upperLimit).toInt();
return new IntIterator(variable, 1, iUpperLimit, 1);
} catch (ArithmeticException ae) {
//
}
} else if (upperLimit.isRational()) {
try {
return new RationalIterator(variable, F.C1, (IRational) upperLimit, F.C1);
} catch (ArithmeticException ae) {
//
}
} else if (upperLimit.isSignedNumber()) {
return new ISignedNumberIterator(variable, F.C1, (ISignedNumber) upperLimit, F.C1);
}
break;
case 4:
lowerLimit = evalEngine.evalWithoutNumericReset(list.arg2());
upperLimit = evalEngine.evalWithoutNumericReset(list.arg3());
step = F.C1;
if (list.arg1().isSymbol()) {
variable = (ISymbol) list.arg1();
} else {
variable = null;
}
if (lowerLimit instanceof Num && upperLimit instanceof Num) {
return new DoubleIterator(variable, ((INum) lowerLimit).doubleValue(), ((INum) upperLimit).doubleValue(), 1.0);
}
if (lowerLimit.isInteger() && upperLimit.isInteger()) {
try {
int iLowerLimit = ((IInteger) lowerLimit).toInt();
int iUpperLimit = ((IInteger) upperLimit).toInt();
return new IntIterator(variable, iLowerLimit, iUpperLimit, 1);
} catch (ArithmeticException ae) {
//
}
} else if (lowerLimit.isRational() && upperLimit.isRational()) {
try {
return new RationalIterator(variable, (IRational) lowerLimit, (IRational) upperLimit, F.C1);
} catch (ArithmeticException ae) {
//
}
} else if (lowerLimit.isSignedNumber() && upperLimit.isSignedNumber()) {
ISignedNumber iLowerLimit = (ISignedNumber) lowerLimit;
ISignedNumber iUpperLimit = (ISignedNumber) upperLimit;
return new ISignedNumberIterator(variable, iLowerLimit, iUpperLimit, F.C1);
}
break;
case 5:
lowerLimit = evalEngine.evalWithoutNumericReset(list.arg2());
upperLimit = evalEngine.evalWithoutNumericReset(list.arg3());
step = evalEngine.evalWithoutNumericReset(list.arg4());
if (list.arg1() instanceof ISymbol) {
variable = (ISymbol) list.arg1();
} else {
variable = null;
}
if (lowerLimit instanceof Num && upperLimit instanceof Num && step instanceof Num) {
return new DoubleIterator(variable, ((INum) lowerLimit).doubleValue(), ((INum) upperLimit).doubleValue(), ((INum) step).doubleValue());
}
if (lowerLimit.isInteger() && upperLimit.isInteger() && step.isInteger()) {
try {
int iLowerLimit = ((IInteger) lowerLimit).toInt();
int iUpperLimit = ((IInteger) upperLimit).toInt();
int iStep = ((IInteger) step).toInt();
return new IntIterator(variable, iLowerLimit, iUpperLimit, iStep);
} catch (ArithmeticException ae) {
//
}
} else if (lowerLimit.isRational() && upperLimit.isRational() && step.isRational()) {
try {
return new RationalIterator(variable, (IRational) lowerLimit, (IRational) upperLimit, (IRational) step);
} catch (ArithmeticException ae) {
//
}
} else if (lowerLimit.isSignedNumber() && upperLimit.isSignedNumber() && step.isSignedNumber()) {
return new ISignedNumberIterator(variable, (ISignedNumber) lowerLimit, (ISignedNumber) upperLimit, (ISignedNumber) step);
}
break;
default:
lowerLimit = null;
upperLimit = null;
step = null;
variable = null;
}
return new ExprIterator(variable, evalEngine, lowerLimit, upperLimit, step, fNumericMode);
} finally {
evalEngine.setNumericMode(localNumericMode);
}
}
use of org.matheclipse.core.interfaces.IInteger in project symja_android_library by axkr.
the class AbstractAST method internalJavaString.
/** {@inheritDoc} */
@Override
public final String internalJavaString(boolean symbolsAsFactoryMethod, int depth, boolean useOperators) {
final String sep = ",";
final IExpr temp = head();
if (temp.equals(F.HoldForm) && size() == 2) {
return arg1().internalFormString(symbolsAsFactoryMethod, depth);
}
if (temp.equals(F.Hold) && size() == 2) {
return arg1().internalFormString(symbolsAsFactoryMethod, depth);
}
if (isInfinity()) {
return "oo";
}
if (isNegativeInfinity()) {
return "Noo";
}
if (isComplexInfinity()) {
return "CComplexInfinity";
}
if (this.equals(F.Slot1)) {
return "Slot1";
}
if (this.equals(F.Slot2)) {
return "Slot2";
}
if (temp.equals(F.Rational) && size() == 3) {
if (arg1().isInteger() && arg2().isInteger()) {
return F.QQ((IInteger) arg1(), (IInteger) arg2()).internalJavaString(symbolsAsFactoryMethod, depth, useOperators);
}
return arg1().internalFormString(symbolsAsFactoryMethod, depth);
}
if (isPower()) {
if (equalsAt(2, F.C1D2)) {
if (arg1().isInteger()) {
// square root of an integer number
IInteger i = (IInteger) arg1();
if (i.equals(F.C2)) {
return "CSqrt2";
} else if (i.equals(F.C3)) {
return "CSqrt3";
} else if (i.equals(F.C5)) {
return "CSqrt5";
} else if (i.equals(F.C6)) {
return "CSqrt6";
} else if (i.equals(F.C7)) {
return "CSqrt7";
} else if (i.equals(F.C10)) {
return "CSqrt10";
}
}
return "Sqrt(" + arg1().internalJavaString(symbolsAsFactoryMethod, depth + 1, useOperators) + ")";
}
if (equalsAt(2, F.C2)) {
return "Sqr(" + arg1().internalJavaString(symbolsAsFactoryMethod, depth + 1, useOperators) + ")";
}
if (equalsAt(2, F.CN1D2) && arg1().isInteger()) {
// negative square root of an integer number
IInteger i = (IInteger) arg1();
if (i.equals(F.C2)) {
return "C1DSqrt2";
} else if (i.equals(F.C3)) {
return "C1DSqrt3";
} else if (i.equals(F.C5)) {
return "C1DSqrt5";
} else if (i.equals(F.C6)) {
return "C1DSqrt6";
} else if (i.equals(F.C7)) {
return "C1DSqrt7";
} else if (i.equals(F.C10)) {
return "C1DSqrt10";
}
}
if (arg2().isInteger()) {
try {
long exp = ((IInteger) arg2()).toLong();
// create Power(arg1, exp)
return "Power(" + arg1().internalJavaString(symbolsAsFactoryMethod, depth + 1, useOperators) + "," + Long.toString(exp) + ")";
} catch (RuntimeException ex) {
}
}
}
StringBuilder text = new StringBuilder(size() * 10);
if (temp.isSymbol()) {
ISymbol sym = (ISymbol) temp;
String name = null;
if (Config.PARSER_USE_LOWERCASE_SYMBOLS) {
name = sym.toString();
if (name.length() > 0) {
name = name.toLowerCase(Locale.ENGLISH);
}
name = AST2Expr.PREDEFINED_SYMBOLS_MAP.get(name);
}
if (name == null && !Character.isUpperCase(sym.toString().charAt(0))) {
text.append("$(");
for (int i = 0; i < size(); i++) {
text.append(get(i).internalJavaString(symbolsAsFactoryMethod, depth + 1, useOperators));
if (i < size() - 1) {
text.append(sep);
}
}
text.append(')');
return text.toString();
}
} else if (temp.isPattern() || temp.isAST()) {
text.append("$(");
for (int i = 0; i < size(); i++) {
text.append(get(i).internalJavaString(symbolsAsFactoryMethod, depth + 1, useOperators));
if (i < size() - 1) {
text.append(sep);
}
}
text.append(')');
return text.toString();
}
if (isTimes() && size() == 3 && arg1().isMinusOne() && !arg2().isTimes()) {
return "Negate(" + arg2().internalJavaString(symbolsAsFactoryMethod, depth + 1, useOperators) + ")";
}
if (useOperators && size() == 3) {
if (isTimes()) {
IExpr arg1 = arg1();
IExpr arg2 = arg2();
boolean isLowerPrecedence = arg1.isPlus();
internalOperatorForm(arg1, isLowerPrecedence, symbolsAsFactoryMethod, depth, useOperators, text);
text.append('*');
isLowerPrecedence = arg2.isPlus();
internalOperatorForm(arg2, isLowerPrecedence, symbolsAsFactoryMethod, depth, useOperators, text);
return text.toString();
} else if (isPlus()) {
IExpr arg1 = arg1();
IExpr arg2 = arg2();
internalOperatorForm(arg1, false, symbolsAsFactoryMethod, depth, useOperators, text);
text.append('+');
internalOperatorForm(arg2, false, symbolsAsFactoryMethod, depth, useOperators, text);
return text.toString();
} else if (isPower()) {
IExpr arg1 = arg1();
IExpr arg2 = arg2();
boolean isLowerPrecedence = arg1.isTimes() || arg1.isPlus();
internalOperatorForm(arg1, isLowerPrecedence, symbolsAsFactoryMethod, depth, useOperators, text);
text.append('^');
isLowerPrecedence = arg2.isTimes() || arg2.isPlus();
internalOperatorForm(arg2, isLowerPrecedence, symbolsAsFactoryMethod, depth, useOperators, text);
return text.toString();
}
}
text.append(temp.internalJavaString(false, 0, useOperators));
text.append('(');
if (isTimes() || isPlus()) {
if (depth == 0 && isList()) {
text.append('\n');
}
internalFormOrderless(this, text, sep, symbolsAsFactoryMethod, depth, useOperators);
if (depth == 0 && isList()) {
text.append('\n');
}
} else {
if (depth == 0 && isList()) {
text.append('\n');
}
for (int i = 1; i < size(); i++) {
text.append(get(i).internalJavaString(symbolsAsFactoryMethod, depth + 1, useOperators));
if (i < size() - 1) {
text.append(sep);
if (depth == 0 && isList()) {
text.append('\n');
}
}
}
if (depth == 0 && isList()) {
text.append('\n');
}
}
text.append(')');
return text.toString();
}
use of org.matheclipse.core.interfaces.IInteger in project symja_android_library by axkr.
the class PartialFractionIntegrateGenerator method addSinglePartialFraction.
@Override
public void addSinglePartialFraction(GenPolynomial<BigRational> genPolynomial, GenPolynomial<BigRational> Di_1, int j) {
if (!genPolynomial.isZERO()) {
BigRational[] numer = new BigRational[3];
BigRational[] denom = new BigRational[3];
IExpr temp;
boolean isDegreeLE2 = Di_1.degree() <= 2;
if (isDegreeLE2 && j == 1L) {
Object[] objects = jas.factorTerms(genPolynomial);
java.math.BigInteger gcd = (java.math.BigInteger) objects[0];
java.math.BigInteger lcm = (java.math.BigInteger) objects[1];
GenPolynomial<edu.jas.arith.BigInteger> genPolynomial2 = ((GenPolynomial<edu.jas.arith.BigInteger>) objects[2]).multiply(edu.jas.arith.BigInteger.valueOf(gcd));
if (genPolynomial2.isONE()) {
GenPolynomial<BigRational> newDi_1 = Di_1.multiply(BigRational.valueOf(lcm));
isQuadratic(newDi_1, denom);
IFraction a = F.fraction(denom[2].numerator(), denom[2].denominator());
IFraction b = F.fraction(denom[1].numerator(), denom[1].denominator());
IFraction c = F.fraction(denom[0].numerator(), denom[0].denominator());
if (a.isZero()) {
// JavaForm[Log[b*x+c]/b]
result.append(Times(Log(Plus(c, Times(b, x))), Power(b, CN1)));
} else {
// compute b^2-4*a*c from
// (a*x^2+b*x+c)
BigRational cmp = denom[1].multiply(denom[1]).subtract(BigRational.valueOf(4L).multiply(denom[2]).multiply(denom[0]));
int cmpTo = cmp.compareTo(BigRational.ZERO);
// (2*a*x+b)
IExpr ax2Plusb = F.Plus(F.Times(F.C2, a, x), b);
if (cmpTo == 0) {
// (-2) / (2*a*x+b)
result.append(F.Times(F.integer(-2L), F.Power(ax2Plusb, F.CN1)));
} else if (cmpTo > 0) {
// (b^2-4ac)^(1/2)
temp = F.eval(F.Power(F.Subtract(F.Sqr(b), F.Times(F.C4, a, c)), F.C1D2));
result.append(F.Times(F.Power(temp, F.CN1), F.Log(F.Times(F.Subtract(ax2Plusb, temp), Power(F.Plus(ax2Plusb, temp), F.CN1)))));
} else {
// (4ac-b^2)^(1/2)
temp = F.eval(F.Power(F.Subtract(F.Times(F.C4, a, c), F.Sqr(b)), F.CN1D2));
result.append(F.Times(F.C2, temp, F.ArcTan(Times(ax2Plusb, temp))));
}
}
} else {
// (B*A*x) / (q*p*x)
isQuadratic(genPolynomial, numer);
IFraction A = F.fraction(numer[1].numerator(), numer[1].denominator());
IFraction B = F.fraction(numer[0].numerator(), numer[0].denominator());
isQuadratic(Di_1, denom);
IFraction p = F.fraction(denom[1].numerator(), denom[1].denominator());
IFraction q = F.fraction(denom[0].numerator(), denom[0].denominator());
if (A.isZero() && !p.isZero()) {
// JavaForm[B*Log[p*x+q]/p]
if (q.isNegative()) {
temp = Times(B, Log(Plus(q.negate(), Times(p.negate(), x))), Power(p, CN1));
} else {
temp = Times(B, Log(Plus(q, Times(p, x))), Power(p, CN1));
}
} else {
// JavaForm[A/2*Log[x^2+p*x+q]+(2*B-A*p)/(4*q-p^2)^(1/2)*ArcTan[(2*x+p)/(4*q-p^2)^(1/2)]]
temp = Plus(Times(C1D2, A, Log(Plus(q, Times(p, x), Power(x, C2)))), Times(ArcTan(Times(Plus(p, Times(C2, x)), Power(Plus(Times(CN1, Power(p, C2)), Times(C4, q)), CN1D2))), Plus(Times(C2, B), Times(CN1, A, p)), Power(Plus(Times(CN1, Power(p, C2)), Times(C4, q)), CN1D2)));
}
result.append(F.eval(temp));
}
} else if (isDegreeLE2 && j > 1L) {
isQuadratic(genPolynomial, numer);
IFraction A = F.fraction(numer[1].numerator(), numer[1].denominator());
IFraction B = F.fraction(numer[0].numerator(), numer[0].denominator());
isQuadratic(Di_1, denom);
IFraction a = F.fraction(denom[2].numerator(), denom[2].denominator());
IFraction b = F.fraction(denom[1].numerator(), denom[1].denominator());
IFraction c = F.fraction(denom[0].numerator(), denom[0].denominator());
IInteger k = F.integer(j);
if (A.isZero()) {
// JavaForm[B*((2*a*x+b)/((k-1)*(4*a*c-b^2)*(a*x^2+b*x+c)^(k-1))+
// (4*k*a-6*a)/((k-1)*(4*a*c-b^2))*Integrate[(a*x^2+b*x+c)^(-k+1),x])]
temp = Times(B, Plus(Times(Integrate(Power(Plus(c, Times(b, x), Times(a, Power(x, C2))), Plus(C1, Times(CN1, k))), x), Plus(Times(F.integer(-6L), a), Times(C4, a, k)), Power(Plus(CN1, k), CN1), Power(Plus(Times(CN1, Power(b, C2)), Times(C4, a, c)), CN1)), Times(Plus(b, Times(C2, a, x)), Power(Plus(CN1, k), CN1), Power(Plus(Times(CN1, Power(b, C2)), Times(C4, a, c)), CN1), Power(Plus(c, Times(b, x), Times(a, Power(x, C2))), Times(CN1, Plus(CN1, k))))));
} else {
// JavaForm[(-A)/(2*a*(k-1)*(a*x^2+b*x+c)^(k-1))+(B-A*b/(2*a))*Integrate[(a*x^2+b*x+c)^(-k),x]]
temp = Plus(Times(Integrate(Power(Plus(c, Times(b, x), Times(a, Power(x, C2))), Times(CN1, k)), x), Plus(B, Times(CN1D2, A, Power(a, CN1), b))), Times(CN1D2, A, Power(a, CN1), Power(Plus(CN1, k), CN1), Power(Plus(c, Times(b, x), Times(a, Power(x, C2))), Times(CN1, Plus(CN1, k)))));
}
result.append(F.eval(temp));
} else {
// ElementaryIntegration<BigRational> ei = new
// ElementaryIntegration<BigRational>(BigRational.ZERO);
// Integral<BigRational> integral= ei.integrate(genPolynomial,
// Di_1);
temp = F.eval(F.Times(jas.rationalPoly2Expr(genPolynomial), F.Power(jas.rationalPoly2Expr(Di_1), F.integer(j * (-1L)))));
if (!temp.isZero()) {
if (temp.isAST()) {
((IAST) temp).addEvalFlags(IAST.IS_DECOMPOSED_PARTIAL_FRACTION);
}
result.append(F.Integrate(temp, x));
}
}
}
}
Aggregations