001    
002    /*
003     * $Id: ResidueTest.java 3366 2010-10-24 13:02:14Z kredel $
004     */
005    
006    package edu.jas.poly;
007    
008    //import java.util.ArrayList;
009    //import java.util.List;
010    
011    import junit.framework.Test;
012    import junit.framework.TestCase;
013    import junit.framework.TestSuite;
014    
015    import org.apache.log4j.BasicConfigurator;
016    //import org.apache.log4j.Logger;
017    
018    import edu.jas.arith.BigRational;
019    import edu.jas.arith.BigInteger;
020    
021    //import edu.jas.structure.RingElem;
022    
023    
024    
025    /**
026     * Residue test with JUnit. 
027     * @author Heinz Kredel.
028     */
029    
030    public class ResidueTest extends TestCase {
031    
032    /**
033     * main.
034     */
035       public static void main (String[] args) {
036           BasicConfigurator.configure();
037           junit.textui.TestRunner.run( suite() );
038       }
039    
040    /**
041     * Constructs a <CODE>ResidueTest</CODE> object.
042     * @param name String.
043     */
044       public ResidueTest(String name) {
045              super(name);
046       }
047    
048    /**
049     * suite.
050     */ 
051     public static Test suite() {
052         TestSuite suite= new TestSuite(ResidueTest.class);
053         return suite;
054       }
055    
056       ResidueRing<BigInteger> fac;
057       GenPolynomialRing<BigRational> pfac;
058       ResidueRing< GenPolynomial<BigRational> > mfac;
059    
060       Residue< BigInteger > a;
061       Residue< BigInteger > b;
062       Residue< BigInteger > c;
063       Residue< BigInteger > d;
064       Residue< BigInteger > e;
065    
066       Residue< GenPolynomial<BigRational> > ap;
067       Residue< GenPolynomial<BigRational> > bp;
068       Residue< GenPolynomial<BigRational> > cp;
069       Residue< GenPolynomial<BigRational> > dp;
070       Residue< GenPolynomial<BigRational> > ep;
071    
072    
073       int rl = 1; 
074       int kl = 13;
075       int ll = 7;
076       int el = 3;
077       float q = 0.4f;
078       int il = 2; 
079       long p = 1152921504606846883L; // 2^60-93; 
080    
081       @Override
082       protected void setUp() {
083           a = b = c = d = e = null;
084           ap = bp = cp = dp = ep = null;
085           BigInteger cfac = new BigInteger(1);
086           fac = new ResidueRing<BigInteger>( cfac, new BigInteger( p ) );
087           pfac = new GenPolynomialRing<BigRational>( new BigRational(1), 1 );
088           GenPolynomial<BigRational> mo = pfac.random(kl,ll,el,q);
089           while ( mo.isConstant() ) {
090                 mo = pfac.random(kl,ll,el,q);
091           }
092           mfac = new ResidueRing<GenPolynomial<BigRational>>( pfac, mo );
093       }
094    
095       @Override
096       protected void tearDown() {
097           a = b = c = d = e = null;
098           ap = bp = cp = dp = ep = null;
099           fac = null;
100           pfac = null;
101           mfac = null;
102       }
103    
104    
105    /**
106     * Test constructor for integer.
107     * 
108     */
109     public void testIntConstruction() {
110         c = fac.getONE();
111         //System.out.println("c = " + c);
112         assertTrue("isZERO( c )", !c.isZERO() );
113         assertTrue("isONE( c )", c.isONE() );
114    
115         d = fac.getZERO();
116         //System.out.println("d = " + d);
117         assertTrue("isZERO( d )", d.isZERO() );
118         assertTrue("isONE( d )", !d.isONE() );
119     }
120    
121    
122    /**
123     * Test constructor for polynomial.
124     * 
125     */
126     public void testPolyConstruction() {
127         cp = mfac.getONE();
128         assertTrue("isZERO( cp )", !cp.isZERO() );
129         assertTrue("isONE( cp )", cp.isONE() );
130    
131         dp = mfac.getZERO();
132         assertTrue("isZERO( dp )", dp.isZERO() );
133         assertTrue("isONE( dp )", !dp.isONE() );
134     }
135    
136    
137    /**
138     * Test random integer.
139     * 
140     */
141     public void testIntRandom() {
142         for (int i = 0; i < 7; i++) {
143             a = fac.random(kl*(i+1));
144            if ( a.isZERO() ) {
145                continue;
146             }
147             //a = fac.random(kl*(i+1), ll+2*i, el+i, q );
148             //System.out.println("a = " + a);
149             assertTrue(" not isZERO( a"+i+" )", !a.isZERO() );
150             assertTrue(" not isONE( a"+i+" )", !a.isONE() );
151         }
152     }
153    
154    
155    /**
156     * Test random polynomial.
157     * 
158     */
159     public void testPolyRandom() {
160         for (int i = 0; i < 7; i++) {
161             ap = mfac.random(kl+i);
162             if ( ap.isZERO() ) {
163                continue;
164             }
165             assertTrue(" not isZERO( ap"+i+" )", !ap.isZERO() );
166             assertTrue(" not isONE( ap"+i+" )", !ap.isONE() );
167         }
168     }
169    
170    
171    /**
172     * Test integer addition.
173     * 
174     */
175     public void testIntAddition() {
176    
177         a = fac.random(kl);
178         b = fac.random(kl);
179    
180         c = a.sum(b);
181         d = c.subtract(b);
182         assertEquals("a+b-b = a",a,d);
183    
184         c = a.sum(b);
185         d = b.sum(a);
186         assertEquals("a+b = b+a",c,d);
187    
188         c = fac.random(kl);
189         d = c.sum( a.sum(b) );
190         e = c.sum( a ).sum(b);
191         assertEquals("c+(a+b) = (c+a)+b",d,e);
192    
193    
194         c = a.sum( fac.getZERO() );
195         d = a.subtract( fac.getZERO() );
196         assertEquals("a+0 = a-0",c,d);
197    
198         c = fac.getZERO().sum( a );
199         d = fac.getZERO().subtract( a.negate() );
200         assertEquals("0+a = 0+(-a)",c,d);
201     }
202    
203    
204    /**
205     * Test polynomial addition.
206     * 
207     */
208     public void testPolyAddition() {
209    
210         ap = mfac.random(kl);
211         bp = mfac.random(kl);
212         //System.out.println("a = " + a);
213         //System.out.println("b = " + b);
214    
215         cp = ap.sum(bp);
216         dp = cp.subtract(bp);
217         assertEquals("a+b-b = a",ap,dp);
218    
219         cp = ap.sum(bp);
220         dp = bp.sum(ap);
221         //System.out.println("c = " + c);
222         //System.out.println("d = " + d);
223    
224         assertEquals("a+b = b+a",cp,dp);
225    
226         cp = mfac.random(kl);
227         dp = cp.sum( ap.sum(bp) );
228         ep = cp.sum( ap ).sum(bp);
229         assertEquals("c+(a+b) = (c+a)+b",dp,ep);
230    
231    
232         cp = ap.sum( mfac.getZERO() );
233         dp = ap.subtract( mfac.getZERO() );
234         assertEquals("a+0 = a-0",cp,dp);
235    
236         cp = mfac.getZERO().sum( ap );
237         dp = mfac.getZERO().subtract( ap.negate() );
238         assertEquals("0+a = 0+(-a)",cp,dp);
239     }
240    
241    
242    /**
243     * Test integer multiplication.
244     * 
245     */
246    
247     public void testIntMultiplication() {
248    
249         a = fac.random(kl);
250         if ( a.isZERO() ) {
251            return;
252         }
253         assertTrue("not isZERO( a )", !a.isZERO() );
254    
255         b = fac.random(kl);
256         if ( b.isZERO() ) {
257            return;
258         }
259         assertTrue("not isZERO( b )", !b.isZERO() );
260    
261         c = b.multiply(a);
262         d = a.multiply(b);
263         assertTrue("not isZERO( c )", !c.isZERO() );
264         assertTrue("not isZERO( d )", !d.isZERO() );
265    
266         //System.out.println("a = " + a);
267         //System.out.println("b = " + b);
268         e = d.subtract(c);
269         assertTrue("isZERO( a*b-b*a ) " + e, e.isZERO() );
270    
271         assertTrue("a*b = b*a", c.equals(d) );
272         assertEquals("a*b = b*a",c,d);
273    
274         c = fac.random(kl);
275         //System.out.println("c = " + c);
276         d = a.multiply( b.multiply(c) );
277         e = (a.multiply(b)).multiply(c);
278    
279         //System.out.println("d = " + d);
280         //System.out.println("e = " + e);
281    
282         //System.out.println("d-e = " + d.subtract(c) );
283    
284         assertEquals("a(bc) = (ab)c",d,e);
285         assertTrue("a(bc) = (ab)c", d.equals(e) );
286    
287         c = a.multiply( fac.getONE() );
288         d = fac.getONE().multiply( a );
289         assertEquals("a*1 = 1*a",c,d);
290    
291         if ( a.isUnit() ) {
292            c = a.inverse();
293            d = c.multiply(a);
294            //System.out.println("a = " + a);
295            //System.out.println("c = " + c);
296            //System.out.println("d = " + d);
297            assertTrue("a*1/a = 1",d.isONE()); 
298         }
299     }
300    
301    
302    /**
303     * Test polynomial multiplication.
304     * 
305     */
306     public void testPolyMultiplication() {
307    
308         ap = mfac.random(kl);
309         if ( ap.isZERO() ) {
310            return;
311         }
312         assertTrue("not isZERO( a )", !ap.isZERO() );
313    
314         bp = mfac.random(kl);
315         if ( bp.isZERO() ) {
316            return;
317         }
318         assertTrue("not isZERO( b )", !bp.isZERO() );
319    
320         cp = bp.multiply(ap);
321         dp = ap.multiply(bp);
322         assertTrue("not isZERO( c )", !cp.isZERO() );
323         assertTrue("not isZERO( d )", !dp.isZERO() );
324    
325         //System.out.println("a = " + a);
326         //System.out.println("b = " + b);
327         ep = dp.subtract(cp);
328         assertTrue("isZERO( a*b-b*a ) " + ep, ep.isZERO() );
329    
330         assertTrue("a*b = b*a", cp.equals(dp) );
331         assertEquals("a*b = b*a",cp,dp);
332    
333         cp = mfac.random(kl);
334         //System.out.println("c = " + c);
335         dp = ap.multiply( bp.multiply(cp) );
336         ep = (ap.multiply(bp)).multiply(cp);
337    
338         //System.out.println("d = " + d);
339         //System.out.println("e = " + e);
340    
341         //System.out.println("d-e = " + d.subtract(c) );
342    
343         assertEquals("a(bc) = (ab)c",dp,ep);
344         assertTrue("a(bc) = (ab)c", dp.equals(ep) );
345    
346         cp = ap.multiply( mfac.getONE() );
347         dp = mfac.getONE().multiply( ap );
348         assertEquals("a*1 = 1*a",cp,dp);
349    
350         if ( ap.isUnit() ) {
351            cp = ap.inverse();
352            dp = cp.multiply(ap);
353            //System.out.println("a = " + a);
354            //System.out.println("c = " + c);
355            //System.out.println("d = " + d);
356            assertTrue("a*1/a = 1",dp.isONE()); 
357         }
358     }
359    
360    
361    }