001    /*
002     * $Id: GroebnerSystem.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.PolynomialList;
016    import edu.jas.poly.OrderedPolynomialList;
017    import edu.jas.poly.GenPolynomial;
018    import edu.jas.poly.GenPolynomialRing;
019    import edu.jas.structure.GcdRingElem;
020    
021    
022    /**
023     * Container for a Groebner system. 
024     * It contains a list of colored systems and a
025     * list of parametric polynomials representing the 
026     * corresponding comprehensive Groebner base.
027     * @param <C> coefficient type
028     */
029    public class GroebnerSystem<C extends GcdRingElem<C>> {
030    
031    
032        private static final Logger logger = Logger.getLogger(GroebnerSystem.class);
033    
034    
035        private final boolean debug = logger.isDebugEnabled();
036    
037    
038        /**
039         * List of colored systems.
040         */
041        public final List<ColoredSystem<C>> list;
042    
043    
044        /**
045         * List of conditions for this Groebner system.
046         */
047        protected List<Condition<C>> conds;
048    
049    
050        /**
051         * Comprehensive Groebner base for this Groebner system.
052         */
053        protected PolynomialList<GenPolynomial<C>> cgb;
054    
055    
056        /**
057         * Constructor for a Groebner system.
058         * @param S a list of colored systems.
059         */
060        public GroebnerSystem(List<ColoredSystem<C>> S) {
061            this.list = S;
062            this.conds = null;
063            this.cgb = null;
064        }
065    
066    
067        /**
068         * Get the String representation.
069         * @see java.lang.Object#toString()
070         */
071        @Override
072        public String toString() {
073            StringBuffer sb = new StringBuffer("GroebnerSystem: \n");
074            boolean first = true;
075            for (ColoredSystem<C> cs : list) {
076                if ( first ) {
077                   first = false;
078                } else {
079                   sb.append("\n");
080                }
081                sb.append( cs.toString() );
082            }
083            sb.append("Conditions:\n");
084            first = true;
085            for ( Condition<C> cond : getConditions() ) {
086                if ( first ) {
087                    first = false;
088                } else {
089                    sb.append("\n");
090                }
091                sb.append( cond.toString() );
092            }
093            sb.append("\n");
094            if ( cgb == null ) {
095               sb.append("Comprehensive Groebner Base not jet computed\n");
096            } else {
097               sb.append("Comprehensive Groebner Base:\n");
098               first = true;
099               for ( GenPolynomial<GenPolynomial<C>> p : getCGB() ) {
100                   if ( first ) {
101                      first = false;
102                   } else {
103                      sb.append(",\n");
104                   }
105                   sb.append( p.toString() );
106               }
107               sb.append("\n");
108            }
109            return sb.toString();
110        }
111    
112    
113        /**
114         * Is this Groebner system equal to other.
115         * @param c other Groebner system.
116         * @return true, if this is equal to other, else false.
117         */
118        @Override
119        @SuppressWarnings("unchecked")
120        public boolean equals(Object c) {
121            GroebnerSystem<C> cs = null;
122            try {
123                cs = (GroebnerSystem<C>) c;
124            } catch (ClassCastException e) {
125                return false;
126            }
127            if (cs == null) {
128                return false;
129            }
130            boolean t = list.equals(cs.list);
131            return t;
132        }
133    
134    
135        /**
136         * Hash code for this colored system.
137         * @see java.lang.Object#hashCode()
138         */
139        @Override
140        public int hashCode() {
141            int h;
142            h = list.hashCode();
143            return h;
144        }
145    
146    
147        /**
148         * Check invariants. Check if all colored systems are determined and 
149         * all invariants are met.
150         * @return true, if all invariants are met, else false.
151         */
152        public boolean checkInvariant() {
153            for (ColoredSystem<C> s : list) {
154                if (!s.checkInvariant()) {
155                    return false;
156                }
157            }
158            return true;
159        }
160    
161    
162        /**
163         * Is each colored system completely determined.
164         * @return true, if each ColoredSystem is determined, else false.
165         */
166        public boolean isDetermined() {
167            for (ColoredSystem<C> s : list) {
168                if (!s.isDetermined()) {
169                    return false;
170                }
171            }
172            return true;
173        }
174    
175    
176        /**
177         * Get list of conditions determining this Groebner system. 
178         * @return list of determining conditions.
179         */
180        public List<Condition<C>> getConditions() {
181            if ( conds != null ) {
182               return conds;
183            }
184            List<Condition<C>> cd = new ArrayList<Condition<C>>( list.size() );
185            for (ColoredSystem<C> cs : list) {
186                cd.add(cs.condition);
187            }
188            conds = cd;
189            return conds;
190        }
191    
192    
193        /**
194         * Get comprehensive Groebner base. 
195         * @return the comprehensive Groebner base for this Groebner system.
196         */
197        public List<GenPolynomial<GenPolynomial<C>>> getCGB() {
198            if ( cgb != null ) {
199               return cgb.list;
200            }
201            // assure conditions are collected
202            List<Condition<C>> unused = getConditions();
203            // combine for CGB
204            Set<GenPolynomial<GenPolynomial<C>>> Gs 
205               = new HashSet<GenPolynomial<GenPolynomial<C>>>();
206            for (ColoredSystem<C> cs : list) {
207                if (debug) {
208                    if (!cs.isDetermined()) {
209                        System.out.println("not determined, cs = " + cs);
210                    }
211                    if (!cs.checkInvariant()) {
212                        System.out.println("not invariant, cs = " + cs);
213                    }
214                }
215                for (ColorPolynomial<C> p : cs.list) {
216                    GenPolynomial<GenPolynomial<C>> f = p.getPolynomial();
217                    Gs.add(f);
218                }
219            }
220            List<GenPolynomial<GenPolynomial<C>>> G 
221                = new ArrayList<GenPolynomial<GenPolynomial<C>>>(Gs);
222            GenPolynomialRing<GenPolynomial<C>> ring = null;
223            if ( G.size() > 0 ) {
224               ring = G.get(0).ring;
225            }
226            cgb = new OrderedPolynomialList<GenPolynomial<C>>(ring,G);
227            return G;
228        }
229    
230    }