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