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