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