"Fossies" - the Fresh Open Source Software Archive

Member "doc/api/src-html/edu/jas/poly/AlgebraicNumberModTest.html" (21 Apr 2019, 25074 Bytes) of package /linux/privat/jas-2.6.5988-doc.zip:


Caution: In this restricted "Fossies" environment the current HTML page may not be correctly presentated and may have some non-functional links. You can here alternatively try to browse the pure source code or just view or download the uninterpreted raw source code. If the rendering is insufficient you may try to find and view the page on the project site itself.

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