001/*
002 * $Id: AlgebraicNumberTest.java 5340 2015-12-06 15:00:18Z kredel $
003 */
004
005package edu.jas.poly;
006
007
008import java.util.HashSet;
009import java.util.Set;
010
011import junit.framework.Test;
012import junit.framework.TestCase;
013import junit.framework.TestSuite;
014
015import edu.jas.arith.BigRational;
016// import edu.jas.structure.RingElem;
017import edu.jas.structure.NotInvertibleException;
018
019
020/**
021 * AlgebraicNumber Test using JUnit.
022 * @author Heinz Kredel.
023 */
024
025public class AlgebraicNumberTest extends TestCase {
026
027
028    /**
029     * main.
030     */
031    public static void main(String[] args) {
032        junit.textui.TestRunner.run(suite());
033    }
034
035
036    /**
037     * Constructs a <CODE>AlgebraicNumberTest</CODE> object.
038     * @param name String.
039     */
040    public AlgebraicNumberTest(String name) {
041        super(name);
042    }
043
044
045    /**
046     * suite.
047     */
048    public static Test suite() {
049        TestSuite suite = new TestSuite(AlgebraicNumberTest.class);
050        return suite;
051    }
052
053
054    //private final static int bitlen = 100;
055
056    AlgebraicNumberRing<BigRational> fac;
057
058
059    GenPolynomialRing<BigRational> mfac;
060
061
062    AlgebraicNumber<BigRational> a, b, c, d, e;
063
064
065    int rl = 1;
066
067
068    int kl = 10;
069
070
071    int ll = 7;
072
073
074    int el = ll;
075
076
077    float q = 0.4f;
078
079
080    @Override
081    protected void setUp() {
082        a = b = c = d = e = null;
083        BigRational bi = new BigRational(1);
084        //bi.setAllIterator();
085        bi.setNoDuplicatesIterator();
086        mfac = new GenPolynomialRing<BigRational>(bi, rl);
087        GenPolynomial<BigRational> mo = mfac.random(kl, ll, 7, q);
088        while (mo.isConstant()) {
089            mo = mfac.random(kl, ll, 5, q);
090        }
091        fac = new AlgebraicNumberRing<BigRational>(mo.monic());
092    }
093
094
095    @Override
096    protected void tearDown() {
097        a = b = c = d = e = null;
098        fac = null;
099    }
100
101
102    /**
103     * Test constructor and toString.
104     */
105    public void testConstruction() {
106        c = fac.getONE();
107        //System.out.println("c = " + c);
108        //System.out.println("c.getVal() = " + c.getVal());
109        assertTrue("length( c ) = 1", c.getVal().length() == 1);
110        assertTrue("isZERO( c )", !c.isZERO());
111        assertTrue("isONE( c )", c.isONE());
112
113        d = fac.getZERO();
114        //System.out.println("d = " + d);
115        //System.out.println("d.getVal() = " + d.getVal());
116        assertTrue("length( d ) = 0", d.getVal().length() == 0);
117        assertTrue("isZERO( d )", d.isZERO());
118        assertTrue("isONE( d )", !d.isONE());
119    }
120
121
122    /**
123     * Test random polynomial.
124     */
125    public void testRandom() {
126        for (int i = 0; i < 7; i++) {
127            a = fac.random(el);
128            //System.out.println("a = " + a);
129            if (a.isZERO() || a.isONE()) {
130                continue;
131            }
132            // fac.random(rl+i, kl*(i+1), ll+2*i, el+i, q );
133            assertTrue("length( a" + i + " ) <> 0", a.getVal().length() >= 0);
134            assertTrue(" not isZERO( a" + i + " )", !a.isZERO());
135            assertTrue(" not isONE( a" + i + " )", !a.isONE());
136        }
137    }
138
139
140    /**
141     * Test addition.
142     */
143    public void testAddition() {
144        a = fac.random(ll);
145        b = fac.random(ll);
146
147        c = a.sum(b);
148        d = c.subtract(b);
149        assertEquals("a+b-b = a", a, d);
150
151        c = a.sum(b);
152        d = b.sum(a);
153        assertEquals("a+b = b+a", c, d);
154
155        c = fac.random(ll);
156        d = c.sum(a.sum(b));
157        e = c.sum(a).sum(b);
158        assertEquals("c+(a+b) = (c+a)+b", d, e);
159
160
161        c = a.sum(fac.getZERO());
162        d = a.subtract(fac.getZERO());
163        assertEquals("a+0 = a-0", c, d);
164
165        c = fac.getZERO().sum(a);
166        d = fac.getZERO().subtract(a.negate());
167        assertEquals("0+a = 0+(-a)", c, d);
168    }
169
170
171    /**
172     * Test object multiplication.
173     */
174    @SuppressWarnings("cast")
175    public void testMultiplication() {
176        a = fac.random(ll);
177        assertTrue("not isZERO( a )", !a.isZERO());
178
179        b = fac.random(ll);
180        assertTrue("not isZERO( b )", !b.isZERO());
181
182        c = b.multiply(a);
183        d = a.multiply(b);
184        assertTrue("not isZERO( c )", !c.isZERO());
185        assertTrue("not isZERO( d )", !d.isZERO());
186
187        //System.out.println("a = " + a);
188        //System.out.println("b = " + b);
189        e = d.subtract(c);
190        assertTrue("isZERO( a*b-b*a ) " + e, e.isZERO());
191
192        assertTrue("a*b = b*a", c.equals(d));
193        assertEquals("a*b = b*a", c, d);
194
195        c = fac.random(ll);
196        //System.out.println("c = " + c);
197        d = a.multiply(b.multiply(c));
198        e = (a.multiply(b)).multiply(c);
199
200        //System.out.println("d = " + d);
201        //System.out.println("e = " + e);
202        //System.out.println("d-e = " + d.subtract(c) );
203
204        assertEquals("a(bc) = (ab)c", d, e);
205        assertTrue("a(bc) = (ab)c", d.equals(e));
206
207        c = a.multiply(fac.getONE());
208        d = fac.getONE().multiply(a);
209        assertEquals("a*1 = 1*a", c, d);
210
211        c = a.inverse();
212        d = c.multiply(a);
213        //System.out.println("a = " + a);
214        //System.out.println("c = " + c);
215        //System.out.println("d = " + d);
216        assertEquals("a*1/a = 1", fac.getONE(), d);
217
218        try {
219            d = fac.getZERO().inverse();
220            fail("0 invertible");
221        } catch (NotInvertibleException expected) {
222            // ok
223        }
224
225        GenPolynomial<BigRational> dp = fac.modul;
226        GenPolynomial<BigRational> cp = fac.modul.multiply(a.val.monic());
227        //System.out.println("dp = " + dp);
228        //System.out.println("cp = " + cp);
229        fac = new AlgebraicNumberRing<BigRational>(cp);
230        a = new AlgebraicNumber<BigRational>(fac, a.val.monic());
231        assertFalse("a !unit mod m*a: " + a, a.isUnit() && !a.isONE());
232
233        try {
234            b = a.inverse();
235            fail("invertible " + a);
236        } catch (AlgebraicNotInvertibleException expected) {
237            //ok
238            //expected.printStackTrace();
239            //System.out.println("expected = " + expected);
240            GenPolynomial<BigRational> f1 = (GenPolynomial<BigRational>) expected.f1;
241            GenPolynomial<BigRational> f2 = (GenPolynomial<BigRational>) expected.f2;
242            assertTrue("f  = " + cp, expected.f.equals(cp));
243            assertTrue("f1 = " + a.val, f1.equals(a.val));
244            assertTrue("f2 = " + dp, f2.equals(dp));
245            assertTrue("f  =  f1*f2 ", expected.f.equals(f1.multiply(f2)));
246        } catch (NotInvertibleException e) {
247            //e.printStackTrace();
248            fail("wrong exception " + e);
249        }
250    }
251
252
253    /**
254     * Test distributive law.
255     */
256    public void testDistributive() {
257        a = fac.random(ll);
258        b = fac.random(ll);
259        c = fac.random(ll);
260
261        d = a.multiply(b.sum(c));
262        e = a.multiply(b).sum(a.multiply(c));
263
264        assertEquals("a(b+c) = ab+ac", d, e);
265    }
266
267
268    /**
269     * Test enumerator.
270     */
271    public void testEnumerator() {
272        //System.out.println("fac = " + fac);
273        long s = 0L;
274        long t = 0L;
275        Set<AlgebraicNumber<BigRational>> elems = new HashSet<AlgebraicNumber<BigRational>>(49);
276        //Iterator<AlgebraicNumber<BigRational>> iter = fac.iterator();
277        for (AlgebraicNumber<BigRational> an : fac) {
278            t++;
279            if (elems.contains(an)) {
280                //System.out.println("dup(an) = " + an);
281                s++;
282            } else {
283                //System.out.println("an = " + an);
284                elems.add(an);
285            }
286            if (t >= 100) {
287                break;
288            }
289        }
290        //System.out.println("elems = " + elems);
291        assertTrue("#elems " + t + ", t = " + elems.size(), t == elems.size());
292        assertTrue("#elems " + t + ", t = " + elems.size(), s == 0L);
293    }
294
295}