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