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