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