001    /*
002     * $Id: SquarefreeRatTest.java 2933 2009-12-29 13:13:34Z kredel $
003     */
004    
005    package edu.jas.ufd;
006    
007    
008    import java.util.SortedMap;
009    
010    import junit.framework.Test;
011    import junit.framework.TestCase;
012    import junit.framework.TestSuite;
013    
014    import edu.jas.arith.BigRational;
015    import edu.jas.kern.ComputerThreads;
016    import edu.jas.poly.ExpVector;
017    import edu.jas.poly.GenPolynomial;
018    import edu.jas.poly.GenPolynomialRing;
019    import edu.jas.poly.PolyUtil;
020    import edu.jas.poly.TermOrder;
021    
022    
023    /**
024     * Squarefree factorization tests with JUnit.
025     * @author Heinz Kredel.
026     */
027    
028    public class SquarefreeRatTest extends TestCase {
029    
030    
031        /**
032         * main.
033         */
034        public static void main(String[] args) {
035            //BasicConfigurator.configure();
036            junit.textui.TestRunner.run(suite());
037            ComputerThreads.terminate();
038        }
039    
040    
041        /**
042         * Constructs a <CODE>SquarefreeRatTest</CODE> object.
043         * @param name String.
044         */
045        public SquarefreeRatTest(String name) {
046            super(name);
047        }
048    
049    
050        /**
051         */
052        public static Test suite() {
053            TestSuite suite = new TestSuite(SquarefreeRatTest.class);
054            return suite;
055        }
056    
057    
058        TermOrder to = new TermOrder(TermOrder.INVLEX);
059    
060    
061        int rl = 3;
062    
063    
064        int kl = 3;
065    
066    
067        int ll = 4;
068    
069    
070        int el = 3;
071    
072    
073        float q = 0.25f;
074    
075    
076        String[] vars;
077    
078    
079        String[] cvars;
080    
081    
082        String[] c1vars;
083    
084    
085        String[] rvars;
086    
087    
088        BigRational fac;
089    
090    
091        GreatestCommonDivisorAbstract<BigRational> ufd;
092    
093    
094        SquarefreeFieldChar0<BigRational> sqf;
095    
096    
097        GenPolynomialRing<BigRational> dfac;
098    
099    
100        GenPolynomial<BigRational> a;
101    
102    
103        GenPolynomial<BigRational> b;
104    
105    
106        GenPolynomial<BigRational> c;
107    
108    
109        GenPolynomial<BigRational> d;
110    
111    
112        GenPolynomial<BigRational> e;
113    
114    
115        GenPolynomialRing<BigRational> cfac;
116    
117    
118        GenPolynomialRing<GenPolynomial<BigRational>> rfac;
119    
120    
121        GenPolynomial<GenPolynomial<BigRational>> ar;
122    
123    
124        GenPolynomial<GenPolynomial<BigRational>> br;
125    
126    
127        GenPolynomial<GenPolynomial<BigRational>> cr;
128    
129    
130        GenPolynomial<GenPolynomial<BigRational>> dr;
131    
132    
133        GenPolynomial<GenPolynomial<BigRational>> er;
134    
135    
136        @Override
137        protected void setUp() {
138            vars = ExpVector.STDVARS(rl);
139            cvars = ExpVector.STDVARS(rl - 1);
140            c1vars = new String[] { cvars[0] };
141            rvars = new String[] { vars[rl - 1] };
142            fac = new BigRational(1);
143            //ufd = new GreatestCommonDivisorSubres<BigRational>();
144            //ufd = GCDFactory.<BigRational> getImplementation(fac);
145            ufd = GCDFactory.getProxy(fac);
146            sqf = new SquarefreeFieldChar0<BigRational>(fac);
147    
148            SquarefreeAbstract<BigRational> sqff = SquarefreeFactory.getImplementation(fac);
149            //System.out.println("sqf  = " + sqf);
150            //System.out.println("sqff = " + sqff);
151            assertEquals("sqf == sqff ", sqf.getClass(), sqff.getClass());
152    
153            a = b = c = d = e = null;
154            ar = br = cr = dr = er = null;
155        }
156    
157    
158        @Override
159        protected void tearDown() {
160            a = b = c = d = e = null;
161            ar = br = cr = dr = er = null;
162            // ComputerThreads.terminate();
163        }
164    
165    
166        /**
167         * Test base squarefree.
168         * 
169         */
170        public void testBaseSquarefree() {
171            //System.out.println("\nbase:");
172    
173            dfac = new GenPolynomialRing<BigRational>(fac, 1, to, rvars);
174    
175            a = dfac.random(kl, ll, el + 2, q);
176            b = dfac.random(kl, ll, el + 2, q);
177            c = dfac.random(kl, ll, el, q);
178            //System.out.println("a  = " + a);
179            //System.out.println("b  = " + b);
180            //System.out.println("c  = " + c);
181    
182            if (a.isZERO() || b.isZERO() || c.isZERO()) {
183                // skip for this turn
184                return;
185            }
186    
187            // a a b b b c
188            d = a.multiply(a).multiply(b).multiply(b).multiply(b).multiply(c);
189            c = a.multiply(b).multiply(c);
190            //System.out.println("d  = " + d);
191            //System.out.println("c  = " + c);
192    
193            c = sqf.baseSquarefreePart(c);
194            d = sqf.baseSquarefreePart(d);
195            //System.out.println("d  = " + d);
196            //System.out.println("c  = " + c);
197            assertTrue("isSquarefree(c) " + c, sqf.isSquarefree(c));
198            assertTrue("isSquarefree(d) " + d, sqf.isSquarefree(d));
199    
200            e = PolyUtil.<BigRational> basePseudoRemainder(d, c);
201            //System.out.println("e  = " + e);
202            assertTrue("squarefree(abc) | squarefree(aabbbc) " + e, e.isZERO());
203        }
204    
205    
206        /**
207         * Test base squarefree factors.
208         * 
209         */
210        public void testBaseSquarefreeFactors() {
211    
212            dfac = new GenPolynomialRing<BigRational>(fac, 1, to, rvars);
213    
214            a = dfac.random(kl, ll, el + 3, q);
215            b = dfac.random(kl, ll, el + 3, q);
216            c = dfac.random(kl, ll, el + 2, q);
217            //System.out.println("a  = " + a);
218            //System.out.println("b  = " + b);
219            //System.out.println("c  = " + c);
220    
221            if (a.isZERO() || b.isZERO() || c.isZERO()) {
222                // skip for this turn
223                return;
224            }
225    
226            // a a b b b c
227            d = a.multiply(a).multiply(b).multiply(b).multiply(b).multiply(c);
228            //System.out.println("d  = " + d);
229    
230            SortedMap<GenPolynomial<BigRational>, Long> sfactors;
231            sfactors = sqf.baseSquarefreeFactors(d);
232            //System.out.println("sfactors = " + sfactors);
233    
234            assertTrue("isFactorization(d,sfactors) ", sqf.isFactorization(d, sfactors));
235        }
236    
237    
238        /**
239         * Test recursive squarefree.
240         * 
241         */
242        public void testRecursiveSquarefree() {
243            //System.out.println("\nrecursive:");
244    
245            cfac = new GenPolynomialRing<BigRational>(fac, 2 - 1, to, c1vars);
246            rfac = new GenPolynomialRing<GenPolynomial<BigRational>>(cfac, 1, to, rvars);
247    
248            ar = rfac.random(kl, ll, el, q);
249            br = rfac.random(kl, ll, el, q);
250            cr = rfac.random(kl, ll, el, q);
251            //System.out.println("ar = " + ar);
252            //System.out.println("br = " + br);
253            //System.out.println("cr = " + cr);
254    
255            if (ar.isZERO() || br.isZERO() || cr.isZERO()) {
256                // skip for this turn
257                return;
258            }
259    
260            dr = ar.multiply(ar).multiply(br).multiply(br);
261            cr = ar.multiply(br);
262            //System.out.println("dr  = " + dr);
263            //System.out.println("cr  = " + cr);
264    
265            cr = sqf.recursiveUnivariateSquarefreePart(cr);
266            dr = sqf.recursiveUnivariateSquarefreePart(dr);
267            //System.out.println("dr  = " + dr);
268            //System.out.println("cr  = " + cr);
269            assertTrue("isSquarefree(cr) " + cr, sqf.isRecursiveSquarefree(cr));
270            assertTrue("isSquarefree(dr) " + dr, sqf.isRecursiveSquarefree(dr));
271    
272            er = PolyUtil.<BigRational> recursivePseudoRemainder(dr, cr);
273            //System.out.println("er  = " + er);
274            assertTrue("squarefree(abc) | squarefree(aabbc) " + er, er.isZERO());
275        }
276    
277    
278        /**
279         * Test recursive squarefree factors.
280         * 
281         */
282        public void testRecursiveSquarefreeFactors() {
283    
284            cfac = new GenPolynomialRing<BigRational>(fac, 2 - 1, to, c1vars);
285            rfac = new GenPolynomialRing<GenPolynomial<BigRational>>(cfac, 1, to, rvars);
286    
287            ar = rfac.random(kl, 3, 2, q);
288            br = rfac.random(kl, 3, 2, q);
289            cr = rfac.random(kl, 3, 2, q);
290            //System.out.println("ar = " + ar);
291            //System.out.println("br = " + br);
292            //System.out.println("cr = " + cr);
293    
294            if (ar.isZERO() || br.isZERO() || cr.isZERO()) {
295                // skip for this turn
296                return;
297            }
298    
299            dr = ar.multiply(cr).multiply(br).multiply(br);
300            //System.out.println("dr  = " + dr);
301    
302            SortedMap<GenPolynomial<GenPolynomial<BigRational>>, Long> sfactors;
303            sfactors = sqf.recursiveUnivariateSquarefreeFactors(dr);
304            //System.out.println("sfactors = " + sfactors);
305            assertTrue("isFactorization(d,sfactors) ", sqf.isRecursiveFactorization(dr, sfactors));
306        }
307    
308    
309        /**
310         * Test squarefree.
311         * 
312         */
313        public void testSquarefree() {
314            //System.out.println("\nfull:");
315    
316            dfac = new GenPolynomialRing<BigRational>(fac, rl, to, vars);
317    
318            a = dfac.random(kl, ll, 2, q);
319            b = dfac.random(kl, ll, 2, q);
320            c = dfac.random(kl, ll, 2, q);
321            //System.out.println("a  = " + a);
322            //System.out.println("b  = " + b);
323            //System.out.println("c  = " + c);
324    
325            if (a.isZERO() || b.isZERO() || c.isZERO()) {
326                // skip for this turn
327                return;
328            }
329    
330            d = a.multiply(a).multiply(b).multiply(b).multiply(c);
331            c = a.multiply(b).multiply(c);
332            //System.out.println("d  = " + d);
333            //System.out.println("c  = " + c);
334    
335            c = sqf.squarefreePart(c);
336            d = sqf.squarefreePart(d);
337            //System.out.println("c  = " + c);
338            //System.out.println("d  = " + d);
339            assertTrue("isSquarefree(d) " + d, sqf.isSquarefree(d));
340            assertTrue("isSquarefree(c) " + c, sqf.isSquarefree(c));
341    
342            e = PolyUtil.<BigRational> basePseudoRemainder(d, c);
343            //System.out.println("e  = " + e);
344            assertTrue("squarefree(abc) | squarefree(aabbc) " + e, e.isZERO());
345        }
346    
347    
348        /**
349         * Test squarefree factors.
350         * 
351         */
352        public void testSquarefreeFactors() {
353    
354            dfac = new GenPolynomialRing<BigRational>(fac, rl, to, vars);
355    
356            a = dfac.random(kl, 3, 2, q);
357            b = dfac.random(kl, 3, 2, q);
358            c = dfac.random(kl, 3, 2, q);
359            //System.out.println("a  = " + a);
360            //System.out.println("b  = " + b);
361            //System.out.println("c  = " + c);
362    
363            if (a.isZERO() || b.isZERO() || c.isZERO()) {
364                // skip for this turn
365                return;
366            }
367    
368            d = a.multiply(a).multiply(b).multiply(b).multiply(b).multiply(c);
369            //System.out.println("d  = " + d);
370    
371            SortedMap<GenPolynomial<BigRational>, Long> sfactors;
372            sfactors = sqf.squarefreeFactors(d);
373            //System.out.println("sfactors = " + sfactors);
374            assertTrue("isFactorization(d,sfactors) ", sqf.isFactorization(d, sfactors));
375        }
376    
377    }