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));
}
}
}
}
use of org.matheclipse.core.interfaces.IInteger in project symja_android_library by axkr.
the class NumberTheory method factorial.
public static IInteger factorial(final IInteger x) {
try {
int ni = x.toInt();
BigInteger result;
if (ni < 0) {
result = BigIntegerMath.factorial(-1 * ni);
if ((ni & 0x0001) == 0x0001) {
// odd integer number
result = result.multiply(BigInteger.valueOf(-1L));
}
} else {
if (ni <= 20) {
return AbstractIntegerSym.valueOf(LongMath.factorial(ni));
}
result = BigIntegerMath.factorial(ni);
}
return AbstractIntegerSym.valueOf(result);
} catch (ArithmeticException ae) {
//
}
IInteger result = F.C1;
if (x.compareTo(F.C0) == -1) {
result = F.CN1;
for (IInteger i = F.CN2; i.compareTo(x) >= 0; i = i.add(F.CN1)) {
result = result.multiply(i);
}
} else {
for (IInteger i = F.C2; i.compareTo(x) <= 0; i = i.add(F.C1)) {
result = result.multiply(i);
}
}
return result;
}
use of org.matheclipse.core.interfaces.IInteger in project symja_android_library by axkr.
the class NumberTheory method fibonacci.
/**
* <p>
* Fibonacci sequence. Algorithm in <code>O(log(n))</code> time.
* </p>
* See: <a href= "https://www.rosettacode.org/wiki/Fibonacci_sequence#Iterative_28"> Roseatta code: Fibonacci
* sequence.</a>
*
* @param iArg
* @return
*/
public static IInteger fibonacci(final IInteger iArg) {
IInteger a = F.C1;
IInteger b = F.C0;
IInteger c = F.C1;
IInteger d = F.C0;
IInteger result = F.C0;
IInteger temp = iArg;
if (iArg.isNegative()) {
temp = temp.negate();
}
while (!temp.isZero()) {
if (temp.isOdd()) {
d = result.multiply(c);
result = a.multiply(c).add(result.multiply(b).add(d));
a = a.multiply(b).add(d);
}
d = c.multiply(c);
c = b.multiply(c).shiftLeft(1).add(d);
b = b.multiply(b).add(d);
temp = temp.shiftRight(1);
}
if (iArg.isNegative() && iArg.isEven()) {
return result.negate();
}
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 symbol
* the variable symbol
* @param engine
* the evaluation engine
* @return the iterator
*/
public static IIterator<IExpr> create(final IAST list, final ISymbol symbol, final EvalEngine engine) {
EvalEngine evalEngine = engine;
IExpr lowerLimit;
IExpr upperLimit;
IExpr step;
ISymbol variable;
boolean fNumericMode;
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 = symbol;
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(symbol, 1, iUpperLimit, 1);
} catch (ArithmeticException ae) {
//
}
} else if (upperLimit.isRational()) {
try {
return new RationalIterator(symbol, 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 = evalEngine.evalWithoutNumericReset(list.arg1());
upperLimit = evalEngine.evalWithoutNumericReset(list.arg2());
step = F.C1;
variable = symbol;
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(symbol, iLowerLimit, iUpperLimit, 1);
} catch (ArithmeticException ae) {
//
}
} else if (lowerLimit.isRational() && upperLimit.isRational()) {
try {
return new RationalIterator(symbol, (IRational) lowerLimit, (IRational) upperLimit, F.C1);
} catch (ArithmeticException ae) {
//
}
} else if (lowerLimit.isSignedNumber() && upperLimit.isSignedNumber()) {
return new ISignedNumberIterator(variable, (ISignedNumber) lowerLimit, (ISignedNumber) upperLimit, F.C1);
}
break;
case 4:
lowerLimit = evalEngine.evalWithoutNumericReset(list.arg1());
upperLimit = evalEngine.evalWithoutNumericReset(list.arg2());
step = evalEngine.evalWithoutNumericReset(list.arg3());
variable = symbol;
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(symbol, iLowerLimit, iUpperLimit, iStep);
} catch (ArithmeticException ae) {
//
}
} else if (lowerLimit.isRational() && upperLimit.isRational() && step.isRational()) {
try {
return new RationalIterator(symbol, (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 AbstractFractionSym method factorInteger.
@Override
public /** {@inheritDoc} */
IAST factorInteger() {
IInteger num = getNumerator();
IInteger den = getDenominator();
IAST result = den.factorInteger();
// negate the exponents of the denominator part
for (int i = 1; i < result.size(); i++) {
IAST list = (IAST) result.get(i);
list.set(2, ((ISignedNumber) list.arg2()).negate());
}
// add the factors from the numerator part
result.appendArgs(num.factorInteger());
EvalAttributes.sort(result);
return result;
}
Aggregations