001/*
002 * $Id$
003 */
004
005package edu.jas.gbufd;
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.BigInteger;
020import edu.jas.arith.BigRational;
021import edu.jas.gb.GroebnerBaseAbstract;
022import edu.jas.gb.GroebnerBaseParallel;
023import edu.jas.kern.ComputerThreads;
024import edu.jas.poly.GenPolynomial;
025import edu.jas.poly.GenPolynomialRing;
026import edu.jas.poly.GenPolynomialTokenizer;
027import edu.jas.poly.OrderedPolynomialList;
028import edu.jas.poly.PolyUtil;
029import edu.jas.poly.PolynomialList;
030import edu.jas.ufd.PolyUfdUtil;
031import edu.jas.ufd.Quotient;
032import edu.jas.ufd.QuotientRing;
033
034
035/**
036 * Groebner base recursive pseudo reduction sequential tests with JUnit.
037 * @author Heinz Kredel
038 */
039
040public class GroebnerBasePseudoRecParTest extends TestCase {
041
042
043
044    /**
045     * main
046     */
047    public static void main(String[] args) {
048        junit.textui.TestRunner.run(suite());
049        ComputerThreads.terminate();
050    }
051
052
053    /**
054     * Constructs a <CODE>GroebnerBasePseudoRecParTest</CODE> object.
055     * @param name String.
056     */
057    public GroebnerBasePseudoRecParTest(String name) {
058        super(name);
059    }
060
061
062    /**
063     * suite.
064     */
065    public static Test suite() {
066        TestSuite suite = new TestSuite(GroebnerBasePseudoRecParTest.class);
067        return suite;
068    }
069
070
071    GenPolynomialRing<GenPolynomial<BigInteger>> fac;
072
073
074    List<GenPolynomial<GenPolynomial<BigInteger>>> L;
075
076
077    PolynomialList<GenPolynomial<BigInteger>> F;
078
079
080    List<GenPolynomial<GenPolynomial<BigInteger>>> G;
081
082
083    GroebnerBaseAbstract<GenPolynomial<BigInteger>> bb;
084
085
086    GroebnerBaseAbstract<GenPolynomial<BigRational>> bbr;
087
088
089    GenPolynomial<GenPolynomial<BigInteger>> a, b, c, d, e;
090
091
092    int rl = 2; //4; //3; 
093
094
095    int kl = 2;
096
097
098    int ll = 5;
099
100
101    int el = 4;
102
103
104    float q = 0.3f; //0.4f
105
106
107    int threads = 3;
108
109
110    @Override
111    protected void setUp() {
112        BigInteger coeff = new BigInteger(9);
113        GenPolynomialRing<BigInteger> cofac = new GenPolynomialRing<BigInteger>(coeff, 1);
114        fac = new GenPolynomialRing<GenPolynomial<BigInteger>>(cofac, rl);
115        a = b = c = d = e = null;
116        bb = new GroebnerBasePseudoRecParallel<BigInteger>(threads, cofac);
117    }
118
119
120    @Override
121    protected void tearDown() {
122        a = b = c = d = e = null;
123        fac = null;
124        bb.terminate();
125        bb = null;
126    }
127
128
129    /**
130     * Test recursive parallel GBase.
131     */
132    public void testRecParallelGBase() {
133        L = new ArrayList<GenPolynomial<GenPolynomial<BigInteger>>>();
134
135        a = fac.random(kl, ll, el, q);
136        b = fac.random(kl, ll, el, q);
137        c = fac.random(kl, ll, el, q);
138        d = fac.getZERO(); //fac.random(kl, ll, el, q);
139        e = c; //fac.random(kl, ll, el, q );
140
141        L.add(a);
142        //System.out.println("L = " + L );
143
144        L = bb.GB(L);
145        assertTrue("isGB( { a } )", bb.isGB(L));
146
147        L.add(b);
148        //System.out.println("L = " + L );
149
150        L = bb.GB(L);
151        assertTrue("isGB( { a, b } )", bb.isGB(L));
152
153        if (bb.commonZeroTest(L) < 0) {
154            L.clear();
155        }
156        L.add(c);
157        //System.out.println("L = " + L );
158
159        L = bb.GB(L);
160        assertTrue("isGB( { a, b, c } )", bb.isGB(L));
161
162        L.add(d);
163        //System.out.println("L = " + L );
164
165        L = bb.GB(L);
166        assertTrue("isGB( { a, b, c, d } )", bb.isGB(L));
167
168        L.add(e);
169        //System.out.println("L = " + L );
170
171        L = bb.GB(L);
172        assertTrue("isGB( { a, b, c, d, e } )", bb.isGB(L));
173    }
174
175
176    /**
177     * Test Hawes2 GBase.
178     */
179    @SuppressWarnings("unchecked")
180    public void testHawes2GBase() {
181        String exam = "IntFunc(a, c, b) (y2, y1, z1, z2, x) G" + "("
182                        + "( x + 2 y1 z1 + { 3 a } y1^2 + 5 y1^4 + { 2 c } y1 ),"
183                        + "( x + 2 y2 z2 + { 3 a } y2^2 + 5 y2^4 + { 2 c } y2 ),"
184                        + "( 2 z2 + { 6 a } y2 + 20 y2^3 + { 2 c } )," + "( 3 z1^2 + y1^2 + { b } ),"
185                        + "( 3 z2^2 + y2^2 + { b } )" + ")";
186        Reader source = new StringReader(exam);
187        GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source);
188        PolynomialList<GenPolynomial<BigRational>> Fr = null;
189        try {
190            Fr = parser.nextPolynomialSet();
191        } catch (ClassCastException e) {
192            fail("" + e);
193        } catch (IOException e) {
194            fail("" + e);
195        }
196        GenPolynomialRing<BigRational> cofac = (GenPolynomialRing<BigRational>) Fr.ring.coFac;
197        GenPolynomialRing<BigInteger> ifac = new GenPolynomialRing<BigInteger>(new BigInteger(), cofac);
198        fac = new GenPolynomialRing<GenPolynomial<BigInteger>>(ifac, Fr.ring);
199        L = PolyUfdUtil.integerFromRationalCoefficients(fac, Fr.list);
200        //System.out.println("F = " + L);
201        //System.out.println("Fr = " + Fr);
202
203        long s, t, q, i;
204        t = System.currentTimeMillis();
205        G = bb.GB(L);
206        t = System.currentTimeMillis() - t;
207        assertTrue("isGB( GB(Hawes2) )", bb.isGB(G));
208        assertEquals("#GB(Hawes2) == 8", 8, G.size());
209        G = OrderedPolynomialList.<GenPolynomial<BigInteger>> sort(G);
210        PolynomialList<GenPolynomial<BigInteger>> Gp = new PolynomialList<GenPolynomial<BigInteger>>(fac, G);
211        //System.out.println("G = " + Gp);
212        assertTrue("nonsense ", !Gp.getList().isEmpty());
213
214
215        GenPolynomialRing<BigRational> rfac = (GenPolynomialRing<BigRational>) Fr.ring.coFac;
216        List<GenPolynomial<GenPolynomial<BigRational>>> Gr, Kr, Lr = Fr.list;
217        bbr = new GroebnerBasePseudoRecParallel<BigRational>(threads, rfac);
218
219        s = System.currentTimeMillis();
220        Gr = bbr.GB(Lr);
221        s = System.currentTimeMillis() - s;
222        Gr = PolyUtil.<BigRational> monicRec(Gr);
223        assertTrue("isGB( GB(Hawes2) )", bbr.isGB(Gr));
224        assertEquals("#GB(Hawes2) == 8", 8, Gr.size());
225        Gr = OrderedPolynomialList.<GenPolynomial<BigRational>> sort(Gr);
226        PolynomialList<GenPolynomial<BigRational>> Grp = new PolynomialList<GenPolynomial<BigRational>>(
227                        Fr.ring, Gr);
228        //System.out.println("Gr = " + Grp)
229        assertTrue("nonsense ", !Grp.getList().isEmpty());;
230        bbr.terminate();
231
232        Kr = PolyUfdUtil.<BigRational> fromIntegerCoefficients(Fr.ring, G);
233        Kr = PolyUtil.<BigRational> monicRec(Kr);
234        assertEquals("ratGB == intGB", Kr, Gr);
235        assertTrue("nonsense ", s >= 0L);
236
237
238        QuotientRing<BigRational> qr = new QuotientRing<BigRational>(rfac);
239        GenPolynomialRing<Quotient<BigRational>> rring = new GenPolynomialRing<Quotient<BigRational>>(qr, fac);
240        List<GenPolynomial<Quotient<BigRational>>> Gq, Lq, Kq;
241        Lq = PolyUfdUtil.<BigRational> quotientFromIntegralCoefficients(rring, Lr);
242        Lq = PolyUtil.<Quotient<BigRational>> monic(Lq);
243        GroebnerBaseAbstract<Quotient<BigRational>> bbq;
244        bbq = new GroebnerBaseParallel<Quotient<BigRational>>(threads); //qr);
245
246        q = System.currentTimeMillis();
247        Gq = bbq.GB(Lq);
248        q = System.currentTimeMillis() - q;
249        assertTrue("isGB( GB(Hawes2) )", bbq.isGB(Gq));
250        assertEquals("#GB(Hawes2) == 8", 8, Gq.size());
251        Gq = OrderedPolynomialList.<Quotient<BigRational>> sort(Gq);
252        PolynomialList<Quotient<BigRational>> Gpq = new PolynomialList<Quotient<BigRational>>(rring, Gq);
253        //System.out.println("Gq = " + Gpq);
254        assertTrue("nonsense ", !Gpq.getList().isEmpty());
255        bbq.terminate();
256
257        Kq = PolyUfdUtil.<BigRational> quotientFromIntegralCoefficients(rring, Gr);
258        Kq = PolyUtil.<Quotient<BigRational>> monic(Kq);
259        assertEquals("ratGB == quotGB", Kq, Gq);
260        assertTrue("nonsense ", q >= 0L);
261
262
263        QuotientRing<BigInteger> qi = new QuotientRing<BigInteger>(ifac);
264        GenPolynomialRing<Quotient<BigInteger>> iring = new GenPolynomialRing<Quotient<BigInteger>>(qi, fac);
265        List<GenPolynomial<Quotient<BigInteger>>> Gqi, Lqi, Kqi;
266        Lqi = PolyUfdUtil.<BigInteger> quotientFromIntegralCoefficients(iring, L);
267        Lqi = PolyUtil.<Quotient<BigInteger>> monic(Lqi);
268        //System.out.println("Lqi = " + Lqi);
269        GroebnerBaseAbstract<Quotient<BigInteger>> bbqi;
270        bbqi = new GroebnerBaseParallel<Quotient<BigInteger>>(threads); //qr);
271
272        i = System.currentTimeMillis();
273        Gqi = bbqi.GB(Lqi);
274        i = System.currentTimeMillis() - i;
275        assertTrue("isGB( GB(Hawes2) )", bbqi.isGB(Gqi));
276        assertEquals("#GB(Hawes2) == 8", 8, Gqi.size());
277        Gqi = OrderedPolynomialList.<Quotient<BigInteger>> sort(Gqi);
278        PolynomialList<Quotient<BigInteger>> Gpqi = new PolynomialList<Quotient<BigInteger>>(iring, Gqi);
279        //System.out.println("Gqi = " + Gpqi);
280        assertTrue("nonsense ", !Gpqi.getList().isEmpty());
281        bbqi.terminate();
282
283        Kqi = PolyUfdUtil.<BigInteger> quotientFromIntegralCoefficients(iring, G);
284        Kqi = PolyUtil.<Quotient<BigInteger>> monic(Kqi);
285        assertEquals("quotRatGB == quotIntGB", Gqi, Kqi);
286        assertTrue("nonsense ", i >= 0L);
287
288        assertTrue("positive times ", s >= 0L && t >= 0L && q >= 0L && i >= 0L);
289        //System.out.println("time: ratGB = " + s + ", intGB = " + t + ", quotRatGB = " + q + ", quotIntGB = "
290        //                + i);
291    }
292
293}