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