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