001/* 002 * $Id: ArithTest.java 4071 2012-07-27 19:59:38Z kredel $ 003 */ 004 005package edu.jas.arith; 006 007 008import junit.framework.Test; 009import junit.framework.TestCase; 010import junit.framework.TestSuite; 011 012import edu.jas.structure.Power; 013 014 015/** 016 * Basic arithmetic tests with JUnit. 017 * @author Heinz Kredel. 018 */ 019 020public class ArithTest extends TestCase { 021 022 023 /** 024 * main. 025 */ 026 public static void main(String[] args) { 027 junit.textui.TestRunner.run(suite()); 028 } 029 030 031 /** 032 * Constructs a <CODE>ArithTest</CODE> object. 033 * @param name String. 034 */ 035 public ArithTest(String name) { 036 super(name); 037 } 038 039 040 /** 041 * suite. 042 */ 043 public static Test suite() { 044 TestSuite suite = new TestSuite(ArithTest.class); 045 return suite; 046 } 047 048 049 /* 050 RingElem a; 051 RingElem b; 052 RingElem c; 053 RingElem d; 054 RingElem e; 055 */ 056 057 @Override 058 protected void setUp() { 059 //a = b = c = d = e = null; 060 } 061 062 063 @Override 064 protected void tearDown() { 065 //a = b = c = d = e = null; 066 } 067 068 069 /** 070 * Test static initialization and constants for BigInteger. 071 */ 072 public void testIntegerConstants() { 073 BigInteger a, b, c, d; 074 a = BigInteger.ZERO; 075 b = BigInteger.ONE; 076 c = b.subtract(b); 077 078 assertTrue("0.isZERO()", a.isZERO()); 079 assertTrue("1.isONE", b.isONE()); 080 081 assertEquals("1-1 = 0", c, a); 082 assertTrue("(1-1).isZERO()", c.isZERO()); 083 084 d = b.multiply(b); 085 assertTrue("1*1 = 1", d.isONE()); 086 087 d = b.multiply(a); 088 assertTrue("1*0 = 0", d.isZERO()); 089 } 090 091 092 //-------------------------------------------------------- 093 094 095 /** 096 * Test string constructor and toString for BigInteger. 097 */ 098 public void testIntegerConstructor() { 099 BigInteger a, b, c, d; 100 a = new BigInteger(1); 101 b = new BigInteger(-1); 102 c = new BigInteger(0); 103 104 d = a.sum(b); 105 assertTrue("'1'.isONE()", a.isONE()); 106 assertTrue("1+(-1) = 0", d.isZERO()); 107 d = a.negate(); 108 assertEquals("-1 = -(1)", d, b); 109 110 d = a.multiply(c); 111 assertTrue("'0'.isZERO()", d.isZERO()); 112 d = b.multiply(b); 113 assertTrue("(-1)*(-1) = 1", d.isONE()); 114 115 a = new BigInteger(3); 116 b = new BigInteger("3"); 117 assertEquals("3 = '3'", a, b); 118 119 a = new BigInteger(-5); 120 b = new BigInteger("-5"); 121 assertEquals("-5 = '-5'", a, b); 122 123 // 0 1 2 3 4 124 // 0123456789012345678901234567890123456789012345 125 String s = "1111111111111111111111111111111111111111111111"; 126 a = new BigInteger(s); 127 String t = a.toString(); 128 assertEquals("stringConstr = toString", s, t); 129 } 130 131 132 //-------------------------------------------------------- 133 134 /** 135 * Test random and compares Integer. 136 * 137 */ 138 public void testIntegerRandom() { 139 BigInteger a, b, c; 140 a = BigInteger.ZERO.random(500); 141 b = new BigInteger("" + a); 142 c = b.subtract(a); 143 144 assertTrue("a-'a' = 0", c.isZERO()); 145 assertEquals("compareTo('a',a) = 0", 0, b.compareTo(a)); 146 assertEquals("signum('a'-a) = 0", 0, c.signum()); 147 } 148 149 150 //-------------------------------------------------------- 151 152 153 /** 154 * Test addition for Integer. 155 * 156 */ 157 public void testIntegerAddition() { 158 BigInteger a, b, c, d, e; 159 // neutral element 160 a = BigInteger.ZERO.random(500); 161 d = a.sum(BigInteger.ZERO); 162 assertEquals("a+0 = a", d, a); 163 d = a.subtract(BigInteger.ZERO); 164 assertEquals("a-0 = a", d, a); 165 166 // inverse operations 167 b = a.sum(a); 168 c = b.subtract(a); 169 assertEquals("(a+a)-a = a", c, a); 170 b = a.subtract(a); 171 c = b.sum(a); 172 assertEquals("(a-a)+a = a", c, a); 173 174 // comutativity 175 b = BigInteger.ZERO.random(500); 176 c = a.sum(b); 177 d = b.sum(a); 178 assertEquals("a+b = b+a", c, d); 179 180 // negation 181 c = a.subtract(b); 182 d = a.sum(b.negate()); 183 assertEquals("a-b = a+(-b)", c, d); 184 185 // associativity 186 c = BigInteger.ZERO.random(500); 187 d = a.sum(b.sum(c)); 188 e = a.sum(b).sum(c); 189 assertEquals("a+(b+c) = (a+b)+c", d, e); 190 } 191 192 193 //-------------------------------------------------------- 194 195 196 /** 197 * Test multiplication for Integer. 198 * 199 */ 200 public void testIntegerMultiplication() { 201 BigInteger a, b, c, d, e; 202 // neutral element 203 a = BigInteger.ZERO.random(500); 204 d = a.multiply(BigInteger.ONE); 205 assertEquals("a*1 = a", d, a); 206 d = a.divide(BigInteger.ONE); 207 assertEquals("a/1 = a", d, a); 208 209 // inverse operations 210 b = a.multiply(a); 211 c = b.divide(a); 212 assertEquals("(a*a)/a = a", c, a); 213 b = a.divide(a); 214 c = b.multiply(a); 215 assertEquals("(a/a)*a = a", c, a); 216 217 // comutativity 218 b = BigInteger.ZERO.random(500); 219 c = a.multiply(b); 220 d = b.multiply(a); 221 assertEquals("a*b = b*a", c, d); 222 223 // inverse 224 d = c.divide(b); 225 // e = c.multiply( b.inverse() ); 226 e = a; 227 assertEquals("a/b = a*(1/b)", d, e); 228 229 // associativity 230 c = BigInteger.ZERO.random(500); 231 d = a.multiply(b.multiply(c)); 232 e = a.multiply(b).multiply(c); 233 assertEquals("a*(b*c) = (a*b)*c", d, e); 234 } 235 236 237 /** 238 * Test static initialization and constants for BigRational. 239 * 240 */ 241 public void testRationalConstants() { 242 BigRational a, b, c, d; 243 a = BigRational.ZERO; 244 b = BigRational.ONE; 245 //System.out.println("a = " + a); 246 //System.out.println("b = " + b); 247 c = b.subtract(b); 248 249 assertTrue("0.isZERO()", a.isZERO()); 250 assertTrue("1.isONE", b.isONE()); 251 252 assertEquals("1-1 = 0", c, a); 253 assertTrue("(1-1).isZERO()", c.isZERO()); 254 255 d = b.multiply(b); 256 assertTrue("1*1 = 1", d.isONE()); 257 258 d = b.multiply(a); 259 assertTrue("1*0 = 0", d.isZERO()); 260 } 261 262 263 /** 264 * Test static initialization and constants for BigComplex. 265 * 266 */ 267 public void testComplexConstants() { 268 BigComplex a, b, c, d; 269 a = BigComplex.ZERO; 270 b = BigComplex.ONE; 271 c = b.subtract(b); 272 273 assertTrue("0.isZERO()", a.isZERO()); 274 assertTrue("1.isONE", b.isONE()); 275 276 assertEquals("1-1 = 0", c, a); 277 assertTrue("(1-1).isZERO()", c.isZERO()); 278 279 d = b.multiply(b); 280 assertTrue("1*1 = 1", d.isONE()); 281 282 d = b.multiply(a); 283 assertTrue("1*0 = 0", d.isZERO()); 284 } 285 286 287 /** 288 * Test static initialization and constants for BigQuaternion. 289 * 290 */ 291 public void testQuaternionConstants() { 292 BigQuaternion a, b, c, d; 293 a = BigQuaternion.ZERO; 294 b = BigQuaternion.ONE; 295 c = b.subtract(b); 296 297 assertTrue("0.isZERO()", a.isZERO()); 298 assertTrue("1.isONE", b.isONE()); 299 300 assertEquals("1-1 = 0", c, a); 301 assertTrue("(1-1).isZERO()", c.isZERO()); 302 303 d = b.multiply(b); 304 assertTrue("1*1 = 1", d.isONE()); 305 306 d = b.multiply(a); 307 assertTrue("1*0 = 0", d.isZERO()); 308 } 309 310 311 //-------------------------------------------------------- 312 313 314 /** 315 * Test string constructor and toString for BigRational. 316 * 317 */ 318 public void testRationalConstructor() { 319 BigRational a, b, c, d; 320 a = new BigRational(1); 321 b = new BigRational(-1); 322 c = new BigRational(0); 323 324 d = a.sum(b); 325 assertTrue("'1'.isONE()", a.isONE()); 326 assertTrue("1+(-1) = 0", d.isZERO()); 327 d = a.negate(); 328 assertEquals("-1 = -(1)", d, b); 329 330 d = a.multiply(c); 331 assertTrue("'0'.isZERO()", d.isZERO()); 332 d = b.multiply(b); 333 assertTrue("(-1)*(-1) = 1", d.isONE()); 334 335 a = new BigRational(3); 336 b = new BigRational("3"); 337 assertEquals("3 = '3'", a, b); 338 339 a = new BigRational(-5); 340 b = new BigRational("-5"); 341 assertEquals("-5 = '-5'", a, b); 342 343 // 0 1 2 3 4 344 // 0123456789012345678901234567890123456789012345 345 String s = "1111111111111111111111111111111111111111111111"; 346 a = new BigRational(s); 347 String t = a.toString(); 348 assertEquals("stringConstr = toString", s, t); 349 350 s = "2/4"; 351 a = new BigRational(s); 352 t = a.toString(5); 353 //System.out.println("a = " + a); 354 //System.out.println("t = " + t); 355 String r = "0.5"; 356 assertEquals("stringConstr = toString", r, t); 357 } 358 359 360 /** 361 * Test string constructor and toString for BigComplex. 362 * 363 */ 364 public void testComplexConstructor() { 365 BigComplex a, b, c, d; 366 a = new BigComplex(1); 367 b = new BigComplex(-1); 368 c = new BigComplex(0); 369 370 d = a.sum(b); 371 assertTrue("'1'.isONE()", a.isONE()); 372 assertTrue("1+(-1) = 0", d.isZERO()); 373 d = a.negate(); 374 assertEquals("-1 = -(1)", d, b); 375 376 d = a.multiply(c); 377 assertTrue("'0'.isZERO()", d.isZERO()); 378 d = b.multiply(b); 379 assertTrue("(-1)*(-1) = 1", d.isONE()); 380 381 a = new BigComplex(3); 382 b = new BigComplex("3"); 383 assertEquals("3 = '3'", a, b); 384 385 a = new BigComplex(-5); 386 b = new BigComplex("-5"); 387 assertEquals("-5 = '-5'", a, b); 388 389 // 0 1 2 3 4 390 // 0123456789012345678901234567890123456789012345 391 String s = "1111111111111111111111111111111111111111111111"; 392 a = new BigComplex(s); 393 String t = a.toString(); 394 assertEquals("stringConstr = toString", s, t); 395 } 396 397 398 /** 399 * Test string constructor and toString for BigQuaternion. 400 * 401 */ 402 public void testQuaternionConstructor() { 403 BigQuaternion a, b, c, d; 404 a = new BigQuaternion(1); 405 b = new BigQuaternion(-1); 406 c = new BigQuaternion(0); 407 408 d = a.sum(b); 409 assertTrue("'1'.isONE()", a.isONE()); 410 assertTrue("1+(-1) = 0", d.isZERO()); 411 d = a.negate(); 412 assertEquals("-1 = -(1)", d, b); 413 414 d = a.multiply(c); 415 assertTrue("'0'.isZERO()", d.isZERO()); 416 d = b.multiply(b); 417 assertTrue("(-1)*(-1) = 1", d.isONE()); 418 419 a = new BigQuaternion(3); 420 b = new BigQuaternion("3"); 421 assertEquals("3 = '3'", a, b); 422 423 a = new BigQuaternion(-5); 424 b = new BigQuaternion("-5"); 425 assertEquals("-5 = '-5'", a, b); 426 427 // 0 1 2 3 4 428 // 0123456789012345678901234567890123456789012345 429 String s = "1111111111111111111111111111111111111111111111"; 430 a = new BigQuaternion(s); 431 String t = a.toString(); 432 assertEquals("stringConstr = toString", s, t); 433 } 434 435 436 //-------------------------------------------------------- 437 438 439 /** 440 * Test random and compares Rational. 441 * 442 */ 443 public void testRationalRandom() { 444 BigRational a, b, c; 445 a = BigRational.ZERO.random(500); 446 b = new BigRational("" + a); 447 c = b.subtract(a); 448 449 assertTrue("a-'a' = 0", c.isZERO()); 450 assertEquals("compareTo('a',a) = 0", 0, b.compareTo(a)); 451 assertEquals("signum('a'-a) = 0", 0, c.signum()); 452 } 453 454 455 /** 456 * Test random and compares Complex. 457 * 458 */ 459 public void testComplexRandom() { 460 BigComplex a, b, c; 461 a = BigComplex.ZERO.random(500); 462 b = new BigComplex("" + a); 463 c = b.subtract(a); 464 465 assertTrue("a-'a' = 0", c.isZERO()); 466 assertEquals("compareTo('a',a) = 0", 0, b.compareTo(a)); 467 assertEquals("signum('a'-a) = 0", 0, c.signum()); 468 } 469 470 471 /** 472 * Test random and compares Quaternion. 473 * 474 */ 475 public void testQuaternionRandom() { 476 BigQuaternion a, b, c; 477 a = BigQuaternion.ZERO.random(500); 478 b = new BigQuaternion("" + a); 479 c = b.subtract(a); 480 481 assertTrue("a-'a' = 0", c.isZERO()); 482 assertEquals("signum('a'-a) = 0", 0, c.signum()); 483 assertEquals("compareTo('a',a) = 0", 0, b.compareTo(a)); 484 } 485 486 487 //-------------------------------------------------------- 488 489 490 /** 491 * Test addition for Rational. 492 * 493 */ 494 public void testRationalAddition() { 495 BigRational a, b, c, d, e; 496 // neutral element 497 a = BigRational.ZERO.random(500); 498 d = a.sum(BigRational.ZERO); 499 assertEquals("a+0 = a", d, a); 500 d = a.subtract(BigRational.ZERO); 501 assertEquals("a-0 = a", d, a); 502 503 // inverse operations 504 b = a.sum(a); 505 c = b.subtract(a); 506 assertEquals("(a+a)-a = a", c, a); 507 b = a.subtract(a); 508 c = b.sum(a); 509 assertEquals("(a-a)+a = a", c, a); 510 511 // comutativity 512 b = BigRational.ZERO.random(500); 513 c = a.sum(b); 514 d = b.sum(a); 515 assertEquals("a+b = b+a", c, d); 516 517 // negation 518 c = a.subtract(b); 519 d = a.sum(b.negate()); 520 assertEquals("a-b = a+(-b)", c, d); 521 522 // associativity 523 c = BigRational.ZERO.random(500); 524 d = a.sum(b.sum(c)); 525 e = a.sum(b).sum(c); 526 assertEquals("a+(b+c) = (a+b)+c", d, e); 527 } 528 529 530 /** 531 * Test addition for Complex. 532 * 533 */ 534 public void testComplexAddition() { 535 BigComplex a, b, c, d, e; 536 // neutral element 537 a = BigComplex.ZERO.random(500); 538 d = a.sum(BigComplex.ZERO); 539 assertEquals("a+0 = a", d, a); 540 d = a.subtract(BigComplex.ZERO); 541 assertEquals("a-0 = a", d, a); 542 543 // inverse operations 544 b = a.sum(a); 545 c = b.subtract(a); 546 assertEquals("(a+a)-a = a", c, a); 547 b = a.subtract(a); 548 c = b.sum(a); 549 assertEquals("(a-a)+a = a", c, a); 550 551 // comutativity 552 b = BigComplex.ZERO.random(500); 553 c = a.sum(b); 554 d = b.sum(a); 555 assertEquals("a+b = b+a", c, d); 556 557 // negation 558 c = a.subtract(b); 559 d = a.sum(b.negate()); 560 assertEquals("a-b = a+(-b)", c, d); 561 562 // associativity 563 c = BigComplex.ZERO.random(500); 564 d = a.sum(b.sum(c)); 565 e = a.sum(b).sum(c); 566 assertEquals("a+(b+c) = (a+b)+c", d, e); 567 } 568 569 570 /** 571 * Test addition for Quaternion. 572 * 573 */ 574 public void testQuaternionAddition() { 575 BigQuaternion a, b, c, d, e; 576 // neutral element 577 a = BigQuaternion.ZERO.random(500); 578 d = a.sum(BigQuaternion.ZERO); 579 assertEquals("a+0 = a", d, a); 580 d = a.subtract(BigQuaternion.ZERO); 581 assertEquals("a-0 = a", d, a); 582 583 // inverse operations 584 b = a.sum(a); 585 c = b.subtract(a); 586 assertEquals("(a+a)-a = a", c, a); 587 b = a.subtract(a); 588 c = b.sum(a); 589 assertEquals("(a-a)+a = a", c, a); 590 591 // comutativity 592 b = BigQuaternion.ZERO.random(500); 593 c = a.sum(b); 594 d = b.sum(a); 595 assertEquals("a+b = b+a", c, d); 596 597 // negation 598 c = a.subtract(b); 599 d = a.sum(b.negate()); 600 assertEquals("a-b = a+(-b)", c, d); 601 602 // associativity 603 c = BigQuaternion.ZERO.random(500); 604 d = a.sum(b.sum(c)); 605 e = a.sum(b).sum(c); 606 assertEquals("a+(b+c) = (a+b)+c", d, e); 607 } 608 609 610 //-------------------------------------------------------- 611 612 613 /** 614 * Test multiplication for Rational. 615 * 616 */ 617 public void testRationalMultiplication() { 618 BigRational a, b, c, d, e; 619 // neutral element 620 a = BigRational.ZERO.random(500); 621 d = a.multiply(BigRational.ONE); 622 assertEquals("a*1 = a", d, a); 623 d = a.divide(BigRational.ONE); 624 assertEquals("a/1 = a", d, a); 625 626 // inverse operations 627 b = a.multiply(a); 628 c = b.divide(a); 629 assertEquals("(a*a)/a = a", c, a); 630 b = a.divide(a); 631 c = b.multiply(a); 632 assertEquals("(a/a)*a = a", c, a); 633 634 // comutativity 635 b = BigRational.ZERO.random(500); 636 c = a.multiply(b); 637 d = b.multiply(a); 638 assertEquals("a*b = b*a", c, d); 639 640 // inverse 641 d = c.divide(b); 642 e = c.multiply(b.inverse()); 643 //e = a; 644 assertEquals("a/b = a*(1/b)", d, e); 645 646 // associativity 647 c = BigRational.ZERO.random(500); 648 d = a.multiply(b.multiply(c)); 649 e = a.multiply(b).multiply(c); 650 assertEquals("a*(b*c) = (a*b)*c", d, e); 651 } 652 653 654 /** 655 * Test multiplication for Complex. 656 * 657 */ 658 public void testComplexMultiplication() { 659 BigComplex a, b, c, d, e; 660 // neutral element 661 a = BigComplex.ZERO.random(500); 662 d = a.multiply(BigComplex.ONE); 663 assertEquals("a*1 = a", d, a); 664 d = a.divide(BigComplex.ONE); 665 assertEquals("a/1 = a", d, a); 666 667 // inverse operations 668 b = a.multiply(a); 669 c = b.divide(a); 670 assertEquals("(a*a)/a = a", c, a); 671 b = a.divide(a); 672 c = b.multiply(a); 673 assertEquals("(a/a)*a = a", c, a); 674 675 // comutativity 676 b = BigComplex.ZERO.random(500); 677 c = a.multiply(b); 678 d = b.multiply(a); 679 assertEquals("a*b = b*a", c, d); 680 681 // inverse 682 d = c.divide(b); 683 e = c.multiply(b.inverse()); 684 //e = a; 685 assertEquals("a/b = a*(1/b)", d, e); 686 687 // associativity 688 c = BigComplex.ZERO.random(500); 689 d = a.multiply(b.multiply(c)); 690 e = a.multiply(b).multiply(c); 691 assertEquals("a*(b*c) = (a*b)*c", d, e); 692 } 693 694 695 /** 696 * Test multiplication for Quaternion. 697 * 698 */ 699 public void testQuaternionMultiplication() { 700 BigQuaternion a, b, c, d, e; 701 // neutral element 702 a = BigQuaternion.ZERO.random(500); 703 d = a.multiply(BigQuaternion.ONE); 704 assertEquals("a*1 = a", d, a); 705 d = a.divide(BigQuaternion.ONE); 706 assertEquals("a/1 = a", d, a); 707 708 // inverse operations 709 b = a.multiply(a); 710 c = b.divide(a); 711 assertEquals("(a*a)/a = a", c, a); 712 b = a.divide(a); 713 c = b.multiply(a); 714 assertEquals("(a/a)*a = a", c, a); 715 716 // inverse 717 b = BigQuaternion.ZERO.random(500); 718 c = b.multiply(a); 719 d = c.divide(b); 720 e = c.multiply(b.inverse()); 721 //e = a; 722 assertEquals("a/b = a*(1/b)", d, e); 723 724 // associativity 725 c = BigQuaternion.ZERO.random(500); 726 d = a.multiply(b.multiply(c)); 727 e = a.multiply(b).multiply(c); 728 assertEquals("a*(b*c) = (a*b)*c", d, e); 729 730 // non comutativity 731 a = BigQuaternion.I; 732 b = BigQuaternion.J; 733 c = a.multiply(b); 734 d = b.multiply(a); 735 assertEquals("I*J = -J*I", c, d.negate()); 736 a = BigQuaternion.I; 737 b = BigQuaternion.K; 738 c = a.multiply(b); 739 d = b.multiply(a); 740 assertEquals("I*K = -K*I", c, d.negate()); 741 a = BigQuaternion.J; 742 b = BigQuaternion.K; 743 c = a.multiply(b); 744 d = b.multiply(a); 745 assertEquals("J*K = -K*J", c, d.negate()); 746 } 747 748 749 /** 750 * Test power for Rational. 751 * 752 */ 753 public void testRationalPower() { 754 BigRational a, b, c, d; 755 a = BigRational.ZERO.random(500); 756 757 // power operations 758 b = Power.<BigRational> positivePower(a, 1); 759 assertEquals("a^1 = a", b, a); 760 761 Power<BigRational> pow = new Power<BigRational>(BigRational.ONE); 762 b = pow.power(a, 1); 763 assertEquals("a^1 = a", b, a); 764 765 b = pow.power(a, 2); 766 c = a.multiply(a); 767 assertEquals("a^2 = a*a", b, c); 768 769 d = pow.power(a, -2); 770 c = b.multiply(d); 771 assertTrue("a^2 * a^-2 = 1", c.isONE()); 772 773 b = pow.power(a, 3); 774 c = a.multiply(a).multiply(a); 775 assertEquals("a^3 = a*a*a", b, c); 776 777 d = pow.power(a, -3); 778 c = b.multiply(d); 779 assertTrue("a^3 * a^-3 = 1", c.isONE()); 780 } 781 782 783 /** 784 * Test power for Integer. 785 * 786 */ 787 public void testIntegerPower() { 788 BigInteger a, b, c, d, e; 789 a = BigInteger.ZERO.random(500); 790 791 // power operations 792 b = Power.<BigInteger> positivePower(a, 1); 793 assertEquals("a^1 = a", b, a); 794 795 Power<BigInteger> pow = new Power<BigInteger>(BigInteger.ONE); 796 b = pow.power(a, 1); 797 assertEquals("a^1 = a", b, a); 798 799 b = pow.power(a, 2); 800 c = a.multiply(a); 801 assertEquals("a^2 = a*a", b, c); 802 803 b = pow.power(a, 3); 804 c = a.multiply(a).multiply(a); 805 assertEquals("a^3 = a*a*a", b, c); 806 807 // mod power operations 808 a = new BigInteger(3); 809 b = Power.<BigInteger> positivePower(a, 1); 810 assertEquals("a^1 = a", b, a); 811 812 a = new BigInteger(11); 813 e = new BigInteger(2); 814 c = Power.<BigInteger> modPositivePower(a, 10, e); 815 assertTrue("3^n mod 2 = 1", c.isONE()); 816 817 // little fermat 818 a = BigInteger.ZERO.random(500); 819 b = new BigInteger(11); 820 c = Power.<BigInteger> modPositivePower(a, 11, b); 821 d = a.remainder(b); 822 assertEquals("a^p = a mod p", c, d); 823 824 c = pow.modPower(a, 11, b); 825 assertEquals("a^p = a mod p", c, d); 826 } 827 828 829 /** 830 * Test Combinatoric. 831 */ 832 public void testCombinatoric() { 833 BigInteger a, b, c; 834 835 a = Combinatoric.binCoeff(5, 0); 836 assertTrue("(5 0) == 1 ", a.isONE()); 837 838 a = Combinatoric.binCoeff(5, 7); 839 //System.out.println(5 + " over " + 7 + " = " + a); 840 assertTrue("(5 7) == 1 ", a.isONE()); 841 842 int n = 7; 843 for (int k = 0; k <= n; k++) { 844 a = Combinatoric.binCoeff(n, k); 845 b = Combinatoric.binCoeff(n, n - k); 846 assertEquals("(5 k) == (5 5-k) ", b, a); 847 //System.out.println(n + " over " + k + " = " + a); 848 } 849 assertTrue("(5 5) == 1 ", a.isONE()); 850 851 b = Combinatoric.binCoeffSum(n, n); 852 //System.out.println("sum( " + n + " over " + n + " ) = " + b); 853 c = Power.positivePower(new BigInteger(2), n); 854 assertEquals("sum(5 5) == 1 ", b, c); 855 856 b = Combinatoric.factorial(3); 857 assertEquals("3! == 6 ", b, new BigInteger(6)); 858 b = Combinatoric.factorial(0); 859 assertEquals("0! == 1 ", b, new BigInteger(1)); 860 } 861 862 863 /** 864 * Test square root. 865 */ 866 public void testSquareRoot() { 867 BigInteger a, b, c, d, e, f; 868 a = BigInteger.ONE; 869 870 b = a.random(47).abs(); 871 //b = c.multiply(c); 872 d = Roots.sqrtInt(b); 873 //System.out.println("b = " + b); 874 //System.out.println("root = " + d); 875 e = d.multiply(d); 876 //System.out.println("root^2 = " + e); 877 assertTrue("root^2 <= a ", e.compareTo(b) <= 0); 878 d = d.sum(BigInteger.ONE); 879 f = d.multiply(d); 880 //System.out.println("(root+1)^2 = " + f); 881 assertTrue("(root+1)^2 >= a ", f.compareTo(b) >= 0); 882 883 c = Roots.sqrt(b); 884 //System.out.println("b = " + b); 885 //System.out.println("root = " + c); 886 e = c.multiply(c); 887 //System.out.println("root^2 = " + e); 888 assertTrue("root^2 <= a ", e.compareTo(b) <= 0); 889 c = c.sum(BigInteger.ONE); 890 f = c.multiply(c); 891 //System.out.println("(root+1)^2 = " + f); 892 assertTrue("(root+1)^2 >= a ", f.compareTo(b) >= 0); 893 } 894 895 896 /** 897 * Test root. 898 */ 899 public void testRoot() { 900 BigInteger a, b, d, e, f; 901 a = BigInteger.ONE; 902 903 b = a.random(47).abs(); 904 //System.out.println("\nb = " + b); 905 //System.out.println("bitsize(b) = " + b.val.bitLength()); 906 for (int n = 2; n < 8; n++) { 907 d = Roots.root(b, n); 908 //System.out.println(n+"-th root = " + d); 909 e = Power.positivePower(d, n); 910 //System.out.println("root^"+n+" = " + e); 911 assertTrue("root^" + n + " <= a " + (b.subtract(e)), e.compareTo(b) <= 0); 912 d = d.sum(BigInteger.ONE); 913 f = Power.positivePower(d, n); 914 //System.out.println("(root+1)^"+n+" = " + f); 915 assertTrue("(root+1)^" + n + " >= a ", f.compareTo(b) >= 0); 916 } 917 } 918 919 920 /** 921 * Test root decimal. 922 */ 923 public void testRootDecimal() { 924 BigDecimal a, b, d, e; 925 a = BigDecimal.ONE; 926 927 b = a.random(7).abs(); 928 //System.out.println("\nb = " + b); 929 //System.out.println("ulp(b) = " + b.val.ulp()); 930 for (int n = 1; n < 8; n++) { 931 d = Roots.root(b, n); 932 //System.out.println(n+"-th root = " + d); 933 e = Power.positivePower(d, n); 934 //System.out.println("root^"+n+" = " + e); 935 e = e.subtract(d).abs(); 936 //System.out.println("e = " + e); 937 e = e.divide(b); 938 //System.out.println("e = " + e); 939 if (b.compareTo(a) > 0) { 940 assertTrue("root^" + n + " == a: " + e, a.compareTo(e) >= 0); 941 } else { 942 assertTrue("root^" + n + " == a: " + e, a.compareTo(e) <= 0); 943 } 944 } 945 } 946 947}