001    
002    /*
003     * $Id: ComplexTest.java 3364 2010-10-24 12:56:06Z kredel $
004     */
005    
006    package edu.jas.poly;
007    
008    //import java.util.ArrayList;
009    //import java.util.List;
010    
011    import junit.framework.Test;
012    import junit.framework.TestCase;
013    import junit.framework.TestSuite;
014    
015    import org.apache.log4j.BasicConfigurator;
016    //import org.apache.log4j.Logger;
017    
018    import edu.jas.arith.BigRational;
019    import edu.jas.arith.BigInteger;
020    
021    //import edu.jas.structure.RingElem;
022    
023    
024    
025    /**
026     * Complex test with JUnit. 
027     * @author Heinz Kredel.
028     */
029    
030    public class ComplexTest extends TestCase {
031    
032    /**
033     * main.
034     */
035       public static void main (String[] args) {
036           BasicConfigurator.configure();
037           junit.textui.TestRunner.run( suite() );
038       }
039    
040    /**
041     * Constructs a <CODE>ComplexTest</CODE> object.
042     * @param name String.
043     */
044       public ComplexTest(String name) {
045              super(name);
046       }
047    
048    /**
049     * suite.
050     */ 
051     public static Test suite() {
052         TestSuite suite= new TestSuite(ComplexTest.class);
053         return suite;
054       }
055    
056       ComplexRing<BigInteger> fac;
057       GenPolynomialRing<BigRational> pfac;
058       ComplexRing< GenPolynomial<BigRational> > mfac;
059    
060       Complex< BigInteger > a;
061       Complex< BigInteger > b;
062       Complex< BigInteger > c;
063       Complex< BigInteger > d;
064       Complex< BigInteger > e;
065    
066       Complex< GenPolynomial<BigRational> > ap;
067       Complex< GenPolynomial<BigRational> > bp;
068       Complex< GenPolynomial<BigRational> > cp;
069       Complex< GenPolynomial<BigRational> > dp;
070       Complex< GenPolynomial<BigRational> > ep;
071    
072    
073       int rl = 1; 
074       int kl = 13;
075       int ll = 7;
076       int el = 3;
077       float q = 0.4f;
078       int il = 2; 
079       long p = 1152921504606846883L; // 2^60-93; 
080    
081       @Override
082       protected void setUp() {
083           a = b = c = d = e = null;
084           ap = bp = cp = dp = ep = null;
085           BigInteger cfac = new BigInteger(1);
086           fac = new ComplexRing<BigInteger>( cfac );
087           pfac = new GenPolynomialRing<BigRational>( new BigRational(1), 1 );
088           GenPolynomial<BigRational> mo = pfac.random(kl,ll,el,q);
089           while ( mo.isConstant() ) {
090                 mo = pfac.random(kl,ll,el,q);
091           }
092           mfac = new ComplexRing<GenPolynomial<BigRational>>( pfac );
093       }
094    
095       @Override
096       protected void tearDown() {
097           a = b = c = d = e = null;
098           ap = bp = cp = dp = ep = null;
099           fac = null;
100           pfac = null;
101           mfac = null;
102       }
103    
104    
105    /**
106     * Test constructor for integer.
107     * 
108     */
109     public void testIntConstruction() {
110         c = fac.getONE();
111         //System.out.println("c = " + c);
112         assertTrue("isZERO( c )", !c.isZERO() );
113         assertTrue("isONE( c )", c.isONE() );
114    
115         d = fac.getZERO();
116         //System.out.println("d = " + d);
117         assertTrue("isZERO( d )", d.isZERO() );
118         assertTrue("isONE( d )", !d.isONE() );
119     }
120    
121    
122    /**
123     * Test constructor for polynomial.
124     * 
125     */
126     public void testPolyConstruction() {
127         cp = mfac.getONE();
128         assertTrue("isZERO( cp )", !cp.isZERO() );
129         assertTrue("isONE( cp )", cp.isONE() );
130    
131         dp = mfac.getZERO();
132         assertTrue("isZERO( dp )", dp.isZERO() );
133         assertTrue("isONE( dp )", !dp.isONE() );
134     }
135    
136    
137    /**
138     * Test random integer.
139     * 
140     */
141     public void testIntRandom() {
142         for (int i = 0; i < 7; i++) {
143             a = fac.random(kl*(i+1));
144            if ( a.isZERO() ) {
145                continue;
146             }
147             //a = fac.random(kl*(i+1), ll+2*i, el+i, q );
148             //System.out.println("a = " + a);
149             assertTrue(" not isZERO( a"+i+" )", !a.isZERO() );
150             assertTrue(" not isONE( a"+i+" )", !a.isONE() );
151         }
152     }
153    
154    
155    /**
156     * Test random polynomial.
157     * 
158     */
159     public void testPolyRandom() {
160         for (int i = 0; i < 7; i++) {
161             ap = mfac.random(kl+i);
162             if ( ap.isZERO() ) {
163                continue;
164             }
165             assertTrue(" not isZERO( ap"+i+" )", !ap.isZERO() );
166             assertTrue(" not isONE( ap"+i+" )", !ap.isONE() );
167         }
168     }
169    
170    
171    /**
172     * Test integer addition.
173     * 
174     */
175     public void testIntAddition() {
176    
177         a = fac.random(kl);
178         b = fac.random(kl);
179    
180         c = a.sum(b);
181         d = c.subtract(b);
182         assertEquals("a+b-b = a",a,d);
183    
184         c = a.sum(b);
185         d = b.sum(a);
186         assertEquals("a+b = b+a",c,d);
187    
188         c = fac.random(kl);
189         d = c.sum( a.sum(b) );
190         e = c.sum( a ).sum(b);
191         assertEquals("c+(a+b) = (c+a)+b",d,e);
192    
193    
194         c = a.sum( fac.getZERO() );
195         d = a.subtract( fac.getZERO() );
196         assertEquals("a+0 = a-0",c,d);
197    
198         c = fac.getZERO().sum( a );
199         d = fac.getZERO().subtract( a.negate() );
200         assertEquals("0+a = 0+(-a)",c,d);
201     }
202    
203    
204    /**
205     * Test polynomial addition.
206     * 
207     */
208     public void testPolyAddition() {
209    
210         ap = mfac.random(kl);
211         bp = mfac.random(kl);
212         //System.out.println("a = " + a);
213         //System.out.println("b = " + b);
214    
215         cp = ap.sum(bp);
216         dp = cp.subtract(bp);
217         assertEquals("a+b-b = a",ap,dp);
218    
219         cp = ap.sum(bp);
220         dp = bp.sum(ap);
221         //System.out.println("c = " + c);
222         //System.out.println("d = " + d);
223    
224         assertEquals("a+b = b+a",cp,dp);
225    
226         cp = mfac.random(kl);
227         dp = cp.sum( ap.sum(bp) );
228         ep = cp.sum( ap ).sum(bp);
229         assertEquals("c+(a+b) = (c+a)+b",dp,ep);
230    
231    
232         cp = ap.sum( mfac.getZERO() );
233         dp = ap.subtract( mfac.getZERO() );
234         assertEquals("a+0 = a-0",cp,dp);
235    
236         cp = mfac.getZERO().sum( ap );
237         dp = mfac.getZERO().subtract( ap.negate() );
238         assertEquals("0+a = 0+(-a)",cp,dp);
239    }
240    
241    
242    /**
243     * Test integer multiplication.
244     * 
245     */
246     public void testIntMultiplication() {
247    
248         a = fac.random(kl);
249         if ( a.isZERO() ) {
250            return;
251         }
252         assertTrue("not isZERO( a )", !a.isZERO() );
253    
254         b = fac.random(kl);
255         if ( b.isZERO() ) {
256            return;
257         }
258         assertTrue("not isZERO( b )", !b.isZERO() );
259    
260         c = b.multiply(a);
261         d = a.multiply(b);
262         assertTrue("not isZERO( c )", !c.isZERO() );
263         assertTrue("not isZERO( d )", !d.isZERO() );
264    
265         //System.out.println("a = " + a);
266         //System.out.println("b = " + b);
267         e = d.subtract(c);
268         assertTrue("isZERO( a*b-b*a ) " + e, e.isZERO() );
269    
270         assertTrue("a*b = b*a", c.equals(d) );
271         assertEquals("a*b = b*a",c,d);
272    
273         c = fac.random(kl);
274         //System.out.println("c = " + c);
275         d = a.multiply( b.multiply(c) );
276         e = (a.multiply(b)).multiply(c);
277    
278         //System.out.println("d = " + d);
279         //System.out.println("e = " + e);
280    
281         //System.out.println("d-e = " + d.subtract(c) );
282    
283         assertEquals("a(bc) = (ab)c",d,e);
284         assertTrue("a(bc) = (ab)c", d.equals(e) );
285    
286         c = a.multiply( fac.getONE() );
287         d = fac.getONE().multiply( a );
288         assertEquals("a*1 = 1*a",c,d);
289    
290         if ( a.isUnit() ) {
291            c = a.inverse();
292            d = c.multiply(a);
293            //System.out.println("a = " + a);
294            //System.out.println("c = " + c);
295            //System.out.println("d = " + d);
296            assertTrue("a*1/a = 1",d.isONE()); 
297         }
298     }
299    
300    
301    /**
302     * Test polynomial multiplication.
303     * 
304     */
305     public void testPolyMultiplication() {
306    
307         ap = mfac.random(kl);
308         if ( ap.isZERO() ) {
309            return;
310         }
311         assertTrue("not isZERO( a )", !ap.isZERO() );
312    
313         bp = mfac.random(kl);
314         if ( bp.isZERO() ) {
315            return;
316         }
317         assertTrue("not isZERO( b )", !bp.isZERO() );
318    
319         cp = bp.multiply(ap);
320         dp = ap.multiply(bp);
321         assertTrue("not isZERO( c )", !cp.isZERO() );
322         assertTrue("not isZERO( d )", !dp.isZERO() );
323    
324         //System.out.println("a = " + a);
325         //System.out.println("b = " + b);
326         ep = dp.subtract(cp);
327         assertTrue("isZERO( a*b-b*a ) " + ep, ep.isZERO() );
328    
329         assertTrue("a*b = b*a", cp.equals(dp) );
330         assertEquals("a*b = b*a",cp,dp);
331    
332         cp = mfac.random(kl);
333         //System.out.println("c = " + c);
334         dp = ap.multiply( bp.multiply(cp) );
335         ep = (ap.multiply(bp)).multiply(cp);
336    
337         //System.out.println("d = " + d);
338         //System.out.println("e = " + e);
339    
340         //System.out.println("d-e = " + d.subtract(c) );
341    
342         assertEquals("a(bc) = (ab)c",dp,ep);
343         assertTrue("a(bc) = (ab)c", dp.equals(ep) );
344    
345         cp = ap.multiply( mfac.getONE() );
346         dp = mfac.getONE().multiply( ap );
347         assertEquals("a*1 = 1*a",cp,dp);
348    
349         if ( ap.isUnit() ) {
350            cp = ap.inverse();
351            dp = cp.multiply(ap);
352            //System.out.println("a = " + a);
353            //System.out.println("c = " + c);
354            //System.out.println("d = " + d);
355            assertTrue("a*1/a = 1",dp.isONE()); 
356         }
357     }
358    
359    
360    /**
361     * Test integer division.
362     * 
363     */
364     public void testIntDivision() {
365    
366         a = fac.random(kl*2);
367         if ( a.isZERO() ) {
368            return;
369         }
370         assertTrue("not isZERO( a )", !a.isZERO() );
371    
372         b = fac.random(kl);
373         if ( b.isZERO() ) {
374            return;
375         }
376         assertTrue("not isZERO( b )", !b.isZERO() );
377         //System.out.println("a = " + a);
378         //System.out.println("b = " + b);
379    
380         c = a.divide(b);
381         //System.out.println("c = " + c);
382         d = a.remainder(b);
383         //System.out.println("d = " + d);
384    
385         e = b.multiply(c).sum(d);
386         //System.out.println("e = " + e);
387         assertEquals("a = b (a/b) + a%b ", a, e );
388    
389         c = a.gcd(b);
390         d = a.divide(c);
391         e = b.divide(c);
392         //System.out.println("c = " + c);
393         //System.out.println("d = " + d);
394         //System.out.println("e = " + e);
395    
396         d = c.multiply(d);
397         e = c.multiply(e);
398         //System.out.println("d = " + d);
399         //System.out.println("e = " + e);
400    
401         assertEquals("a/gcd(a,b)*gcd(a,b) = a ", a, d );
402         assertEquals("b/gcd(a,b)*gcd(a,b) = b ", b, e );
403    
404         Complex<BigInteger>[] gf = a.egcd(b);
405         c = gf[0];
406         d = gf[1];
407         e = gf[2];
408         //System.out.println("c = " + c);
409         //System.out.println("d = " + d);
410         //System.out.println("e = " + e);
411         d = d.multiply(a);
412         e = e.multiply(b);
413         d = d.sum( e );
414         //System.out.println("d = " + d);
415         assertEquals("d*a + e*b = c = gcd(a,b)", c, d );
416     }
417    
418    
419    /**
420     * Test polynomial division.
421     * 
422     */
423     public void testPolyDivision() {
424    
425         ap = mfac.random(kl);
426         if ( ap.isZERO() ) {
427            return;
428         }
429         assertTrue("not isZERO( ap )", !ap.isZERO() );
430    
431         bp = mfac.random(kl/2);
432         if ( bp.isZERO() ) {
433            return;
434         }
435         assertTrue("not isZERO( bp )", !bp.isZERO() );
436         //System.out.println("ap = " + ap);
437         //System.out.println("bp = " + bp);
438    
439         cp = ap.divide(bp);
440         //System.out.println("cp = " + cp);
441         dp = ap.remainder(bp);
442         //System.out.println("dp = " + dp);
443    
444         ep = bp.multiply(cp).sum(dp);
445         //System.out.println("ep = " + ep);
446         assertEquals("ap = bp (ap/bp) + ap%bp ", ap, ep );
447    
448         // not applicable:
449    //      cp = ap.gcd(bp);
450    //      dp = ap.divide(cp);
451    //      ep = bp.divide(cp);
452    //      System.out.println("cp = " + cp);
453    //      System.out.println("dp = " + dp);
454    //      System.out.println("ep = " + ep);
455    
456    //      dp = cp.multiply(dp);
457    //      ep = cp.multiply(ep);
458    //      System.out.println("dp = " + dp);
459    //      System.out.println("ep = " + ep);
460    
461    //      assertEquals("ap/gcd(ap,bp)*gcd(ap,bp) = ap ", ap, dp );
462    //      assertEquals("bp/gcd(ap,bp)*gcd(ap,bp) = bp ", bp, ep );
463     }
464    
465    }