aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/brick/math/src/Internal
diff options
context:
space:
mode:
authorMario <mario@mariovavti.com>2023-07-11 18:57:18 +0000
committerMario <mario@mariovavti.com>2023-07-11 18:57:18 +0000
commit63fb8d03929189bfc8cbf53d23cb79984fe2c3cd (patch)
treeb9f74bd8c7721dca7ece251fdbb9a7c4fe9b949a /vendor/brick/math/src/Internal
parent57796a2f962d045445cbf69237bb3d6786e4d0d4 (diff)
parent384de0925e502cfa8fe6ca287530ef5529fdff10 (diff)
downloadvolse-hubzilla-63fb8d03929189bfc8cbf53d23cb79984fe2c3cd.tar.gz
volse-hubzilla-63fb8d03929189bfc8cbf53d23cb79984fe2c3cd.tar.bz2
volse-hubzilla-63fb8d03929189bfc8cbf53d23cb79984fe2c3cd.zip
Merge branch '8.6RC'8.6
Diffstat (limited to 'vendor/brick/math/src/Internal')
-rw-r--r--vendor/brick/math/src/Internal/Calculator.php110
-rw-r--r--vendor/brick/math/src/Internal/Calculator/BcMathCalculator.php47
-rw-r--r--vendor/brick/math/src/Internal/Calculator/GmpCalculator.php48
-rw-r--r--vendor/brick/math/src/Internal/Calculator/NativeCalculator.php57
4 files changed, 20 insertions, 242 deletions
diff --git a/vendor/brick/math/src/Internal/Calculator.php b/vendor/brick/math/src/Internal/Calculator.php
index a6eac799f..b8cecda96 100644
--- a/vendor/brick/math/src/Internal/Calculator.php
+++ b/vendor/brick/math/src/Internal/Calculator.php
@@ -34,10 +34,8 @@ abstract class Calculator
/**
* The Calculator instance in use.
- *
- * @var Calculator|null
*/
- private static $instance;
+ private static ?Calculator $instance = null;
/**
* Sets the Calculator instance to use.
@@ -45,8 +43,6 @@ abstract class Calculator
* An instance is typically set only in unit tests: the autodetect is usually the best option.
*
* @param Calculator|null $calculator The calculator instance, or NULL to revert to autodetect.
- *
- * @return void
*/
final public static function set(?Calculator $calculator) : void
{
@@ -58,8 +54,6 @@ abstract class Calculator
*
* If none has been explicitly set, the fastest available implementation will be returned.
*
- * @return Calculator
- *
* @psalm-pure
* @psalm-suppress ImpureStaticProperty
*/
@@ -77,8 +71,6 @@ abstract class Calculator
* Returns the fastest available Calculator implementation.
*
* @codeCoverageIgnore
- *
- * @return Calculator
*/
private static function detect() : Calculator
{
@@ -96,9 +88,6 @@ abstract class Calculator
/**
* Extracts the sign & digits of the operands.
*
- * @param string $a The first operand.
- * @param string $b The second operand.
- *
* @return array{bool, bool, string, string} Whether $a and $b are negative, followed by their digits.
*/
final protected function init(string $a, string $b) : array
@@ -114,10 +103,6 @@ abstract class Calculator
/**
* Returns the absolute value of a number.
- *
- * @param string $n The number.
- *
- * @return string The absolute value.
*/
final public function abs(string $n) : string
{
@@ -126,10 +111,6 @@ abstract class Calculator
/**
* Negates a number.
- *
- * @param string $n The number.
- *
- * @return string The negated value.
*/
final public function neg(string $n) : string
{
@@ -147,9 +128,6 @@ abstract class Calculator
/**
* Compares two numbers.
*
- * @param string $a The first number.
- * @param string $b The second number.
- *
* @return int [-1, 0, 1] If the first number is less than, equal to, or greater than the second number.
*/
final public function cmp(string $a, string $b) : int
@@ -180,31 +158,16 @@ abstract class Calculator
/**
* Adds two numbers.
- *
- * @param string $a The augend.
- * @param string $b The addend.
- *
- * @return string The sum.
*/
abstract public function add(string $a, string $b) : string;
/**
* Subtracts two numbers.
- *
- * @param string $a The minuend.
- * @param string $b The subtrahend.
- *
- * @return string The difference.
*/
abstract public function sub(string $a, string $b) : string;
/**
* Multiplies two numbers.
- *
- * @param string $a The multiplicand.
- * @param string $b The multiplier.
- *
- * @return string The product.
*/
abstract public function mul(string $a, string $b) : string;
@@ -234,7 +197,7 @@ abstract class Calculator
* @param string $a The dividend.
* @param string $b The divisor, must not be zero.
*
- * @return string[] An array containing the quotient and remainder.
+ * @return array{string, string} An array containing the quotient and remainder.
*/
abstract public function divQR(string $a, string $b) : array;
@@ -249,10 +212,7 @@ abstract class Calculator
abstract public function pow(string $a, int $e) : string;
/**
- * @param string $a
* @param string $b The modulus; must not be zero.
- *
- * @return string
*/
public function mod(string $a, string $b) : string
{
@@ -266,10 +226,7 @@ abstract class Calculator
*
* This method can be overridden by the concrete implementation if the underlying library has built-in support.
*
- * @param string $x
* @param string $m The modulus; must not be negative or zero.
- *
- * @return string|null
*/
public function modInverse(string $x, string $m) : ?string
{
@@ -283,9 +240,7 @@ abstract class Calculator
$modVal = $this->mod($x, $m);
}
- $x = '0';
- $y = '0';
- $g = $this->gcdExtended($modVal, $m, $x, $y);
+ [$g, $x] = $this->gcdExtended($modVal, $m);
if ($g !== '1') {
return null;
@@ -300,8 +255,6 @@ abstract class Calculator
* @param string $base The base number; must be positive or zero.
* @param string $exp The exponent; must be positive or zero.
* @param string $mod The modulus; must be strictly positive.
- *
- * @return string The power.
*/
abstract public function modPow(string $base, string $exp, string $mod) : string;
@@ -311,9 +264,6 @@ abstract class Calculator
* This method can be overridden by the concrete implementation if the underlying library
* has built-in support for GCD calculations.
*
- * @param string $a The first number.
- * @param string $b The second number.
- *
* @return string The GCD, always positive, or zero if both arguments are zero.
*/
public function gcd(string $a, string $b) : string
@@ -329,24 +279,21 @@ abstract class Calculator
return $this->gcd($b, $this->divR($a, $b));
}
- private function gcdExtended(string $a, string $b, string &$x, string &$y) : string
+ /**
+ * @return array{string, string, string} GCD, X, Y
+ */
+ private function gcdExtended(string $a, string $b) : array
{
if ($a === '0') {
- $x = '0';
- $y = '1';
-
- return $b;
+ return [$b, '0', '1'];
}
- $x1 = '0';
- $y1 = '0';
-
- $gcd = $this->gcdExtended($this->mod($b, $a), $a, $x1, $y1);
+ [$gcd, $x1, $y1] = $this->gcdExtended($this->mod($b, $a), $a);
$x = $this->sub($y1, $this->mul($this->divQ($b, $a), $x1));
$y = $x1;
- return $gcd;
+ return [$gcd, $x, $y];
}
/**
@@ -354,10 +301,6 @@ abstract class Calculator
*
* The result is the largest x such that x² ≤ n.
* The input MUST NOT be negative.
- *
- * @param string $n The number.
- *
- * @return string The square root.
*/
abstract public function sqrt(string $n) : string;
@@ -489,10 +432,10 @@ abstract class Calculator
* @param string $b The divisor, must not be zero.
* @param int $roundingMode The rounding mode.
*
- * @return string
- *
* @throws \InvalidArgumentException If the rounding mode is invalid.
* @throws RoundingNecessaryException If RoundingMode::UNNECESSARY is provided but rounding is necessary.
+ *
+ * @psalm-suppress ImpureFunctionCall
*/
final public function divRound(string $a, string $b, int $roundingMode) : string
{
@@ -570,11 +513,6 @@ abstract class Calculator
*
* This method can be overridden by the concrete implementation if the underlying library
* has built-in support for bitwise operations.
- *
- * @param string $a
- * @param string $b
- *
- * @return string
*/
public function and(string $a, string $b) : string
{
@@ -586,11 +524,6 @@ abstract class Calculator
*
* This method can be overridden by the concrete implementation if the underlying library
* has built-in support for bitwise operations.
- *
- * @param string $a
- * @param string $b
- *
- * @return string
*/
public function or(string $a, string $b) : string
{
@@ -602,11 +535,6 @@ abstract class Calculator
*
* This method can be overridden by the concrete implementation if the underlying library
* has built-in support for bitwise operations.
- *
- * @param string $a
- * @param string $b
- *
- * @return string
*/
public function xor(string $a, string $b) : string
{
@@ -616,11 +544,9 @@ abstract class Calculator
/**
* Performs a bitwise operation on a decimal number.
*
- * @param string $operator The operator to use, must be "and", "or" or "xor".
- * @param string $a The left operand.
- * @param string $b The right operand.
- *
- * @return string
+ * @param 'and'|'or'|'xor' $operator The operator to use.
+ * @param string $a The left operand.
+ * @param string $b The right operand.
*/
private function bitwise(string $operator, string $a, string $b) : string
{
@@ -678,8 +604,6 @@ abstract class Calculator
/**
* @param string $number A positive, binary number.
- *
- * @return string
*/
private function twosComplement(string $number) : string
{
@@ -709,8 +633,6 @@ abstract class Calculator
* Converts a decimal number to a binary string.
*
* @param string $number The number to convert, positive or zero, only digits.
- *
- * @return string
*/
private function toBinary(string $number) : string
{
@@ -728,8 +650,6 @@ abstract class Calculator
* Returns the positive decimal representation of a binary number.
*
* @param string $bytes The bytes representing the number.
- *
- * @return string
*/
private function toDecimal(string $bytes) : string
{
diff --git a/vendor/brick/math/src/Internal/Calculator/BcMathCalculator.php b/vendor/brick/math/src/Internal/Calculator/BcMathCalculator.php
index 6632b378a..5457a3c98 100644
--- a/vendor/brick/math/src/Internal/Calculator/BcMathCalculator.php
+++ b/vendor/brick/math/src/Internal/Calculator/BcMathCalculator.php
@@ -15,99 +15,58 @@ use Brick\Math\Internal\Calculator;
*/
class BcMathCalculator extends Calculator
{
- /**
- * {@inheritdoc}
- */
public function add(string $a, string $b) : string
{
return \bcadd($a, $b, 0);
}
- /**
- * {@inheritdoc}
- */
public function sub(string $a, string $b) : string
{
return \bcsub($a, $b, 0);
}
- /**
- * {@inheritdoc}
- */
public function mul(string $a, string $b) : string
{
return \bcmul($a, $b, 0);
}
- /**
- * {@inheritdoc}
- *
- * @psalm-suppress InvalidNullableReturnType
- * @psalm-suppress NullableReturnStatement
- */
public function divQ(string $a, string $b) : string
{
return \bcdiv($a, $b, 0);
}
/**
- * {@inheritdoc}
- *
* @psalm-suppress InvalidNullableReturnType
* @psalm-suppress NullableReturnStatement
*/
public function divR(string $a, string $b) : string
{
- if (version_compare(PHP_VERSION, '7.2') >= 0) {
- return \bcmod($a, $b, 0);
- }
-
- return \bcmod($a, $b);
+ return \bcmod($a, $b, 0);
}
- /**
- * {@inheritdoc}
- */
public function divQR(string $a, string $b) : array
{
$q = \bcdiv($a, $b, 0);
+ $r = \bcmod($a, $b, 0);
- if (version_compare(PHP_VERSION, '7.2') >= 0) {
- $r = \bcmod($a, $b, 0);
- } else {
- $r = \bcmod($a, $b);
- }
-
- assert($q !== null);
assert($r !== null);
return [$q, $r];
}
- /**
- * {@inheritdoc}
- */
public function pow(string $a, int $e) : string
{
return \bcpow($a, (string) $e, 0);
}
- /**
- * {@inheritdoc}
- *
- * @psalm-suppress InvalidNullableReturnType
- * @psalm-suppress NullableReturnStatement
- */
public function modPow(string $base, string $exp, string $mod) : string
{
return \bcpowmod($base, $exp, $mod, 0);
}
/**
- * {@inheritDoc}
- *
- * @psalm-suppress NullableReturnStatement
* @psalm-suppress InvalidNullableReturnType
+ * @psalm-suppress NullableReturnStatement
*/
public function sqrt(string $n) : string
{
diff --git a/vendor/brick/math/src/Internal/Calculator/GmpCalculator.php b/vendor/brick/math/src/Internal/Calculator/GmpCalculator.php
index 52d18800a..42d4c6927 100644
--- a/vendor/brick/math/src/Internal/Calculator/GmpCalculator.php
+++ b/vendor/brick/math/src/Internal/Calculator/GmpCalculator.php
@@ -15,49 +15,31 @@ use Brick\Math\Internal\Calculator;
*/
class GmpCalculator extends Calculator
{
- /**
- * {@inheritdoc}
- */
public function add(string $a, string $b) : string
{
return \gmp_strval(\gmp_add($a, $b));
}
- /**
- * {@inheritdoc}
- */
public function sub(string $a, string $b) : string
{
return \gmp_strval(\gmp_sub($a, $b));
}
- /**
- * {@inheritdoc}
- */
public function mul(string $a, string $b) : string
{
return \gmp_strval(\gmp_mul($a, $b));
}
- /**
- * {@inheritdoc}
- */
public function divQ(string $a, string $b) : string
{
return \gmp_strval(\gmp_div_q($a, $b));
}
- /**
- * {@inheritdoc}
- */
public function divR(string $a, string $b) : string
{
return \gmp_strval(\gmp_div_r($a, $b));
}
- /**
- * {@inheritdoc}
- */
public function divQR(string $a, string $b) : array
{
[$q, $r] = \gmp_div_qr($a, $b);
@@ -68,17 +50,11 @@ class GmpCalculator extends Calculator
];
}
- /**
- * {@inheritdoc}
- */
public function pow(string $a, int $e) : string
{
return \gmp_strval(\gmp_pow($a, $e));
}
- /**
- * {@inheritdoc}
- */
public function modInverse(string $x, string $m) : ?string
{
$result = \gmp_invert($x, $m);
@@ -90,65 +66,41 @@ class GmpCalculator extends Calculator
return \gmp_strval($result);
}
- /**
- * {@inheritdoc}
- */
public function modPow(string $base, string $exp, string $mod) : string
{
return \gmp_strval(\gmp_powm($base, $exp, $mod));
}
- /**
- * {@inheritdoc}
- */
public function gcd(string $a, string $b) : string
{
return \gmp_strval(\gmp_gcd($a, $b));
}
- /**
- * {@inheritdoc}
- */
public function fromBase(string $number, int $base) : string
{
return \gmp_strval(\gmp_init($number, $base));
}
- /**
- * {@inheritdoc}
- */
public function toBase(string $number, int $base) : string
{
return \gmp_strval($number, $base);
}
- /**
- * {@inheritdoc}
- */
public function and(string $a, string $b) : string
{
return \gmp_strval(\gmp_and($a, $b));
}
- /**
- * {@inheritdoc}
- */
public function or(string $a, string $b) : string
{
return \gmp_strval(\gmp_or($a, $b));
}
- /**
- * {@inheritdoc}
- */
public function xor(string $a, string $b) : string
{
return \gmp_strval(\gmp_xor($a, $b));
}
- /**
- * {@inheritDoc}
- */
public function sqrt(string $n) : string
{
return \gmp_strval(\gmp_sqrt($n));
diff --git a/vendor/brick/math/src/Internal/Calculator/NativeCalculator.php b/vendor/brick/math/src/Internal/Calculator/NativeCalculator.php
index 020a6338b..7c679d24d 100644
--- a/vendor/brick/math/src/Internal/Calculator/NativeCalculator.php
+++ b/vendor/brick/math/src/Internal/Calculator/NativeCalculator.php
@@ -19,17 +19,13 @@ class NativeCalculator extends Calculator
* The max number of digits the platform can natively add, subtract, multiply or divide without overflow.
* For multiplication, this represents the max sum of the lengths of both operands.
*
- * For addition, it is assumed that an extra digit can hold a carry (1) without overflowing.
+ * In addition, it is assumed that an extra digit can hold a carry (1) without overflowing.
* Example: 32-bit: max number 1,999,999,999 (9 digits + carry)
* 64-bit: max number 1,999,999,999,999,999,999 (18 digits + carry)
- *
- * @var int
*/
- private $maxDigits;
+ private int $maxDigits;
/**
- * Class constructor.
- *
* @codeCoverageIgnore
*/
public function __construct()
@@ -48,9 +44,6 @@ class NativeCalculator extends Calculator
}
}
- /**
- * {@inheritdoc}
- */
public function add(string $a, string $b) : string
{
/**
@@ -82,17 +75,11 @@ class NativeCalculator extends Calculator
return $result;
}
- /**
- * {@inheritdoc}
- */
public function sub(string $a, string $b) : string
{
return $this->add($a, $this->neg($b));
}
- /**
- * {@inheritdoc}
- */
public function mul(string $a, string $b) : string
{
/**
@@ -136,25 +123,16 @@ class NativeCalculator extends Calculator
return $result;
}
- /**
- * {@inheritdoc}
- */
public function divQ(string $a, string $b) : string
{
return $this->divQR($a, $b)[0];
}
- /**
- * {@inheritdoc}
- */
public function divR(string $a, string $b): string
{
return $this->divQR($a, $b)[1];
}
- /**
- * {@inheritdoc}
- */
public function divQR(string $a, string $b) : array
{
if ($a === '0') {
@@ -210,9 +188,6 @@ class NativeCalculator extends Calculator
return [$q, $r];
}
- /**
- * {@inheritdoc}
- */
public function pow(string $a, int $e) : string
{
if ($e === 0) {
@@ -240,8 +215,6 @@ class NativeCalculator extends Calculator
/**
* Algorithm from: https://www.geeksforgeeks.org/modular-exponentiation-power-in-modular-arithmetic/
- *
- * {@inheritdoc}
*/
public function modPow(string $base, string $exp, string $mod) : string
{
@@ -276,8 +249,6 @@ class NativeCalculator extends Calculator
/**
* Adapted from https://cp-algorithms.com/num_methods/roots_newton.html
- *
- * {@inheritDoc}
*/
public function sqrt(string $n) : string
{
@@ -306,11 +277,6 @@ class NativeCalculator extends Calculator
/**
* Performs the addition of two non-signed large integers.
- *
- * @param string $a The first operand.
- * @param string $b The second operand.
- *
- * @return string
*/
private function doAdd(string $a, string $b) : string
{
@@ -363,11 +329,6 @@ class NativeCalculator extends Calculator
/**
* Performs the subtraction of two non-signed large integers.
- *
- * @param string $a The first operand.
- * @param string $b The second operand.
- *
- * @return string
*/
private function doSub(string $a, string $b) : string
{
@@ -445,11 +406,6 @@ class NativeCalculator extends Calculator
/**
* Performs the multiplication of two non-signed large integers.
- *
- * @param string $a The first operand.
- * @param string $b The second operand.
- *
- * @return string
*/
private function doMul(string $a, string $b) : string
{
@@ -522,9 +478,6 @@ class NativeCalculator extends Calculator
/**
* Performs the division of two non-signed large integers.
*
- * @param string $a The first operand.
- * @param string $b The second operand.
- *
* @return string[] The quotient and remainder.
*/
private function doDiv(string $a, string $b) : array
@@ -583,9 +536,6 @@ class NativeCalculator extends Calculator
/**
* Compares two non-signed large numbers.
*
- * @param string $a The first operand.
- * @param string $b The second operand.
- *
* @return int [-1, 0, 1]
*/
private function doCmp(string $a, string $b) : int
@@ -607,9 +557,6 @@ class NativeCalculator extends Calculator
*
* The numbers must only consist of digits, without leading minus sign.
*
- * @param string $a The first operand.
- * @param string $b The second operand.
- *
* @return array{string, string, int}
*/
private function pad(string $a, string $b) : array