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