001/*
002 * $Id$
003 */
004
005package edu.jas.ufd;
006
007
008import edu.jas.arith.BigInteger;
009import edu.jas.arith.ModInteger;
010import edu.jas.poly.GenPolynomial;
011import edu.jas.poly.GenPolynomialRing;
012import edu.jas.poly.PolyUtil;
013import edu.jas.poly.TermOrder;
014
015import junit.framework.Test;
016import junit.framework.TestCase;
017import junit.framework.TestSuite;
018
019
020/**
021 * GreatestCommonDivisor timing tests with JUnit. Change xtestMethod to
022 * testMethod to activate.
023 * @author Heinz Kredel
024 */
025public class GCDTimingTest extends TestCase {
026
027
028    /**
029     * main.
030     */
031    public static void main(String[] args) {
032        junit.textui.TestRunner.run(suite());
033    }
034
035
036    /**
037     * Constructs a <CODE>GCDTimingTest</CODE> object.
038     * @param name String.
039     */
040    public GCDTimingTest(String name) {
041        super(name);
042    }
043
044
045    /**
046     */
047    public static Test suite() {
048        TestSuite suite = new TestSuite(GCDTimingTest.class);
049        return suite;
050    }
051
052
053    GreatestCommonDivisorAbstract<BigInteger> ufd_si;
054
055
056    GreatestCommonDivisorAbstract<BigInteger> ufd_pp;
057
058
059    GreatestCommonDivisorSubres<BigInteger> ufd_sr; // because of non sparse pseudo remainder
060
061
062    GreatestCommonDivisorAbstract<BigInteger> ufd_mosi;
063
064
065    GreatestCommonDivisorAbstract<BigInteger> ufd_moevsi;
066
067
068    TermOrder to = new TermOrder(TermOrder.INVLEX);
069
070
071    GenPolynomialRing<BigInteger> dfac;
072
073
074    GenPolynomialRing<BigInteger> cfac;
075
076
077    GenPolynomialRing<GenPolynomial<BigInteger>> rfac;
078
079
080    BigInteger ai, bi, ci, di, ei;
081
082
083    GenPolynomial<BigInteger> a, b, c, d, e;
084
085
086    GenPolynomial<GenPolynomial<BigInteger>> ar, br, cr, dr, er;
087
088
089    int rl = 5;
090
091
092    int kl = 4;
093
094
095    int ll = 5;
096
097
098    int el = 3;
099
100
101    float q = 0.3f;
102
103
104    @Override
105    protected void setUp() {
106        a = b = c = d = e = null;
107        ai = bi = ci = di = ei = null;
108        ar = br = cr = dr = er = null;
109        ufd_si = new GreatestCommonDivisorSimple<BigInteger>();
110        ufd_pp = new GreatestCommonDivisorPrimitive<BigInteger>();
111        ufd_sr = new GreatestCommonDivisorSubres<BigInteger>();
112        ufd_mosi = new GreatestCommonDivisorModular<ModInteger>(true);
113        ufd_moevsi = new GreatestCommonDivisorModular<ModInteger>();
114        dfac = new GenPolynomialRing<BigInteger>(new BigInteger(1), rl, to);
115        cfac = new GenPolynomialRing<BigInteger>(new BigInteger(1), rl - 1, to);
116        rfac = new GenPolynomialRing<GenPolynomial<BigInteger>>(cfac, 1, to);
117    }
118
119
120    @Override
121    protected void tearDown() {
122        a = b = c = d = e = null;
123        ai = bi = ci = di = ei = null;
124        ar = br = cr = dr = er = null;
125        ufd_si = null;
126        ufd_pp = null;
127        ufd_sr = null;
128        dfac = null;
129        cfac = null;
130        rfac = null;
131    }
132
133
134    /**
135     * Test dummy for junit.
136     */
137    public void testDummy() {
138        assertTrue("ufd_pp != null", ufd_pp != null);
139    }
140
141
142    /**
143     * Test base gcd simple.
144     */
145    public void xtestBaseGcd() {
146        dfac = new GenPolynomialRing<BigInteger>(new BigInteger(1), 1, to);
147
148        long t;
149
150        for (int i = 0; i < 10; i++) {
151            a = dfac.random(kl * (i + 2), ll + 2 * i, el + 2 * i, q);
152            b = dfac.random(kl * (i + 2), ll + 2 * i, el + 2 * i, q);
153            c = dfac.random(kl * (i + 2), ll + 2 * i, el + 2 * i, q);
154            c = c.multiply(dfac.univariate(0));
155            //a = ufd.basePrimitivePart(a);
156            //b = ufd.basePrimitivePart(b);
157            //c = ufd.basePrimitivePart(c).abs();
158
159            //System.out.println("a  = " + a);
160            //System.out.println("b  = " + b);
161            //System.out.println("c  = " + c);
162
163            if (a.isZERO() || b.isZERO() || c.isZERO()) {
164                // skip for this turn
165                continue;
166            }
167            assertTrue("length( c" + i + " ) <> 0", c.length() > 0);
168            //assertTrue(" not isZERO( c"+i+" )", !c.isZERO() );
169            //assertTrue(" not isONE( c"+i+" )", !c.isONE() );
170
171            a = a.multiply(c);
172            b = b.multiply(c);
173
174            System.out.println(
175                            "\ndegrees: a = " + a.degree() + ", b = " + b.degree() + ", c = " + c.degree());
176            /*
177            t = System.currentTimeMillis();
178            d = ufd_si.baseGcd(a,b);
179            t = System.currentTimeMillis() - t;
180            e = PolyUtil.<BigInteger>baseSparsePseudoRemainder(d,c);
181            //System.out.println("d  = " + d);
182            assertTrue("c | gcd(ac,bc) " + e, e.isZERO() );
183            System.out.println("simple prs        time = " + t);
184            */
185
186            t = System.currentTimeMillis();
187            d = ufd_pp.baseGcd(a, b);
188            t = System.currentTimeMillis() - t;
189            e = PolyUtil.<BigInteger> baseSparsePseudoRemainder(d, c);
190            //System.out.println("d  = " + d);
191
192            assertTrue("c | gcd(ac,bc) " + e, e.isZERO());
193            System.out.println("primitive prs     time = " + t);
194
195
196            t = System.currentTimeMillis();
197            d = ufd_sr.baseGcd(a, b);
198            t = System.currentTimeMillis() - t;
199            e = PolyUtil.<BigInteger> baseSparsePseudoRemainder(d, c);
200            //System.out.println("d  = " + d);
201
202            assertTrue("c | gcd(ac,bc) " + e, e.isZERO());
203            System.out.println("subsresultant prs time = " + t);
204        }
205    }
206
207
208    /**
209     * Test recursive gcd.
210     */
211    public void xtestRecursiveGCD() {
212        cfac = new GenPolynomialRing<BigInteger>(new BigInteger(1), 2 - 1, to);
213        rfac = new GenPolynomialRing<GenPolynomial<BigInteger>>(cfac, 1, to);
214
215        long t;
216
217        for (int i = 0; i < 5; i++) {
218            ar = rfac.random(kl, ll, el + i, q);
219            br = rfac.random(kl, ll, el + i, q);
220            cr = rfac.random(kl, ll, el, q);
221            cr = cr.multiply(rfac.univariate(0));
222            //System.out.println("ar = " + ar);
223            //System.out.println("br = " + br);
224            //System.out.println("cr = " + cr);
225
226            if (ar.isZERO() || br.isZERO() || cr.isZERO()) {
227                // skip for this turn
228                continue;
229            }
230            assertTrue("length( cr" + i + " ) <> 0", cr.length() > 0);
231            //assertTrue(" not isZERO( c"+i+" )", !c.isZERO() );
232            //assertTrue(" not isONE( c"+i+" )", !c.isONE() );
233
234            ar = ar.multiply(cr);
235            br = br.multiply(cr);
236            //System.out.println("ar = " + ar);
237            //System.out.println("br = " + br);
238
239            System.out.println("\ndegrees: a = " + ar.degree() + ", b = " + br.degree() + ", c = "
240                            + cr.degree());
241
242            t = System.currentTimeMillis();
243            dr = ufd_si.recursiveUnivariateGcd(ar, br);
244            t = System.currentTimeMillis() - t;
245            //System.out.println("dr = " + dr);
246
247            //er = PolyUtil.<BigInteger>recursiveSparsePseudoRemainder(dr,cr);
248            //System.out.println("er = " + er);
249
250            //assertTrue("c | gcd(ac,bc) " + er, er.isZERO() );
251            System.out.println("simple prs        time = " + t);
252            /*
253            */
254
255            t = System.currentTimeMillis();
256            dr = ufd_pp.recursiveUnivariateGcd(ar, br);
257            t = System.currentTimeMillis() - t;
258            //System.out.println("dr = " + dr);
259
260            er = PolyUtil.<BigInteger> recursiveSparsePseudoRemainder(dr, cr);
261            //System.out.println("er = " + er);
262
263            assertTrue("c | gcd(ac,bc) " + er, er.isZERO());
264            System.out.println("primitive prs     time = " + t);
265
266
267            t = System.currentTimeMillis();
268            dr = ufd_sr.recursiveUnivariateGcd(ar, br);
269            t = System.currentTimeMillis() - t;
270            //System.out.println("dr = " + dr);
271
272            er = PolyUtil.<BigInteger> recursiveDensePseudoRemainder(dr, cr);
273            //System.out.println("er = " + er);
274
275            assertTrue("c | gcd(ac,bc) " + er, er.isZERO());
276            System.out.println("subresultant prs  time = " + t);
277        }
278    }
279
280
281    /**
282     * Test gcd.
283     */
284    public void xtestGCD() {
285        long t;
286
287        dfac = new GenPolynomialRing<BigInteger>(new BigInteger(1), 3, to);
288
289        for (int i = 0; i < 5; i++) {
290            a = dfac.random(kl + i * 30, ll + i, 2 * el, q);
291            b = dfac.random(kl + i * 30, ll + i, 2 * el, q);
292            c = dfac.random(kl, ll, el, q);
293            //c = dfac.getONE();
294            //c = c.multiply( dfac.univariate(0) ).multiply( dfac.univariate(4) );
295            //c = c.multiply( dfac.univariate(0) );
296            c = ufd_pp.primitivePart(c).abs();
297            //System.out.println("a = " + a);
298            //System.out.println("b = " + b);
299            //System.out.println("c = " + c);
300
301            if (a.isZERO() || b.isZERO() || c.isZERO()) {
302                // skip for this turn
303                continue;
304            }
305            assertTrue("length( c" + i + " ) <> 0", c.length() > 0);
306            //assertTrue(" not isZERO( c"+i+" )", !c.isZERO() );
307            //assertTrue(" not isONE( c"+i+" )", !c.isONE() );
308
309            a = a.multiply(c);
310            b = b.multiply(c);
311            //System.out.println("a = " + a);
312            //System.out.println("b = " + b);
313            //System.out.println("c = " + c);
314
315            System.out.println(
316                            "\ndegrees: a = " + a.degree() + ", b = " + b.degree() + ", c = " + c.degree());
317            /*
318            t = System.currentTimeMillis();
319            d = ufd_si.gcd(a,b);
320            t = System.currentTimeMillis() - t;
321            e = PolyUtil.<BigInteger>baseSparsePseudoRemainder(d,c);
322            //System.out.println("d  = " + d);
323            assertTrue("c | gcd(ac,bc) " + e, e.isZERO() );
324            System.out.println("simple prs        time = " + t);
325            */
326            /*
327            t = System.currentTimeMillis();
328            d = ufd_pp.gcd(a,b);
329            t = System.currentTimeMillis() - t;
330            e = PolyUtil.<BigInteger>baseSparsePseudoRemainder(d,c);
331            //System.out.println("d  = " + d);
332            assertTrue("c | gcd(ac,bc) " + e, e.isZERO() );
333            System.out.println("primitive prs     time = " + t);
334            */
335
336            t = System.currentTimeMillis();
337            d = ufd_sr.gcd(a, b);
338            t = System.currentTimeMillis() - t;
339            e = PolyUtil.<BigInteger> baseSparsePseudoRemainder(d, c);
340            //System.out.println("d  = " + d);
341
342            assertTrue("c | gcd(ac,bc) " + e, e.isZERO());
343            System.out.println("subsresultant prs time = " + t);
344
345
346            t = System.currentTimeMillis();
347            d = ufd_mosi.gcd(a, b);
348            t = System.currentTimeMillis() - t;
349            e = PolyUtil.<BigInteger> baseSparsePseudoRemainder(d, c);
350            //System.out.println("d  = " + d);
351
352            assertTrue("c | gcd(ac,bc) " + e, e.isZERO());
353            System.out.println("modular simple    time = " + t);
354
355
356            t = System.currentTimeMillis();
357            d = ufd_moevsi.gcd(a, b);
358            t = System.currentTimeMillis() - t;
359            e = PolyUtil.<BigInteger> baseSparsePseudoRemainder(d, c);
360            //System.out.println("d  = " + d);
361
362            assertTrue("c | gcd(ac,bc) " + e, e.isZERO());
363            System.out.println("modular eval      time = " + t);
364        }
365    }
366
367}