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 403]

   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Calculation\Engineering;
   4  
   5  use Complex\Complex as ComplexObject;
   6  use Complex\Exception as ComplexException;
   7  use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
   8  use PhpOffice\PhpSpreadsheet\Calculation\Exception;
   9  use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
  10  
  11  class Complex
  12  {
  13      use ArrayEnabled;
  14  
  15      /**
  16       * COMPLEX.
  17       *
  18       * Converts real and imaginary coefficients into a complex number of the form x +/- yi or x +/- yj.
  19       *
  20       * Excel Function:
  21       *        COMPLEX(realNumber,imaginary[,suffix])
  22       *
  23       * @param mixed $realNumber the real float coefficient of the complex number
  24       *                      Or can be an array of values
  25       * @param mixed $imaginary the imaginary float coefficient of the complex number
  26       *                      Or can be an array of values
  27       * @param mixed $suffix The character suffix for the imaginary component of the complex number.
  28       *                          If omitted, the suffix is assumed to be "i".
  29       *                      Or can be an array of values
  30       *
  31       * @return array|string
  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 COMPLEX($realNumber = 0.0, $imaginary = 0.0, $suffix = 'i')
  36      {
  37          if (is_array($realNumber) || is_array($imaginary) || is_array($suffix)) {
  38              return self::evaluateArrayArguments([self::class, __FUNCTION__], $realNumber, $imaginary, $suffix);
  39          }
  40  
  41          $realNumber = $realNumber ?? 0.0;
  42          $imaginary = $imaginary ?? 0.0;
  43          $suffix = $suffix ?? 'i';
  44  
  45          try {
  46              $realNumber = EngineeringValidations::validateFloat($realNumber);
  47              $imaginary = EngineeringValidations::validateFloat($imaginary);
  48          } catch (Exception $e) {
  49              return $e->getMessage();
  50          }
  51  
  52          if (($suffix == 'i') || ($suffix == 'j') || ($suffix == '')) {
  53              $complex = new ComplexObject($realNumber, $imaginary, $suffix);
  54  
  55              return (string) $complex;
  56          }
  57  
  58          return ExcelError::VALUE();
  59      }
  60  
  61      /**
  62       * IMAGINARY.
  63       *
  64       * Returns the imaginary coefficient of a complex number in x + yi or x + yj text format.
  65       *
  66       * Excel Function:
  67       *        IMAGINARY(complexNumber)
  68       *
  69       * @param array|string $complexNumber the complex number for which you want the imaginary
  70       *                                         coefficient
  71       *                      Or can be an array of values
  72       *
  73       * @return array|float|string (string if an error)
  74       *         If an array of numbers is passed as an argument, then the returned result will also be an array
  75       *            with the same dimensions
  76       */
  77      public static function IMAGINARY($complexNumber)
  78      {
  79          if (is_array($complexNumber)) {
  80              return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $complexNumber);
  81          }
  82  
  83          try {
  84              $complex = new ComplexObject($complexNumber);
  85          } catch (ComplexException $e) {
  86              return ExcelError::NAN();
  87          }
  88  
  89          return $complex->getImaginary();
  90      }
  91  
  92      /**
  93       * IMREAL.
  94       *
  95       * Returns the real coefficient of a complex number in x + yi or x + yj text format.
  96       *
  97       * Excel Function:
  98       *        IMREAL(complexNumber)
  99       *
 100       * @param array|string $complexNumber the complex number for which you want the real coefficient
 101       *                      Or can be an array of values
 102       *
 103       * @return array|float|string (string if an error)
 104       *         If an array of numbers is passed as an argument, then the returned result will also be an array
 105       *            with the same dimensions
 106       */
 107      public static function IMREAL($complexNumber)
 108      {
 109          if (is_array($complexNumber)) {
 110              return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $complexNumber);
 111          }
 112  
 113          try {
 114              $complex = new ComplexObject($complexNumber);
 115          } catch (ComplexException $e) {
 116              return ExcelError::NAN();
 117          }
 118  
 119          return $complex->getReal();
 120      }
 121  }