001/* 002 * $Id: ColoredSystem.java 5121 2015-02-16 22:48:13Z 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 * Get the Script representation. 141 * @see edu.jas.structure.Element#toScript() 142 */ 143 public String toScript() { 144 StringBuffer s = new StringBuffer("ColoredSystem: \n"); 145 if (list.size() > 0) { 146 s.append("polynomial ring : " + list.get(0).green.ring.toScript() + "\n"); 147 } else { 148 s.append("parameter polynomial ring : " + condition.zero.getRing().toScript() + "\n"); 149 } 150 s.append("conditions == 0 : " + getConditionZero().toString() + "\n"); 151 s.append("conditions != 0 : " + getConditionNonZero().toString() + "\n"); 152 if (debug) { 153 s.append("green coefficients:\n" + getGreenCoefficients().toString() + "\n"); 154 s.append("red coefficients:\n" + getRedCoefficients().toString() + "\n"); 155 } 156 s.append("colored polynomials:\n" + list + "\n"); 157 s.append("uncolored polynomials:\n" + getPolynomialList() + "\n"); 158 if (debug) { 159 s.append("essential polynomials:\n" + getEssentialPolynomialList() + "\n"); 160 } 161 if (pairlist != null) { 162 s.append(pairlist.toString() + "\n"); 163 } 164 return s.toString(); 165 } 166 167 168 /** 169 * Is this colored system equal to other. 170 * @param c other colored system. 171 * @return true, if this is equal to other, else false. 172 */ 173 @Override 174 @SuppressWarnings("unchecked") 175 public boolean equals(Object c) { 176 ColoredSystem<C> cs = null; 177 try { 178 cs = (ColoredSystem<C>) c; 179 } catch (ClassCastException e) { 180 return false; 181 } 182 if (cs == null) { 183 return false; 184 } 185 boolean t = (condition.equals(cs.condition) && list.equals(cs.list)); 186 if (!t) { 187 return t; 188 } 189 // now t == true 190 t = pairlist.equals(cs.pairlist); 191 if (!t) { 192 System.out.println("pairlists not equal " + pairlist + ", " + cs.pairlist); 193 } 194 return true; // if lists are equal ignore pairlists 195 } 196 197 198 /** 199 * Hash code for this colored system. 200 * @see java.lang.Object#hashCode() 201 */ 202 @Override 203 public int hashCode() { 204 int h; 205 h = condition.hashCode(); 206 h = h << 17; 207 h += list.hashCode(); 208 // h = h << 11; 209 // h += pairlist.hashCode(); 210 return h; 211 } 212 213 214 /** 215 * Get zero condition. 216 * @return condition.zero. 217 */ 218 public List<GenPolynomial<C>> getConditionZero() { 219 return condition.zero.getList(); 220 } 221 222 223 /** 224 * Get non zero condition. 225 * @return condition.nonZero. 226 */ 227 public List<GenPolynomial<C>> getConditionNonZero() { 228 return condition.nonZero.mset; 229 } 230 231 232 /** 233 * Get list of red coefficients of polynomials. 234 * @return list of all red coefficients of polynomials. 235 */ 236 public List<GenPolynomial<C>> getRedCoefficients() { 237 Set<GenPolynomial<C>> F = new HashSet<GenPolynomial<C>>(); 238 for (ColorPolynomial<C> s : list) { 239 F.addAll(s.red.getMap().values()); 240 } 241 List<GenPolynomial<C>> M = new ArrayList<GenPolynomial<C>>(F); 242 return M; 243 } 244 245 246 /** 247 * Get list of green coefficients of polynomials. 248 * @return list of all green coefficients of polynomials. 249 */ 250 public List<GenPolynomial<C>> getGreenCoefficients() { 251 Set<GenPolynomial<C>> F = new HashSet<GenPolynomial<C>>(); 252 for (ColorPolynomial<C> s : list) { 253 F.addAll(s.green.getMap().values()); 254 } 255 List<GenPolynomial<C>> M = new ArrayList<GenPolynomial<C>>(F); 256 return M; 257 } 258 259 260 /** 261 * Get list of full polynomials. 262 * @return list of all full polynomials. 263 */ 264 public List<GenPolynomial<GenPolynomial<C>>> getPolynomialList() { 265 List<GenPolynomial<GenPolynomial<C>>> F = new ArrayList<GenPolynomial<GenPolynomial<C>>>(); 266 for (ColorPolynomial<C> s : list) { 267 F.add(s.getPolynomial()); 268 } 269 return F; 270 } 271 272 273 /** 274 * Get list of essential polynomials. 275 * @return list of all essential polynomials. 276 */ 277 public List<GenPolynomial<GenPolynomial<C>>> getEssentialPolynomialList() { 278 List<GenPolynomial<GenPolynomial<C>>> F = new ArrayList<GenPolynomial<GenPolynomial<C>>>(); 279 for (ColorPolynomial<C> s : list) { 280 F.add(s.getEssentialPolynomial()); 281 } 282 return F; 283 } 284 285 286 /** 287 * Check invariants. Check if all polynomials are determined and if the 288 * color of all coefficients is correct with respect to the condition. 289 * @return true, if all invariants are met, else false. 290 */ 291 public boolean checkInvariant() { 292 if (!isDetermined()) { 293 return false; 294 } 295 if (!condition.isDetermined(list)) { 296 return false; 297 } 298 // Condition<C> cond = condition; 299 for (ColorPolynomial<C> s : list) { 300 if (!s.checkInvariant()) { 301 System.out.println("notInvariant " + s); 302 System.out.println("condition: " + condition); 303 return false; 304 } 305 for (GenPolynomial<C> g : s.green.getMap().values()) { 306 if (condition.color(g) != Condition.Color.GREEN) { 307 System.out.println("notGreen " + g); 308 System.out.println("condition: " + condition); 309 System.out.println("colors: " + s); 310 return false; 311 } 312 } 313 for (GenPolynomial<C> r : s.red.getMap().values()) { 314 if (condition.color(r) != Condition.Color.RED) { 315 System.out.println("notRed " + r); 316 System.out.println("condition: " + condition); 317 System.out.println("colors: " + s); 318 return false; 319 } 320 } 321 for (GenPolynomial<C> w : s.white.getMap().values()) { 322 if (condition.color(w) != Condition.Color.WHITE) { 323 // System.out.println("notWhite " + w); 324 // System.out.println("condition: " + condition); 325 // System.out.println("colors: " + s); 326 continue; // no error 327 // return false; 328 } 329 } 330 } 331 return true; 332 } 333 334 335 /** 336 * Is this colored system completely determined. 337 * @return true, if each ColorPolynomial is determined, else false. 338 */ 339 public boolean isDetermined() { 340 for (ColorPolynomial<C> s : list) { 341 if (s.isZERO()) { 342 continue; 343 } 344 if (!s.isDetermined()) { 345 System.out.println("not simple determined " + s); 346 System.out.println("condition: " + condition); 347 return false; 348 } 349 if (!condition.isDetermined(s)) { 350 return false; 351 } 352 } 353 return true; 354 } 355 356 357 /** 358 * Re determine colorings of polynomials. 359 * @return colored system with re determined colored polynomials. 360 */ 361 public ColoredSystem<C> reDetermine() { 362 if (condition == null || condition.zero.isONE()) { 363 return this; 364 } 365 List<ColorPolynomial<C>> Sn = new ArrayList<ColorPolynomial<C>>(list.size()); 366 for (ColorPolynomial<C> c : list) { 367 ColorPolynomial<C> a = condition.reDetermine(c); 368 // if ( !a.isZERO() ) { 369 Sn.add(a); // must also add zeros 370 // } 371 } 372 return new ColoredSystem<C>(condition, Sn, pairlist); 373 } 374 375}