| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Brick\Math\Internal; |
| 6 |
|
| 7 |
use Brick\Math\Exception\RoundingNecessaryException; |
| 8 |
use Brick\Math\RoundingMode; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
abstract class Calculator |
| 24 |
{ |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public const MAX_POWER = 1_000_000; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public const ALPHABET = '0123456789abcdefghijklmnopqrstuvwxyz'; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
private static ?Calculator $instance = null; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
final public static function set(?Calculator $calculator) : void |
| 48 |
{ |
| 49 |
self::$instance = $calculator; |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
final public static function get() : Calculator |
| 61 |
{ |
| 62 |
if (self::$instance === null) { |
| 63 |
|
| 64 |
self::$instance = self::detect(); |
| 65 |
} |
| 66 |
|
| 67 |
return self::$instance; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
private static function detect() : Calculator |
| 76 |
{ |
| 77 |
if (\extension_loaded('gmp')) { |
| 78 |
return new Calculator\GmpCalculator(); |
| 79 |
} |
| 80 |
|
| 81 |
if (\extension_loaded('bcmath')) { |
| 82 |
return new Calculator\BcMathCalculator(); |
| 83 |
} |
| 84 |
|
| 85 |
return new Calculator\NativeCalculator(); |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
final protected function init(string $a, string $b) : array |
| 94 |
{ |
| 95 |
return [ |
| 96 |
$aNeg = ($a[0] === '-'), |
| 97 |
$bNeg = ($b[0] === '-'), |
| 98 |
|
| 99 |
$aNeg ? \substr($a, 1) : $a, |
| 100 |
$bNeg ? \substr($b, 1) : $b, |
| 101 |
]; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
final public function abs(string $n) : string |
| 108 |
{ |
| 109 |
return ($n[0] === '-') ? \substr($n, 1) : $n; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
final public function neg(string $n) : string |
| 116 |
{ |
| 117 |
if ($n === '0') { |
| 118 |
return '0'; |
| 119 |
} |
| 120 |
|
| 121 |
if ($n[0] === '-') { |
| 122 |
return \substr($n, 1); |
| 123 |
} |
| 124 |
|
| 125 |
return '-' . $n; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
final public function cmp(string $a, string $b) : int |
| 136 |
{ |
| 137 |
[$aNeg, $bNeg, $aDig, $bDig] = $this->init($a, $b); |
| 138 |
|
| 139 |
if ($aNeg && ! $bNeg) { |
| 140 |
return -1; |
| 141 |
} |
| 142 |
|
| 143 |
if ($bNeg && ! $aNeg) { |
| 144 |
return 1; |
| 145 |
} |
| 146 |
|
| 147 |
$aLen = \strlen($aDig); |
| 148 |
$bLen = \strlen($bDig); |
| 149 |
|
| 150 |
if ($aLen < $bLen) { |
| 151 |
$result = -1; |
| 152 |
} elseif ($aLen > $bLen) { |
| 153 |
$result = 1; |
| 154 |
} else { |
| 155 |
$result = $aDig <=> $bDig; |
| 156 |
} |
| 157 |
|
| 158 |
return $aNeg ? -$result : $result; |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
abstract public function add(string $a, string $b) : string; |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
abstract public function sub(string $a, string $b) : string; |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
abstract public function mul(string $a, string $b) : string; |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
abstract public function divQ(string $a, string $b) : string; |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
abstract public function divR(string $a, string $b) : string; |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
abstract public function divQR(string $a, string $b) : array; |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
abstract public function pow(string $a, int $e) : string; |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
public function mod(string $a, string $b) : string |
| 220 |
{ |
| 221 |
return $this->divR($this->add($this->divR($a, $b), $b), $b); |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
public function modInverse(string $x, string $m) : ?string |
| 234 |
{ |
| 235 |
if ($m === '1') { |
| 236 |
return '0'; |
| 237 |
} |
| 238 |
|
| 239 |
$modVal = $x; |
| 240 |
|
| 241 |
if ($x[0] === '-' || ($this->cmp($this->abs($x), $m) >= 0)) { |
| 242 |
$modVal = $this->mod($x, $m); |
| 243 |
} |
| 244 |
|
| 245 |
[$g, $x] = $this->gcdExtended($modVal, $m); |
| 246 |
|
| 247 |
if ($g !== '1') { |
| 248 |
return null; |
| 249 |
} |
| 250 |
|
| 251 |
return $this->mod($this->add($this->mod($x, $m), $m), $m); |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
abstract public function modPow(string $base, string $exp, string $mod) : string; |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
public function gcd(string $a, string $b) : string |
| 272 |
{ |
| 273 |
if ($a === '0') { |
| 274 |
return $this->abs($b); |
| 275 |
} |
| 276 |
|
| 277 |
if ($b === '0') { |
| 278 |
return $this->abs($a); |
| 279 |
} |
| 280 |
|
| 281 |
return $this->gcd($b, $this->divR($a, $b)); |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
private function gcdExtended(string $a, string $b) : array |
| 288 |
{ |
| 289 |
if ($a === '0') { |
| 290 |
return [$b, '0', '1']; |
| 291 |
} |
| 292 |
|
| 293 |
[$gcd, $x1, $y1] = $this->gcdExtended($this->mod($b, $a), $a); |
| 294 |
|
| 295 |
$x = $this->sub($y1, $this->mul($this->divQ($b, $a), $x1)); |
| 296 |
$y = $x1; |
| 297 |
|
| 298 |
return [$gcd, $x, $y]; |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
n. |
| 305 |
|
| 306 |
|
| 307 |
abstract public function sqrt(string $n) : string; |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
public function fromBase(string $number, int $base) : string |
| 321 |
{ |
| 322 |
return $this->fromArbitraryBase(\strtolower($number), self::ALPHABET, $base); |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
public function toBase(string $number, int $base) : string |
| 337 |
{ |
| 338 |
$negative = ($number[0] === '-'); |
| 339 |
|
| 340 |
if ($negative) { |
| 341 |
$number = \substr($number, 1); |
| 342 |
} |
| 343 |
|
| 344 |
$number = $this->toArbitraryBase($number, self::ALPHABET, $base); |
| 345 |
|
| 346 |
if ($negative) { |
| 347 |
return '-' . $number; |
| 348 |
} |
| 349 |
|
| 350 |
return $number; |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
final public function fromArbitraryBase(string $number, string $alphabet, int $base) : string |
| 364 |
{ |
| 365 |
|
| 366 |
$number = \ltrim($number, $alphabet[0]); |
| 367 |
|
| 368 |
if ($number === '') { |
| 369 |
return '0'; |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
if ($number === $alphabet[1]) { |
| 374 |
return '1'; |
| 375 |
} |
| 376 |
|
| 377 |
$result = '0'; |
| 378 |
$power = '1'; |
| 379 |
|
| 380 |
$base = (string) $base; |
| 381 |
|
| 382 |
for ($i = \strlen($number) - 1; $i >= 0; $i--) { |
| 383 |
$index = \strpos($alphabet, $number[$i]); |
| 384 |
|
| 385 |
if ($index !== 0) { |
| 386 |
$result = $this->add($result, ($index === 1) |
| 387 |
? $power |
| 388 |
: $this->mul($power, (string) $index) |
| 389 |
); |
| 390 |
} |
| 391 |
|
| 392 |
if ($i !== 0) { |
| 393 |
$power = $this->mul($power, $base); |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
return $result; |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
final public function toArbitraryBase(string $number, string $alphabet, int $base) : string |
| 410 |
{ |
| 411 |
if ($number === '0') { |
| 412 |
return $alphabet[0]; |
| 413 |
} |
| 414 |
|
| 415 |
$base = (string) $base; |
| 416 |
$result = ''; |
| 417 |
|
| 418 |
while ($number !== '0') { |
| 419 |
[$number, $remainder] = $this->divQR($number, $base); |
| 420 |
$remainder = (int) $remainder; |
| 421 |
|
| 422 |
$result .= $alphabet[$remainder]; |
| 423 |
} |
| 424 |
|
| 425 |
return \strrev($result); |
| 426 |
} |
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
final public function divRound(string $a, string $b, RoundingMode $roundingMode) : string |
| 443 |
{ |
| 444 |
[$quotient, $remainder] = $this->divQR($a, $b); |
| 445 |
|
| 446 |
$hasDiscardedFraction = ($remainder !== '0'); |
| 447 |
$isPositiveOrZero = ($a[0] === '-') === ($b[0] === '-'); |
| 448 |
|
| 449 |
$discardedFractionSign = function() use ($remainder, $b) : int { |
| 450 |
$r = $this->abs($this->mul($remainder, '2')); |
| 451 |
$b = $this->abs($b); |
| 452 |
|
| 453 |
return $this->cmp($r, $b); |
| 454 |
}; |
| 455 |
|
| 456 |
$increment = false; |
| 457 |
|
| 458 |
switch ($roundingMode) { |
| 459 |
case RoundingMode::UNNECESSARY: |
| 460 |
if ($hasDiscardedFraction) { |
| 461 |
throw RoundingNecessaryException::roundingNecessary(); |
| 462 |
} |
| 463 |
break; |
| 464 |
|
| 465 |
case RoundingMode::UP: |
| 466 |
$increment = $hasDiscardedFraction; |
| 467 |
break; |
| 468 |
|
| 469 |
case RoundingMode::DOWN: |
| 470 |
break; |
| 471 |
|
| 472 |
case RoundingMode::CEILING: |
| 473 |
$increment = $hasDiscardedFraction && $isPositiveOrZero; |
| 474 |
break; |
| 475 |
|
| 476 |
case RoundingMode::FLOOR: |
| 477 |
$increment = $hasDiscardedFraction && ! $isPositiveOrZero; |
| 478 |
break; |
| 479 |
|
| 480 |
case RoundingMode::HALF_UP: |
| 481 |
$increment = $discardedFractionSign() >= 0; |
| 482 |
break; |
| 483 |
|
| 484 |
case RoundingMode::HALF_DOWN: |
| 485 |
$increment = $discardedFractionSign() > 0; |
| 486 |
break; |
| 487 |
|
| 488 |
case RoundingMode::HALF_CEILING: |
| 489 |
$increment = $isPositiveOrZero ? $discardedFractionSign() >= 0 : $discardedFractionSign() > 0; |
| 490 |
break; |
| 491 |
|
| 492 |
case RoundingMode::HALF_FLOOR: |
| 493 |
$increment = $isPositiveOrZero ? $discardedFractionSign() > 0 : $discardedFractionSign() >= 0; |
| 494 |
break; |
| 495 |
|
| 496 |
case RoundingMode::HALF_EVEN: |
| 497 |
$lastDigit = (int) $quotient[-1]; |
| 498 |
$lastDigitIsEven = ($lastDigit % 2 === 0); |
| 499 |
$increment = $lastDigitIsEven ? $discardedFractionSign() > 0 : $discardedFractionSign() >= 0; |
| 500 |
break; |
| 501 |
|
| 502 |
default: |
| 503 |
throw new \InvalidArgumentException('Invalid rounding mode.'); |
| 504 |
} |
| 505 |
|
| 506 |
if ($increment) { |
| 507 |
return $this->add($quotient, $isPositiveOrZero ? '1' : '-1'); |
| 508 |
} |
| 509 |
|
| 510 |
return $quotient; |
| 511 |
} |
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
public function and(string $a, string $b) : string |
| 520 |
{ |
| 521 |
return $this->bitwise('and', $a, $b); |
| 522 |
} |
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
public function or(string $a, string $b) : string |
| 531 |
{ |
| 532 |
return $this->bitwise('or', $a, $b); |
| 533 |
} |
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
public function xor(string $a, string $b) : string |
| 542 |
{ |
| 543 |
return $this->bitwise('xor', $a, $b); |
| 544 |
} |
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
private function bitwise(string $operator, string $a, string $b) : string |
| 554 |
{ |
| 555 |
[$aNeg, $bNeg, $aDig, $bDig] = $this->init($a, $b); |
| 556 |
|
| 557 |
$aBin = $this->toBinary($aDig); |
| 558 |
$bBin = $this->toBinary($bDig); |
| 559 |
|
| 560 |
$aLen = \strlen($aBin); |
| 561 |
$bLen = \strlen($bBin); |
| 562 |
|
| 563 |
if ($aLen > $bLen) { |
| 564 |
$bBin = \str_repeat("\x00", $aLen - $bLen) . $bBin; |
| 565 |
} elseif ($bLen > $aLen) { |
| 566 |
$aBin = \str_repeat("\x00", $bLen - $aLen) . $aBin; |
| 567 |
} |
| 568 |
|
| 569 |
if ($aNeg) { |
| 570 |
$aBin = $this->twosComplement($aBin); |
| 571 |
} |
| 572 |
if ($bNeg) { |
| 573 |
$bBin = $this->twosComplement($bBin); |
| 574 |
} |
| 575 |
|
| 576 |
$value = match ($operator) { |
| 577 |
'and' => $aBin & $bBin, |
| 578 |
'or' => $aBin | $bBin, |
| 579 |
'xor' => $aBin ^ $bBin, |
| 580 |
}; |
| 581 |
|
| 582 |
$negative = match ($operator) { |
| 583 |
'and' => $aNeg and $bNeg, |
| 584 |
'or' => $aNeg or $bNeg, |
| 585 |
'xor' => $aNeg xor $bNeg, |
| 586 |
}; |
| 587 |
|
| 588 |
if ($negative) { |
| 589 |
$value = $this->twosComplement($value); |
| 590 |
} |
| 591 |
|
| 592 |
$result = $this->toDecimal($value); |
| 593 |
|
| 594 |
return $negative ? $this->neg($result) : $result; |
| 595 |
} |
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
private function twosComplement(string $number) : string |
| 601 |
{ |
| 602 |
$xor = \str_repeat("\xff", \strlen($number)); |
| 603 |
|
| 604 |
$number ^= $xor; |
| 605 |
|
| 606 |
for ($i = \strlen($number) - 1; $i >= 0; $i--) { |
| 607 |
$byte = \ord($number[$i]); |
| 608 |
|
| 609 |
if (++$byte !== 256) { |
| 610 |
$number[$i] = \chr($byte); |
| 611 |
break; |
| 612 |
} |
| 613 |
|
| 614 |
$number[$i] = "\x00"; |
| 615 |
|
| 616 |
if ($i === 0) { |
| 617 |
$number = "\x01" . $number; |
| 618 |
} |
| 619 |
} |
| 620 |
|
| 621 |
return $number; |
| 622 |
} |
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
private function toBinary(string $number) : string |
| 630 |
{ |
| 631 |
$result = ''; |
| 632 |
|
| 633 |
while ($number !== '0') { |
| 634 |
[$number, $remainder] = $this->divQR($number, '256'); |
| 635 |
$result .= \chr((int) $remainder); |
| 636 |
} |
| 637 |
|
| 638 |
return \strrev($result); |
| 639 |
} |
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
private function toDecimal(string $bytes) : string |
| 647 |
{ |
| 648 |
$result = '0'; |
| 649 |
$power = '1'; |
| 650 |
|
| 651 |
for ($i = \strlen($bytes) - 1; $i >= 0; $i--) { |
| 652 |
$index = \ord($bytes[$i]); |
| 653 |
|
| 654 |
if ($index !== 0) { |
| 655 |
$result = $this->add($result, ($index === 1) |
| 656 |
? $power |
| 657 |
: $this->mul($power, (string) $index) |
| 658 |
); |
| 659 |
} |
| 660 |
|
| 661 |
if ($i !== 0) { |
| 662 |
$power = $this->mul($power, '256'); |
| 663 |
} |
| 664 |
} |
| 665 |
|
| 666 |
return $result; |
| 667 |
} |
| 668 |
} |