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