001/*
002 * $Id$
003 */
004
005package edu.jas.fd;
006
007
008import edu.jas.arith.BigQuaternion;
009import edu.jas.arith.BigQuaternionRing;
010import edu.jas.gbufd.SolvableSyzygyAbstract;
011import edu.jas.gbufd.SolvableSyzygySeq;
012import edu.jas.kern.ComputerThreads;
013import edu.jas.poly.GenPolynomial;
014import edu.jas.poly.GenSolvablePolynomial;
015import edu.jas.poly.GenSolvablePolynomialRing;
016import edu.jas.poly.RecSolvablePolynomialRing;
017import edu.jas.poly.RelationGenerator;
018import edu.jas.poly.TermOrder;
019import edu.jas.poly.TermOrderByName;
020import edu.jas.poly.WeylRelationsIterated;
021import edu.jas.structure.GcdRingElem;
022
023import junit.framework.Test;
024import junit.framework.TestCase;
025import junit.framework.TestSuite;
026
027
028/**
029 * GCD LeftRight PRS algorithm tests with JUnit. <b>Note:</b> not in sync with
030 * implementation.
031 * @author Heinz Kredel
032 */
033
034public class GCDLeftRightTest extends TestCase {
035
036
037    /**
038     * main.
039     */
040    public static void main(String[] args) {
041        junit.textui.TestRunner.run(suite());
042        ComputerThreads.terminate();
043    }
044
045
046    /**
047     * Constructs a <CODE>GCDLeftRightTest</CODE> object.
048     * @param name String.
049     */
050    public GCDLeftRightTest(String name) {
051        super(name);
052    }
053
054
055    /**
056     */
057    public static Test suite() {
058        TestSuite suite = new TestSuite(GCDLeftRightTest.class);
059        return suite;
060    }
061
062
063    //GreatestCommonDivisorAbstract<BigQuaternion> fd;
064    GreatestCommonDivisorLR<BigQuaternion> fd;
065
066
067    TermOrder to = TermOrderByName.INVLEX;
068
069
070    BigQuaternionRing cfac;
071
072
073    GenSolvablePolynomialRing<BigQuaternion> dfac;
074
075
076    //GenSolvablePolynomialRing<GenPolynomial<BigQuaternion>> rfac;
077    RecSolvablePolynomialRing<BigQuaternion> rfac;
078
079
080    GenSolvablePolynomial<BigQuaternion> a, b, c, d, e, f, g, h, r, s;
081
082
083    GenSolvablePolynomial<GenPolynomial<BigQuaternion>> ar, br, cr, dr, er;
084
085
086    SolvableSyzygyAbstract<BigQuaternion> syz;
087
088
089    int rl = 4;
090
091
092    int kl = 2;
093
094
095    int ll = 4;
096
097
098    int el = 3;
099
100
101    float q = 0.35f;
102
103
104    @Override
105    protected void setUp() {
106        a = b = c = d = e = null;
107        ar = br = cr = dr = er = null;
108        String[] vars = new String[] { "a", "b", "c", "d" };
109        cfac = new BigQuaternionRing();
110        syz = new SolvableSyzygySeq<BigQuaternion>(cfac);
111        //System.out.println("syz = " + syz);
112        fd = new GreatestCommonDivisorLR<BigQuaternion>(cfac, syz);
113        //fd = new GreatestCommonDivisorFake<BigQuaternion>(cfac);
114        dfac = new GenSolvablePolynomialRing<BigQuaternion>(cfac, rl, to, vars);
115        RelationGenerator<BigQuaternion> wl = new WeylRelationsIterated<BigQuaternion>();
116        dfac.addRelations(wl);
117        rfac = (RecSolvablePolynomialRing<BigQuaternion>) dfac.recursive(1);
118        //System.out.println("dfac = " + dfac);
119    }
120
121
122    @Override
123    protected void tearDown() {
124        a = b = c = d = e = null;
125        ar = br = cr = dr = er = null;
126        fd = null;
127        cfac = null;
128        dfac = null;
129        rfac = null;
130        syz = null;
131    }
132
133
134    /**
135     * Test base field gcd left - right.
136     */
137    public void testBaseGcdLeftRight() {
138        String[] uvars = new String[] { "x" };
139        dfac = new GenSolvablePolynomialRing<BigQuaternion>(cfac, 1, to, uvars);
140        BigQuaternion cc;
141        for (int i = 0; i < 1; i++) {
142            a = dfac.random(kl * (i + 2), ll + i, el + 1, q);
143            b = dfac.random(kl * (i + 1), ll + i, el + 1, q);
144            c = dfac.random(kl * (i + 1), ll + 1, el + 0, q);
145            //c = dfac.getONE(); 
146            c = c.multiply(dfac.univariate(0));
147            cc = cfac.random(kl);
148            //c = c.multiplyLeft(cc);
149            if (c.isZERO() || cc.isZERO()) {
150                // skip for this turn
151                continue;
152            }
153            //a = fd.basePrimitivePart(a);
154            //b = fd.basePrimitivePart(b);
155            //c = (GenSolvablePolynomial<BigQuaternion>) fd.basePrimitivePart(c).abs();
156            //System.out.println("a  = " + a);
157            //System.out.println("b  = " + b);
158            //System.out.println("cc = " + cc);
159            //System.out.println("c  = " + c);
160
161            a = a.multiply(c).multiplyLeft(cc);
162            b = b.multiply(c).multiplyLeft(cc);
163            //System.out.println("a  = " + a);
164            //System.out.println("b  = " + b);
165
166            GCDcoFactors<BigQuaternion> cont = fd.leftRightBaseGcd(a, b);
167            //d = (GenSolvablePolynomial<BigQuaternion>) cont.left;
168            //System.out.println("cont = " + cont);
169            //System.out.println("cont.isGCD() = " + cont.isGCD());
170            assertTrue("cont.isGCD(): " + cont, cont.isGCD());
171        }
172    }
173
174
175    /**
176     * Test base field gcd right - left.
177     */
178    public void testBaseGcdRightLeft() {
179        String[] uvars = new String[] { "x" };
180        dfac = new GenSolvablePolynomialRing<BigQuaternion>(cfac, 1, to, uvars);
181        BigQuaternion cc;
182        for (int i = 0; i < 1; i++) {
183            a = dfac.random(kl * (i + 2), ll + i, el + 1, q);
184            b = dfac.random(kl * (i + 1), ll + i, el + 1, q);
185            c = dfac.random(kl * (i + 1), ll + 1, el + 0, q);
186            //c = dfac.getONE(); 
187            c = c.multiply(dfac.univariate(0));
188            cc = cfac.random(kl);
189            //c = c.multiplyLeft(cc);
190            if (c.isZERO() || cc.isZERO()) {
191                // skip for this turn
192                continue;
193            }
194            //a = fd.basePrimitivePart(a);
195            //b = fd.basePrimitivePart(b);
196            //c = (GenSolvablePolynomial<BigQuaternion>) fd.basePrimitivePart(c).abs();
197            //System.out.println("a  = " + a);
198            //System.out.println("b  = " + b);
199            //System.out.println("cc = " + cc);
200            //System.out.println("c  = " + c);
201
202            a = c.multiply(cc).multiply(a);
203            b = c.multiply(cc).multiply(b);
204            //System.out.println("a  = " + a);
205            //System.out.println("b  = " + b);
206
207            GCDcoFactors<BigQuaternion> cont = fd.rightLeftBaseGcd(a, b);
208            //d = (GenSolvablePolynomial<BigQuaternion>) cont.left;
209            //System.out.println("cont = " + cont);
210            //System.out.println("cont.isGCD() = " + cont.isGCD());
211            assertTrue("cont.isGCD(): " + cont, cont.isGCD());
212        }
213    }
214
215
216    /**
217     * Test base field left gcd.
218     */
219    public void testBaseLeftGcd() {
220        String[] uvars = new String[] { "x" };
221        dfac = new GenSolvablePolynomialRing<BigQuaternion>(cfac, 1, to, uvars);
222        BigQuaternion cc;
223        for (int i = 0; i < 1; i++) {
224            a = dfac.random(kl * (i + 2), ll + i, el + 1, q);
225            b = dfac.random(kl * (i + 1), ll + i, el + 1, q);
226            c = dfac.random(kl * (i + 1), ll + 1, el + 0, q);
227            //c = dfac.getONE(); 
228            c = c.multiply(dfac.univariate(0));
229            cc = cfac.random(kl);
230            c = c.multiplyLeft(cc);
231            if (c.isZERO() || cc.isZERO()) {
232                // skip for this turn
233                continue;
234            }
235            //a = fd.basePrimitivePart(a);
236            //b = fd.basePrimitivePart(b);
237            //c = (GenSolvablePolynomial<BigQuaternion>) fd.basePrimitivePart(c).abs();
238            //System.out.println("a  = " + a);
239            //System.out.println("b  = " + b);
240            ////System.out.println("cc = " + cc);
241            //System.out.println("c  = " + c);
242
243            a = c.multiply(a);
244            b = c.multiply(b);
245            //System.out.println("a  = " + a);
246            //System.out.println("b  = " + b);
247
248            d = fd.leftBaseGcd(a, b);
249            //System.out.println("c = " + c);
250            //System.out.println("d = " + d);
251            //assertTrue("cont.isGCD(): " + cont, cont.isGCD());
252
253            e = FDUtil.<BigQuaternion> rightBaseSparsePseudoRemainder(a, d);
254            //System.out.println("e = " + e);
255            assertTrue("e == 0: " + e, e.isZERO());
256            f = FDUtil.<BigQuaternion> rightBaseSparsePseudoRemainder(b, d);
257            //System.out.println("f = " + f);
258            assertTrue("f == 0: " + f, f.isZERO());
259        }
260    }
261
262
263    /**
264     * Test base field right gcd.
265     */
266    public void testBaseRightGcd() {
267        String[] uvars = new String[] { "x" };
268        dfac = new GenSolvablePolynomialRing<BigQuaternion>(cfac, 1, to, uvars);
269        BigQuaternion cc;
270        for (int i = 0; i < 1; i++) {
271            a = dfac.random(kl * (i + 2), ll + i, el + 1, q);
272            b = dfac.random(kl * (i + 1), ll + i, el + 1, q);
273            c = dfac.random(kl * (i + 1), ll + 1, el + 0, q);
274            //c = dfac.getONE(); 
275            c = c.multiply(dfac.univariate(0));
276            cc = cfac.random(kl);
277            c = c.multiply(cc);
278            if (c.isZERO() || cc.isZERO()) {
279                // skip for this turn
280                continue;
281            }
282            //a = fd.basePrimitivePart(a);
283            //b = fd.basePrimitivePart(b);
284            //c = (GenSolvablePolynomial<BigQuaternion>) fd.basePrimitivePart(c).abs();
285            //System.out.println("a  = " + a);
286            //System.out.println("b  = " + b);
287            ////System.out.println("cc = " + cc);
288            //System.out.println("c  = " + c);
289
290            a = a.multiply(c);
291            b = b.multiply(c);
292            //System.out.println("a  = " + a);
293            //System.out.println("b  = " + b);
294
295            d = fd.rightBaseGcd(a, b);
296            //System.out.println("c = " + c);
297            //System.out.println("d = " + d);
298            //assertTrue("cont.isGCD(): " + cont, cont.isGCD());
299
300            e = FDUtil.<BigQuaternion> leftBaseSparsePseudoRemainder(a, d);
301            //System.out.println("e = " + e);
302            assertTrue("e == 0: " + e, e.isZERO());
303            f = FDUtil.<BigQuaternion> leftBaseSparsePseudoRemainder(b, d);
304            //System.out.println("f = " + f);
305            assertTrue("f == 0: " + f, f.isZERO());
306        }
307    }
308
309
310    /**
311     * Test base integer gcd left - right.
312     */
313    public void testBaseIntegerGcdLeftRight() {
314        String[] uvars = new String[] { "x" };
315        cfac = new BigQuaternionRing(true);
316        syz = new SolvableSyzygySeq<BigQuaternion>(cfac);
317        //System.out.println("syz = " + syz);
318        dfac = new GenSolvablePolynomialRing<BigQuaternion>(cfac, 1, to, uvars);
319        fd = new GreatestCommonDivisorLR<BigQuaternion>(cfac, syz);
320        BigQuaternion cc;
321        for (int i = 0; i < 1; i++) {
322            a = dfac.random(kl * (i + 2), ll + 1, el + 2, q);
323            b = dfac.random(kl * (i + 1), ll + 1, el + 2, q);
324            c = dfac.random(kl * (i + 1), ll + 1, el + 1, q);
325            //c = dfac.getONE(); 
326            c = c.multiply(dfac.univariate(0));
327            c = (GenSolvablePolynomial<BigQuaternion>) c.abs();
328            cc = cfac.random(kl);
329            //cc = cfac.getONE(); 
330            //c = c.multiplyLeft(cc);
331            if (c.isZERO() || cc.isZERO()) {
332                // skip for this turn
333                continue;
334            }
335            //a = fd.basePrimitivePart(a);
336            //b = fd.basePrimitivePart(b);
337            //c = (GenSolvablePolynomial<BigQuaternion>) fd.basePrimitivePart(c).abs();
338            // replace - by ~ in coefficents for polynomial tokenizer
339            //a = dfac.parse("( 3/2i1/2j1/2k~1/2 ) x^3 - ( 1i~1j0k~2 ) x + ( 11/2i15/2j1/2k~1/2 )");
340            //b = dfac.parse("( ~1i2j3k1 ) x^4 + ( 0i1j~2k0 ) x^2 + ( 1/2i~3/2j1/2k3/2 )");
341            //c = dfac.parse("( 1/2i1/2j~1/2k1/2 ) x^4 + ( 1i~1j0k0 ) x^3 + 1i1j0k1 x");
342
343            //System.out.println("a  = " + a);
344            //System.out.println("b  = " + b);
345            //System.out.println("cc = " + cc);
346            //System.out.println("c  = " + c);
347
348            a = a.multiply(c).multiplyLeft(cc);
349            b = b.multiply(c).multiplyLeft(cc);
350            //System.out.println("a  = " + a);
351            //System.out.println("b  = " + b);
352
353            GCDcoFactors<BigQuaternion> cont = fd.leftRightBaseGcd(a, b);
354            //d = (GenSolvablePolynomial<BigQuaternion>) cont.left;
355            //System.out.println("cont = " + cont);
356            //System.out.println("cont.isGCD(): = " + cont.isGCD());
357            //System.out.println("r = " + cont.right + ", l=" + cont.left);
358            //System.out.println("c = " + c + ", cc = " + cc); 
359            assertTrue("cont.isGCD(): " + cont, cont.isGCD());
360        }
361    }
362
363
364    /**
365     * Test base integer gcd right - left.
366     */
367    public void testBaseIntegerGcdRightLeft() {
368        String[] uvars = new String[] { "x" };
369        cfac = new BigQuaternionRing(true);
370        syz = new SolvableSyzygySeq<BigQuaternion>(cfac);
371        //System.out.println("syz = " + syz);
372        dfac = new GenSolvablePolynomialRing<BigQuaternion>(cfac, 1, to, uvars);
373        fd = new GreatestCommonDivisorLR<BigQuaternion>(cfac, syz);
374        BigQuaternion cc;
375        for (int i = 0; i < 1; i++) {
376            a = dfac.random(kl * (i + 2), ll + 1, el + 2, q);
377            b = dfac.random(kl * (i + 1), ll + 1, el + 2, q);
378            c = dfac.random(kl * (i + 1), ll + 1, el + 1, q);
379            //c = dfac.getONE(); 
380            c = c.multiply(dfac.univariate(0));
381            c = (GenSolvablePolynomial<BigQuaternion>) c.abs();
382            cc = cfac.random(kl);
383            //cc = cfac.getONE(); 
384            //c = c.multiplyLeft(cc);
385            if (c.isZERO() || cc.isZERO()) {
386                // skip for this turn
387                continue;
388            }
389            //System.out.println("a  = " + a);
390            //System.out.println("b  = " + b);
391            //System.out.println("cc = " + cc);
392            //System.out.println("c  = " + c);
393
394            a = a.multiply(c).multiplyLeft(cc);
395            b = b.multiply(c).multiplyLeft(cc);
396            //System.out.println("a  = " + a);
397            //System.out.println("b  = " + b);
398
399            GCDcoFactors<BigQuaternion> cont = fd.rightLeftBaseGcd(a, b);
400            //d = (GenSolvablePolynomial<BigQuaternion>) cont.left;
401            //System.out.println("cont = " + cont);
402            //System.out.println("cont.isGCD() = " + cont.isGCD());
403            //System.out.println("r = " + cont.right + ", l=" + cont.left);
404            //System.out.println("c = " + c + ", cc = " + cc); 
405            assertTrue("cont.isGCD(): " + cont, cont.isGCD());
406        }
407    }
408
409
410    /**
411     * Test base integral left gcd.
412     */
413    public void testBaseIntegralLeftGcd() {
414        String[] uvars = new String[] { "x" };
415        cfac = new BigQuaternionRing(true);
416        syz = new SolvableSyzygySeq<BigQuaternion>(cfac);
417        //System.out.println("syz = " + syz);
418        dfac = new GenSolvablePolynomialRing<BigQuaternion>(cfac, 1, to, uvars);
419        fd = new GreatestCommonDivisorLR<BigQuaternion>(cfac, syz);
420        BigQuaternion cc;
421        for (int i = 0; i < 1; i++) {
422            a = dfac.random(kl * (i + 2), ll + i, el + 1, q);
423            b = dfac.random(kl * (i + 1), ll + i, el + 1, q);
424            c = dfac.random(kl * (i + 1), ll + 1, el + 0, q);
425            //c = dfac.getONE(); 
426            c = c.multiply(dfac.univariate(0));
427            cc = cfac.random(kl);
428            c = c.multiplyLeft(cc);
429            if (c.isZERO() || cc.isZERO()) {
430                // skip for this turn
431                continue;
432            }
433            // replace - by ~ in coefficents for polynomial tokenizer
434            //a = dfac.parse("( 1i0j~1k0 ) x^2 - ( 9/2i5/2j~1/2k~1/2 ) x + 5/2i5/2j9/2k3/2");
435            //b = dfac.parse("1i1j1k1 x^3 + ( 1i~1j1k1 )");
436            ////c = dfac.parse("( ~3i11j~3k~1 ) x^2 - ( 5i~10j~5k0 ) x");
437            //c = dfac.parse("( ~3i11j~3k~1 ) x - ( 5i~10j~5k0 ) ");
438
439            //System.out.println("a  = " + a);
440            //System.out.println("b  = " + b);
441            ////System.out.println("cc = " + cc);
442            //System.out.println("c  = " + c);
443
444            a = c.multiply(a);
445            b = c.multiply(b);
446            //System.out.println("a  = " + a);
447            //System.out.println("b  = " + b);
448
449            d = fd.leftBaseGcd(a, b);
450            //System.out.println("c = " + c);
451            //System.out.println("d = " + d);
452
453            e = FDUtil.<BigQuaternion> rightBaseSparsePseudoRemainder(a, d);
454            //System.out.println("e = " + e);
455            assertTrue("e == 0: " + e, e.isZERO());
456            f = FDUtil.<BigQuaternion> rightBaseSparsePseudoRemainder(b, d);
457            //System.out.println("f = " + f);
458            assertTrue("f == 0: " + f, f.isZERO());
459        }
460    }
461
462
463    /**
464     * Test base integral right gcd.
465     */
466    public void testBaseIntegralRightGcd() {
467        String[] uvars = new String[] { "x" };
468        cfac = new BigQuaternionRing(true);
469        syz = new SolvableSyzygySeq<BigQuaternion>(cfac);
470        //System.out.println("syz = " + syz);
471        dfac = new GenSolvablePolynomialRing<BigQuaternion>(cfac, 1, to, uvars);
472        fd = new GreatestCommonDivisorLR<BigQuaternion>(cfac, syz);
473        BigQuaternion cc;
474        for (int i = 0; i < 1; i++) {
475            a = dfac.random(kl * (i + 2), ll + i, el + 1, q);
476            b = dfac.random(kl * (i + 1), ll + i, el + 1, q);
477            c = dfac.random(kl * (i + 1), ll + 1, el + 0, q);
478            //c = dfac.getONE(); 
479            c = c.multiply(dfac.univariate(0));
480            cc = cfac.random(kl);
481            c = c.multiply(cc);
482            if (c.isZERO() || cc.isZERO()) {
483                // skip for this turn
484                continue;
485            }
486            // replace - by ~ in coefficents for polynomial tokenizer
487            //a = dfac.parse("( 5/2i~1/2j~3/2k~3/2 ) x^2 - ( 1i0j0k~1 )");
488            //b = dfac.parse("( 1i~1j1k0 ) x^2 - 1i2j0k0 x + ( 1/2i1/2j~1/2k3/2 )");
489            //c = dfac.parse("( 0i~1j~2k~14 ) x");
490
491            //System.out.println("a  = " + a);
492            //System.out.println("b  = " + b);
493            ////System.out.println("cc = " + cc);
494            //System.out.println("c  = " + c);
495
496            a = a.multiply(c);
497            b = b.multiply(c);
498            //System.out.println("a  = " + a);
499            //System.out.println("b  = " + b);
500
501            d = fd.rightBaseGcd(a, b);
502            //System.out.println("c  = " + c);
503            //System.out.println("d  = " + d);
504            //d = fd.leftBasePrimitivePart(fd.rightBasePrimitivePart(d));
505            //System.out.println("d = " + d);
506
507            e = FDUtil.<BigQuaternion> leftBaseSparsePseudoRemainder(a, d);
508            //System.out.println("e  = " + e);
509            assertTrue("e == 0: " + e, e.isZERO());
510            f = FDUtil.<BigQuaternion> leftBaseSparsePseudoRemainder(b, d);
511            //System.out.println("f  = " + f);
512            assertTrue("f == 0: " + f, f.isZERO());
513        }
514    }
515
516
517    /**
518     * Test base integer quotient remainder.
519     */
520    public void testBaseQR() {
521        String[] uvars = new String[] { "x" };
522        cfac = new BigQuaternionRing(true);
523        syz = new SolvableSyzygySeq<BigQuaternion>(cfac);
524        //System.out.println("syz = " + syz);
525        dfac = new GenSolvablePolynomialRing<BigQuaternion>(cfac, 1, to, uvars);
526        fd = new GreatestCommonDivisorLR<BigQuaternion>(cfac, syz);
527        GreatestCommonDivisorAbstract<BigQuaternion> fds = new GreatestCommonDivisorSimple<BigQuaternion>(
528                        cfac);
529        //BigQuaternion cc;
530        for (int i = 0; i < 1; i++) {
531            a = dfac.random(kl * (i + 2), ll + 1, el + 2, q);
532            b = dfac.random(kl * (i + 1), ll + 1, el + 2, q);
533            c = dfac.random(kl * (i + 1), ll + 1, el + 1, q);
534            //c = dfac.getONE(); 
535            c = c.multiply(dfac.univariate(0));
536            c = (GenSolvablePolynomial<BigQuaternion>) c.abs();
537            if (c.isZERO()) {
538                // skip for this turn
539                continue;
540            }
541            //System.out.println("a  = " + a);
542            //System.out.println("b  = " + b);
543            //System.out.println("c  = " + c);
544
545            a = a.multiply(c);
546            b = b.multiply(c);
547            //System.out.println("a  = " + a);
548            //System.out.println("b  = " + b);
549
550            GenSolvablePolynomial<BigQuaternion>[] aqr, bqr;
551
552            aqr = FDUtil.<BigQuaternion> leftBasePseudoQuotientRemainder(a, c);
553            bqr = FDUtil.<BigQuaternion> leftBasePseudoQuotientRemainder(b, c);
554            d = aqr[0];
555            e = bqr[0];
556
557            //System.out.println("d  = " + d + ", rem = " + aqr[1]);
558            //System.out.println("e  = " + e + ", rem = " + bqr[1]);
559            assertTrue("a rem == 0: ", aqr[1].isZERO());
560            assertTrue("b rem == 0: ", bqr[1].isZERO());
561
562            boolean t;
563            f = d.multiply(c);
564            //System.out.println("f  = " + f);
565            //System.out.println("a  = " + a);
566            t = f.equals(a);
567            //System.out.println("d*c == a: " + t);
568
569            BigQuaternion qa, qb, oa, ob;
570            qa = a.leadingBaseCoefficient();
571            qb = f.leadingBaseCoefficient();
572            GcdRingElem<BigQuaternion>[] oc = fd.leftOreCond(qa, qb);
573            oa = (BigQuaternion) oc[0];
574            ob = (BigQuaternion) oc[1];
575
576            a = a.multiplyLeft(oa);
577            f = f.multiplyLeft(ob);
578            //System.out.println("f  = " + f);
579            //System.out.println("a  = " + a);
580            t = f.equals(a);
581            //System.out.println("d*c == a: " + t);
582            assertTrue("d*c == a: ", t);
583
584
585            g = e.multiply(c);
586            //System.out.println("g  = " + g);
587            //System.out.println("b  = " + b);
588            t = g.equals(b);
589            //System.out.println("e*c == b: " + t);
590            assertTrue("e*c == b: ", t || true);
591
592            r = (GenSolvablePolynomial<BigQuaternion>) fds.leftBasePrimitivePart(b).abs();
593            s = (GenSolvablePolynomial<BigQuaternion>) fds.leftBasePrimitivePart(g).abs();
594            //System.out.println("pp(b)  = " + r);
595            //System.out.println("pp(g)  = " + s);
596            assertEquals("pp(b) == pp(g): ", r, s);
597
598            qa = b.leadingBaseCoefficient();
599            qb = g.leadingBaseCoefficient();
600            oc = fd.leftOreCond(qa, qb);
601            oa = (BigQuaternion) oc[0];
602            ob = (BigQuaternion) oc[1];
603
604            b = b.multiplyLeft(oa);
605            g = g.multiplyLeft(ob);
606            //System.out.println("g  = " + g);
607            //System.out.println("b  = " + b);
608            t = g.equals(b);
609            //System.out.println("e*c == b: " + t);
610            assertTrue("e*c == b: ", t);
611        }
612    }
613
614
615    /**
616     * Test base integral left / right pseudo division.
617     */
618    public void testBaseIntegralDivision() {
619        String[] uvars = new String[] { "x" };
620        cfac = new BigQuaternionRing(true);
621        syz = new SolvableSyzygySeq<BigQuaternion>(cfac);
622        //System.out.println("syz = " + syz);
623        dfac = new GenSolvablePolynomialRing<BigQuaternion>(cfac, 1, to, uvars);
624        fd = new GreatestCommonDivisorLR<BigQuaternion>(cfac, syz);
625        BigQuaternion cc;
626        for (int i = 0; i < 1; i++) {
627            a = dfac.random(kl * (i + 2), ll + i, el + 1, q);
628            b = dfac.random(kl * (i + 1), ll + i, el + 1, q);
629            c = dfac.random(kl * (i + 1), ll + 1, el + 0, q);
630            //c = dfac.getONE(); 
631            c = c.multiply(dfac.univariate(0));
632            cc = cfac.random(kl);
633            c = c.multiplyLeft(cc);
634            if (c.isZERO() || cc.isZERO()) {
635                // skip for this turn
636                continue;
637            }
638            // replace - by ~ in coefficents for polynomial tokenizer
639            //a = dfac.parse("( 1i~3j~3k~3 ) x^3 - ( 9i7j~12k~8 ) x^2 + 20i17j14k10 x + ( 19/2i27/2j~31/2k~7/2 ) ");
640            //b = dfac.parse("( 3i2j~1k0 ) x^4 + ( 2i1j~3k~1 ) x^3 + ( 3i0j2k~1 ) x + ( 5/2i3/2j1/2k~5/2 ) ");
641            //c = dfac.parse("0i0j4k0 x - ( 1/2i~1/2j~5/2k~5/2 ) ");
642
643            //System.out.println("a  = " + a);
644            //System.out.println("b  = " + b);
645            ////System.out.println("cc = " + cc);
646            //System.out.println("c  = " + c);
647
648            //System.out.println("ppl(a) = " + fd.leftPrimitivePart(a));
649            //System.out.println("ppl(b) = " + fd.leftPrimitivePart(b));
650            //System.out.println("ppr(a) = " + fd.rightPrimitivePart(a));
651            //System.out.println("ppr(b) = " + fd.rightPrimitivePart(b));
652            //b = fd.rightPrimitivePart(b);
653
654            s = b;
655            r = a;
656            while (!r.isZERO()) {
657                GenSolvablePolynomial<BigQuaternion>[] qr;
658                GenSolvablePolynomial<BigQuaternion> x, y, z;
659                qr = FDUtil.<BigQuaternion> rightBasePseudoQuotientRemainder(s, r);
660                y = qr[0];
661                x = qr[1];
662                z = (GenSolvablePolynomial<BigQuaternion>) r.multiply(y).sum(x);
663                //System.out.println("z = " + z + ", s = " + s);
664                BigQuaternion lz, ls;
665                lz = z.leadingBaseCoefficient();
666                ls = s.leadingBaseCoefficient();
667                GcdRingElem[] oc = fd.rightOreCond(lz, ls);
668                z = z.multiply((BigQuaternion) oc[0]);
669                s = s.multiply((BigQuaternion) oc[1]);
670                //System.out.println("z * oa = " + z);
671                //System.out.println("s * os = " + s);
672                //System.out.println("z * oz == s * os: " + z.equals(s));
673                assertEquals("z * oz == s * os: ", z, s);
674                s = r;
675                r = x;
676                //System.out.println("r  = " + r);
677            }
678            //System.out.println("s  = " + s);
679            //s = fd.rightPrimitivePart(s);
680            //s = fd.leftPrimitivePart(s);
681            //System.out.println("s  = " + s);
682            //System.out.println("c  = " + c + ", s==c: " + s.equals(c));
683            g = s;
684
685            GenSolvablePolynomial<BigQuaternion>[] qr;
686            qr = FDUtil.<BigQuaternion> rightBasePseudoQuotientRemainder(a, g);
687            d = qr[0];
688            h = (GenSolvablePolynomial<BigQuaternion>) g.multiply(d).sum(qr[1]);
689            //System.out.println("h  = " + h);
690            //System.out.println("a  = " + a);
691
692            BigQuaternion lh, la, lb;
693            lh = h.leadingBaseCoefficient();
694            la = a.leadingBaseCoefficient();
695            GcdRingElem[] oc = fd.rightOreCond(lh, la);
696            h = h.multiply((BigQuaternion) oc[0]);
697            s = a.multiply((BigQuaternion) oc[1]);
698            assertEquals("h * oh == a * oa: ", h, s);
699
700            //assertTrue("r==0: ", qr[1].isZERO());
701
702            qr = FDUtil.<BigQuaternion> rightBasePseudoQuotientRemainder(b, g);
703            e = qr[0];
704            h = (GenSolvablePolynomial<BigQuaternion>) g.multiply(e).sum(qr[1]);
705            //System.out.println("h  = " + h);
706            //System.out.println("b  = " + b);
707
708            lh = h.leadingBaseCoefficient();
709            lb = b.leadingBaseCoefficient();
710            oc = fd.rightOreCond(lh, lb);
711            h = h.multiply((BigQuaternion) oc[0]);
712            s = b.multiply((BigQuaternion) oc[1]);
713            assertEquals("h * oh == a * oa: ", h, s);
714
715            //System.out.println("d  = " + d + ", rem = " + qr[1]);
716            //System.out.println("e  = " + e + ", rem = " + qr[1]);
717            f = g.multiply(d);
718            g = g.multiply(e);
719            //System.out.println("f  = " + f + ", a==f? " + a.equals(f));
720            //System.out.println("g  = " + g + ", b==g? " + b.equals(g));
721
722            BigQuaternion lf, lg;
723            la = a.leadingBaseCoefficient();
724            lb = b.leadingBaseCoefficient();
725            lf = f.leadingBaseCoefficient();
726            lg = g.leadingBaseCoefficient();
727
728            oc = fd.rightOreCond(la, lf);
729            r = a.multiply((BigQuaternion) oc[0]);
730            s = f.multiply((BigQuaternion) oc[1]);
731            //System.out.println("a * oa = " + r);
732            //System.out.println("f * of = " + s);
733            //System.out.println("a * oa == f * of: " + r.equals(s));
734            assertEquals("a * oa == f * of: ", r, s);
735
736            oc = fd.rightOreCond(lb, lg);
737            r = b.multiply((BigQuaternion) oc[0]);
738            s = g.multiply((BigQuaternion) oc[1]);
739            //System.out.println("b * ob = " + r);
740            //System.out.println("g * og = " + s);
741            //System.out.println("b * ob == g * og: " + r.equals(s));
742            assertEquals("b * ob == g * og: ", r, s);
743
744        }
745    }
746}