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