001/*
002 * $Id: GroebnerBaseRationalTest.java 5048 2014-12-30 17:45:01Z kredel $
003 */
004
005package edu.jas.gbufd;
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
018import org.apache.log4j.BasicConfigurator;
019import org.apache.log4j.Logger;
020
021import edu.jas.arith.BigRational;
022import edu.jas.gb.GroebnerBaseAbstract;
023import edu.jas.gb.GroebnerBaseSeq;
024import edu.jas.poly.GenPolynomial;
025import edu.jas.poly.GenPolynomialRing;
026import edu.jas.poly.GenPolynomialTokenizer;
027import edu.jas.poly.PolynomialList;
028
029
030/**
031 * Groebner base sequential rational fraction free tests with JUnit.
032 * @author Heinz Kredel.
033 */
034
035public class GroebnerBaseRationalTest extends TestCase {
036
037
038    private static final Logger logger = Logger.getLogger(GroebnerBaseRationalTest.class);
039
040
041    /**
042     * main
043     */
044    public static void main(String[] args) {
045        BasicConfigurator.configure();
046        junit.textui.TestRunner.run(suite());
047    }
048
049
050    /**
051     * Constructs a <CODE>GroebnerBaseRationalTest</CODE> object.
052     * @param name String.
053     */
054    public GroebnerBaseRationalTest(String name) {
055        super(name);
056    }
057
058
059    /**
060     * suite.
061     */
062    public static Test suite() {
063        TestSuite suite = new TestSuite(GroebnerBaseRationalTest.class);
064        return suite;
065    }
066
067
068    GenPolynomialRing<BigRational> fac;
069
070
071    List<GenPolynomial<BigRational>> L, Lp;
072
073
074    PolynomialList<BigRational> F;
075
076
077    List<GenPolynomial<BigRational>> G, Gp;
078
079
080    GroebnerBaseAbstract<BigRational> bb;
081
082
083    GroebnerBaseAbstract<BigRational> bbp;
084
085
086    GenPolynomial<BigRational> a, b, c, d, e;
087
088
089    int threads = 2;
090
091
092    int rl = 4; //4; //3; 
093
094
095    int kl = 7; // 10
096
097
098    int ll = 7;
099
100
101    int el = 3; // 4
102
103
104    float q = 0.2f; //0.4f
105
106
107    @Override
108    protected void setUp() {
109        BigRational coeff = new BigRational(9);
110        fac = new GenPolynomialRing<BigRational>(coeff, rl);
111        a = b = c = d = e = null;
112        bb = new GroebnerBaseRational<BigRational>();
113        bbp = new GroebnerBaseRational<BigRational>(threads);
114    }
115
116
117    @Override
118    protected void tearDown() {
119        bbp.terminate();
120        a = b = c = d = e = null;
121        fac = null;
122        bb = null;
123        bbp = null;
124    }
125
126
127    /**
128     * Test sequential GBase.
129     */
130    public void testSequentialGBase() {
131        L = new ArrayList<GenPolynomial<BigRational>>();
132
133        a = fac.random(kl, ll, el, q);
134        b = fac.random(kl, ll, el, q);
135        c = fac.random(kl, ll, el, q);
136        d = fac.random(kl, ll, el, q);
137        e = d; //fac.random(kl, ll, el, q );
138
139        if (a.isZERO() || b.isZERO() || c.isZERO() || d.isZERO()) {
140            return;
141        }
142
143        assertTrue("not isZERO( a )", !a.isZERO());
144        L.add(a);
145
146        L = bb.GB(L);
147        assertTrue("isGB( { a } )", bb.isGB(L));
148        assertTrue("isMinimalGB( { a } )", bb.isMinimalGB(L));
149
150        assertTrue("not isZERO( b )", !b.isZERO());
151        L.add(b);
152        //System.out.println("L = " + L.size() );
153
154        L = bb.GB(L);
155        assertTrue("isGB( { a, b } )", bb.isGB(L));
156        assertTrue("isMinimalGB( { a, b } )", bb.isMinimalGB(L));
157
158        assertTrue("not isZERO( c )", !c.isZERO());
159        L.add(c);
160
161        L = bb.GB(L);
162        assertTrue("isGB( { a, b, c } )", bb.isGB(L));
163        assertTrue("isMinimalGB( { a, b, c } )", bb.isMinimalGB(L));
164
165        assertTrue("not isZERO( d )", !d.isZERO());
166        L.add(d);
167
168        L = bb.GB(L);
169        assertTrue("isGB( { a, b, c, d } )", bb.isGB(L));
170        assertTrue("isMinimalGB( { a, b, c, d } )", bb.isMinimalGB(L));
171
172        assertTrue("not isZERO( e )", !e.isZERO());
173        L.add(e);
174
175        L = bb.GB(L);
176        assertTrue("isGB( { a, b, c, d, e } )", bb.isGB(L));
177        assertTrue("isMinimalGB( { a, b, c, d, e } )", bb.isMinimalGB(L));
178    }
179
180
181    /**
182     * Test parallel GBase.
183     */
184    public void testParallelGBase() {
185        L = new ArrayList<GenPolynomial<BigRational>>();
186
187        a = fac.random(kl, ll, el, q);
188        b = fac.random(kl, ll, el, q);
189        c = fac.random(kl, ll, el, q);
190        d = fac.random(kl, ll, el, q);
191        e = d; //fac.random(kl, ll, el, q );
192
193        if (a.isZERO() || b.isZERO() || c.isZERO() || d.isZERO()) {
194            return;
195        }
196
197        assertTrue("not isZERO( a )", !a.isZERO());
198        L.add(a);
199
200        L = bbp.GB(L);
201        assertTrue("isGB( { a } )", bbp.isGB(L));
202        assertTrue("isMinimalGB( { a } )", bbp.isMinimalGB(L));
203
204        assertTrue("not isZERO( b )", !b.isZERO());
205        L.add(b);
206        //System.out.println("L = " + L.size() );
207
208        L = bbp.GB(L);
209        assertTrue("isGB( { a, b } )", bbp.isGB(L));
210        assertTrue("isMinimalGB( { a, b } )", bbp.isMinimalGB(L));
211
212        assertTrue("not isZERO( c )", !c.isZERO());
213        L.add(c);
214
215        L = bbp.GB(L);
216        assertTrue("isGB( { a, b, c } )", bbp.isGB(L));
217        assertTrue("isMinimalGB( { a, b, c } )", bbp.isMinimalGB(L));
218
219        assertTrue("not isZERO( d )", !d.isZERO());
220        L.add(d);
221
222        L = bbp.GB(L);
223        assertTrue("isGB( { a, b, c, d } )", bbp.isGB(L));
224        assertTrue("isMinimalGB( { a, b, c, d } )", bbp.isMinimalGB(L));
225
226        assertTrue("not isZERO( e )", !e.isZERO());
227        L.add(e);
228
229        L = bbp.GB(L);
230        assertTrue("isGB( { a, b, c, d, e } )", bbp.isGB(L));
231        assertTrue("isMinimalGB( { a, b, c, d, e } )", bbp.isMinimalGB(L));
232    }
233
234
235    /**
236     * Test Trinks7 GBase.
237     */
238    @SuppressWarnings("cast")
239    public void testTrinks7GBase() {
240        String exam = "(B,S,T,Z,P,W) L " + "( " + "( 45 P + 35 S - 165 B - 36 ), "
241                        + "( 35 P + 40 Z + 25 T - 27 S ), " + "( 15 W + 25 S P + 30 Z - 18 T - 165 B**2 ), "
242                        + "( - 9 W + 15 T P + 20 S Z ), " + "( P W + 2 T Z - 11 B**3 ), "
243                        + "( 99 W - 11 B S + 3 B**2 ), " + "( B**2 + 33/50 B + 2673/10000 ) " + ") ";
244        @SuppressWarnings("unused")
245        String exam2 = "(x,y,z) L " + "( " + "( z y**2 + 2 x + 1/2 )" + "( z x**2 - y**2 - 1/2 x )"
246                        + "( -z + y**2 x + 4 x**2 + 1/4 )" + " )";
247
248        Reader source = new StringReader(exam);
249        GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source);
250        try {
251            F = (PolynomialList<BigRational>) parser.nextPolynomialSet();
252        } catch (ClassCastException e) {
253            fail("" + e);
254        } catch (IOException e) {
255            fail("" + e);
256        }
257        //System.out.println("F = " + F);
258
259        G = bb.GB(F.list);
260        assertTrue("isGB( GB(Trinks7) )", bb.isGB(G));
261        assertTrue("isMinimalGB( GB(Trinks7) )", bb.isMinimalGB(G));
262        assertEquals("#GB(Trinks7) == 6", 6, G.size());
263        Gp = bbp.GB(F.list);
264        assertTrue("isGB( GB(Trinks7) )", bb.isGB(Gp));
265        assertTrue("isMinimalGB( GB(Trinks7) )", bb.isMinimalGB(Gp));
266        assertEquals("#GB(Trinks7) == 6", 6, Gp.size());
267        assertEquals("G == Gp: ", G, Gp);
268        //PolynomialList<BigRational> trinks = new PolynomialList<BigRational>(F.ring,G);
269        //System.out.println("G = " + trinks);
270    }
271
272
273    /**
274     * Test Trinks7 compare GBase.
275     */
276    @SuppressWarnings("cast")
277    public void testTrinks7CompareGBase() {
278        String exam = "(B,S,T,Z,P,W) L " + "( " + "( 45 P + 35 S - 165 B - 36 ), "
279                        + "( 35 P + 40 Z + 25 T - 27 S ), " + "( 15 W + 25 S P + 30 Z - 18 T - 165 B**2 ), "
280                        + "( - 9 W + 15 T P + 20 S Z ), " + "( P W + 2 T Z - 11 B**3 ), "
281                        + "( 99 W - 11 B S + 3 B**2 ), "
282                        //+ "( B**2 + 33/50 B + 2673/10000 ) "
283                        + ") ";
284
285        Reader source = new StringReader(exam);
286        GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source);
287        try {
288            F = (PolynomialList<BigRational>) parser.nextPolynomialSet();
289        } catch (ClassCastException e) {
290            fail("" + e);
291        } catch (IOException e) {
292            fail("" + e);
293        }
294        //System.out.println("F = " + F);
295
296        long i = System.currentTimeMillis();
297        G = bb.GB(F.list);
298        i = System.currentTimeMillis() - i;
299        assertTrue("isGB( GB(Trinks7) )", bb.isGB(G));
300        assertTrue("isMinimalGB( GB(Trinks7) )", bb.isMinimalGB(G));
301        assertEquals("#GB(Trinks7) == 6", 6, G.size());
302
303        long p = System.currentTimeMillis();
304        Gp = bbp.GB(F.list);
305        p = System.currentTimeMillis() - p;
306        assertTrue("isGB( GB(Trinks7) )", bbp.isGB(G));
307        assertTrue("isMinimalGB( GB(Trinks7) )", bbp.isMinimalGB(G));
308        assertEquals("#GB(Trinks7) == 6", 6, Gp.size());
309
310        GroebnerBaseAbstract<BigRational> bbr = new GroebnerBaseSeq<BigRational>();
311        List<GenPolynomial<BigRational>> Gr;
312        long r = System.currentTimeMillis();
313        Gr = bbr.GB(F.list);
314        r = System.currentTimeMillis() - r;
315        assertTrue("isGB( GB(Trinks7) )", bbr.isGB(Gr));
316        assertTrue("isMinimalGB( GB(Trinks7) )", bbr.isMinimalGB(Gr));
317        assertEquals("#GB(Trinks7) == 6", 6, Gr.size());
318
319        if (logger.isInfoEnabled()) {
320            logger.info("time: seq = " + i + ", par = " + p + ", rat = " + r);
321        }
322        assertEquals("GB_r == GB_i", G, Gr);
323        assertEquals("GB_r == GB_p", G, Gp);
324        //PolynomialList<BigRational> trinks = new PolynomialList<BigRational>(F.ring,G);
325        //System.out.println("G = " + trinks);
326    }
327
328}