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\MathTrig;
   4  
   5  use PhpOffice\PhpSpreadsheet\Calculation\Exception;
   6  use PhpOffice\PhpSpreadsheet\Calculation\Functions;
   7  use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
   8  
   9  class SumSquares
  10  {
  11      /**
  12       * SUMSQ.
  13       *
  14       * SUMSQ returns the sum of the squares of the arguments
  15       *
  16       * Excel Function:
  17       *        SUMSQ(value1[,value2[, ...]])
  18       *
  19       * @param mixed ...$args Data values
  20       *
  21       * @return float|string
  22       */
  23      public static function sumSquare(...$args)
  24      {
  25          try {
  26              $returnValue = 0;
  27  
  28              // Loop through arguments
  29              foreach (Functions::flattenArray($args) as $arg) {
  30                  $arg1 = Helpers::validateNumericNullSubstitution($arg, 0);
  31                  $returnValue += ($arg1 * $arg1);
  32              }
  33          } catch (Exception $e) {
  34              return $e->getMessage();
  35          }
  36  
  37          return $returnValue;
  38      }
  39  
  40      private static function getCount(array $array1, array $array2): int
  41      {
  42          $count = count($array1);
  43          if ($count !== count($array2)) {
  44              throw new Exception(ExcelError::NA());
  45          }
  46  
  47          return $count;
  48      }
  49  
  50      /**
  51       * These functions accept only numeric arguments, not even strings which are numeric.
  52       *
  53       * @param mixed $item
  54       */
  55      private static function numericNotString($item): bool
  56      {
  57          return is_numeric($item) && !is_string($item);
  58      }
  59  
  60      /**
  61       * SUMX2MY2.
  62       *
  63       * @param mixed[] $matrixData1 Matrix #1
  64       * @param mixed[] $matrixData2 Matrix #2
  65       *
  66       * @return float|string
  67       */
  68      public static function sumXSquaredMinusYSquared($matrixData1, $matrixData2)
  69      {
  70          try {
  71              $array1 = Functions::flattenArray($matrixData1);
  72              $array2 = Functions::flattenArray($matrixData2);
  73              $count = self::getCount($array1, $array2);
  74  
  75              $result = 0;
  76              for ($i = 0; $i < $count; ++$i) {
  77                  if (self::numericNotString($array1[$i]) && self::numericNotString($array2[$i])) {
  78                      $result += ($array1[$i] * $array1[$i]) - ($array2[$i] * $array2[$i]);
  79                  }
  80              }
  81          } catch (Exception $e) {
  82              return $e->getMessage();
  83          }
  84  
  85          return $result;
  86      }
  87  
  88      /**
  89       * SUMX2PY2.
  90       *
  91       * @param mixed[] $matrixData1 Matrix #1
  92       * @param mixed[] $matrixData2 Matrix #2
  93       *
  94       * @return float|string
  95       */
  96      public static function sumXSquaredPlusYSquared($matrixData1, $matrixData2)
  97      {
  98          try {
  99              $array1 = Functions::flattenArray($matrixData1);
 100              $array2 = Functions::flattenArray($matrixData2);
 101              $count = self::getCount($array1, $array2);
 102  
 103              $result = 0;
 104              for ($i = 0; $i < $count; ++$i) {
 105                  if (self::numericNotString($array1[$i]) && self::numericNotString($array2[$i])) {
 106                      $result += ($array1[$i] * $array1[$i]) + ($array2[$i] * $array2[$i]);
 107                  }
 108              }
 109          } catch (Exception $e) {
 110              return $e->getMessage();
 111          }
 112  
 113          return $result;
 114      }
 115  
 116      /**
 117       * SUMXMY2.
 118       *
 119       * @param mixed[] $matrixData1 Matrix #1
 120       * @param mixed[] $matrixData2 Matrix #2
 121       *
 122       * @return float|string
 123       */
 124      public static function sumXMinusYSquared($matrixData1, $matrixData2)
 125      {
 126          try {
 127              $array1 = Functions::flattenArray($matrixData1);
 128              $array2 = Functions::flattenArray($matrixData2);
 129              $count = self::getCount($array1, $array2);
 130  
 131              $result = 0;
 132              for ($i = 0; $i < $count; ++$i) {
 133                  if (self::numericNotString($array1[$i]) && self::numericNotString($array2[$i])) {
 134                      $result += ($array1[$i] - $array2[$i]) * ($array1[$i] - $array2[$i]);
 135                  }
 136              }
 137          } catch (Exception $e) {
 138              return $e->getMessage();
 139          }
 140  
 141          return $result;
 142      }
 143  }