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