| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Calculation\Engineering; |
| 4 |
|
| 5 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; |
| 6 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Exception; |
| 7 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError; |
| 8 |
|
| 9 |
class BesselY |
| 10 |
{ |
| 11 |
use ArrayEnabled; |
| 12 |
|
| 13 |
/** |
| 14 |
* BESSELY. |
| 15 |
* |
| 16 |
* Returns the Bessel function, which is also called the Weber function or the Neumann function. |
| 17 |
* |
| 18 |
* Excel Function: |
| 19 |
* BESSELY(x,ord) |
| 20 |
* |
| 21 |
* @param mixed $x A float value at which to evaluate the function. |
| 22 |
* If x is nonnumeric, BESSELY returns the #VALUE! error value. |
| 23 |
* Or can be an array of values |
| 24 |
* @param mixed $ord The integer order of the Bessel function. |
| 25 |
* If ord is not an integer, it is truncated. |
| 26 |
* If $ord is nonnumeric, BESSELY returns the #VALUE! error value. |
| 27 |
* If $ord < 0, BESSELY returns the #NUM! error value. |
| 28 |
* Or can be an array of values |
| 29 |
* |
| 30 |
* @return array<mixed>|float|string Result, or a string containing an error |
| 31 |
* If an array of numbers is passed as an argument, then the returned result will also be an array |
| 32 |
* with the same dimensions |
| 33 |
*/ |
| 34 |
public static function BESSELY($x, $ord) |
| 35 |
{ |
| 36 |
if (is_array($x) || is_array($ord)) { |
| 37 |
return self::evaluateArrayArguments([self::class, __FUNCTION__], $x, $ord); |
| 38 |
} |
| 39 |
|
| 40 |
try { |
| 41 |
$x = EngineeringValidations::validateFloat($x); |
| 42 |
$ord = EngineeringValidations::validateInt($ord); |
| 43 |
} catch (Exception $e) { |
| 44 |
return $e->getMessage(); |
| 45 |
} |
| 46 |
|
| 47 |
if (($ord < 0) || ($x <= 0.0)) { |
| 48 |
return ExcelError::NAN(); |
| 49 |
} |
| 50 |
|
| 51 |
$fBy = self::calculate($x, $ord); |
| 52 |
|
| 53 |
return (is_nan($fBy)) ? ExcelError::NAN() : $fBy; |
| 54 |
} |
| 55 |
|
| 56 |
private static function calculate(float $x, int $ord): float |
| 57 |
{ |
| 58 |
switch ($ord) { |
| 59 |
case 0: |
| 60 |
return self::besselY0($x); |
| 61 |
case 1: |
| 62 |
return self::besselY1($x); |
| 63 |
default: |
| 64 |
return self::besselY2($x, $ord); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Mollify Phpstan. |
| 70 |
* |
| 71 |
* @codeCoverageIgnore |
| 72 |
*/ |
| 73 |
private static function callBesselJ(float $x, int $ord): float |
| 74 |
{ |
| 75 |
$rslt = BesselJ::BESSELJ($x, $ord); |
| 76 |
if (!is_float($rslt)) { |
| 77 |
throw new Exception('Unexpected array or string'); |
| 78 |
} |
| 79 |
|
| 80 |
return $rslt; |
| 81 |
} |
| 82 |
|
| 83 |
private static function besselY0(float $x): float |
| 84 |
{ |
| 85 |
if ($x < 8.0) { |
| 86 |
$y = ($x * $x); |
| 87 |
$ans1 = -2957821389.0 + $y * (7062834065.0 + $y * (-512359803.6 + $y * (10879881.29 + $y |
| 88 |
* (-86327.92757 + $y * 228.4622733)))); |
| 89 |
$ans2 = 40076544269.0 + $y * (745249964.8 + $y * (7189466.438 + $y |
| 90 |
* (47447.26470 + $y * (226.1030244 + $y)))); |
| 91 |
|
| 92 |
return $ans1 / $ans2 + 0.636619772 * self::callBesselJ($x, 0) * log($x); |
| 93 |
} |
| 94 |
|
| 95 |
$z = 8.0 / $x; |
| 96 |
$y = ($z * $z); |
| 97 |
$xx = $x - 0.785398164; |
| 98 |
$ans1 = 1 + $y * (-0.1098628627e-2 + $y * (0.2734510407e-4 + $y * (-0.2073370639e-5 + $y * 0.2093887211e-6))); |
| 99 |
$ans2 = -0.1562499995e-1 + $y * (0.1430488765e-3 + $y * (-0.6911147651e-5 + $y * (0.7621095161e-6 + $y |
| 100 |
* (-0.934945152e-7)))); |
| 101 |
|
| 102 |
return sqrt(0.636619772 / $x) * (sin($xx) * $ans1 + $z * cos($xx) * $ans2); |
| 103 |
} |
| 104 |
|
| 105 |
private static function besselY1(float $x): float |
| 106 |
{ |
| 107 |
if ($x < 8.0) { |
| 108 |
$y = ($x * $x); |
| 109 |
$ans1 = $x * (-0.4900604943e13 + $y * (0.1275274390e13 + $y * (-0.5153438139e11 + $y |
| 110 |
* (0.7349264551e9 + $y * (-0.4237922726e7 + $y * 0.8511937935e4))))); |
| 111 |
$ans2 = 0.2499580570e14 + $y * (0.4244419664e12 + $y * (0.3733650367e10 + $y * (0.2245904002e8 + $y |
| 112 |
* (0.1020426050e6 + $y * (0.3549632885e3 + $y))))); |
| 113 |
|
| 114 |
return ($ans1 / $ans2) + 0.636619772 * (self::callBesselJ($x, 1) * log($x) - 1 / $x); |
| 115 |
} |
| 116 |
|
| 117 |
$z = 8.0 / $x; |
| 118 |
$y = $z * $z; |
| 119 |
$xx = $x - 2.356194491; |
| 120 |
$ans1 = 1.0 + $y * (0.183105e-2 + $y * (-0.3516396496e-4 + $y * (0.2457520174e-5 + $y * (-0.240337019e-6)))); |
| 121 |
$ans2 = 0.04687499995 + $y * (-0.2002690873e-3 + $y * (0.8449199096e-5 + $y |
| 122 |
* (-0.88228987e-6 + $y * 0.105787412e-6))); |
| 123 |
|
| 124 |
return sqrt(0.636619772 / $x) * (sin($xx) * $ans1 + $z * cos($xx) * $ans2); |
| 125 |
} |
| 126 |
|
| 127 |
private static function besselY2(float $x, int $ord): float |
| 128 |
{ |
| 129 |
$fTox = 2.0 / $x; |
| 130 |
$fBym = self::besselY0($x); |
| 131 |
$fBy = self::besselY1($x); |
| 132 |
for ($n = 1; $n < $ord; ++$n) { |
| 133 |
$fByp = $n * $fTox * $fBy - $fBym; |
| 134 |
$fBym = $fBy; |
| 135 |
$fBy = $fByp; |
| 136 |
} |
| 137 |
|
| 138 |
return $fBy; |
| 139 |
} |
| 140 |
} |
| 141 |
|