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