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