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