001
002/*
003 * $Id$
004 */
005
006package edu.jas.ufd;
007
008
009import java.util.SortedMap;
010
011import edu.jas.arith.BigRational;
012import edu.jas.kern.ComputerThreads;
013import edu.jas.kern.PrettyPrint;
014import edu.jas.poly.GenPolynomialRing;
015import edu.jas.poly.TermOrder;
016
017import junit.framework.Test;
018import junit.framework.TestCase;
019import junit.framework.TestSuite;
020
021
022/**
023 * Quotient over BigRational GenPolynomial tests with JUnit.
024 * @author Heinz Kredel
025 */
026
027public class QuotientRatTest extends TestCase {
028
029
030    /**
031     * main.
032     */
033    public static void main(String[] args) {
034        junit.textui.TestRunner.run(suite());
035    }
036
037
038    /**
039     * Constructs a <CODE>QuotientRatTest</CODE> object.
040     * @param name String.
041     */
042    public QuotientRatTest(String name) {
043        super(name);
044    }
045
046
047    /**
048     * suite.
049     */
050    public static Test suite() {
051        TestSuite suite = new TestSuite(QuotientRatTest.class);
052        return suite;
053    }
054
055
056    //private final static int bitlen = 100;
057
058    QuotientRing<BigRational> zFac, efac;
059
060
061    GenPolynomialRing<BigRational> mfac;
062
063
064    Quotient<BigRational> a, b, c, d, e;
065
066
067    Quotient<BigRational> az, bz, cz, dz, ez;
068
069
070    int rl = 3;
071
072
073    int kl = 5;
074
075
076    int ll = 3; //6;
077
078
079    int el = 2;
080
081
082    float q = 0.4f;
083
084
085    @Override
086    protected void setUp() {
087        a = b = c = d = e = null;
088        TermOrder to = new TermOrder(TermOrder.INVLEX);
089        mfac = new GenPolynomialRing<BigRational>(new BigRational(1), rl, to);
090        efac = new QuotientRing<BigRational>(mfac);
091        zFac = new QuotientRing<BigRational>(mfac, false);
092    }
093
094
095    @Override
096    protected void tearDown() {
097        a = b = c = d = e = null;
098        //efac.terminate();
099        efac = null;
100        zFac = null;
101        ComputerThreads.terminate();
102    }
103
104
105    /**
106     * Test constructor and toString.
107     */
108    public void testConstruction() {
109        c = efac.getONE();
110        //System.out.println("c = " + c);
111        //System.out.println("c.val = " + c.val);
112        assertTrue("length( c ) = 1", c.num.length() == 1);
113        assertTrue("isZERO( c )", !c.isZERO());
114        assertTrue("isONE( c )", c.isONE());
115
116        d = efac.getZERO();
117        //System.out.println("d = " + d);
118        //System.out.println("d.val = " + d.val);
119        assertTrue("length( d ) = 0", d.num.length() == 0);
120        assertTrue("isZERO( d )", d.isZERO());
121        assertTrue("isONE( d )", !d.isONE());
122    }
123
124
125    /**
126     * Test random polynomial.
127     */
128    public void testRandom() {
129        for (int i = 0; i < 7; i++) {
130            //a = efac.random(ll+i);
131            a = efac.random(kl * (i + 1), ll + 2 + 2 * i, el, q);
132            //System.out.println("a = " + a);
133            if (a.isZERO() || a.isONE()) {
134                continue;
135            }
136            assertTrue("length( a" + i + " ) <> 0", a.num.length() >= 0);
137            assertTrue(" not isZERO( a" + i + " )", !a.isZERO());
138            assertTrue(" not isONE( a" + i + " )", !a.isONE());
139        }
140    }
141
142
143    /**
144     * Test addition.
145     */
146    public void testAddition() {
147        a = efac.random(kl, ll, el, q);
148        b = efac.random(kl, ll, el, q);
149        //System.out.println("a = " + a);
150        //System.out.println("b = " + b);
151
152        c = a.sum(b);
153        d = c.subtract(b);
154        //System.out.println("c = " + c);
155        //System.out.println("d = " + d);
156        d = d.monic();
157        //System.out.println("d = " + d);
158        assertEquals("a+b-b = a", a, d);
159
160        c = a.sum(b);
161        d = b.sum(a);
162        //System.out.println("c = " + c);
163        //System.out.println("d = " + d);
164        assertEquals("a+b = b+a", c, d);
165
166        //System.out.println("monic(d) = " + d.monic());
167
168        c = efac.random(kl, ll, el, q);
169        //System.out.println("c = " + c);
170        d = c.sum(a.sum(b));
171        e = c.sum(a).sum(b);
172        //System.out.println("d = " + d);
173        //System.out.println("e = " + e);
174        assertEquals("c+(a+b) = (c+a)+b", d, e);
175
176
177        c = a.sum(efac.getZERO());
178        d = a.subtract(efac.getZERO());
179        assertEquals("a+0 = a-0", c, d);
180
181        c = efac.getZERO().sum(a);
182        d = efac.getZERO().subtract(a.negate());
183        assertEquals("0+a = 0+(-a)", c, d);
184    }
185
186
187    /**
188     * Test object multiplication.
189     */
190    public void testMultiplication() {
191        a = efac.random(kl, ll, el, q);
192        //assertTrue("not isZERO( a )", !a.isZERO() );
193
194        b = efac.random(kl, ll, el, q);
195        //assertTrue("not isZERO( b )", !b.isZERO() );
196
197        c = b.multiply(a);
198        d = a.multiply(b);
199        //assertTrue("not isZERO( c )", !c.isZERO() );
200        //assertTrue("not isZERO( d )", !d.isZERO() );
201
202        //System.out.println("a = " + a);
203        //System.out.println("b = " + b);
204        e = d.subtract(c);
205        assertTrue("isZERO( a*b-b*a ) " + e, e.isZERO());
206
207        assertTrue("a*b = b*a", c.equals(d));
208        assertEquals("a*b = b*a", c, d);
209
210        c = efac.random(kl, ll, el, q);
211        //System.out.println("c = " + c);
212        d = a.multiply(b.multiply(c));
213        e = (a.multiply(b)).multiply(c);
214
215        //System.out.println("d = " + d);
216        //System.out.println("e = " + e);
217
218        //System.out.println("d-e = " + d.subtract(c) );
219
220        assertEquals("a(bc) = (ab)c", d, e);
221        assertTrue("a(bc) = (ab)c", d.equals(e));
222
223        c = a.multiply(efac.getONE());
224        d = efac.getONE().multiply(a);
225        assertEquals("a*1 = 1*a", c, d);
226
227        if (a.isUnit()) {
228            c = a.inverse();
229            d = c.multiply(a);
230            //System.out.println("a = " + a);
231            //System.out.println("c = " + c);
232            //System.out.println("d = " + d);
233            assertTrue("a*1/a = 1", d.isONE());
234        }
235    }
236
237
238    /**
239     * Test addition with syzygy gcd and euclids gcd.
240     */
241    public void xtestAdditionGcd() {
242        long te, tz;
243
244        a = efac.random(kl, ll, el, q);
245        b = efac.random(kl, ll, el, q);
246        //System.out.println("a = " + a);
247        //System.out.println("b = " + b);
248
249        az = new Quotient<BigRational>(zFac, a.num, a.den, true);
250        bz = new Quotient<BigRational>(zFac, b.num, b.den, true);
251
252        te = System.currentTimeMillis();
253        c = a.sum(b);
254        d = c.subtract(b);
255        d = d.monic();
256        te = System.currentTimeMillis() - te;
257        assertEquals("a+b-b = a", a, d);
258
259        tz = System.currentTimeMillis();
260        cz = az.sum(bz);
261        dz = cz.subtract(bz);
262        dz = dz.monic();
263        tz = System.currentTimeMillis() - tz;
264        assertEquals("a+b-b = a", az, dz);
265
266        System.out.println("te = " + te);
267        System.out.println("tz = " + tz);
268
269        c = a.sum(b);
270        d = b.sum(a);
271        //System.out.println("c = " + c);
272        //System.out.println("d = " + d);
273        assertEquals("a+b = b+a", c, d);
274
275        c = efac.random(kl, ll, el, q);
276        cz = new Quotient<BigRational>(zFac, c.num, c.den, true);
277
278
279        te = System.currentTimeMillis();
280        d = c.sum(a.sum(b));
281        e = c.sum(a).sum(b);
282        te = System.currentTimeMillis() - te;
283        assertEquals("c+(a+b) = (c+a)+b", d, e);
284
285        tz = System.currentTimeMillis();
286        dz = cz.sum(az.sum(bz));
287        ez = cz.sum(az).sum(bz);
288        tz = System.currentTimeMillis() - tz;
289        assertEquals("c+(a+b) = (c+a)+b", dz, ez);
290
291        System.out.println("te = " + te);
292        System.out.println("tz = " + tz);
293
294        c = a.sum(efac.getZERO());
295        d = a.subtract(efac.getZERO());
296        assertEquals("a+0 = a-0", c, d);
297
298        c = efac.getZERO().sum(a);
299        d = efac.getZERO().subtract(a.negate());
300        assertEquals("0+a = 0+(-a)", c, d);
301    }
302
303
304    /**
305     * Test parse().
306     */
307    public void testParse() {
308        a = efac.random(kl * 2, ll * 2, el * 2, q * 2);
309        //assertTrue("not isZERO( a )", !a.isZERO() );
310
311        //PrettyPrint.setInternal();
312        //System.out.println("a = " + a);
313        PrettyPrint.setPretty();
314        //System.out.println("a = " + a);
315        String p = a.toString();
316        //System.out.println("p = " + p);
317        b = efac.parse(p);
318        //System.out.println("b = " + b);
319
320        //c = a.subtract(b);
321        //System.out.println("c = " + c);
322        assertEquals("parse(a.toSting()) = a", a, b);
323    }
324
325
326    /**
327     * Test factorization.
328     */
329    public void testFactors() {
330        a = efac.random(kl, ll, el, q);
331        b = efac.random(kl, ll, el, q);
332        b = b.multiply(b);
333        //System.out.println("a = " + a);
334        //System.out.println("b = " + b);
335
336        c = a.multiply(b);
337        //System.out.println("c = " + c);
338
339        SortedMap<Quotient<BigRational>, Long> factors = PolyUfdUtil.<BigRational> factors(c);
340        //System.out.println("factors = " + factors);
341
342        boolean t = PolyUfdUtil.<BigRational> isFactorization(c, factors);
343        //System.out.println("t = " + t);
344        assertTrue("c == prod(factors): " + c + ", " + factors, t);
345    }
346
347}