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 * GroebnerBase parallel tests with JUnit.
028 * @author Heinz Kredel
029 */
030
031public class GroebnerBaseParTest 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>GroebnerBaseParTest</CODE> object.
045     * @param name String.
046     */
047    public GroebnerBaseParTest(String name) {
048        super(name);
049    }
050
051
052    /**
053     * suite.
054     */
055    public static Test suite() {
056        TestSuite suite = new TestSuite(GroebnerBaseParTest.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    GroebnerBase<BigRational> bbseq;
071
072
073    GroebnerBase<BigRational> bbpar;
074
075
076    GenPolynomial<BigRational> a, b, c, d, e;
077
078
079    int rl = 3; //4; //3; 
080
081
082    int kl = 10;
083
084
085    int ll = 7;
086
087
088    int el = 3;
089
090
091    float q = 0.2f; //0.4f
092
093
094    int threads = 2;
095
096
097    @Override
098    protected void setUp() {
099        BigRational coeff = new BigRational(9);
100        fac = new GenPolynomialRing<BigRational>(coeff, rl);
101        a = b = c = d = e = null;
102        bbseq = new GroebnerBaseSeq<BigRational>();
103        bbpar = new GroebnerBaseParallel<BigRational>(threads);
104    }
105
106
107    @Override
108    protected void tearDown() {
109        a = b = c = d = e = null;
110        fac = null;
111        bbseq = null;
112        ((GroebnerBaseParallel<BigRational>) bbpar).terminate();
113        bbpar = null;
114    }
115
116
117    /**
118     * Test parallel GBase.
119     */
120    public void testParallelGBase() {
121
122        L = new ArrayList<GenPolynomial<BigRational>>();
123
124        a = fac.random(kl, ll, el, q);
125        b = fac.random(kl, ll, el, q);
126        c = fac.random(kl, ll, el, q);
127        d = fac.random(kl, ll, el, q);
128        e = d; //fac.random(kl, ll, el, q );
129
130        if (a.isZERO() || b.isZERO() || c.isZERO() || d.isZERO()) {
131            return;
132        }
133
134        assertTrue("not isZERO( a )", !a.isZERO());
135        L.add(a);
136
137        L = bbpar.GB(L);
138        assertTrue("isGB( { a } )", bbpar.isGB(L));
139
140        assertTrue("not isZERO( b )", !b.isZERO());
141        L.add(b);
142        //System.out.println("L = " + L.size() );
143
144        L = bbpar.GB(L);
145        assertTrue("isGB( { a, b } )", bbpar.isGB(L));
146
147        assertTrue("not isZERO( c )", !c.isZERO());
148        L.add(c);
149
150        L = bbpar.GB(L);
151        assertTrue("isGB( { a, b, c } )", bbpar.isGB(L));
152
153        assertTrue("not isZERO( d )", !d.isZERO());
154        L.add(d);
155
156        L = bbpar.GB(L);
157        assertTrue("isGB( { a, b, c, d } )", bbpar.isGB(L));
158
159        assertTrue("not isZERO( e )", !e.isZERO());
160        L.add(e);
161
162        L = bbpar.GB(L);
163        assertTrue("isGB( { a, b, c, d, e } )", bbpar.isGB(L));
164    }
165
166
167    /**
168     * Test compare sequential with parallel GBase.
169     */
170    public void testSequentialParallelGBase() {
171
172        List<GenPolynomial<BigRational>> Gs, Gp;
173
174        L = new ArrayList<GenPolynomial<BigRational>>();
175
176        a = fac.random(kl, ll, el, q);
177        b = fac.random(kl, ll, el, q);
178        c = fac.random(kl, ll, el, q);
179        d = fac.random(kl, ll, el, q);
180        e = d; //fac.random(kl, ll, el, q );
181
182        if (a.isZERO() || b.isZERO() || c.isZERO() || d.isZERO()) {
183            return;
184        }
185
186        L.add(a);
187        Gs = bbseq.GB(L);
188        Gp = bbpar.GB(L);
189
190        assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp, Gs.containsAll(Gp));
191        assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp, Gp.containsAll(Gs));
192
193        L = Gs;
194        L.add(b);
195        Gs = bbseq.GB(L);
196        Gp = bbpar.GB(L);
197
198        assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp, Gs.containsAll(Gp));
199        assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp, Gp.containsAll(Gs));
200
201        L = Gs;
202        L.add(c);
203        Gs = bbseq.GB(L);
204        Gp = bbpar.GB(L);
205
206        assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp, Gs.containsAll(Gp));
207        assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp, Gp.containsAll(Gs));
208
209        L = Gs;
210        L.add(d);
211        Gs = bbseq.GB(L);
212        Gp = bbpar.GB(L);
213
214        assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp, Gs.containsAll(Gp));
215        assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp, Gp.containsAll(Gs));
216
217        L = Gs;
218        L.add(e);
219        Gs = bbseq.GB(L);
220        Gp = bbpar.GB(L);
221
222        assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp, Gs.containsAll(Gp));
223        assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp, Gp.containsAll(Gs));
224    }
225
226
227    /**
228     * Test Trinks7 GBase.
229     */
230    @SuppressWarnings("unchecked")
231    public void testTrinks7GBase() {
232        String exam = "(B,S,T,Z,P,W) L " + "( " + "( 45 P + 35 S - 165 B - 36 ), "
233                        + "( 35 P + 40 Z + 25 T - 27 S ), " + "( 15 W + 25 S P + 30 Z - 18 T - 165 B**2 ), "
234                        + "( - 9 W + 15 T P + 20 S Z ), " + "( P W + 2 T Z - 11 B**3 ), "
235                        + "( 99 W - 11 B S + 3 B**2 ), " + "( B**2 + 33/50 B + 2673/10000 ) " + ") ";
236        Reader source = new StringReader(exam);
237        GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source);
238        try {
239            F = (PolynomialList<BigRational>) parser.nextPolynomialSet();
240        } catch (ClassCastException e) {
241            fail("" + e);
242        } catch (IOException e) {
243            fail("" + e);
244        }
245        //System.out.println("F = " + F);
246
247        G = bbpar.GB(F.list);
248        assertTrue("isGB( GB(Trinks7) )", bbpar.isGB(G));
249        assertEquals("#GB(Trinks7) == 6", 6, G.size());
250        //PolynomialList<BigRational> trinks = new PolynomialList<BigRational>(F.ring,G);
251        //System.out.println("G = " + trinks);
252    }
253}