001/*
002 * $Id: GroebnerBaseSeqIterTest.java 5365 2015-12-26 18:35:37Z 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
020import edu.jas.arith.BigRational;
021import edu.jas.poly.GenPolynomial;
022import edu.jas.poly.GenPolynomialRing;
023import edu.jas.poly.GenPolynomialTokenizer;
024import edu.jas.poly.PolynomialList;
025
026
027/**
028 * Groebner base sequential iterative GB tests with JUnit.
029 * @author Heinz Kredel.
030 */
031
032public class GroebnerBaseSeqIterTest extends TestCase {
033
034
035    /**
036     * main
037     */
038    public static void main(String[] args) {
039        BasicConfigurator.configure();
040        junit.textui.TestRunner.run(suite());
041    }
042
043
044    /**
045     * Constructs a <CODE>GroebnerBaseSeqIterTest</CODE> object.
046     * @param name String.
047     */
048    public GroebnerBaseSeqIterTest(String name) {
049        super(name);
050    }
051
052
053    /**
054     * suite.
055     */
056    public static Test suite() {
057        TestSuite suite = new TestSuite(GroebnerBaseSeqIterTest.class);
058        return suite;
059    }
060
061
062    GenPolynomialRing<BigRational> fac;
063
064
065    List<GenPolynomial<BigRational>> L, G;
066
067
068    PolynomialList<BigRational> F;
069
070
071    GroebnerBaseAbstract<BigRational> bb;
072
073
074    GenPolynomial<BigRational> a, b, c, d, e;
075
076
077    int rl = 4; //4; //3; 
078
079
080    int kl = 7; // 10
081
082
083    int ll = 7;
084
085
086    int el = 3; // 4
087
088
089    float q = 0.2f; //0.4f
090
091
092    @Override
093    protected void setUp() {
094        BigRational coeff = new BigRational(9);
095        fac = new GenPolynomialRing<BigRational>(coeff, rl);
096        a = b = c = d = e = null;
097        bb = new GroebnerBaseSeqIter<BigRational>();
098    }
099
100
101    @Override
102    protected void tearDown() {
103        a = b = c = d = e = null;
104        fac = null;
105        bb = null;
106    }
107
108
109    /**
110     * Test sequential GBase.
111     */
112    public void testSequentialGBase() {
113        L = new ArrayList<GenPolynomial<BigRational>>();
114
115        a = fac.random(kl, ll, el, q);
116        b = fac.random(kl, ll, el, q);
117        c = fac.random(kl, ll, el, q);
118        d = fac.random(kl, ll, el, q);
119        e = d; //fac.random(kl, ll, el, q );
120
121        if (a.isZERO() || b.isZERO() || c.isZERO() || d.isZERO()) {
122            return;
123        }
124
125        assertTrue("not isZERO( a )", !a.isZERO());
126        L.add(a);
127
128        L = bb.GB(L);
129        assertTrue("isGB( { a } )", bb.isGB(L));
130
131        assertTrue("not isZERO( b )", !b.isZERO());
132        L.add(b);
133        //System.out.println("L = " + L.size() );
134
135        L = bb.GB(L);
136        assertTrue("isGB( { a, b } )", bb.isGB(L));
137
138        assertTrue("not isZERO( c )", !c.isZERO());
139        L.add(c);
140
141        L = bb.GB(L);
142        assertTrue("isGB( { a, b, c } )", bb.isGB(L));
143
144        assertTrue("not isZERO( d )", !d.isZERO());
145        L.add(d);
146
147        L = bb.GB(L);
148        assertTrue("isGB( { a, b, c, d } )", bb.isGB(L));
149
150        assertTrue("not isZERO( e )", !e.isZERO());
151        L.add(e);
152
153        L = bb.GB(L);
154        assertTrue("isGB( { a, b, c, d, e } )", bb.isGB(L));
155    }
156
157
158    /**
159     * Test Trinks7 GBase.
160     */
161    @SuppressWarnings("cast")
162    public void testTrinks7GBase() {
163        String exam = "(B,S,T,Z,P,W) L " + "( " + "( 45 P + 35 S - 165 B - 36 ), "
164                        + "( 35 P + 40 Z + 25 T - 27 S ), " + "( 15 W + 25 S P + 30 Z - 18 T - 165 B**2 ), "
165                        + "( - 9 W + 15 T P + 20 S Z ), " + "( P W + 2 T Z - 11 B**3 ), "
166                        + "( 99 W - 11 B S + 3 B**2 ), " + "( B**2 + 33/50 B + 2673/10000 ) " + ") ";
167        @SuppressWarnings("unused")
168        String exam2 = "(x,y,z) L " + "( " + "( z y**2 + 2 x + 1/2 )" + "( z x**2 - y**2 - 1/2 x )"
169                        + "( -z + y**2 x + 4 x**2 + 1/4 )" + " )";
170
171        Reader source = new StringReader(exam);
172        GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source);
173        try {
174            F = (PolynomialList<BigRational>) parser.nextPolynomialSet();
175        } catch (ClassCastException e) {
176            fail("" + e);
177        } catch (IOException e) {
178            fail("" + e);
179        }
180        //System.out.println("F = " + F);
181
182        G = bb.GB(F.list);
183        //System.out.println("G = " + G);
184        assertTrue("isGB( GB(Trinks7) )", bb.isGB(G));
185        assertEquals("#GB(Trinks7) == 6", 6, G.size());
186        //PolynomialList<BigRational> trinks = new PolynomialList<BigRational>(F.ring,G);
187        //System.out.println("G = " + trinks);
188    }
189
190
191    /**
192     * Test Trinks7 GBase, syz pair list.
193     */
194    @SuppressWarnings("cast")
195    public void testTrinks7GBaseSyz() {
196        GroebnerBase<BigRational> bbs;
197        bbs = new GroebnerBaseSeqIter<BigRational>(new ReductionSeq<BigRational>(),
198                        new OrderedSyzPairlist<BigRational>());
199
200        String exam = "(B,S,T,Z,P,W) L " + "( " + "( 45 P + 35 S - 165 B - 36 ), "
201                        + "( 35 P + 40 Z + 25 T - 27 S ), " + "( 15 W + 25 S P + 30 Z - 18 T - 165 B**2 ), "
202                        + "( - 9 W + 15 T P + 20 S Z ), " + "( P W + 2 T Z - 11 B**3 ), "
203                        + "( 99 W - 11 B S + 3 B**2 ), " + "( B**2 + 33/50 B + 2673/10000 ) " + ") ";
204        @SuppressWarnings("unused")
205        String exam2 = "(x,y,z) L " + "( " + "( z y**2 + 2 x + 1/2 )" + "( z x**2 - y**2 - 1/2 x )"
206                        + "( -z + y**2 x + 4 x**2 + 1/4 )" + " )";
207
208        Reader source = new StringReader(exam);
209        GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source);
210        try {
211            F = (PolynomialList<BigRational>) parser.nextPolynomialSet();
212        } catch (ClassCastException e) {
213            fail("" + e);
214        } catch (IOException e) {
215            fail("" + e);
216        }
217        //System.out.println("F = " + F);
218
219        G = bbs.GB(F.list);
220        assertTrue("isGB( GB(Trinks7) )", bbs.isGB(G));
221        assertEquals("#GB(Trinks7) == 6", 6, G.size());
222        //PolynomialList<BigRational> trinks = new PolynomialList<BigRational>(F.ring,G);
223        //System.out.println("G = " + trinks);
224        assertTrue("isGB( GB(Trinks7) )", bb.isGB(G));
225
226        //Reduction<BigRational> rd = new ReductionSeq<BigRational>();
227        //System.out.println("G.contains(F) = " + rd.normalform(G,F.list) );
228    }
229
230
231    /**
232     * Test Trinks7 GBase, min pair list.
233     */
234    @SuppressWarnings("cast")
235    public void testTrinks7GBaseMin() {
236        bb = new GroebnerBaseSeqIter<BigRational>(new ReductionSeq<BigRational>(),
237                        new OrderedMinPairlist<BigRational>());
238
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        Reader source = new StringReader(exam);
244        GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source);
245        try {
246            F = (PolynomialList<BigRational>) parser.nextPolynomialSet();
247        } catch (ClassCastException e) {
248            fail("" + e);
249        } catch (IOException e) {
250            fail("" + e);
251        }
252        //System.out.println("F = " + F);
253
254        G = bb.GB(F.list);
255        assertTrue("isGB( GB(Trinks7) )", bb.isGB(G));
256        assertEquals("#GB(Trinks7) == 6", 6, G.size());
257        //PolynomialList<BigRational> trinks = new PolynomialList<BigRational>(F.ring,G);
258        //System.out.println("G = " + trinks);
259    }
260
261}