001    /*
002     * $Id: ModSolvableGroebnerBaseAbstract.java 3445 2010-12-25 17:24:04Z kredel $
003     */
004    
005    package edu.jas.gbmod;
006    
007    import java.util.ArrayList;
008    import java.util.List;
009    
010    import org.apache.log4j.Logger;
011    
012    
013    import edu.jas.structure.RingElem;
014    
015    import edu.jas.gb.SolvableGroebnerBase;
016    import edu.jas.gb.SolvableGroebnerBaseSeq;
017    import edu.jas.poly.GenSolvablePolynomial;
018    import edu.jas.poly.GenSolvablePolynomialRing;
019    import edu.jas.poly.ModuleList;
020    import edu.jas.poly.PolynomialList;
021    
022    
023    
024    /**
025     * Module solvable Groebner Bases class.
026     * Implements module solvable Groebner bases and GB test.
027     * @param <C> coefficient type
028     * @author Heinz Kredel
029     */
030    
031    public class ModSolvableGroebnerBaseAbstract<C extends RingElem<C>> 
032           implements ModSolvableGroebnerBase<C> {
033    
034        private static final Logger logger = Logger.getLogger(ModSolvableGroebnerBase.class);
035        private final boolean debug = logger.isDebugEnabled();
036    
037    
038        /**
039         * Used Solvable Groebner base algorithm.
040         */
041        protected final SolvableGroebnerBase<C> sbb;
042    
043    
044        /**
045         * Constructor.
046         */
047        public ModSolvableGroebnerBaseAbstract() {
048            sbb = new SolvableGroebnerBaseSeq<C>();
049        }
050    
051    
052        /**
053         * Module left Groebner base test.
054         * @param modv number of modul variables.
055         * @param F a module basis.
056         * @return true, if F is a left Groebner base, else false.
057         */
058        public boolean 
059               isLeftGB(int modv, List<GenSolvablePolynomial<C>> F) {  
060            return sbb.isLeftGB(modv,F);
061        }
062    
063    
064        /**
065         * Module left Groebner base test.
066         * @param M a module basis.
067         * @return true, if M is a left Groebner base, else false.
068         */
069        public boolean 
070               isLeftGB(ModuleList<C> M) {  
071            if ( M == null || M.list == null ) {
072                return true;
073            }
074            if ( M.rows == 0 || M.cols == 0 ) {
075                return true;
076            }
077            int modv = M.cols; // > 0  
078            PolynomialList<C> F = M.getPolynomialList();
079            return sbb.isLeftGB(modv,F.castToSolvableList());
080        }
081    
082    
083        /**
084         * Left Groebner base using pairlist class.
085         * @param modv number of modul variables.
086         * @param F a module basis.
087         * @return leftGB(F) a left Groebner base for F.
088         */
089        public List<GenSolvablePolynomial<C>> 
090               leftGB(int modv, List<GenSolvablePolynomial<C>> F) {  
091            return sbb.leftGB(modv,F);
092        }
093    
094        /**
095         * Left Groebner base using pairlist class.
096         * @param M a module basis.
097         * @return leftGB(M) a left Groebner base for M.
098         */
099        public ModuleList<C> 
100               leftGB(ModuleList<C> M) {  
101            ModuleList<C> N = M;
102            if ( M == null || M.list == null ) {
103                return N;
104            }
105            if ( M.rows == 0 || M.cols == 0 ) {
106                return N;
107            }
108            PolynomialList<C> F = M.getPolynomialList();
109            if ( debug ) {
110               logger.info("F left +++++++++++++++++++ \n" + F);
111            }
112            GenSolvablePolynomialRing<C> sring 
113                = (GenSolvablePolynomialRing<C>)F.ring;
114            int modv = M.cols;
115            List<GenSolvablePolynomial<C>> G 
116                = sbb.leftGB(modv,F.castToSolvableList());
117            F = new PolynomialList<C>(sring,G);
118            if ( debug ) {
119               logger.info("G left +++++++++++++++++++ \n" + F);
120            }
121            N = F.getModuleList(modv);
122            return N;
123        }
124    
125    
126    
127        /**
128         * Module twosided Groebner base test.
129         * @param modv number of modul variables.
130         * @param F a module basis.
131         * @return true, if F is a twosided Groebner base, else false.
132         */
133        public boolean 
134               isTwosidedGB(int modv, List<GenSolvablePolynomial<C>> F) {  
135            return sbb.isTwosidedGB(modv,F);
136        }
137    
138        /**
139         * Module twosided Groebner base test.
140         * @param M a module basis.
141         * @return true, if M is a twosided Groebner base, else false.
142         */
143        public boolean 
144               isTwosidedGB(ModuleList<C> M) {  
145            if ( M == null || M.list == null ) {
146                return true;
147            }
148            if ( M.rows == 0 || M.cols == 0 ) {
149                return true;
150            }
151            PolynomialList<C> F = M.getPolynomialList();
152            int modv = M.cols; // > 0  
153            return sbb.isTwosidedGB(modv,F.castToSolvableList());
154        }
155    
156    
157        /**
158         * Twosided Groebner base using pairlist class.
159         * @param modv number of modul variables.
160         * @param F a module basis.
161         * @return tsGB(F) a twosided Groebner base for F.
162         */
163        public List<GenSolvablePolynomial<C>> 
164               twosidedGB(int modv, List<GenSolvablePolynomial<C>> F) {  
165            return sbb.twosidedGB(modv,F);
166        }
167    
168        /**
169         * Twosided Groebner base using pairlist class.
170         * @param M a module basis.
171         * @return tsGB(M) a twosided Groebner base for M.
172         */
173        public ModuleList<C> 
174               twosidedGB(ModuleList<C> M) {  
175            ModuleList<C> N = M;
176            if ( M == null || M.list == null ) {
177                return N;
178            }
179            if ( M.rows == 0 || M.cols == 0 ) {
180                return N;
181            }
182            PolynomialList<C> F = M.getPolynomialList();
183            GenSolvablePolynomialRing<C> sring 
184                = (GenSolvablePolynomialRing<C>)F.ring;
185            int modv = M.cols;
186            List<GenSolvablePolynomial<C>> G 
187                = sbb.twosidedGB(modv,F.castToSolvableList());
188            F = new PolynomialList<C>(sring,G);
189            N = F.getModuleList(modv);
190            return N;
191        }
192    
193    
194        /**
195         * Module right Groebner base test.
196         * @param modv number of modul variables.
197         * @param F a module basis.
198         * @return true, if F is a right Groebner base, else false.
199         */
200        public boolean 
201               isRightGB(int modv, List<GenSolvablePolynomial<C>> F) {  
202            return sbb.isRightGB(modv,F);
203        }
204    
205    
206        /**
207         * Module right Groebner base test.
208         * @param M a module basis.
209         * @return true, if M is a right Groebner base, else false.
210         */
211        public boolean 
212               isRightGB(ModuleList<C> M) {  
213            if ( M == null || M.list == null ) {
214                return true;
215            }
216            if ( M.rows == 0 || M.cols == 0 ) {
217                return true;
218            }
219            int modv = M.cols; // > 0  
220            PolynomialList<C> F = M.getPolynomialList();
221            //System.out.println("F test = " + F);
222            return sbb.isRightGB( modv, F.castToSolvableList() );
223        }
224    
225    
226        /**
227         * Right Groebner base using pairlist class.
228         * @param modv number of modul variables.
229         * @param F a module basis.
230         * @return rightGB(F) a right Groebner base for F.
231         */
232        public List<GenSolvablePolynomial<C>> 
233               rightGB(int modv, List<GenSolvablePolynomial<C>> F) {  
234            if ( modv == 0 ) {
235               return sbb.rightGB(modv,F);
236            }
237            throw new UnsupportedOperationException("modv != 0 not jet implemented");
238            // return sbb.rightGB(modv,F);
239        }
240    
241    
242        /**
243         * Right Groebner base using pairlist class.
244         * @param M a module basis.
245         * @return rightGB(M) a right Groebner base for M.
246         */
247        public ModuleList<C> 
248               rightGB(ModuleList<C> M) {  
249            ModuleList<C> N = M;
250            if ( M == null || M.list == null ) {
251                return N;
252            }
253            if ( M.rows == 0 || M.cols == 0 ) {
254                return N;
255            }
256            if ( debug ) {
257               logger.info("M ====================== \n" + M);
258            }
259            List<List<GenSolvablePolynomial<C>>> mlist = M.castToSolvableList();
260            GenSolvablePolynomialRing<C> sring 
261                = (GenSolvablePolynomialRing<C>)M.ring;
262            GenSolvablePolynomialRing<C> rring = sring.reverse(true); //true
263            sring = rring.reverse(true); // true
264    
265            List<List<GenSolvablePolynomial<C>>> nlist = 
266                new ArrayList<List<GenSolvablePolynomial<C>>>( M.rows );
267            for ( List<GenSolvablePolynomial<C>> row : mlist ) {
268                List<GenSolvablePolynomial<C>> nrow = 
269                    new ArrayList<GenSolvablePolynomial<C>>( row.size() );
270                for ( GenSolvablePolynomial<C> elem : row ) {
271                    GenSolvablePolynomial<C> nelem 
272                       = (GenSolvablePolynomial<C>)elem.reverse(rring);
273                    nrow.add( nelem );
274                }
275                nlist.add( nrow );
276            }
277            ModuleList<C> rM = new ModuleList<C>( rring, nlist );
278            if ( debug ) {
279               logger.info("rM -------------------- \n" + rM);
280            }
281            ModuleList<C> rMg = leftGB( rM );
282            if ( debug ) {
283               logger.info("rMg -------------------- \n" + rMg);
284            }
285    
286            mlist = rMg.castToSolvableList();
287            nlist = new ArrayList<List<GenSolvablePolynomial<C>>>( rMg.rows );
288            for ( List<GenSolvablePolynomial<C>> row : mlist ) {
289                List<GenSolvablePolynomial<C>> nrow = 
290                    new ArrayList<GenSolvablePolynomial<C>>( row.size() );
291                for ( GenSolvablePolynomial<C> elem : row ) {
292                    GenSolvablePolynomial<C> nelem 
293                       = (GenSolvablePolynomial<C>)elem.reverse(sring);
294                    nrow.add( nelem );
295                }
296                nlist.add( nrow );
297            }
298            ModuleList<C> Mg = new ModuleList<C>( sring, nlist );
299            if ( debug ) {
300               logger.info("Mg -------------------- \n" + Mg);
301            }
302            return Mg;
303        }
304    
305    }