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