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