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;
013
014import junit.framework.Test;
015import junit.framework.TestCase;
016import junit.framework.TestSuite;
017
018
019import edu.jas.arith.BigRational;
020import edu.jas.poly.GenPolynomialTokenizer;
021import edu.jas.poly.GenWordPolynomial;
022import edu.jas.poly.GenWordPolynomialRing;
023import edu.jas.poly.PolynomialList;
024import edu.jas.poly.WordFactory;
025
026
027/**
028 * Groebner base sequential tests with JUnit.
029 * @author Heinz Kredel
030 */
031
032public class WordGroebnerBaseSeqTest extends TestCase {
033
034
035
036    /**
037     * main
038     */
039    public static void main(String[] args) {
040        junit.textui.TestRunner.run(suite());
041    }
042
043
044    /**
045     * Constructs a <CODE>WordGroebnerBaseSeqTest</CODE> object.
046     * @param name String.
047     */
048    public WordGroebnerBaseSeqTest(String name) {
049        super(name);
050    }
051
052
053    /**
054     * suite.
055     */
056    public static Test suite() {
057        TestSuite suite = new TestSuite(WordGroebnerBaseSeqTest.class);
058        return suite;
059    }
060
061
062    GenWordPolynomialRing<BigRational> fac;
063
064
065    WordFactory wfac;
066
067
068    List<GenWordPolynomial<BigRational>> L;
069
070
071    PolynomialList<BigRational> F;
072
073
074    List<GenWordPolynomial<BigRational>> G;
075
076
077    WordGroebnerBase<BigRational> bb;
078
079
080    GenWordPolynomial<BigRational> a;
081
082
083    GenWordPolynomial<BigRational> b;
084
085
086    GenWordPolynomial<BigRational> c;
087
088
089    GenWordPolynomial<BigRational> d;
090
091
092    GenWordPolynomial<BigRational> e;
093
094
095    int kl = 3; // 10
096
097
098    int ll = 7;
099
100
101    int el = 4; // 4
102
103
104    @Override
105    protected void setUp() {
106        BigRational coeff = new BigRational(0);
107        wfac = new WordFactory("a");
108        fac = new GenWordPolynomialRing<BigRational>(coeff, wfac);
109        a = b = c = d = e = null;
110        bb = new WordGroebnerBaseSeq<BigRational>();
111    }
112
113
114    @Override
115    protected void tearDown() {
116        a = b = c = d = e = null;
117        fac = null;
118        bb = null;
119    }
120
121
122    /**
123     * Test sequential univariate Word GBase.
124     */
125    public void testSequentialGBase() {
126        L = new ArrayList<GenWordPolynomial<BigRational>>();
127        a = fac.random(kl, ll, el);
128        b = fac.random(kl, ll, el);
129        c = fac.random(kl, ll, el);
130        d = fac.random(kl, ll, el);
131        e = d; //fac.random(kl, ll, el);
132        while (a.isZERO()) {
133            a = fac.random(kl, ll, el);
134        }
135        while (b.isZERO()) {
136            b = fac.random(kl, ll, el);
137        }
138        while (c.isZERO()) {
139            c = fac.random(kl, ll, el);
140        }
141        while (d.isZERO()) {
142            d = fac.random(kl, ll, el);
143        }
144
145        L.add(a);
146        //System.out.println("L = " + L);
147        L = bb.GB(L);
148        assertTrue("isGB( { a } )", bb.isGB(L));
149
150        L.add(a.multiply(b));
151        //System.out.println("L = " + L);
152        L = bb.GB(L);
153        assertTrue("isGB( { a, b } )", bb.isGB(L));
154
155        L.add(a.multiply(c));
156        //System.out.println("L = " + L);
157        L = bb.GB(L);
158        assertTrue("isGB( { a, b, c } )", bb.isGB(L));
159
160        L.add(a.multiply(d));
161        //System.out.println("L = " + L);
162        L = bb.GB(L);
163        assertTrue("isGB( { a, b, c, d } )", bb.isGB(L));
164
165        L.add(e);
166        //System.out.println("L = " + L);
167        L = bb.GB(L);
168        assertTrue("isGB( { a, b, c, d, e } )", bb.isGB(L));
169
170        L.clear();
171        L.add(a);
172        L.add(a.multiply(b));
173        L.add(a.multiply(c));
174        L.add(a.multiply(d));
175        //System.out.println("L = " + L);
176        L = bb.GB(L);
177        assertTrue("isGB( { a, b, c, d } )", bb.isGB(L));
178    }
179
180
181    /**
182     * Test example 1 word GBase.
183     */
184    @SuppressWarnings("unchecked")
185    public void testExample1GBase() {
186        String exam = "(x,y,z) L " + "( " + "( z y**2 + 2 x + 1/2 )" + "( z x**2 - y**2 - 1/2 x )"
187                        + "( -z + y**2 x + 4 x**2 + 1/4 )" + " )";
188
189        Reader source = new StringReader(exam);
190        GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source);
191        try {
192            F = (PolynomialList<BigRational>) parser.nextPolynomialSet();
193        } catch (ClassCastException e) {
194            fail("" + e);
195        } catch (IOException e) {
196            fail("" + e);
197        }
198        //System.out.println("F = " + F);
199
200        fac = new GenWordPolynomialRing(F.ring);
201        //System.out.println("fac = " + fac);
202
203        L = fac.valueOf(F.list);
204        //System.out.println("L = " + L);
205
206        G = bb.GB(L);
207        //System.out.println("G = " + G);
208        assertTrue("isGB( G )", bb.isGB(G));
209    }
210
211
212    /**
213     * Test Trinks7 as non-commutative example word GBase.
214     */
215    @SuppressWarnings("unchecked")
216    public void testTrinks7GBase() {
217        String exam = "(B,S,T,Z,P,W) L " + "( " + "( 45 P + 35 S - 165 B - 36 ), "
218                        + "( 35 P + 40 Z + 25 T - 27 S ), " + "( 15 W + 25 S P + 30 Z - 18 T - 165 B**2 ), "
219                        + "( - 9 W + 15 T P + 20 S Z ), " + "( P W + 2 T Z - 11 B**3 ), "
220                        + "( 99 W - 11 B S + 3 B**2 ), " + "( B**2 + 33/50 B + 2673/10000 ) " // is needed
221                        + ") ";
222
223        Reader source = new StringReader(exam);
224        GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source);
225        try {
226            F = (PolynomialList<BigRational>) parser.nextPolynomialSet();
227        } catch (ClassCastException e) {
228            fail("" + e);
229        } catch (IOException e) {
230            fail("" + e);
231        }
232        //System.out.println("F = " + F);
233
234        fac = new GenWordPolynomialRing(F.ring);
235        //System.out.println("fac = " + fac);
236
237        L = fac.valueOf(F.list);
238        //System.out.println("L = " + L);
239
240        G = bb.GB(L);
241        //System.out.println("G = " + G);
242        assertTrue("isGB( G )", bb.isGB(G));
243        assertTrue("#G == 6", G.size() == 6);
244    }
245
246
247    /**
248     * Test example 2 word GBase.
249     */
250    @SuppressWarnings("unchecked")
251    public void testExample2GBase() {
252        String exam = "(x,y,z) L " + "( " + "( x y - z )" // will not be correct when converted to non-com
253                        + "( y z + 2 x + z )" + "( y z + x )" + " )";
254
255        Reader source = new StringReader(exam);
256        GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source);
257        try {
258            F = (PolynomialList<BigRational>) parser.nextPolynomialSet();
259        } catch (ClassCastException e) {
260            fail("" + e);
261        } catch (IOException e) {
262            fail("" + e);
263        }
264        //System.out.println("F = " + F);
265
266        fac = new GenWordPolynomialRing(F.ring);
267        //System.out.println("fac = " + fac);
268
269        L = fac.valueOf(F.list);
270        //System.out.println("L = " + L);
271
272        G = bb.GB(L);
273        //System.out.println("G = " + G);
274        assertTrue("isGB( G )", bb.isGB(G));
275    }
276
277}