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