001/*
002 * $Id$
003 */
004
005package edu.jas.gb;
006
007
008import java.util.ArrayList;
009import java.util.List;
010
011import junit.framework.Test;
012import junit.framework.TestCase;
013import junit.framework.TestSuite;
014
015import edu.jas.arith.BigRational;
016import edu.jas.kern.ComputerThreads;
017import edu.jas.poly.GenPolynomial;
018import edu.jas.poly.GenPolynomialRing;
019import edu.jas.poly.ModuleList;
020import edu.jas.poly.PolynomialList;
021import edu.jas.poly.TermOrder;
022import edu.jas.poly.TermOrderByName;
023
024
025/**
026 * ModGroebnerBase sequential and parallel tests with JUnit.
027 * @author Heinz Kredel
028 */
029
030public class ModGroebnerBaseTest extends TestCase {
031
032
033    /**
034     * main.
035     */
036    public static void main(String[] args) {
037        junit.textui.TestRunner.run(suite());
038        ComputerThreads.terminate();
039    }
040
041
042    /**
043     * Constructs a <CODE>ModGroebnerBaseTest</CODE> object.
044     * @param name String.
045     */
046    public ModGroebnerBaseTest(String name) {
047        super(name);
048    }
049
050
051    /**
052     * suite.
053     */
054    public static Test suite() {
055        TestSuite suite = new TestSuite(ModGroebnerBaseTest.class);
056        return suite;
057    }
058
059
060    GenPolynomialRing<BigRational> fac;
061
062
063    PolynomialList<BigRational> F;
064
065
066    List<GenPolynomial<BigRational>> G;
067
068
069    GenPolynomial<BigRational> a, b, c, d, e;
070
071
072    TermOrder tord;
073
074
075    List<List<GenPolynomial<BigRational>>> L;
076
077
078    List<GenPolynomial<BigRational>> V;
079
080
081    ModuleList<BigRational> M, N, K, I;
082
083
084    GroebnerBaseAbstract<BigRational> mbb;
085
086
087    ReductionAbstract<BigRational> red;
088
089
090    int rl = 3; //4; //3; 
091
092
093    int kl = 7;
094
095
096    int ll = 5;
097
098
099    int el = 2;
100
101
102    float q = 0.2f; //0.4f
103
104
105    BigRational coeff;
106
107
108    @Override
109    protected void setUp() {
110        coeff = new BigRational();
111        tord = TermOrderByName.DEFAULT; // INVLEX
112        fac = new GenPolynomialRing<BigRational>(coeff, rl, tord);
113        mbb = new GroebnerBaseSeq<BigRational>(); //coeff);
114        red = new ReductionSeq<BigRational>();
115        a = b = c = d = e = null;
116
117        do {
118            a = fac.random(kl, ll, el, q);
119            b = fac.random(kl, ll, el, q);
120            c = fac.random(kl, ll, el, q);
121            d = fac.random(kl, ll, el, q);
122        } while (a.isZERO() || b.isZERO() || c.isZERO() || d.isZERO());
123        e = d; //fac.random(kl, ll, el, q );
124    }
125
126
127    @Override
128    protected void tearDown() {
129        mbb.terminate();
130        mbb = null;
131        red = null;
132        a = b = c = d = e = null;
133        fac = null;
134        tord = null;
135    }
136
137
138    /**
139     * Test sequential GBase.
140     */
141    public void testSequentialModGB() {
142        L = new ArrayList<List<GenPolynomial<BigRational>>>();
143        V = new ArrayList<GenPolynomial<BigRational>>();
144        V.add(a);
145        V.add(fac.getZERO());
146        V.add(fac.getONE());
147        L.add(V);
148        M = new ModuleList<BigRational>(fac, L);
149        assertTrue("isGB( { (a,0,1) } )", mbb.isGB(M));
150
151        N = mbb.GB(M);
152        assertTrue("is( { (a,0,1) } )", mbb.isGB(N));
153
154        V = new ArrayList<GenPolynomial<BigRational>>();
155        V.add(b);
156        V.add(fac.getONE());
157        V.add(fac.getZERO());
158        L.add(V);
159        M = new ModuleList<BigRational>(fac, L);
160        //System.out.println("L = " + L.size() );
161
162        N = mbb.GB(M);
163        assertTrue("isGB( { (a,0,1),(b,1,0) } )", mbb.isGB(N));
164        //System.out.println("N = " + N );
165
166        V = new ArrayList<GenPolynomial<BigRational>>();
167        V.add(c);
168        V.add(fac.getZERO());
169        V.add(fac.getZERO());
170        L.add(V);
171        M = new ModuleList<BigRational>(fac, L);
172        //System.out.println("L = " + L.size() );
173
174        N = mbb.GB(M);
175        assertTrue("isGB( { (a,),(b,),(c,) } )", mbb.isGB(N));
176        //System.out.println("N = " + N );
177
178        V = new ArrayList<GenPolynomial<BigRational>>();
179        V.add(d);
180        V.add(fac.getZERO());
181        V.add(fac.getZERO());
182        L.add(V);
183        M = new ModuleList<BigRational>(fac, L);
184        //System.out.println("L = " + L.size() );
185
186        N = mbb.GB(M);
187        assertTrue("isGB( { (a,b,c,d) } )", mbb.isGB(N));
188        //System.out.println("N = " + N );
189    }
190
191
192    /**
193     * Test parallel GBase.
194     */
195    public void testParallelModGB() {
196        mbb = new GroebnerBaseParallel<BigRational>(); //coeff);
197
198        L = new ArrayList<List<GenPolynomial<BigRational>>>();
199
200        V = new ArrayList<GenPolynomial<BigRational>>();
201        V.add(a);
202        V.add(fac.getZERO());
203        V.add(fac.getONE());
204        L.add(V);
205        M = new ModuleList<BigRational>(fac, L);
206        assertTrue("isGB( { (a,0,1) } )", mbb.isGB(M));
207
208        N = mbb.GB(M);
209        assertTrue("is( { (a,0,1) } )", mbb.isGB(N));
210
211        V = new ArrayList<GenPolynomial<BigRational>>();
212        V.add(b);
213        V.add(fac.getONE());
214        V.add(fac.getZERO());
215        L.add(V);
216        M = new ModuleList<BigRational>(fac, L);
217        //System.out.println("L = " + L.size() );
218
219        N = mbb.GB(M);
220        assertTrue("isGB( { (a,0,1),(b,1,0) } )", mbb.isGB(N));
221        //System.out.println("N = " + N );
222
223        V = new ArrayList<GenPolynomial<BigRational>>();
224        V.add(c);
225        V.add(fac.getZERO());
226        V.add(fac.getZERO());
227        L.add(V);
228        M = new ModuleList<BigRational>(fac, L);
229        //System.out.println("L = " + L.size() );
230
231        N = mbb.GB(M);
232        assertTrue("isGB( { (a,),(b,),(c,) } )", mbb.isGB(N));
233        //System.out.println("N = " + N );
234
235        V = new ArrayList<GenPolynomial<BigRational>>();
236        V.add(d);
237        V.add(fac.getZERO());
238        V.add(fac.getZERO());
239        L.add(V);
240        M = new ModuleList<BigRational>(fac, L);
241        //System.out.println("L = " + L.size() );
242
243        N = mbb.GB(M);
244        assertTrue("isGB( { (a,b,c,d) } )", mbb.isGB(N));
245        //System.out.println("N = " + N );
246    }
247
248    
249    /**
250     * Test sequential GBase with TOP and POT term order.
251     */
252    public void testSequentialModTOPGB() {
253        //System.out.println("a = " + a);
254        //System.out.println("b = " + b);
255
256        L = new ArrayList<List<GenPolynomial<BigRational>>>();
257        V = new ArrayList<GenPolynomial<BigRational>>();
258        V.add(a);
259        V.add(fac.getZERO());
260        V.add(fac.getONE());
261        L.add(V);
262        M = new ModuleList<BigRational>(fac, L);
263        assertTrue("isGB( { (a,0,1) } )", mbb.isGB(M));
264
265        N = mbb.GB(M);
266        assertTrue("is( { (a,0,1) } )", mbb.isGB(N));
267
268        K = mbb.GB(M, true);
269        assertTrue("is( { (a,0,1) } )", mbb.isGB(K, true));
270        assertEquals("N == K", N, K);
271
272        V = new ArrayList<GenPolynomial<BigRational>>();
273        V.add(b);
274        V.add(fac.getONE());
275        V.add(fac.getZERO());
276        L.add(V);
277        M = new ModuleList<BigRational>(fac, L);
278        //System.out.println("M = " + M);
279
280        N = mbb.GB(M);
281        assertTrue("isGB( { (a,0,1),(b,1,0) } )", mbb.isGB(N));
282        //System.out.println("N = " + N);
283
284        K = mbb.GB(M,true);
285        assertTrue("is( { (a,0,1) } )", mbb.isGB(K, true));
286        //System.out.println("K = " + K);
287
288        I = red.normalform(K, N, true);
289        //System.out.println("I = " + I);
290        assertTrue("K.nf(N) == (0)", I.isZERO());
291    
292        I = red.normalform(N, K);
293        //System.out.println("I = " + I);
294        assertTrue("N.nf(K) == (0)", I.isZERO());
295    }
296
297}