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 / BesselJ.php

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

180 lines 4.9 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 BesselJ
10 {
11 use ArrayEnabled;
12
13 /**
14 * BESSELJ.
15 *
16 * Returns the Bessel function
17 *
18 * Excel Function:
19 * BESSELJ(x,ord)
20 *
21 * NOTE: The MS Excel implementation of the BESSELJ function is still not accurate, particularly for higher order
22 * values with x < -8 and x > 8. This code provides a more accurate calculation
23 *
24 * @param mixed $x A float value at which to evaluate the function.
25 * If x is nonnumeric, BESSELJ returns the #VALUE! error value.
26 * Or can be an array of values
27 * @param mixed $ord The integer order of the Bessel function.
28 * If ord is not an integer, it is truncated.
29 * If $ord is nonnumeric, BESSELJ returns the #VALUE! error value.
30 * If $ord < 0, BESSELJ returns the #NUM! error value.
31 * Or can be an array of values
32 *
33 * @return array<mixed>|float|string Result, or a string containing an error
34 * If an array of numbers is passed as an argument, then the returned result will also be an array
35 * with the same dimensions
36 */
37 public static function BESSELJ($x, $ord)
38 {
39 if (is_array($x) || is_array($ord)) {
40 return self::evaluateArrayArguments([self::class, __FUNCTION__], $x, $ord);
41 }
42
43 try {
44 $x = EngineeringValidations::validateFloat($x);
45 $ord = EngineeringValidations::validateInt($ord);
46 } catch (Exception $e) {
47 return $e->getMessage();
48 }
49
50 if ($ord < 0) {
51 return ExcelError::NAN();
52 }
53
54 $fResult = self::calculate($x, $ord);
55
56 return (is_nan($fResult)) ? ExcelError::NAN() : $fResult;
57 }
58
59 private static function calculate(float $x, int $ord): float
60 {
61 switch ($ord) {
62 case 0:
63 return self::besselJ0($x);
64 case 1:
65 return self::besselJ1($x);
66 default:
67 return self::besselJ2($x, $ord);
68 }
69 }
70
71 private static function besselJ0(float $x): float
72 {
73 $ax = abs($x);
74
75 if ($ax < 8.0) {
76 $y = $x * $x;
77 $ans1 = 57568490574.0 + $y * (-13362590354.0 + $y * (651619640.7 + $y * (-11214424.18 + $y
78 * (77392.33017 + $y * (-184.9052456)))));
79 $ans2 = 57568490411.0 + $y * (1029532985.0 + $y * (9494680.718 + $y * (59272.64853 + $y
80 * (267.8532712 + $y * 1.0))));
81
82 return $ans1 / $ans2;
83 }
84
85 $z = 8.0 / $ax;
86 $y = $z * $z;
87 $xx = $ax - 0.785398164;
88 $ans1 = 1.0 + $y * (-0.1098628627e-2 + $y * (0.2734510407e-4 + $y * (-0.2073370639e-5 + $y * 0.2093887211e-6)));
89 $ans2 = -0.1562499995e-1 + $y * (0.1430488765e-3 + $y * (-0.6911147651e-5 + $y
90 * (0.7621095161e-6 - $y * 0.934935152e-7)));
91
92 return sqrt(0.636619772 / $ax) * (cos($xx) * $ans1 - $z * sin($xx) * $ans2);
93 }
94
95 private static function besselJ1(float $x): float
96 {
97 $ax = abs($x);
98
99 if ($ax < 8.0) {
100 $y = $x * $x;
101 $ans1 = $x * (72362614232.0 + $y * (-7895059235.0 + $y * (242396853.1 + $y
102 * (-2972611.439 + $y * (15704.48260 + $y * (-30.16036606))))));
103 $ans2 = 144725228442.0 + $y * (2300535178.0 + $y * (18583304.74 + $y * (99447.43394 + $y
104 * (376.9991397 + $y * 1.0))));
105
106 return $ans1 / $ans2;
107 }
108
109 $z = 8.0 / $ax;
110 $y = $z * $z;
111 $xx = $ax - 2.356194491;
112
113 $ans1 = 1.0 + $y * (0.183105e-2 + $y * (-0.3516396496e-4 + $y * (0.2457520174e-5 + $y * (-0.240337019e-6))));
114 $ans2 = 0.04687499995 + $y * (-0.2002690873e-3 + $y * (0.8449199096e-5 + $y
115 * (-0.88228987e-6 + $y * 0.105787412e-6)));
116 $ans = sqrt(0.636619772 / $ax) * (cos($xx) * $ans1 - $z * sin($xx) * $ans2);
117
118 return ($x < 0.0) ? -$ans : $ans;
119 }
120
121 private static function besselJ2(float $x, int $ord): float
122 {
123 $ax = abs($x);
124 if ($ax === 0.0) {
125 return 0.0;
126 }
127
128 if ($ax > $ord) {
129 return self::besselj2a($ax, $ord, $x);
130 }
131
132 return self::besselj2b($ax, $ord, $x);
133 }
134
135 private static function besselj2a(float $ax, int $ord, float $x): float
136 {
137 $tox = 2.0 / $ax;
138 $bjm = self::besselJ0($ax);
139 $bj = self::besselJ1($ax);
140 for ($j = 1; $j < $ord; ++$j) {
141 $bjp = $j * $tox * $bj - $bjm;
142 $bjm = $bj;
143 $bj = $bjp;
144 }
145 $ans = $bj;
146
147 return ($x < 0.0 && ($ord % 2) == 1) ? -$ans : $ans;
148 }
149
150 private static function besselj2b(float $ax, int $ord, float $x): float
151 {
152 $tox = 2.0 / $ax;
153 $jsum = false;
154 $bjp = $ans = $sum = 0.0;
155 $bj = 1.0;
156 for ($j = 2 * ($ord + (int) sqrt(40.0 * $ord)); $j > 0; --$j) {
157 $bjm = $j * $tox * $bj - $bjp;
158 $bjp = $bj;
159 $bj = $bjm;
160 if (abs($bj) > 1.0e+10) {
161 $bj *= 1.0e-10;
162 $bjp *= 1.0e-10;
163 $ans *= 1.0e-10;
164 $sum *= 1.0e-10;
165 }
166 if ($jsum === true) {
167 $sum += $bj;
168 }
169 $jsum = $jsum === false;
170 if ($j === $ord) {
171 $ans = $bjp;
172 }
173 }
174 $sum = 2.0 * $sum - $bj;
175 $ans /= $sum;
176
177 return ($x < 0.0 && ($ord % 2) === 1) ? -$ans : $ans;
178 }
179 }
180