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