/*
* $Id$
*/
package edu.jas.poly;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import org.apache.log4j.Logger;
import edu.jas.structure.RingElem;
/**
* RecSolvablePolynomial generic recursive solvable polynomials implementing
* RingElem. n-variate ordered solvable polynomials over solvable polynomial
* coefficients. Objects of this class are intended to be immutable. The
* implementation is based on TreeMap respectively SortedMap from exponents to
* coefficients by extension of GenPolynomial.
* @param coefficient type
* @author Heinz Kredel
*/
public class RecSolvablePolynomial> extends GenSolvablePolynomial> {
/**
* The factory for the recursive solvable polynomial ring. Hides super.ring.
*/
public final RecSolvablePolynomialRing ring;
private static final Logger logger = Logger.getLogger(RecSolvablePolynomial.class);
private final boolean debug = logger.isDebugEnabled();
/**
* Constructor for zero RecSolvablePolynomial.
* @param r solvable polynomial ring factory.
*/
public RecSolvablePolynomial(RecSolvablePolynomialRing r) {
super(r);
ring = r;
}
/**
* Constructor for RecSolvablePolynomial.
* @param r solvable polynomial ring factory.
* @param e exponent.
*/
public RecSolvablePolynomial(RecSolvablePolynomialRing r, ExpVector e) {
this(r);
val.put(e, ring.getONECoefficient());
}
/**
* Constructor for RecSolvablePolynomial.
* @param r solvable polynomial ring factory.
* @param c coefficient polynomial.
* @param e exponent.
*/
public RecSolvablePolynomial(RecSolvablePolynomialRing r, GenPolynomial c, ExpVector e) {
this(r);
if (c != null && !c.isZERO()) {
val.put(e, c);
}
}
/**
* Constructor for RecSolvablePolynomial.
* @param r solvable polynomial ring factory.
* @param c coefficient polynomial.
*/
public RecSolvablePolynomial(RecSolvablePolynomialRing r, GenPolynomial c) {
this(r, c, r.evzero);
}
/**
* Constructor for RecSolvablePolynomial.
* @param r solvable polynomial ring factory.
* @param S solvable polynomial.
*/
public RecSolvablePolynomial(RecSolvablePolynomialRing r, GenSolvablePolynomial> S) {
this(r, S.val);
}
/**
* Constructor for RecSolvablePolynomial.
* @param r solvable polynomial ring factory.
* @param v the SortedMap of some other (solvable) polynomial.
*/
protected RecSolvablePolynomial(RecSolvablePolynomialRing r, SortedMap> v) {
this(r);
val.putAll(v); // assume no zero coefficients
}
/**
* Get the corresponding element factory.
* @return factory for this Element.
* @see edu.jas.structure.Element#factory()
*/
@Override
public RecSolvablePolynomialRing factory() {
return ring;
}
/**
* Clone this RecSolvablePolynomial.
* @see java.lang.Object#clone()
*/
@Override
public RecSolvablePolynomial copy() {
return new RecSolvablePolynomial(ring, this.val);
}
/**
* Comparison with any other object.
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object B) {
if (!(B instanceof RecSolvablePolynomial)) {
return false;
}
return super.equals(B);
}
/**
* RecSolvablePolynomial multiplication.
* @param Bp RecSolvablePolynomial.
* @return this*Bp, where * denotes solvable multiplication.
*/
// not @Override
public RecSolvablePolynomial multiply(RecSolvablePolynomial Bp) {
if (Bp == null || Bp.isZERO()) {
return ring.getZERO();
}
if (this.isZERO()) {
return this;
}
assert (ring.nvar == Bp.ring.nvar);
if (debug) {
logger.info("ring = " + ring.toScript());
}
ExpVector Z = ring.evzero;
GenPolynomialRing cfac = (GenPolynomialRing) ring.coFac;
ExpVector Zc = cfac.evzero;
RecSolvablePolynomial Dp = ring.getZERO().copy();
RecSolvablePolynomial zero = ring.getZERO().copy();
GenPolynomial one = ring.getONECoefficient();
RecSolvablePolynomial C1 = null;
RecSolvablePolynomial C2 = null;
Map> A = val;
Map> B = Bp.val;
Set>> Bk = B.entrySet();
if (debug)
logger.info("input A = " + this);
for (Map.Entry> y : A.entrySet()) {
GenPolynomial a = y.getValue();
ExpVector e = y.getKey();
if (debug)
logger.info("e = " + e + ", a = " + a);
int[] ep = e.dependencyOnVariables();
int el1 = ring.nvar + 1;
if (ep.length > 0) {
el1 = ep[0];
}
//int el1s = ring.nvar + 1 - el1;
if (debug)
logger.info("input B = " + Bp);
for (Map.Entry> x : Bk) {
GenPolynomial b = x.getValue();
ExpVector f = x.getKey();
if (debug)
logger.info("f = " + f + ", b = " + b);
int[] fp = f.dependencyOnVariables();
int fl1 = 0;
if (fp.length > 0) {
fl1 = fp[fp.length - 1];
}
int fl1s = ring.nvar + 1 - fl1;
// polynomial coefficient multiplication e*b = P_eb, for a*((e*b)*f)
RecSolvablePolynomial Cps = ring.getZERO().copy();
RecSolvablePolynomial Cs = null;
if (ring.coeffTable.isEmpty() || b.isConstant() || e.isZERO()) { // symmetric
Cps = new RecSolvablePolynomial(ring, b, e);
if (debug)
logger.info("symmetric coeff, e*b: b = " + b + ", e = " + e);
} else { // unsymmetric
if (debug)
logger.info("unsymmetric coeff, e*b: b = " + b + ", e = " + e);
for (Map.Entry z : b.val.entrySet()) {
C c = z.getValue();
GenPolynomial cc = b.ring.getONE().multiply(c);
ExpVector g = z.getKey();
if (debug)
logger.info("g = " + g + ", c = " + c);
int[] gp = g.dependencyOnVariables();
int gl1 = 0;
if (gp.length > 0) {
gl1 = gp[gp.length - 1];
}
int gl1s = b.ring.nvar + 1 - gl1;
if (debug) {
logger.info("gl1s = " + gl1s);
}
// split e = e1 * e2, g = g1 * g2
ExpVector e1 = e;
ExpVector e2 = Z;
if (!e.isZERO()) {
e1 = e.subst(el1, 0);
e2 = Z.subst(el1, e.getVal(el1));
}
ExpVector e4;
ExpVector g1 = g;
ExpVector g2 = Zc;
if (!g.isZERO()) {
g1 = g.subst(gl1, 0);
g2 = Zc.subst(gl1, g.getVal(gl1));
}
if (debug) {
logger.info("coeff, e1 = " + e1 + ", e2 = " + e2 + ", Cps = " + Cps);
logger.info("coeff, g1 = " + g1 + ", g2 = " + g2);
}
TableRelation> crel = ring.coeffTable.lookup(e2, g2);
if (debug)
logger.info("coeff, crel = " + crel.p);
//logger.info("coeff, e = " + e + " g, = " + g + ", crel = " + crel);
Cs = new RecSolvablePolynomial(ring, crel.p);
// rest of multiplication and update relations
if (crel.f != null) {
GenPolynomial c2 = b.ring.getONE().multiply(crel.f);
C2 = new RecSolvablePolynomial(ring, c2, Z);
Cs = Cs.multiply(C2);
if (crel.e == null) {
e4 = e2;
} else {
e4 = e2.subtract(crel.e);
}
ring.coeffTable.update(e4, g2, Cs);
}
if (crel.e != null) { // process left part
C1 = new RecSolvablePolynomial(ring, one, crel.e);
Cs = C1.multiply(Cs);
ring.coeffTable.update(e2, g2, Cs);
}
if (!g1.isZERO()) { // process right part
GenPolynomial c2 = b.ring.getONE().multiply(g1);
C2 = new RecSolvablePolynomial(ring, c2, Z);
Cs = Cs.multiply(C2);
}
if (!e1.isZERO()) { // process left part
C1 = new RecSolvablePolynomial(ring, one, e1);
Cs = C1.multiply(Cs);
}
//System.out.println("e1*Cs*g1 = " + Cs);
Cs = Cs.multiplyLeft(cc); // assume c, coeff(cc) commutes with Cs
Cps = (RecSolvablePolynomial) Cps.sum(Cs);
} // end b loop
if (debug)
logger.info("coeff, Cs = " + Cs + ", Cps = " + Cps);
}
if (debug)
logger.info("coeff-poly: Cps = " + Cps);
// polynomial multiplication P_eb*f, for a*(P_eb*f)
RecSolvablePolynomial Dps = ring.getZERO().copy();
RecSolvablePolynomial Ds = null;
RecSolvablePolynomial D1, D2;
if (ring.table.isEmpty() || Cps.isConstant() || f.isZERO()) { // symmetric
if (debug)
logger.info("symmetric poly, P_eb*f: Cps = " + Cps + ", f = " + f);
ExpVector g = e.sum(f);
if (Cps.isConstant()) {
Ds = new RecSolvablePolynomial(ring, Cps.leadingBaseCoefficient(), g); // symmetric!
} else {
Ds = shift(Cps, f); // symmetric
}
} else { // eventually unsymmetric
if (debug)
logger.info("unsymmetric poly, P_eb*f: Cps = " + Cps + ", f = " + f);
for (Map.Entry> z : Cps.val.entrySet()) {
// split g = g1 * g2, f = f1 * f2
GenPolynomial c = z.getValue();
ExpVector g = z.getKey();
if (debug)
logger.info("g = " + g + ", c = " + c);
int[] gp = g.dependencyOnVariables();
int gl1 = ring.nvar + 1;
if (gp.length > 0) {
gl1 = gp[0];
}
int gl1s = ring.nvar + 1 - gl1;
if (gl1s <= fl1s) { // symmetric
ExpVector h = g.sum(f);
if (debug)
logger.info("disjoint poly: g = " + g + ", f = " + f + ", h = " + h);
Ds = (RecSolvablePolynomial) zero.sum(one, h); // symmetric!
} else {
ExpVector g1 = g.subst(gl1, 0);
ExpVector g2 = Z.subst(gl1, g.getVal(gl1)); // bug el1, gl1
ExpVector g4;
ExpVector f1 = f.subst(fl1, 0);
ExpVector f2 = Z.subst(fl1, f.getVal(fl1));
if (debug) {
logger.info("poly, g1 = " + g1 + ", f1 = " + f1 + ", Dps = " + Dps);
logger.info("poly, g2 = " + g2 + ", f2 = " + f2);
}
TableRelation> rel = ring.table.lookup(g2, f2);
if (debug)
logger.info("poly, g = " + g + ", f = " + f + ", rel = " + rel);
Ds = new RecSolvablePolynomial(ring, rel.p); //ring.copy(rel.p);
if (rel.f != null) {
D2 = new RecSolvablePolynomial(ring, one, rel.f);
Ds = Ds.multiply(D2);
if (rel.e == null) {
g4 = g2;
} else {
g4 = g2.subtract(rel.e);
}
ring.table.update(g4, f2, Ds);
}
if (rel.e != null) {
D1 = new RecSolvablePolynomial(ring, one, rel.e);
Ds = D1.multiply(Ds);
ring.table.update(g2, f2, Ds);
}
if (!f1.isZERO()) {
D2 = new RecSolvablePolynomial(ring, one, f1);
Ds = Ds.multiply(D2);
//ring.table.update(?,f1,Ds)
}
if (!g1.isZERO()) {
D1 = new RecSolvablePolynomial(ring, one, g1);
Ds = D1.multiply(Ds);
//ring.table.update(e1,?,Ds)
}
}
Ds = Ds.multiplyLeft(c); // assume c commutes with Cs
Dps = (RecSolvablePolynomial) Dps.sum(Ds);
} // end Dps loop
Ds = Dps;
}
if (debug) {
logger.info("recursion+: Ds = " + Ds + ", a = " + a);
}
// polynomial coefficient multiplication a*(P_eb*f) = a*Ds
Ds = Ds.multiplyLeft(a); // multiply(a,b); // non-symmetric
if (debug)
logger.info("recursion-: Ds = " + Ds);
Dp = (RecSolvablePolynomial) Dp.sum(Ds);
if (debug)
logger.info("end B loop: Dp = " + Dp);
} // end B loop
if (debug)
logger.info("end A loop: Dp = " + Dp);
} // end A loop
return Dp;
}
/**
* RecSolvablePolynomial left and right multiplication. Product with two
* polynomials.
* @param S RecSolvablePolynomial.
* @param T RecSolvablePolynomial.
* @return S*this*T.
*/
// not @Override
public RecSolvablePolynomial multiply(RecSolvablePolynomial S, RecSolvablePolynomial T) {
if (S.isZERO() || T.isZERO() || this.isZERO()) {
return ring.getZERO();
}
if (S.isONE()) {
return multiply(T);
}
if (T.isONE()) {
return S.multiply(this);
}
return S.multiply(this).multiply(T);
}
/**
* RecSolvablePolynomial multiplication. Product with coefficient ring
* element.
* @param b coefficient polynomial.
* @return this*b, where * is coefficient multiplication.
*/
//todo Override
public RecSolvablePolynomial recMultiply(GenPolynomial b) {
RecSolvablePolynomial Cp = ring.getZERO().copy();
if (b == null || b.isZERO()) {
return Cp;
}
Cp = new RecSolvablePolynomial(ring, b, ring.evzero);
return multiply(Cp);
// wrong:
// Map> Cm = Cp.val; //getMap();
// Map> Am = val;
// for (Map.Entry> y : Am.entrySet()) {
// ExpVector e = y.getKey();
// GenPolynomial a = y.getValue();
// GenPolynomial c = a.multiply(b);
// if (!c.isZERO()) {
// Cm.put(e, c);
// }
// }
// return Cp;
}
/**
* RecSolvablePolynomial left and right multiplication. Product with
* coefficient ring element.
* @param b coefficient polynomial.
* @param c coefficient polynomial.
* @return b*this*c, where * is coefficient multiplication.
*/
@Override
public RecSolvablePolynomial multiply(GenPolynomial b, GenPolynomial c) {
RecSolvablePolynomial Cp = ring.getZERO().copy();
if (b == null || b.isZERO()) {
return Cp;
}
if (c == null || c.isZERO()) {
return Cp;
}
RecSolvablePolynomial Cb = new RecSolvablePolynomial(ring, b, ring.evzero);
RecSolvablePolynomial Cc = new RecSolvablePolynomial(ring, c, ring.evzero);
return Cb.multiply(this).multiply(Cc);
// wrong:
// Map> Cm = Cp.val; //getMap();
// Map> Am = val;
// for (Map.Entry> y : Am.entrySet()) {
// ExpVector e = y.getKey();
// GenPolynomial a = y.getValue();
// GenPolynomial d = b.multiply(a).multiply(c);
// if (!d.isZERO()) {
// Cm.put(e, d);
// }
// }
// return Cp;
}
/*
* RecSolvablePolynomial multiplication. Product with coefficient ring
* element.
* @param b coefficient of coefficient.
* @return this*b, where * is coefficient multiplication.
*/
//@Override not possible
//public RecSolvablePolynomial multiply(C b) { ... }
/**
* RecSolvablePolynomial multiplication. Product with exponent vector.
* @param e exponent.
* @return this * xe, where * denotes solvable multiplication.
*/
@Override
public RecSolvablePolynomial multiply(ExpVector e) {
if (e == null || e.isZERO()) {
return this;
}
GenPolynomial b = ring.getONECoefficient();
return multiply(b, e);
}
/**
* RecSolvablePolynomial left and right multiplication. Product with
* exponent vector.
* @param e exponent.
* @param f exponent.
* @return xe * this * xf, where * denotes solvable
* multiplication.
*/
@Override
public RecSolvablePolynomial multiply(ExpVector e, ExpVector f) {
if (e == null || e.isZERO()) {
return this;
}
if (f == null || f.isZERO()) {
return this;
}
GenPolynomial b = ring.getONECoefficient();
return multiply(b, e, b, f);
}
/**
* RecSolvablePolynomial multiplication. Product with ring element and
* exponent vector.
* @param b coefficient polynomial.
* @param e exponent.
* @return this * b xe, where * denotes solvable multiplication.
*/
@Override
public RecSolvablePolynomial multiply(GenPolynomial b, ExpVector e) {
if (b == null || b.isZERO()) {
return ring.getZERO();
}
RecSolvablePolynomial Cp = new RecSolvablePolynomial(ring, b, e);
return multiply(Cp);
}
/**
* RecSolvablePolynomial left and right multiplication. Product with ring
* element and exponent vector.
* @param b coefficient polynomial.
* @param e exponent.
* @param c coefficient polynomial.
* @param f exponent.
* @return b xe * this * c xf, where * denotes
* solvable multiplication.
*/
@Override
public RecSolvablePolynomial multiply(GenPolynomial b, ExpVector e, GenPolynomial c, ExpVector f) {
if (b == null || b.isZERO()) {
return ring.getZERO();
}
if (c == null || c.isZERO()) {
return ring.getZERO();
}
RecSolvablePolynomial Cp = new RecSolvablePolynomial(ring, b, e);
RecSolvablePolynomial Dp = new RecSolvablePolynomial(ring, c, f);
return multiply(Cp, Dp);
}
/**
* RecSolvablePolynomial multiplication. Left product with ring element and
* exponent vector.
* @param b coefficient polynomial.
* @param e exponent.
* @return b xe * this, where * denotes solvable multiplication.
*/
@Override
public RecSolvablePolynomial multiplyLeft(GenPolynomial b, ExpVector e) {
if (b == null || b.isZERO()) {
return ring.getZERO();
}
RecSolvablePolynomial Cp = new RecSolvablePolynomial(ring, b, e);
return Cp.multiply(this);
}
/**
* RecSolvablePolynomial multiplication. Left product with exponent vector.
* @param e exponent.
* @return xe * this, where * denotes solvable multiplication.
*/
@Override
public RecSolvablePolynomial multiplyLeft(ExpVector e) {
if (e == null || e.isZERO()) {
return this;
}
GenPolynomial b = ring.getONECoefficient();
RecSolvablePolynomial Cp = new RecSolvablePolynomial(ring, b, e);
return Cp.multiply(this);
}
/**
* RecSolvablePolynomial multiplication. Left product with coefficient ring
* element.
* @param b coefficient polynomial.
* @return b*this, where * is coefficient multiplication.
*/
@Override
public RecSolvablePolynomial multiplyLeft(GenPolynomial b) {
RecSolvablePolynomial Cp = ring.getZERO().copy();
if (b == null || b.isZERO()) {
return Cp;
}
GenSolvablePolynomial bb = null;
if (b instanceof GenSolvablePolynomial) {
//throw new RuntimeException("wrong method dispatch in JRE ");
logger.debug("warn: wrong method dispatch in JRE multiply(b) - trying to fix");
bb = (GenSolvablePolynomial) b;
}
Map> Cm = Cp.val; //getMap();
Map> Am = val;
GenPolynomial c;
for (Map.Entry> y : Am.entrySet()) {
ExpVector e = y.getKey();
GenPolynomial a = y.getValue();
if (bb != null) {
GenSolvablePolynomial aa = (GenSolvablePolynomial) a;
c = bb.multiply(aa);
} else {
c = b.multiply(a);
}
if (!c.isZERO()) {
Cm.put(e, c);
}
}
return Cp;
}
/**
* RecSolvablePolynomial multiplication. Left product with 'monomial'.
* @param m 'monomial'.
* @return m * this, where * denotes solvable multiplication.
*/
@Override
public RecSolvablePolynomial multiplyLeft(Map.Entry> m) {
if (m == null) {
return ring.getZERO();
}
return multiplyLeft(m.getValue(), m.getKey());
}
/**
* RecSolvablePolynomial multiplication. Product with 'monomial'.
* @param m 'monomial'.
* @return this * m, where * denotes solvable multiplication.
*/
@Override
public RecSolvablePolynomial multiply(Map.Entry> m) {
if (m == null) {
return ring.getZERO();
}
return multiply(m.getValue(), m.getKey());
}
/**
* RecSolvablePolynomial multiplication.
* Left product with coefficient ring element.
* @param B solvable polynomial.
* @param f exponent vector.
* @return B*f, where * is commutative multiplication.
*/
protected RecSolvablePolynomial shift(RecSolvablePolynomial B, ExpVector f) {
RecSolvablePolynomial C = ring.getZERO().copy();
if (B == null || B.isZERO()) {
return C;
}
if (f == null || f.isZERO()) {
return B;
}
Map> Cm = C.val;
Map> Bm = B.val;
for (Map.Entry> y : Bm.entrySet()) {
ExpVector e = y.getKey();
GenPolynomial a = y.getValue();
ExpVector d = e.sum(f);
if (!a.isZERO()) {
Cm.put(d, a);
}
}
return C;
}
}
Ads help maintain this website.