ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
BesselK.php
Go to the documentation of this file.
1 <?php
2 
4 
7 
8 class BesselK
9 {
28  public static function BESSELK($x, $ord)
29  {
31  $ord = Functions::flattenSingleValue($ord);
32 
33  try {
36  } catch (Exception $e) {
37  return $e->getMessage();
38  }
39 
40  if (($ord < 0) || ($x <= 0.0)) {
41  return Functions::NAN();
42  }
43 
44  $fBk = self::calculate($x, $ord);
45 
46  return (is_nan($fBk)) ? Functions::NAN() : $fBk;
47  }
48 
49  private static function calculate(float $x, int $ord): float
50  {
51  // special cases
52  switch ($ord) {
53  case 0:
54  return self::besselK0($x);
55  case 1:
56  return self::besselK1($x);
57  }
58 
59  return self::besselK2($x, $ord);
60  }
61 
62  private static function besselK0(float $x): float
63  {
64  if ($x <= 2) {
65  $fNum2 = $x * 0.5;
66  $y = ($fNum2 * $fNum2);
67 
68  return -log($fNum2) * BesselI::BESSELI($x, 0) +
69  (-0.57721566 + $y * (0.42278420 + $y * (0.23069756 + $y * (0.3488590e-1 + $y * (0.262698e-2 + $y *
70  (0.10750e-3 + $y * 0.74e-5))))));
71  }
72 
73  $y = 2 / $x;
74 
75  return exp(-$x) / sqrt($x) *
76  (1.25331414 + $y * (-0.7832358e-1 + $y * (0.2189568e-1 + $y * (-0.1062446e-1 + $y *
77  (0.587872e-2 + $y * (-0.251540e-2 + $y * 0.53208e-3))))));
78  }
79 
80  private static function besselK1(float $x): float
81  {
82  if ($x <= 2) {
83  $fNum2 = $x * 0.5;
84  $y = ($fNum2 * $fNum2);
85 
86  return log($fNum2) * BesselI::BESSELI($x, 1) +
87  (1 + $y * (0.15443144 + $y * (-0.67278579 + $y * (-0.18156897 + $y * (-0.1919402e-1 + $y *
88  (-0.110404e-2 + $y * (-0.4686e-4))))))) / $x;
89  }
90 
91  $y = 2 / $x;
92 
93  return exp(-$x) / sqrt($x) *
94  (1.25331414 + $y * (0.23498619 + $y * (-0.3655620e-1 + $y * (0.1504268e-1 + $y * (-0.780353e-2 + $y *
95  (0.325614e-2 + $y * (-0.68245e-3)))))));
96  }
97 
98  private static function besselK2(float $x, int $ord)
99  {
100  $fTox = 2 / $x;
101  $fBkm = self::besselK0($x);
102  $fBk = self::besselK1($x);
103  for ($n = 1; $n < $ord; ++$n) {
104  $fBkp = $fBkm + $n * $fTox * $fBk;
105  $fBkm = $fBk;
106  $fBk = $fBkp;
107  }
108 
109  return $fBk;
110  }
111 }
$y
Definition: example_007.php:83
$n
Definition: RandomTest.php:85
static flattenSingleValue($value='')
Convert an array to a single scalar value by extracting the first element.
Definition: Functions.php:649
$x
Definition: complexTest.php:9