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