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