001/*
002 * $Id: GroebnerBaseSeqPairParTest.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, sequential pair list, tests with JUnit.
030 * @author Heinz Kredel
031 */
032
033public class GroebnerBaseSeqPairParTest 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>GroebnerBaseSeqPairParTest</CODE> object.
047     * @param name String.
048     */
049    public GroebnerBaseSeqPairParTest(String name) {
050        super(name);
051    }
052
053
054    /**
055     * suite.
056     */
057    public static Test suite() {
058        TestSuite suite = new TestSuite(GroebnerBaseSeqPairParTest.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 GroebnerBaseSeqPairParallel<BigRational>(threads);
125    }
126
127
128    @Override
129    protected void tearDown() {
130        a = b = c = d = e = null;
131        fac = null;
132        bbseq = null;
133        bbpar.terminate();
134        bbpar = null;
135        bbspar.terminate();
136        bbspar = null;
137    }
138
139
140    /**
141     * Test parallel GBase.
142     * 
143     */
144    public void testSeqPairParallelGBase() {
145
146        L = new ArrayList<GenPolynomial<BigRational>>();
147
148        a = fac.random(kl, ll, el, q);
149        b = fac.random(kl, ll, el, q);
150        c = fac.random(kl, ll, el, q);
151        d = fac.random(kl, ll, el, q);
152        e = d; //fac.random(kl, ll, el, q );
153
154        if (a.isZERO() || b.isZERO() || c.isZERO() || d.isZERO()) {
155            return;
156        }
157
158        assertTrue("not isZERO( a )", !a.isZERO());
159        L.add(a);
160
161        L = bbspar.GB(L);
162        assertTrue("isGB( { a } )", bbspar.isGB(L));
163
164        assertTrue("not isZERO( b )", !b.isZERO());
165        L.add(b);
166        //System.out.println("L = " + L.size() );
167
168        L = bbspar.GB(L);
169        assertTrue("isGB( { a, b } )", bbspar.isGB(L));
170
171        assertTrue("not isZERO( c )", !c.isZERO());
172        L.add(c);
173
174        L = bbspar.GB(L);
175        assertTrue("isGB( { a, b, c } )", bbspar.isGB(L));
176
177        assertTrue("not isZERO( d )", !d.isZERO());
178        L.add(d);
179
180        L = bbspar.GB(L);
181        assertTrue("isGB( { a, b, c, d } )", bbspar.isGB(L));
182
183        assertTrue("not isZERO( e )", !e.isZERO());
184        L.add(e);
185
186        L = bbspar.GB(L);
187        assertTrue("isGB( { a, b, c, d, e } )", bbspar.isGB(L));
188    }
189
190
191    /**
192     * Test compare sequential with parallel GBase.
193     * 
194     */
195    public void testSequentialSeqPairParallelGBase() {
196
197        List<GenPolynomial<BigRational>> Gs, Gp;
198
199        L = new ArrayList<GenPolynomial<BigRational>>();
200
201        a = fac.random(kl, ll, el, q);
202        b = fac.random(kl, ll, el, q);
203        c = fac.random(kl, ll, el, q);
204        d = fac.random(kl, ll, el, q);
205        e = d; //fac.random(kl, ll, el, q );
206
207        if (a.isZERO() || b.isZERO() || c.isZERO() || d.isZERO()) {
208            return;
209        }
210
211        L.add(a);
212        Gs = bbseq.GB(L);
213        Gp = bbspar.GB(L);
214
215        assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp, Gs.containsAll(Gp));
216        assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp, Gp.containsAll(Gs));
217
218        L = Gs;
219        L.add(b);
220        Gs = bbseq.GB(L);
221        Gp = bbspar.GB(L);
222
223        assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp, Gs.containsAll(Gp));
224        assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp, Gp.containsAll(Gs));
225
226        L = Gs;
227        L.add(c);
228        Gs = bbseq.GB(L);
229        Gp = bbspar.GB(L);
230
231        assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp, Gs.containsAll(Gp));
232        assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp, Gp.containsAll(Gs));
233
234        L = Gs;
235        L.add(d);
236        Gs = bbseq.GB(L);
237        Gp = bbspar.GB(L);
238
239        assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp, Gs.containsAll(Gp));
240        assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp, Gp.containsAll(Gs));
241
242        L = Gs;
243        L.add(e);
244        Gs = bbseq.GB(L);
245        Gp = bbspar.GB(L);
246
247        assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp, Gs.containsAll(Gp));
248        assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp, Gp.containsAll(Gs));
249    }
250
251
252    /**
253     * Test compare parallel with sequential pair parallel GBase.
254     * 
255     */
256    public void testParallelSeqPairParallelGBase() {
257
258        List<GenPolynomial<BigRational>> Gs, Gp;
259
260        L = new ArrayList<GenPolynomial<BigRational>>();
261
262        a = fac.random(kl, ll, el, q);
263        b = fac.random(kl, ll, el, q);
264        c = fac.random(kl, ll, el, q);
265        d = fac.random(kl, ll, el, q);
266        e = d; //fac.random(kl, ll, el, q );
267
268        if (a.isZERO() || b.isZERO() || c.isZERO() || d.isZERO()) {
269            return;
270        }
271
272        L.add(a);
273        Gs = bbpar.GB(L);
274        Gp = bbspar.GB(L);
275
276        assertTrue("Gs.containsAll(Gp) " + Gs + ", " + Gp, Gs.containsAll(Gp));
277        assertTrue("Gp.containsAll(Gs) " + Gs + ", " + Gp, Gp.containsAll(Gs));
278
279        L = Gs;
280        L.add(b);
281        Gs = bbpar.GB(L);
282        Gp = bbspar.GB(L);
283
284        assertTrue("Gs.containsAll(Gp) " + Gs + ", " + Gp, Gs.containsAll(Gp));
285        assertTrue("Gp.containsAll(Gs) " + Gs + ", " + Gp, Gp.containsAll(Gs));
286
287        L = Gs;
288        L.add(c);
289        Gs = bbpar.GB(L);
290        Gp = bbspar.GB(L);
291
292        assertTrue("Gs.containsAll(Gp) " + Gs + ", " + Gp, Gs.containsAll(Gp));
293        assertTrue("Gp.containsAll(Gs) " + Gs + ", " + Gp, Gp.containsAll(Gs));
294
295        L = Gs;
296        L.add(d);
297        Gs = bbpar.GB(L);
298        Gp = bbspar.GB(L);
299
300        assertTrue("Gs.containsAll(Gp) " + Gs + ", " + Gp, Gs.containsAll(Gp));
301        assertTrue("Gp.containsAll(Gs) " + Gs + ", " + Gp, Gp.containsAll(Gs));
302
303        L = Gs;
304        L.add(e);
305        Gs = bbpar.GB(L);
306        Gp = bbspar.GB(L);
307
308        assertTrue("Gs.containsAll(Gp) " + Gs + ", " + Gp, Gs.containsAll(Gp));
309        assertTrue("Gp.containsAll(Gs) " + Gs + ", " + Gp, Gp.containsAll(Gs));
310    }
311
312
313    /**
314     * Test Trinks7 GBase.
315     * 
316     */
317    @SuppressWarnings("unchecked")
318    public void testTrinks7GBase() {
319        String exam = "(B,S,T,Z,P,W) L " + "( " + "( 45 P + 35 S - 165 B - 36 ), "
320                        + "( 35 P + 40 Z + 25 T - 27 S ), " + "( 15 W + 25 S P + 30 Z - 18 T - 165 B**2 ), "
321                        + "( - 9 W + 15 T P + 20 S Z ), " + "( P W + 2 T Z - 11 B**3 ), "
322                        + "( 99 W - 11 B S + 3 B**2 ) " + ", ( B**2 + 33/50 B + 2673/10000 ) " + ") ";
323        Reader source = new StringReader(exam);
324        GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source);
325        try {
326            F = (PolynomialList<BigRational>) parser.nextPolynomialSet();
327        } catch (ClassCastException e) {
328            fail("" + e);
329        } catch (IOException e) {
330            fail("" + e);
331        }
332        //System.out.println("F = " + F);
333
334        long t;
335        /*     
336               t = System.currentTimeMillis();
337               G = bbseq.GB( F.list );
338               t = System.currentTimeMillis() - t;
339               System.out.println("bbseq ms = " + t);     
340               t = System.currentTimeMillis();
341               G = bbpar.GB( F.list );
342               t = System.currentTimeMillis() - t;
343               System.out.println("bbpar ms = " + t);     
344        */
345        t = System.currentTimeMillis();
346        G = bbspar.GB(F.list);
347        t = System.currentTimeMillis() - t;
348        //System.out.println("bbspar ms = " + t);     
349        assertTrue("nonsense ", t >= 0L);
350
351        assertTrue("isGB( GB(Trinks7) )", bbspar.isGB(G));
352        assertEquals("#GB(Trinks7) == 6", 6, G.size());
353        //PolynomialList<BigRational> trinks = new PolynomialList<BigRational>(F.ring,G);
354        //System.out.println("G = " + trinks);
355    }
356
357}