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