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