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;
013import java.util.Random;
014
015import junit.framework.Test;
016import junit.framework.TestCase;
017import junit.framework.TestSuite;
018
019
020import edu.jas.arith.BigRational;
021import edu.jas.poly.GenPolynomial;
022import edu.jas.poly.GenPolynomialRing;
023import edu.jas.poly.GenPolynomialTokenizer;
024import edu.jas.poly.OrderedPolynomialList;
025import edu.jas.poly.PolynomialList;
026import edu.jas.poly.TermOrderByName;
027
028
029/**
030 * Groebner base signature based sequential iterative GB tests with JUnit.
031 * @author Heinz Kredel
032 */
033
034public class GroebnerBaseSigSeqIterTest extends TestCase {
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>GroebnerBaseSigSeqIterTest</CODE> object.
047     * @param name String.
048     */
049    public GroebnerBaseSigSeqIterTest(String name) {
050        super(name);
051    }
052
053
054    /**
055     * suite.
056     */
057    public static Test suite() {
058        TestSuite suite = new TestSuite(GroebnerBaseSigSeqIterTest.class);
059        return suite;
060    }
061
062
063    GenPolynomialRing<BigRational> fac;
064
065
066    List<GenPolynomial<BigRational>> L, G, Gp;
067
068
069    PolynomialList<BigRational> F;
070
071
072    GroebnerBaseAbstract<BigRational> bb, bbsig, bbggv, bbarri, bbf5z;
073
074
075    GenPolynomial<BigRational> a, b, c, d, e;
076
077
078    int rl = 4; //4; //3; 
079
080
081    int kl = 3; // 10
082
083
084    int ll = 5;
085
086
087    int el = 3; // 4
088
089
090    float q = 0.2f; //0.4f
091
092
093    @Override
094    protected void setUp() {
095        BigRational coeff = new BigRational(9);
096        String[] vars = new String[] { "u", "x", "y", "z" };
097        fac = new GenPolynomialRing<BigRational>(coeff, vars, TermOrderByName.IGRLEX);
098        a = b = c = d = e = null;
099        bb = new GroebnerBaseSeqIter<BigRational>();
100        bbsig = new GroebnerBaseSigSeqIter<BigRational>();
101        bbggv = new GroebnerBaseGGVSigSeqIter<BigRational>();
102        bbarri = new GroebnerBaseArriSigSeqIter<BigRational>();
103        bbf5z = new GroebnerBaseF5zSigSeqIter<BigRational>();
104    }
105
106
107    @Override
108    protected void tearDown() {
109        a = b = c = d = e = null;
110        fac = null;
111        bb = null;
112    }
113
114
115    /**
116     * Test sequential GBase.
117     */
118    public void testSequentialGBase() {
119        L = new ArrayList<GenPolynomial<BigRational>>();
120
121        a = fac.parse("x^4 + 4/5 x^2 - 12/25 u * x - 183/175");
122        b = fac.parse("x^3 * y + 40/7 x^3 + 4/5 x * y - 12/25 u * y + 183/2450 u^2 + 32/7 x - 96/35 u");
123        c = fac.parse("u^2 * x + 14 y + 80");
124        d = fac.parse("y^2 - 5/4 x^2 - 1");
125        e = fac.parse("z");
126
127        int x = (new Random()).nextInt(4);
128        switch (x) {
129        case 0:
130            bb = bbf5z;
131            break;
132        case 1:
133            bb = bbggv;
134            break;
135        case 2:
136            bb = bbarri;
137            break;
138        default:
139            break;
140        }
141
142        L.add(a);
143        L.add(b);
144        L.add(c);
145        L.add(d);
146        L.add(e);
147        L = bb.GB(L);
148        assertTrue("isGB( { a, b, c, d, e } ): " + L, bb.isGB(L));
149        //System.out.println("L = " + L);
150    }
151
152
153    /**
154     * Test random sequential GBase.
155     */
156    public void testRandomSequentialGBase() {
157        L = new ArrayList<GenPolynomial<BigRational>>();
158
159        a = fac.random(kl, ll, el, q);
160        b = fac.random(kl, ll, el, q);
161        c = fac.univariate(0); //fac.random(kl, ll, el, q);
162        d = fac.random(kl, ll, el, q);
163        e = d; //fac.random(kl, ll, el, q );
164
165        int x = (new Random()).nextInt(4);
166        switch (x) {
167        case 0:
168            bb = bbf5z;
169            break;
170        case 1:
171            bb = bbggv;
172            break;
173        case 2:
174            bb = bbarri;
175            break;
176        default:
177            break;
178        }
179
180        L.add(a);
181        L = bb.GB(L);
182        assertTrue("isGB( { a } ): " + L, bb.isGB(L));
183
184        L.add(b);
185        //System.out.println("L = " + L.size() );
186        L = bb.GB(L);
187        assertTrue("isGB( { a, b } ): " + L, bb.isGB(L));
188
189        L.add(c);
190        L = bb.GB(L);
191        assertTrue("isGB( { a, b, c } ): " + L, bb.isGB(L));
192
193        L.add(d);
194        L = bb.GB(L);
195        assertTrue("isGB( { a, b, c, d } ): " + L, bb.isGB(L));
196
197        L.add(e);
198        L = bb.GB(L);
199        assertTrue("isGB( { a, b, c, d, e } ): " + L, bb.isGB(L));
200        //System.out.println("L = " + L);
201    }
202
203
204    /**
205     * Test Trinks7 GBase.
206     */
207    @SuppressWarnings({ "unchecked", "cast" })
208    public void testTrinks7GBase() {
209        String exam = "(B,S,T,Z,P,W) L " + "( "
210                        + "( P W + 2 T Z - 11 B**3 ), "
211                        //+ "( B**2 + 33/50 B + 2673/10000 ) " 
212                        + "( 45 P + 35 S - 165 B - 36 ), " + "( 35 P + 40 Z + 25 T - 27 S ), "
213                        + "( 15 W + 25 S P + 30 Z - 18 T - 165 B**2 ), " + "( - 9 W + 15 T P + 20 S Z ), "
214                        + "( 99 W - 11 B S + 3 B**2 ), " + ") ";
215
216        Reader source = new StringReader(exam);
217        GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source);
218        try {
219            F = (PolynomialList<BigRational>) parser.nextPolynomialSet();
220        } catch (ClassCastException e) {
221            fail("" + e);
222        } catch (IOException e) {
223            fail("" + e);
224        }
225        //System.out.println("F = " + F);
226
227        G = bb.GB(F.list);
228        long t1 = System.currentTimeMillis();
229        G = bb.GB(F.list);
230        t1 = System.currentTimeMillis() - t1;
231        assertTrue("isGB( GB(Trinks7) )", bb.isGB(G));
232        assertEquals("#GB(Trinks7) == 6", 6, G.size());
233        G = OrderedPolynomialList.<BigRational> sort(G);
234
235        long t2 = System.currentTimeMillis();
236        Gp = G; //bbsig.GB(F.list);
237        t2 = System.currentTimeMillis() - t2;
238        assertTrue("isGB( GB(Trinks7) )", bb.isGB(Gp));
239        assertEquals("#GB(Trinks7) == 6", 6, Gp.size());
240        Gp = OrderedPolynomialList.<BigRational> sort(Gp);
241        assertEquals("GB == GBp", G, Gp);
242
243        Gp = bbf5z.GB(F.list);
244        long t5 = System.currentTimeMillis();
245        Gp = bbf5z.GB(F.list);
246        t5 = System.currentTimeMillis() - t5;
247        assertTrue("isGB( GB(Trinks7) )", bb.isGB(Gp));
248        assertEquals("#GB(Trinks7) == 6", 6, Gp.size());
249        Gp = OrderedPolynomialList.<BigRational> sort(Gp);
250        assertEquals("GB == GBp", G, Gp);
251
252        Gp = bbarri.GB(F.list);
253        long t4 = System.currentTimeMillis();
254        Gp = bbarri.GB(F.list);
255        t4 = System.currentTimeMillis() - t4;
256        assertTrue("isGB( GB(Trinks7) )", bb.isGB(Gp));
257        assertEquals("#GB(Trinks7) == 6", 6, Gp.size());
258        Gp = OrderedPolynomialList.<BigRational> sort(Gp);
259        assertEquals("GB == GBp", G, Gp);
260
261        Gp = bbggv.GB(F.list);
262        long t3 = System.currentTimeMillis();
263        Gp = bbggv.GB(F.list);
264        t3 = System.currentTimeMillis() - t3;
265        assertTrue("isGB( GB(Trinks7) )", bb.isGB(Gp));
266        assertEquals("#GB(Trinks7) == 6", 6, Gp.size());
267        Gp = OrderedPolynomialList.<BigRational> sort(Gp);
268        assertEquals("GB == GBp", G, Gp);
269
270        //System.out.println("G = " + G);
271        //System.out.println("iter  executed in " + t1 + " milliseconds");
272        ////System.out.println("sig   executed in " + t2 + " milliseconds");
273        //System.out.println("ggv   executed in " + t3 + " milliseconds");
274        //System.out.println("arris executed in " + t4 + " milliseconds");
275        //System.out.println("f5z   executed in " + t5 + " milliseconds");
276        long t = t1 + t2 + t3 + t4 + t5;
277        assertTrue("times >= 0: " + t, t >= 0); //findbugs and compiler
278
279        assertTrue("isGB( GB(Trinks7) )", bb.isGB(G));
280        assertEquals("#GB(Trinks7) == 6", 6, G.size());
281        //PolynomialList<BigRational> trinks = new PolynomialList<BigRational>(F.ring,G);
282        //System.out.println("G = " + trinks);
283    }
284
285}