001/*
002 * $Id: GenPolynomialTest.java 5946 2018-10-27 10:24:27Z kredel $
003 */
004
005package edu.jas.poly;
006
007
008import java.util.ArrayList;
009import java.util.Iterator;
010import java.util.List;
011import java.util.Map;
012import java.util.TreeMap;
013import java.util.SortedMap;
014import java.util.Spliterator;
015import java.util.stream.Stream;
016import java.util.stream.Collectors;
017import java.util.concurrent.ForkJoinPool;
018
019import junit.framework.Test;
020import junit.framework.TestCase;
021import junit.framework.TestSuite;
022
023import edu.jas.arith.BigInteger;
024import edu.jas.arith.ModInteger;
025import edu.jas.arith.ModIntegerRing;
026import edu.jas.arith.BigRational;
027import edu.jas.structure.RingElem;
028import edu.jas.structure.UnaryFunctor;
029import edu.jas.util.ListUtil;
030import edu.jas.util.MapEntry;
031
032
033/**
034 * GenPolynomial tests with JUnit.
035 * @author Heinz Kredel
036 */
037
038public class GenPolynomialTest extends TestCase {
039
040
041    /**
042     * main
043     */
044    public static void main(String[] args) {
045        junit.textui.TestRunner.run(suite());
046    }
047
048
049    /**
050     * Constructs a <CODE>GenPolynomialTest</CODE> object.
051     * @param name String.
052     */
053    public GenPolynomialTest(String name) {
054        super(name);
055    }
056
057
058    /**
059     * suite.
060     */
061    public static Test suite() {
062        TestSuite suite = new TestSuite(GenPolynomialTest.class);
063        return suite;
064    }
065
066
067    int rl = 6;
068
069
070    int kl = 5;
071
072
073    int ll = 7;
074
075
076    int el = 4;
077
078
079    float q = 0.5f;
080
081
082    @Override
083    protected void setUp() {
084    }
085
086
087    @Override
088    protected void tearDown() {
089    }
090
091
092    /**
093     * Test constructors and factory.
094     */
095    public void testConstructors() {
096        // rational numbers
097        BigRational rf = new BigRational();
098        // System.out.println("rf = " + rf);
099
100        //BigRational r = rf.fromInteger( 99 );
101        // System.out.println("r = " + r);
102        //r = rf.random( 9 ).sum(r);
103        // System.out.println("r = " + r);
104        //assertFalse("r.isZERO() = ", r.isZERO());
105
106        //RingElem<BigRational> re = new BigRational( 3 );
107        // System.out.println("re = " + re);
108        //rf = (BigRational) re;
109
110        //RingFactory<BigRational> ref = new BigRational( 3 );
111        // System.out.println("re = " + re);
112        //rf = (BigRational) ref;
113
114        // polynomials over rational numbers
115        GenPolynomialRing<BigRational> pf = new GenPolynomialRing<BigRational>(rf, 2);
116        // System.out.println("pf = " + pf);
117
118        GenPolynomial<BigRational> p = pf.getONE();
119        // System.out.println("p = " + p);
120        p = pf.random(9);
121        // System.out.println("p = " + p);
122        p = pf.getZERO();
123        // System.out.println("p = " + p);
124
125        RingElem<GenPolynomial<BigRational>> pe = new GenPolynomial<BigRational>(pf);
126        //System.out.println("pe = " + pe);
127        //System.out.println("p.equals(pe) = " + p.equals(pe) );
128        //System.out.println("p.equals(p) = " + p.equals(p) );
129        assertTrue("p.equals(pe) = ", p.equals(pe));
130        assertTrue("p.equals(p) = ", p.equals(p));
131
132        pe = pe.sum(p); // why not p = p.add(pe) ?
133        //System.out.println("pe = " + pe);
134        assertTrue("pe.isZERO() = ", pe.isZERO());
135        p = pf.random(9);
136        p = p.subtract(p);
137        //System.out.println("p = " + p);
138        //System.out.println("p.isZERO() = " + p.isZERO());
139        assertTrue("p.isZERO() = ", p.isZERO());
140
141        // polynomials over (polynomials over rational numbers)
142        GenPolynomialRing<GenPolynomial<BigRational>> ppf = new GenPolynomialRing<GenPolynomial<BigRational>>(
143                                                                                                              pf, 3);
144        // System.out.println("ppf = " + ppf);
145
146        GenPolynomial<GenPolynomial<BigRational>> pp = ppf.getONE();
147        // System.out.println("pp = " + pp);
148        pp = ppf.random(2);
149        // System.out.println("pp = " + pp);
150        pp = ppf.getZERO();
151        // System.out.println("pp = " + pp);
152
153        RingElem<GenPolynomial<GenPolynomial<BigRational>>> ppe = new GenPolynomial<GenPolynomial<BigRational>>(
154                                                                                                                ppf);
155        // System.out.println("ppe = " + ppe);
156        // System.out.println("pp.equals(ppe) = " + pp.equals(ppe) );
157        // System.out.println("pp.equals(pp) = " + pp.equals(pp) );
158        assertTrue("pp.equals(ppe) = ", pp.equals(ppe));
159        assertTrue("pp.equals(pp) = ", pp.equals(pp));
160
161        ppe = ppe.sum(pp); // why not pp = pp.add(ppe) ?
162        //System.out.println("ppe = " + ppe);
163        assertTrue("ppe.isZERO() = ", ppe.isZERO());
164        pp = ppf.random(2);
165        pp = pp.subtract(pp);
166        //System.out.println("pp = " + pp);
167        //System.out.println("pp.isZERO() = " + pp.isZERO());
168        assertTrue("pp.isZERO() = ", pp.isZERO());
169
170        // polynomials over (polynomials over (polynomials over rational numbers))
171        GenPolynomialRing<GenPolynomial<GenPolynomial<BigRational>>> pppf = new GenPolynomialRing<GenPolynomial<GenPolynomial<BigRational>>>(
172                                                                                                                                             ppf, 4);
173        // System.out.println("pppf = " + pppf);
174
175        GenPolynomial<GenPolynomial<GenPolynomial<BigRational>>> ppp = pppf.getONE();
176        //System.out.println("ppp = " + ppp);
177        ppp = pppf.random(2);
178        // System.out.println("ppp = " + ppp);
179        ppp = pppf.getZERO();
180        // System.out.println("ppp = " + ppp);
181
182        RingElem<GenPolynomial<GenPolynomial<GenPolynomial<BigRational>>>> pppe = new GenPolynomial<GenPolynomial<GenPolynomial<BigRational>>>(
183                                                                                                                                               pppf);
184        // System.out.println("pppe = " + pppe);
185        // System.out.println("ppp.equals(pppe) = " + ppp.equals(pppe) );
186        // System.out.println("ppp.equals(ppp) = " + ppp.equals(ppp) );
187        assertTrue("ppp.equals(pppe) = ", ppp.equals(pppe));
188        assertTrue("ppp.equals(ppp) = ", ppp.equals(ppp));
189
190        pppe = pppe.sum(ppp); // why not ppp = ppp.add(pppe) ?
191        // System.out.println("pppe = " + pppe);
192        assertTrue("pppe.isZERO() = ", pppe.isZERO());
193        ppp = pppf.random(2);
194        ppp = ppp.subtract(ppp);
195        // System.out.println("ppp = " + ppp);
196        // System.out.println("ppp.isZERO() = " + ppp.isZERO());
197        assertTrue("ppp.isZERO() = ", ppp.isZERO());
198
199        // some tests
200        //GenPolynomial<BigRational> pfx = new GenPolynomial<BigRational>();
201        //System.out.println("pfx = " + pfx);
202    }
203
204
205    /**
206     * Test extension and contraction.
207     */
208    public void testExtendContract() {
209        // rational numbers
210        BigRational cf = new BigRational(99);
211        // System.out.println("cf = " + cf);
212
213        // polynomials over rational numbers
214        GenPolynomialRing<BigRational> pf = new GenPolynomialRing<BigRational>(cf, rl);
215        // System.out.println("pf = " + pf);
216
217        GenPolynomial<BigRational> a = pf.random(kl, ll, el, q);
218        //System.out.println("a = " + a);
219
220        int k = rl;
221        GenPolynomialRing<BigRational> pfe = pf.extend(k);
222        GenPolynomialRing<BigRational> pfec = pfe.contract(k);
223        assertEquals("pf == pfec", pf, pfec);
224
225        GenPolynomial<BigRational> ae = a.extend(pfe, 0, 0);
226
227        Map<ExpVector, GenPolynomial<BigRational>> m = ae.contract(pfec);
228        List<GenPolynomial<BigRational>> ml = new ArrayList<GenPolynomial<BigRational>>(m.values());
229        GenPolynomial<BigRational> aec = ml.get(0);
230        assertEquals("a == aec", a, aec);
231        //System.out.println("ae = " + ae);
232        //System.out.println("aec = " + aec);
233    }
234
235
236    /**
237     * Test reversion.
238     */
239    public void testReverse() {
240        // rational numbers
241        BigRational cf = new BigRational(99);
242        // System.out.println("cf = " + cf);
243
244        // polynomials over rational numbers
245        GenPolynomialRing<BigRational> pf = new GenPolynomialRing<BigRational>(cf, rl);
246        //System.out.println("pf = " + pf);
247
248        GenPolynomial<BigRational> a = pf.random(kl, ll, el, q);
249        //System.out.println("a = " + a);
250
251        GenPolynomialRing<BigRational> pfr = pf.reverse();
252        GenPolynomialRing<BigRational> pfrr = pfr.reverse();
253        assertEquals("pf == pfrr", pf, pfrr);
254        //System.out.println("pfr = " + pfr);
255
256        GenPolynomial<BigRational> ar = a.reverse(pfr);
257        GenPolynomial<BigRational> arr = ar.reverse(pfrr);
258        assertEquals("a == arr", a, arr);
259        //System.out.println("ar = " + ar);
260        //System.out.println("arr = " + arr);
261    }
262
263
264    /**
265     * Test accessors.
266     */
267    public void testAccessors() {
268        // rational numbers
269        BigRational rf = new BigRational();
270        // System.out.println("rf = " + rf);
271
272        // polynomials over rational numbers
273        GenPolynomialRing<BigRational> pf = new GenPolynomialRing<BigRational>(rf, rl);
274        // System.out.println("pf = " + pf);
275
276        // test 1
277        GenPolynomial<BigRational> p = pf.getONE();
278        // System.out.println("p = " + p);
279
280        ExpVector e = p.leadingExpVector();
281        BigRational c = p.leadingBaseCoefficient();
282
283        GenPolynomial<BigRational> f = new GenPolynomial<BigRational>(pf, c, e);
284        assertEquals("1 == 1 ", p, f);
285
286        GenPolynomial<BigRational> r = p.reductum();
287        assertTrue("red(1) == 0 ", r.isZERO());
288
289        // test 0
290        p = pf.getZERO();
291        // System.out.println("p = " + p);
292        e = p.leadingExpVector();
293        c = p.leadingBaseCoefficient();
294
295        f = new GenPolynomial<BigRational>(pf, c, e);
296        assertEquals("0 == 0 ", p, f);
297
298        r = p.reductum();
299        assertTrue("red(0) == 0 ", r.isZERO());
300
301        // test random
302        p = pf.random(kl, 2 * ll, el, q);
303        // System.out.println("p = " + p);
304        e = p.leadingExpVector();
305        c = p.leadingBaseCoefficient();
306        r = p.reductum();
307
308        f = new GenPolynomial<BigRational>(pf, c, e);
309        f = r.sum(f);
310        assertEquals("p == lm(f)+red(f) ", p, f);
311
312        // test iteration over random
313        GenPolynomial<BigRational> g;
314        g = p;
315        f = pf.getZERO();
316        while (!g.isZERO()) {
317            e = g.leadingExpVector();
318            c = g.leadingBaseCoefficient();
319            //System.out.println("c e = " + c + " " + e);
320            r = g.reductum();
321            f = f.sum(c, e);
322            g = r;
323        }
324        assertEquals("p == lm(f)+lm(red(f))+... ", p, f);
325    }
326
327
328    /**
329     * Test homogeneous.
330     */
331    public void testHomogeneous() {
332        // rational numbers
333        BigRational rf = new BigRational();
334        // System.out.println("rf = " + rf);
335
336        // polynomials over rational numbers
337        GenPolynomialRing<BigRational> pf = new GenPolynomialRing<BigRational>(rf, 2); //rl);
338        // System.out.println("pf = " + pf);
339
340        // test 1
341        GenPolynomial<BigRational> p = pf.getONE();
342        // System.out.println("p = " + p);
343        assertTrue("1 is homogeneous " + p, p.isHomogeneous());
344
345        // test 0
346        p = pf.getZERO();
347        // System.out.println("p = " + p);
348        assertTrue("0 is homogeneous " + p, p.isHomogeneous());
349
350        // test random
351        p = pf.random(kl, 2 * ll, el, q);
352        //p = pf.random(kl,ll,el,q);
353        //System.out.println("p = " + p);
354        assertFalse("rnd is homogeneous " + p, p.isHomogeneous());
355
356        // make homogeneous
357        GenPolynomialRing<BigRational> pfh = pf.extend(1);
358        //System.out.println("pfh = " + pfh);
359        // remove block order
360        TermOrder to = new TermOrder(TermOrder.IGRLEX);
361        pfh = new GenPolynomialRing<BigRational>(pfh, to);
362        //System.out.println("pfh = " + pfh);
363
364        GenPolynomial<BigRational> ph = p.homogenize(pfh);
365        //System.out.println("ph = " + ph);
366        assertTrue("ph is homogeneous " + ph, ph.isHomogeneous());
367        GenPolynomial<BigRational> ps = ph.deHomogenize(pf);
368        //System.out.println("ps = " + ps);
369        assertEquals("phs == p ", ps, p); // findbugs
370        //System.out.println("p is homogeneous = " + p.isHomogeneous());
371        //System.out.println("ph is homogeneous = " + ph.isHomogeneous());
372
373        GenPolynomial<BigRational> s = pf.random(kl, ll, el, q);
374        //System.out.println("s = " + s);
375        assertFalse("rnd is homogeneous " + s, s.isHomogeneous() && !s.isConstant());
376
377        GenPolynomial<BigRational> sh = s.homogenize(pfh);
378        //System.out.println("sh = " + sh);
379        assertTrue("sh is homogeneous " + sh, sh.isHomogeneous());
380        GenPolynomial<BigRational> ss = sh.deHomogenize(pf);
381        //System.out.println("ss = " + ss);
382        assertEquals("ss = s ", ss, s);
383
384        GenPolynomial<BigRational> th = ph.multiply(sh);
385        //System.out.println("th = " + th);
386        assertTrue("th is homogeneous " + th, th.isHomogeneous());
387
388        GenPolynomial<BigRational> t = p.multiply(s);
389        //System.out.println("t = " + t);
390
391        GenPolynomial<BigRational> ts = th.deHomogenize(pf);
392        //System.out.println("ts = " + ts);
393        assertEquals("ts = t ", ts, t);
394    }
395
396
397    /**
398     * Test weight homogeneous.
399     */
400    public void testWeightHomogeneous() {
401        // rational numbers
402        BigRational rf = new BigRational();
403        // System.out.println("rf = " + rf);
404
405        // weight term order
406        long[] weight = new long[] { 2, 3, 4, 5 };
407        TermOrder to = TermOrderByName.weightOrder(weight);
408        // System.out.println("to = " + to);
409
410        // polynomials over rational numbers
411        GenPolynomialRing<BigRational> pf = new GenPolynomialRing<BigRational>(rf, 4, to);
412        // System.out.println("pf = " + pf.toScript());
413
414        // test 1
415        GenPolynomial<BigRational> p = pf.getONE();
416        // System.out.println("p = " + p);
417        assertTrue("1 is weight homogeneous " + p, p.isWeightHomogeneous());
418
419        // test 0
420        p = pf.getZERO();
421        // System.out.println("p = " + p);
422        assertTrue("0 is weight homogeneous " + p, p.isWeightHomogeneous());
423
424        // test random
425        p = pf.random(kl, 3 * ll, el, q);
426        // System.out.println("p = " + p);
427        assertFalse("rnd is weight homogeneous " + p, p.isWeightHomogeneous());
428
429        GenPolynomial<BigRational> pl = p.leadingWeightPolynomial();
430        // System.out.println("pl = " + pl);
431        assertTrue("lw(rnd) is weight homogeneous " + pl, pl.isWeightHomogeneous());
432
433        GenPolynomial<BigRational> r = pf.random(kl, 3 * ll, el, q);
434        // System.out.println("r = " + r);
435        assertFalse("rnd is weight homogeneous " + r, r.isWeightHomogeneous());
436
437        GenPolynomial<BigRational> rl = r.leadingWeightPolynomial();
438        // System.out.println("rl = " + rl);
439        assertTrue("lw(rnd) is weight homogeneous " + rl, rl.isWeightHomogeneous());
440
441        GenPolynomial<BigRational> t = pl.multiply(rl);
442        // System.out.println("t = " + t);
443        assertTrue("lw(rnd)*lw(rnd) is weight homogeneous " + t, t.isWeightHomogeneous());
444    }
445
446
447    /**
448     * Test univariate.
449     */
450    public void testUnivariate() {
451        BigInteger rf = new BigInteger();
452        // System.out.println("rf = " + rf);
453
454        // polynomials over integral numbers
455        GenPolynomialRing<BigInteger> pf = new GenPolynomialRing<BigInteger>(rf, rl);
456        //System.out.println("pf = " + pf);
457
458        GenPolynomial<BigInteger> a, b, c, d;
459
460        // x**1
461        a = pf.univariate(pf.nvar - 1);
462        //System.out.println("a = " + a);
463        assertTrue("deg(a) = 1: ", a.degree() == 1);
464        assertEquals("xi == xi: ", pf.vars[0], a.toString());
465
466        b = pf.univariate(pf.vars[0]);
467        //System.out.println("b = " + b);
468        assertTrue("deg(b) = 1: ", b.degree() == 1);
469        assertEquals("xi == xi: ", pf.vars[0], b.toString());
470
471        c = pf.univariate(0);
472        //System.out.println("c = " + c);
473        assertTrue("deg(c) = 1: ", c.degree() == 1);
474        assertEquals("xi == xi: ", pf.vars[pf.nvar - 1], c.toString());
475
476        d = pf.univariate(pf.vars[pf.nvar - 1]);
477        //System.out.println("d = " + d);
478        assertTrue("deg(c) = 1: ", c.degree() == 1);
479        assertEquals("xi == xi: ", pf.vars[pf.nvar - 1], d.toString());
480
481        // x**7
482        a = pf.univariate(pf.nvar - 1, 7);
483        //System.out.println("a = " + a);
484        assertTrue("deg(a) = 7: ", a.degree() == 7);
485        assertEquals("xi == xi: ", pf.vars[0] + "^7", a.toString());
486
487        b = pf.univariate(pf.vars[0], 7);
488        //System.out.println("b = " + b);
489        assertTrue("deg(b) = 7: ", b.degree() == 7);
490        assertEquals("xi == xi: ", pf.vars[0] + "^7", b.toString());
491
492        c = pf.univariate(0, 7);
493        //System.out.println("c = " + c);
494        assertTrue("deg(c) = 7: ", c.degree() == 7);
495        assertEquals("xi == xi: ", pf.vars[pf.nvar - 1] + "^7", c.toString());
496
497        d = pf.univariate(pf.vars[pf.nvar - 1], 7);
498        //System.out.println("d = " + d);
499        assertTrue("deg(c) = 7: ", c.degree() == 7);
500        assertEquals("xi == xi: ", pf.vars[pf.nvar - 1] + "^7", d.toString());
501    }
502
503
504    /**
505     * Test iterators.
506     */
507    public void testIterators() {
508        // integers
509        BigInteger rf = new BigInteger();
510        // System.out.println("rf = " + rf);
511
512        // polynomials over integral numbers
513        GenPolynomialRing<BigInteger> pf = new GenPolynomialRing<BigInteger>(rf, rl);
514        // System.out.println("pf = " + pf);
515
516        // random polynomial
517        GenPolynomial<BigInteger> p = pf.random(kl, 2 * ll, el, q);
518        //System.out.println("p = " + p);
519
520        // test monomials
521        for (Monomial<BigInteger> m : p) {
522            //System.out.println("m = " + m);
523            assertFalse("m.c == 0 ", m.coefficient().isZERO());
524            assertFalse("m.e < (0) ", m.exponent().signum() < 0);
525        }
526
527        // test exponents
528        Iterator<ExpVector> et = p.exponentIterator();
529        while (et.hasNext()) {
530            ExpVector e = et.next();
531            //System.out.println("e = " + e);
532            assertFalse("e < (0) ", e.signum() < 0);
533        }
534
535        // test coefficents
536        Iterator<BigInteger> ct = p.coefficientIterator();
537        while (ct.hasNext()) {
538            BigInteger i = ct.next();
539            //System.out.println("i = " + i);
540            assertFalse("i == 0 ", i.isZERO());
541        }
542    }
543
544
545    /**
546     * Test spliterators.
547     */
548    public void testSpliterators() {
549        // integers
550        BigInteger rf = new BigInteger();
551        BigInteger num = rf.fromInteger(1);
552        // polynomials over integral numbers
553        GenPolynomialRing<BigInteger> pf = new GenPolynomialRing<BigInteger>(rf, rl);
554        //System.out.println("pf = " + pf.toScript());
555        // random polynomial
556        GenPolynomial<BigInteger> p = pf.random(kl, 22 * ll, el, q);
557        //System.out.println("p = " + p.length());
558        List<BigInteger> coeffs = new ArrayList<BigInteger>();
559
560        // create spliterator and run on it
561        PolySpliterator<BigInteger> psplit = new PolySpliterator<BigInteger>(p.val);
562        //System.out.println("ps = " + psplit);
563        //System.out.println("ps = "); printCharacteristic(psplit);
564        //psplit.forEachRemaining( m -> System.out.print(m.c.toScript() + ", "));
565        //System.out.println("\n");
566        psplit.forEachRemaining( m -> coeffs.add(m.c.multiply(num)) );
567        assertTrue("#coeffs == size: ", p.val.size() == coeffs.size());
568        coeffs.clear();
569        
570        // create spliterator and split it
571        //psplit = new PolySpliterator<BigInteger>(p.val);
572        Spliterator<Monomial<BigInteger>> split = p.spliterator(); 
573        //System.out.println("ps = " + split);
574        //PolySpliterator<BigInteger> rest = split.trySplit();
575        Spliterator<Monomial<BigInteger>> rest = split.trySplit(); 
576        //System.out.println("rest = "); printCharacteristic(rest);
577        //System.out.println("ps = "); printCharacteristic(split);
578
579        //System.out.println("rest = " + rest);
580        //rest.forEachRemaining( m -> System.out.print(m.c.toScript() + ", "));
581        rest.forEachRemaining( m -> coeffs.add(m.c.multiply(num)) );
582        //System.out.println("\nps = " + split);
583        //split.forEachRemaining( m -> System.out.print(m.c.toScript() + ", "));
584        split.forEachRemaining( m -> coeffs.add(m.c.multiply(num)) );
585        assertTrue("#coeffs == size: ", p.val.size() == coeffs.size());
586
587        assertTrue("coeffs == p.coefficients: ", ListUtil.<BigInteger>equals(coeffs,p.val.values()));
588    }
589
590
591    void printCharacteristic(Spliterator sp) {
592        for (int c = 0x0; c < 0x4000; c++) {
593            if (sp.hasCharacteristics(c)) {
594                System.out.println(String.format("char: %3x", c));
595            }
596        }
597    }
598
599    
600    /**
601     * Test coefficient map function.
602     */
603    public void testMap() {
604        // integers
605        BigInteger rf = new BigInteger();
606        // System.out.println("rf = " + rf);
607
608        // polynomials over integral numbers
609        GenPolynomialRing<BigInteger> pf = new GenPolynomialRing<BigInteger>(rf, rl);
610        // System.out.println("pf = " + pf);
611
612        // random polynomial
613        GenPolynomial<BigInteger> p = pf.random(kl, 2 * ll, el, q);
614        //System.out.println("p = " + p);
615
616        // test times 1
617        GenPolynomial<BigInteger> q;
618        q = p.map(new Multiply<BigInteger>(rf.getONE()));
619        assertEquals("p == q ", p, q);
620
621        // test times 0
622        q = p.map(new Multiply<BigInteger>(rf.getZERO()));
623        assertTrue("q == 0: " + q, q.isZERO());
624
625        // test times -1
626        q = p.map(new Multiply<BigInteger>(rf.getONE().negate()));
627        assertEquals("p == q ", p.negate(), q);
628    }
629
630    
631    /**
632     * Test streams.
633     */
634    public void testStreams() {
635        // modular integers
636        ModIntegerRing rf = new ModIntegerRing(32003);
637        ModInteger num = rf.fromInteger(-1);
638        // polynomials over integral numbers
639        GenPolynomialRing<ModInteger> pf = new GenPolynomialRing<ModInteger>(rf, rl);
640        //System.out.println("pf = " + pf.toScript());
641        // random polynomial
642        GenPolynomial<ModInteger> p = pf.random(kl, 222 * ll, 2+el, q);
643        //System.out.println("p = " + p.length());
644        GenPolynomial<ModInteger> q;
645
646        // negate coefficients 
647        long tn = System.nanoTime();
648        q = p.negate();
649        tn = System.nanoTime() - tn;
650        //System.out.println("q = " + q.length() + ", neg time = " + tn);
651        assertTrue("time >= 0 ", tn >= 0);
652        //System.out.println("p+q = " + p.sum(q));
653        assertTrue("p+q == 0 ", p.sum(q).isZERO());
654
655        // map multiply to coefficients
656        long tm = System.nanoTime();
657        q = p.map(c -> c.multiply(num));
658        tm = System.nanoTime() - tm;
659        //System.out.println("q = " + q.length() + ", old time = " + tm);
660        assertTrue("time >= 0 ", tm >= 0);
661        //System.out.println("p+q = " + p.sum(q));
662        assertTrue("p+q == 0 ", p.sum(q).isZERO());
663
664        // map multiply to coefficients stream
665        long ts = System.nanoTime();
666        q = p.mapOnStream(me -> new MapEntry<ExpVector,ModInteger>(me.getKey(), me.getValue().multiply(num)), false);
667        ts = System.nanoTime() - ts;
668        //System.out.println("q = " + q.length() + ", seq time = " + ts);
669        assertTrue("time >= 0 ", ts >= 0);
670        //System.out.println("p+q = " + p.sum(q));
671        assertTrue("p+q == 0 ", p.sum(q).isZERO());
672
673        // map multiply to coefficients parallel stream
674        long tp = System.nanoTime();
675        q = p.mapOnStream(me -> new MapEntry<ExpVector,ModInteger>(me.getKey(), me.getValue().multiply(num)), true);
676        tp = System.nanoTime() - tp;
677        //System.out.println("q = " + q.length() + ", par time = " + tp);
678        assertTrue("time >= 0 ", tp >= 0);
679        //System.out.println("p+q = " + p.sum(q));
680        assertTrue("p+q == 0 ", p.sum(q).isZERO());
681        System.out.println("map time: neg, old, seq, par, = " + tn + ", " + tm + ", " + ts + ", " + tp);
682
683        //System.out.println("ForkJoinPool: " + ForkJoinPool.commonPool());
684    }
685
686    
687    /**
688     * Test bitLength.
689     */
690    public void testBitLength() {
691        // integers
692        BigInteger rf = new BigInteger();
693        // System.out.println("rf = " + rf);
694
695        // polynomials over integral numbers
696        GenPolynomialRing<BigInteger> pf = new GenPolynomialRing<BigInteger>(rf, 5);
697        // System.out.println("pf = " + pf);
698
699        GenPolynomial<BigInteger> a, b, c;
700        a = pf.getZERO();
701        assertEquals("blen(0) = 0", 0, a.bitLength());
702
703        a = pf.getONE();
704        assertEquals("blen(1) = 7", 7, a.bitLength());
705
706        // random polynomials
707        a = pf.random(kl, 2 * ll, el, q);
708        //System.out.println("a = " + a);
709        //System.out.println("blen(a) = " + a.bitLength());
710        assertTrue("blen(random) >= 0", 0 <= a.bitLength());
711
712        b = pf.random(kl, 2 * ll, el, q);
713        //System.out.println("b = " + b);
714        //System.out.println("blen(b) = " + b.bitLength());
715        assertTrue("blen(random) >= 0", 0 <= b.bitLength());
716
717        //c = a.multiply(b);
718        c = a.sum(b);
719        //System.out.println("c = " + c);
720        //System.out.println("blen(a)+blen(b) = " + (a.bitLength()+b.bitLength()));
721        //System.out.println("blen(c) = " + c.bitLength());
722        assertTrue("blen(random) >= 0", 0 <= c.bitLength());
723        assertTrue("blen(random)+blen(random) >= blen(random+random)",
724                   a.bitLength() + b.bitLength() >= c.bitLength());
725    }
726
727}
728
729
730/**
731 * Internal scalar multiplication functor.
732 */
733class Multiply<C extends RingElem<C>> implements UnaryFunctor<C, C> {
734
735
736    C x;
737
738
739    public Multiply(C x) {
740        this.x = x;
741    }
742
743
744    public C eval(C c) {
745        return c.multiply(x);
746    }
747}