001/*
002 * $Id: PolyGBUtilTest.java 5863 2018-07-20 11:13:34Z kredel $
003 */
004
005package edu.jas.gbufd;
006
007
008import junit.framework.Test;
009import junit.framework.TestCase;
010import junit.framework.TestSuite;
011
012
013import edu.jas.arith.BigInteger;
014import edu.jas.arith.BigRational;
015import edu.jas.arith.ModInteger;
016import edu.jas.arith.ModIntegerRing;
017import edu.jas.arith.PrimeList;
018import edu.jas.kern.ComputerThreads;
019import edu.jas.poly.GenPolynomial;
020import edu.jas.poly.GenPolynomialRing;
021import edu.jas.poly.TermOrder;
022import edu.jas.ufd.GCDProxy;
023import edu.jas.ufd.GreatestCommonDivisorAbstract;
024import edu.jas.ufd.GreatestCommonDivisorModEval;
025import edu.jas.ufd.GreatestCommonDivisorModular;
026import edu.jas.ufd.GreatestCommonDivisorSimple;
027import edu.jas.ufd.GreatestCommonDivisorSubres;
028
029
030/**
031 * PolyGBUtil tests with JUnit.
032 * @author Heinz Kredel
033 */
034public class PolyGBUtilTest extends TestCase {
035
036
037    /**
038     * main.
039     */
040    public static void main(String[] args) {
041        junit.textui.TestRunner.run(suite());
042        ComputerThreads.terminate();
043    }
044
045
046    /**
047     * Constructs a <CODE>PolyUtilTest</CODE> object.
048     * @param name String.
049     */
050    public PolyGBUtilTest(String name) {
051        super(name);
052    }
053
054
055    /**
056     */
057    public static Test suite() {
058        TestSuite suite = new TestSuite(PolyGBUtilTest.class);
059        return suite;
060    }
061
062
063    //TermOrder to = new TermOrder(TermOrder.INVLEX);
064    TermOrder to = new TermOrder(TermOrder.IGRLEX);
065
066
067    int rl = 3;
068
069
070    int kl = 3;
071
072
073    int ll = 4;
074
075
076    int el = 3;
077
078
079    float q = 0.29f;
080
081
082    @Override
083    protected void setUp() {
084    }
085
086
087    @Override
088    protected void tearDown() {
089    }
090
091
092    /**
093     * Test resultant modular.
094     */
095    public void testResultantModular() {
096        GenPolynomialRing<ModInteger> dfac;
097        ModIntegerRing mi;
098
099        PrimeList primes = new PrimeList();
100        mi = new ModIntegerRing(primes.get(1)); // 17, 19, 23, 41, 163, 
101        dfac = new GenPolynomialRing<ModInteger>(mi, rl, to);
102        //System.out.println("dfac = " + dfac);
103
104        GreatestCommonDivisorAbstract<ModInteger> ufds = new GreatestCommonDivisorSimple<ModInteger>();
105        GreatestCommonDivisorAbstract<ModInteger> sres = new GreatestCommonDivisorSubres<ModInteger>();
106        GreatestCommonDivisorAbstract<ModInteger> ufdm = new GreatestCommonDivisorModEval<ModInteger>();
107        GenPolynomial<ModInteger> a, b, c, d, e;
108
109        for (int i = 0; i < 1; i++) {
110            a = dfac.random(kl, ll, el, q);
111            b = dfac.random(kl, ll + 2, el, q);
112            //a = dfac.parse("6 x0^4 - 17");
113            //b = dfac.parse("6 x1^2 - 7 x0^2 - 5 x1 - 14");
114            //a = dfac.parse("5 x1^4 * x2^4 + 2 x1^2 + x0^2");
115            //b = dfac.parse("5 x0^2 * x1^2 + 2 x2^2 + 5 x2 + 15");
116            //a = dfac.parse("x0^3 * x2^2 + 6 x0^4 + 6 x0 + 7");
117            //b = dfac.parse("7 x0^2 * x2^2 + 3 x2^2 + 4 x1^2 * x2 + 4 x2 + 4 x1^2 + x1 + 7");
118            //System.out.println("a = " + a);
119            //System.out.println("b = " + b);
120
121            c = ufds.resultant(a, b);
122            d = sres.resultant(a, b);
123            e = ufdm.resultant(a, b);
124
125            boolean t1 = PolyGBUtil.<ModInteger> isResultant(a, b, c);
126            //System.out.println("t1 = " + t1);
127            boolean t2 = PolyGBUtil.<ModInteger> isResultant(a, b, d);
128            //System.out.println("t2 = " + t2);
129            boolean t3 = PolyGBUtil.<ModInteger> isResultant(a, b, e);
130            //System.out.println("t3 = " + t3);
131
132            //System.out.println("c = " + c);
133            //System.out.println("d = " + d);
134            //System.out.println("e = " + e);
135
136            assertTrue("isResultant(a,b,c): " + c, t1);
137            assertTrue("isResultant(a,b,d): " + d, t2);
138            assertTrue("isResultant(a,b,e): " + e, t3);
139        }
140    }
141
142
143    /**
144     * Test resultant integer.
145     */
146    public void testResultantInteger() {
147        GenPolynomialRing<BigInteger> dfac;
148        dfac = new GenPolynomialRing<BigInteger>(new BigInteger(1), rl, to);
149        //System.out.println("dfac = " + dfac);
150
151        GreatestCommonDivisorAbstract<BigInteger> ufds = new GreatestCommonDivisorSimple<BigInteger>();
152        GreatestCommonDivisorAbstract<BigInteger> sres = new GreatestCommonDivisorSubres<BigInteger>();
153        GreatestCommonDivisorAbstract<BigInteger> ufdm = new GreatestCommonDivisorModular<ModInteger>(); //true);
154        GenPolynomial<BigInteger> a, b, c, d, e;
155
156        for (int i = 0; i < 1; i++) {
157            a = dfac.random(kl, ll, el, q);
158            b = dfac.random(kl, ll + 2, el, q);
159            //a = dfac.parse("6 x0^4 - 17");
160            //b = dfac.parse("6 x1^2 - 7 x0^2 - 5 x1 - 14");
161            //System.out.println("a = " + a);
162            //System.out.println("b = " + b);
163
164            c = ufds.resultant(a, b);
165            d = sres.resultant(a, b);
166            e = ufdm.resultant(a, b);
167
168            boolean t1 = PolyGBUtil.<BigInteger> isResultant(a, b, c);
169            //System.out.println("t1 = " + t1);
170            boolean t2 = PolyGBUtil.<BigInteger> isResultant(a, b, d);
171            //System.out.println("t2 = " + t2);
172            boolean t3 = PolyGBUtil.<BigInteger> isResultant(a, b, e);
173            //System.out.println("t3 = " + t3);
174
175            //System.out.println("c = " + c);
176            //System.out.println("d = " + d);
177            //System.out.println("e = " + e);
178
179            assertTrue("isResultant(a,b,d): " + d, t2);
180            assertTrue("isResultant(a,b,e): " + e, t3);
181            assertTrue("isResultant(a,b,c): " + c, t1);
182        }
183    }
184
185
186    /**
187     * Test resultant modular parallel proxy.
188     */
189    public void testResultantModularParallel() {
190        GenPolynomialRing<ModInteger> dfac;
191        ModIntegerRing mi;
192
193        PrimeList primes = new PrimeList();
194        mi = new ModIntegerRing(primes.get(1)); // 17, 19, 23, 41, 163, 
195        dfac = new GenPolynomialRing<ModInteger>(mi, rl, to);
196        //System.out.println("dfac = " + dfac);
197
198        GreatestCommonDivisorAbstract<ModInteger> ufds = new GreatestCommonDivisorSimple<ModInteger>();
199        GreatestCommonDivisorAbstract<ModInteger> sres = new GreatestCommonDivisorSubres<ModInteger>();
200        GreatestCommonDivisorAbstract<ModInteger> ufdm = new GreatestCommonDivisorModEval<ModInteger>();
201
202        GreatestCommonDivisorAbstract<ModInteger> pufds = new GCDProxy<ModInteger>(sres, ufds);
203        GreatestCommonDivisorAbstract<ModInteger> pufdm = new GCDProxy<ModInteger>(ufdm, sres);
204
205        GenPolynomial<ModInteger> a, b, c, d;
206
207        for (int i = 0; i < 1; i++) {
208            a = dfac.random(kl, ll, el, q);
209            b = dfac.random(kl, ll + 2, el, q);
210            //System.out.println("a = " + a);
211            //System.out.println("b = " + b);
212
213            c = pufds.resultant(a, b);
214            d = pufdm.resultant(a, b);
215
216            boolean t1 = PolyGBUtil.<ModInteger> isResultant(a, b, c);
217            //System.out.println("t1 = " + t1);
218            boolean t2 = PolyGBUtil.<ModInteger> isResultant(a, b, d);
219            //System.out.println("t2 = " + t2);
220
221            //System.out.println("c = " + c);
222            //System.out.println("d = " + d);
223
224            assertTrue("isResultant(a,b,c): " + c, t1);
225            assertTrue("isResultant(a,b,d): " + d, t2);
226        }
227    }
228
229
230    /**
231     * Test resultant integer parallel proxy.
232     */
233    public void testResultantIntegerProxy() {
234        GenPolynomialRing<BigInteger> dfac;
235        dfac = new GenPolynomialRing<BigInteger>(new BigInteger(1), rl, to);
236        //System.out.println("dfac = " + dfac);
237
238        GreatestCommonDivisorAbstract<BigInteger> ufds = new GreatestCommonDivisorSimple<BigInteger>();
239        GreatestCommonDivisorAbstract<BigInteger> sres = new GreatestCommonDivisorSubres<BigInteger>();
240        GreatestCommonDivisorAbstract<BigInteger> ufdm = new GreatestCommonDivisorModular<ModInteger>(); //true);
241
242        GreatestCommonDivisorAbstract<BigInteger> pufds = new GCDProxy<BigInteger>(sres, ufds);
243        GreatestCommonDivisorAbstract<BigInteger> pufdm = new GCDProxy<BigInteger>(ufdm, sres);
244
245        GenPolynomial<BigInteger> a, b, c, d;
246
247        for (int i = 0; i < 1; i++) {
248            a = dfac.random(kl, ll, el, q);
249            b = dfac.random(kl, ll + 1, el, q);
250            //System.out.println("a = " + a);
251            //System.out.println("b = " + b);
252
253            c = pufds.resultant(a, b);
254            d = pufdm.resultant(a, b);
255
256            boolean t1 = PolyGBUtil.<BigInteger> isResultant(a, b, c);
257            //System.out.println("t1 = " + t1);
258            boolean t2 = PolyGBUtil.<BigInteger> isResultant(a, b, d);
259            //System.out.println("t2 = " + t2);
260
261            //System.out.println("c = " + c);
262            //System.out.println("d = " + d);
263
264            assertTrue("isResultant(a,b,d): " + d, t2);
265            assertTrue("isResultant(a,b,c): " + c, t1);
266        }
267    }
268
269
270    /**
271     * Test coefficient base pseudo remainder.
272     */
273    public void testCoefficientBasePseudoRemainder() {
274        GenPolynomialRing<BigRational> dfac;
275        BigRational br = new BigRational();
276        to = new TermOrder(TermOrder.INVLEX);
277        //String[] vars = new String[] { "x1",  "x2", "x3" };
278        String[] vars = new String[] { "x1", "x2" };
279        dfac = new GenPolynomialRing<BigRational>(br, to, vars);
280        //System.out.println("dfac = " + dfac);
281        GenPolynomialRing<GenPolynomial<BigRational>> rfac = dfac.recursive(1);
282        //System.out.println("rfac = " + rfac);
283        GenPolynomialRing<BigRational> cfac = (GenPolynomialRing<BigRational>) rfac.coFac;
284        //System.out.println("cfac = " + cfac);
285        GenPolynomial<GenPolynomial<BigRational>> ar, cr, dr, er;
286        GenPolynomial<BigRational> b;
287
288        ar = rfac.random(kl, ll, el, q * 1.1f);
289        b = cfac.random(kl, ll + 2, el * 2, q);
290        //System.out.println("ar = " + ar);
291        //System.out.println("b  = " + b);
292
293        cr = PolyGBUtil.<BigRational> coefficientPseudoRemainderBase(ar, b);
294        //System.out.println("cr = " + cr);
295        assertTrue("deg(c) < deg(a): ", cr.degree(0) <= ar.degree(0) || ar.degree(0) == 0);
296        assertTrue("deg(lfcd(c)) < deg(b): ",
297                        cr.leadingBaseCoefficient().degree(0) < b.degree(0) || b.degree(0) == 0);
298
299        dr = ar.multiply(b);
300        //System.out.println("dr = " + dr);
301        cr = PolyGBUtil.<BigRational> coefficientPseudoRemainderBase(dr, b);
302        //System.out.println("cr = " + cr);
303        assertTrue("c == 0: ", cr.isZERO());
304
305        long s = ar.degree(0);
306        er = rfac.univariate(0, s + 1);
307        //System.out.println("er = " + er);
308        er = er.multiply(b.multiply(b));
309        er = er.sum(ar);
310        //System.out.println("er = " + er);
311        cr = PolyGBUtil.<BigRational> coefficientPseudoRemainderBase(er, b);
312        //System.out.println("cr = " + cr);
313        assertTrue("deg(c) < deg(a): ", cr.degree(0) < er.degree(0));
314    }
315
316
317    /**
318     * Test coefficient recursive pseudo remainder.
319     */
320    public void testCoefficientRecursivePseudoRemainder() {
321        GenPolynomialRing<BigRational> dfac;
322        BigRational br = new BigRational();
323        to = new TermOrder(TermOrder.INVLEX);
324        String[] vars = new String[] { "x1", "x2", "x3" };
325        //String[] vars = new String[] { "x1",  "x2" };
326        dfac = new GenPolynomialRing<BigRational>(br, to, vars);
327        //System.out.println("dfac = " + dfac);
328        GenPolynomialRing<GenPolynomial<BigRational>> r1fac = dfac.recursive(2);
329        //System.out.println("r1fac = " + r1fac);
330        GenPolynomialRing<GenPolynomial<GenPolynomial<BigRational>>> rfac = r1fac.recursive(1);
331        //System.out.println("rfac = " + rfac);
332        GenPolynomialRing<GenPolynomial<BigRational>> cfac = (GenPolynomialRing<GenPolynomial<BigRational>>) rfac.coFac;
333        //System.out.println("cfac = " + cfac);
334        GenPolynomial<GenPolynomial<GenPolynomial<BigRational>>> ar, cr, dr, er;
335        GenPolynomial<GenPolynomial<BigRational>> b;
336
337        ar = rfac.random(kl, ll, el, q);
338        b = cfac.random(kl, ll + 2, el, q);
339        //System.out.println("ar = " + ar);
340        //System.out.println("b  = " + b);
341
342        cr = PolyGBUtil.<BigRational> coefficientPseudoRemainder(ar, b);
343        //System.out.println("cr = " + cr);
344        assertTrue("deg(c) < deg(a): ", cr.degree(0) <= ar.degree(0) || ar.degree(0) == 0);
345        assertTrue("deg(lfcd(c)) < deg(b): ",
346                        cr.leadingBaseCoefficient().degree(0) < b.degree(0) || b.degree(0) == 0);
347
348        dr = ar.multiply(b);
349        //System.out.println("dr = " + dr);
350        cr = PolyGBUtil.<BigRational> coefficientPseudoRemainder(dr, b);
351        //System.out.println("cr = " + cr);
352        assertTrue("c == 0: ", cr.isZERO());
353
354        long s = ar.degree(0);
355        er = rfac.univariate(0, s + 1);
356        ////System.out.println("er = " + er);
357        er = er.multiply(b.multiply(cfac.fromInteger(100)));
358        er = er.sum(ar);
359        //System.out.println("er = " + er);
360        cr = PolyGBUtil.<BigRational> coefficientPseudoRemainder(er, b);
361        //System.out.println("cr = " + cr);
362        assertTrue("deg(c) < deg(a): ", cr.degree(0) < er.degree(0));
363    }
364
365}