[Home] DMelt member access. Java source code: edu.jas.application.PolyUtilApp
/*
* $Id$
*/
package edu.jas.application;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.TreeSet;
import org.apache.log4j.Logger;
import edu.jas.arith.BigDecimal;
import edu.jas.arith.Product;
import edu.jas.arith.ProductRing;
import edu.jas.arith.Rational;
import edu.jas.poly.AlgebraicNumber;
import edu.jas.poly.AlgebraicNumberRing;
import edu.jas.poly.Complex;
import edu.jas.poly.ComplexRing;
import edu.jas.poly.ExpVector;
import edu.jas.poly.GenPolynomial;
import edu.jas.poly.GenPolynomialRing;
import edu.jas.poly.PolyUtil;
import edu.jas.poly.PolynomialList;
import edu.jas.poly.TermOrder;
import edu.jas.root.ComplexRoots;
import edu.jas.root.ComplexRootsAbstract;
import edu.jas.root.ComplexRootsSturm;
import edu.jas.root.Interval;
import edu.jas.root.InvalidBoundaryException;
import edu.jas.root.RealAlgebraicNumber;
import edu.jas.root.RealAlgebraicRing;
import edu.jas.root.RealRootTuple;
import edu.jas.root.RealRootsAbstract;
import edu.jas.root.RealRootsSturm;
import edu.jas.root.Rectangle;
import edu.jas.root.RootFactory;
import edu.jas.structure.GcdRingElem;
import edu.jas.structure.RingElem;
import edu.jas.structure.RingFactory;
import edu.jas.structure.UnaryFunctor;
import edu.jas.util.ListUtil;
/**
* Polynomial utilities for applications, for example conversion ExpVector to
* Product or zero dimensional ideal root computation.
* @param coefficient type
* @author Heinz Kredel
*/
public class PolyUtilApp> {
private static final Logger logger = Logger.getLogger(PolyUtilApp.class);
private static boolean debug = logger.isDebugEnabled();
/**
* Product representation.
* @param coefficient type.
* @param pfac polynomial ring factory.
* @param L list of polynomials to be represented.
* @return Product represenation of L in the polynomial ring pfac.
*/
public static > List>>> toProductRes(
GenPolynomialRing>> pfac, List>> L) {
List>>> list = new ArrayList>>>();
if (L == null || L.size() == 0) {
return list;
}
GenPolynomial>> b;
for (GenPolynomial> a : L) {
b = toProductRes(pfac, a);
list.add(b);
}
return list;
}
/**
* Product representation.
* @param coefficient type.
* @param pfac polynomial ring factory.
* @param A polynomial to be represented.
* @return Product represenation of A in the polynomial ring pfac.
*/
public static > GenPolynomial>> toProductRes(
GenPolynomialRing>> pfac, GenPolynomial> A) {
GenPolynomial>> P = pfac.getZERO().copy();
if (A == null || A.isZERO()) {
return P;
}
RingFactory>> rpfac = pfac.coFac;
ProductRing> fac = (ProductRing>) rpfac;
Product> p;
for (Map.Entry> y : A.getMap().entrySet()) {
ExpVector e = y.getKey();
GenPolynomial a = y.getValue();
p = toProductRes(fac, a);
if (!p.isZERO()) {
P.doPutToMap(e, p);
}
}
return P;
}
/**
* Product representation.
* @param coefficient type.
* @param pfac product ring factory.
* @param c coefficient to be represented.
* @return Product represenation of c in the ring pfac.
*/
public static > Product> toProductRes(ProductRing> pfac,
GenPolynomial c) {
SortedMap> elem = new TreeMap>();
for (int i = 0; i < pfac.length(); i++) {
RingFactory> rfac = pfac.getFactory(i);
ResidueRing fac = (ResidueRing) rfac;
Residue u = new Residue(fac, c);
//fac.fromInteger( c.getVal() );
if (!u.isZERO()) {
elem.put(i, u);
}
}
return new Product>(pfac, elem);
}
/**
* Product residue representation.
* @param coefficient type.
* @param CS list of ColoredSystems from comprehensive GB system.
* @return Product residue represenation of CS.
*/
public static > List>>> toProductRes(
List> CS) {
List>>> list = new ArrayList>>>();
if (CS == null || CS.size() == 0) {
return list;
}
GenPolynomialRing> pr = null;
List>> rrl = new ArrayList>>(CS.size());
for (ColoredSystem cs : CS) {
Ideal id = cs.condition.zero;
ResidueRing r = new ResidueRing(id);
if (!rrl.contains(r)) {
rrl.add(r);
}
if (pr == null) {
if (cs.list.size() > 0) {
pr = cs.list.get(0).green.ring;
}
}
}
ProductRing> pfac;
pfac = new ProductRing>(rrl);
//System.out.println("pfac = " + pfac);
GenPolynomialRing>> rf = new GenPolynomialRing>>(pfac, pr.nvar,
pr.tord, pr.getVars());
GroebnerSystem gs = new GroebnerSystem(CS);
List>> F = gs.getCGB();
list = PolyUtilApp. toProductRes(rf, F);
return list;
}
/**
* Residue coefficient representation.
* @param pfac polynomial ring factory.
* @param L list of polynomials to be represented.
* @return Represenation of L in the polynomial ring pfac.
*/
public static > List>> toResidue(
GenPolynomialRing> pfac, List>> L) {
List>> list = new ArrayList>>();
if (L == null || L.size() == 0) {
return list;
}
GenPolynomial> b;
for (GenPolynomial> a : L) {
b = toResidue(pfac, a);
if (!b.isZERO()) {
list.add(b);
}
}
return list;
}
/**
* Residue coefficient representation.
* @param pfac polynomial ring factory.
* @param A polynomial to be represented.
* @return Represenation of A in the polynomial ring pfac.
*/
public static > GenPolynomial> toResidue(
GenPolynomialRing> pfac, GenPolynomial> A) {
GenPolynomial> P = pfac.getZERO().copy();
if (A == null || A.isZERO()) {
return P;
}
RingFactory> rpfac = pfac.coFac;
ResidueRing fac = (ResidueRing) rpfac;
Residue p;
for (Map.Entry> y : A.getMap().entrySet()) {
ExpVector e = y.getKey();
GenPolynomial a = y.getValue();
p = new Residue(fac, a);
if (!p.isZERO()) {
P.doPutToMap(e, p);
}
}
return P;
}
/**
* Product slice.
* @param coefficient type.
* @param L list of polynomials with product coefficients.
* @return Slices represenation of L.
*/
public static > Map, PolynomialList>> productSlice(
PolynomialList>> L) {
Map, PolynomialList>> map;
RingFactory>> fpr = L.ring.coFac;
ProductRing> pr = (ProductRing>) fpr;
int s = pr.length();
map = new TreeMap, PolynomialList>>();
List>> slist;
List>>> plist = L.list;
PolynomialList> spl;
for (int i = 0; i < s; i++) {
RingFactory> r = pr.getFactory(i);
ResidueRing rr = (ResidueRing) r;
Ideal id = rr.ideal;
GenPolynomialRing cof = rr.ring;
GenPolynomialRing> pfc;
pfc = new GenPolynomialRing>(cof, L.ring);
slist = fromProduct(pfc, plist, i);
spl = new PolynomialList>(pfc, slist);
PolynomialList> d = map.get(id);
if (d != null) {
throw new RuntimeException("ideal exists twice " + id);
}
map.put(id, spl);
}
return map;
}
/**
* Product slice at i.
* @param coefficient type.
* @param L list of polynomials with product coeffients.
* @param i index of slice.
* @return Slice of of L at i.
*/
public static > PolynomialList> productSlice(
PolynomialList>> L, int i) {
RingFactory>> fpr = L.ring.coFac;
ProductRing> pr = (ProductRing>) fpr;
List>> slist;
List>>> plist = L.list;
PolynomialList> spl;
RingFactory> r = pr.getFactory(i);
ResidueRing rr = (ResidueRing) r;
GenPolynomialRing cof = rr.ring;
GenPolynomialRing> pfc;
pfc = new GenPolynomialRing>(cof, L.ring);
slist = fromProduct(pfc, plist, i);
spl = new PolynomialList>(pfc, slist);
return spl;
}
/**
* From product representation.
* @param coefficient type.
* @param pfac polynomial ring factory.
* @param L list of polynomials to be converted from product representation.
* @param i index of product representation to be taken.
* @return Represenation of i-slice of L in the polynomial ring pfac.
*/
public static > List>> fromProduct(
GenPolynomialRing> pfac, List>>> L,
int i) {
List>> list = new ArrayList>>();
if (L == null || L.size() == 0) {
return list;
}
GenPolynomial> b;
for (GenPolynomial>> a : L) {
b = fromProduct(pfac, a, i);
if (!b.isZERO()) {
b = b.abs();
if (!list.contains(b)) {
list.add(b);
}
}
}
return list;
}
/**
* From product representation.
* @param coefficient type.
* @param pfac polynomial ring factory.
* @param P polynomial to be converted from product representation.
* @param i index of product representation to be taken.
* @return Represenation of i-slice of P in the polynomial ring pfac.
*/
public static > GenPolynomial> fromProduct(
GenPolynomialRing> pfac, GenPolynomial>> P, int i) {
GenPolynomial> b = pfac.getZERO().copy();
if (P == null || P.isZERO()) {
return b;
}
for (Map.Entry>> y : P.getMap().entrySet()) {
ExpVector e = y.getKey();
Product> a = y.getValue();
Residue r = a.get(i);
if (r != null && !r.isZERO()) {
GenPolynomial p = r.val;
if (!p.isZERO()) {
b.doPutToMap(e, p);
}
}
}
return b;
}
/**
* Product slice to String.
* @param coefficient type.
* @param L list of polynomials with to be represented.
* @return Product represenation of L in the polynomial ring pfac.
*/
public static > String productSliceToString(
Map, PolynomialList>> L) {
Set>> sl = new TreeSet>>();
PolynomialList> pl = null;
StringBuffer sb = new StringBuffer(); //"\nproductSlice ----------------- begin");
for (Map.Entry, PolynomialList>> en : L.entrySet()) {
sb.append("\n\ncondition == 0:\n");
sb.append(en.getKey().list.toScript());
pl = en.getValue(); //L.get(id);
sl.addAll(pl.list);
sb.append("\ncorresponding ideal:\n");
sb.append(pl.toScript());
}
//List>> sll
// = new ArrayList>>( sl );
//pl = new PolynomialList>(pl.ring,sll);
// sb.append("\nunion = " + pl.toString());
//sb.append("\nproductSlice ------------------------- end\n");
return sb.toString();
}
/**
* Product slice to String.
* @param coefficient type.
* @param L list of polynomials with product coefficients.
* @return string represenation of slices of L.
*/
public static > String productToString(PolynomialList>> L) {
Map, PolynomialList>> M;
M = productSlice(L);
String s = productSliceToString(M);
return s;
}
/**
* Construct superset of complex roots for zero dimensional ideal(G).
* @param I zero dimensional ideal.
* @param eps desired precision.
* @return list of coordinates of complex roots for ideal(G)
*/
public static & Rational> List>> complexRootTuples(
Ideal I, D eps) {
List> univs = I.constructUnivariate();
if (logger.isInfoEnabled()) {
logger.info("univs = " + univs);
}
return complexRoots(I, univs, eps);
}
/**
* Construct superset of complex roots for zero dimensional ideal(G).
* @param I zero dimensional ideal.
* @param univs list of univariate polynomials.
* @param eps desired precision.
* @return list of coordinates of complex roots for ideal(G)
*/
public static & Rational> List>> complexRoots(
Ideal I, List> univs, D eps) {
List>> croots = new ArrayList>>();
RingFactory cf = I.list.ring.coFac;
ComplexRing cr = new ComplexRing(cf);
ComplexRootsAbstract cra = new ComplexRootsSturm(cr);
List>> cunivs = new ArrayList>>();
for (GenPolynomial p : univs) {
GenPolynomialRing> pfac = new GenPolynomialRing>(cr, p.ring);
//System.out.println("pfac = " + pfac.toScript());
GenPolynomial> cp = PolyUtil. toComplex(pfac, p);
cunivs.add(cp);
//System.out.println("cp = " + cp);
}
for (int i = 0; i < I.list.ring.nvar; i++) {
List> cri = cra.approximateRoots(cunivs.get(i), eps);
//System.out.println("cri = " + cri);
croots.add(cri);
}
croots = ListUtil.> tupleFromList(croots);
return croots;
}
/**
* Construct superset of complex roots for zero dimensional ideal(G).
* @param Il list of zero dimensional ideals with univariate polynomials.
* @param eps desired precision.
* @return list of coordinates of complex roots for ideal(cap_i(G_i))
*/
public static & Rational> List>> complexRootTuples(
List> Il, D eps) {
List>> croots = new ArrayList>>();
for (IdealWithUniv I : Il) {
List>> cr = complexRoots(I.ideal, I.upolys, eps);
croots.addAll(cr);
}
return croots;
}
/**
* Construct superset of complex roots for zero dimensional ideal(G).
* @param Il list of zero dimensional ideals with univariate polynomials.
* @param eps desired precision.
* @return list of ideals with coordinates of complex roots for
* ideal(cap_i(G_i))
*/
public static & Rational> List> complexRoots(
List> Il, D eps) {
List> Ic = new ArrayList>(Il.size());
for (IdealWithUniv I : Il) {
List>> cr = complexRoots(I.ideal, I.upolys, eps);
IdealWithComplexRoots ic = new IdealWithComplexRoots(I, cr);
Ic.add(ic);
}
return Ic;
}
/**
* Construct superset of complex roots for zero dimensional ideal(G).
* @param G list of polynomials of a of zero dimensional ideal.
* @param eps desired precision.
* @return list of ideals with coordinates of complex roots for ideal(G)
*/
public static & Rational> List> complexRoots(
Ideal G, D eps) {
List> Il = G.zeroDimDecomposition();
return complexRoots(Il, eps);
}
/**
* Construct superset of real roots for zero dimensional ideal(G).
* @param I zero dimensional ideal.
* @param eps desired precision.
* @return list of coordinates of real roots for ideal(G)
*/
public static & Rational> List> realRootTuples(Ideal I,
D eps) {
List> univs = I.constructUnivariate();
if (logger.isInfoEnabled()) {
logger.info("univs = " + univs);
}
return realRoots(I, univs, eps);
}
/**
* Construct superset of real roots for zero dimensional ideal(G).
* @param I zero dimensional ideal.
* @param univs list of univariate polynomials.
* @param eps desired precision.
* @return list of coordinates of real roots for ideal(G)
*/
public static & Rational> List> realRoots(Ideal I,
List> univs, D eps) {
List> roots = new ArrayList>();
//RingFactory cf = (RingFactory) I.list.ring.coFac;
RealRootsAbstract rra = new RealRootsSturm();
for (int i = 0; i < I.list.ring.nvar; i++) {
List rri = rra.approximateRoots(univs.get(i), eps);
//System.out.println("rri = " + rri);
roots.add(rri);
}
//System.out.println("roots-1 = " + roots);
roots = ListUtil. tupleFromList(roots);
//System.out.println("roots-2 = " + roots);
return roots;
}
/**
* Construct superset of real roots for zero dimensional ideal(G).
* @param Il list of zero dimensional ideals with univariate polynomials.
* @param eps desired precision.
* @return list of coordinates of real roots for ideal(cap_i(G_i))
*/
public static & Rational> List> realRootTuples(
List> Il, D eps) {
List> rroots = new ArrayList>();
for (IdealWithUniv I : Il) {
List> rr = realRoots(I.ideal, I.upolys, eps);
rroots.addAll(rr);
}
return rroots;
}
/**
* Construct superset of real roots for zero dimensional ideal(G).
* @param Il list of zero dimensional ideals with univariate polynomials.
* @param eps desired precision.
* @return list of ideals with coordinates of real roots for
* ideal(cap_i(G_i))
*/
public static & Rational> List> realRoots(
List> Il, D eps) {
List> Ir = new ArrayList>(Il.size());
for (IdealWithUniv I : Il) {
List> rr = realRoots(I.ideal, I.upolys, eps);
IdealWithRealRoots ir = new IdealWithRealRoots(I, rr);
Ir.add(ir);
}
return Ir;
}
/**
* Construct superset of real roots for zero dimensional ideal(G).
* @param G list of polynomials of a of zero dimensional ideal.
* @param eps desired precision.
* @return list of ideals with coordinates of real roots for ideal(G)
*/
public static & Rational> List> realRoots(Ideal G,
D eps) {
List> Il = G.zeroDimDecomposition();
return realRoots(Il, eps);
}
/**
* Test for real roots of zero dimensional ideal(L).
* @param L list of polynomials.
* @param roots list of real roots for ideal(G).
* @param eps desired precision.
* @return true if root is a list of coordinates of real roots for ideal(L)
*/
public static boolean isRealRoots(List> L, List> roots,
BigDecimal eps) {
if (L == null || L.size() == 0) {
return true;
}
// polynomials with decimal coefficients
BigDecimal dc = BigDecimal.ONE;
GenPolynomialRing dfac = L.get(0).ring;
//System.out.println("dfac = " + dfac);
for (GenPolynomial dp : L) {
//System.out.println("dp = " + dp);
for (List r : roots) {
//System.out.println("r = " + r);
BigDecimal ev = PolyUtil. evaluateAll(dc, dfac, dp, r);
if (ev.abs().compareTo(eps) > 0) {
System.out.println("ev = " + ev);
return false;
}
}
}
return true;
}
/**
* Test for complex roots of zero dimensional ideal(L).
* @param L list of polynomials.
* @param roots list of real roots for ideal(G).
* @param eps desired precision.
* @return true if root is a list of coordinates of complex roots for
* ideal(L)
*/
public static boolean isComplexRoots(List>> L,
List>> roots, BigDecimal eps) {
if (L == null || L.size() == 0) {
return true;
}
// polynomials with decimal coefficients
BigDecimal dc = BigDecimal.ONE;
ComplexRing dcc = new ComplexRing(dc);
GenPolynomialRing> dfac = L.get(0).ring;
//System.out.println("dfac = " + dfac);
for (GenPolynomial> dp : L) {
//System.out.println("dp = " + dp);
for (List> r : roots) {
//System.out.println("r = " + r);
Complex ev = PolyUtil.> evaluateAll(dcc, dfac, dp, r);
if (ev.norm().getRe().compareTo(eps) > 0) {
System.out.println("ev = " + ev);
return false;
}
}
}
return true;
}
/**
* Construct real roots for zero dimensional ideal(G).
* @param I zero dimensional ideal with univariate irreducible polynomials
* and bi-variate polynomials.
* @return real algebraic roots for ideal(G)
*/
public static & Rational> IdealWithRealAlgebraicRoots realAlgebraicRoots(
IdealWithUniv I) {
List