001/*
002 * $Id$
003 */
004
005package edu.jas.poly;
006
007import junit.framework.Test;
008import junit.framework.TestCase;
009import junit.framework.TestSuite;
010
011import edu.jas.poly.GenPolynomial;
012import edu.jas.arith.BigComplex;
013import edu.jas.arith.Roots;
014
015
016/**
017 * Complex coefficients GenPolynomial tests with JUnit.
018 * @author Heinz Kredel
019 */
020
021public 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    GenPolynomialRing<BigComplex> fac;
047
048    GenPolynomial<BigComplex> a, b, c, d, e;
049
050    int rl = 7; 
051    int kl = 10;
052    int ll = 10;
053    int el = 5;
054    float q = 0.5f;
055
056    protected void setUp() {
057        a = b = c = d = e = null;
058        fac = new GenPolynomialRing<BigComplex>(new BigComplex(1),rl);
059    }
060
061    protected void tearDown() {
062        a = b = c = d = e = null;
063        fac = null;
064    }
065
066
067    /**
068     * Test constructor and toString.
069     */
070    public void testConstruction() {
071        c = fac.getONE();
072        assertTrue("length( c ) = 1", c.length() == 1);
073        assertTrue("isZERO( c )", !c.isZERO() );
074        assertTrue("isONE( c )", c.isONE() );
075
076        d = fac.getZERO();
077        assertTrue("length( d ) = 0", d.length() == 0);
078        assertTrue("isZERO( d )", d.isZERO() );
079        assertTrue("isONE( d )", !d.isONE() );
080    }
081
082
083    /**
084     * Test random polynomial.
085     */
086    public void testRandom() {
087        for (int i = 0; i < 7; i++) {
088            a = fac.random(ll);
089            // fac.random(kl*(i+1), ll+2*i, el+i, q );
090            assertTrue("length( a"+i+" ) <> 0", a.length() >= 0);
091            assertTrue(" not isZERO( a"+i+" )", !a.isZERO() );
092            assertTrue(" not isONE( a"+i+" )", !a.isONE() );
093        }
094    }
095
096
097    /**
098     * Test addition.
099     */
100    public void testAddition() {
101        a = fac.random(ll);
102        b = fac.random(ll);
103
104        c = a.sum(b);
105        d = c.subtract(b);
106        assertEquals("a+b-b = a",a,d);
107
108        c = fac.random(ll);
109
110        ExpVector u = ExpVector.random(rl,el,q);
111        BigComplex x = BigComplex.CRAND(kl);
112
113        b = new GenPolynomial<BigComplex>(fac,x, u);
114        c = a.sum(b);
115        d = a.sum(x,u);
116        assertEquals("a+p(x,u) = a+(x,u)",c,d);
117
118        c = a.subtract(b);
119        d = a.subtract(x,u);
120        assertEquals("a-p(x,u) = a-(x,u)",c,d);
121
122        a = new GenPolynomial<BigComplex>(fac);
123        b = new GenPolynomial<BigComplex>(fac,x, u);
124        c = b.sum(a);
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
133
134    /**
135     * Test object multiplication.
136     */
137    public void testMultiplication() {
138        a = fac.random(ll);
139        assertTrue("not isZERO( a )", !a.isZERO() );
140
141        b = fac.random(ll);
142        assertTrue("not isZERO( b )", !b.isZERO() );
143
144        c = b.multiply(a);
145        d = a.multiply(b);
146        assertTrue("not isZERO( c )", !c.isZERO() );
147        assertTrue("not isZERO( d )", !d.isZERO() );
148
149        //System.out.println("a = " + a);
150        //System.out.println("b = " + b);
151        e = d.subtract(c);
152        assertTrue("isZERO( a*b-b*a ) " + e, e.isZERO() );
153
154        assertTrue("a*b = b*a", c.equals(d) );
155        assertEquals("a*b = b*a",c,d);
156
157        c = fac.random(ll);
158        //System.out.println("c = " + c);
159        d = a.multiply( b.multiply(c) );
160        e = (a.multiply(b)).multiply(c);
161
162        //System.out.println("d = " + d);
163        //System.out.println("e = " + e);
164
165        //System.out.println("d-e = " + d.subtract(c) );
166
167        assertEquals("a(bc) = (ab)c",d,e);
168        assertTrue("a(bc) = (ab)c", d.equals(e) );
169
170        BigComplex x = a.leadingBaseCoefficient().inverse();
171        c = a.monic();
172        d = a.multiply(x);
173        assertEquals("a.monic() = a(1/ldcf(a))",c,d);
174
175        BigComplex y = b.leadingBaseCoefficient().inverse();
176        c = b.monic();
177        d = b.multiply(y);
178        assertEquals("b.monic() = b(1/ldcf(b))",c,d);
179
180        e = new GenPolynomial<BigComplex>(fac,y);
181        d = b.multiply(e);
182        assertEquals("b.monic() = b(1/ldcf(b))",c,d);
183
184        d = e.multiply(b);
185        assertEquals("b.monic() = (1/ldcf(b) (0))*b",c,d);
186    }
187
188    
189    /**
190     * Test absolute norm.
191     */
192    public void testAbsNorm() {
193        BigComplex r;
194        a = fac.getONE().negate();
195        //System.out.println("a = " + a);
196
197        r = PolyUtil.<BigComplex> absNorm(a);
198        //System.out.println("r = " + r);
199        assertTrue("isONE( absNorm(-1) )", r.isONE() );
200
201        a = fac.random(kl*2, ll+2, el, q );
202        //System.out.println("a = " + a);
203
204        r = PolyUtil.<BigComplex> absNorm(a);
205        //System.out.println("r = " + r);
206        assertTrue(" not isZERO( absNorm(a) )", !r.isZERO() || a.isZERO() );
207    }
208
209}