Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 400 and 401] [Versions 401 and 402] [Versions 401 and 403]

   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 BesselI
  10  {
  11      use ArrayEnabled;
  12  
  13      /**
  14       * BESSELI.
  15       *
  16       *    Returns the modified Bessel function In(x), which is equivalent to the Bessel function evaluated
  17       *        for purely imaginary arguments
  18       *
  19       *    Excel Function:
  20       *        BESSELI(x,ord)
  21       *
  22       * NOTE: The MS Excel implementation of the BESSELI function is still not accurate.
  23       *       This code provides a more accurate calculation
  24       *
  25       * @param mixed $x A float value at which to evaluate the function.
  26       *                                If x is nonnumeric, BESSELI returns the #VALUE! error value.
  27       *                      Or can be an array of values
  28       * @param mixed $ord The integer order of the Bessel function.
  29       *                                If ord is not an integer, it is truncated.
  30       *                                If $ord is nonnumeric, BESSELI returns the #VALUE! error value.
  31       *                                If $ord < 0, BESSELI returns the #NUM! error value.
  32       *                      Or can be an array of values
  33       *
  34       * @return array|float|string Result, or a string containing an error
  35       *         If an array of numbers is passed as an argument, then the returned result will also be an array
  36       *            with the same dimensions
  37       */
  38      public static function BESSELI($x, $ord)
  39      {
  40          if (is_array($x) || is_array($ord)) {
  41              return self::evaluateArrayArguments([self::class, __FUNCTION__], $x, $ord);
  42          }
  43  
  44          try {
  45              $x = EngineeringValidations::validateFloat($x);
  46              $ord = EngineeringValidations::validateInt($ord);
  47          } catch (Exception $e) {
  48              return $e->getMessage();
  49          }
  50  
  51          if ($ord < 0) {
  52              return ExcelError::NAN();
  53          }
  54  
  55          $fResult = self::calculate($x, $ord);
  56  
  57          return (is_nan($fResult)) ? ExcelError::NAN() : $fResult;
  58      }
  59  
  60      private static function calculate(float $x, int $ord): float
  61      {
  62          // special cases
  63          switch ($ord) {
  64              case 0:
  65                  return self::besselI0($x);
  66              case 1:
  67                  return self::besselI1($x);
  68          }
  69  
  70          return self::besselI2($x, $ord);
  71      }
  72  
  73      private static function besselI0(float $x): float
  74      {
  75          $ax = abs($x);
  76  
  77          if ($ax < 3.75) {
  78              $y = $x / 3.75;
  79              $y = $y * $y;
  80  
  81              return 1.0 + $y * (3.5156229 + $y * (3.0899424 + $y * (1.2067492
  82                                  + $y * (0.2659732 + $y * (0.360768e-1 + $y * 0.45813e-2)))));
  83          }
  84  
  85          $y = 3.75 / $ax;
  86  
  87          return (exp($ax) / sqrt($ax)) * (0.39894228 + $y * (0.1328592e-1 + $y * (0.225319e-2 + $y * (-0.157565e-2
  88                              + $y * (0.916281e-2 + $y * (-0.2057706e-1 + $y * (0.2635537e-1 +
  89                                          $y * (-0.1647633e-1 + $y * 0.392377e-2))))))));
  90      }
  91  
  92      private static function besselI1(float $x): float
  93      {
  94          $ax = abs($x);
  95  
  96          if ($ax < 3.75) {
  97              $y = $x / 3.75;
  98              $y = $y * $y;
  99              $ans = $ax * (0.5 + $y * (0.87890594 + $y * (0.51498869 + $y * (0.15084934 + $y * (0.2658733e-1 +
 100                                      $y * (0.301532e-2 + $y * 0.32411e-3))))));
 101  
 102              return ($x < 0.0) ? -$ans : $ans;
 103          }
 104  
 105          $y = 3.75 / $ax;
 106          $ans = 0.2282967e-1 + $y * (-0.2895312e-1 + $y * (0.1787654e-1 - $y * 0.420059e-2));
 107          $ans = 0.39894228 + $y * (-0.3988024e-1 + $y * (-0.362018e-2 + $y * (0.163801e-2 +
 108                          $y * (-0.1031555e-1 + $y * $ans))));
 109          $ans *= exp($ax) / sqrt($ax);
 110  
 111          return ($x < 0.0) ? -$ans : $ans;
 112      }
 113  
 114      private static function besselI2(float $x, int $ord): float
 115      {
 116          if ($x === 0.0) {
 117              return 0.0;
 118          }
 119  
 120          $tox = 2.0 / abs($x);
 121          $bip = 0;
 122          $ans = 0.0;
 123          $bi = 1.0;
 124  
 125          for ($j = 2 * ($ord + (int) sqrt(40.0 * $ord)); $j > 0; --$j) {
 126              $bim = $bip + $j * $tox * $bi;
 127              $bip = $bi;
 128              $bi = $bim;
 129  
 130              if (abs($bi) > 1.0e+12) {
 131                  $ans *= 1.0e-12;
 132                  $bi *= 1.0e-12;
 133                  $bip *= 1.0e-12;
 134              }
 135  
 136              if ($j === $ord) {
 137                  $ans = $bip;
 138              }
 139          }
 140  
 141          $ans *= self::besselI0($x) / $bi;
 142  
 143          return ($x < 0.0 && (($ord % 2) === 1)) ? -$ans : $ans;
 144      }
 145  }