001/*
002 * $Id$
003 */
004
005package edu.jas.gb;
006
007
008import java.util.ArrayList;
009import java.util.List;
010
011import junit.framework.Test;
012import junit.framework.TestCase;
013import junit.framework.TestSuite;
014
015import org.apache.logging.log4j.Logger;
016import org.apache.logging.log4j.LogManager; 
017
018import edu.jas.arith.BigRational;
019import edu.jas.kern.ComputerThreads;
020import edu.jas.poly.GenSolvablePolynomial;
021import edu.jas.poly.GenSolvablePolynomialRing;
022import edu.jas.poly.ModuleList;
023import edu.jas.poly.PolynomialList;
024import edu.jas.poly.RelationGenerator;
025import edu.jas.poly.RelationTable;
026import edu.jas.poly.TermOrder;
027import edu.jas.poly.WeylRelations;
028
029
030/**
031 * ModSolvableGroebnerBase sequential and parallel tests with JUnit.
032 * @author Heinz Kredel
033 */
034
035public class ModSolvableGroebnerBaseTest extends TestCase {
036
037
038    private static final Logger logger = LogManager.getLogger(ModSolvableGroebnerBaseTest.class);
039
040
041    /**
042     * main.
043     */
044    public static void main(String[] args) {
045        junit.textui.TestRunner.run(suite());
046        ComputerThreads.terminate();
047    }
048
049
050    /**
051     * Constructs a <CODE>ModSolvableGroebnerBaseTest</CODE> object.
052     * @param name String.
053     */
054    public ModSolvableGroebnerBaseTest(String name) {
055        super(name);
056    }
057
058
059    /**
060     * suite.
061     */
062    public static Test suite() {
063        TestSuite suite = new TestSuite(ModSolvableGroebnerBaseTest.class);
064        return suite;
065    }
066
067
068    int port = 4711;
069
070
071    BigRational cfac;
072
073
074    GenSolvablePolynomialRing<BigRational> pfac;
075
076
077    GenSolvablePolynomial<BigRational> a, b, c, d, e;
078
079
080    TermOrder tord;
081
082
083    GenSolvablePolynomial<BigRational> one, zero;
084
085
086    RelationTable<BigRational> table;
087
088
089    List<List<GenSolvablePolynomial<BigRational>>> L;
090
091
092    List<GenSolvablePolynomial<BigRational>> V;
093
094
095    PolynomialList<BigRational> F, G;
096
097
098    ModuleList<BigRational> M, N, K, I;
099
100
101    SolvableGroebnerBaseAbstract<BigRational> sbb;
102
103
104    SolvableReductionAbstract<BigRational> sred;
105
106
107    int rl = 3; //4; //3; 
108
109
110    int kl = 4;
111
112
113    int ll = 3;
114
115
116    int el = 2;
117
118
119    float q = 0.2f; //0.4f
120
121
122    @Override
123    protected void setUp() {
124        cfac = new BigRational(1);
125        tord = new TermOrder();
126        pfac = new GenSolvablePolynomialRing<BigRational>(cfac, rl, tord);
127        if (Math.random() > 0.5) {
128            sbb = new SolvableGroebnerBaseSeq<BigRational>(); //cfac);
129        } else {
130            sbb = new SolvableGroebnerBaseParallel<BigRational>(); //cfac);
131        }
132        sred = new SolvableReductionSeq<BigRational>();
133        logger.info("test with " + sbb.getClass().getSimpleName());
134
135        a = b = c = d = e = null;
136        do {
137            a = pfac.random(kl, ll, el, q);
138            b = pfac.random(kl, ll, el, q);
139            c = pfac.random(kl, ll, el, q);
140            d = pfac.random(kl, ll, el, q);
141        } while (a.isZERO() || b.isZERO() || c.isZERO() || d.isZERO());
142        e = d; // = pfac.random(kl, ll, el, q );
143        one = pfac.getONE();
144        zero = pfac.getZERO();
145    }
146
147
148    @Override
149    protected void tearDown() {
150        a = b = c = d = e = null;
151        one = null;
152        zero = null;
153        sbb.terminate();
154        sbb = null;
155        sred = null;
156    }
157
158
159    /**
160     * Test sequential left GBase.
161     */
162    public void testSequentialLeftModSolvableGB() {
163        L = new ArrayList<List<GenSolvablePolynomial<BigRational>>>();
164
165        V = new ArrayList<GenSolvablePolynomial<BigRational>>();
166        V.add(a);
167        V.add(zero);
168        V.add(one);
169        L.add(V);
170        M = new ModuleList<BigRational>(pfac, L);
171        assertTrue("isLeftGB( { (a,0,1) } )", sbb.isLeftGB(M));
172        //System.out.println("M = " + M );
173
174        N = sbb.leftGB(M);
175        //System.out.println("N = " + N );
176        assertTrue("isLeftGB( { (a,0,1) } )", sbb.isLeftGB(N));
177
178        V = new ArrayList<GenSolvablePolynomial<BigRational>>();
179        V.add(b);
180        V.add(one);
181        V.add(zero);
182        L.add(V);
183        M = new ModuleList<BigRational>(pfac, L);
184        //System.out.println("L = " + L.size() );
185
186        N = sbb.leftGB(M);
187        assertTrue("isLeftGB( { (a,0,1),(b,1,0) } )", sbb.isLeftGB(N));
188        //System.out.println("N = " + N );
189
190        V = new ArrayList<GenSolvablePolynomial<BigRational>>();
191        V.add(c);
192        V.add(zero);
193        V.add(zero);
194        L.add(V);
195        M = new ModuleList<BigRational>(pfac, L);
196        //System.out.println("M = " + M );
197        //System.out.println("L = " + L.size() );
198
199        N = sbb.leftGB(M);
200        assertTrue("isLeftGB( { (a,),(b,),(c,) } )", sbb.isLeftGB(N));
201        //System.out.println("N = " + N );
202
203        V = new ArrayList<GenSolvablePolynomial<BigRational>>();
204        V.add(d);
205        V.add(zero);
206        V.add(zero);
207        L.add(V);
208        M = new ModuleList<BigRational>(pfac, L);
209        //System.out.println("M = " + M );
210        //System.out.println("L = " + L.size() );
211
212        N = sbb.leftGB(M);
213        assertTrue("isLeftGB( { (a,b,c,d) } )", sbb.isLeftGB(N));
214        //System.out.println("N = " + N );
215
216    }
217
218
219    /**
220     * Test sequential left Weyl GBase.
221     */
222    public void testSequentialLeftModSolvableWeylGB() {
223        int rloc = 4;
224        pfac = new GenSolvablePolynomialRing<BigRational>(cfac, rloc, tord);
225        //System.out.println("pfac = " + pfac);
226        //System.out.println("pfac end");
227
228        RelationGenerator<BigRational> wl = new WeylRelations<BigRational>();
229        //System.out.println("wl = ");
230        wl.generate(pfac);
231        //System.out.println("generate = ");
232        table = pfac.table;
233        //System.out.println("table = ");
234        //System.out.println("table = " + table.size());
235
236        do {
237            a = pfac.random(kl, ll, el, q);
238            b = pfac.random(kl, ll, el, q);
239            c = pfac.random(kl, ll, el, q);
240            d = pfac.random(kl, ll, el, q);
241        } while (a.isZERO() || b.isZERO() || c.isZERO() || d.isZERO());
242        e = d; // = pfac.random(kl, ll, el, q );
243        one = pfac.getONE();
244        zero = pfac.getZERO();
245        //System.out.println("a = " + a );
246        //System.out.println("b = " + b );
247        //System.out.println("c = " + c );
248        //System.out.println("d = " + d );
249        //System.out.println("e = " + e );
250
251        L = new ArrayList<List<GenSolvablePolynomial<BigRational>>>();
252
253        V = new ArrayList<GenSolvablePolynomial<BigRational>>();
254        V.add(a);
255        V.add(zero);
256        V.add(one);
257        L.add(V);
258        M = new ModuleList<BigRational>(pfac, L);
259        assertTrue("isLeftGB( { (a,0,1) } )", sbb.isLeftGB(M));
260
261        N = sbb.leftGB(M);
262        assertTrue("isLeftGB( { (a,0,1) } )", sbb.isLeftGB(N));
263
264        V = new ArrayList<GenSolvablePolynomial<BigRational>>();
265        V.add(b);
266        V.add(one);
267        V.add(zero);
268        L.add(V);
269        M = new ModuleList<BigRational>(pfac, L);
270        //System.out.println("L = " + L.size() );
271
272        N = sbb.leftGB(M);
273        assertTrue("isLeftGB( { (a,0,1),(b,1,0) } )", sbb.isLeftGB(N));
274        //System.out.println("N = " + N );
275
276        V = new ArrayList<GenSolvablePolynomial<BigRational>>();
277        V.add(c);
278        V.add(zero);
279        V.add(zero);
280        L.add(V);
281        M = new ModuleList<BigRational>(pfac, L);
282        //System.out.println("M = " + M );
283        //System.out.println("L = " + L.size() );
284
285        N = sbb.leftGB(M);
286        assertTrue("isLeftGB( { (a,),(b,),(c,) } )", sbb.isLeftGB(N));
287        //System.out.println("N = " + N );
288
289        V = new ArrayList<GenSolvablePolynomial<BigRational>>();
290        V.add(d);
291        V.add(zero);
292        V.add(zero);
293        L.add(V);
294        M = new ModuleList<BigRational>(pfac, L);
295        //System.out.println("M = " + M );
296        //System.out.println("L = " + L.size() );
297
298        N = sbb.leftGB(M);
299        assertTrue("isLeftGB( { (a,b,c,d) } )", sbb.isLeftGB(N));
300        //System.out.println("N = " + N );
301    }
302
303
304    /**
305     * Test sequential twosided GBase.
306     */
307    public void testSequentialTSModSolvableGB() {
308        L = new ArrayList<List<GenSolvablePolynomial<BigRational>>>();
309
310        V = new ArrayList<GenSolvablePolynomial<BigRational>>();
311        V.add(a);
312        V.add(zero);
313        V.add(one);
314        L.add(V);
315        M = new ModuleList<BigRational>(pfac, L);
316        assertTrue("isTwosidedGB( { (a,0,1) } )", sbb.isTwosidedGB(M));
317
318        N = sbb.twosidedGB(M);
319        assertTrue("isTwosidedGB( { (a,0,1) } )", sbb.isTwosidedGB(N));
320
321        V = new ArrayList<GenSolvablePolynomial<BigRational>>();
322        V.add(b);
323        V.add(one);
324        V.add(zero);
325        L.add(V);
326        M = new ModuleList<BigRational>(pfac, L);
327        //System.out.println("L = " + L.size() );
328
329        N = sbb.twosidedGB(M);
330        assertTrue("isTwosidedGB( { (a,0,1),(b,1,0) } )", sbb.isTwosidedGB(N));
331        //System.out.println("N = " + N );
332
333        V = new ArrayList<GenSolvablePolynomial<BigRational>>();
334        V.add(c);
335        V.add(zero);
336        V.add(zero);
337        L.add(V);
338        M = new ModuleList<BigRational>(pfac, L);
339        //System.out.println("L = " + L.size() );
340
341        N = sbb.twosidedGB(M);
342        assertTrue("isTwosidedGB( { (a,),(b,),(c,) } )", sbb.isTwosidedGB(N));
343        //System.out.println("N = " + N );
344
345        V = new ArrayList<GenSolvablePolynomial<BigRational>>();
346        V.add(d);
347        V.add(zero);
348        V.add(zero);
349        L.add(V);
350        M = new ModuleList<BigRational>(pfac, L);
351        //System.out.println("L = " + L.size() );
352
353        N = sbb.twosidedGB(M);
354        assertTrue("isTwosidedGB( { (a,b,c,d) } )", sbb.isTwosidedGB(N));
355        //System.out.println("N = " + N );
356    }
357
358
359    /**
360     * Test sequential twosided Weyl GBase.
361     */
362    public void testSequentialTSModSolvableWeylGB() {
363        int rloc = 4;
364        pfac = new GenSolvablePolynomialRing<BigRational>(cfac, rloc, tord);
365        //System.out.println("pfac = " + pfac);
366        //System.out.println("pfac end");
367
368        RelationGenerator<BigRational> wl = new WeylRelations<BigRational>();
369        //System.out.println("wl = ");
370        wl.generate(pfac);
371        //System.out.println("generate = ");
372        table = pfac.table;
373        //System.out.println("table = ");
374        //System.out.println("table = " + table.size());
375
376        do {
377            a = pfac.random(kl, ll, el, q);
378            b = pfac.random(kl, ll, el, q);
379            c = pfac.random(kl, ll, el, q);
380            d = pfac.random(kl, ll, el, q);
381        } while (a.isZERO() || b.isZERO() || c.isZERO() || d.isZERO());
382        e = d; // = pfac.random(kl, ll, el, q );
383        one = pfac.getONE();
384        zero = pfac.getZERO();
385        //System.out.println("a = " + a );
386        //System.out.println("b = " + b );
387        //System.out.println("c = " + c );
388        //System.out.println("d = " + d );
389        //System.out.println("e = " + e );
390
391        L = new ArrayList<List<GenSolvablePolynomial<BigRational>>>();
392
393        V = new ArrayList<GenSolvablePolynomial<BigRational>>();
394        V.add(a);
395        V.add(zero);
396        V.add(one);
397        L.add(V);
398        M = new ModuleList<BigRational>(pfac, L);
399        // not true in general
400        assertTrue("isTwosidedGB( { (a,0,1) } )", sbb.isTwosidedGB(M) || !pfac.isCommutative());
401
402        N = sbb.twosidedGB(M);
403        assertTrue("isTwosidedGB( { (a,0,1) } )", sbb.isTwosidedGB(N));
404
405        V = new ArrayList<GenSolvablePolynomial<BigRational>>();
406        V.add(b);
407        V.add(one);
408        V.add(zero);
409        L.add(V);
410        M = new ModuleList<BigRational>(pfac, L);
411        //System.out.println("L = " + L.size() );
412
413        N = sbb.twosidedGB(M);
414        assertTrue("isTwosidedGB( { (a,0,1),(b,1,0) } )", sbb.isTwosidedGB(N));
415        //System.out.println("N = " + N );
416
417        V = new ArrayList<GenSolvablePolynomial<BigRational>>();
418        V.add(c);
419        V.add(zero);
420        V.add(zero);
421        L.add(V);
422        M = new ModuleList<BigRational>(pfac, L);
423        //System.out.println("M = " + M );
424        //System.out.println("L = " + L.size() );
425
426        N = sbb.twosidedGB(M);
427        assertTrue("isTwosidedGB( { (a,),(b,),(c,) } )", sbb.isTwosidedGB(N));
428        //System.out.println("N = " + N );
429
430        V = new ArrayList<GenSolvablePolynomial<BigRational>>();
431        V.add(d);
432        V.add(zero);
433        V.add(zero);
434        L.add(V);
435        M = new ModuleList<BigRational>(pfac, L);
436        //System.out.println("M = " + M );
437        //System.out.println("L = " + L.size() );
438
439        N = sbb.twosidedGB(M);
440        assertTrue("isTwosidedGB( { (a,b,c,d) } )", sbb.isTwosidedGB(N));
441        //System.out.println("N = " + N );
442    }
443
444
445    /**
446     * Test sequential right GBase.
447     */
448    public void testSequentialRightModSolvableGB() {
449        L = new ArrayList<List<GenSolvablePolynomial<BigRational>>>();
450
451        V = new ArrayList<GenSolvablePolynomial<BigRational>>();
452        V.add(a);
453        V.add(zero);
454        V.add(one);
455        L.add(V);
456        M = new ModuleList<BigRational>(pfac, L);
457        assertTrue("isRightGB( { (a,0,1) } )", sbb.isRightGB(M));
458        //System.out.println("M = " + M );
459
460        N = sbb.rightGB(M);
461        //System.out.println("N = " + N );
462        assertTrue("isRightGB( { (a,0,1) } )", sbb.isRightGB(N));
463
464        V = new ArrayList<GenSolvablePolynomial<BigRational>>();
465        V.add(b);
466        V.add(one);
467        V.add(zero);
468        L.add(V);
469        M = new ModuleList<BigRational>(pfac, L);
470        //System.out.println("L = " + L.size() );
471
472        //System.out.println("M = " + M );
473        N = sbb.rightGB(M);
474        //System.out.println("N = " + N );
475        assertTrue("isRightGB( { (a,0,1),(b,1,0) } )", sbb.isRightGB(N));
476
477        V = new ArrayList<GenSolvablePolynomial<BigRational>>();
478        V.add(c);
479        V.add(zero);
480        V.add(zero);
481        L.add(V);
482        M = new ModuleList<BigRational>(pfac, L);
483        //System.out.println("M = " + M );
484        //System.out.println("L = " + L.size() );
485
486        N = sbb.rightGB(M);
487        assertTrue("isRightGB( { (a,),(b,),(c,) } )", sbb.isRightGB(N));
488        //System.out.println("N = " + N );
489
490        V = new ArrayList<GenSolvablePolynomial<BigRational>>();
491        V.add(d);
492        V.add(zero);
493        V.add(zero);
494        L.add(V);
495        M = new ModuleList<BigRational>(pfac, L);
496        //System.out.println("M = " + M );
497        //System.out.println("L = " + L.size() );
498
499        N = sbb.rightGB(M);
500        assertTrue("isRightGB( { (a,b,c,d) } )", sbb.isRightGB(N));
501        //System.out.println("N = " + N );
502    }
503
504
505    /**
506     * Test sequential right Weyl GBase.
507     */
508    public void testSequentialRightModSolvableWeylGB() {
509        int rloc = 4;
510        pfac = new GenSolvablePolynomialRing<BigRational>(cfac, rloc, tord);
511        //System.out.println("pfac = " + pfac);
512        //System.out.println("pfac end");
513
514        RelationGenerator<BigRational> wl = new WeylRelations<BigRational>();
515        //System.out.println("wl = ");
516        wl.generate(pfac);
517        //System.out.println("generate = ");
518        table = pfac.table;
519        //System.out.println("table = ");
520        //System.out.println("table = " + table.size());
521
522        do {
523            a = pfac.random(kl, ll, el, q);
524            b = pfac.random(kl, ll, el, q);
525            c = pfac.random(kl, ll, el, q);
526            d = pfac.random(kl, ll, el, q);
527        } while (a.isZERO() || b.isZERO() || c.isZERO() || d.isZERO());
528        e = d; // = pfac.random(kl, ll, el, q );
529        one = pfac.getONE();
530        zero = pfac.getZERO();
531        //System.out.println("a = " + a );
532        //System.out.println("b = " + b );
533        //System.out.println("c = " + c );
534        //System.out.println("d = " + d );
535        //System.out.println("e = " + e );
536
537        L = new ArrayList<List<GenSolvablePolynomial<BigRational>>>();
538
539        V = new ArrayList<GenSolvablePolynomial<BigRational>>();
540        V.add(a);
541        V.add(zero);
542        V.add(one);
543        L.add(V);
544        M = new ModuleList<BigRational>(pfac, L);
545        assertTrue("isRightGB( { (a,0,1) } )", sbb.isRightGB(M));
546
547        N = sbb.rightGB(M);
548        assertTrue("isRightGB( { (a,0,1) } )", sbb.isRightGB(N));
549
550        V = new ArrayList<GenSolvablePolynomial<BigRational>>();
551        V.add(b);
552        V.add(one);
553        V.add(zero);
554        L.add(V);
555        M = new ModuleList<BigRational>(pfac, L);
556        //System.out.println("L = " + L.size() );
557
558        //System.out.println("M = " + M );
559        N = sbb.rightGB(M);
560        //System.out.println("N = " + N );
561        assertTrue("isRightGB( { (a,0,1),(b,1,0) } )", sbb.isRightGB(N));
562
563        V = new ArrayList<GenSolvablePolynomial<BigRational>>();
564        V.add(c);
565        V.add(zero);
566        V.add(zero);
567        L.add(V);
568        M = new ModuleList<BigRational>(pfac, L);
569        //System.out.println("M = " + M );
570        //System.out.println("L = " + L.size() );
571
572        N = sbb.rightGB(M);
573        assertTrue("isRightGB( { (a,),(b,),(c,) } )", sbb.isRightGB(N));
574        //System.out.println("N = " + N );
575
576        V = new ArrayList<GenSolvablePolynomial<BigRational>>();
577        V.add(d);
578        V.add(zero);
579        V.add(zero);
580        L.add(V);
581        M = new ModuleList<BigRational>(pfac, L);
582        //System.out.println("M = " + M );
583        //System.out.println("L = " + L.size() );
584
585        N = sbb.rightGB(M);
586        assertTrue("isRightGB( { (a,b,c,d) } )", sbb.isRightGB(N));
587        //System.out.println("N = " + N );
588    }
589
590
591    /**
592     * Test sequential left GBase with TOP and POT term order.
593     */
594    public void testSequentialModTOPleftGB() {
595        //System.out.println("a = " + a);
596        //System.out.println("b = " + b);
597
598        L = new ArrayList<List<GenSolvablePolynomial<BigRational>>>();
599        V = new ArrayList<GenSolvablePolynomial<BigRational>>();
600        V.add(a);
601        V.add(pfac.getZERO());
602        V.add(pfac.getONE());
603        L.add(V);
604        M = new ModuleList<BigRational>(pfac, L);
605        assertTrue("isGB( { (a,0,1) } )", sbb.isLeftGB(M));
606
607        N = sbb.leftGB(M);
608        assertTrue("is( { (a,0,1) } )", sbb.isLeftGB(N));
609
610        K = sbb.leftGB(M, true);
611        assertTrue("is( { (a,0,1) } )", sbb.isLeftGB(K, true));
612        assertEquals("N == K", N, K);
613
614        V = new ArrayList<GenSolvablePolynomial<BigRational>>();
615        V.add(b);
616        V.add(pfac.getONE());
617        V.add(pfac.getZERO());
618        L.add(V);
619        M = new ModuleList<BigRational>(pfac, L);
620        //System.out.println("M = " + M);
621
622        N = sbb.leftGB(M);
623        assertTrue("isGB( { (a,0,1),(b,1,0) } )", sbb.isLeftGB(N));
624        //System.out.println("N = " + N);
625
626        K = sbb.leftGB(M,true);
627        assertTrue("is( { (a,0,1) } )", sbb.isLeftGB(K, true));
628        //System.out.println("K = " + K);
629
630        I = sred.leftNormalform(N, K);
631        //System.out.println("I = " + I);
632        assertTrue("N.lnf(K) == (0)", I.isZERO());
633
634        I = sred.leftNormalform(K, N, true);
635        //System.out.println("I = " + I);
636        assertTrue("K.lnf(N) == (0)", I.isZERO());
637    }
638
639    
640    /**
641     * Test sequential twosided GBase with TOP and POT term order.
642     */
643    public void testSequentialModTOPtwosidedGB() {
644        //System.out.println("a = " + a);
645        //System.out.println("b = " + b);
646
647        L = new ArrayList<List<GenSolvablePolynomial<BigRational>>>();
648        V = new ArrayList<GenSolvablePolynomial<BigRational>>();
649        V.add(a);
650        V.add(pfac.getZERO());
651        V.add(pfac.getONE());
652        L.add(V);
653        M = new ModuleList<BigRational>(pfac, L);
654        assertTrue("isGB( { (a,0,1) } )", sbb.isTwosidedGB(M));
655
656        N = sbb.twosidedGB(M);
657        assertTrue("is( { (a,0,1) } )", sbb.isTwosidedGB(N));
658
659        K = sbb.twosidedGB(M, true);
660        assertTrue("is( { (a,0,1) } )", sbb.isTwosidedGB(K, true));
661        assertEquals("N == K", N, K);
662
663        V = new ArrayList<GenSolvablePolynomial<BigRational>>();
664        V.add(b);
665        V.add(pfac.getONE());
666        V.add(pfac.getZERO());
667        L.add(V);
668        M = new ModuleList<BigRational>(pfac, L);
669        //System.out.println("M = " + M.toScript());
670
671        N = sbb.twosidedGB(M);
672        assertTrue("isGB( { (a,0,1),(b,1,0) } )", sbb.isTwosidedGB(N));
673        //System.out.println("N = " + N);
674
675        K = sbb.twosidedGB(M,true);
676        assertTrue("is( { (a,0,1) } )", sbb.isTwosidedGB(K, true));
677        //System.out.println("K = " + K);
678
679        I = sred.leftNormalform(N, K);
680        //System.out.println("I = " + I);
681        assertTrue("N.lnf(K) == (0)", I.isZERO());
682
683        I = sred.leftNormalform(K, N, true);
684        //System.out.println("I = " + I);
685        assertTrue("K.lnf(N) == (0)", I.isZERO());
686    }
687
688    
689    /*
690     * No TOP term order for sequential right GBase.
691     */
692
693}