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