001/*
002 * $Id$
003 */
004
005package edu.jas.ps;
006
007
008import java.util.HashSet;
009import java.util.List;
010import java.util.Map;
011import java.util.Set;
012
013import edu.jas.arith.BigRational;
014import edu.jas.poly.ExpVector;
015import edu.jas.poly.GenPolynomial;
016import edu.jas.poly.GenPolynomialRing;
017
018import junit.framework.Test;
019import junit.framework.TestCase;
020import junit.framework.TestSuite;
021
022
023/**
024 * Multivariate power series tests with JUnit.
025 * @author Heinz Kredel
026 */
027
028public class MultiVarPowerSeriesTest extends TestCase {
029
030
031    /**
032     * main.
033     */
034    public static void main(String[] args) {
035        junit.textui.TestRunner.run(suite());
036    }
037
038
039    /**
040     * Constructs a <CODE>MultiVarPowerSeriesTest</CODE> object.
041     * @param name String.
042     */
043    public MultiVarPowerSeriesTest(String name) {
044        super(name);
045    }
046
047
048    /**
049     */
050    public static Test suite() {
051        TestSuite suite = new TestSuite(MultiVarPowerSeriesTest.class);
052        return suite;
053    }
054
055
056    BigRational cfac;
057
058
059    MultiVarPowerSeriesRing<BigRational> fac;
060
061
062    MultiVarPowerSeries<BigRational> a, b, c, d, e, f;
063
064
065    int rl = 2;
066
067
068    int kl = 10;
069
070
071    float q = 0.3f;
072
073
074    @Override
075    protected void setUp() {
076        a = b = c = d = e = null;
077        String[] vars = new String[] { "x", "y" };
078        cfac = new BigRational(1);
079        fac = new MultiVarPowerSeriesRing<BigRational>(cfac, rl, vars);
080        //System.out.println("fac = " + fac);
081        //System.out.println("fac = " + fac.toScript());
082    }
083
084
085    @Override
086    protected void tearDown() {
087        a = b = c = d = e = null;
088        fac = null;
089    }
090
091
092    /**
093     * Test generate.
094     */
095    public void testGenerate() {
096        String s = fac.toScript();
097        //System.out.println("fac.toScript: " + s + ", " + s.length());
098        assertTrue("#s == 17: " + s, s.length() == 17);
099
100        List<MultiVarPowerSeries<BigRational>> gens = fac.generators();
101        assertFalse("#gens != () ", gens.isEmpty());
102        //System.out.println("generators: " + gens);
103
104        // test equals
105        Set<MultiVarPowerSeries<BigRational>> set = new HashSet<MultiVarPowerSeries<BigRational>>(gens);
106        //System.out.println("gen set: " + set);
107        assertEquals("#gens == #set: ", gens.size(), set.size());
108
109        // test for elements 0, 1
110        a = fac.getZERO();
111        b = fac.getONE();
112        assertFalse("0 not in #set: ", set.contains(a));
113        assertTrue("1 in #set: ", set.contains(b));
114
115        // specific tests
116        assertEquals("#gens == rl+1 ", rl + 1, gens.size());
117        Set<Integer> iset = new HashSet<Integer>(set.size());
118        for (MultiVarPowerSeries<BigRational> p : gens) {
119            //System.out.println("p = " + p.toScript() + ", # = " + p.hashCode() + ", red = " + p.reductum());
120            assertTrue("red(p) == 0 ", p.reductum().isZERO());
121            iset.add(p.hashCode());
122        }
123        assertEquals("#gens == #iset: ", gens.size(), iset.size());
124    }
125
126
127    /**
128     * Test MultiVarCoefficients.
129     */
130    public void testCoefficients() {
131        BigRational cf = new BigRational(0);
132        GenPolynomialRing<BigRational> pring = new GenPolynomialRing<BigRational>(cf, rl);
133
134        MultiVarCoefficients<BigRational> zeros = new Zeros(pring);
135        MultiVarCoefficients<BigRational> ones = new Ones(pring);
136        MultiVarCoefficients<BigRational> vars = new Vars(pring);
137
138        int m = 5;
139        ExpVectorIterable eitbl = new ExpVectorIterable(rl, true, m);
140        //System.out.println("eitbl 0 = " + eitbl.iterator().hasNext());
141        for (ExpVector e : eitbl) {
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        //System.out.println("eitbl 1 = " + eitbl.iterator().hasNext());
151        for (ExpVector e : eitbl) {
152            BigRational c = ones.get(e);
153            //System.out.println("c = " + c + ", e = " + e);
154            assertTrue("isONE( c )", c.isONE());
155        }
156        //System.out.println("coeffCache = " + ones.coeffCache);
157        //System.out.println("zeroCache  = " + ones.zeroCache);
158        assertTrue("zeroCache is empty", ones.zeroCache.isEmpty());
159
160        for (int i = 0; i <= m; i++) {
161            GenPolynomial<BigRational> c = ones.getHomPart(i);
162            //System.out.println("c = " + c + ", i = " + i);
163            GenPolynomial<BigRational> d = ones.getHomPart(i);
164            //System.out.println("d = " + d + ", i = " + i);
165            assertTrue("c.equals(d) ", c.equals(d));
166        }
167        //System.out.println("coeffCache = " + ones.coeffCache);
168        //System.out.println("zeroCache  = " + ones.zeroCache);
169        //System.out.println("homCheck   = " + ones.homCheck);
170        //System.out.println("homCheck   = " + ones.homCheck.length());
171        assertTrue("zeroCache is empty", ones.zeroCache.isEmpty());
172        assertTrue("#coeffCache = " + m, ones.coeffCache.size() == (m + 1));
173        assertTrue("#homCheck = " + m, ones.homCheck.length() == (m + 1));
174
175        for (int i = 0; i <= m; i++) {
176            GenPolynomial<BigRational> c = vars.getHomPart(i);
177            //System.out.println("c = " + c + ", i = " + i);
178            assertTrue("c==0 || deg(c)==1 ", c.isZERO() || c.degree() == 1L);
179        }
180        //System.out.println("coeffCache = " + vars.coeffCache);
181        //System.out.println("zeroCache  = " + vars.zeroCache);
182        //System.out.println("homCheck   = " + vars.homCheck);
183        //System.out.println("homCheck   = " + vars.homCheck.length());
184        //no-more: assertTrue("zeroCache is not empty", !vars.zeroCache.isEmpty());
185        assertTrue("#coeffCache = " + m, vars.coeffCache.size() == (m + 1));
186        assertTrue("#homCheck = " + m, vars.homCheck.length() == (m + 1));
187    }
188
189
190    /**
191     * Test constructor and generators.
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.copy();
216        assertEquals("copy(c) == c.clone() ", a, b);
217        assertTrue("copy(c) == c.clone() ", a.equals(b));
218
219        a = fac.fromInteger(1);
220        assertEquals("1 == fromInteger(1) ", a, c);
221
222        b = fac.fromInteger(java.math.BigInteger.ONE);
223        assertEquals("1 == fromInteger(1) ", b, c);
224
225        e = fac.generate((i) -> i.isZERO() ? cfac.getONE() : cfac.getZERO());
226        //System.out.println("e = " + e);
227        assertTrue("isZERO( e )", !e.isZERO());
228        assertTrue("isONE( e )", e.isONE());
229    }
230
231
232    /**
233     * Test random power series.
234     */
235    public void testRandom() {
236        for (int i = 0; i < 5; i++) {
237            a = fac.random(i + 2);
238            if (a.isZERO() || a.isONE()) {
239                continue;
240            }
241            //System.out.println("a = " + a);
242            assertTrue(" not isZERO( a" + i + " )", !a.isZERO());
243            assertTrue(" not isONE( a" + i + " )", !a.isONE());
244        }
245    }
246
247
248    /**
249     * Test MultiVarCoefficients in power series.
250     */
251    public void testCoefficientsInPS() {
252        MultiVarCoefficients<BigRational> zeros = new Zeros(fac);
253        MultiVarCoefficients<BigRational> ones = new Ones(fac);
254        MultiVarCoefficients<BigRational> vars = new Vars(fac);
255
256        a = new MultiVarPowerSeries<BigRational>(fac, zeros);
257        b = new MultiVarPowerSeries<BigRational>(fac, ones);
258        c = new MultiVarPowerSeries<BigRational>(fac, vars);
259
260        int m = 5;
261        ExpVectorIterable eitbl = new ExpVectorIterable(rl, true, m);
262        for (ExpVector e : eitbl) {
263            BigRational r = a.coefficient(e);
264            //System.out.println("r = " + r + ", e = " + e);
265            assertTrue("isZERO( r )", r.isZERO());
266        }
267        //System.out.println("#a = " + a.lazyCoeffs.coeffCache);
268        assertTrue("coeffCache is one element", a.lazyCoeffs.coeffCache.size() == (m + 1));
269        assertTrue("isZERO( a )", a.isZERO()); // after previous
270
271        for (ExpVector e : eitbl) {
272            BigRational r = b.coefficient(e);
273            //System.out.println("r = " + r + ", e = " + e);
274            assertTrue("isONE( r )", r.isONE());
275        }
276        assertTrue("zeroCache is empty", b.lazyCoeffs.zeroCache.isEmpty());
277
278        for (int i = 0; i <= m; i++) {
279            GenPolynomial<BigRational> p = b.homogeneousPart(i);
280            //System.out.println("p = " + p + ", i = " + i);
281            GenPolynomial<BigRational> q = b.homogeneousPart(i);
282            //System.out.println("q = " + q + ", i = " + i);
283            assertTrue("p.equals(q) ", p.equals(q));
284        }
285        assertTrue("zeroCache is empty", b.lazyCoeffs.zeroCache.isEmpty());
286        assertTrue("#coeffCache = " + m, b.lazyCoeffs.coeffCache.size() == (m + 1));
287        assertTrue("#homCheck = " + m, b.lazyCoeffs.homCheck.length() == (m + 1));
288
289        for (int i = 0; i <= m; i++) {
290            GenPolynomial<BigRational> p = c.homogeneousPart(i);
291            //System.out.println("p = " + p + ", i = " + i);
292            assertTrue("p==0 || deg(p)==1 ", p.isZERO() || p.degree() == 1L);
293        }
294        //no-more:assertTrue("zeroCache is not empty", !c.lazyCoeffs.zeroCache.isEmpty());
295        assertTrue("#coeffCache = " + m, c.lazyCoeffs.coeffCache.size() == (m + 1));
296        assertTrue("#homCheck = " + m, c.lazyCoeffs.homCheck.length() == (m + 1));
297    }
298
299
300    /**
301     * Test addition.
302     */
303    public void testAddition() {
304        a = fac.random(kl);
305        b = fac.random(kl);
306
307        c = a.sum(b);
308        d = b.sum(a);
309        assertEquals("a+b = b+a", c, d);
310
311        d = c.subtract(b);
312        assertEquals("a+b-b = a", a, d);
313        d = c.sum(b.negate());
314        assertEquals("a+b-b = a", a, d);
315
316        c = fac.random(kl);
317        d = a.sum(b.sum(c));
318        e = a.sum(b).sum(c);
319        assertEquals("a+(b+c) = (a+b)+c", d, e);
320
321        Map.Entry<ExpVector, BigRational> ma = a.orderMonomial();
322        c = a.reductum().sum(ma);
323        assertEquals("a = red(a)+om(a)", a, c);
324    }
325
326
327    /**
328     * Test multiplication.
329     */
330    public void testMultiplication() {
331        a = fac.random(kl);
332        b = fac.random(kl);
333
334        if (a.isZERO() || b.isZERO()) {
335            return;
336        }
337        assertTrue("not isZERO( a )", !a.isZERO());
338        assertTrue("not isZERO( b )", !b.isZERO());
339
340        c = b.multiply(a);
341        d = a.multiply(b);
342        assertTrue("not isZERO( c )", !c.isZERO());
343        assertTrue("not isZERO( d )", !d.isZERO());
344
345        //System.out.println("a = " + a);
346        //System.out.println("b = " + b);
347        e = d.subtract(c);
348        assertTrue("isZERO( a*b-b*a ) " + e, e.isZERO());
349
350        assertTrue("a*b = b*a", c.equals(d));
351        assertEquals("a*b = b*a", c, d);
352
353        c = fac.random(kl);
354        //System.out.println("c = " + c);
355        d = a.multiply(b.multiply(c));
356        e = (a.multiply(b)).multiply(c);
357        //System.out.println("d = " + d);
358        //System.out.println("e = " + e);
359        //System.out.println("d-e = " + d.subtract(c) );
360
361        assertEquals("a(bc) = (ab)c", d, e);
362        assertTrue("a(bc) = (ab)c", d.equals(e));
363
364        ExpVector ev = ExpVector.random(rl, 5, 0.8f);
365        BigRational br = fac.coFac.random(5);
366
367        b = a.shift(ev).multiply(br);
368        c = a.multiply(br, ev);
369        assertEquals("(a ev) br = a (ev,br)", b, c);
370        //System.out.println("a  = " + a);
371        //System.out.println("ev = " + ev);
372        //System.out.println("br = " + br);
373        //System.out.println("b  = " + b);
374        //System.out.println("c  = " + c);
375    }
376
377
378    /**
379     * Test distributive law.
380     */
381    public void testDistributive() {
382        a = fac.random(kl, q);
383        b = fac.random(kl, q);
384        c = fac.random(kl, q);
385
386        d = a.multiply(b.sum(c));
387        e = a.multiply(b).sum(a.multiply(c));
388
389        assertEquals("a(b+c) = ab+ac", d, e);
390    }
391
392
393    /**
394     * Test inverse.
395     */
396    public void testInverse() {
397        a = fac.getONE();
398        assertTrue("not isZERO( a )", !a.isZERO());
399        assertTrue("isUnit( a )", a.isUnit());
400        //System.out.println("a = " + a);
401
402        b = a.inverse();
403        c = a.multiply(b);
404        assertTrue("isONE( c )", c.isONE());
405        //System.out.println("b = " + b);
406        //System.out.println("c = " + c);
407
408        a = fac.random(kl);
409        if (!a.isUnit()) {
410            a = fac.fromInteger(23); //return;
411        }
412        //System.out.println("a = " + a);
413        b = a.inverse();
414        c = a.multiply(b);
415        assertTrue("isONE( c )", c.isONE());
416        //System.out.println("b = " + b);
417        //System.out.println("c = " + c);
418
419        b = fac.random(kl);
420        c = b.divide(a);
421        d = c.multiply(a);
422        assertEquals("b/a * a == b ", d, b);
423    }
424
425
426    /**
427     * Test reductum.
428     */
429    public void testReductum() {
430        a = fac.random(kl);
431        //System.out.println("a = " + a);
432
433        Map.Entry<ExpVector, BigRational> m = a.orderMonomial();
434        //System.out.println("m = " + m);
435        ExpVector k = m.getKey();
436        BigRational br = m.getValue();
437
438        b = fac.getONE().multiply(br, k);
439        //System.out.println("b = " + b);
440
441        c = a.reductum();
442        //System.out.println("c = " + c);
443
444        d = c.sum(b);
445        //System.out.println("d = " + d);
446        assertEquals("a = red(a)+1*lm(a) ", a, d);
447
448        e = c.sum(br, k);
449        //System.out.println("e = " + e);
450        assertEquals("a = red(a)+lm(a) ", a, e);
451
452        e = a.subtract(br, k);
453        //System.out.println("e = " + e);
454        assertEquals("a - lm(a) = red(a) ", c, e);
455
456        b = fac.random(kl);
457        String s = b.toString(); // generate and cache some coefficients
458        //System.out.println("b = " + b);
459        assertFalse("s.size > 0 " + s, s.length() == 0); // java-5
460
461        c = a.sum(b);
462        //System.out.println("c = " + c);
463
464        d = a.sum(b.lazyCoeffs);
465        //System.out.println("d = " + d);
466
467        //         while ( !c.isZERO() ) {
468        //             c = c.reductum();
469        //             //System.out.println("c = " + c);
470        //      }
471        //         assertTrue("red^n(a) == 0 ", c.isZERO());
472
473        br = new BigRational(2, 3);
474        c = a.prepend(br, 0);
475        d = c.reductum(0);
476        assertEquals("red(a + br_0,0) = a ", d, a);
477
478        c = a.shift(3, 0);
479        d = c.shift(-3, 0);
480        assertEquals("shift(shift(a,3,),-3,0) = a ", d, a);
481    }
482
483
484    /**
485     * Test polynomial constructions.
486     */
487    public void testPolynomial() {
488        GenPolynomialRing<BigRational> pr = fac.polyRing();
489        //System.out.println("pr = " + pr);
490
491        GenPolynomial<BigRational> p = pr.random(kl, 3, 3, q + q);
492        //System.out.println("p = " + p);
493
494        a = fac.fromPolynomial(p);
495        //System.out.println("a = " + a);
496
497        GenPolynomial<BigRational> s = a.asPolynomial();
498        //System.out.println("s = " + s);
499        assertEquals("asPolynomial(fromPolynomial(p)) = p ", p, s);
500
501        b = fac.fromPolynomial(s);
502        //System.out.println("b = " + b);
503        assertEquals("fromPolynomial(asPolynomial(s)) = s ", a, b);
504    }
505
506}
507
508
509class Zeros extends MultiVarCoefficients<BigRational> {
510
511
512    public Zeros(MultiVarPowerSeriesRing<BigRational> pf) {
513        super(pf);
514    }
515
516
517    public Zeros(GenPolynomialRing<BigRational> pf) {
518        super(pf);
519    }
520
521
522    @Override
523    public BigRational generate(ExpVector i) {
524        return pfac.coFac.getZERO();
525    }
526
527}
528
529
530class Ones extends MultiVarCoefficients<BigRational> {
531
532
533    public Ones(MultiVarPowerSeriesRing<BigRational> pf) {
534        super(pf);
535    }
536
537
538    public Ones(GenPolynomialRing<BigRational> pf) {
539        super(pf);
540    }
541
542
543    @Override
544    public BigRational generate(ExpVector i) {
545        return pfac.coFac.getONE();
546    }
547
548}
549
550
551class Vars extends MultiVarCoefficients<BigRational> {
552
553
554    public Vars(MultiVarPowerSeriesRing<BigRational> pf) {
555        super(pf);
556    }
557
558
559    public Vars(GenPolynomialRing<BigRational> pf) {
560        super(pf);
561    }
562
563
564    @Override
565    public BigRational generate(ExpVector i) {
566        int[] v = i.dependencyOnVariables();
567        if (v.length == 1 && i.getVal(v[0]) == 1L) {
568            return pfac.coFac.getONE();
569        }
570        return pfac.coFac.getZERO();
571    }
572
573}