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