001/*
002 * $Id$
003 */
004
005package edu.jas.poly;
006
007
008import java.io.IOException;
009import java.io.StringReader;
010import java.util.ArrayList;
011import java.util.Arrays;
012import java.util.List;
013import java.util.TreeMap;
014
015import edu.jas.arith.BigInteger;
016import edu.jas.arith.BigRational;
017import edu.jas.structure.RingElem;
018import edu.jas.vector.GenMatrix;
019import edu.jas.vector.GenMatrixRing;
020
021import junit.framework.Test;
022import junit.framework.TestCase;
023import junit.framework.TestSuite;
024
025
026/**
027 * GenExteriorPolynomial tests with JUnit.
028 * @author Heinz Kredel
029 */
030
031public class GenExteriorPolynomialTest extends TestCase {
032
033
034    /**
035     * main
036     */
037    public static void main(String[] args) {
038        junit.textui.TestRunner.run(suite());
039    }
040
041
042    /**
043     * Constructs a <CODE>GenExteriorPolynomialTest</CODE> object.
044     * @param name String.
045     */
046    public GenExteriorPolynomialTest(String name) {
047        super(name);
048    }
049
050
051    /**
052     * suite.
053     */
054    public static Test suite() {
055        TestSuite suite = new TestSuite(GenExteriorPolynomialTest.class);
056        return suite;
057    }
058
059
060    int rl = 6;
061
062
063    int kl = 8;
064
065
066    int ll = 6;
067
068
069    int el = 4;
070
071
072    float ql = 0.4f;
073
074
075    @Override
076    protected void setUp() {
077    }
078
079
080    @Override
081    protected void tearDown() {
082    }
083
084
085    /**
086     * Test constructors and factory.
087     */
088    public void testConstructors() {
089        // integers
090        BigInteger rf = new BigInteger();
091        //System.out.println("rf = " + rf);
092
093        // 6 non-commuting vars
094        IndexFactory wf = new IndexFactory(6);
095        //System.out.println("wf = " + wf);
096
097        // polynomials over integers
098        GenExteriorPolynomialRing<BigInteger> pf = new GenExteriorPolynomialRing<BigInteger>(rf, wf);
099        //System.out.println("pf = " + pf);
100        assertFalse("not commutative", pf.isCommutative());
101        assertTrue("associative", pf.isAssociative());
102        assertFalse("not field", pf.isField());
103        assertEquals("pf == pf: ", pf, pf);
104
105        String s = pf.toScript();
106        //System.out.println("pf.toScript: " + s + ", " + s.length());
107        assertTrue("#s >= 35: " + s, s.length() >= 35);
108
109        s = pf.toString();
110        //System.out.println("pf.toString: " + s + ", " + s.length());
111        assertTrue("#s >= 40: " + s, s.length() >= 40);
112
113        GenExteriorPolynomial<BigInteger> p = pf.getONE();
114        //System.out.println("p = " + p);
115        assertTrue("p == 1", p.isONE());
116        p = pf.getZERO();
117        assertTrue("p == 0", p.isZERO());
118        //System.out.println("p = " + p);
119        //p = pf.random(kl, ll, el);
120        //System.out.println("p = " + p);
121
122        List<GenExteriorPolynomial<BigInteger>> gens = pf.generators();
123        //System.out.println("gens = " + gens);
124        assertTrue("#gens == 7", gens.size() == 7);
125
126        gens = pf.getGenerators();
127        //System.out.println("gens = " + gens);
128        assertTrue("#gens == 7", gens.size() == 7);
129
130        RingElem<GenExteriorPolynomial<BigInteger>> pe = new GenExteriorPolynomial<BigInteger>(pf);
131        //System.out.println("pe = " + pe);
132        //System.out.println("p.equals(pe) = " + p.equals(pe) );
133        //System.out.println("p.equals(p) = " + p.equals(p) );
134        assertTrue("p.equals(pe) = ", p.equals(pe));
135        assertTrue("p.equals(p) = ", p.equals(p));
136
137        pe = pe.sum(p);
138        //System.out.println("pe = " + pe);
139        assertTrue("pe.isZERO() = ", pe.isZERO());
140        //p = pf.random(9);
141        p = pf.random(kl, ll, el);
142        p = p.subtract(p);
143        //System.out.println("p = " + p);
144        //System.out.println("p.isZERO() = " + p.isZERO());
145        assertTrue("p.isZERO() = ", p.isZERO());
146
147        // polynomials over (polynomials over integers)
148        // 3 non-commuting vars
149        IndexFactory wf2 = new IndexFactory(3);
150        //System.out.println("wf2 = " + wf2);
151
152        GenExteriorPolynomialRing<GenExteriorPolynomial<BigInteger>> ppf;
153        ppf = new GenExteriorPolynomialRing<GenExteriorPolynomial<BigInteger>>(pf, wf2);
154        //System.out.println("ppf = " + ppf);
155
156        GenExteriorPolynomial<GenExteriorPolynomial<BigInteger>> pp = ppf.getONE();
157        //System.out.println("pp = " + pp);
158        assertTrue("pp == 1", pp.isONE());
159        //pp = ppf.random(2);
160        pp = ppf.random(kl, ll, el);
161        //System.out.println("pp = " + pp);
162        pp = ppf.getZERO();
163        //System.out.println("pp = " + pp);
164        assertTrue("pp == 0", pp.isZERO());
165
166        List<GenExteriorPolynomial<GenExteriorPolynomial<BigInteger>>> pgens = ppf.generators();
167        //System.out.println("pgens = " + pgens);
168        assertTrue("#pgens == 7+3", pgens.size() == 10);
169
170        RingElem<GenExteriorPolynomial<GenExteriorPolynomial<BigInteger>>> ppe;
171        ppe = new GenExteriorPolynomial<GenExteriorPolynomial<BigInteger>>(ppf);
172        //System.out.println("ppe = " + ppe);
173        //System.out.println("pp.equals(ppe) = " + pp.equals(ppe) );
174        //System.out.println("pp.equals(pp) = " + pp.equals(pp) );
175        assertTrue("pp.equals(ppe) = ", pp.equals(ppe));
176        assertTrue("pp.equals(pp) = ", pp.equals(pp));
177
178        ppe = ppe.sum(pp); // why not pp = pp.sum(ppe) ?
179        //System.out.println("ppe = " + ppe);
180        assertTrue("ppe.isZERO() = ", ppe.isZERO());
181        //pp = ppf.random(2);
182        pp = ppf.random(kl, ll, el);
183        //System.out.println("pp = " + pp);
184        pp = pp.subtract(pp);
185        //System.out.println("pp.isZERO() = " + pp.isZERO());
186        assertTrue("pp.isZERO() = ", pp.isZERO());
187
188        // polynomials over (polynomials over (polynomials over integers))
189        // 3 non-commuting vars
190        IndexFactory wf3 = new IndexFactory(3);
191        //System.out.println("wf3 = " + wf3);
192        GenExteriorPolynomialRing<GenExteriorPolynomial<GenExteriorPolynomial<BigInteger>>> pppf;
193        pppf = new GenExteriorPolynomialRing<GenExteriorPolynomial<GenExteriorPolynomial<BigInteger>>>(ppf,
194                        wf3);
195        //System.out.println("pppf = " + pppf);
196
197        GenExteriorPolynomial<GenExteriorPolynomial<GenExteriorPolynomial<BigInteger>>> ppp = pppf.getONE();
198        //System.out.println("ppp = " + ppp);
199        assertTrue("ppp == 1", ppp.isONE());
200        //ppp = pppf.random(2);
201        ppp = pppf.random(kl, ll, el);
202        //System.out.println("ppp = " + ppp);
203        ppp = pppf.getZERO();
204        //System.out.println("ppp = " + ppp);
205        assertTrue("ppp == 0", ppp.isZERO());
206
207        List<GenExteriorPolynomial<GenExteriorPolynomial<GenExteriorPolynomial<BigInteger>>>> ppgens = pppf
208                        .generators();
209        //System.out.println("ppgens = " + ppgens);
210        assertTrue("#ppgens == 7+3+3", ppgens.size() == 13);
211
212        RingElem<GenExteriorPolynomial<GenExteriorPolynomial<GenExteriorPolynomial<BigInteger>>>> pppe;
213        pppe = new GenExteriorPolynomial<GenExteriorPolynomial<GenExteriorPolynomial<BigInteger>>>(pppf);
214        //System.out.println("pppe = " + pppe);
215        // System.out.println("ppp.equals(pppe) = " + ppp.equals(pppe) );
216        // System.out.println("ppp.equals(ppp) = " + ppp.equals(ppp) );
217        assertTrue("ppp.equals(pppe) = ", ppp.equals(pppe));
218        assertTrue("ppp.equals(ppp) = ", ppp.equals(ppp));
219
220        pppe = pppe.sum(ppp);
221        //System.out.println("pppe = " + pppe);
222        assertTrue("pppe.isZERO() = ", pppe.isZERO());
223        //ppp = pppf.random(2);
224        ppp = pppf.random(kl, ll, el);
225        //System.out.println("ppp = " + ppp);
226        ppp = ppp.subtract(ppp);
227        //System.out.println("ppp.isZERO() = " + ppp.isZERO());
228        assertTrue("ppp.isZERO() = ", ppp.isZERO());
229    }
230
231
232    /**
233     * Test accessors.
234     */
235    public void testAccessors() {
236        // integers
237        BigInteger rf = new BigInteger();
238        // System.out.println("rf = " + rf);
239
240        // 6 non-commuting vars
241        IndexFactory wf = new IndexFactory(6);
242        //System.out.println("wf = " + wf);
243
244        // polynomials over integers
245        GenExteriorPolynomialRing<BigInteger> pf = new GenExteriorPolynomialRing<BigInteger>(rf, wf);
246        //System.out.println("pf = " + pf);
247
248        // test 1
249        GenExteriorPolynomial<BigInteger> p = pf.getONE();
250        //System.out.println("p = " + p);
251
252        IndexList e = p.leadingIndexList();
253        BigInteger c = p.leadingBaseCoefficient();
254
255        GenExteriorPolynomial<BigInteger> f = new GenExteriorPolynomial<BigInteger>(pf, c, e);
256        assertEquals("1 == 1 ", p, f);
257
258        GenExteriorPolynomial<BigInteger> r = p.reductum();
259        assertTrue("red(1) == 0 ", r.isZERO());
260
261        // test 0
262        p = pf.getZERO();
263        // System.out.println("p = " + p);
264        e = p.leadingIndexList();
265        c = p.leadingBaseCoefficient();
266
267        f = new GenExteriorPolynomial<BigInteger>(pf, c, e);
268        assertEquals("0 == 0 ", p, f);
269
270        r = p.reductum();
271        assertTrue("red(0) == 0 ", r.isZERO());
272
273        // test random
274        p = pf.random(kl, ll, el);
275        //System.out.println("p = " + p);
276        e = p.leadingIndexList();
277        c = p.leadingBaseCoefficient();
278        r = p.reductum();
279
280        f = new GenExteriorPolynomial<BigInteger>(pf, c, e);
281        f = r.sum(f);
282        assertEquals("p == lm(f)+red(f) ", p, f);
283
284        // test iteration over random
285        GenExteriorPolynomial<BigInteger> g;
286        g = p;
287        f = pf.getZERO();
288        while (!g.isZERO()) {
289            e = g.leadingIndexList();
290            c = g.leadingBaseCoefficient();
291            //System.out.println("c e = " + c + " " + e);
292            r = g.reductum();
293            f = f.sum(c, e);
294            g = r;
295        }
296        assertEquals("p == lm(f)+lm(red(f))+... ", p, f);
297    }
298
299
300    /**
301     * Test addition.
302     */
303    public void testAddition() {
304        // integers
305        BigInteger rf = new BigInteger();
306        // System.out.println("rf = " + rf);
307
308        // 6 non-commuting vars
309        IndexFactory wf = new IndexFactory(6);
310        //System.out.println("wf = " + wf);
311
312        // polynomials over integers
313        GenExteriorPolynomialRing<BigInteger> fac = new GenExteriorPolynomialRing<BigInteger>(rf, wf);
314        //System.out.println("fac = " + fac);
315
316        GenExteriorPolynomial<BigInteger> a = fac.random(kl, ll, el);
317        GenExteriorPolynomial<BigInteger> b = fac.random(kl, ll, el);
318
319        GenExteriorPolynomial<BigInteger> c = a.sum(b);
320        GenExteriorPolynomial<BigInteger> d = c.subtract(b);
321        GenExteriorPolynomial<BigInteger> e;
322        assertEquals("a+b-b = a", a, d);
323        //System.out.println("a = " + a);
324        //System.out.println("b = " + b);
325        //System.out.println("c = " + c);
326        //System.out.println("d = " + d);
327        assertTrue("deg(a+b) >= deg(a)", c.degree() >= a.degree());
328        assertTrue("deg(a+b) >= deg(b)", c.degree() >= b.degree());
329
330        c = fac.random(kl, ll, el);
331        //System.out.println("\nc = " + c);
332        d = a.sum(b.sum(c));
333        e = (a.sum(b)).sum(c);
334
335        //System.out.println("d = " + d);
336        //System.out.println("e = " + e);
337        //System.out.println("d-e = " + d.subtract(e) );
338        assertEquals("a+(b+c) = (a+b)+c", d, e);
339
340        IndexList u = wf.random(rl);
341        BigInteger x = rf.random(kl);
342
343        b = new GenExteriorPolynomial<BigInteger>(fac, x, u);
344        c = a.sum(b);
345        d = a.sum(x, u);
346        assertEquals("a+p(x,u) = a+(x,u)", c, d);
347        //System.out.println("\nc = " + c);
348        //System.out.println("d = " + d);
349
350        c = a.subtract(b);
351        d = a.subtract(x, u);
352        assertEquals("a-p(x,u) = a-(x,u)", c, d);
353        //System.out.println("c = " + c);
354        //System.out.println("d = " + d);
355
356        //a = new GenExteriorPolynomial<BigInteger>(fac);
357        b = new GenExteriorPolynomial<BigInteger>(fac, x, u);
358        c = b.sum(a);
359        d = a.sum(x, u);
360        assertEquals("a+p(x,u) = a+(x,u)", c, d);
361        //System.out.println("a = " + a);
362        //System.out.println("b = " + b);
363        //System.out.println("c = " + c);
364        //System.out.println("d = " + d);
365
366        c = a.subtract(b);
367        d = a.subtract(x, u);
368        assertEquals("a-p(x,u) = a-(x,u)", c, d);
369        //System.out.println("c = " + c);
370        //System.out.println("d = " + d);
371    }
372
373
374    /**
375     * Test multiplication.
376     */
377    public void testMultiplication() {
378        // integers
379        BigInteger rf = new BigInteger();
380        // System.out.println("rf = " + rf);
381
382        // 6 non-commuting vars
383        IndexFactory wf = new IndexFactory(6);
384        //System.out.println("wf = " + wf);
385
386        // polynomials over integers
387        GenExteriorPolynomialRing<BigInteger> fac = new GenExteriorPolynomialRing<BigInteger>(rf, wf);
388        //System.out.println("fac = " + fac);
389
390        GenExteriorPolynomial<BigInteger> a = fac.random(kl, ll, el);
391        GenExteriorPolynomial<BigInteger> b = fac.random(kl, ll, el);
392
393        GenExteriorPolynomial<BigInteger> c = a.multiply(b);
394        GenExteriorPolynomial<BigInteger> d = b.multiply(a);
395        GenExteriorPolynomial<BigInteger> e;
396        //assertFalse("a*b != b*a: " + a + " /\\ " + b + " == " + c, c.equals(d)); // to many fails
397        //System.out.println("a = " + a);
398        //System.out.println("b = " + b);
399        //System.out.println("c = " + c);
400        //System.out.println("d = " + d);
401        assertTrue("maxNorm(a*b) >= maxNorm(a)", c.maxNorm().compareTo(a.maxNorm()) >= 0);
402        assertTrue("maxNorm(a*b) >= maxNorm(b)", c.maxNorm().compareTo(b.maxNorm()) >= 0);
403
404        c = fac.random(kl, ll, el);
405        //System.out.println("c = " + c);
406        d = a.multiply(b.multiply(c));
407        e = (a.multiply(b)).multiply(c);
408
409        //System.out.println("d = " + d);
410        //System.out.println("e = " + e);
411        //System.out.println("d-e = " + d.subtract(e) );
412        assertEquals("a*(b*c) = (a*b)*c:", d, e);
413
414        e = b.multiply(a,c);
415        //System.out.println("d = " + d);
416        //System.out.println("e = " + e);
417        assertEquals("a*(b*c) = a*b*c:", d, e);
418
419        IndexList u = wf.random(rl);
420        BigInteger x = rf.random(kl);
421
422        b = new GenExteriorPolynomial<BigInteger>(fac, x, u);
423        c = a.multiply(b);
424        d = a.multiply(x, u);
425        assertEquals("a*p(x,u) = a*(x,u)", c, d);
426        //System.out.println("c = " + c);
427        //System.out.println("d = " + d);
428
429        //a = new GenExteriorPolynomial<BigInteger>(fac);
430        b = new GenExteriorPolynomial<BigInteger>(fac, x, u);
431        c = a.multiply(b);
432        d = a.multiply(x, u);
433        assertEquals("a*p(x,u) = a*(x,u)", c, d);
434        //System.out.println("a = " + a);
435        //System.out.println("b = " + b);
436        //System.out.println("c = " + c);
437        //System.out.println("d = " + d);
438
439        BigInteger y = rf.random(kl);
440        c = a.multiply(x, y);
441        //System.out.println("c = " + c);
442        d = a.multiply(y, x);
443        //System.out.println("d = " + d);
444        assertEquals("x a y = y a x", c, d);
445
446        IndexList v = wf.random(rl);
447        c = a.multiply(x, u, y, v);
448        //System.out.println("c = " + c);
449        d = a.multiply(y, u, x, v);
450        //System.out.println("d = " + d);
451        assertEquals("x u a y v = y u a x v", c, d);
452
453        c = a.multiply(u, v);
454        //System.out.println("c = " + c);
455        d = a.multiply(rf.getONE(), u, v);
456        //System.out.println("d = " + d);
457        assertEquals("u a v = 1 u a v", c, d);
458    }
459
460
461    /**
462     * Test distributive law.
463     */
464    public void testDistributive() {
465        // integers
466        BigInteger rf = new BigInteger();
467        // System.out.println("rf = " + rf);
468
469        // 6 non-commuting vars
470        IndexFactory wf = new IndexFactory(6);
471        //System.out.println("wf = " + wf);
472
473        // polynomials over integers
474        GenExteriorPolynomialRing<BigInteger> fac = new GenExteriorPolynomialRing<BigInteger>(rf, wf);
475        //System.out.println("fac = " + fac);
476
477        GenExteriorPolynomial<BigInteger> a = fac.random(kl, ll, el);
478        GenExteriorPolynomial<BigInteger> b = fac.random(kl, ll, el);
479        GenExteriorPolynomial<BigInteger> c = fac.random(kl, ll, el);
480        GenExteriorPolynomial<BigInteger> d, e;
481
482        d = a.multiply(b.sum(c));
483        e = a.multiply(b).sum(a.multiply(c));
484
485        assertEquals("a(b+c) = ab+ac", d, e);
486    }
487
488
489    /**
490     * Test division.
491     */
492    public void testDivision() {
493        // rational
494        BigRational rf = new BigRational();
495        // System.out.println("rf = " + rf);
496
497        // 6 non-commuting vars
498        IndexFactory wf = new IndexFactory(6);
499        //System.out.println("wf = " + wf);
500
501        // polynomials over integers
502        GenExteriorPolynomialRing<BigRational> fac = new GenExteriorPolynomialRing<BigRational>(rf, wf);
503        //System.out.println("fac = " + fac);
504
505        GenExteriorPolynomial<BigRational> a = fac.random(kl + 2, ll, el);
506        GenExteriorPolynomial<BigRational> b = fac.random(kl - 1, ll, 2);
507        GenExteriorPolynomial<BigRational> c, d, e;
508
509        //System.out.println("a = " + a);
510        //System.out.println("b = " + b);
511
512        //c = fac.getZERO();
513        //d = a;
514        GenExteriorPolynomial<BigRational>[] qr = a.quotientRemainder(fac.getONE());
515        c = qr[0];
516        d = qr[1];
517        //System.out.println("c = " + c);
518        //System.out.println("d = " + d);
519        e = c.multiply(fac.getONE()).sum(d);
520        //System.out.println("e = " + e);
521        assertEquals("a = 0 a + a: ", a, e);
522
523        // //c = fac.getONE();
524        // //d = fac.getZERO();
525        // qr = a.quotientRemainder(b);
526        // c = qr[0];
527        // d = qr[1];
528        // //System.out.println("q = " + c);
529        // //System.out.println("r = " + d);
530        // e = c.multiply(b).sum(d);
531        // //System.out.println("q b = " + c.multiply(b));
532        // //System.out.println("e = " + e);
533        // assertEquals("a = q b + r: ", a, e);
534    }
535
536
537    /*
538     * Test constructors and factory.
539     */
540    @SuppressWarnings("unchecked")
541    public void testParser() {
542        BigInteger rf = new BigInteger();
543        //System.out.println("rf = " + rf.toScriptFactory());
544
545        // commuting vars: abcdef
546        String[] sa = new String[] { "a", "b", "c", "d", "e", "f" };
547        //String ss = "E(1,2,3,4,5,6)";
548        IndexFactory wf = new IndexFactory(6);
549        //System.out.println("wf = " + wf.toScript());
550
551        // index list polynomials over integers
552        GenExteriorPolynomialRing<BigInteger> pf = new GenExteriorPolynomialRing<BigInteger>(rf, wf);
553        //System.out.println("pf = " + pf.toScript());
554        assertFalse("not commutative", pf.isCommutative());
555        assertTrue("associative", pf.isAssociative());
556        assertFalse("not field", pf.isField());
557
558        List<GenExteriorPolynomial<BigInteger>> gens = pf.generators();
559        //System.out.println("gens = " + gens);
560        GenExteriorPolynomial<BigInteger> g1, g2, g3, g4, epol;
561        g1 = gens.get(1);
562        g2 = gens.get(2);
563        g3 = gens.get(3);
564        g4 = gens.get(4);
565        //System.out.println("g1 = " + g1 + ", " + g1.toScript());
566        //System.out.println("g2 = " + g2);
567        assertEquals("#s == 5: ", g1.toString().length(), 5);
568        assertEquals("#s == 4: ", g2.toScript().length(), 4);
569
570        epol = g1.multiply(g2).subtract(g3.multiply(g4));
571        //System.out.println("epol = " + epol);
572
573        //StringReader sr = new StringReader("1 E(1,2)**2 + E(1,2) - 1 E(3,4)**0");
574        StringReader sr = new StringReader("1 E(1,2) - 1 E(3,4)");
575        GenPolynomialTokenizer tok = new GenPolynomialTokenizer(sr);
576
577        GenExteriorPolynomial<BigInteger> a, b;
578        // parse of tokenizer
579        try {
580            a = (GenExteriorPolynomial) tok.nextExteriorPolynomial(pf);
581        } catch (IOException e) {
582            a = null;
583            e.printStackTrace();
584        }
585        //System.out.println("a = " + a);
586        assertEquals("parse() == ab - ba: ", a, epol);
587
588        // now parse of factory
589        a = pf.parse("1 E(1,2) - 1 E(3,4)");
590        //System.out.println("a = " + a);
591        assertEquals("parse() == 1 E(1,2) - 1 E(3,4): ", a, epol);
592
593        // commutative polynomials over integers
594        GenPolynomialRing<BigInteger> fac = new GenPolynomialRing<BigInteger>(rf, sa);
595        //System.out.println("fac = " + fac.toScript());
596        assertTrue("commutative", fac.isCommutative());
597        assertTrue("associative", fac.isAssociative());
598        assertFalse("not field", fac.isField());
599
600        sr = new StringReader("E(1,2) - E(3,4)");
601        tok = new GenPolynomialTokenizer(fac, sr);
602        // parse exterior with tokenizer
603        try {
604            a = (GenExteriorPolynomial) tok.nextExteriorPolynomial();
605        } catch (IOException e) {
606            a = null;
607            e.printStackTrace();
608        }
609        //System.out.println("a = " + a);
610        assertEquals("parse() == E(1,2) - E(3,4): ", a, epol);
611    }
612
613
614    /**
615     * Test iterators.
616     */
617    public void testIterators() {
618        // integers
619        BigInteger rf = new BigInteger();
620        //System.out.println("rf = " + rf);
621
622        // index list polynomials over integral numbers
623        GenExteriorPolynomialRing<BigInteger> pf = new GenExteriorPolynomialRing<BigInteger>(rf, "abcdef");
624        //System.out.println("pf = " + pf);
625
626        // random polynomial
627        GenExteriorPolynomial<BigInteger> p = pf.random(kl, 2 * ll, el);
628        //System.out.println("p = " + p);
629
630        // test monomials
631        for (IndexListMonomial<BigInteger> m : p) {
632            //System.out.println("m = " + m);
633            assertFalse("m.c == 0 ", m.coefficient().isZERO());
634            assertFalse("m.e == 0 ", m.indexlist().isZERO());
635        }
636    }
637
638
639    /**
640     * Test matrix and vector.
641     */
642    public void testMatrix() {
643        // rational numbers
644        BigRational rf = new BigRational();
645        // System.out.println("rf = " + rf);
646
647        // matrix size
648        int c = 13; // to big 23;
649        IndexFactory ixfac = new IndexFactory(c);
650        //System.out.println("ixfac = " + ixfac);
651
652        // exterior polynomials over rational numbers
653        GenExteriorPolynomialRing<BigRational> epf;
654        epf = new GenExteriorPolynomialRing<BigRational>(rf, ixfac);
655        //System.out.println("epf = " + epf.toScript());
656
657        // matrix over rational numbers
658        int r = c;
659        GenMatrixRing<BigRational> mf = new GenMatrixRing<BigRational>(rf, r, r);
660        //System.out.println("mf = " + mf.toScript());
661
662        GenMatrix<BigRational> A = mf.getONE();
663        //System.out.println("A = " + A);
664        List<GenExteriorPolynomial<BigRational>> em = epf.fromMatrix(A);
665        //System.out.println("em = " + em);
666        assertEquals("#em == #A: ", em.size(), A.ring.rows);
667        BigRational dr = epf.determinant(em);
668        //System.out.println("dr = " + dr);
669        assertTrue("det(em) == 1: ", dr.isONE());
670
671        A = mf.getZERO();
672        //System.out.println("A = " + A);
673        em = epf.fromMatrix(A);
674        //System.out.println("em = " + em);
675        assertEquals("#em == #A: ", em.size(), 0);
676        dr = epf.determinant(em);
677        //System.out.println("dr = " + dr);
678        assertTrue("det(em) == 0: ", dr.isZERO());
679
680        A = mf.randomUpper(3, 0.55f);
681        //System.out.println("A = " + A);
682        em = epf.fromMatrix(A);
683        //System.out.println("em = " + em);
684        assertTrue("#em <= #A: ", em.size() <= A.matrix.size());
685        dr = epf.determinant(em);
686        //System.out.println("dr = " + dr);
687        assertTrue("det(em) == 0: ", dr.isZERO());
688
689        A = mf.random(5, 0.67f);
690        //System.out.println("A = " + A);
691        em = epf.fromMatrix(A);
692        //System.out.println("em = " + em);
693        assertTrue("#em <= #A: ", em.size() <= A.matrix.size());
694        dr = epf.determinant(em);
695        //System.out.println("dr = " + dr);
696        assertFalse("det(em) != 0: ", dr.isZERO());
697
698        GenPolynomialRing<BigRational> fac;
699        fac = new GenPolynomialRing<BigRational>(rf, new String[] { "lambda" });
700        //System.out.println("fac = " + fac.toScript());
701
702        //System.out.println("A = " + A);
703        //GenPolynomial<BigRational> cp = fac.charPolynomial(A);
704        //System.out.println("cp = " + cp);
705        //BigRational er = fac.determinantFromCharPol(cp);
706
707        BigRational er = fac.determinant(A);
708        //System.out.println("\ner = " + er);
709        //System.out.println("dr = " + dr);
710        assertEquals("det_exterior(A) == det_charpol(A): ", dr, er);
711    }
712
713
714    /*
715     * Test conversions.
716     */
717    public void testConversion() {
718        BigRational rf = new BigRational();
719        //System.out.println("rf = " + rf.toScriptFactory());
720
721        IndexFactory wf = new IndexFactory(0, 20);
722        //System.out.println("wf = " + wf.toScript());
723
724        // exterior polynomials over integers
725        GenExteriorPolynomialRing<BigRational> pf;
726        pf = new GenExteriorPolynomialRing<BigRational>(rf, wf);
727        //System.out.println("pf = " + pf.toScript());
728
729        // commutative polynomials over rationals
730        GenPolynomialRing<BigRational> fac;
731        fac = new GenPolynomialRing<BigRational>(rf, new String[] { "a", "b", "c", "d" });
732        //System.out.println("fac = " + fac.toScript());
733
734        GenPolynomial<BigRational> p = fac.random(kl / 2, ll, 2, ql);
735        //System.out.println("p = " + p);
736
737        GenExteriorPolynomial<BigRational> a = pf.valueOf(p);
738        //System.out.println("a = " + a);
739        List<GenPolynomial<BigRational>> Pl = new ArrayList<GenPolynomial<BigRational>>();
740        Pl.add(p);
741        List<GenExteriorPolynomial<BigRational>> El = pf.valueOf(Pl);
742        assertEquals("a == El[0]: ", a, El.get(0));
743        assertEquals("sumNorm(p) == sumNorm(a): ", p.sumNorm(), a.sumNorm());
744
745        GenExteriorPolynomial<BigRational> b = pf.valueOf(a);
746        //System.out.println("b = " + b);
747        assertEquals("a == b: ", a, b);
748
749        GenExteriorPolynomial<BigRational> c = pf.fromInteger(7);
750        //System.out.println("c = " + c);
751        assertEquals("7 == ldcf(c): ", rf.fromInteger(7), c.leadingBaseCoefficient());
752    }
753
754
755    /*
756     * Test resultant.
757     */
758    public void testResultant() {
759        BigRational rf = new BigRational();
760        //System.out.println("rf = " + rf.toScriptFactory());
761        IndexFactory wf = new IndexFactory(0, 20);
762        //System.out.println("wf = " + wf.toScript());
763
764        // exterior polynomials over rationals
765        GenExteriorPolynomialRing<BigRational> pf;
766        pf = new GenExteriorPolynomialRing<BigRational>(rf, wf);
767        //System.out.println("pf = " + pf.toScript());
768
769        // commutative univariate polynomials over rationals
770        GenPolynomialRing<BigRational> fac;
771        fac = new GenPolynomialRing<BigRational>(rf, new String[] { "i" });
772        //System.out.println("fac = " + fac.toScript());
773
774        GenPolynomial<BigRational> p = fac.random(kl / 2, ll, el, ql);
775        //System.out.println("p = " + p);
776        GenPolynomial<BigRational> q = fac.random(kl / 2, ll, el, ql);
777        //System.out.println("q = " + q);
778        BigRational r = pf.resultant(p, q);
779        //System.out.println("r = " + r);
780
781        GenPolynomial<BigRational> g = p.gcd(q);
782        //System.out.println("g = " + g);
783        assertTrue("res != 0 && gcd == 1: " + r + ", " + g, !r.isZERO() && g.isONE());
784
785        //System.out.println("fac.gens = " + fac.generators());
786        GenPolynomial<BigRational> f = fac.random(kl / 3, ll, el / 2, ql);
787        f = f.sum(fac.generators().get(1));
788        //System.out.println("f = " + f);
789        p = p.multiply(f);
790        q = q.multiply(f);
791        //System.out.println("p = " + p);
792        //System.out.println("q = " + q);
793        r = pf.resultant(p, q);
794        //System.out.println("r = " + r);
795
796        g = p.gcd(q);
797        //System.out.println("g = " + g);
798        assertTrue("res == 0 && gcd != 1: " + r + ", " + g, r.isZERO() && !g.isONE());
799    }
800
801
802    /*
803     * Test old example after Blonski, 1983.
804     */
805    //@SuppressWarnings("unchecked")
806    public void testExample() {
807        BigInteger rf = new BigInteger();
808        //System.out.println("rf = " + rf.toScriptFactory());
809
810        // non-commuting indexes: 1 2 3 4
811        //String ss = "E(1,2,3,4)";
812        IndexFactory wf = new IndexFactory(4); // (1,4)
813        //System.out.println("wf = " + wf.toScript());
814
815        // index list polynomials over integers
816        GenExteriorPolynomialRing<BigInteger> pf;
817        pf = new GenExteriorPolynomialRing<BigInteger>(rf, wf);
818        //System.out.println("pf = " + pf.toScript());
819        assertFalse("not commutative", pf.isCommutative());
820        assertTrue("associative", pf.isAssociative());
821        assertFalse("not field", pf.isField());
822
823        GenExteriorPolynomial<BigInteger> emaxd, p1, p2, q1, q2, s, g1, g2, e1, e2, e1dual, e2dual, q, qs, qt,
824                        g1dual, g2dual, s1, s2;
825        // parse points in 4-space as polynomials
826        emaxd = pf.parse("E(1,2,3,4)"); // wf.imax 
827        //System.out.println("emaxd = " + emaxd + ", imax = " + pf.ixfac.imax);
828        p1 = pf.parse("1 E(1) + 5 E(2) - 2 E(3) + 1 E(4)");
829        p2 = pf.parse("4 E(1) + 3 E(2) + 6 E(3) + 1 E(4)");
830        //System.out.println("p1 = " + p1);
831        //System.out.println("p2 = " + p2);
832        q1 = pf.parse("3 E(1) - 2 E(2) - 1 E(3) + 1 E(4)");
833        q2 = pf.parse("1 E(2) + 5 E(3) + 1 E(4)");
834        //System.out.println("q1 = " + q1);
835        //System.out.println("q2 = " + q2);
836        s = pf.parse("1 E(3) + 1 E(4)");
837        //System.out.println("s = " + s);
838
839        // compute line(gerade) p1..p2 and q1..q2
840        g1 = p1.multiply(p2).abs();
841        g2 = q1.multiply(q2).abs().divide(new BigInteger(3));
842        //System.out.println("g1 = p1 /\\ p2 = " + g1);
843        //System.out.println("g2 = q1 /\\ q2 = " + g2);
844        //System.out.println("pp(g2) = " + q1.multiply(q2).coeffPrimitivePart());
845        assertEquals("g2 == pp(g2): ", g2, q1.multiply(q2).coeffPrimitivePart());
846
847        // compute plane(ebene) g1..s and g2..s
848        e1 = g1.multiply(s).abs().divide(new BigInteger(17));
849        e2 = g2.multiply(s);
850        //System.out.println("e1 = g1 /\\ s = " + e1);
851        //System.out.println("e2 = g2 /\\ s = " + e2);
852        assertEquals("e1 == pp(e1): ", e1, g1.multiply(s).coeffPrimitivePart());
853
854        // compute dual planes of e1, e2 as e1..emaxd and e2..emaxd
855        e1dual = e1.interiorRightProduct(emaxd).abs();
856        e2dual = e2.interiorRightProduct(emaxd).abs();
857        //System.out.println("e1dual = e1 |_ emaxd = " + e1dual);
858        //System.out.println("e2dual = e2 |_ emaxd = " + e2dual);
859
860        // compute intersection of plane e1, e2 via dual plane sum
861        q = e1dual.multiply(e2dual).abs().divide(new BigInteger(5));
862        //System.out.println("q  = (e1dual /\\ e2dual) = " + q);
863        assertEquals("q == pp(q): ", q, e1dual.multiply(e2dual).coeffPrimitivePart());
864        qs = q.interiorRightProduct(emaxd).abs();
865        //System.out.println("qs = (e1dual /\\ e2dual) |_ emaxd = " + qs);
866        qt = e1.interiorLeftProduct(e2dual).abs().divide(new BigInteger(5));
867        //System.out.println("qt = e1 _| e2dual                = " + qt);
868        assertEquals("qt == pp(qt): ", qt, e1.interiorLeftProduct(e2dual).coeffPrimitivePart());
869        assertEquals("qs == qt: ", qs, qt);
870
871        // compute dual line(gerade) of g1, g2
872        g1dual = g1.interiorRightProduct(emaxd);
873        g2dual = g2.interiorRightProduct(emaxd).abs();
874        //System.out.println("g1dual = g1 |_ emaxd = " + g1dual);
875        //System.out.println("g2dual = g2 |_ emaxd = " + g2dual);
876
877        // compute intersection of g1..e2 and g2..e1
878        s1 = e2.interiorLeftProduct(g1dual).abs().divide(new BigInteger(5));
879        //System.out.println("s1 = e2 _| g1dual = " + s1);
880        s2 = e1.interiorLeftProduct(g2dual).abs().divide(new BigInteger(5));
881        //System.out.println("s2 = e1 _| g2dual = " + s2);
882
883        // check intersection of s..qs, qs..e1 and qs..e2
884        //System.out.println(" s /\\ qs =  s \\in qs = " + s.multiply(qs));
885        //System.out.println("qs /\\ e1 = qs \\in e1 = " + qs.multiply(e1));
886        //System.out.println("qs /\\ e2 = qs \\in e2 = " + qs.multiply(e2));
887        assertTrue("qs /\\ s == 0: ", qs.multiply(s).isZERO());
888        assertTrue("qs /\\ e1 == 0: ", qs.multiply(e1).isZERO());
889        assertTrue("qs /\\ e2 == 0: ", qs.multiply(e2).isZERO());
890    }
891
892
893    /**
894     * Test exterior derivation.
895     */
896    @SuppressWarnings("unchecked")
897    public void testDerivation() {
898        // rationals
899        BigRational rf = new BigRational();
900        //System.out.println("rf = " + rf.toScriptFactory());
901
902        // 3/6 commuting vars
903        String[] vars = new String[] { "x1", "x2", "x3" };
904        //System.out.println("vars = " + Arrays.toString(vars));
905
906        // polynomials over rationals
907        GenPolynomialRing<BigRational> pf = new GenPolynomialRing<BigRational>(rf, vars);
908        //System.out.println("pf = " + pf);
909
910        String s = pf.toScript();
911        //System.out.println("pf.toScript: " + s + ", " + s.length());
912        assertEquals("#s == 38: " + s, s.length(), 38);
913
914        s = pf.toString();
915        //System.out.println("pf.toString: " + s + ", " + s.length());
916        assertEquals("#s == 31: " + s, s.length(), 31);
917
918        GenPolynomial<BigRational> p = pf.getONE();
919        //System.out.println("p = " + p);
920        assertTrue("p == 1", p.isONE());
921        p = pf.getZERO();
922        assertTrue("p == 0", p.isZERO());
923        //System.out.println("p = " + p);
924
925        List<GenPolynomial<BigRational>> gens = pf.generators();
926        //System.out.println("gens = " + gens);
927        assertTrue("#gens == 4", gens.size() == 4);
928
929        RingElem<GenPolynomial<BigRational>> pe = new GenPolynomial<BigRational>(pf);
930        //System.out.println("pe = " + pe);
931        assertTrue("p.equals(pe) = ", p.equals(pe));
932        assertTrue("p.equals(p) = ", p.equals(p));
933
934        pe = pe.sum(p);
935        //System.out.println("pe = " + pe);
936        assertTrue("pe.isZERO() = ", pe.isZERO());
937        //p = pf.random(9);
938        p = pf.random(kl, ll, el, ql);
939        p = p.subtract(p);
940        //System.out.println("p = " + p);
941        //System.out.println("p.isZERO() = " + p.isZERO());
942        assertTrue("p.isZERO() = ", p.isZERO());
943
944
945        // exterior polynomials over (polynomials over rationals)
946        // 3 non-commuting vars
947        IndexFactory wf2 = new IndexFactory(3);
948        //System.out.println("wf2 = " + wf2);
949
950        GenExteriorPolynomialRing<GenPolynomial<BigRational>> ppf;
951        ppf = new GenExteriorPolynomialRing<GenPolynomial<BigRational>>(pf, wf2);
952        //System.out.println("ppf = " + ppf.toScript());
953
954        GenExteriorPolynomial<GenPolynomial<BigRational>> pp = ppf.getONE();
955        //System.out.println("pp = " + pp);
956        assertTrue("pp == 1", pp.isONE());
957        pp = ppf.getZERO();
958        //System.out.println("pp = " + pp);
959        assertTrue("pp == 0", pp.isZERO());
960
961        List<GenExteriorPolynomial<GenPolynomial<BigRational>>> pgens = ppf.generators();
962        //System.out.println("pgens = " + pgens);
963        assertTrue("#pgens == 4+3", pgens.size() == 4 + 3);
964
965        assertFalse("commutative", ppf.isCommutative());
966        assertTrue("associative", ppf.isAssociative());
967        assertFalse("not field", ppf.isField());
968        assertEquals("ppf == ppf: ", ppf, ppf);
969
970        pp = ppf.random(kl, ll, el);
971        //System.out.println("pp = " + pp);
972        long deg = pp.degree();
973        GenExteriorPolynomial<GenPolynomial<BigRational>> der;
974        der = PolyUtil.<BigRational> exteriorDerivativePoly(pp);
975        //System.out.println("der = " + der);
976        assertTrue("deg >= 0: ", deg >= 0 && der.degree() >= 0);
977        deg = der.degree();
978        der = PolyUtil.<BigRational> exteriorDerivativePoly(der);
979        //System.out.println("der(der) = " + der);
980        assertTrue("deg >= 0: ", deg >= 0 && der.degree() >= 0);
981
982
983        //StringReader sr = new StringReader("x1 x2 x3 E(1)");
984        StringReader sr = new StringReader("{x1 + x2 + x3} E(1) E(2)");
985        //StringReader sr = new StringReader("{x1 + x2 + x3**3} E(1)"); // E(2)
986        //System.out.println("sr = " + sr);
987        GenPolynomialTokenizer tok = new GenPolynomialTokenizer(sr);
988        //System.out.println("ppf = " + ppf.toScript());
989
990        // parse with tokenizer
991        try {
992            pp = (GenExteriorPolynomial<GenPolynomial<BigRational>>) tok.nextExteriorPolynomial(ppf);
993        } catch (IOException e) {
994            e.printStackTrace();
995            return;
996        }
997        //System.out.println("pp = " + pp);
998        deg = pp.degree();
999        der = PolyUtil.<BigRational> exteriorDerivativePoly(pp);
1000        //System.out.println("der = " + der);
1001        assertTrue("deg >= 0: ", deg >= 0 && der.degree() >= 0);
1002        deg = der.degree();
1003        der = PolyUtil.<BigRational> exteriorDerivativePoly(der);
1004        //System.out.println("der(der) = " + der);
1005        assertTrue("deg >= 0: ", deg >= 0 && der.degree() >= 0);
1006        assertTrue("der(der) == 0: ", der.isZERO());
1007
1008
1009        sr = new StringReader("x3 E(1) + x3 E(2)");
1010        //sr = new StringReader("x1 E(1) + x2 E(2)");
1011        //sr = new StringReader("x2 E(1) + x1 E(2)");
1012        //System.out.println("sr = " + sr);
1013        tok = new GenPolynomialTokenizer(sr);
1014        //System.out.println("ppf = " + ppf.toScript());
1015
1016        // parse with tokenizer
1017        try {
1018            pp = (GenExteriorPolynomial<GenPolynomial<BigRational>>) tok.nextExteriorPolynomial(ppf);
1019        } catch (IOException e) {
1020            e.printStackTrace();
1021            return;
1022        }
1023        //System.out.println("pp = " + pp);
1024        deg = pp.degree();
1025        der = PolyUtil.<BigRational> exteriorDerivativePoly(pp);
1026        //System.out.println("der = " + der);
1027        assertTrue("deg >= 0: ", deg >= 0 && der.degree() >= 0);
1028        deg = der.degree();
1029        der = PolyUtil.<BigRational> exteriorDerivativePoly(der);
1030        //System.out.println("der(der) = " + der);
1031        assertTrue("deg >= 0: ", deg >= 0 && der.degree() >= 0);
1032        assertTrue("der(der) == 0: ", der.isZERO());
1033    }
1034
1035
1036    /**
1037     * Test k-forms.
1038     */
1039    public void testForms() {
1040        // integers
1041        BigInteger rf = new BigInteger();
1042        //System.out.println("rf = " + rf);
1043
1044        // 6 non-commuting vars
1045        IndexFactory wf = new IndexFactory(6, false);
1046        //System.out.println("wf = " + wf);
1047        IndexFactory wfw = new IndexFactory(6, true);
1048        //System.out.println("wfw = " + wfw + ", " + wfw.toScript());
1049
1050        // polynomials over integers
1051        GenExteriorPolynomialRing<BigInteger> pf = new GenExteriorPolynomialRing<BigInteger>(rf, wf);
1052        //System.out.println("pf = " + pf);
1053        GenExteriorPolynomialRing<BigInteger> pfw = new GenExteriorPolynomialRing<BigInteger>(rf, wfw);
1054        //System.out.println("pfw = " + pfw);
1055
1056        // test 1
1057        GenExteriorPolynomial<BigInteger> p = pf.getONE();
1058        //System.out.println("p = " + p);
1059        assertTrue("homogen(p): ", p.isHomogeneous());
1060
1061        GenExteriorPolynomial<BigInteger> q = pf.random(kl, ll*4, rl);
1062        //System.out.println("q = " + q);
1063        for (int i = 0; i <= wf.imaxlength; i++) {
1064            GenExteriorPolynomial<BigInteger> h = q.homogeneousPart(i);
1065            //System.out.println("h("+ i + ") = " + h);
1066            assertTrue("homogen(h): ", h.isHomogeneous());
1067            GenExteriorPolynomial<BigInteger> f = q.form(i);
1068            //System.out.println("f = " + f);
1069            assertTrue("homogen(f): ", f.isHomogeneous());
1070            assertEquals("homogen(h) == form(h): ", h, f);
1071        }
1072
1073        p = pfw.getZERO();
1074        for (int i = 0; i <= wf.imaxlength; i++) {
1075            GenExteriorPolynomial<BigInteger> hf = pfw.randomForm(kl, ll, i);
1076            //System.out.println("hf(" + i + ") = " + hf);
1077            assertTrue("homogen(hf): ", hf.isHomogeneous());
1078            assertTrue("deg(hf): ", hf.isZERO() || hf.degree() == (long)i);
1079            p = p.sum(hf);
1080        }
1081        //System.out.println("p = " + p);
1082    }
1083
1084}