001 /*
002 * $Id: GenPolynomialTest.java 2232 2008-12-06 21:56:18Z kredel $
003 */
004
005 package edu.jas.poly;
006
007 import java.util.Map;
008 import java.util.List;
009 import java.util.ArrayList;
010 import java.util.Iterator;
011
012
013 import junit.framework.Test;
014 import junit.framework.TestCase;
015 import junit.framework.TestSuite;
016
017
018 import org.apache.log4j.BasicConfigurator;
019
020
021 import edu.jas.arith.BigRational;
022 import edu.jas.arith.BigInteger;
023
024 import edu.jas.poly.ExpVector;
025 import edu.jas.poly.GenPolynomial;
026 import edu.jas.poly.GenPolynomialRing;
027
028 import edu.jas.structure.RingElem;
029 import edu.jas.structure.UnaryFunctor;
030
031
032 /**
033 * GenPolynomial tests with JUnit.
034 * @author Heinz Kredel.
035 */
036
037 public class GenPolynomialTest extends TestCase {
038
039 /**
040 * main
041 */
042 public static void main (String[] args) {
043 BasicConfigurator.configure();
044 junit.textui.TestRunner.run( suite() );
045 }
046
047 /**
048 * Constructs a <CODE>GenPolynomialTest</CODE> object.
049 * @param name String.
050 */
051 public GenPolynomialTest(String name) {
052 super(name);
053 }
054
055 /**
056 * suite.
057 */
058 public static Test suite() {
059 TestSuite suite= new TestSuite(GenPolynomialTest.class);
060 return suite;
061 }
062
063 //private final static int bitlen = 100;
064
065 int rl = 6;
066 int kl = 10;
067 int ll = 7;
068 int el = 4;
069 float q = 0.5f;
070
071 protected void setUp() {
072 // a = b = c = d = e = null;
073 }
074
075 protected void tearDown() {
076 // a = b = c = d = e = null;
077 }
078
079
080 /**
081 * Test constructors and factory.
082 *
083 */
084 public void testConstructors() {
085 // rational numbers
086 BigRational rf = new BigRational();
087 // System.out.println("rf = " + rf);
088
089 BigRational r = rf.fromInteger( 99 );
090 // System.out.println("r = " + r);
091 r = rf.random( 9 );
092 // System.out.println("r = " + r);
093
094 RingElem<BigRational> re = new BigRational( 3 );
095 // System.out.println("re = " + re);
096
097
098 // polynomials over rational numbers
099 GenPolynomialRing<BigRational> pf = new GenPolynomialRing<BigRational>(rf,2);
100 // System.out.println("pf = " + pf);
101
102 GenPolynomial<BigRational> p = pf.getONE();
103 // System.out.println("p = " + p);
104 p = pf.random( 9 );
105 // System.out.println("p = " + p);
106 p = pf.getZERO();
107 // System.out.println("p = " + p);
108
109 RingElem< GenPolynomial<BigRational> > pe = new GenPolynomial<BigRational>( pf );
110 //System.out.println("pe = " + pe);
111 //System.out.println("p.equals(pe) = " + p.equals(pe) );
112 //System.out.println("p.equals(p) = " + p.equals(p) );
113 assertTrue("p.equals(pe) = ", p.equals(pe) );
114 assertTrue("p.equals(p) = ", p.equals(p) );
115
116 pe = pe.sum( p ); // why not p = p.add(pe) ?
117 //System.out.println("pe = " + pe);
118 p = pf.random( 9 );
119 p = p.subtract( p );
120 //System.out.println("p = " + p);
121 //System.out.println("p.isZERO() = " + p.isZERO());
122 assertTrue("p.isZERO() = ", p.isZERO());
123
124
125 // polynomials over (polynomials over rational numbers)
126 GenPolynomialRing< GenPolynomial<BigRational> > ppf = new GenPolynomialRing< GenPolynomial<BigRational> >(pf,3);
127 // System.out.println("ppf = " + ppf);
128
129 GenPolynomial< GenPolynomial<BigRational> > pp = ppf.getONE();
130 // System.out.println("pp = " + pp);
131 pp = ppf.random( 2 );
132 // System.out.println("pp = " + pp);
133 pp = ppf.getZERO();
134 // System.out.println("pp = " + pp);
135
136 RingElem< GenPolynomial< GenPolynomial<BigRational> > > ppe = new GenPolynomial< GenPolynomial<BigRational> >( ppf );
137 // System.out.println("ppe = " + ppe);
138 // System.out.println("pp.equals(ppe) = " + pp.equals(ppe) );
139 // System.out.println("pp.equals(pp) = " + pp.equals(pp) );
140 assertTrue("pp.equals(ppe) = ", pp.equals(ppe) );
141 assertTrue("pp.equals(pp) = ", pp.equals(pp) );
142
143 ppe = ppe.sum( pp ); // why not pp = pp.add(ppe) ?
144 //System.out.println("ppe = " + ppe);
145 pp = ppf.random( 2 );
146 pp = pp.subtract( pp );
147 //System.out.println("pp = " + pp);
148 //System.out.println("pp.isZERO() = " + pp.isZERO());
149 assertTrue("pp.isZERO() = ", pp.isZERO());
150
151
152 // polynomials over (polynomials over (polynomials over rational numbers))
153 GenPolynomialRing< GenPolynomial< GenPolynomial<BigRational> > > pppf = new GenPolynomialRing< GenPolynomial< GenPolynomial<BigRational> > >(ppf,4);
154 // System.out.println("pppf = " + pppf);
155
156 GenPolynomial< GenPolynomial< GenPolynomial<BigRational> > > ppp = pppf.getONE();
157 //System.out.println("ppp = " + ppp);
158 ppp = pppf.random( 2 );
159 // System.out.println("ppp = " + ppp);
160 ppp = pppf.getZERO();
161 // System.out.println("ppp = " + ppp);
162
163 RingElem< GenPolynomial< GenPolynomial< GenPolynomial<BigRational> > > > pppe = new GenPolynomial< GenPolynomial< GenPolynomial<BigRational> > >( pppf );
164 // System.out.println("pppe = " + pppe);
165 // System.out.println("ppp.equals(pppe) = " + ppp.equals(pppe) );
166 // System.out.println("ppp.equals(ppp) = " + ppp.equals(ppp) );
167 assertTrue("ppp.equals(pppe) = ", ppp.equals(pppe) );
168 assertTrue("ppp.equals(ppp) = ", ppp.equals(ppp) );
169
170 pppe = pppe.sum( ppp ); // why not ppp = ppp.add(pppe) ?
171 // System.out.println("pppe = " + pppe);
172 ppp = pppf.random( 2 );
173 ppp = ppp.subtract( ppp );
174 // System.out.println("ppp = " + ppp);
175 // System.out.println("ppp.isZERO() = " + ppp.isZERO());
176 assertTrue("ppp.isZERO() = ", ppp.isZERO());
177
178 // some tests
179 //GenPolynomial<BigRational> pfx = new GenPolynomial<BigRational>();
180 //System.out.println("pfx = " + pfx);
181
182 }
183
184
185 /**
186 * Test extension and contraction.
187 *
188 */
189 public void testExtendContract() {
190 // rational numbers
191 BigRational cf = new BigRational( 99 );
192 // System.out.println("cf = " + cf);
193
194 // polynomials over rational numbers
195 GenPolynomialRing<BigRational> pf = new GenPolynomialRing<BigRational>(cf,rl);
196 // System.out.println("pf = " + pf);
197
198 GenPolynomial<BigRational> a = pf.random(kl,ll,el,q);
199 //System.out.println("a = " + a);
200
201 int k = rl;
202 GenPolynomialRing<BigRational> pfe = pf.extend(k);
203 GenPolynomialRing<BigRational> pfec = pfe.contract(k);
204 assertEquals("pf == pfec",pf,pfec);
205
206 GenPolynomial<BigRational> ae = a.extend(pfe,0,0);
207
208 Map<ExpVector,GenPolynomial<BigRational>> m = ae.contract(pfec);
209 List<GenPolynomial<BigRational>> ml = new ArrayList<GenPolynomial<BigRational>>( m.values() );
210 GenPolynomial<BigRational> aec = ml.get(0);
211 assertEquals("a == aec",a,aec);
212 //System.out.println("ae = " + ae);
213 //System.out.println("aec = " + aec);
214 }
215
216
217 /**
218 * Test reversion.
219 *
220 */
221 public void testReverse() {
222 // rational numbers
223 BigRational cf = new BigRational( 99 );
224 // System.out.println("cf = " + cf);
225
226 // polynomials over rational numbers
227 GenPolynomialRing<BigRational> pf = new GenPolynomialRing<BigRational>(cf,rl);
228 //System.out.println("pf = " + pf);
229
230 GenPolynomial<BigRational> a = pf.random(kl,ll,el,q);
231 //System.out.println("a = " + a);
232
233 int k = rl;
234 GenPolynomialRing<BigRational> pfr = pf.reverse();
235 GenPolynomialRing<BigRational> pfrr = pfr.reverse();
236 assertEquals("pf == pfrr",pf,pfrr);
237 //System.out.println("pfr = " + pfr);
238
239 GenPolynomial<BigRational> ar = a.reverse(pfr);
240 GenPolynomial<BigRational> arr = ar.reverse(pfrr);
241 assertEquals("a == arr",a,arr);
242 //System.out.println("ar = " + ar);
243 //System.out.println("arr = " + arr);
244 }
245
246
247 /**
248 * Test accessors.
249 *
250 */
251 public void testAccessors() {
252 // rational numbers
253 BigRational rf = new BigRational();
254 // System.out.println("rf = " + rf);
255
256 // polynomials over rational numbers
257 GenPolynomialRing<BigRational> pf
258 = new GenPolynomialRing<BigRational>(rf,rl);
259 // System.out.println("pf = " + pf);
260
261 // test 1
262 GenPolynomial<BigRational> p = pf.getONE();
263 // System.out.println("p = " + p);
264
265 ExpVector e = p.leadingExpVector();
266 BigRational c = p.leadingBaseCoefficient();
267
268 GenPolynomial<BigRational> f
269 = new GenPolynomial<BigRational>(pf,c,e);
270 assertEquals("1 == 1 ",p,f);
271
272 GenPolynomial<BigRational> r = p.reductum();
273 assertTrue("red(1) == 0 ",r.isZERO());
274
275
276 // test 0
277 p = pf.getZERO();
278 // System.out.println("p = " + p);
279 e = p.leadingExpVector();
280 c = p.leadingBaseCoefficient();
281
282 f = new GenPolynomial<BigRational>(pf,c,e);
283 assertEquals("0 == 0 ",p,f);
284
285 r = p.reductum();
286 assertTrue("red(0) == 0 ",r.isZERO());
287
288
289 // test random
290 p = pf.random(kl,2*ll,el,q);
291 // System.out.println("p = " + p);
292 e = p.leadingExpVector();
293 c = p.leadingBaseCoefficient();
294 r = p.reductum();
295
296 f = new GenPolynomial<BigRational>(pf,c,e);
297 f = r.sum(f);
298 assertEquals("p == lm(f)+red(f) ",p,f);
299
300
301 // test iteration over random
302 GenPolynomial<BigRational> g;
303 g = p;
304 f = pf.getZERO();
305 while ( !g.isZERO() ) {
306 e = g.leadingExpVector();
307 c = g.leadingBaseCoefficient();
308 //System.out.println("c e = " + c + " " + e);
309 r = g.reductum();
310 f = f.sum(c,e);
311 g = r;
312 }
313 assertEquals("p == lm(f)+lm(red(f))+... ",p,f);
314 }
315
316
317 /**
318 * Test iterators.
319 *
320 */
321 public void testIterators() {
322 // integers
323 BigInteger rf = new BigInteger();
324 // System.out.println("rf = " + rf);
325
326 // polynomials over integral numbers
327 GenPolynomialRing<BigInteger> pf
328 = new GenPolynomialRing<BigInteger>(rf,rl);
329 // System.out.println("pf = " + pf);
330
331 // random polynomial
332 GenPolynomial<BigInteger> p = pf.random(kl,2*ll,el,q);
333 //System.out.println("p = " + p);
334
335 // test monomials
336 for ( Monomial<BigInteger> m : p ) {
337 //System.out.println("m = " + m);
338 assertFalse("m.c == 0 ",m.coefficient().isZERO());
339 assertFalse("m.e < (0) ",m.exponent().signum()<0);
340 }
341
342 // test exponents
343 Iterator<ExpVector> et = p.exponentIterator();
344 while ( et.hasNext() ) {
345 ExpVector e = et.next();
346 //System.out.println("e = " + e);
347 assertFalse("e < (0) ", e.signum() < 0);
348 }
349
350 // test coefficents
351 Iterator<BigInteger> ct = p.coefficientIterator();
352 while ( ct.hasNext() ) {
353 BigInteger i = ct.next();
354 //System.out.println("i = " + i);
355 assertFalse("i == 0 ", i.isZERO());
356 }
357 }
358
359
360 /**
361 * Test coefficient map function.
362 *
363 */
364 public void testMap() {
365 // integers
366 BigInteger rf = new BigInteger();
367 // System.out.println("rf = " + rf);
368
369 // polynomials over integral numbers
370 GenPolynomialRing<BigInteger> pf
371 = new GenPolynomialRing<BigInteger>(rf,rl);
372 // System.out.println("pf = " + pf);
373
374 // random polynomial
375 GenPolynomial<BigInteger> p = pf.random(kl,2*ll,el,q);
376 //System.out.println("p = " + p);
377
378 // test times 1
379 GenPolynomial<BigInteger> q;
380 q = p.map( new Multiply<BigInteger>( rf.getONE() ) );
381 assertEquals("p == q ",p,q);
382
383 // test times 0
384 q = p.map( new Multiply<BigInteger>( rf.getZERO() ) );
385 assertTrue("q == 0 ",q.isZERO());
386
387 // test times -1
388 q = p.map( new Multiply<BigInteger>( rf.getONE().negate() ) );
389 assertEquals("p == q ",p.negate(),q);
390 }
391
392 }
393
394 /**
395 * Internal scalar multiplication functor.
396 */
397 class Multiply<C extends RingElem<C>> implements UnaryFunctor<C,C> {
398 C x;
399 public Multiply(C x) {
400 this.x = x;
401 }
402 public C eval(C c) {
403 return c.multiply(x);
404 }
405 }