001/*
002 * $Id$
003 */
004
005package edu.jas.poly;
006
007
008import java.util.List;
009
010import edu.jas.arith.BigInteger;
011
012import junit.framework.Test;
013import junit.framework.TestCase;
014import junit.framework.TestSuite;
015
016
017/**
018 * Quotient tests with JUnit.
019 * @author Heinz Kredel
020 */
021public class QuotientTest extends TestCase {
022
023
024    /**
025     * main.
026     */
027    public static void main(String[] args) {
028        junit.textui.TestRunner.run(suite());
029    }
030
031
032    /**
033     * Constructs a <CODE>QuotientTest</CODE> object.
034     * @param name String.
035     */
036    public QuotientTest(String name) {
037        super(name);
038    }
039
040
041    /**
042     * suite.
043     */
044    public static Test suite() {
045        TestSuite suite = new TestSuite(QuotientTest.class);
046        return suite;
047    }
048
049
050    QuotientRing<BigInteger> fac;
051
052
053    GenPolynomialRing<BigInteger> pfac;
054
055
056    QuotientRing<GenPolynomial<BigInteger>> mfac;
057
058
059    Quotient<BigInteger> a, b, c, d, e;
060
061
062    Quotient<GenPolynomial<BigInteger>> ap, bp, cp, dp, ep;
063
064
065    int rl = 2;
066
067
068    int kl = 17;
069
070
071    int ll = 3; //6;
072
073
074    int el = 2;
075
076
077    float q = 0.4f;
078
079    @Override
080    protected void setUp() {
081        a = b = c = d = e = null;
082        ap = bp = cp = dp = ep = null;
083        fac = new QuotientRing<BigInteger>(new BigInteger(1));
084        pfac = new GenPolynomialRing<BigInteger>(new BigInteger(1), 1);
085        mfac = new QuotientRing<GenPolynomial<BigInteger>>(pfac);
086    }
087
088
089    @Override
090    protected void tearDown() {
091        a = b = c = d = e = null;
092        ap = bp = cp = dp = ep = null;
093        fac = null;
094        pfac = null;
095        mfac = null;
096    }
097
098
099    /**
100     * Test factory for integer.
101     */
102    public void testIntRing() {
103        assertFalse("#ring infinite", fac.isFinite());
104        assertTrue("associative ring", fac.isAssociative());
105        assertTrue("commutative ring", fac.isCommutative());
106        assertTrue("characteristic p", fac.characteristic().signum() == 0);
107        assertTrue("no field", fac.isField());
108    }
109
110
111    /**
112     * Test factory for polynomial.
113     */
114    public void testPolyRing() {
115        assertFalse("#ring infinite", mfac.isFinite());
116        assertTrue("associative ring", mfac.isAssociative());
117        assertTrue("commutative ring", mfac.isCommutative());
118        assertTrue("characteristic zero", mfac.characteristic().signum() == 0);
119        assertTrue("no field", mfac.isField());
120    }
121
122
123    /**
124     * Test constructor for integer.
125     */
126    public void testIntConstruction() {
127        c = fac.getONE();
128        //System.out.println("c = " + c);
129        assertTrue("isZERO( c )", !c.isZERO());
130        assertTrue("isONE( c )", c.isONE());
131
132        d = fac.getZERO();
133        //System.out.println("d = " + d);
134        assertTrue("isZERO( d )", d.isZERO());
135        assertTrue("isONE( d )", !d.isONE());
136
137        List<Quotient<BigInteger>> gens = fac.generators();
138        //System.out.println("gens = " + gens);
139        assertTrue("#gens == 1: ", gens.size() == 1);
140        for (Quotient<BigInteger> v : gens) {
141            a = fac.parse(v.toString());
142            assertEquals("a == v", a, v);
143        }
144    }
145
146
147    /**
148     * Test constructor for polynomial.
149     */
150    public void testPolyConstruction() {
151        cp = mfac.getONE();
152        assertTrue("isZERO( cp )", !cp.isZERO());
153        assertTrue("isONE( cp )", cp.isONE());
154
155        dp = mfac.getZERO();
156        assertTrue("isZERO( dp )", dp.isZERO());
157        assertTrue("isONE( dp )", !dp.isONE());
158
159        List<Quotient<GenPolynomial<BigInteger>>> gens = mfac.generators();
160        //System.out.println("gens = " + gens);
161        assertTrue("#gens == 3: ", gens.size() == 3);
162        for (Quotient<GenPolynomial<BigInteger>> v : gens) {
163            ap = mfac.parse(v.toString());
164            assertEquals("ap == v", ap, v);
165        }
166    }
167
168
169    /**
170     * Test random integer.
171     */
172    public void testIntRandom() {
173        for (int i = 0; i < 7; i++) {
174            a = fac.random(kl * (i + 1));
175            //a = fac.random(kl*(i+1), ll+2+2*i, el, q );
176            //System.out.println("a = " + a);
177            assertTrue(" not isZERO( a" + i + " )", !a.isZERO());
178            assertTrue(" not isONE( a" + i + " )", !a.isONE());
179        }
180    }
181
182
183    /**
184     * Test random polynomial.
185     */
186    public void testPolyRandom() {
187        for (int i = 0; i < 7; i++) {
188            ap = mfac.random(kl + i);
189            assertTrue(" not isZERO( ap" + i + " )", !ap.isZERO());
190            assertTrue(" not isONE( ap" + i + " )", !ap.isONE());
191        }
192    }
193
194
195    /**
196     * Test integer addition.
197     */
198    public void testIntAddition() {
199        a = fac.random(kl);
200        b = fac.random(kl);
201        //System.out.println("a = " + a);
202        //System.out.println("b = " + b);
203
204        c = a.sum(b);
205        d = c.subtract(b);
206        assertEquals("a+b-b = a", a, d);
207
208        c = a.sum(b);
209        d = b.sum(a);
210        //System.out.println("c = " + c);
211        //System.out.println("d = " + d);
212
213        assertEquals("a+b = b+a", c, d);
214
215        c = fac.random(kl);
216        d = c.sum(a.sum(b));
217        e = c.sum(a).sum(b);
218        assertEquals("c+(a+b) = (c+a)+b", d, e);
219
220
221        c = a.sum(fac.getZERO());
222        d = a.subtract(fac.getZERO());
223        assertEquals("a+0 = a-0", c, d);
224
225        c = fac.getZERO().sum(a);
226        d = fac.getZERO().subtract(a.negate());
227        assertEquals("0+a = 0+(-a)", c, d);
228    }
229
230
231    /**
232     * Test polynomial addition.
233     */
234    public void testPolyAddition() {
235        ap = mfac.random(kl);
236        bp = mfac.random(kl);
237        //System.out.println("a = " + a);
238        //System.out.println("b = " + b);
239
240        cp = ap.sum(bp);
241        dp = cp.subtract(bp);
242        assertEquals("a+b-b = a", ap, dp);
243
244        cp = ap.sum(bp);
245        dp = bp.sum(ap);
246        //System.out.println("c = " + c);
247        //System.out.println("d = " + d);
248
249        assertEquals("a+b = b+a", cp, dp);
250
251        cp = mfac.random(kl);
252        dp = cp.sum(ap.sum(bp));
253        ep = cp.sum(ap).sum(bp);
254        assertEquals("c+(a+b) = (c+a)+b", dp, ep);
255
256
257        cp = ap.sum(mfac.getZERO());
258        dp = ap.subtract(mfac.getZERO());
259        assertEquals("a+0 = a-0", cp, dp);
260
261        cp = mfac.getZERO().sum(ap);
262        dp = mfac.getZERO().subtract(ap.negate());
263        assertEquals("0+a = 0+(-a)", cp, dp);
264    }
265
266
267    /**
268     * Test integer multiplication.
269     */
270    public void testIntMultiplication() {
271        a = fac.random(kl);
272        assertTrue("not isZERO( a )", !a.isZERO());
273
274        b = fac.random(kl);
275        assertTrue("not isZERO( b )", !b.isZERO());
276
277        c = b.multiply(a);
278        d = a.multiply(b);
279        assertTrue("not isZERO( c )", !c.isZERO());
280        assertTrue("not isZERO( d )", !d.isZERO());
281
282        //System.out.println("a = " + a);
283        //System.out.println("b = " + b);
284        e = d.subtract(c);
285        assertTrue("isZERO( a*b-b*a ) " + e, e.isZERO());
286
287        assertTrue("a*b = b*a", c.equals(d));
288        assertEquals("a*b = b*a", c, d);
289
290        c = fac.random(kl);
291        //System.out.println("c = " + c);
292        d = a.multiply(b.multiply(c));
293        e = (a.multiply(b)).multiply(c);
294
295        //System.out.println("d = " + d);
296        //System.out.println("e = " + e);
297
298        //System.out.println("d-e = " + d.subtract(c) );
299
300        assertEquals("a(bc) = (ab)c", d, e);
301        assertTrue("a(bc) = (ab)c", d.equals(e));
302
303        c = a.multiply(fac.getONE());
304        d = fac.getONE().multiply(a);
305        assertEquals("a*1 = 1*a", c, d);
306
307        if (a.isUnit()) {
308            c = a.inverse();
309            d = c.multiply(a);
310            //System.out.println("a = " + a);
311            //System.out.println("c = " + c);
312            //System.out.println("d = " + d);
313            assertTrue("a*1/a = 1", d.isONE());
314        }
315
316        Quotient<BigInteger>[] qr;
317        c = b.multiply(a);
318        qr = c.quotientRemainder(a);
319        assertEquals("b*a / a == b", qr[0], b);
320        assertTrue("b*a rem a == 0", qr[1].isZERO());
321    }
322
323
324    /**
325     * Test polynomial multiplication.
326     */
327    public void testPolyMultiplication() {
328        ap = mfac.random(kl);
329        assertTrue("not isZERO( a )", !ap.isZERO());
330
331        bp = mfac.random(kl);
332        assertTrue("not isZERO( b )", !bp.isZERO());
333
334        cp = bp.multiply(ap);
335        dp = ap.multiply(bp);
336        assertTrue("not isZERO( c )", !cp.isZERO());
337        assertTrue("not isZERO( d )", !dp.isZERO());
338
339        //System.out.println("a = " + a);
340        //System.out.println("b = " + b);
341        ep = dp.subtract(cp);
342        assertTrue("isZERO( a*b-b*a ) " + ep, ep.isZERO());
343
344        assertTrue("a*b = b*a", cp.equals(dp));
345        assertEquals("a*b = b*a", cp, dp);
346
347        cp = mfac.random(kl);
348        //System.out.println("c = " + c);
349        dp = ap.multiply(bp.multiply(cp));
350        ep = (ap.multiply(bp)).multiply(cp);
351
352        //System.out.println("d = " + d);
353        //System.out.println("e = " + e);
354
355        //System.out.println("d-e = " + d.subtract(c) );
356
357        assertEquals("a(bc) = (ab)c", dp, ep);
358        assertTrue("a(bc) = (ab)c", dp.equals(ep));
359
360        cp = ap.multiply(mfac.getONE());
361        dp = mfac.getONE().multiply(ap);
362        assertEquals("a*1 = 1*a", cp, dp);
363
364        if (ap.isUnit()) {
365            cp = ap.inverse();
366            dp = cp.multiply(ap);
367            //System.out.println("a = " + a);
368            //System.out.println("c = " + c);
369            //System.out.println("d = " + d);
370            assertTrue("a*1/a = 1", dp.isONE());
371        }
372
373        Quotient<GenPolynomial<BigInteger>>[] qr;
374        cp = bp.multiply(ap);
375        qr = cp.quotientRemainder(ap);
376        assertEquals("b*a / a == b", qr[0], bp);
377        assertTrue("b*a rem a == 0", qr[1].isZERO());
378    }
379
380}