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