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