001    /*
002     * $Id: ComplexGenPolynomialTest.java 1888 2008-07-12 13:37:34Z 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.poly.GenPolynomial;
012    import edu.jas.arith.BigComplex;
013    //import edu.jas.structure.RingElem;
014    
015    
016    /**
017     * Complex coefficients GenPolynomial tests with JUnit.
018     * @author Heinz Kredel.
019     */
020    
021    public class ComplexGenPolynomialTest extends TestCase {
022    
023    /**
024     * main.
025     */
026       public static void main (String[] args) {
027              junit.textui.TestRunner.run( suite() );
028       }
029    
030    /**
031     * Constructs a <CODE>ComplexGenPolynomialTest</CODE> object.
032     * @param name String.
033     */
034       public ComplexGenPolynomialTest(String name) {
035              super(name);
036       }
037    
038    /**
039     * suite.
040     */ 
041     public static Test suite() {
042         TestSuite suite= new TestSuite(ComplexGenPolynomialTest.class);
043         return suite;
044       }
045    
046       //private final static int bitlen = 100;
047    
048       GenPolynomialRing<BigComplex> fac;
049    
050       GenPolynomial<BigComplex> a;
051       GenPolynomial<BigComplex> b;
052       GenPolynomial<BigComplex> c;
053       GenPolynomial<BigComplex> d;
054       GenPolynomial<BigComplex> e;
055    
056       int rl = 7; 
057       int kl = 10;
058       int ll = 10;
059       int el = 5;
060       float q = 0.5f;
061    
062       protected void setUp() {
063           a = b = c = d = e = null;
064           fac = new GenPolynomialRing<BigComplex>(new BigComplex(1),rl);
065       }
066    
067       protected void tearDown() {
068           a = b = c = d = e = null;
069           fac = null;
070       }
071    
072    
073    /**
074     * Test constructor and toString.
075     * 
076     */
077     public void testConstruction() {
078         c = fac.getONE();
079         assertTrue("length( c ) = 1", c.length() == 1);
080         assertTrue("isZERO( c )", !c.isZERO() );
081         assertTrue("isONE( c )", c.isONE() );
082    
083         d = fac.getZERO();
084         assertTrue("length( d ) = 0", d.length() == 0);
085         assertTrue("isZERO( d )", d.isZERO() );
086         assertTrue("isONE( d )", !d.isONE() );
087     }
088    
089    
090    /**
091     * Test random polynomial.
092     * 
093     */
094     public void testRandom() {
095         for (int i = 0; i < 7; i++) {
096             a = fac.random(ll);
097                 // fac.random(rl+i, kl*(i+1), ll+2*i, el+i, q );
098             assertTrue("length( a"+i+" ) <> 0", a.length() >= 0);
099             assertTrue(" not isZERO( a"+i+" )", !a.isZERO() );
100             assertTrue(" not isONE( a"+i+" )", !a.isONE() );
101         }
102     }
103    
104    
105    /**
106     * Test addition.
107     * 
108     */
109     public void testAddition() {
110    
111         a = fac.random(ll);
112         b = fac.random(ll);
113    
114         c = a.sum(b);
115         d = c.subtract(b);
116         assertEquals("a+b-b = a",a,d);
117    
118         c = fac.random(ll);
119    
120         ExpVector u = ExpVector.EVRAND(rl,el,q);
121         BigComplex x = BigComplex.CRAND(kl);
122    
123         b = new GenPolynomial<BigComplex>(fac,x, u);
124         c = a.sum(b);
125         d = a.sum(x,u);
126         assertEquals("a+p(x,u) = a+(x,u)",c,d);
127    
128         c = a.subtract(b);
129         d = a.subtract(x,u);
130         assertEquals("a-p(x,u) = a-(x,u)",c,d);
131    
132         a = new GenPolynomial<BigComplex>(fac);
133         b = new GenPolynomial<BigComplex>(fac,x, u);
134         c = b.sum(a);
135         d = a.sum(x,u);
136         assertEquals("a+p(x,u) = a+(x,u)",c,d);
137    
138         c = a.subtract(b);
139         d = a.subtract(x,u);
140         assertEquals("a-p(x,u) = a-(x,u)",c,d);
141     }
142    
143    
144    /**
145     * Test object multiplication.
146     * 
147     */
148     public void testMultiplication() {
149    
150         a = fac.random(ll);
151         assertTrue("not isZERO( a )", !a.isZERO() );
152    
153         b = fac.random(ll);
154         assertTrue("not isZERO( b )", !b.isZERO() );
155    
156         c = b.multiply(a);
157         d = a.multiply(b);
158         assertTrue("not isZERO( c )", !c.isZERO() );
159         assertTrue("not isZERO( d )", !d.isZERO() );
160    
161         //System.out.println("a = " + a);
162         //System.out.println("b = " + b);
163         e = d.subtract(c);
164         assertTrue("isZERO( a*b-b*a ) " + e, e.isZERO() );
165    
166         assertTrue("a*b = b*a", c.equals(d) );
167         assertEquals("a*b = b*a",c,d);
168    
169         c = fac.random(ll);
170         //System.out.println("c = " + c);
171         d = a.multiply( b.multiply(c) );
172         e = (a.multiply(b)).multiply(c);
173    
174         //System.out.println("d = " + d);
175         //System.out.println("e = " + e);
176    
177         //System.out.println("d-e = " + d.subtract(c) );
178    
179         assertEquals("a(bc) = (ab)c",d,e);
180         assertTrue("a(bc) = (ab)c", d.equals(e) );
181    
182         BigComplex x = a.leadingBaseCoefficient().inverse();
183         c = a.monic();
184         d = a.multiply(x);
185         assertEquals("a.monic() = a(1/ldcf(a))",c,d);
186    
187         BigComplex y = b.leadingBaseCoefficient().inverse();
188         c = b.monic();
189         d = b.multiply(y);
190         assertEquals("b.monic() = b(1/ldcf(b))",c,d);
191    
192         e = new GenPolynomial<BigComplex>(fac,y);
193         d = b.multiply(e);
194         assertEquals("b.monic() = b(1/ldcf(b))",c,d);
195    
196         d = e.multiply(b);
197         assertEquals("b.monic() = (1/ldcf(b) (0))*b",c,d);
198     }
199    
200    }