001/*
002 * $Id$
003 */
004
005package edu.jas.root;
006
007
008import junit.framework.Test;
009import junit.framework.TestCase;
010import junit.framework.TestSuite;
011
012
013import edu.jas.arith.BigRational;
014import edu.jas.poly.Complex;
015import edu.jas.poly.ComplexRing;
016import edu.jas.poly.GenPolynomial;
017import edu.jas.poly.GenPolynomialRing;
018import edu.jas.structure.NotInvertibleException;
019
020
021/**
022 * ComplexAlgebraicNumber Test using JUnit.
023 * @author Heinz Kredel
024 */
025
026public class ComplexAlgebraicTest extends TestCase {
027
028
029    /**
030     * main.
031     */
032    public static void main(String[] args) {
033        junit.textui.TestRunner.run(suite());
034    }
035
036
037    /**
038     * Constructs a <CODE>ComplexAlgebraicTest</CODE> object.
039     * @param name String.
040     */
041    public ComplexAlgebraicTest(String name) {
042        super(name);
043    }
044
045
046    /**
047     * suite.
048     */
049    public static Test suite() {
050        TestSuite suite = new TestSuite(ComplexAlgebraicTest.class);
051        return suite;
052    }
053
054
055    ComplexAlgebraicRing<BigRational> fac;
056
057
058    GenPolynomialRing<Complex<BigRational>> mfac;
059
060
061    ComplexAlgebraicNumber<BigRational> a, b, c, d, e;
062
063
064    ComplexAlgebraicNumber<BigRational> alpha;
065
066
067    int rl = 1;
068
069
070    int kl = 10;
071
072
073    int ll = 10;
074
075
076    int el = ll;
077
078
079    float q = 0.5f;
080
081
082    @Override
083    protected void setUp() {
084        a = b = c = d = e = null;
085        ComplexRing<BigRational> cfac = new ComplexRing<BigRational>(new BigRational(1));
086        Complex<BigRational> im = cfac.getIMAG();
087        BigRational rfac = new BigRational();
088        BigRational two = new BigRational(2);
089        Complex<BigRational> nw = new Complex<BigRational>(cfac, rfac.getZERO(), two);
090        Complex<BigRational> sw = new Complex<BigRational>(cfac, rfac.getZERO(), rfac.getZERO());
091        Complex<BigRational> se = new Complex<BigRational>(cfac, two, rfac.getZERO());
092        Complex<BigRational> ne = new Complex<BigRational>(cfac, two, two);
093        Rectangle<BigRational> positive = new Rectangle<BigRational>(nw, sw, se, ne);
094        //System.out.println("postiv = " + positive);
095        String[] vars = new String[] { "alpha" };
096        mfac = new GenPolynomialRing<Complex<BigRational>>(cfac, rl, vars);
097        Complex<BigRational> r1 = cfac.fromInteger(1).sum(im);
098        Complex<BigRational> r2 = r1.conjugate();
099        GenPolynomial<Complex<BigRational>> mo = mfac.univariate(0, 1);
100        mo = mo.subtract(r1).multiply(mo.subtract(r2)); // (x - (1+i))((x - (1-i))) 
101        fac = new ComplexAlgebraicRing<BigRational>(mo, positive);
102        alpha = fac.getGenerator();
103        //System.out.println("fac = " + fac);
104    }
105
106
107    @Override
108    protected void tearDown() {
109        a = b = c = d = e = null;
110        fac = null;
111        alpha = null;
112    }
113
114
115    /**
116     * Test constructor and toString.
117     */
118    public void testConstruction() {
119        c = fac.getONE();
120        //System.out.println("c = " + c);
121        //System.out.println("c.getVal() = " + c.getVal());
122        assertTrue("length( c ) = 1", c.number.getVal().length() == 1);
123        assertTrue("isZERO( c )", !c.isZERO());
124        assertTrue("isONE( c )", c.isONE());
125
126        d = fac.getZERO();
127        //System.out.println("d = " + d);
128        //System.out.println("d.getVal() = " + d.getVal());
129        assertTrue("length( d ) = 0", d.number.getVal().length() == 0);
130        assertTrue("isZERO( d )", d.isZERO());
131        assertTrue("isONE( d )", !d.isONE());
132    }
133
134
135    /**
136     * Test random polynomial.
137     */
138    public void testRandom() {
139        for (int i = 0; i < 7; i++) {
140            a = fac.random(el);
141            //System.out.println("a = " + a);
142            if (a.isZERO() || a.isONE()) {
143                continue;
144            }
145            // fac.random(kl*(i+1), ll+2*i, el+i, q );
146            assertTrue("length( a" + i + " ) <> 0", a.number.getVal().length() >= 0);
147            assertTrue(" not isZERO( a" + i + " )", !a.isZERO());
148            assertTrue(" not isONE( a" + i + " )", !a.isONE());
149        }
150    }
151
152
153    /**
154     * Test addition.
155     */
156    public void testAddition() {
157        a = fac.random(ll);
158        b = fac.random(ll);
159
160        c = a.sum(b);
161        d = c.subtract(b);
162        assertEquals("a+b-b = a", a, d);
163
164        c = a.sum(b);
165        d = b.sum(a);
166        assertEquals("a+b = b+a", c, d);
167
168        c = fac.random(ll);
169        d = c.sum(a.sum(b));
170        e = c.sum(a).sum(b);
171        assertEquals("c+(a+b) = (c+a)+b", d, e);
172
173
174        c = a.sum(fac.getZERO());
175        d = a.subtract(fac.getZERO());
176        assertEquals("a+0 = a-0", c, d);
177
178        c = fac.getZERO().sum(a);
179        d = fac.getZERO().subtract(a.negate());
180        assertEquals("0+a = 0+(-a)", c, d);
181    }
182
183
184    /**
185     * Test object multiplication.
186     */
187    public void testMultiplication() {
188        a = fac.random(ll);
189        assertTrue("not isZERO( a )", !a.isZERO());
190
191        b = fac.random(ll);
192        assertTrue("not isZERO( b )", !b.isZERO());
193
194        c = b.multiply(a);
195        d = a.multiply(b);
196        assertTrue("not isZERO( c )", !c.isZERO());
197        assertTrue("not isZERO( d )", !d.isZERO());
198
199        //System.out.println("a = " + a);
200        //System.out.println("b = " + b);
201        e = d.subtract(c);
202        assertTrue("isZERO( a*b-b*a ) " + e, e.isZERO());
203
204        assertTrue("a*b = b*a", c.equals(d));
205        assertEquals("a*b = b*a", c, d);
206
207        c = fac.random(ll);
208        //System.out.println("c = " + c);
209        d = a.multiply(b.multiply(c));
210        e = (a.multiply(b)).multiply(c);
211
212        //System.out.println("d = " + d);
213        //System.out.println("e = " + e);
214
215        //System.out.println("d-e = " + d.subtract(c) );
216
217        assertEquals("a(bc) = (ab)c", d, e);
218        assertTrue("a(bc) = (ab)c", d.equals(e));
219
220        c = a.multiply(fac.getONE());
221        d = fac.getONE().multiply(a);
222        assertEquals("a*1 = 1*a", c, d);
223
224
225        c = a.inverse();
226        d = c.multiply(a);
227        //System.out.println("a = " + a);
228        //System.out.println("c = " + c);
229        //System.out.println("d = " + d);
230        assertEquals("a*1/a = 1", fac.getONE(), d);
231
232        try {
233            a = fac.getZERO().inverse();
234        } catch (NotInvertibleException expected) {
235            return;
236        }
237        fail("0 invertible");
238    }
239
240
241    /**
242     * Test distributive law.
243     */
244    public void testDistributive() {
245        a = fac.random(ll);
246        b = fac.random(ll);
247        c = fac.random(ll);
248
249        d = a.multiply(b.sum(c));
250        e = a.multiply(b).sum(a.multiply(c));
251
252        assertEquals("a(b+c) = ab+ac", d, e);
253    }
254
255
256    /**
257     * Test compareTo of complex algebraic numbers.
258     */
259    public void testCompare() {
260        a = fac.random(ll).abs();
261        b = a.sum(fac.getONE());
262        c = b.sum(fac.getONE());
263
264        int ab = a.compareTo(b);
265        int bc = b.compareTo(c);
266        int ac = a.compareTo(c);
267
268        assertTrue("a < a+1 ", ab < 0);
269        assertTrue("a+1 < a+2 ", bc < 0);
270        assertTrue("a < a+2 ", ac < 0);
271
272        a = a.negate();
273        b = a.sum(fac.getONE());
274        c = b.sum(fac.getONE());
275
276        ab = a.compareTo(b);
277        bc = b.compareTo(c);
278        ac = a.compareTo(c);
279
280        assertTrue("a < a+1 ", ab < 0);
281        assertTrue("a+1 < a+2 ", bc < 0);
282        assertTrue("a < a+2 ", ac < 0);
283    }
284
285}