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