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