/*
* $Id$
*/
package edu.jas.application;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import edu.jas.gbufd.MultiplicativeSet;
import edu.jas.gbufd.MultiplicativeSetSquarefree;
import edu.jas.poly.ExpVector;
import edu.jas.poly.GenPolynomial;
import edu.jas.poly.GenPolynomialRing;
import edu.jas.structure.GcdRingElem;
/**
* Condition. An ideal of polynomials considered to be zero and a multiplicative
* set of polynomials considered to be non-zero.
* @param coefficient type
* @author Heinz Kredel.
*/
public class Condition> implements Serializable {
private static final Logger logger = Logger.getLogger(Condition.class);
//private final boolean debug = logger.isDebugEnabled();
/**
* Colors.
*/
public static enum Color {
GREEN, RED, WHITE
};
/**
* Data structure for condition zero.
*/
public final Ideal zero;
/**
* Data structure for condition non-zero.
*/
public final MultiplicativeSet nonZero;
/**
* Condition constructor. Constructs an empty condition with squarefree
* multiplicative set.
* @param ring polynomial ring factory for coefficients.
*/
public Condition(GenPolynomialRing ring) {
this(new Ideal(ring), new MultiplicativeSetSquarefree(ring));
//this(new Ideal(ring),new MultiplicativeSetFactors(ring));
if (ring == null) {
throw new IllegalArgumentException("only for non null rings");
}
}
/**
* Condition constructor. Constructs a condition with squarefree
* multiplicative set.
* @param z an ideal of zero polynomials.
*/
public Condition(Ideal z) {
this(z, new MultiplicativeSetSquarefree(z.getRing()));
//this(z,new MultiplicativeSetFactors(z.list.ring));
}
/**
* Condition constructor.
* @param nz a list of non-zero polynomials.
*/
public Condition(MultiplicativeSet nz) {
this(new Ideal(nz.ring), nz);
}
/**
* Condition constructor.
* @param z an ideal of zero polynomials.
* @param nz a list of non-zero polynomials.
*/
public Condition(Ideal z, MultiplicativeSet nz) {
if (z == null || nz == null) {
throw new IllegalArgumentException("only for non null condition parts");
}
zero = z;
nonZero = nz;
}
/**
* toString.
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Condition[ 0 == " + zero.getList().toString() + ", 0 != " + nonZero.mset.toString() + " ]";
}
/**
* equals.
* @param ob an Object.
* @return true if this is equal to o, else false.
*/
@Override
@SuppressWarnings("unchecked")
public boolean equals(Object ob) {
Condition c = null;
try {
c = (Condition) ob;
} catch (ClassCastException e) {
return false;
}
if (c == null) {
return false;
}
if (!zero.equals(c.zero)) {
return false;
}
if (!nonZero.equals(c.nonZero)) {
return false;
}
// better:
//if ( nonZero.removeFactors(c.nonZero).size() != 0 ) {
// return false;
//}
//if ( c.nonZero.removeFactors(nonZero).size() != 0 ) {
// return false;
//}
return true;
}
/**
* Hash code for this condition.
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
int h;
h = zero.getList().hashCode();
h = h << 17;
h += nonZero.hashCode();
// h = h << 11;
// h += pairlist.hashCode();
return h;
}
/**
* Is empty condition.
* @return true if this is the empty condition, else false.
*/
public boolean isEmpty() {
return (zero.isZERO() && nonZero.isEmpty());
}
/**
* Is contradictory.
* @return true if this condition is contradictory, else false.
*/
public boolean isContradictory() {
if (zero.isZERO()) {
return false;
}
boolean t = zero.isONE();
if (t) {
logger.info("contradiction zero.isONE(): " + this);
return t;
}
for (GenPolynomial p : zero.getList()) {
t = nonZero.contains(p);
if (t) {
logger.info("contradiction nonZero.contains(zero): " + this + ", pol = " + p);
return t;
}
}
for (GenPolynomial p : nonZero.mset) {
t = zero.contains(p);
if (t) {
logger.info("contradiction zero.contains(nonZero): " + this + ", pol = " + p);
return t;
}
}
return false;
}
/**
* Extend condition with zero polynomial.
* @param z a polynomial to be treated as zero.
* @return new condition.
*/
public Condition extendZero(GenPolynomial z) {
// assert color(z) == white
z = z.monic();
Ideal idz = zero.sum(z);
logger.info("added to ideal: " + z);
Condition nc = new Condition(idz, nonZero);
//if (true) {
return nc.simplify();
//}
//return nc;
}
/**
* Extend condition with non-zero polynomial.
* @param nz a polynomial to be treated as non-zero.
* @return new condition.
*/
public Condition extendNonZero(GenPolynomial nz) {
// assert color(nz) == white
GenPolynomial n = zero.normalform(nz).monic();
if (n == null || n.isZERO()) {
return this;
}
MultiplicativeSet ms = nonZero.add(n);
logger.info("added to non zero: " + n);
Condition nc = new Condition(zero, ms);
//if (true) {
return nc.simplify();
//}
//return nc;
}
/**
* Simplify zero and non-zero polynomial conditions.
* @return new simplified condition.
*/
public Condition simplify() {
//if (false) { // no simplification
// return this;
//}
Ideal idz = zero.squarefree();
if (!idz.getList().equals(zero.getList())) {
logger.info("simplify squarefree: " + zero.getList() + " to " + idz.getList());
}
List> ml = idz.normalform(nonZero.mset);
MultiplicativeSet ms = nonZero;
if (!ml.equals(nonZero.mset)) {
if (ml.size() != nonZero.mset.size()) {
logger.info("contradiction(==0):");
logger.info("simplify normalform contradiction: " + nonZero.mset + " to " + ml);
return null;
}
logger.info("simplify normalform: " + nonZero.mset + " to " + ml);
ms = nonZero.replace(ml);
}
List> Z = ms.removeFactors(idz.getList());
if (!Z.equals(idz.getList())) {
if (Z.size() != idz.getList().size()) { // never true
logger.info("contradiction(!=0):");
logger.info("simplify removeFactors contradiction: " + idz.getList() + " to " + Z);
return null;
}
logger.info("simplify removeFactors: " + idz.getList() + " to " + Z);
idz = new Ideal(zero.getRing(), Z); // changes ideal
}
Condition nc = new Condition(idz, ms);
if (nc.isContradictory()) { // evenatually a factor became 1
//logger.info("simplify contradiction: " + nc);
return null;
}
if (idz.equals(zero) && ms.equals(nonZero)) {
return this;
}
logger.info("condition simplified: " + this + " to " + nc);
//if (false) { // no further simplification
// return nc;
//}
return nc.simplify();
}
/**
* Determine color of polynomial.
* @param c polynomial to be colored.
* @return color of c.
*/
public Color color(GenPolynomial c) {
GenPolynomial m = zero.normalform(c).monic();
//non-sense: m = nonZero.removeFactors(m);
if (m.isZERO()) { // zero.contains(m)
// System.out.println("m in id = " + m);
return Color.GREEN;
}
if (m.isConstant()) {
// System.out.println("m constant " + m);
return Color.RED;
}
if (nonZero.contains(m) || nonZero.contains(c)) {
// System.out.println("m or c in nonzero " + m);
return Color.RED;
}
//System.out.println("m white " + m + ", c = " + c);
return Color.WHITE;
}
/**
* Determine polynomial. If this condition does not determine the
* polynomial, then a run-time exception is thrown.
* @param A polynomial.
* @return new determined colored polynomial.
*/
public ColorPolynomial determine(GenPolynomial> A) {
ColorPolynomial cp = null;
if (A == null) {
return cp;
}
GenPolynomial> zero = A.ring.getZERO();
GenPolynomial> green = zero;
GenPolynomial> red = zero;
GenPolynomial> white = zero;
if (A.isZERO()) {
cp = new ColorPolynomial(green, red, white);
return cp;
}
GenPolynomial> Ap = A;
GenPolynomial> Bp;
while (!Ap.isZERO()) {
Map.Entry> m = Ap.leadingMonomial();
ExpVector e = m.getKey();
GenPolynomial c = m.getValue();
Bp = Ap.reductum();
// System.out.println( "color(" + c + ") = " + color(c) );
switch (color(c)) {
case GREEN:
green = green.sum(c, e);
Ap = Bp;
continue;
case RED:
red = red.sum(c, e);
white = Bp;
return new ColorPolynomial(green, red, white);
// since break is not possible
default:
System.out.println("undetermined cond = " + this);
System.out.println("undetermined poly A = " + A);
System.out.println("undetermined poly green = " + green);
System.out.println("undetermined poly red = " + red);
System.out.println("undetermined poly Ap = " + Ap);
System.out.println("undetermined coeff c = " + c);
throw new RuntimeException("undetermined, c is white = " + c);
// is catched in minimalGB
}
}
cp = new ColorPolynomial(green, red, white);
// System.out.println("determined = " + cp);
return cp;
}
/**
* Determine list of polynomials. If this condition does not determine all
* polynomials, then a run-time exception is thrown. The returned list does
* not contain polynomials with all green terms.
* @param L list of polynomial.
* @return new determined list of colored polynomials.
*/
public List> determine(List>> L) {
List> cl = null;
if (L == null) {
return cl;
}
cl = new ArrayList>(L.size());
for (GenPolynomial> A : L) {
ColorPolynomial c = determine(A);
if (c != null && !c.isZERO()) {
cl.add(c);
}
}
return cl;
}
/**
* Re determine colored polynomial.
* @param s colored polynomial.
* @return determined colored polynomial wrt. this.conditions.
*/
public ColorPolynomial reDetermine(ColorPolynomial s) {
ColorPolynomial p = determine(s.getEssentialPolynomial());
// assume green terms stay green wrt. this condition
GenPolynomial> g = s.green.sum(p.green);
p = new ColorPolynomial(g, p.red, p.white);
return p;
}
/**
* Re determine list of colored polynomials.
* @param S list of colored polynomials.
* @return list of determined colored polynomials wrt. this.conditions.
*/
public List> reDetermine(List> S) {
if (S == null || S.isEmpty()) {
return S;
}
List> P = new ArrayList>();
for (ColorPolynomial s : S) {
ColorPolynomial p = reDetermine(s);
P.add(p);
}
return P;
}
/**
* Is determined colored polynomial.
* @param s colored polynomial.
* @return true if the colored polynomial is correctly determined wrt.
* this.condition.
*/
public boolean isDetermined(ColorPolynomial s) {
ColorPolynomial p = determine(s.getPolynomial());
boolean t = p.equals(s);
if (!t) {
System.out.println("not determined s = " + s);
System.out.println("not determined p = " + p);
System.out.println("not determined cond = " + this);
}
return t;
}
/**
* Is determined list of colored polynomial.
* @param S list of colored polynomials.
* @return true if the colored polynomials in S are correctly determined
* wrt. this.condition.
*/
public boolean isDetermined(List> S) {
if (S == null) {
return true;
}
for (ColorPolynomial p : S) {
if (!isDetermined(p)) {
return false;
}
}
return true;
}
}
Ads help maintain this website.