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]

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