001    /*
002     * $Id: GroebnerBaseSeqTest.java 3652 2011-06-02 18:17:04Z kredel $
003     */
004    
005    package edu.jas.gb;
006    
007    
008    import java.util.List;
009    import java.util.ArrayList;
010    import java.io.IOException;
011    import java.io.Reader;
012    import java.io.StringReader;
013    
014    import junit.framework.Test;
015    import junit.framework.TestCase;
016    import junit.framework.TestSuite;
017    
018    import org.apache.log4j.BasicConfigurator;
019    //import org.apache.log4j.Logger;
020    
021    import edu.jas.arith.BigRational;
022    import edu.jas.gb.GroebnerBase;
023    import edu.jas.poly.GenPolynomial;
024    import edu.jas.poly.GenPolynomialRing;
025    import edu.jas.poly.GenPolynomialTokenizer;
026    import edu.jas.poly.PolynomialList;
027    
028    
029    /**
030     * Groebner base sequential tests with JUnit.
031     * @author Heinz Kredel.
032     */
033    
034    public class GroebnerBaseSeqTest extends TestCase {
035    
036        //private static final Logger logger = Logger.getLogger(GroebnerBaseSeqTest.class);
037    
038        /**
039         * main
040         */
041        public static void main (String[] args) {
042            BasicConfigurator.configure();
043            junit.textui.TestRunner.run( suite() );
044        }
045    
046    
047        /**
048         * Constructs a <CODE>GroebnerBaseSeqTest</CODE> object.
049         * @param name String.
050         */
051        public GroebnerBaseSeqTest(String name) {
052            super(name);
053        }
054    
055    
056        /**
057         * suite.
058         */ 
059        public static Test suite() {
060            TestSuite suite= new TestSuite(GroebnerBaseSeqTest.class);
061            return suite;
062        }
063    
064    
065        GenPolynomialRing<BigRational> fac;
066    
067        List<GenPolynomial<BigRational>> L;
068        PolynomialList<BigRational> F;
069        List<GenPolynomial<BigRational>> G;
070    
071        GroebnerBase<BigRational> bb;
072    
073        GenPolynomial<BigRational> a;
074        GenPolynomial<BigRational> b;
075        GenPolynomial<BigRational> c;
076        GenPolynomial<BigRational> d;
077        GenPolynomial<BigRational> e;
078    
079        int rl = 4; //4; //3; 
080        int kl = 7; // 10
081        int ll = 7;
082        int el = 3; // 4
083        float q = 0.2f; //0.4f
084    
085    
086        protected void setUp() {
087            BigRational coeff = new BigRational(9);
088            fac = new GenPolynomialRing<BigRational>(coeff,rl);
089            a = b = c = d = e = null;
090            bb = new GroebnerBaseSeq<BigRational>();
091        }
092    
093    
094        protected void tearDown() {
095            a = b = c = d = e = null;
096            fac = null;
097            bb = null;
098        }
099    
100    
101        /**
102         * Test sequential GBase.
103         */
104        public void testSequentialGBase() {
105            L = new ArrayList<GenPolynomial<BigRational>>();
106    
107            a = fac.random(kl, ll, el, q );
108            b = fac.random(kl, ll, el, q );
109            c = fac.random(kl, ll, el, q );
110            d = fac.random(kl, ll, el, q );
111            e = d; //fac.random(kl, ll, el, q );
112    
113            if ( a.isZERO() || b.isZERO() || c.isZERO() || d.isZERO() ) {
114                return;
115            }
116    
117            assertTrue("not isZERO( a )", !a.isZERO() );
118            L.add(a);
119    
120            L = bb.GB( L );
121            assertTrue("isGB( { a } )", bb.isGB(L) );
122    
123            assertTrue("not isZERO( b )", !b.isZERO() );
124            L.add(b);
125            //System.out.println("L = " + L.size() );
126    
127            L = bb.GB( L );
128            assertTrue("isGB( { a, b } )", bb.isGB(L) );
129    
130            assertTrue("not isZERO( c )", !c.isZERO() );
131            L.add(c);
132    
133            L = bb.GB( L );
134            assertTrue("isGB( { a, b, c } )", bb.isGB(L) );
135    
136            assertTrue("not isZERO( d )", !d.isZERO() );
137            L.add(d);
138    
139            L = bb.GB( L );
140            assertTrue("isGB( { a, b, c, d } )", bb.isGB(L) );
141    
142            assertTrue("not isZERO( e )", !e.isZERO() );
143            L.add(e);
144    
145            L = bb.GB( L );
146            assertTrue("isGB( { a, b, c, d, e } )", bb.isGB(L) );
147        }
148    
149    
150        /**
151         * Test Trinks7 GBase.
152         */
153        @SuppressWarnings("unchecked") 
154        public void testTrinks7GBase() {
155            String exam = "(B,S,T,Z,P,W) L "
156                + "( "  
157                + "( 45 P + 35 S - 165 B - 36 ), " 
158                + "( 35 P + 40 Z + 25 T - 27 S ), "
159                + "( 15 W + 25 S P + 30 Z - 18 T - 165 B**2 ), "
160                + "( - 9 W + 15 T P + 20 S Z ), "
161                + "( P W + 2 T Z - 11 B**3 ), "
162                + "( 99 W - 11 B S + 3 B**2 ), "
163                + "( B**2 + 33/50 B + 2673/10000 ) "
164                + ") ";
165            String exam2 = "(x,y,z) L "
166                + "( "
167                + "( z y**2 + 2 x + 1/2 )"
168                + "( z x**2 - y**2 - 1/2 x )"
169                + "( -z + y**2 x + 4 x**2 + 1/4 )"
170                + " )";
171    
172            Reader source = new StringReader( exam );
173            GenPolynomialTokenizer parser
174                = new GenPolynomialTokenizer( source );
175            try {
176                F = (PolynomialList<BigRational>) parser.nextPolynomialSet();
177            } catch(ClassCastException e) {
178                fail(""+e);
179            } catch(IOException e) {
180                fail(""+e);
181            }
182            //System.out.println("F = " + F);
183    
184            G = bb.GB(F.list);
185            assertTrue("isGB( GB(Trinks7) )", bb.isGB(G) );
186            assertEquals("#GB(Trinks7) == 6", 6, G.size() );
187            PolynomialList<BigRational> trinks 
188                = new PolynomialList<BigRational>(F.ring,G);
189            //System.out.println("G = " + trinks);
190        }
191    
192    
193        /**
194         * Test sequential extended GBase.
195         * 
196         */
197        public void testSequentialExtendedGBase() {
198    
199            L = new ArrayList<GenPolynomial<BigRational>>();
200    
201            ExtendedGB<BigRational> exgb;
202    
203            a = fac.random(kl, ll, el, q );
204            b = fac.random(kl, ll, el, q );
205            c = fac.random(kl, ll, el, q );
206            d = fac.random(kl, ll, el, q );
207            e = d; //fac.random(kl, ll, el, q );
208    
209            if ( a.isZERO() || b.isZERO() || c.isZERO() || d.isZERO() ) {
210                return;
211            }
212    
213            assertTrue("not isZERO( a )", !a.isZERO() );
214            L.add(a);
215            //System.out.println("L = " + L );
216    
217            exgb = bb.extGB( L );
218            // System.out.println("exgb = " + exgb );
219            assertTrue("isGB( { a } )", bb.isGB(exgb.G) );
220            assertTrue("isRmat( { a } )", bb.isReductionMatrix(exgb) );
221    
222            assertTrue("not isZERO( b )", !b.isZERO() );
223            L.add(b);
224            //System.out.println("L = " + L );
225    
226            exgb = bb.extGB( L );
227            //System.out.println("exgb = " + exgb );
228            assertTrue("isGB( { a, b } )", bb.isGB(exgb.G) );
229            assertTrue("isRmat( { a, b } )", bb.isReductionMatrix(exgb) );
230    
231            assertTrue("not isZERO( c )", !c.isZERO() );
232            L.add(c);
233    
234            exgb = bb.extGB( L );
235            //System.out.println("exgb = " + exgb );
236            assertTrue("isGB( { a, b, c } )", bb.isGB(exgb.G) );
237            assertTrue("isRmat( { a, b, c } )", bb.isReductionMatrix(exgb) );
238    
239            assertTrue("not isZERO( d )", !d.isZERO() );
240            L.add(d);
241    
242            exgb = bb.extGB( L );
243            //System.out.println("exgb = " + exgb );
244            assertTrue("isGB( { a, b, c, d } )", bb.isGB(exgb.G) );
245            assertTrue("isRmat( { a, b, c, d } )", bb.isReductionMatrix(exgb) );
246    
247    
248            assertTrue("not isZERO( e )", !e.isZERO() );
249            L.add(e);
250    
251            exgb = bb.extGB( L );
252            //System.out.println("exgb = " + exgb );
253            assertTrue("isGB( { a, b, c, d, e } )", bb.isGB(exgb.G) );
254            assertTrue("isRmat( { a, b, c, d, e } )", bb.isReductionMatrix(exgb) );
255        }
256    
257    
258        /**
259         * Test Trinks7 GBase.
260         * 
261         */
262        @SuppressWarnings("unchecked") 
263        public void testTrinks7ExtendedGBase() {
264            String exam = "(B,S,T,Z,P,W) L "
265                + "( "  
266                + "( 45 P + 35 S - 165 B - 36 ), " 
267                + "( 35 P + 40 Z + 25 T - 27 S ), "
268                + "( 15 W + 25 S P + 30 Z - 18 T - 165 B**2 ), "
269                + "( - 9 W + 15 T P + 20 S Z ), "
270                + "( P W + 2 T Z - 11 B**3 ), "
271                + "( 99 W - 11 B S + 3 B**2 ), "
272                + "( B**2 + 33/50 B + 2673/10000 ) "
273                + ") ";
274            Reader source = new StringReader( exam );
275            GenPolynomialTokenizer parser
276                = new GenPolynomialTokenizer( source );
277            try {
278                F = (PolynomialList<BigRational>) parser.nextPolynomialSet();
279            } catch(ClassCastException e) {
280                fail(""+e);
281            } catch(IOException e) {
282                fail(""+e);
283            }
284            //System.out.println("F = " + F);
285    
286            ExtendedGB<BigRational> exgb;
287            exgb = bb.extGB(F.list);
288            //System.out.println("exgb = " + exgb );
289            assertTrue("isGB( GB(Trinks7) )", bb.isGB(exgb.G) );
290            //assertEquals("#GB(Trinks7) == 6", 6, exgb.G.size() );
291            assertTrue("isRmat( GB(Trinks7) )", bb.isReductionMatrix(exgb) );
292            PolynomialList<BigRational> trinks 
293                = new PolynomialList<BigRational>(F.ring,G);
294            //System.out.println("G = " + trinks);
295        }
296    
297    
298        /**
299         * Test Trinks7 GBase, syz pair list.
300         * 
301         */
302        @SuppressWarnings("unchecked") 
303        public void testTrinks7GBaseSyz() {
304            GroebnerBase<BigRational> bbs;
305            bbs = new GroebnerBaseSeq<BigRational>(new ReductionSeq<BigRational>(),
306                     new OrderedSyzPairlist<BigRational>());
307    
308            String exam = "(B,S,T,Z,P,W) L "
309                + "( "  
310                + "( 45 P + 35 S - 165 B - 36 ), " 
311                + "( 35 P + 40 Z + 25 T - 27 S ), "
312                + "( 15 W + 25 S P + 30 Z - 18 T - 165 B**2 ), "
313                + "( - 9 W + 15 T P + 20 S Z ), "
314                + "( P W + 2 T Z - 11 B**3 ), "
315                + "( 99 W - 11 B S + 3 B**2 ), "
316                + "( B**2 + 33/50 B + 2673/10000 ) "
317                + ") ";
318            String exam2 = "(x,y,z) L "
319                + "( "
320                + "( z y**2 + 2 x + 1/2 )"
321                + "( z x**2 - y**2 - 1/2 x )"
322                + "( -z + y**2 x + 4 x**2 + 1/4 )"
323                + " )";
324    
325            Reader source = new StringReader( exam );
326            GenPolynomialTokenizer parser
327                = new GenPolynomialTokenizer( source );
328            try {
329                F = (PolynomialList<BigRational>) parser.nextPolynomialSet();
330            } catch(ClassCastException e) {
331                fail(""+e);
332            } catch(IOException e) {
333                fail(""+e);
334            }
335            //System.out.println("F = " + F);
336    
337            G = bbs.GB(F.list);
338            assertTrue("isGB( GB(Trinks7) )", bbs.isGB(G) );
339            assertEquals("#GB(Trinks7) == 6", 6, G.size() );
340            PolynomialList<BigRational> trinks 
341                = new PolynomialList<BigRational>(F.ring,G);
342            //System.out.println("G = " + trinks);
343            assertTrue("isGB( GB(Trinks7) )", bb.isGB(G) );
344    
345            Reduction<BigRational> rd = new ReductionSeq<BigRational>();
346            //System.out.println("G.contains(F) = " + rd.normalform(G,F.list) );
347        }
348    
349    
350        /**
351         * Test Trinks7 GBase, min pair list.
352         * 
353         */
354        @SuppressWarnings("unchecked") 
355        public void testTrinks7GBaseMin() {
356            bb = new GroebnerBaseSeq<BigRational>(new ReductionSeq<BigRational>(),
357                     new OrderedMinPairlist<BigRational>());
358    
359            String exam = "(B,S,T,Z,P,W) L "
360                + "( "  
361                + "( 45 P + 35 S - 165 B - 36 ), " 
362                + "( 35 P + 40 Z + 25 T - 27 S ), "
363                + "( 15 W + 25 S P + 30 Z - 18 T - 165 B**2 ), "
364                + "( - 9 W + 15 T P + 20 S Z ), "
365                + "( P W + 2 T Z - 11 B**3 ), "
366                + "( 99 W - 11 B S + 3 B**2 ), "
367                + "( B**2 + 33/50 B + 2673/10000 ) "
368                + ") ";
369            Reader source = new StringReader( exam );
370            GenPolynomialTokenizer parser
371                = new GenPolynomialTokenizer( source );
372            try {
373                F = (PolynomialList<BigRational>) parser.nextPolynomialSet();
374            } catch(ClassCastException e) {
375                fail(""+e);
376            } catch(IOException e) {
377                fail(""+e);
378            }
379            //System.out.println("F = " + F);
380    
381            G = bb.GB(F.list);
382            assertTrue("isGB( GB(Trinks7) )", bb.isGB(G) );
383            assertEquals("#GB(Trinks7) == 6", 6, G.size() );
384            PolynomialList<BigRational> trinks 
385                = new PolynomialList<BigRational>(F.ring,G);
386            //System.out.println("G = " + trinks);
387        }
388    
389    
390        /**
391         * Test sequential GBase, both.
392         * 
393         */
394        public void testSequentialGBaseBoth() {
395            GroebnerBase<BigRational> bbs = new GroebnerBaseSeq<BigRational>(new ReductionSeq<BigRational>(),
396                                                                             new OrderedSyzPairlist<BigRational>());
397    
398            L = new ArrayList<GenPolynomial<BigRational>>();
399            List<GenPolynomial<BigRational>> G;
400    
401            do {
402                a = fac.random(kl, ll, el, q );
403            } while ( a.isZERO() || a.isONE() );
404            do {
405                b = fac.random(kl, ll, el, q );
406            } while ( b.isZERO() || b.isONE() );
407            do {
408                c = fac.random(kl, ll, el, q );
409            } while ( c.isZERO() || c.isONE() );
410            do {
411                d = fac.random(kl, ll, el, q );
412            } while ( d.isZERO() || d.isONE() );
413            e = d; //fac.random(kl, ll, el, q );
414    
415            //if ( a.isZERO() || b.isZERO() || c.isZERO() || d.isZERO() ) {
416            //    return;
417            //}
418    
419            L.add(a);
420    
421            G = bb.GB( L );
422            assertTrue("isGB( { a } )", bb.isGB(G) );
423            G = bbs.GB( L );
424            assertTrue("isGB( { a } )", bbs.isGB(G) );
425    
426            G.add(b);
427            L = G;
428    
429            G = bb.GB( L );
430            assertTrue("isGB( { a, b } )", bb.isGB(G) );
431            G = bbs.GB( L );
432            assertTrue("isGB( { a, b } )", bbs.isGB(G) );
433    
434            G.add(c);
435            L = G;
436    
437            G = bb.GB( L );
438            assertTrue("isGB( { a, b, c } )", bb.isGB(G) );
439            G = bbs.GB( L );
440            assertTrue("isGB( { a, b, c } )", bbs.isGB(G) );
441    
442            G.add(d);
443            L = G;
444    
445            G = bb.GB( L );
446            assertTrue("isGB( { a, b, c, d } )", bb.isGB(G) );
447            G = bbs.GB( L );
448            assertTrue("isGB( { a, b, c, d } )", bbs.isGB(G) );
449    
450            G.add(e);
451            L = G;
452    
453            G = bb.GB( L );
454            assertTrue("isGB( { a, b, c, d, e } )", bb.isGB(G) );
455            G = bbs.GB( L );
456            assertTrue("isGB( { a, b, c, d, e } )", bbs.isGB(G) );
457        }
458    
459    }
460    
461    
462