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