001/*
002 * $Id$
003 */
004
005package edu.jas.fd;
006
007
008import java.util.Arrays;
009
010import edu.jas.arith.BigRational;
011import edu.jas.kern.ComputerThreads;
012import edu.jas.kern.PrettyPrint;
013import edu.jas.poly.GenSolvablePolynomial;
014import edu.jas.poly.GenSolvablePolynomialRing;
015import edu.jas.poly.RelationGenerator;
016import edu.jas.poly.ExpVector;
017import edu.jas.poly.TermOrder;
018import edu.jas.poly.WeylRelations;
019
020import junit.framework.Test;
021import junit.framework.TestCase;
022import junit.framework.TestSuite;
023
024
025/**
026 * SolvableQuotient over BigRational GenSolvablePolynomial tests with JUnit.
027 * @author Heinz Kredel
028 */
029
030public class SolvableQuotientTest extends TestCase {
031
032
033    /**
034     * main.
035     */
036    public static void main(String[] args) {
037        junit.textui.TestRunner.run(suite());
038    }
039
040
041    /**
042     * Constructs a <CODE>SolvableQuotientTest</CODE> object.
043     * @param name String.
044     */
045    public SolvableQuotientTest(String name) {
046        super(name);
047    }
048
049
050    /**
051     * suite.
052     */
053    public static Test suite() {
054        TestSuite suite = new TestSuite(SolvableQuotientTest.class);
055        return suite;
056    }
057
058
059    SolvableQuotientRing<BigRational> zFac;
060
061
062    SolvableQuotientRing<BigRational> efac;
063
064
065    GenSolvablePolynomialRing<BigRational> mfac;
066
067
068    SolvableQuotient<BigRational> a, b, c, d, e;
069
070
071    SolvableQuotient<BigRational> az, bz, cz, dz, ez;
072
073
074    int rl = 4;
075
076
077    int kl = 2;
078
079
080    int ll = 3; //6;
081
082
083    int el = 2;
084
085
086    float q = 0.2f;
087
088
089    @Override
090    protected void setUp() {
091        a = b = c = d = e = null;
092        TermOrder to = new TermOrder(TermOrder.INVLEX);
093        String[] vars = new String[] { "w", "x", "y", "z" };
094        mfac = new GenSolvablePolynomialRing<BigRational>(new BigRational(1), rl, to, vars);
095        RelationGenerator<BigRational> wl = new WeylRelations<BigRational>();
096        wl.generate(mfac);
097        efac = new SolvableQuotientRing<BigRational>(mfac);
098        zFac = new SolvableQuotientRing<BigRational>(mfac);
099    }
100
101
102    @Override
103    protected void tearDown() {
104        a = b = c = d = e = null;
105        //efac.terminate();
106        efac = null;
107        zFac = null;
108        ComputerThreads.terminate();
109    }
110
111
112    /**
113     * Test constructor and toString.
114     */
115    public void testConstruction() {
116        String ts = efac.toScript();
117        //System.out.println("efac = " + ts);
118        assertTrue("SRF in efac: " + ts, ts.indexOf("SRF") >= 0);
119        ts = efac.toString();
120        //System.out.println("efac = " + ts);
121        assertTrue("RatFunc in efac: " + ts, ts.indexOf("RatFunc") >= 0);
122        int h = efac.hashCode();
123        //System.out.println("efac.hashCode = " + h);
124        assertTrue("hashCode != 0", h != 0);
125
126        c = efac.getONE();
127        //System.out.println("c = " + c);
128        assertTrue("length( c ) = 1", c.num.length() == 1);
129        assertTrue("isZERO( c )", !c.isZERO());
130        assertTrue("isONE( c )", c.isONE());
131        ts = c.toScript();
132        //System.out.println("c = " + ts);
133        assertTrue("1 in c: " + ts, ts.indexOf("1") >= 0);
134        //ts = c.toString();
135        //System.out.println("c = " + ts);
136        assertTrue("1 in c: " + ts, ts.indexOf("1") >= 0);
137
138        d = efac.getZERO();
139        //System.out.println("d = " + d);
140        //System.out.println("d.val = " + d.val);
141        assertTrue("length( d ) = 0", d.num.length() == 0);
142        assertTrue("isZERO( d )", d.isZERO());
143        assertTrue("isONE( d )", !d.isONE());
144
145        for (SolvableQuotient<BigRational> g : efac.generators()) {
146            //System.out.println("g = " + g);
147            assertFalse("not isZERO( g )", g.isZERO());
148        }
149        //wrong, solved: 
150        assertTrue("isAssociative: ", efac.isAssociative());
151    }
152
153
154    /**
155     * Test random polynomial.
156     */
157    public void testRandom() {
158        for (int i = 0; i < 3; i++) {
159            //a = efac.random(ll+i);
160            a = efac.random(kl * i, ll + i/2, el, q);
161            //System.out.println("a = " + a);
162            if (a.isZERO() || a.isONE()) {
163                continue;
164            }
165            assertTrue("length( a" + i + " ) <> 0", a.num.length() >= 0);
166            assertTrue(" not isZERO( a" + i + " )", !a.isZERO());
167            assertTrue(" not isONE( a" + i + " )", !a.isONE());
168            assertEquals("a == a: ", a, a);
169            int h = a.hashCode();
170            //System.out.println("a.hashCode = " + h);
171            assertTrue("hashCode != 0", h != 0);
172        }
173    }
174
175
176    /**
177     * Test addition.
178     */
179    public void testAddition() {
180        a = efac.random(kl, ll, el, q);
181        b = efac.random(kl, ll, el, q);
182        //System.out.println("a = " + a);
183        //System.out.println("b = " + b);
184
185        c = a.sum(efac.getZERO());
186        d = a.subtract(efac.getZERO());
187        assertEquals("a+0 = a-0", c, d);
188
189        c = efac.getZERO().sum(a);
190        d = efac.getZERO().subtract(a.negate());
191        assertEquals("0+a = 0+(-a)", c, d);
192
193        c = a.sum(b);
194        d = c.subtract(b);
195        //System.out.println("c = " + c);
196        //System.out.println("d = " + d);
197        assertEquals("(a+b-b) == a: " + a + ", " + b, a, d);
198
199        c = a.sum(b);
200        d = b.sum(a);
201        //System.out.println("c = " + c);
202        //System.out.println("d = " + d);
203        assertEquals("a+b = b+a", c, d);
204
205        //c = efac.random(kl,ll,el,q);
206        c = new SolvableQuotient<BigRational>(efac, mfac.univariate(1, 2)); //efac.random(kl,ll,el,q);
207        //System.out.println("c = " + c);
208        d = c.sum(a.sum(b));
209        e = c.sum(a).sum(b);
210        //System.out.println("d = " + d);
211        //System.out.println("e = " + e);
212        assertEquals("c+(a+b) = (c+a)+b", d, e);
213    }
214
215
216    /**
217     * Test multiplication.
218     */
219    public void testMultiplication() {
220        a = efac.random(kl, ll, el, q);
221        b = efac.random(kl, ll, el, q);
222        //System.out.println("a = " + a);
223        //System.out.println("b = " + b);
224
225        c = a.multiply(efac.getONE());
226        d = efac.getONE().multiply(a);
227        //System.out.println("c = " + c);
228        //System.out.println("d = " + d);
229        assertEquals("a*1 = 1*a", c, a);
230        assertEquals("a*1 = 1*a", c, d);
231
232        c = b.multiply(a);
233        d = a.multiply(b);
234        //System.out.println("c = " + c);
235        //System.out.println("d = " + d);
236        //non-com assertFalse("a*b = b*a", c.equals(d) );
237        //e = d.subtract(c);
238        //non-com assertFalse("not isZERO( a*b-b*a ) " + e, e.isZERO() );
239
240        c = new SolvableQuotient<BigRational>(efac, mfac.univariate(1, 2)); //efac.random(kl,ll,el,q);
241        //System.out.println("c = " + c);
242        d = a.multiply(b.multiply(c));
243        //System.out.println("d = " + d);
244        e = (a.multiply(b)).multiply(c);
245        //System.out.println("e = " + e);
246        assertEquals("a(bc) = (ab)c", d, e);
247        //assertTrue("a(bc) = (ab)c", d.equals(e) );
248        if (a.isUnit()) {
249            c = a.inverse();
250            d = c.multiply(a);
251            //System.out.println("a = " + a);
252            //System.out.println("c = " + c);
253            //System.out.println("d = " + d);
254            assertTrue("a*1/a = 1", d.isONE());
255        }
256
257        c = new SolvableQuotient<BigRational>(efac, mfac.univariate(1, 2));
258        //System.out.println("c = " + c.toScript());
259        GenSolvablePolynomial<BigRational> n = c.num;
260        d = a.multiply(n);
261        e = a.multiply(c);
262        assertEquals("a*c.n = a*c", d, e);
263
264        ExpVector ldt = n.leadingExpVector();
265        d = a.multiply(ldt);
266        //e = a.multiply(c);
267        assertEquals("a*ldt(c.n) = a*c", d, e);
268
269        BigRational ldcf = BigRational.ONE;
270        ldcf = ldcf.sum(ldcf);
271        d = a.multiply(ldcf);
272        e = d.multiply(BigRational.HALF);
273        //System.out.println("a = " + a);
274        //System.out.println("d = " + d);
275        assertEquals("a = (a*2)*1/2", a, e);
276    }
277
278
279    /**
280     * Test addition without syzygy gcd.
281     */
282    public void testAdditionGcd() {
283        long te, tz;
284
285        a = efac.random(kl, ll, el, q);
286        b = efac.random(kl, ll, el, q);
287        //System.out.println("a = " + a);
288        //System.out.println("b = " + b);
289
290        az = new SolvableQuotient<BigRational>(zFac, a.num, a.den, true);
291        bz = new SolvableQuotient<BigRational>(zFac, b.num, b.den, true);
292        //if (false) {
293        //    return;
294        //}
295
296        te = System.currentTimeMillis();
297        c = a.sum(b);
298        d = c.subtract(b);
299        d = d.monic();
300        te = System.currentTimeMillis() - te;
301        assertEquals("a+b-b = a", a, d);
302
303        tz = System.currentTimeMillis();
304        cz = az.sum(bz);
305        dz = cz.subtract(bz);
306        dz = dz.monic();
307        tz = System.currentTimeMillis() - tz;
308        assertEquals("a+b-b = a", az, dz);
309
310        if (tz >= 0L || te >= 0L) { // true, findbugs
311            return;
312        }
313        System.out.println("te = " + te);
314        System.out.println("tz = " + tz);
315
316        c = a.sum(b);
317        d = b.sum(a);
318        //System.out.println("c = " + c);
319        //System.out.println("d = " + d);
320        assertEquals("a+b = b+a", c, d);
321
322        c = efac.random(kl, ll, el, q);
323        cz = new SolvableQuotient<BigRational>(zFac, c.num, c.den, true);
324
325
326        te = System.currentTimeMillis();
327        d = c.sum(a.sum(b));
328        e = c.sum(a).sum(b);
329        te = System.currentTimeMillis() - te;
330        assertEquals("c+(a+b) = (c+a)+b", d, e);
331
332        tz = System.currentTimeMillis();
333        dz = cz.sum(az.sum(bz));
334        ez = cz.sum(az).sum(bz);
335        tz = System.currentTimeMillis() - tz;
336        assertEquals("c+(a+b) = (c+a)+b", dz, ez);
337
338        System.out.println("te = " + te);
339        System.out.println("tz = " + tz);
340
341        c = a.sum(efac.getZERO());
342        d = a.subtract(efac.getZERO());
343        assertEquals("a+0 = a-0", c, d);
344
345        c = efac.getZERO().sum(a);
346        d = efac.getZERO().subtract(a.negate());
347        assertEquals("0+a = 0+(-a)", c, d);
348    }
349
350
351    /**
352     * Test parse.
353     */
354    public void testParse() {
355        a = efac.random(kl, ll, el + 1, q);
356        //PrettyPrint.setInternal();
357        //System.out.println("a = " + a);
358        PrettyPrint.setPretty();
359        //System.out.println("a = " + a);
360        String p = a.toString();
361        //System.out.println("p = " + p);
362        b = efac.parse(p);
363        //System.out.println("b = " + b);
364
365        //c = a.subtract(b);
366        //System.out.println("c = " + c);
367        assertEquals("parse(a.toSting()) = a", a, b);
368    }
369
370
371    /**
372     * Test egcd and fraction.
373     */
374    public void testEgcdFraction() {
375        a = efac.random(kl, ll, el, q);
376        b = efac.random(kl, ll, el, q);
377        //System.out.println("a = " + a);
378        //System.out.println("b = " + b);
379
380        SolvableQuotient<BigRational>[] egcd = a.egcd(b);
381        //System.out.println("egcd = " + Arrays.toString(egcd));
382        SolvableQuotient<BigRational> e0 = egcd[0];
383        SolvableQuotient<BigRational> e1 = egcd[1];
384        SolvableQuotient<BigRational> e2 = egcd[2];
385
386        c = a.multiply(e1).sum( b.multiply(e2) );
387        //System.out.println("c = " + c);
388        assertEquals("egcd[0] == c: " + e0 + "!=" + c, e0, c);
389
390        c = a.rightFraction();
391        //System.out.println("rf(a) = " + c);
392        assertTrue("isRightFraction: " + a + "rf" + c, a.isRightFraction(c) );
393        //System.out.println("a.num * c.den: " + a.num.multiply(c.den) );
394        //System.out.println("a.den * c.num: " + a.den.multiply(c.num) );
395    }
396
397}