001/*
002 * $Id: GBAlgorithmBuilderTest.java 5862 2018-07-20 10:56:22Z kredel $
003 */
004
005package edu.jas.application;
006
007
008import java.util.List;
009
010import junit.framework.Test;
011import junit.framework.TestCase;
012import junit.framework.TestSuite;
013
014
015
016import edu.jas.arith.BigInteger;
017import edu.jas.arith.BigRational;
018import edu.jas.gb.DGroebnerBaseSeq;
019import edu.jas.gb.EGroebnerBaseSeq;
020import edu.jas.gb.GBOptimized;
021import edu.jas.gb.GBProxy;
022import edu.jas.gb.GroebnerBaseAbstract;
023import edu.jas.gb.GroebnerBaseParallel;
024import edu.jas.gb.GroebnerBaseSeq;
025import edu.jas.gb.GroebnerBaseSeqIter;
026import edu.jas.gb.GroebnerBaseSigSeqIter;
027import edu.jas.gb.GroebnerBaseF5zSigSeqIter;
028import edu.jas.gb.GroebnerBaseGGVSigSeqIter;
029import edu.jas.gb.GroebnerBaseArriSigSeqIter;
030import edu.jas.gb.GroebnerBaseParIter;
031import edu.jas.gbufd.GBFactory;
032import edu.jas.gbufd.GroebnerBaseFGLM;
033import edu.jas.gbufd.GroebnerBaseWalk;
034import edu.jas.gbufd.GroebnerBasePseudoSeq;
035import edu.jas.gbufd.GroebnerBaseRational;
036import edu.jas.kern.ComputerThreads;
037import edu.jas.poly.GenPolynomial;
038import edu.jas.poly.GenPolynomialRing;
039
040
041/**
042 * GBAlgorithmBuilder tests with JUnit.
043 * @author Heinz Kredel
044 */
045
046public class GBAlgorithmBuilderTest extends TestCase {
047
048
049    /**
050     * main.
051     */
052    public static void main(String[] args) {
053        
054        junit.textui.TestRunner.run(suite());
055    }
056
057
058    /**
059     * Constructs a <CODE>GBAlgorithmBuilderTest</CODE> object.
060     * @param name String.
061     */
062    public GBAlgorithmBuilderTest(String name) {
063        super(name);
064    }
065
066
067    /**
068     * suite.
069     */
070    public static Test suite() {
071        TestSuite suite = new TestSuite(GBAlgorithmBuilderTest.class);
072        return suite;
073    }
074
075
076    GBAlgorithmBuilder builder;
077
078
079    @Override
080    protected void setUp() {
081        builder = null;
082    }
083
084
085    @Override
086    protected void tearDown() {
087        builder = null;
088        ComputerThreads.terminate();
089    }
090
091
092    /**
093     * Test basic construction for BigRational.
094     */
095    public void testConstructionRational() {
096        BigRational bf = new BigRational(1);
097        String[] vars = new String[] { "a", "b", "c" };
098        GenPolynomialRing<BigRational> pf = new GenPolynomialRing<BigRational>(bf, vars);
099
100        GBAlgorithmBuilder<BigRational> ab = GBAlgorithmBuilder.<BigRational> polynomialRing(pf);
101        //System.out.println("ab = " + ab);
102
103        GroebnerBaseAbstract<BigRational> bb = ab.build();
104        //System.out.println("bb = " + bb);
105        assertTrue("instance of " + bb, bb instanceof GroebnerBaseSeq);
106    }
107
108
109    /**
110     * Test construction for BigRational and FGLM.
111     */
112    public void testConstructionRationalFGLM() {
113        BigRational bf = new BigRational(1);
114        String[] vars = new String[] { "a", "b", "c" };
115        GenPolynomialRing<BigRational> pf = new GenPolynomialRing<BigRational>(bf, vars);
116
117        GBAlgorithmBuilder<BigRational> ab = GBAlgorithmBuilder.<BigRational> polynomialRing(pf);
118        //System.out.println("ab = " + ab);
119
120        ab = ab.graded();
121        //System.out.println("ab = " + ab);
122
123        GroebnerBaseAbstract<BigRational> bb = ab.build();
124        //System.out.println("bb = " + bb);
125        assertTrue("instance of " + bb, bb instanceof GroebnerBaseFGLM);
126    }
127
128
129    /**
130     * Test construction for BigRational and Groebner walk.
131     */
132    public void testConstructionRationalWalk() {
133        BigRational bf = new BigRational(1);
134        String[] vars = new String[] { "a", "b", "c" };
135        GenPolynomialRing<BigRational> pf = new GenPolynomialRing<BigRational>(bf, vars);
136
137        GBAlgorithmBuilder<BigRational> ab = GBAlgorithmBuilder.<BigRational> polynomialRing(pf);
138        //System.out.println("ab = " + ab);
139
140        ab = ab.walk();
141        //System.out.println("ab = " + ab);
142
143        GroebnerBaseAbstract<BigRational> bb = ab.build();
144        //System.out.println("bb = " + bb);
145        assertTrue("instance of " + bb, bb instanceof GroebnerBaseWalk);
146    }
147
148
149    /**
150     * Test construction for BigRational and parallel.
151     */
152    public void testConstructionRationalParallel() {
153        BigRational bf = new BigRational(1);
154        String[] vars = new String[] { "a", "b", "c" };
155        GenPolynomialRing<BigRational> pf = new GenPolynomialRing<BigRational>(bf, vars);
156
157        GBAlgorithmBuilder<BigRational> ab = GBAlgorithmBuilder.<BigRational> polynomialRing(pf);
158        //System.out.println("ab = " + ab);
159
160        ab = ab.parallel();
161        //System.out.println("ab = " + ab);
162
163        GroebnerBaseAbstract<BigRational> bb = ab.build();
164        //System.out.println("bb = " + bb);
165        assertTrue("instance of " + bb, bb instanceof GBProxy);
166
167        GBProxy<BigRational> bbp = (GBProxy<BigRational>) bb;
168        assertTrue("instance of " + bbp.e1, bbp.e1 instanceof GroebnerBaseSeq);
169        assertTrue("instance of " + bbp.e2, bbp.e2 instanceof GroebnerBaseParallel);
170    }
171
172
173    /**
174     * Test construction for BigRational fraction free and parallel.
175     */
176    public void testConstructionRationalFFParallel() {
177        BigRational bf = new BigRational(1);
178        String[] vars = new String[] { "a", "b", "c" };
179        GenPolynomialRing<BigRational> pf = new GenPolynomialRing<BigRational>(bf, vars);
180
181        GBAlgorithmBuilder<BigRational> ab = GBAlgorithmBuilder.<BigRational> polynomialRing(pf);
182        //System.out.println("ab = " + ab);
183
184        ab = ab.fractionFree();
185        //System.out.println("ab = " + ab);
186
187        ab = ab.parallel();
188        //System.out.println("ab = " + ab);
189
190        GroebnerBaseAbstract<BigRational> bb = ab.build();
191        //System.out.println("bb = " + bb);
192        assertTrue("instance of " + bb, bb instanceof GBProxy);
193
194        GBProxy<BigRational> bbp = (GBProxy<BigRational>) bb;
195        assertTrue("instance of " + bbp.e1, bbp.e1 instanceof GroebnerBaseRational);
196        assertTrue("instance of " + bbp.e2, bbp.e2 instanceof GroebnerBaseRational);
197    }
198
199
200    /**
201     * Test construction for BigRational and optimize.
202     */
203    public void testConstructionRationalOptimized() {
204        BigRational bf = new BigRational(1);
205        String[] vars = new String[] { "a", "b", "c" };
206        GenPolynomialRing<BigRational> pf = new GenPolynomialRing<BigRational>(bf, vars);
207
208        GBAlgorithmBuilder<BigRational> ab = GBAlgorithmBuilder.<BigRational> polynomialRing(pf);
209        //System.out.println("ab = " + ab);
210
211        ab = ab.optimize();
212        //System.out.println("ab = " + ab);
213
214        GroebnerBaseAbstract<BigRational> bb = ab.build();
215        //System.out.println("bb = " + bb);
216        assertTrue("instance of " + bb, bb instanceof GBOptimized);
217    }
218
219
220    /**
221     * Test construction for BigRational and fraction free.
222     */
223    public void testConstructionRationalFF() {
224        BigRational bf = new BigRational(1);
225        String[] vars = new String[] { "a", "b", "c" };
226        GenPolynomialRing<BigRational> pf = new GenPolynomialRing<BigRational>(bf, vars);
227
228        GBAlgorithmBuilder<BigRational> ab = GBAlgorithmBuilder.<BigRational> polynomialRing(pf);
229        //System.out.println("ab = " + ab);
230
231        ab = ab.fractionFree();
232        //System.out.println("ab = " + ab);
233
234        GroebnerBaseAbstract<BigRational> bb = ab.build();
235        //System.out.println("bb = " + bb);
236        assertTrue("instance of " + bb, bb instanceof GroebnerBaseRational);
237    }
238
239
240    /**
241     * Test basic construction for BigInteger.
242     */
243    public void testConstructionInteger() {
244        BigInteger bf = new BigInteger(1);
245        String[] vars = new String[] { "a", "b", "c" };
246        GenPolynomialRing<BigInteger> pf = new GenPolynomialRing<BigInteger>(bf, vars);
247
248        GBAlgorithmBuilder<BigInteger> ab = GBAlgorithmBuilder.<BigInteger> polynomialRing(pf);
249        //System.out.println("ab = " + ab);
250
251        GroebnerBaseAbstract<BigInteger> bb = ab.build();
252        //System.out.println("bb = " + bb);
253        assertTrue("instance of " + bb, bb instanceof GroebnerBasePseudoSeq);
254    }
255
256
257    /**
258     * Test construction for d-GB BigInteger.
259     */
260    public void testConstructionIntegerDGB() {
261        BigInteger bf = new BigInteger(1);
262        String[] vars = new String[] { "a", "b", "c" };
263        GenPolynomialRing<BigInteger> pf = new GenPolynomialRing<BigInteger>(bf, vars);
264
265        GBAlgorithmBuilder<BigInteger> ab = GBAlgorithmBuilder.<BigInteger> polynomialRing(pf);
266        //System.out.println("ab = " + ab);
267
268        ab = ab.domainAlgorithm(GBFactory.Algo.dgb);
269        //System.out.println("ab = " + ab);
270
271        GroebnerBaseAbstract<BigInteger> bb = ab.build();
272        //System.out.println("bb = " + bb);
273        assertTrue("instance of " + bb, bb instanceof DGroebnerBaseSeq);
274    }
275
276
277    /**
278     * Test construction for e-GB BigInteger.
279     */
280    public void testConstructionIntegerEGB() {
281        BigInteger bf = new BigInteger(1);
282        String[] vars = new String[] { "a", "b", "c" };
283        GenPolynomialRing<BigInteger> pf = new GenPolynomialRing<BigInteger>(bf, vars);
284
285        GBAlgorithmBuilder<BigInteger> ab = GBAlgorithmBuilder.<BigInteger> polynomialRing(pf);
286        //System.out.println("ab = " + ab);
287
288        ab = ab.domainAlgorithm(GBFactory.Algo.egb);
289        //System.out.println("ab = " + ab);
290
291        GroebnerBaseAbstract<BigInteger> bb = ab.build();
292        //System.out.println("bb = " + bb);
293        assertTrue("instance of " + bb, bb instanceof EGroebnerBaseSeq);
294    }
295
296
297    /**
298     * Test construction for BigRational and more.
299     */
300    public void testConstructionRationalMore() {
301        BigRational bf = new BigRational(1);
302        String[] vars = new String[] { "a", "b", "c" };
303        GenPolynomialRing<BigRational> pf = new GenPolynomialRing<BigRational>(bf, vars);
304
305        GroebnerBaseAbstract<BigRational> bb = GBAlgorithmBuilder.<BigRational> polynomialRing(pf)
306                        .fractionFree().optimize().build();
307        //System.out.println("bb = " + bb);
308        assertTrue("instance of " + bb, bb instanceof GBOptimized);
309
310        bb = GBAlgorithmBuilder.<BigRational> polynomialRing(pf).fractionFree().parallel().optimize().build();
311        //System.out.println("bb = " + bb);
312        assertTrue("instance of " + bb, bb instanceof GBOptimized);
313
314        bb = GBAlgorithmBuilder.<BigRational> polynomialRing(pf).fractionFree().graded().parallel()
315                        .optimize().build();
316        //System.out.println("bb = " + bb);
317        assertTrue("instance of " + bb, bb instanceof GBOptimized);
318    }
319
320
321    /**
322     * Test construction for BigRational and more and compute.
323     */
324    public void testConstructionRationalMoreCompute() {
325        List<GenPolynomial<BigRational>> cp = ExamplesGeoTheorems.getExample();
326        GenPolynomialRing<BigRational> pf = cp.get(0).ring;
327
328        GroebnerBaseAbstract<BigRational> bb;
329        //bb = GBAlgorithmBuilder.<BigRational> polynomialRing(pf).fractionFree().parallel().optimize().build();
330        //bb = GBAlgorithmBuilder.<BigRational> polynomialRing(pf).parallel().optimize().build();
331        //bb = GBAlgorithmBuilder.<BigRational> polynomialRing(pf).build();
332        bb = GBAlgorithmBuilder.<BigRational> polynomialRing(pf).optimize().build();
333        //bb = GBAlgorithmBuilder.<BigRational> polynomialRing(pf).fractionFree().build();
334        //bb = GBAlgorithmBuilder.<BigRational> polynomialRing(pf).parallel().build();
335        //bb = GBAlgorithmBuilder.<BigRational> polynomialRing(pf).fractionFree().optimize().build();
336        //bb = GBAlgorithmBuilder.<BigRational> polynomialRing(pf).parallel().fractionFree().optimize().build();
337        //bb = GBAlgorithmBuilder.<BigRational> polynomialRing(pf).optimize().fractionFree().build();
338        //bb = GBAlgorithmBuilder.<BigRational> polynomialRing(pf).fractionFree().optimize().parallel().build();
339        //System.out.println("bb = " + bb);
340        //assertTrue("instance of " + bb, bb instanceof GBOptimized);
341
342        List<GenPolynomial<BigRational>> gb;
343        long t;
344        t = System.currentTimeMillis();
345        gb = bb.GB(cp);
346        t = System.currentTimeMillis() - t;
347        //System.out.println("time(gb) = " + t);
348
349        t = System.currentTimeMillis();
350        gb = bb.GB(cp);
351        t = System.currentTimeMillis() - t;
352        //System.out.println("time(gb) = " + t);
353
354        t = System.currentTimeMillis();
355        gb = bb.GB(cp);
356        t = System.currentTimeMillis() - t;
357        //System.out.println("time(gb) = " + t);
358        assertTrue("t >= 0: ", t >= 0L); // praise findbugs
359
360        assertTrue("isGB: ", bb.isGB(gb));
361        bb.terminate();
362        //System.out.println("gb = " + gb);
363        //System.out.println("bb = " + bb);
364    }
365
366
367    /**
368     * Test construction for BigRational and pairlists and compute.
369     */
370    public void testConstructionRationalParilistCompute() {
371        List<GenPolynomial<BigRational>> cp = ExamplesGeoTheorems.getExample();
372        GenPolynomialRing<BigRational> pf = cp.get(0).ring;
373
374        GroebnerBaseAbstract<BigRational> bb;
375        List<GenPolynomial<BigRational>> gb;
376        long t;
377
378        bb = GBAlgorithmBuilder.<BigRational> polynomialRing(pf).normalPairlist().build();
379        t = System.currentTimeMillis();
380        gb = bb.GB(cp);
381        t = System.currentTimeMillis() - t;
382        //System.out.println("time(gb) = " + t);
383        assertTrue("isGB: ", bb.isGB(gb));
384
385        bb = GBAlgorithmBuilder.<BigRational> polynomialRing(pf).syzygyPairlist().build();
386        t = System.currentTimeMillis();
387        gb = bb.GB(cp);
388        t = System.currentTimeMillis() - t;
389        //System.out.println("time(gb) = " + t);
390        assertTrue("isGB: ", bb.isGB(gb));
391
392        bb = GBAlgorithmBuilder.<BigRational> polynomialRing(pf).simplePairlist().build();
393        t = System.currentTimeMillis();
394        gb = bb.GB(cp);
395        t = System.currentTimeMillis() - t;
396        //System.out.println("time(gb) = " + t);
397        assertTrue("isGB: ", bb.isGB(gb));
398
399        assertTrue("t >= 0: ", t >= 0L); // for findbugs
400    }
401
402
403    /**
404     * Test construction for BigRational and iterate.
405     */
406    public void testConstructionRationalIterate() {
407        BigRational bf = new BigRational(1);
408        String[] vars = new String[] { "a", "b", "c" };
409        GenPolynomialRing<BigRational> pf = new GenPolynomialRing<BigRational>(bf, vars);
410
411        GBAlgorithmBuilder<BigRational> ab = GBAlgorithmBuilder.<BigRational> polynomialRing(pf);
412        //System.out.println("ab = " + ab);
413
414        ab = ab.iterated();
415        //System.out.println("ab = " + ab);
416
417        GroebnerBaseAbstract<BigRational> bb = ab.build();
418        //System.out.println("bb = " + bb);
419        assertTrue("instance of " + bb, bb instanceof GroebnerBaseSeqIter);
420
421        ab = GBAlgorithmBuilder.<BigRational> polynomialRing(pf);
422        ab = ab.iterated().parallel();
423        //System.out.println("ab = " + ab);
424
425        bb = ab.build();
426        //System.out.println("bb = " + bb);
427        assertTrue("instance of " + bb, bb instanceof GBProxy);
428
429        GBProxy<BigRational> bbp = (GBProxy<BigRational>) bb;
430        assertTrue("instance of " + bbp.e1, bbp.e1 instanceof GroebnerBaseSeqIter);
431        assertTrue("instance of " + bbp.e2, bbp.e2 instanceof GroebnerBaseParIter);
432    }
433
434
435    /**
436     * Test construction for BigRational and signature based GBs.
437     */
438    public void testConstructionRationalSignatureBased() {
439        BigRational bf = new BigRational(1);
440        String[] vars = new String[] { "a", "b", "c" };
441        GenPolynomialRing<BigRational> pf = new GenPolynomialRing<BigRational>(bf, vars);
442
443        GBAlgorithmBuilder<BigRational> ab = GBAlgorithmBuilder.<BigRational> polynomialRing(pf);
444        //System.out.println("ab = " + ab);
445
446        ab = ab.F5();
447        //System.out.println("ab = " + ab);
448
449        GroebnerBaseAbstract<BigRational> bb = ab.build();
450        //System.out.println("bb = " + bb);
451        assertTrue("instance of " + bb, bb instanceof GroebnerBaseSigSeqIter);
452        assertTrue("instance of " + bb, bb instanceof GroebnerBaseF5zSigSeqIter);
453
454
455        ab = GBAlgorithmBuilder.<BigRational> polynomialRing(pf);
456        ab = ab.GGV();
457        //System.out.println("ab = " + ab);
458
459        bb = ab.build();
460        //System.out.println("bb = " + bb);
461        assertTrue("instance of " + bb, bb instanceof GroebnerBaseSigSeqIter);
462        assertTrue("instance of " + bb, bb instanceof GroebnerBaseGGVSigSeqIter);
463
464
465        ab = GBAlgorithmBuilder.<BigRational> polynomialRing(pf);
466        ab = ab.Arri();
467        //System.out.println("ab = " + ab);
468
469        bb = ab.build();
470        //System.out.println("bb = " + bb);
471        assertTrue("instance of " + bb, bb instanceof GroebnerBaseSigSeqIter);
472        assertTrue("instance of " + bb, bb instanceof GroebnerBaseArriSigSeqIter);
473    }
474
475}