PluginProbe
TablePress – Tables in WordPress made easy / 3.4
TablePress – Tables in WordPress made easy v3.4
3.4 3.3.4 3.3.3 3.3.2 3.3.1 trunk 1.12 1.14 1.9.2 2.0.4 2.1.7 2.1.8 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3 2.3.1 2.3.2 2.4 2.4.1 2.4.2 2.4.3 All 45 releases
tablepress / libraries / vendor / PhpSpreadsheet / Calculation / Engineering / BesselK.php

BesselK.php in TablePress – Tables in WordPress made easy 3.4, at libraries/vendor/PhpSpreadsheet/Calculation/Engineering/BesselK.php

134 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 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<mixed>|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($x, $ord)
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 switch ($ord) {
60 case 0:
61 return self::besselK0($x);
62 case 1:
63 return self::besselK1($x);
64 default:
65 return self::besselK2($x, $ord);
66 }
67 }
68
69 /**
70 * Mollify Phpstan.
71 *
72 * @codeCoverageIgnore
73 */
74 private static function callBesselI(float $x, int $ord): float
75 {
76 $rslt = BesselI::BESSELI($x, $ord);
77 if (!is_float($rslt)) {
78 throw new Exception('Unexpected array or string');
79 }
80
81 return $rslt;
82 }
83
84 private static function besselK0(float $x): float
85 {
86 if ($x <= 2) {
87 $fNum2 = $x * 0.5;
88 $y = ($fNum2 * $fNum2);
89
90 return -log($fNum2) * self::callBesselI($x, 0)
91 + (-0.57721566 + $y * (0.42278420 + $y * (0.23069756 + $y * (0.3488590e-1 + $y * (0.262698e-2 + $y
92 * (0.10750e-3 + $y * 0.74e-5))))));
93 }
94
95 $y = 2 / $x;
96
97 return exp(-$x) / sqrt($x)
98 * (1.25331414 + $y * (-0.7832358e-1 + $y * (0.2189568e-1 + $y * (-0.1062446e-1 + $y
99 * (0.587872e-2 + $y * (-0.251540e-2 + $y * 0.53208e-3))))));
100 }
101
102 private static function besselK1(float $x): float
103 {
104 if ($x <= 2) {
105 $fNum2 = $x * 0.5;
106 $y = ($fNum2 * $fNum2);
107
108 return log($fNum2) * self::callBesselI($x, 1)
109 + (1 + $y * (0.15443144 + $y * (-0.67278579 + $y * (-0.18156897 + $y * (-0.1919402e-1 + $y
110 * (-0.110404e-2 + $y * (-0.4686e-4))))))) / $x;
111 }
112
113 $y = 2 / $x;
114
115 return exp(-$x) / sqrt($x)
116 * (1.25331414 + $y * (0.23498619 + $y * (-0.3655620e-1 + $y * (0.1504268e-1 + $y * (-0.780353e-2 + $y
117 * (0.325614e-2 + $y * (-0.68245e-3)))))));
118 }
119
120 private static function besselK2(float $x, int $ord): float
121 {
122 $fTox = 2 / $x;
123 $fBkm = self::besselK0($x);
124 $fBk = self::besselK1($x);
125 for ($n = 1; $n < $ord; ++$n) {
126 $fBkp = $fBkm + $n * $fTox * $fBk;
127 $fBkm = $fBk;
128 $fBk = $fBkp;
129 }
130
131 return $fBk;
132 }
133 }
134