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