001/* 002 * $Id: ColoredSystem.java 4125 2012-08-19 19:05:22Z kredel $ 003 */ 004 005package edu.jas.application; 006 007 008import java.util.ArrayList; 009import java.util.HashSet; 010import java.util.List; 011import java.util.Set; 012 013import org.apache.log4j.Logger; 014 015import edu.jas.poly.GenPolynomial; 016import edu.jas.structure.GcdRingElem; 017 018 019/** 020 * Container for a condition, a corresponding colored polynomial list and a 021 * Groebner base pair list. 022 * @param <C> coefficient type 023 */ 024public class ColoredSystem<C extends GcdRingElem<C>> { 025 026 027 private static final Logger logger = Logger.getLogger(ColoredSystem.class); 028 029 030 private final boolean debug = logger.isDebugEnabled(); 031 032 033 /** 034 * Condition determinig this colored system. 035 */ 036 public final Condition<C> condition; 037 038 039 /** 040 * Colored polynomials of this system. 041 */ 042 public final List<ColorPolynomial<C>> list; 043 044 045 /** 046 * Groebner base pair list of this system. 047 */ 048 public final OrderedCPairlist<C> pairlist; 049 050 051 /** 052 * Constructor for a colored polynomial system. 053 * @param cond a condition. 054 * @param S a list of colored polynomials. 055 */ 056 public ColoredSystem(Condition<C> cond, List<ColorPolynomial<C>> S) { 057 this(cond, S, null); 058 } 059 060 061 /** 062 * Constructor for a colored polynomial system. 063 * @param cond a condition. 064 * @param S a list of colored polynomials. 065 * @param pl a ordered pair list. 066 */ 067 public ColoredSystem(Condition<C> cond, List<ColorPolynomial<C>> S, OrderedCPairlist<C> pl) { 068 this.condition = cond; 069 this.list = S; 070 this.pairlist = pl; 071 } 072 073 074 /** 075 * Copy this colored polynomial system. 076 * @return a clone of this. 077 */ 078 public ColoredSystem<C> copy() { 079 return new ColoredSystem<C>(condition, list, pairlist.copy()); 080 } 081 082 083 /** 084 * Add to list of colored systems. This is added to the list of colored 085 * systems, if a system with the same condition is not already contained. 086 * @param L a list of colored systems. 087 * @return L.add(this) if this not in L, else L. 088 */ 089 public List<ColoredSystem<C>> addToList(List<ColoredSystem<C>> L) { 090 List<ColoredSystem<C>> S = new ArrayList<ColoredSystem<C>>(L.size() + 1); 091 boolean contained = false; 092 for (ColoredSystem<C> x : L) { 093 if (condition.equals(x.condition) && list.equals(x.list)) { 094 logger.info("replaced system = " + x.condition); 095 S.add(this); 096 contained = true; 097 } else { // copy existing 098 // System.out.println("kept system = " + x); 099 S.add(x); 100 } 101 } 102 if (!contained) { 103 S.add(this); 104 } 105 return S; 106 } 107 108 109 /** 110 * Get the String representation. 111 * @see java.lang.Object#toString() 112 */ 113 @Override 114 public String toString() { 115 StringBuffer s = new StringBuffer("ColoredSystem: \n"); 116 if (list.size() > 0) { 117 s.append("polynomial ring : " + list.get(0).green.ring + "\n"); 118 } else { 119 s.append("parameter polynomial ring : " + condition.zero.getRing() + "\n"); 120 } 121 s.append("conditions == 0 : " + getConditionZero() + "\n"); 122 s.append("conditions != 0 : " + getConditionNonZero() + "\n"); 123 if (debug) { 124 s.append("green coefficients:\n" + getGreenCoefficients() + "\n"); 125 s.append("red coefficients:\n" + getRedCoefficients() + "\n"); 126 } 127 s.append("colored polynomials:\n" + list + "\n"); 128 s.append("uncolored polynomials:\n" + getPolynomialList() + "\n"); 129 if (debug) { 130 s.append("essential polynomials:\n" + getEssentialPolynomialList() + "\n"); 131 } 132 if (pairlist != null) { 133 s.append(pairlist.toString() + "\n"); 134 } 135 return s.toString(); 136 } 137 138 139 /** 140 * Is this colored system equal to other. 141 * @param c other colored system. 142 * @return true, if this is equal to other, else false. 143 */ 144 @Override 145 @SuppressWarnings("unchecked") 146 public boolean equals(Object c) { 147 ColoredSystem<C> cs = null; 148 try { 149 cs = (ColoredSystem<C>) c; 150 } catch (ClassCastException e) { 151 return false; 152 } 153 if (cs == null) { 154 return false; 155 } 156 boolean t = (condition.equals(cs.condition) && list.equals(cs.list)); 157 if (!t) { 158 return t; 159 } 160 // now t == true 161 t = pairlist.equals(cs.pairlist); 162 if (!t) { 163 System.out.println("pairlists not equal " + pairlist + ", " + cs.pairlist); 164 } 165 return true; // if lists are equal ignore pairlists 166 } 167 168 169 /** 170 * Hash code for this colored system. 171 * @see java.lang.Object#hashCode() 172 */ 173 @Override 174 public int hashCode() { 175 int h; 176 h = condition.hashCode(); 177 h = h << 17; 178 h += list.hashCode(); 179 // h = h << 11; 180 // h += pairlist.hashCode(); 181 return h; 182 } 183 184 185 /** 186 * Get zero condition. 187 * @return condition.zero. 188 */ 189 public List<GenPolynomial<C>> getConditionZero() { 190 return condition.zero.getList(); 191 } 192 193 194 /** 195 * Get non zero condition. 196 * @return condition.nonZero. 197 */ 198 public List<GenPolynomial<C>> getConditionNonZero() { 199 return condition.nonZero.mset; 200 } 201 202 203 /** 204 * Get list of red coefficients of polynomials. 205 * @return list of all red coefficients of polynomials. 206 */ 207 public List<GenPolynomial<C>> getRedCoefficients() { 208 Set<GenPolynomial<C>> F = new HashSet<GenPolynomial<C>>(); 209 for (ColorPolynomial<C> s : list) { 210 F.addAll(s.red.getMap().values()); 211 } 212 List<GenPolynomial<C>> M = new ArrayList<GenPolynomial<C>>(F); 213 return M; 214 } 215 216 217 /** 218 * Get list of green coefficients of polynomials. 219 * @return list of all green coefficients of polynomials. 220 */ 221 public List<GenPolynomial<C>> getGreenCoefficients() { 222 Set<GenPolynomial<C>> F = new HashSet<GenPolynomial<C>>(); 223 for (ColorPolynomial<C> s : list) { 224 F.addAll(s.green.getMap().values()); 225 } 226 List<GenPolynomial<C>> M = new ArrayList<GenPolynomial<C>>(F); 227 return M; 228 } 229 230 231 /** 232 * Get list of full polynomials. 233 * @return list of all full polynomials. 234 */ 235 public List<GenPolynomial<GenPolynomial<C>>> getPolynomialList() { 236 List<GenPolynomial<GenPolynomial<C>>> F = new ArrayList<GenPolynomial<GenPolynomial<C>>>(); 237 for (ColorPolynomial<C> s : list) { 238 F.add(s.getPolynomial()); 239 } 240 return F; 241 } 242 243 244 /** 245 * Get list of essential polynomials. 246 * @return list of all essential polynomials. 247 */ 248 public List<GenPolynomial<GenPolynomial<C>>> getEssentialPolynomialList() { 249 List<GenPolynomial<GenPolynomial<C>>> F = new ArrayList<GenPolynomial<GenPolynomial<C>>>(); 250 for (ColorPolynomial<C> s : list) { 251 F.add(s.getEssentialPolynomial()); 252 } 253 return F; 254 } 255 256 257 /** 258 * Check invariants. Check if all polynomials are determined and if the 259 * color of all coefficients is correct with respect to the condition. 260 * @return true, if all invariants are met, else false. 261 */ 262 public boolean checkInvariant() { 263 if (!isDetermined()) { 264 return false; 265 } 266 if (!condition.isDetermined(list)) { 267 return false; 268 } 269 // Condition<C> cond = condition; 270 for (ColorPolynomial<C> s : list) { 271 if (!s.checkInvariant()) { 272 System.out.println("notInvariant " + s); 273 System.out.println("condition: " + condition); 274 return false; 275 } 276 for (GenPolynomial<C> g : s.green.getMap().values()) { 277 if (condition.color(g) != Condition.Color.GREEN) { 278 System.out.println("notGreen " + g); 279 System.out.println("condition: " + condition); 280 System.out.println("colors: " + s); 281 return false; 282 } 283 } 284 for (GenPolynomial<C> r : s.red.getMap().values()) { 285 if (condition.color(r) != Condition.Color.RED) { 286 System.out.println("notRed " + r); 287 System.out.println("condition: " + condition); 288 System.out.println("colors: " + s); 289 return false; 290 } 291 } 292 for (GenPolynomial<C> w : s.white.getMap().values()) { 293 if (condition.color(w) != Condition.Color.WHITE) { 294 // System.out.println("notWhite " + w); 295 // System.out.println("condition: " + condition); 296 // System.out.println("colors: " + s); 297 continue; // no error 298 // return false; 299 } 300 } 301 } 302 return true; 303 } 304 305 306 /** 307 * Is this colored system completely determined. 308 * @return true, if each ColorPolynomial is determined, else false. 309 */ 310 public boolean isDetermined() { 311 for (ColorPolynomial<C> s : list) { 312 if (s.isZERO()) { 313 continue; 314 } 315 if (!s.isDetermined()) { 316 System.out.println("not simple determined " + s); 317 System.out.println("condition: " + condition); 318 return false; 319 } 320 if (!condition.isDetermined(s)) { 321 return false; 322 } 323 } 324 return true; 325 } 326 327 328 /** 329 * Re determine colorings of polynomials. 330 * @return colored system with re determined colored polynomials. 331 */ 332 public ColoredSystem<C> reDetermine() { 333 if (condition == null || condition.zero.isONE()) { 334 return this; 335 } 336 List<ColorPolynomial<C>> Sn = new ArrayList<ColorPolynomial<C>>(list.size()); 337 for (ColorPolynomial<C> c : list) { 338 ColorPolynomial<C> a = condition.reDetermine(c); 339 // if ( !a.isZERO() ) { 340 Sn.add(a); // must also add zeros 341 // } 342 } 343 return new ColoredSystem<C>(condition, Sn, pairlist); 344 } 345 346}