| 1441 |
ariadna |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace PhpOffice\PhpSpreadsheet\Calculation\Engineering;
|
|
|
4 |
|
|
|
5 |
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
|
|
|
6 |
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
|
|
|
7 |
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
|
|
|
8 |
|
|
|
9 |
class BesselK
|
|
|
10 |
{
|
|
|
11 |
use ArrayEnabled;
|
|
|
12 |
|
|
|
13 |
/**
|
|
|
14 |
* BESSELK.
|
|
|
15 |
*
|
|
|
16 |
* Returns the modified Bessel function Kn(x), which is equivalent to the Bessel functions evaluated
|
|
|
17 |
* for purely imaginary arguments.
|
|
|
18 |
*
|
|
|
19 |
* Excel Function:
|
|
|
20 |
* BESSELK(x,ord)
|
|
|
21 |
*
|
|
|
22 |
* @param mixed $x A float value at which to evaluate the function.
|
|
|
23 |
* If x is nonnumeric, BESSELK returns the #VALUE! error value.
|
|
|
24 |
* Or can be an array of values
|
|
|
25 |
* @param mixed $ord The integer order of the Bessel function.
|
|
|
26 |
* If ord is not an integer, it is truncated.
|
|
|
27 |
* If $ord is nonnumeric, BESSELK returns the #VALUE! error value.
|
|
|
28 |
* If $ord < 0, BESSELKI returns the #NUM! error value.
|
|
|
29 |
* Or can be an array of values
|
|
|
30 |
*
|
|
|
31 |
* @return array|float|string Result, or a string containing an error
|
|
|
32 |
* If an array of numbers is passed as an argument, then the returned result will also be an array
|
|
|
33 |
* with the same dimensions
|
|
|
34 |
*/
|
|
|
35 |
public static function BESSELK(mixed $x, mixed $ord): array|string|float
|
|
|
36 |
{
|
|
|
37 |
if (is_array($x) || is_array($ord)) {
|
|
|
38 |
return self::evaluateArrayArguments([self::class, __FUNCTION__], $x, $ord);
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
try {
|
|
|
42 |
$x = EngineeringValidations::validateFloat($x);
|
|
|
43 |
$ord = EngineeringValidations::validateInt($ord);
|
|
|
44 |
} catch (Exception $e) {
|
|
|
45 |
return $e->getMessage();
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
if (($ord < 0) || ($x <= 0.0)) {
|
|
|
49 |
return ExcelError::NAN();
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
$fBk = self::calculate($x, $ord);
|
|
|
53 |
|
|
|
54 |
return (is_nan($fBk)) ? ExcelError::NAN() : $fBk;
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
private static function calculate(float $x, int $ord): float
|
|
|
58 |
{
|
|
|
59 |
return match ($ord) {
|
|
|
60 |
|
|
|
61 |
1 => self::besselK1($x),
|
|
|
62 |
default => self::besselK2($x, $ord),
|
|
|
63 |
};
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
/**
|
|
|
67 |
* Mollify Phpstan.
|
|
|
68 |
*
|
|
|
69 |
* @codeCoverageIgnore
|
|
|
70 |
*/
|
|
|
71 |
private static function callBesselI(float $x, int $ord): float
|
|
|
72 |
{
|
|
|
73 |
$rslt = BesselI::BESSELI($x, $ord);
|
|
|
74 |
if (!is_float($rslt)) {
|
|
|
75 |
throw new Exception('Unexpected array or string');
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
return $rslt;
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
private static function besselK0(float $x): float
|
|
|
82 |
{
|
|
|
83 |
if ($x <= 2) {
|
|
|
84 |
$fNum2 = $x * 0.5;
|
|
|
85 |
$y = ($fNum2 * $fNum2);
|
|
|
86 |
|
|
|
87 |
return -log($fNum2) * self::callBesselI($x, 0)
|
|
|
88 |
+ (-0.57721566 + $y * (0.42278420 + $y * (0.23069756 + $y * (0.3488590e-1 + $y * (0.262698e-2 + $y
|
|
|
89 |
* (0.10750e-3 + $y * 0.74e-5))))));
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
$y = 2 / $x;
|
|
|
93 |
|
|
|
94 |
return exp(-$x) / sqrt($x)
|
|
|
95 |
* (1.25331414 + $y * (-0.7832358e-1 + $y * (0.2189568e-1 + $y * (-0.1062446e-1 + $y
|
|
|
96 |
* (0.587872e-2 + $y * (-0.251540e-2 + $y * 0.53208e-3))))));
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
private static function besselK1(float $x): float
|
|
|
100 |
{
|
|
|
101 |
if ($x <= 2) {
|
|
|
102 |
$fNum2 = $x * 0.5;
|
|
|
103 |
$y = ($fNum2 * $fNum2);
|
|
|
104 |
|
|
|
105 |
return log($fNum2) * self::callBesselI($x, 1)
|
|
|
106 |
+ (1 + $y * (0.15443144 + $y * (-0.67278579 + $y * (-0.18156897 + $y * (-0.1919402e-1 + $y
|
|
|
107 |
* (-0.110404e-2 + $y * (-0.4686e-4))))))) / $x;
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
$y = 2 / $x;
|
|
|
111 |
|
|
|
112 |
return exp(-$x) / sqrt($x)
|
|
|
113 |
* (1.25331414 + $y * (0.23498619 + $y * (-0.3655620e-1 + $y * (0.1504268e-1 + $y * (-0.780353e-2 + $y
|
|
|
114 |
* (0.325614e-2 + $y * (-0.68245e-3)))))));
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
private static function besselK2(float $x, int $ord): float
|
|
|
118 |
{
|
|
|
119 |
$fTox = 2 / $x;
|
|
|
120 |
$fBkm = self::besselK0($x);
|
|
|
121 |
$fBk = self::besselK1($x);
|
|
|
122 |
for ($n = 1; $n < $ord; ++$n) {
|
|
|
123 |
$fBkp = $fBkm + $n * $fTox * $fBk;
|
|
|
124 |
$fBkm = $fBk;
|
|
|
125 |
$fBk = $fBkp;
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
return $fBk;
|
|
|
129 |
}
|
|
|
130 |
}
|