use of edu.jas.poly.ComplexRing in project symja_android_library by axkr.
the class Algebra method factorComplex.
public static IAST factorComplex(GenPolynomial<BigRational> polyRat, JASConvert<BigRational> jas, List<IExpr> varList, ISymbol head, boolean noGCDLCM) {
TermOrder termOrder = TermOrderByName.Lexicographic;
// Object[] objects = jas.factorTerms(polyRat);
String[] vars = new String[varList.size()];
for (int i = 0; i < varList.size(); i++) {
vars[i] = varList.get(i).toString();
}
Object[] objects = JASConvert.rationalFromRationalCoefficientsFactor(new GenPolynomialRing<BigRational>(BigRational.ZERO, varList.size(), termOrder, vars), polyRat);
java.math.BigInteger gcd = (java.math.BigInteger) objects[0];
java.math.BigInteger lcm = (java.math.BigInteger) objects[1];
GenPolynomial<BigRational> poly = (GenPolynomial<BigRational>) objects[2];
ComplexRing<BigRational> cfac = new ComplexRing<BigRational>(BigRational.ZERO);
GenPolynomialRing<Complex<BigRational>> cpfac = new GenPolynomialRing<Complex<BigRational>>(cfac, 1, termOrder);
GenPolynomial<Complex<BigRational>> a = PolyUtil.complexFromAny(cpfac, poly);
FactorComplex<BigRational> factorAbstract = new FactorComplex<BigRational>(cfac);
SortedMap<GenPolynomial<Complex<BigRational>>, Long> map = factorAbstract.factors(a);
IAST result = F.ast(head);
if (!noGCDLCM) {
if (!gcd.equals(java.math.BigInteger.ONE) || !lcm.equals(java.math.BigInteger.ONE)) {
result.append(F.fraction(gcd, lcm));
}
}
GenPolynomial<Complex<BigRational>> temp;
for (SortedMap.Entry<GenPolynomial<Complex<BigRational>>, Long> entry : map.entrySet()) {
if (entry.getKey().isONE() && entry.getValue().equals(1L)) {
continue;
}
temp = entry.getKey();
result.append(F.Power(jas.complexPoly2Expr(entry.getKey()), F.integer(entry.getValue())));
}
return result;
}
use of edu.jas.poly.ComplexRing in project symja_android_library by axkr.
the class JASConvert method complex2Poly.
/**
* Convert a complex number into a jas polynomial. If the conversion isn't possible this method
* throws a <code>JASConversionException</code>.
*
* @param complexValue the complex value containing a rational real and imaginary part
* @return a jas polynomial
* @throws JASConversionException
*/
private GenPolynomial<C> complex2Poly(final IComplex complexValue) throws JASConversionException {
IRational reRational = complexValue.reRational();
IRational imRational = complexValue.imRational();
BigRational nre = new BigRational(reRational.toBigNumerator());
BigRational dre = new BigRational(reRational.toBigDenominator());
BigRational re = nre.divide(dre);
BigRational nim = new BigRational(imRational.toBigNumerator());
BigRational dim = new BigRational(imRational.toBigDenominator());
BigRational im = nim.divide(dim);
if (fRingFactory instanceof ComplexRing) {
ComplexRing ring = (ComplexRing) fRingFactory;
Complex<BigRational> c = new Complex<BigRational>(ring, re, im);
return new GenPolynomial(fPolyFactory, c);
} else {
// "ComplexRing expected"
throw new JASConversionException();
}
}
use of edu.jas.poly.ComplexRing in project symja_android_library by axkr.
the class RootIntervals method croots.
/**
* Complex numeric roots intervals.
*
* @param ast
* @return
*/
public static IAST croots(final IExpr arg, boolean numeric) {
try {
VariablesSet eVar = new VariablesSet(arg);
if (!eVar.isSize(1)) {
// only possible for univariate polynomials
return F.NIL;
}
IExpr expr = F.evalExpandAll(arg);
ASTRange r = new ASTRange(eVar.getVarList(), 1);
List<IExpr> varList = r;
ComplexRing<BigRational> cfac = new ComplexRing<BigRational>(new BigRational(1));
ComplexRootsAbstract<BigRational> cr = new ComplexRootsSturm<BigRational>(cfac);
JASConvert<Complex<BigRational>> jas = new JASConvert<Complex<BigRational>>(varList, cfac);
GenPolynomial<Complex<BigRational>> poly = jas.numericExpr2JAS(expr);
Squarefree<Complex<BigRational>> engine = SquarefreeFactory.<Complex<BigRational>>getImplementation(cfac);
poly = engine.squarefreePart(poly);
List<Rectangle<BigRational>> roots = cr.complexRoots(poly);
BigRational len = new BigRational(1, 100000L);
IAST resultList = F.List();
if (numeric) {
for (Rectangle<BigRational> root : roots) {
Rectangle<BigRational> refine = cr.complexRootRefinement(root, poly, len);
resultList.append(JASConvert.jas2Numeric(refine.getCenter(), Config.DEFAULT_ROOTS_CHOP_DELTA));
}
} else {
IAST rectangleList;
for (Rectangle<BigRational> root : roots) {
rectangleList = F.List();
Rectangle<BigRational> refine = cr.complexRootRefinement(root, poly, len);
rectangleList.append(JASConvert.jas2Complex(refine.getNW()));
rectangleList.append(JASConvert.jas2Complex(refine.getSW()));
rectangleList.append(JASConvert.jas2Complex(refine.getSE()));
rectangleList.append(JASConvert.jas2Complex(refine.getNE()));
resultList.append(rectangleList);
// System.out.println("refine = " + refine);
}
}
return resultList;
} catch (InvalidBoundaryException e) {
if (Config.SHOW_STACKTRACE) {
e.printStackTrace();
}
} catch (JASConversionException e) {
if (Config.SHOW_STACKTRACE) {
e.printStackTrace();
}
}
return F.NIL;
}
use of edu.jas.poly.ComplexRing in project symja_android_library by axkr.
the class JASConvert method fraction2Poly.
/**
* Convert a fractional number into a jas polynomial.
*
* @param exprPoly
* @return a jas polynomial
*/
private GenPolynomial<C> fraction2Poly(final IFraction exprPoly) {
// .toJavaBigInteger();
BigInteger n = exprPoly.toBigNumerator();
// .toJavaBigInteger();
BigInteger d = exprPoly.toBigDenominator();
BigRational nr = new BigRational(n);
BigRational dr = new BigRational(d);
BigRational r = nr.divide(dr);
if (fRingFactory instanceof ComplexRing) {
ComplexRing<BigRational> ring = (ComplexRing<BigRational>) fRingFactory;
Complex<BigRational> c = new Complex<BigRational>(ring, r);
return new GenPolynomial(fPolyFactory, c);
} else {
return new GenPolynomial(fPolyFactory, r);
}
}
use of edu.jas.poly.ComplexRing in project symja_android_library by axkr.
the class Algebra method cancelGCD.
/**
* Calculate the 3 elements result array
*
* <pre>
* [
* commonFactor,
* numeratorPolynomial.divide(gcd(numeratorPolynomial, denominatorPolynomial)),
* denominatorPolynomial.divide(gcd(numeratorPolynomial, denominatorPolynomial))
* ]
* </pre>
*
* for the given expressions <code>numeratorPolynomial</code> and <code>denominatorPolynomial
* </code>.
*
* @param numerator an expression which should be converted to JAS polynomial (using
* substitutions)
* @param denominator a expression which could be converted to JAS polynomial (using
* substitutions)
* @return <code>null</code> if the expressions couldn't be converted to JAS polynomials or gcd
* equals 1
* @throws JASConversionException
*/
public static IExpr[] cancelGCD(final IExpr numerator, final IExpr denominator) throws JASConversionException {
try {
if (denominator.isInteger() && numerator.isPlus()) {
IExpr[] result = Cancel.cancelPlusIntegerGCD((IAST) numerator, (IInteger) denominator);
if (result != null) {
return result;
}
}
VariablesSet eVar = new VariablesSet(numerator);
eVar.addVarList(denominator);
if (eVar.size() == 0) {
return null;
}
IAST vars = eVar.getVarList();
PolynomialHomogenization substitutions = new PolynomialHomogenization(vars, EvalEngine.get());
IExpr[] subst = substitutions.replaceForward(numerator, denominator);
IExpr numeratorPolynomial = subst[0];
IExpr denominatorPolynomial = subst[1];
if (substitutions.size() > 0) {
eVar.clear();
eVar.addAll(substitutions.substitutedVariablesSet());
vars = eVar.getVarList();
}
try {
ExprPolynomialRing ring = new ExprPolynomialRing(vars);
ExprPolynomial pol1 = ring.create(numeratorPolynomial);
ExprPolynomial pol2 = ring.create(denominatorPolynomial);
List<IExpr> varList = eVar.getVarList().copyTo();
JASIExpr jas = new JASIExpr(varList, true);
GenPolynomial<IExpr> p1 = jas.expr2IExprJAS(pol1);
GenPolynomial<IExpr> p2 = jas.expr2IExprJAS(pol2);
GreatestCommonDivisor<IExpr> engine;
engine = GCDFactory.getImplementation(ExprRingFactory.CONST);
GenPolynomial<IExpr> gcd = engine.gcd(p1, p2);
IExpr[] result = new IExpr[3];
if (gcd.isONE()) {
return null;
// result[0] = jas.exprPoly2Expr(gcd);
// result[1] = jas.exprPoly2Expr(p1);
// result[2] = jas.exprPoly2Expr(p2);
} else {
result[0] = F.C1;
result[1] = F.eval(jas.exprPoly2Expr(p1.divide(gcd)));
result[2] = F.eval(jas.exprPoly2Expr(p2.divide(gcd)));
}
result[0] = substitutions.replaceBackward(result[0]);
result[1] = substitutions.replaceBackward(result[1]);
result[2] = substitutions.replaceBackward(result[2]);
return result;
} catch (RuntimeException rex) {
}
List<IExpr> varList = eVar.getVarList().copyTo();
ComplexRing<BigRational> cfac = new ComplexRing<BigRational>(BigRational.ZERO);
JASConvert<Complex<BigRational>> jas = new JASConvert<Complex<BigRational>>(varList, cfac);
GenPolynomial<Complex<BigRational>> p1 = jas.expr2JAS(numeratorPolynomial, false);
GenPolynomial<Complex<BigRational>> p2 = jas.expr2JAS(denominatorPolynomial, false);
GreatestCommonDivisor<Complex<BigRational>> engine;
engine = GCDFactory.getImplementation(cfac);
GenPolynomial<Complex<BigRational>> gcd;
// if (numeratorPolynomial.isSymbol()||denominatorPolynomial.isSymbol() ) {
// gcd = jas.expr2IExprJAS(F.C1);
// }else {
gcd = engine.gcd(p1, p2);
// }
IExpr[] result = new IExpr[3];
if (gcd.isONE()) {
return null;
// result[0] = jas.complexPoly2Expr(gcd);
// result[1] = jas.complexPoly2Expr(p1);
// result[2] = jas.complexPoly2Expr(p2);
} else {
result[0] = F.C1;
result[1] = F.eval(jas.complexPoly2Expr(p1.divide(gcd)));
result[2] = F.eval(jas.complexPoly2Expr(p2.divide(gcd)));
}
result[0] = substitutions.replaceBackward(result[0]);
result[1] = substitutions.replaceBackward(result[1]);
result[2] = substitutions.replaceBackward(result[2]);
return result;
} catch (RuntimeException e) {
LOGGER.debug("Algebra.cancelGCD() failed", e);
}
return null;
}
Aggregations