Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 39 and 311]

   1  <?php
   2  
   3  /**
   4   *
   5   * Function code for the complex addition operation
   6   *
   7   * @copyright  Copyright (c) 2013-2018 Mark Baker (https://github.com/MarkBaker/PHPComplex)
   8   * @license    https://opensource.org/licenses/MIT    MIT
   9   */
  10  namespace Complex;
  11  
  12  /**
  13   * Adds two or more complex numbers
  14   *
  15   * @param     array of string|integer|float|Complex    $complexValues   The numbers to add
  16   * @return    Complex
  17   */
  18  function add(...$complexValues): Complex
  19  {
  20      if (count($complexValues) < 2) {
  21          throw new \Exception('This function requires at least 2 arguments');
  22      }
  23  
  24      $base = array_shift($complexValues);
  25      $result = clone Complex::validateComplexArgument($base);
  26  
  27      foreach ($complexValues as $complex) {
  28          $complex = Complex::validateComplexArgument($complex);
  29  
  30          if ($result->isComplex() && $complex->isComplex() &&
  31              $result->getSuffix() !== $complex->getSuffix()) {
  32              throw new Exception('Suffix Mismatch');
  33          }
  34  
  35          $real = $result->getReal() + $complex->getReal();
  36          $imaginary = $result->getImaginary() + $complex->getImaginary();
  37  
  38          $result = new Complex(
  39              $real,
  40              $imaginary,
  41              ($imaginary == 0.0) ? null : max($result->getSuffix(), $complex->getSuffix())
  42          );
  43      }
  44  
  45      return $result;
  46  }