use of edu.jas.poly.Complex 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.poly.Complex 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.poly.Complex 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.Complex 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.Complex in project symja_android_library by axkr.
the class JASConvert method fraction2Poly.
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 ring = (ComplexRing) fRingFactory;
Complex<BigRational> c = new Complex<BigRational>(ring, r);
return new GenPolynomial(fPolyFactory, c);
} else {
return new GenPolynomial(fPolyFactory, r);
}
}
Aggregations