use of edu.jas.arith.BigRational in project symja_android_library by axkr.
the class JASIExpr method complexPoly2Expr.
/**
* Convert a JAS complex polynomial to <code>IExpr</code>.
*
* @param poly
* @return
* @throws ArithmeticException
* @throws ClassCastException
*/
public IExpr complexPoly2Expr(final GenPolynomial<Complex<BigRational>> poly) throws ArithmeticException, ClassCastException {
if (poly.length() == 0) {
return F.C0;
}
IAST result = F.Plus();
for (Monomial<Complex<BigRational>> monomial : poly) {
Complex<BigRational> coeff = monomial.coefficient();
ExpVector exp = monomial.exponent();
BigRational re = coeff.getRe();
BigRational im = coeff.getIm();
IAST monomTimes = F.Times(F.complex(F.fraction(re.numerator(), re.denominator()), F.fraction(im.numerator(), im.denominator())));
long lExp;
for (int i = 0; i < exp.length(); i++) {
lExp = exp.getVal(i);
if (lExp != 0) {
monomTimes.append(F.Power(fVariables.get(i), F.integer(lExp)));
}
}
if (monomTimes.isAST1()) {
result.append(monomTimes.arg1());
} else {
result.append(monomTimes);
}
}
if (result.isAST1()) {
return result.arg1();
} else {
return result;
}
}
use of edu.jas.arith.BigRational in project symja_android_library by axkr.
the class JASConvert method complexPoly2Expr.
/**
* Convert a JAS complex polynomial to <code>IExpr</code>.
*
* @param poly
* @return
* @throws ArithmeticException
* @throws ClassCastException
*/
public IExpr complexPoly2Expr(final GenPolynomial<Complex<BigRational>> poly) throws ArithmeticException, ClassCastException {
if (poly.length() == 0) {
return F.C0;
}
IAST result = F.Plus();
for (Monomial<Complex<BigRational>> monomial : poly) {
Complex<BigRational> coeff = monomial.coefficient();
ExpVector exp = monomial.exponent();
IAST monomTimes = F.Times();
monomialToExpr(coeff, exp, monomTimes);
result.append(monomTimes.getOneIdentity(F.C1));
}
return result.getOneIdentity(F.C0);
}
use of edu.jas.arith.BigRational in project symja_android_library by axkr.
the class JASConvert method rationalFromRationalCoefficientsFactor.
/**
* BigRational from BigRational coefficients. Represent as polynomial with
* BigInteger coefficients by multiplication with the gcd of the numerators
* and the lcm of the denominators of the BigRational coefficients. <br />
*
* @param fac
* result polynomial factory.
* @param A
* polynomial with BigRational coefficients to be converted.
* @return Object[] with 3 entries: [0]->gcd [1]->lcm and [2]->polynomial
* with BigInteger coefficients.
*/
public static Object[] rationalFromRationalCoefficientsFactor(GenPolynomialRing<BigRational> fac, GenPolynomial<BigRational> A) {
Object[] result = new Object[3];
if (A == null || A.isZERO()) {
result[0] = java.math.BigInteger.ONE;
result[1] = java.math.BigInteger.ZERO;
result[2] = fac.getZERO();
return result;
}
java.math.BigInteger gcd = null;
java.math.BigInteger lcm = null;
int sLCM = 0;
int sGCD = 0;
// lcm of denominators
Iterator<BigRational> iter = A.coefficientIterator();
while (iter.hasNext()) {
BigRational y = iter.next();
java.math.BigInteger numerator = y.numerator();
java.math.BigInteger denominator = y.denominator();
// lcm = lcm(lcm,x)
if (lcm == null) {
lcm = denominator;
sLCM = denominator.signum();
} else {
java.math.BigInteger d = lcm.gcd(denominator);
lcm = lcm.multiply(denominator.divide(d));
}
// gcd = gcd(gcd,x)
if (gcd == null) {
gcd = numerator;
sGCD = numerator.signum();
} else {
gcd = gcd.gcd(numerator);
}
}
if (sLCM < 0) {
lcm = lcm.negate();
}
if (sGCD < 0) {
gcd = gcd.negate();
}
result[0] = gcd;
result[1] = lcm;
result[2] = PolyUtil.<BigRational, BigRational>map(fac, A, new RatToRatFactor(gcd, lcm));
return result;
}
use of edu.jas.arith.BigRational in project symja_android_library by axkr.
the class PartialFractionIntegrateGenerator method isQuadratic.
/**
* Check if the polynomial has maximum degree 2 in 1 variable and return the
* coefficients.
*
* @param poly
* @return <code>null</code> if the polynomials degree > 2 and number of
* variables <> 1
*/
public static boolean isQuadratic(GenPolynomial<BigRational> poly, BigRational[] result) {
if (poly.degree() <= 2 && poly.numberOfVariables() == 1) {
result[0] = BigRational.ZERO;
result[1] = BigRational.ZERO;
result[2] = BigRational.ZERO;
for (Monomial<BigRational> monomial : poly) {
BigRational coeff = monomial.coefficient();
ExpVector exp = monomial.exponent();
for (int i = 0; i < exp.length(); i++) {
result[(int) exp.getVal(i)] = coeff;
}
}
return true;
}
return false;
}
use of edu.jas.arith.BigRational in project symja_android_library by axkr.
the class Algebra method partialFractionDecompositionRational.
/**
* Returns an AST with head <code>Plus</code>, which contains the partial fraction decomposition of the numerator
* and denominator parts.
*
* @param pf
* partial fraction generator
* @param parts
* @param variable
* a variable
* @return <code>F.NIL</code> if the partial fraction decomposition wasn't constructed
*/
public static IExpr partialFractionDecompositionRational(IPartialFractionGenerator pf, IExpr[] parts, ISymbol variable) {
try {
IAST variableList = F.List(variable);
IExpr exprNumerator = F.evalExpandAll(parts[0]);
IExpr exprDenominator = F.evalExpandAll(parts[1]);
ASTRange r = new ASTRange(variableList, 1);
List<IExpr> varList = r;
String[] varListStr = new String[1];
varListStr[0] = variableList.arg1().toString();
JASConvert<BigRational> jas = new JASConvert<BigRational>(varList, BigRational.ZERO);
GenPolynomial<BigRational> numerator = jas.expr2JAS(exprNumerator, false);
GenPolynomial<BigRational> denominator = jas.expr2JAS(exprDenominator, false);
// get factors
FactorAbstract<BigRational> factorAbstract = FactorFactory.getImplementation(BigRational.ZERO);
SortedMap<GenPolynomial<BigRational>, Long> sfactors = factorAbstract.baseFactors(denominator);
List<GenPolynomial<BigRational>> D = new ArrayList<GenPolynomial<BigRational>>(sfactors.keySet());
SquarefreeAbstract<BigRational> sqf = SquarefreeFactory.getImplementation(BigRational.ZERO);
List<List<GenPolynomial<BigRational>>> Ai = sqf.basePartialFraction(numerator, sfactors);
if (Ai.size() > 0) {
// IAST result = F.Plus();
pf.allocPlus(Ai.size() * 2);
pf.setJAS(jas);
if (!Ai.get(0).get(0).isZERO()) {
pf.addNonFractionalPart(Ai.get(0).get(0));
}
for (int i = 1; i < Ai.size(); i++) {
List<GenPolynomial<BigRational>> list = Ai.get(i);
int j = 0;
for (GenPolynomial<BigRational> genPolynomial : list) {
if (!genPolynomial.isZERO()) {
GenPolynomial<BigRational> Di_1 = D.get(i - 1);
pf.addSinglePartialFraction(genPolynomial, Di_1, j);
}
j++;
}
}
return pf.getResult();
}
} catch (JASConversionException e) {
if (Config.DEBUG) {
e.printStackTrace();
}
}
return F.NIL;
}
Aggregations