001/*
002 * $Id: MultiVarPowerSeriesTest.java 4125 2012-08-19 19:05:22Z kredel $
003 */
004
005package edu.jas.ps;
006
007
008import java.util.ArrayList;
009import java.util.List;
010import java.util.Map;
011
012import junit.framework.Test;
013import junit.framework.TestCase;
014import junit.framework.TestSuite;
015
016import edu.jas.arith.BigRational;
017import edu.jas.poly.ExpVector;
018import edu.jas.poly.GenPolynomial;
019import edu.jas.poly.GenPolynomialRing;
020
021
022/**
023 * Multivariate power series tests with JUnit.
024 * @author Heinz Kredel.
025 */
026
027public class MultiVarPowerSeriesTest extends TestCase {
028
029
030    /**
031     * main.
032     */
033    public static void main(String[] args) {
034        junit.textui.TestRunner.run(suite());
035    }
036
037
038    /**
039     * Constructs a <CODE>MultiVarPowerSeriesTest</CODE> object.
040     * @param name String.
041     */
042    public MultiVarPowerSeriesTest(String name) {
043        super(name);
044    }
045
046
047    /**
048     */
049    public static Test suite() {
050        TestSuite suite = new TestSuite(MultiVarPowerSeriesTest.class);
051        return suite;
052    }
053
054
055    MultiVarPowerSeriesRing<BigRational> fac;
056
057
058    MultiVarPowerSeries<BigRational> a;
059
060
061    MultiVarPowerSeries<BigRational> b;
062
063
064    MultiVarPowerSeries<BigRational> c;
065
066
067    MultiVarPowerSeries<BigRational> d;
068
069
070    MultiVarPowerSeries<BigRational> e;
071
072
073    MultiVarPowerSeries<BigRational> f;
074
075
076    int rl = 2;
077
078
079    int kl = 10;
080
081
082    float q = 0.3f;
083
084
085    @Override
086    protected void setUp() {
087        a = b = c = d = e = null;
088        String[] vars = new String[] { "x", "y" };
089        fac = new MultiVarPowerSeriesRing<BigRational>(new BigRational(1), rl, vars);
090        //System.out.println("fac = " + fac);
091        //System.out.println("fac = " + fac.toScript());
092    }
093
094
095    @Override
096    protected void tearDown() {
097        a = b = c = d = e = null;
098        fac = null;
099    }
100
101
102    /**
103     * Test MultiVarCoefficients.
104     * 
105     */
106    public void testCoefficients() {
107        BigRational cf = new BigRational(0);
108        GenPolynomialRing<BigRational> pring = new GenPolynomialRing<BigRational>(cf, rl);
109
110        MultiVarCoefficients<BigRational> zeros = new Zeros(pring);
111        MultiVarCoefficients<BigRational> ones = new Ones(pring);
112        MultiVarCoefficients<BigRational> vars = new Vars(pring);
113
114        int m = 5;
115        ExpVectorIterable eiter = new ExpVectorIterable(rl, true, m);
116        for (ExpVector e : eiter) {
117            BigRational c = zeros.get(e);
118            //System.out.println("c = " + c + ", e = " + e);
119            assertTrue("isZERO( c )", c.isZERO());
120        }
121        //System.out.println("coeffCache = " + zeros.coeffCache);
122        //System.out.println("zeroCache  = " + zeros.zeroCache);
123        assertTrue("coeffCache is one element", zeros.coeffCache.size() == (m + 1));
124
125        for (ExpVector e : eiter) {
126            BigRational c = ones.get(e);
127            //System.out.println("c = " + c + ", e = " + e);
128            assertTrue("isONE( c )", c.isONE());
129        }
130        //System.out.println("coeffCache = " + ones.coeffCache);
131        //System.out.println("zeroCache  = " + ones.zeroCache);
132        assertTrue("zeroCache is empty", ones.zeroCache.isEmpty());
133
134        for (int i = 0; i <= m; i++) {
135            GenPolynomial<BigRational> c = ones.getHomPart(i);
136            //System.out.println("c = " + c + ", i = " + i);
137            GenPolynomial<BigRational> d = ones.getHomPart(i);
138            //System.out.println("d = " + d + ", i = " + i);
139            assertTrue("c.equals(d) ", c.equals(d));
140        }
141        //System.out.println("coeffCache = " + ones.coeffCache);
142        //System.out.println("zeroCache  = " + ones.zeroCache);
143        //System.out.println("homCheck   = " + ones.homCheck);
144        //System.out.println("homCheck   = " + ones.homCheck.length());
145        assertTrue("zeroCache is empty", ones.zeroCache.isEmpty());
146        assertTrue("#coeffCache = " + m, ones.coeffCache.size() == (m + 1));
147        assertTrue("#homCheck = " + m, ones.homCheck.length() == (m + 1));
148
149        for (int i = 0; i <= m; i++) {
150            GenPolynomial<BigRational> c = vars.getHomPart(i);
151            //System.out.println("c = " + c + ", i = " + i);
152            assertTrue("c==0 || deg(c)==1 ", c.isZERO() || c.degree() == 1L);
153        }
154        //System.out.println("coeffCache = " + vars.coeffCache);
155        //System.out.println("zeroCache  = " + vars.zeroCache);
156        //System.out.println("homCheck   = " + vars.homCheck);
157        //System.out.println("homCheck   = " + vars.homCheck.length());
158        //no-more: assertTrue("zeroCache is not empty", !vars.zeroCache.isEmpty());
159        assertTrue("#coeffCache = " + m, vars.coeffCache.size() == (m + 1));
160        assertTrue("#homCheck = " + m, vars.homCheck.length() == (m + 1));
161    }
162
163
164    /**
165     * Test constructor and generators.
166     * 
167     */
168    public void testConstruction() {
169        //System.out.println("fac = " + fac);
170        //System.out.println("fac = " + fac.toScript());
171
172        c = fac.getONE();
173        //System.out.println("c = " + c);
174        assertTrue("isZERO( c )", !c.isZERO());
175        assertTrue("isONE( c )", c.isONE());
176
177        d = fac.getZERO();
178        //System.out.println("d = " + d);
179        assertTrue("isZERO( d )", d.isZERO());
180        assertTrue("isONE( d )", !d.isONE());
181
182        List<MultiVarPowerSeries<BigRational>> gens = fac.generators();
183        assertTrue("#gens == rl+1 ", rl + 1 == gens.size());
184        for (MultiVarPowerSeries<BigRational> p : gens) {
185            //System.out.println("p = " + p);
186            assertTrue("red(p) == 0 ", p.reductum().isZERO());
187        }
188
189        a = fac.copy(c);
190        b = c.copy();
191        assertEquals("copy(c) == c.clone() ", a, b);
192
193        a = fac.fromInteger(1);
194        assertEquals("1 == fromInteger(1) ", a, c);
195
196        b = fac.fromInteger(java.math.BigInteger.ONE);
197        assertEquals("1 == fromInteger(1) ", b, c);
198
199    }
200
201
202    /**
203     * Test random polynomial.
204     */
205    public void testRandom() {
206        for (int i = 0; i < 5; i++) {
207            a = fac.random(i + 2);
208            //System.out.println("a = " + a);
209            assertTrue(" not isZERO( a" + i + " )", !a.isZERO());
210            assertTrue(" not isONE( a" + i + " )", !a.isONE());
211        }
212    }
213
214
215    /**
216     * Test MultiVarCoefficients in power series.
217     * 
218     */
219    public void testCoefficientsInPS() {
220
221        MultiVarCoefficients<BigRational> zeros = new Zeros(fac);
222        MultiVarCoefficients<BigRational> ones = new Ones(fac);
223        MultiVarCoefficients<BigRational> vars = new Vars(fac);
224
225        a = new MultiVarPowerSeries<BigRational>(fac, zeros);
226        b = new MultiVarPowerSeries<BigRational>(fac, ones);
227        c = new MultiVarPowerSeries<BigRational>(fac, vars);
228
229        int m = 5;
230        ExpVectorIterable eiter = new ExpVectorIterable(rl, true, m);
231        for (ExpVector e : eiter) {
232            BigRational r = a.coefficient(e);
233            //System.out.println("r = " + r + ", e = " + e);
234            assertTrue("isZERO( r )", r.isZERO());
235        }
236        //System.out.println("#a = " + a.lazyCoeffs.coeffCache);
237        assertTrue("coeffCache is one element", a.lazyCoeffs.coeffCache.size() == (m + 1));
238        assertTrue("isZERO( a )", a.isZERO()); // after previous
239
240        for (ExpVector e : eiter) {
241            BigRational r = b.coefficient(e);
242            //System.out.println("r = " + r + ", e = " + e);
243            assertTrue("isONE( r )", r.isONE());
244        }
245        assertTrue("zeroCache is empty", b.lazyCoeffs.zeroCache.isEmpty());
246
247        for (int i = 0; i <= m; i++) {
248            GenPolynomial<BigRational> p = b.homogeneousPart(i);
249            //System.out.println("p = " + p + ", i = " + i);
250            GenPolynomial<BigRational> q = b.homogeneousPart(i);
251            //System.out.println("q = " + q + ", i = " + i);
252            assertTrue("p.equals(q) ", p.equals(q));
253        }
254        assertTrue("zeroCache is empty", b.lazyCoeffs.zeroCache.isEmpty());
255        assertTrue("#coeffCache = " + m, b.lazyCoeffs.coeffCache.size() == (m + 1));
256        assertTrue("#homCheck = " + m, b.lazyCoeffs.homCheck.length() == (m + 1));
257
258        for (int i = 0; i <= m; i++) {
259            GenPolynomial<BigRational> p = c.homogeneousPart(i);
260            //System.out.println("p = " + p + ", i = " + i);
261            assertTrue("p==0 || deg(p)==1 ", p.isZERO() || p.degree() == 1L);
262        }
263        //no-more:assertTrue("zeroCache is not empty", !c.lazyCoeffs.zeroCache.isEmpty());
264        assertTrue("#coeffCache = " + m, c.lazyCoeffs.coeffCache.size() == (m + 1));
265        assertTrue("#homCheck = " + m, c.lazyCoeffs.homCheck.length() == (m + 1));
266    }
267
268
269    /**
270     * Test addition.
271     * 
272     */
273    public void testAddition() {
274        a = fac.random(kl);
275        b = fac.random(kl);
276
277        c = a.sum(b);
278        d = b.sum(a);
279        assertEquals("a+b = b+a", c, d);
280
281        d = c.subtract(b);
282        assertEquals("a+b-b = a", a, d);
283        d = c.sum(b.negate());
284        assertEquals("a+b-b = a", a, d);
285
286        c = fac.random(kl);
287        d = a.sum(b.sum(c));
288        e = a.sum(b).sum(c);
289        assertEquals("a+(b+c) = (a+b)+c", d, e);
290
291        Map.Entry<ExpVector, BigRational> ma = a.orderMonomial();
292        c = a.reductum().sum(ma);
293        assertEquals("a = red(a)+om(a)", a, c);
294    }
295
296
297    /**
298     * Test multiplication.
299     * 
300     */
301    public void testMultiplication() {
302        a = fac.random(kl);
303        b = fac.random(kl);
304
305        if (a.isZERO() || b.isZERO()) {
306            return;
307        }
308        assertTrue("not isZERO( a )", !a.isZERO());
309        assertTrue("not isZERO( b )", !b.isZERO());
310
311        c = b.multiply(a);
312        d = a.multiply(b);
313        assertTrue("not isZERO( c )", !c.isZERO());
314        assertTrue("not isZERO( d )", !d.isZERO());
315
316        //System.out.println("a = " + a);
317        //System.out.println("b = " + b);
318        e = d.subtract(c);
319        assertTrue("isZERO( a*b-b*a ) " + e, e.isZERO());
320
321        assertTrue("a*b = b*a", c.equals(d));
322        assertEquals("a*b = b*a", c, d);
323
324        c = fac.random(kl);
325        //System.out.println("c = " + c);
326        d = a.multiply(b.multiply(c));
327        e = (a.multiply(b)).multiply(c);
328        //System.out.println("d = " + d);
329        //System.out.println("e = " + e);
330        //System.out.println("d-e = " + d.subtract(c) );
331
332        assertEquals("a(bc) = (ab)c", d, e);
333        assertTrue("a(bc) = (ab)c", d.equals(e));
334
335        ExpVector ev = ExpVector.random(rl, 5, 0.8f);
336        BigRational br = fac.coFac.random(5);
337
338        b = a.shift(ev).multiply(br);
339        c = a.multiply(br, ev);
340        assertEquals("(a ev) br = a (ev,br)", b, c);
341        //System.out.println("a  = " + a);
342        //System.out.println("ev = " + ev);
343        //System.out.println("br = " + br);
344        //System.out.println("b  = " + b);
345        //System.out.println("c  = " + c);
346    }
347
348
349    /**
350     * Test distributive law.
351     * 
352     */
353    public void testDistributive() {
354        a = fac.random(kl, q);
355        b = fac.random(kl, q);
356        c = fac.random(kl, q);
357
358        d = a.multiply(b.sum(c));
359        e = a.multiply(b).sum(a.multiply(c));
360
361        assertEquals("a(b+c) = ab+ac", d, e);
362    }
363
364
365    /**
366     * Test inverse.
367     * 
368     */
369    public void testInverse() {
370        a = fac.getONE();
371        assertTrue("not isZERO( a )", !a.isZERO());
372        assertTrue("isUnit( a )", a.isUnit());
373        //System.out.println("a = " + a);
374
375        b = a.inverse();
376        c = a.multiply(b);
377        assertTrue("isONE( c )", c.isONE());
378        //System.out.println("b = " + b);
379        //System.out.println("c = " + c);
380
381        a = fac.random(kl);
382        if (!a.isUnit()) {
383            a = fac.fromInteger(23); //return;
384        }
385        //System.out.println("a = " + a);
386        b = a.inverse();
387        c = a.multiply(b);
388        assertTrue("isONE( c )", c.isONE());
389        //System.out.println("b = " + b);
390        //System.out.println("c = " + c);
391
392        b = fac.random(kl);
393        c = b.divide(a);
394        d = c.multiply(a);
395        assertEquals("b/a * a == b ", d, b);
396    }
397
398
399    /**
400     * Test fix point constructions.
401     * 
402     */
403    public void testFixpoints() {
404        int r = 0;
405        UnivPowerSeriesRing<BigRational> ufac = new UnivPowerSeriesRing<BigRational>(fac.coFac, fac.vars[r]);
406        UnivPowerSeries<BigRational> exp = ufac.getEXP();
407        //System.out.println("exp = " + exp);
408
409        a = fac.fromPowerSeries(exp, 0);
410        b = fac.fromPowerSeries(exp, 1);
411        //System.out.println("a = " + a);
412        //System.out.println("b = " + b);
413
414        c = fac.getEXP(0);
415        d = fac.getEXP(1);
416        //System.out.println("c = " + c);
417        //System.out.println("d = " + d);
418        assertEquals("a == c ", a, c);
419        assertEquals("b == d ", b, d);
420
421        e = d.differentiate(0);
422        //System.out.println("e = " + e);
423        assertTrue("isZERO( e )", e.isZERO());
424        e = d.differentiate(1);
425        //System.out.println("e = " + e);
426        assertEquals("e == d ", d, e);
427    }
428
429
430    /**
431     * Test reductum.
432     * 
433     */
434    public void testReductum() {
435        a = fac.random(kl);
436        //System.out.println("a = " + a);
437
438        Map.Entry<ExpVector, BigRational> m = a.orderMonomial();
439        //System.out.println("m = " + m);
440        ExpVector k = m.getKey();
441        BigRational br = m.getValue();
442
443        b = fac.getONE().multiply(br, k);
444        //System.out.println("b = " + b);
445
446        c = a.reductum();
447        //System.out.println("c = " + c);
448
449        d = c.sum(b);
450        //System.out.println("d = " + d);
451        assertEquals("a = red(a)+1*lm(a) ", a, d);
452
453        e = c.sum(br, k);
454        //System.out.println("e = " + e);
455        assertEquals("a = red(a)+lm(a) ", a, e);
456
457        e = a.subtract(br, k);
458        //System.out.println("e = " + e);
459        assertEquals("a - lm(a) = red(a) ", c, e);
460
461        b = fac.random(kl);
462        String s = b.toString(); // generate and cache some coefficients
463        //System.out.println("b = " + b);
464        assertFalse("s.size > 0 " + s, s.length() == 0); // java-5
465
466        c = a.sum(b);
467        //System.out.println("c = " + c);
468
469        d = a.sum(b.lazyCoeffs);
470        //System.out.println("d = " + d);
471
472        //         while ( !c.isZERO() ) {
473        //             c = c.reductum();
474        //             //System.out.println("c = " + c);
475        //      }
476        //         assertTrue("red^n(a) == 0 ", c.isZERO());
477
478        br = new BigRational(2, 3);
479        c = a.prepend(br, 0);
480        d = c.reductum(0);
481        assertEquals("red(a + br_0,0) = a ", d, a);
482
483        c = a.shift(3, 0);
484        d = c.shift(-3, 0);
485        assertEquals("shift(shift(a,3,),-3,0) = a ", d, a);
486    }
487
488
489    /**
490     * Test polynomial constructions.
491     * 
492     */
493    public void testPolynomial() {
494        GenPolynomialRing<BigRational> pr = fac.polyRing();
495        //System.out.println("pr = " + pr);
496
497        GenPolynomial<BigRational> p = pr.random(kl, 3, 3, q + q);
498        //System.out.println("p = " + p);
499
500        a = fac.fromPolynomial(p);
501        //System.out.println("a = " + a);
502
503        GenPolynomial<BigRational> s = a.asPolynomial();
504        //System.out.println("s = " + s);
505        assertEquals("asPolynomial(fromPolynomial(p)) = p ", p, s);
506        //         if ( a.isUnit() ) {
507        //             b = a.inverse();
508        //             System.out.println("b = " + b);
509        //      }
510    }
511
512
513    /**
514     * Test gcd.
515     * 
516     */
517    public void testGcd() {
518        a = fac.random(kl);
519        //System.out.println("a = " + a);
520
521        b = fac.random(kl);
522        //System.out.println("b = " + b);
523
524        c = a.gcd(b);
525        //System.out.println("c = " + c);
526
527        d = a.divide(c);
528        //System.out.println("d = " + d);
529
530        e = b.divide(c);
531        //System.out.println("e = " + e);
532
533        f = d.gcd(e);
534        //System.out.println("f = " + f);
535        assertTrue("gcd(a/gcd(a,b),b/gcd(a,b)) == 1 ", f.isONE());
536    }
537
538
539    /**
540     * Test Taylor series.
541     * 
542     */
543    public void testTaylor() {
544        BigRational ar = new BigRational(5);
545        BigRational br = new BigRational(0);
546        BigRational cr = new BigRational(-5);
547        List<BigRational> Ar = new ArrayList<BigRational>(rl);
548        List<BigRational> Br = new ArrayList<BigRational>(rl);
549        List<BigRational> Cr = new ArrayList<BigRational>(rl);
550        for (int i = 0; i < rl; i++) {
551            Ar.add(ar);
552            Br.add(br);
553            Cr.add(cr);
554        }
555        GenPolynomialRing<BigRational> pr = fac.polyRing();
556        //System.out.println("pr  = " + pr.toScript());
557
558        GenPolynomial<BigRational> p = pr.random(kl, 3, 3, q + q);
559        //System.out.println("p   = " + p);
560        int tdeg = (int) p.degree();
561        fac.setTruncate(tdeg + 1);
562
563        TaylorFunction<BigRational> F = new PolynomialTaylorFunction<BigRational>(p);
564
565        MultiVarPowerSeries<BigRational> pps = fac.fromPolynomial(p);
566        //System.out.println("pps = " + pps);
567        MultiVarPowerSeries<BigRational> ps = fac.seriesOfTaylor(F, Br);
568        //System.out.println("ps  = " + ps);
569        assertEquals("taylor(p) == p", ps, pps);
570
571        MultiVarPowerSeries<BigRational> psa = fac.seriesOfTaylor(F, Ar);
572        //System.out.println("psa  = " + psa);
573        F = new PolynomialTaylorFunction<BigRational>(psa.asPolynomial());
574        MultiVarPowerSeries<BigRational> psc = fac.seriesOfTaylor(F, Cr);
575        //System.out.println("psc  = " + psc);
576        assertEquals("taylor(taylor(p,5),-5) == p", ps, psc);
577
578        for (GenPolynomial<BigRational> g : pr.generators()) {
579            F = new PolynomialTaylorFunction<BigRational>(g);
580            ps = fac.seriesOfTaylor(F, Br);
581            //System.out.println("g   = " + g);
582            //System.out.println("ps  = " + ps);
583            pps = fac.fromPolynomial(g);
584            //System.out.println("pps = " + pps);
585            assertEquals("taylor(p) == p", ps, pps);
586
587            psa = fac.seriesOfTaylor(F, Ar);
588            //System.out.println("psa  = " + psa);
589            F = new PolynomialTaylorFunction<BigRational>(psa.asPolynomial());
590            psc = fac.seriesOfTaylor(F, Cr);
591            //System.out.println("psc  = " + psc);
592            assertEquals("taylor(taylor(p,5),-5) == p", ps, psc);
593        }
594    }
595
596
597    /**
598     * Test evaluation.
599     * 
600     */
601    public void testEvaluation() {
602        a = fac.random(kl, q);
603        b = fac.random(kl, q);
604        BigRational fv = new BigRational(0);
605        List<BigRational> v = new ArrayList<BigRational>(rl);
606        for ( int i = 0; i < rl; i++ ) {
607            v.add( fv.random(kl) );
608        }
609
610        BigRational av = a.evaluate(v);
611        BigRational bv = b.evaluate(v);
612
613        c = a.sum(b);
614        BigRational cv = c.evaluate(v);
615        BigRational dv = av.sum(bv);
616
617        assertEquals("a(v)+b(v) = (a+b)(v) ", cv, dv);
618
619        c = fac.getZERO();
620        cv = c.evaluate(v);
621        dv = fv.getZERO();
622        assertEquals("0(v) = 0 ", cv, dv);
623
624        c = fac.getONE();
625        cv = c.evaluate(v);
626        dv = fv.getONE();
627        assertEquals("1(v) = 1 ", cv, dv);
628    }
629}
630
631
632class Zeros extends MultiVarCoefficients<BigRational> {
633
634    public Zeros(MultiVarPowerSeriesRing<BigRational> pf) {
635        super(pf);
636    }
637    public Zeros(GenPolynomialRing<BigRational> pf) {
638        super(pf);
639    }
640
641    @Override
642    public BigRational generate(ExpVector i) {
643        return pfac.coFac.getZERO();
644    }
645
646}
647
648
649class Ones extends MultiVarCoefficients<BigRational> {
650
651    public Ones(MultiVarPowerSeriesRing<BigRational> pf) {
652        super(pf);
653    }
654    public Ones(GenPolynomialRing<BigRational> pf) {
655        super(pf);
656    }
657
658    @Override
659    public BigRational generate(ExpVector i) {
660        return pfac.coFac.getONE();
661    }
662
663}
664
665
666class Vars extends MultiVarCoefficients<BigRational> {
667
668    public Vars(MultiVarPowerSeriesRing<BigRational> pf) {
669        super(pf);
670    }
671    public Vars(GenPolynomialRing<BigRational> pf) {
672        super(pf);
673    }
674
675    @Override
676    public BigRational generate(ExpVector i) {
677        int[] v = i.dependencyOnVariables();
678        if (v.length == 1 && i.getVal(v[0]) == 1L) {
679            return pfac.coFac.getONE();
680        }
681        return pfac.coFac.getZERO();
682    }
683
684}