001/* 002 * $Id: MultiVarPowerSeriesTest.java 5688 2017-01-03 08:45:09Z kredel $ 003 */ 004 005package edu.jas.ps; 006 007 008import java.util.ArrayList; 009import java.util.List; 010import java.util.Map; 011 012import edu.jas.arith.BigRational; 013import edu.jas.poly.ExpVector; 014import edu.jas.poly.GenPolynomial; 015import edu.jas.poly.GenPolynomialRing; 016 017import junit.framework.Test; 018import junit.framework.TestCase; 019import junit.framework.TestSuite; 020 021 022/** 023 * Multivariate power series tests with JUnit. 024 * @author Heinz Kredel 025 */ 026 027public class MultiVarPowerSeriesTest extends TestCase { 028 029 030 /** 031 * main. 032 */ 033 public static void main(String[] args) { 034 junit.textui.TestRunner.run(suite()); 035 } 036 037 038 /** 039 * Constructs a <CODE>MultiVarPowerSeriesTest</CODE> object. 040 * @param name String. 041 */ 042 public MultiVarPowerSeriesTest(String name) { 043 super(name); 044 } 045 046 047 /** 048 */ 049 public static Test suite() { 050 TestSuite suite = new TestSuite(MultiVarPowerSeriesTest.class); 051 return suite; 052 } 053 054 055 BigRational cfac; 056 057 058 MultiVarPowerSeriesRing<BigRational> fac; 059 060 061 MultiVarPowerSeries<BigRational> a, b, c, d, e, f; 062 063 064 int rl = 2; 065 066 067 int kl = 10; 068 069 070 float q = 0.3f; 071 072 073 @Override 074 protected void setUp() { 075 a = b = c = d = e = null; 076 String[] vars = new String[] { "x", "y" }; 077 cfac = new BigRational(1); 078 fac = new MultiVarPowerSeriesRing<BigRational>(cfac, rl, vars); 079 //System.out.println("fac = " + fac); 080 //System.out.println("fac = " + fac.toScript()); 081 } 082 083 084 @Override 085 protected void tearDown() { 086 a = b = c = d = e = null; 087 fac = null; 088 } 089 090 091 /** 092 * Test MultiVarCoefficients. 093 */ 094 public void testCoefficients() { 095 BigRational cf = new BigRational(0); 096 GenPolynomialRing<BigRational> pring = new GenPolynomialRing<BigRational>(cf, rl); 097 098 MultiVarCoefficients<BigRational> zeros = new Zeros(pring); 099 MultiVarCoefficients<BigRational> ones = new Ones(pring); 100 MultiVarCoefficients<BigRational> vars = new Vars(pring); 101 102 int m = 5; 103 ExpVectorIterable eiter = new ExpVectorIterable(rl, true, m); 104 for (ExpVector e : eiter) { 105 BigRational c = zeros.get(e); 106 //System.out.println("c = " + c + ", e = " + e); 107 assertTrue("isZERO( c )", c.isZERO()); 108 } 109 //System.out.println("coeffCache = " + zeros.coeffCache); 110 //System.out.println("zeroCache = " + zeros.zeroCache); 111 assertTrue("coeffCache is one element", zeros.coeffCache.size() == (m + 1)); 112 113 for (ExpVector e : eiter) { 114 BigRational c = ones.get(e); 115 //System.out.println("c = " + c + ", e = " + e); 116 assertTrue("isONE( c )", c.isONE()); 117 } 118 //System.out.println("coeffCache = " + ones.coeffCache); 119 //System.out.println("zeroCache = " + ones.zeroCache); 120 assertTrue("zeroCache is empty", ones.zeroCache.isEmpty()); 121 122 for (int i = 0; i <= m; i++) { 123 GenPolynomial<BigRational> c = ones.getHomPart(i); 124 //System.out.println("c = " + c + ", i = " + i); 125 GenPolynomial<BigRational> d = ones.getHomPart(i); 126 //System.out.println("d = " + d + ", i = " + i); 127 assertTrue("c.equals(d) ", c.equals(d)); 128 } 129 //System.out.println("coeffCache = " + ones.coeffCache); 130 //System.out.println("zeroCache = " + ones.zeroCache); 131 //System.out.println("homCheck = " + ones.homCheck); 132 //System.out.println("homCheck = " + ones.homCheck.length()); 133 assertTrue("zeroCache is empty", ones.zeroCache.isEmpty()); 134 assertTrue("#coeffCache = " + m, ones.coeffCache.size() == (m + 1)); 135 assertTrue("#homCheck = " + m, ones.homCheck.length() == (m + 1)); 136 137 for (int i = 0; i <= m; i++) { 138 GenPolynomial<BigRational> c = vars.getHomPart(i); 139 //System.out.println("c = " + c + ", i = " + i); 140 assertTrue("c==0 || deg(c)==1 ", c.isZERO() || c.degree() == 1L); 141 } 142 //System.out.println("coeffCache = " + vars.coeffCache); 143 //System.out.println("zeroCache = " + vars.zeroCache); 144 //System.out.println("homCheck = " + vars.homCheck); 145 //System.out.println("homCheck = " + vars.homCheck.length()); 146 //no-more: assertTrue("zeroCache is not empty", !vars.zeroCache.isEmpty()); 147 assertTrue("#coeffCache = " + m, vars.coeffCache.size() == (m + 1)); 148 assertTrue("#homCheck = " + m, vars.homCheck.length() == (m + 1)); 149 } 150 151 152 /** 153 * Test constructor and generators. 154 */ 155 public void testConstruction() { 156 //System.out.println("fac = " + fac); 157 //System.out.println("fac = " + fac.toScript()); 158 159 c = fac.getONE(); 160 //System.out.println("c = " + c); 161 assertTrue("isZERO( c )", !c.isZERO()); 162 assertTrue("isONE( c )", c.isONE()); 163 164 d = fac.getZERO(); 165 //System.out.println("d = " + d); 166 assertTrue("isZERO( d )", d.isZERO()); 167 assertTrue("isONE( d )", !d.isONE()); 168 169 List<MultiVarPowerSeries<BigRational>> gens = fac.generators(); 170 assertTrue("#gens == rl+1 ", rl + 1 == gens.size()); 171 for (MultiVarPowerSeries<BigRational> p : gens) { 172 //System.out.println("p = " + p); 173 assertTrue("red(p) == 0 ", p.reductum().isZERO()); 174 } 175 176 a = fac.copy(c); 177 b = c.copy(); 178 assertEquals("copy(c) == c.clone() ", a, b); 179 180 a = fac.fromInteger(1); 181 assertEquals("1 == fromInteger(1) ", a, c); 182 183 b = fac.fromInteger(java.math.BigInteger.ONE); 184 assertEquals("1 == fromInteger(1) ", b, c); 185 186 e = fac.generate((i) -> i.isZERO() ? cfac.getONE() : cfac.getZERO()); 187 //System.out.println("e = " + e); 188 assertTrue("isZERO( e )", !e.isZERO()); 189 assertTrue("isONE( e )", e.isONE()); 190 } 191 192 193 /** 194 * Test random polynomial. 195 */ 196 public void testRandom() { 197 for (int i = 0; i < 5; i++) { 198 a = fac.random(i + 2); 199 //System.out.println("a = " + a); 200 assertTrue(" not isZERO( a" + i + " )", !a.isZERO()); 201 assertTrue(" not isONE( a" + i + " )", !a.isONE()); 202 } 203 } 204 205 206 /** 207 * Test MultiVarCoefficients in power series. 208 */ 209 public void testCoefficientsInPS() { 210 211 MultiVarCoefficients<BigRational> zeros = new Zeros(fac); 212 MultiVarCoefficients<BigRational> ones = new Ones(fac); 213 MultiVarCoefficients<BigRational> vars = new Vars(fac); 214 215 a = new MultiVarPowerSeries<BigRational>(fac, zeros); 216 b = new MultiVarPowerSeries<BigRational>(fac, ones); 217 c = new MultiVarPowerSeries<BigRational>(fac, vars); 218 219 int m = 5; 220 ExpVectorIterable eiter = new ExpVectorIterable(rl, true, m); 221 for (ExpVector e : eiter) { 222 BigRational r = a.coefficient(e); 223 //System.out.println("r = " + r + ", e = " + e); 224 assertTrue("isZERO( r )", r.isZERO()); 225 } 226 //System.out.println("#a = " + a.lazyCoeffs.coeffCache); 227 assertTrue("coeffCache is one element", a.lazyCoeffs.coeffCache.size() == (m + 1)); 228 assertTrue("isZERO( a )", a.isZERO()); // after previous 229 230 for (ExpVector e : eiter) { 231 BigRational r = b.coefficient(e); 232 //System.out.println("r = " + r + ", e = " + e); 233 assertTrue("isONE( r )", r.isONE()); 234 } 235 assertTrue("zeroCache is empty", b.lazyCoeffs.zeroCache.isEmpty()); 236 237 for (int i = 0; i <= m; i++) { 238 GenPolynomial<BigRational> p = b.homogeneousPart(i); 239 //System.out.println("p = " + p + ", i = " + i); 240 GenPolynomial<BigRational> q = b.homogeneousPart(i); 241 //System.out.println("q = " + q + ", i = " + i); 242 assertTrue("p.equals(q) ", p.equals(q)); 243 } 244 assertTrue("zeroCache is empty", b.lazyCoeffs.zeroCache.isEmpty()); 245 assertTrue("#coeffCache = " + m, b.lazyCoeffs.coeffCache.size() == (m + 1)); 246 assertTrue("#homCheck = " + m, b.lazyCoeffs.homCheck.length() == (m + 1)); 247 248 for (int i = 0; i <= m; i++) { 249 GenPolynomial<BigRational> p = c.homogeneousPart(i); 250 //System.out.println("p = " + p + ", i = " + i); 251 assertTrue("p==0 || deg(p)==1 ", p.isZERO() || p.degree() == 1L); 252 } 253 //no-more:assertTrue("zeroCache is not empty", !c.lazyCoeffs.zeroCache.isEmpty()); 254 assertTrue("#coeffCache = " + m, c.lazyCoeffs.coeffCache.size() == (m + 1)); 255 assertTrue("#homCheck = " + m, c.lazyCoeffs.homCheck.length() == (m + 1)); 256 } 257 258 259 /** 260 * Test addition. 261 */ 262 public void testAddition() { 263 a = fac.random(kl); 264 b = fac.random(kl); 265 266 c = a.sum(b); 267 d = b.sum(a); 268 assertEquals("a+b = b+a", c, d); 269 270 d = c.subtract(b); 271 assertEquals("a+b-b = a", a, d); 272 d = c.sum(b.negate()); 273 assertEquals("a+b-b = a", a, d); 274 275 c = fac.random(kl); 276 d = a.sum(b.sum(c)); 277 e = a.sum(b).sum(c); 278 assertEquals("a+(b+c) = (a+b)+c", d, e); 279 280 Map.Entry<ExpVector, BigRational> ma = a.orderMonomial(); 281 c = a.reductum().sum(ma); 282 assertEquals("a = red(a)+om(a)", a, c); 283 } 284 285 286 /** 287 * Test multiplication. 288 */ 289 public void testMultiplication() { 290 a = fac.random(kl); 291 b = fac.random(kl); 292 293 if (a.isZERO() || b.isZERO()) { 294 return; 295 } 296 assertTrue("not isZERO( a )", !a.isZERO()); 297 assertTrue("not isZERO( b )", !b.isZERO()); 298 299 c = b.multiply(a); 300 d = a.multiply(b); 301 assertTrue("not isZERO( c )", !c.isZERO()); 302 assertTrue("not isZERO( d )", !d.isZERO()); 303 304 //System.out.println("a = " + a); 305 //System.out.println("b = " + b); 306 e = d.subtract(c); 307 assertTrue("isZERO( a*b-b*a ) " + e, e.isZERO()); 308 309 assertTrue("a*b = b*a", c.equals(d)); 310 assertEquals("a*b = b*a", c, d); 311 312 c = fac.random(kl); 313 //System.out.println("c = " + c); 314 d = a.multiply(b.multiply(c)); 315 e = (a.multiply(b)).multiply(c); 316 //System.out.println("d = " + d); 317 //System.out.println("e = " + e); 318 //System.out.println("d-e = " + d.subtract(c) ); 319 320 assertEquals("a(bc) = (ab)c", d, e); 321 assertTrue("a(bc) = (ab)c", d.equals(e)); 322 323 ExpVector ev = ExpVector.random(rl, 5, 0.8f); 324 BigRational br = fac.coFac.random(5); 325 326 b = a.shift(ev).multiply(br); 327 c = a.multiply(br, ev); 328 assertEquals("(a ev) br = a (ev,br)", b, c); 329 //System.out.println("a = " + a); 330 //System.out.println("ev = " + ev); 331 //System.out.println("br = " + br); 332 //System.out.println("b = " + b); 333 //System.out.println("c = " + c); 334 } 335 336 337 /** 338 * Test distributive law. 339 */ 340 public void testDistributive() { 341 a = fac.random(kl, q); 342 b = fac.random(kl, q); 343 c = fac.random(kl, q); 344 345 d = a.multiply(b.sum(c)); 346 e = a.multiply(b).sum(a.multiply(c)); 347 348 assertEquals("a(b+c) = ab+ac", d, e); 349 } 350 351 352 /** 353 * Test inverse. 354 */ 355 public void testInverse() { 356 a = fac.getONE(); 357 assertTrue("not isZERO( a )", !a.isZERO()); 358 assertTrue("isUnit( a )", a.isUnit()); 359 //System.out.println("a = " + a); 360 361 b = a.inverse(); 362 c = a.multiply(b); 363 assertTrue("isONE( c )", c.isONE()); 364 //System.out.println("b = " + b); 365 //System.out.println("c = " + c); 366 367 a = fac.random(kl); 368 if (!a.isUnit()) { 369 a = fac.fromInteger(23); //return; 370 } 371 //System.out.println("a = " + a); 372 b = a.inverse(); 373 c = a.multiply(b); 374 assertTrue("isONE( c )", c.isONE()); 375 //System.out.println("b = " + b); 376 //System.out.println("c = " + c); 377 378 b = fac.random(kl); 379 c = b.divide(a); 380 d = c.multiply(a); 381 assertEquals("b/a * a == b ", d, b); 382 } 383 384 385 /** 386 * Test fix point constructions. 387 */ 388 public void testFixpoints() { 389 int r = 0; 390 UnivPowerSeriesRing<BigRational> ufac = new UnivPowerSeriesRing<BigRational>(fac.coFac, fac.vars[r]); 391 UnivPowerSeries<BigRational> exp = ufac.getEXP(); 392 //System.out.println("exp = " + exp); 393 394 a = fac.fromPowerSeries(exp, 0); 395 b = fac.fromPowerSeries(exp, 1); 396 //System.out.println("a = " + a); 397 //System.out.println("b = " + b); 398 399 c = fac.getEXP(0); 400 d = fac.getEXP(1); 401 //System.out.println("c = " + c); 402 //System.out.println("d = " + d); 403 assertEquals("a == c ", a, c); 404 assertEquals("b == d ", b, d); 405 406 e = d.differentiate(0); 407 //System.out.println("e = " + e); 408 assertTrue("isZERO( e )", e.isZERO()); 409 e = d.differentiate(1); 410 //System.out.println("e = " + e); 411 assertEquals("e == d ", d, e); 412 } 413 414 415 /** 416 * Test reductum. 417 */ 418 public void testReductum() { 419 a = fac.random(kl); 420 //System.out.println("a = " + a); 421 422 Map.Entry<ExpVector, BigRational> m = a.orderMonomial(); 423 //System.out.println("m = " + m); 424 ExpVector k = m.getKey(); 425 BigRational br = m.getValue(); 426 427 b = fac.getONE().multiply(br, k); 428 //System.out.println("b = " + b); 429 430 c = a.reductum(); 431 //System.out.println("c = " + c); 432 433 d = c.sum(b); 434 //System.out.println("d = " + d); 435 assertEquals("a = red(a)+1*lm(a) ", a, d); 436 437 e = c.sum(br, k); 438 //System.out.println("e = " + e); 439 assertEquals("a = red(a)+lm(a) ", a, e); 440 441 e = a.subtract(br, k); 442 //System.out.println("e = " + e); 443 assertEquals("a - lm(a) = red(a) ", c, e); 444 445 b = fac.random(kl); 446 String s = b.toString(); // generate and cache some coefficients 447 //System.out.println("b = " + b); 448 assertFalse("s.size > 0 " + s, s.length() == 0); // java-5 449 450 c = a.sum(b); 451 //System.out.println("c = " + c); 452 453 d = a.sum(b.lazyCoeffs); 454 //System.out.println("d = " + d); 455 456 // while ( !c.isZERO() ) { 457 // c = c.reductum(); 458 // //System.out.println("c = " + c); 459 // } 460 // assertTrue("red^n(a) == 0 ", c.isZERO()); 461 462 br = new BigRational(2, 3); 463 c = a.prepend(br, 0); 464 d = c.reductum(0); 465 assertEquals("red(a + br_0,0) = a ", d, a); 466 467 c = a.shift(3, 0); 468 d = c.shift(-3, 0); 469 assertEquals("shift(shift(a,3,),-3,0) = a ", d, a); 470 } 471 472 473 /** 474 * Test polynomial constructions. 475 */ 476 public void testPolynomial() { 477 GenPolynomialRing<BigRational> pr = fac.polyRing(); 478 //System.out.println("pr = " + pr); 479 480 GenPolynomial<BigRational> p = pr.random(kl, 3, 3, q + q); 481 //System.out.println("p = " + p); 482 483 a = fac.fromPolynomial(p); 484 //System.out.println("a = " + a); 485 486 GenPolynomial<BigRational> s = a.asPolynomial(); 487 //System.out.println("s = " + s); 488 assertEquals("asPolynomial(fromPolynomial(p)) = p ", p, s); 489 // if ( a.isUnit() ) { 490 // b = a.inverse(); 491 // System.out.println("b = " + b); 492 // } 493 } 494 495 496 /** 497 * Test gcd. 498 */ 499 public void testGcd() { 500 a = fac.random(kl); 501 //System.out.println("a = " + a); 502 503 b = fac.random(kl); 504 //System.out.println("b = " + b); 505 506 c = a.gcd(b); 507 //System.out.println("c = " + c); 508 509 d = a.divide(c); 510 //System.out.println("d = " + d); 511 512 e = b.divide(c); 513 //System.out.println("e = " + e); 514 515 f = d.gcd(e); 516 //System.out.println("f = " + f); 517 assertTrue("gcd(a/gcd(a,b),b/gcd(a,b)) == 1 ", f.isONE()); 518 } 519 520 521 /** 522 * Test Taylor series. 523 */ 524 public void testTaylor() { 525 BigRational ar = new BigRational(5); 526 BigRational br = new BigRational(0); 527 BigRational cr = new BigRational(-5); 528 List<BigRational> Ar = new ArrayList<BigRational>(rl); 529 List<BigRational> Br = new ArrayList<BigRational>(rl); 530 List<BigRational> Cr = new ArrayList<BigRational>(rl); 531 for (int i = 0; i < rl; i++) { 532 Ar.add(ar); 533 Br.add(br); 534 Cr.add(cr); 535 } 536 GenPolynomialRing<BigRational> pr = fac.polyRing(); 537 //System.out.println("pr = " + pr.toScript()); 538 539 GenPolynomial<BigRational> p = pr.random(kl, 3, 3, q + q); 540 //System.out.println("p = " + p); 541 int tdeg = (int) p.degree(); 542 fac.setTruncate(tdeg + 1); 543 544 TaylorFunction<BigRational> F = new PolynomialTaylorFunction<BigRational>(p); 545 546 MultiVarPowerSeries<BigRational> pps = fac.fromPolynomial(p); 547 //System.out.println("pps = " + pps); 548 MultiVarPowerSeries<BigRational> ps = fac.seriesOfTaylor(F, Br); 549 //System.out.println("ps = " + ps); 550 assertEquals("taylor(p) == p", ps, pps); 551 552 MultiVarPowerSeries<BigRational> psa = fac.seriesOfTaylor(F, Ar); 553 //System.out.println("psa = " + psa); 554 F = new PolynomialTaylorFunction<BigRational>(psa.asPolynomial()); 555 MultiVarPowerSeries<BigRational> psc = fac.seriesOfTaylor(F, Cr); 556 //System.out.println("psc = " + psc); 557 assertEquals("taylor(taylor(p,5),-5) == p", ps, psc); 558 559 for (GenPolynomial<BigRational> g : pr.generators()) { 560 F = new PolynomialTaylorFunction<BigRational>(g); 561 ps = fac.seriesOfTaylor(F, Br); 562 //System.out.println("g = " + g); 563 //System.out.println("ps = " + ps); 564 pps = fac.fromPolynomial(g); 565 //System.out.println("pps = " + pps); 566 assertEquals("taylor(p) == p", ps, pps); 567 568 psa = fac.seriesOfTaylor(F, Ar); 569 //System.out.println("psa = " + psa); 570 F = new PolynomialTaylorFunction<BigRational>(psa.asPolynomial()); 571 psc = fac.seriesOfTaylor(F, Cr); 572 //System.out.println("psc = " + psc); 573 assertEquals("taylor(taylor(p,5),-5) == p", ps, psc); 574 } 575 } 576 577 578 /** 579 * Test evaluation. 580 */ 581 public void testEvaluation() { 582 a = fac.random(kl, q); 583 b = fac.random(kl, q); 584 BigRational fv = new BigRational(0); 585 List<BigRational> v = new ArrayList<BigRational>(rl); 586 for (int i = 0; i < rl; i++) { 587 v.add(fv.random(kl)); 588 } 589 590 BigRational av = a.evaluate(v); 591 BigRational bv = b.evaluate(v); 592 593 c = a.sum(b); 594 BigRational cv = c.evaluate(v); 595 BigRational dv = av.sum(bv); 596 597 assertEquals("a(v)+b(v) = (a+b)(v) ", cv, dv); 598 599 c = fac.getZERO(); 600 cv = c.evaluate(v); 601 dv = fv.getZERO(); 602 assertEquals("0(v) = 0 ", cv, dv); 603 604 c = fac.getONE(); 605 cv = c.evaluate(v); 606 dv = fv.getONE(); 607 assertEquals("1(v) = 1 ", cv, dv); 608 } 609} 610 611 612class Zeros extends MultiVarCoefficients<BigRational> { 613 614 615 public Zeros(MultiVarPowerSeriesRing<BigRational> pf) { 616 super(pf); 617 } 618 619 620 public Zeros(GenPolynomialRing<BigRational> pf) { 621 super(pf); 622 } 623 624 625 @Override 626 public BigRational generate(ExpVector i) { 627 return pfac.coFac.getZERO(); 628 } 629 630} 631 632 633class Ones extends MultiVarCoefficients<BigRational> { 634 635 636 public Ones(MultiVarPowerSeriesRing<BigRational> pf) { 637 super(pf); 638 } 639 640 641 public Ones(GenPolynomialRing<BigRational> pf) { 642 super(pf); 643 } 644 645 646 @Override 647 public BigRational generate(ExpVector i) { 648 return pfac.coFac.getONE(); 649 } 650 651} 652 653 654class Vars extends MultiVarCoefficients<BigRational> { 655 656 657 public Vars(MultiVarPowerSeriesRing<BigRational> pf) { 658 super(pf); 659 } 660 661 662 public Vars(GenPolynomialRing<BigRational> pf) { 663 super(pf); 664 } 665 666 667 @Override 668 public BigRational generate(ExpVector i) { 669 int[] v = i.dependencyOnVariables(); 670 if (v.length == 1 && i.getVal(v[0]) == 1L) { 671 return pfac.coFac.getONE(); 672 } 673 return pfac.coFac.getZERO(); 674 } 675 676}