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