001/* 002 * $Id: GenPolynomialTest.java 5370 2015-12-27 13:42:22Z 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 = 5; 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 weight homogeneous. 392 */ 393 public void testWeightHomogeneous() { 394 // rational numbers 395 BigRational rf = new BigRational(); 396 // System.out.println("rf = " + rf); 397 398 // weight term order 399 long[] weight = new long[] { 2, 3, 4, 5 }; 400 TermOrder to = TermOrderByName.weightOrder(weight); 401 // System.out.println("to = " + to); 402 403 // polynomials over rational numbers 404 GenPolynomialRing<BigRational> pf = new GenPolynomialRing<BigRational>(rf, 4, to); 405 // System.out.println("pf = " + pf.toScript()); 406 407 // test 1 408 GenPolynomial<BigRational> p = pf.getONE(); 409 // System.out.println("p = " + p); 410 assertTrue("1 is weight homogeneous " + p, p.isWeightHomogeneous()); 411 412 // test 0 413 p = pf.getZERO(); 414 // System.out.println("p = " + p); 415 assertTrue("0 is weight homogeneous " + p, p.isWeightHomogeneous()); 416 417 // test random 418 p = pf.random(kl, 3 * ll, el, q); 419 // System.out.println("p = " + p); 420 assertFalse("rnd is weight homogeneous " + p, p.isWeightHomogeneous()); 421 422 GenPolynomial<BigRational> pl = p.leadingWeightPolynomial(); 423 // System.out.println("pl = " + pl); 424 assertTrue("lw(rnd) is weight homogeneous " + pl, pl.isWeightHomogeneous()); 425 426 GenPolynomial<BigRational> r = pf.random(kl, 3 * ll, el, q); 427 // System.out.println("r = " + r); 428 assertFalse("rnd is weight homogeneous " + r, r.isWeightHomogeneous()); 429 430 GenPolynomial<BigRational> rl = r.leadingWeightPolynomial(); 431 // System.out.println("rl = " + rl); 432 assertTrue("lw(rnd) is weight homogeneous " + rl, rl.isWeightHomogeneous()); 433 434 GenPolynomial<BigRational> t = pl.multiply(rl); 435 // System.out.println("t = " + t); 436 assertTrue("lw(rnd)*lw(rnd) is weight homogeneous " + t, t.isWeightHomogeneous()); 437 } 438 439 440 /** 441 * Test univariate. 442 */ 443 public void testUnivariate() { 444 BigInteger rf = new BigInteger(); 445 // System.out.println("rf = " + rf); 446 447 // polynomials over integral numbers 448 GenPolynomialRing<BigInteger> pf = new GenPolynomialRing<BigInteger>(rf, rl); 449 //System.out.println("pf = " + pf); 450 451 GenPolynomial<BigInteger> a, b, c, d; 452 453 // x**1 454 a = pf.univariate(pf.nvar-1); 455 //System.out.println("a = " + a); 456 assertTrue("deg(a) = 1: ", a.degree() == 1); 457 assertEquals("xi == xi: ", pf.vars[0], a.toString()); 458 459 b = pf.univariate(pf.vars[0]); 460 //System.out.println("b = " + b); 461 assertTrue("deg(b) = 1: ", b.degree() == 1); 462 assertEquals("xi == xi: ", pf.vars[0], b.toString()); 463 464 c = pf.univariate(0); 465 //System.out.println("c = " + c); 466 assertTrue("deg(c) = 1: ", c.degree() == 1); 467 assertEquals("xi == xi: ", pf.vars[pf.nvar-1], c.toString()); 468 469 d = pf.univariate(pf.vars[pf.nvar-1]); 470 //System.out.println("d = " + d); 471 assertTrue("deg(c) = 1: ", c.degree() == 1); 472 assertEquals("xi == xi: ", pf.vars[pf.nvar-1], d.toString()); 473 474 // x**7 475 a = pf.univariate(pf.nvar-1, 7); 476 //System.out.println("a = " + a); 477 assertTrue("deg(a) = 7: ", a.degree() == 7); 478 assertEquals("xi == xi: ", pf.vars[0]+"^7", a.toString()); 479 480 b = pf.univariate(pf.vars[0], 7); 481 //System.out.println("b = " + b); 482 assertTrue("deg(b) = 7: ", b.degree() == 7); 483 assertEquals("xi == xi: ", pf.vars[0]+"^7", b.toString()); 484 485 c = pf.univariate(0, 7); 486 //System.out.println("c = " + c); 487 assertTrue("deg(c) = 7: ", c.degree() == 7); 488 assertEquals("xi == xi: ", pf.vars[pf.nvar-1]+"^7", c.toString()); 489 490 d = pf.univariate(pf.vars[pf.nvar-1], 7); 491 //System.out.println("d = " + d); 492 assertTrue("deg(c) = 7: ", c.degree() == 7); 493 assertEquals("xi == xi: ", pf.vars[pf.nvar-1]+"^7", d.toString()); 494 } 495 496 497 /** 498 * Test iterators. 499 */ 500 public void testIterators() { 501 // integers 502 BigInteger rf = new BigInteger(); 503 // System.out.println("rf = " + rf); 504 505 // polynomials over integral numbers 506 GenPolynomialRing<BigInteger> pf = new GenPolynomialRing<BigInteger>(rf, rl); 507 // System.out.println("pf = " + pf); 508 509 // random polynomial 510 GenPolynomial<BigInteger> p = pf.random(kl, 2 * ll, el, q); 511 //System.out.println("p = " + p); 512 513 // test monomials 514 for (Monomial<BigInteger> m : p) { 515 //System.out.println("m = " + m); 516 assertFalse("m.c == 0 ", m.coefficient().isZERO()); 517 assertFalse("m.e < (0) ", m.exponent().signum() < 0); 518 } 519 520 // test exponents 521 Iterator<ExpVector> et = p.exponentIterator(); 522 while (et.hasNext()) { 523 ExpVector e = et.next(); 524 //System.out.println("e = " + e); 525 assertFalse("e < (0) ", e.signum() < 0); 526 } 527 528 // test coefficents 529 Iterator<BigInteger> ct = p.coefficientIterator(); 530 while (ct.hasNext()) { 531 BigInteger i = ct.next(); 532 //System.out.println("i = " + i); 533 assertFalse("i == 0 ", i.isZERO()); 534 } 535 } 536 537 538 /** 539 * Test coefficient map function. 540 */ 541 public void testMap() { 542 // integers 543 BigInteger rf = new BigInteger(); 544 // System.out.println("rf = " + rf); 545 546 // polynomials over integral numbers 547 GenPolynomialRing<BigInteger> pf = new GenPolynomialRing<BigInteger>(rf, rl); 548 // System.out.println("pf = " + pf); 549 550 // random polynomial 551 GenPolynomial<BigInteger> p = pf.random(kl, 2 * ll, el, q); 552 //System.out.println("p = " + p); 553 554 // test times 1 555 GenPolynomial<BigInteger> q; 556 q = p.map(new Multiply<BigInteger>(rf.getONE())); 557 assertEquals("p == q ", p, q); 558 559 // test times 0 560 q = p.map(new Multiply<BigInteger>(rf.getZERO())); 561 assertTrue("q == 0 ", q.isZERO()); 562 563 // test times -1 564 q = p.map(new Multiply<BigInteger>(rf.getONE().negate())); 565 assertEquals("p == q ", p.negate(), q); 566 } 567 568} 569 570 571/** 572 * Internal scalar multiplication functor. 573 */ 574class Multiply<C extends RingElem<C>> implements UnaryFunctor<C, C> { 575 576 577 C x; 578 579 580 public Multiply(C x) { 581 this.x = x; 582 } 583 584 585 public C eval(C c) { 586 return c.multiply(x); 587 } 588}