Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 400 and 401] [Versions 400 and 402] [Versions 400 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\Functions;
   8  
   9  class ComplexFunctions
  10  {
  11      /**
  12       * IMABS.
  13       *
  14       * Returns the absolute value (modulus) of a complex number in x + yi or x + yj text format.
  15       *
  16       * Excel Function:
  17       *        IMABS(complexNumber)
  18       *
  19       * @param string $complexNumber the complex number for which you want the absolute value
  20       *
  21       * @return float|string
  22       */
  23      public static function IMABS($complexNumber)
  24      {
  25          $complexNumber = Functions::flattenSingleValue($complexNumber);
  26  
  27          try {
  28              $complex = new ComplexObject($complexNumber);
  29          } catch (ComplexException $e) {
  30              return Functions::NAN();
  31          }
  32  
  33          return $complex->abs();
  34      }
  35  
  36      /**
  37       * IMARGUMENT.
  38       *
  39       * Returns the argument theta of a complex number, i.e. the angle in radians from the real
  40       * axis to the representation of the number in polar coordinates.
  41       *
  42       * Excel Function:
  43       *        IMARGUMENT(complexNumber)
  44       *
  45       * @param string $complexNumber the complex number for which you want the argument theta
  46       *
  47       * @return float|string
  48       */
  49      public static function IMARGUMENT($complexNumber)
  50      {
  51          $complexNumber = Functions::flattenSingleValue($complexNumber);
  52  
  53          try {
  54              $complex = new ComplexObject($complexNumber);
  55          } catch (ComplexException $e) {
  56              return Functions::NAN();
  57          }
  58  
  59          if ($complex->getReal() == 0.0 && $complex->getImaginary() == 0.0) {
  60              return Functions::DIV0();
  61          }
  62  
  63          return $complex->argument();
  64      }
  65  
  66      /**
  67       * IMCONJUGATE.
  68       *
  69       * Returns the complex conjugate of a complex number in x + yi or x + yj text format.
  70       *
  71       * Excel Function:
  72       *        IMCONJUGATE(complexNumber)
  73       *
  74       * @param string $complexNumber the complex number for which you want the conjugate
  75       *
  76       * @return string
  77       */
  78      public static function IMCONJUGATE($complexNumber)
  79      {
  80          $complexNumber = Functions::flattenSingleValue($complexNumber);
  81  
  82          try {
  83              $complex = new ComplexObject($complexNumber);
  84          } catch (ComplexException $e) {
  85              return Functions::NAN();
  86          }
  87  
  88          return (string) $complex->conjugate();
  89      }
  90  
  91      /**
  92       * IMCOS.
  93       *
  94       * Returns the cosine of a complex number in x + yi or x + yj text format.
  95       *
  96       * Excel Function:
  97       *        IMCOS(complexNumber)
  98       *
  99       * @param string $complexNumber the complex number for which you want the cosine
 100       *
 101       * @return float|string
 102       */
 103      public static function IMCOS($complexNumber)
 104      {
 105          $complexNumber = Functions::flattenSingleValue($complexNumber);
 106  
 107          try {
 108              $complex = new ComplexObject($complexNumber);
 109          } catch (ComplexException $e) {
 110              return Functions::NAN();
 111          }
 112  
 113          return (string) $complex->cos();
 114      }
 115  
 116      /**
 117       * IMCOSH.
 118       *
 119       * Returns the hyperbolic cosine of a complex number in x + yi or x + yj text format.
 120       *
 121       * Excel Function:
 122       *        IMCOSH(complexNumber)
 123       *
 124       * @param string $complexNumber the complex number for which you want the hyperbolic cosine
 125       *
 126       * @return float|string
 127       */
 128      public static function IMCOSH($complexNumber)
 129      {
 130          $complexNumber = Functions::flattenSingleValue($complexNumber);
 131  
 132          try {
 133              $complex = new ComplexObject($complexNumber);
 134          } catch (ComplexException $e) {
 135              return Functions::NAN();
 136          }
 137  
 138          return (string) $complex->cosh();
 139      }
 140  
 141      /**
 142       * IMCOT.
 143       *
 144       * Returns the cotangent of a complex number in x + yi or x + yj text format.
 145       *
 146       * Excel Function:
 147       *        IMCOT(complexNumber)
 148       *
 149       * @param string $complexNumber the complex number for which you want the cotangent
 150       *
 151       * @return float|string
 152       */
 153      public static function IMCOT($complexNumber)
 154      {
 155          $complexNumber = Functions::flattenSingleValue($complexNumber);
 156  
 157          try {
 158              $complex = new ComplexObject($complexNumber);
 159          } catch (ComplexException $e) {
 160              return Functions::NAN();
 161          }
 162  
 163          return (string) $complex->cot();
 164      }
 165  
 166      /**
 167       * IMCSC.
 168       *
 169       * Returns the cosecant of a complex number in x + yi or x + yj text format.
 170       *
 171       * Excel Function:
 172       *        IMCSC(complexNumber)
 173       *
 174       * @param string $complexNumber the complex number for which you want the cosecant
 175       *
 176       * @return float|string
 177       */
 178      public static function IMCSC($complexNumber)
 179      {
 180          $complexNumber = Functions::flattenSingleValue($complexNumber);
 181  
 182          try {
 183              $complex = new ComplexObject($complexNumber);
 184          } catch (ComplexException $e) {
 185              return Functions::NAN();
 186          }
 187  
 188          return (string) $complex->csc();
 189      }
 190  
 191      /**
 192       * IMCSCH.
 193       *
 194       * Returns the hyperbolic cosecant of a complex number in x + yi or x + yj text format.
 195       *
 196       * Excel Function:
 197       *        IMCSCH(complexNumber)
 198       *
 199       * @param string $complexNumber the complex number for which you want the hyperbolic cosecant
 200       *
 201       * @return float|string
 202       */
 203      public static function IMCSCH($complexNumber)
 204      {
 205          $complexNumber = Functions::flattenSingleValue($complexNumber);
 206  
 207          try {
 208              $complex = new ComplexObject($complexNumber);
 209          } catch (ComplexException $e) {
 210              return Functions::NAN();
 211          }
 212  
 213          return (string) $complex->csch();
 214      }
 215  
 216      /**
 217       * IMSIN.
 218       *
 219       * Returns the sine of a complex number in x + yi or x + yj text format.
 220       *
 221       * Excel Function:
 222       *        IMSIN(complexNumber)
 223       *
 224       * @param string $complexNumber the complex number for which you want the sine
 225       *
 226       * @return float|string
 227       */
 228      public static function IMSIN($complexNumber)
 229      {
 230          $complexNumber = Functions::flattenSingleValue($complexNumber);
 231  
 232          try {
 233              $complex = new ComplexObject($complexNumber);
 234          } catch (ComplexException $e) {
 235              return Functions::NAN();
 236          }
 237  
 238          return (string) $complex->sin();
 239      }
 240  
 241      /**
 242       * IMSINH.
 243       *
 244       * Returns the hyperbolic sine of a complex number in x + yi or x + yj text format.
 245       *
 246       * Excel Function:
 247       *        IMSINH(complexNumber)
 248       *
 249       * @param string $complexNumber the complex number for which you want the hyperbolic sine
 250       *
 251       * @return float|string
 252       */
 253      public static function IMSINH($complexNumber)
 254      {
 255          $complexNumber = Functions::flattenSingleValue($complexNumber);
 256  
 257          try {
 258              $complex = new ComplexObject($complexNumber);
 259          } catch (ComplexException $e) {
 260              return Functions::NAN();
 261          }
 262  
 263          return (string) $complex->sinh();
 264      }
 265  
 266      /**
 267       * IMSEC.
 268       *
 269       * Returns the secant of a complex number in x + yi or x + yj text format.
 270       *
 271       * Excel Function:
 272       *        IMSEC(complexNumber)
 273       *
 274       * @param string $complexNumber the complex number for which you want the secant
 275       *
 276       * @return float|string
 277       */
 278      public static function IMSEC($complexNumber)
 279      {
 280          $complexNumber = Functions::flattenSingleValue($complexNumber);
 281  
 282          try {
 283              $complex = new ComplexObject($complexNumber);
 284          } catch (ComplexException $e) {
 285              return Functions::NAN();
 286          }
 287  
 288          return (string) $complex->sec();
 289      }
 290  
 291      /**
 292       * IMSECH.
 293       *
 294       * Returns the hyperbolic secant of a complex number in x + yi or x + yj text format.
 295       *
 296       * Excel Function:
 297       *        IMSECH(complexNumber)
 298       *
 299       * @param string $complexNumber the complex number for which you want the hyperbolic secant
 300       *
 301       * @return float|string
 302       */
 303      public static function IMSECH($complexNumber)
 304      {
 305          $complexNumber = Functions::flattenSingleValue($complexNumber);
 306  
 307          try {
 308              $complex = new ComplexObject($complexNumber);
 309          } catch (ComplexException $e) {
 310              return Functions::NAN();
 311          }
 312  
 313          return (string) $complex->sech();
 314      }
 315  
 316      /**
 317       * IMTAN.
 318       *
 319       * Returns the tangent of a complex number in x + yi or x + yj text format.
 320       *
 321       * Excel Function:
 322       *        IMTAN(complexNumber)
 323       *
 324       * @param string $complexNumber the complex number for which you want the tangent
 325       *
 326       * @return float|string
 327       */
 328      public static function IMTAN($complexNumber)
 329      {
 330          $complexNumber = Functions::flattenSingleValue($complexNumber);
 331  
 332          try {
 333              $complex = new ComplexObject($complexNumber);
 334          } catch (ComplexException $e) {
 335              return Functions::NAN();
 336          }
 337  
 338          return (string) $complex->tan();
 339      }
 340  
 341      /**
 342       * IMSQRT.
 343       *
 344       * Returns the square root of a complex number in x + yi or x + yj text format.
 345       *
 346       * Excel Function:
 347       *        IMSQRT(complexNumber)
 348       *
 349       * @param string $complexNumber the complex number for which you want the square root
 350       *
 351       * @return string
 352       */
 353      public static function IMSQRT($complexNumber)
 354      {
 355          $complexNumber = Functions::flattenSingleValue($complexNumber);
 356  
 357          try {
 358              $complex = new ComplexObject($complexNumber);
 359          } catch (ComplexException $e) {
 360              return Functions::NAN();
 361          }
 362  
 363          $theta = self::IMARGUMENT($complexNumber);
 364          if ($theta === Functions::DIV0()) {
 365              return '0';
 366          }
 367  
 368          return (string) $complex->sqrt();
 369      }
 370  
 371      /**
 372       * IMLN.
 373       *
 374       * Returns the natural logarithm of a complex number in x + yi or x + yj text format.
 375       *
 376       * Excel Function:
 377       *        IMLN(complexNumber)
 378       *
 379       * @param string $complexNumber the complex number for which you want the natural logarithm
 380       *
 381       * @return string
 382       */
 383      public static function IMLN($complexNumber)
 384      {
 385          $complexNumber = Functions::flattenSingleValue($complexNumber);
 386  
 387          try {
 388              $complex = new ComplexObject($complexNumber);
 389          } catch (ComplexException $e) {
 390              return Functions::NAN();
 391          }
 392  
 393          if ($complex->getReal() == 0.0 && $complex->getImaginary() == 0.0) {
 394              return Functions::NAN();
 395          }
 396  
 397          return (string) $complex->ln();
 398      }
 399  
 400      /**
 401       * IMLOG10.
 402       *
 403       * Returns the common logarithm (base 10) of a complex number in x + yi or x + yj text format.
 404       *
 405       * Excel Function:
 406       *        IMLOG10(complexNumber)
 407       *
 408       * @param string $complexNumber the complex number for which you want the common logarithm
 409       *
 410       * @return string
 411       */
 412      public static function IMLOG10($complexNumber)
 413      {
 414          $complexNumber = Functions::flattenSingleValue($complexNumber);
 415  
 416          try {
 417              $complex = new ComplexObject($complexNumber);
 418          } catch (ComplexException $e) {
 419              return Functions::NAN();
 420          }
 421  
 422          if ($complex->getReal() == 0.0 && $complex->getImaginary() == 0.0) {
 423              return Functions::NAN();
 424          }
 425  
 426          return (string) $complex->log10();
 427      }
 428  
 429      /**
 430       * IMLOG2.
 431       *
 432       * Returns the base-2 logarithm of a complex number in x + yi or x + yj text format.
 433       *
 434       * Excel Function:
 435       *        IMLOG2(complexNumber)
 436       *
 437       * @param string $complexNumber the complex number for which you want the base-2 logarithm
 438       *
 439       * @return string
 440       */
 441      public static function IMLOG2($complexNumber)
 442      {
 443          $complexNumber = Functions::flattenSingleValue($complexNumber);
 444  
 445          try {
 446              $complex = new ComplexObject($complexNumber);
 447          } catch (ComplexException $e) {
 448              return Functions::NAN();
 449          }
 450  
 451          if ($complex->getReal() == 0.0 && $complex->getImaginary() == 0.0) {
 452              return Functions::NAN();
 453          }
 454  
 455          return (string) $complex->log2();
 456      }
 457  
 458      /**
 459       * IMEXP.
 460       *
 461       * Returns the exponential of a complex number in x + yi or x + yj text format.
 462       *
 463       * Excel Function:
 464       *        IMEXP(complexNumber)
 465       *
 466       * @param string $complexNumber the complex number for which you want the exponential
 467       *
 468       * @return string
 469       */
 470      public static function IMEXP($complexNumber)
 471      {
 472          $complexNumber = Functions::flattenSingleValue($complexNumber);
 473  
 474          try {
 475              $complex = new ComplexObject($complexNumber);
 476          } catch (ComplexException $e) {
 477              return Functions::NAN();
 478          }
 479  
 480          return (string) $complex->exp();
 481      }
 482  
 483      /**
 484       * IMPOWER.
 485       *
 486       * Returns a complex number in x + yi or x + yj text format raised to a power.
 487       *
 488       * Excel Function:
 489       *        IMPOWER(complexNumber,realNumber)
 490       *
 491       * @param string $complexNumber the complex number you want to raise to a power
 492       * @param float $realNumber the power to which you want to raise the complex number
 493       *
 494       * @return string
 495       */
 496      public static function IMPOWER($complexNumber, $realNumber)
 497      {
 498          $complexNumber = Functions::flattenSingleValue($complexNumber);
 499          $realNumber = Functions::flattenSingleValue($realNumber);
 500  
 501          try {
 502              $complex = new ComplexObject($complexNumber);
 503          } catch (ComplexException $e) {
 504              return Functions::NAN();
 505          }
 506  
 507          if (!is_numeric($realNumber)) {
 508              return Functions::VALUE();
 509          }
 510  
 511          return (string) $complex->pow($realNumber);
 512      }
 513  }