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 Matrix\Builder;
   6  use Matrix\Div0Exception as MatrixDiv0Exception;
   7  use Matrix\Exception as MatrixException;
   8  use Matrix\Matrix;
   9  use PhpOffice\PhpSpreadsheet\Calculation\Exception;
  10  use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
  11  
  12  class MatrixFunctions
  13  {
  14      /**
  15       * Convert parameter to Matrix.
  16       *
  17       * @param mixed $matrixValues A matrix of values
  18       */
  19      private static function getMatrix($matrixValues): Matrix
  20      {
  21          $matrixData = [];
  22          if (!is_array($matrixValues)) {
  23              $matrixValues = [[$matrixValues]];
  24          }
  25  
  26          $row = 0;
  27          foreach ($matrixValues as $matrixRow) {
  28              if (!is_array($matrixRow)) {
  29                  $matrixRow = [$matrixRow];
  30              }
  31              $column = 0;
  32              foreach ($matrixRow as $matrixCell) {
  33                  if ((is_string($matrixCell)) || ($matrixCell === null)) {
  34                      throw new Exception(ExcelError::VALUE());
  35                  }
  36                  $matrixData[$row][$column] = $matrixCell;
  37                  ++$column;
  38              }
  39              ++$row;
  40          }
  41  
  42          return new Matrix($matrixData);
  43      }
  44  
  45      /**
  46       * SEQUENCE.
  47       *
  48       * Generates a list of sequential numbers in an array.
  49       *
  50       * Excel Function:
  51       *      SEQUENCE(rows,[columns],[start],[step])
  52       *
  53       * @param mixed $rows the number of rows to return, defaults to 1
  54       * @param mixed $columns the number of columns to return, defaults to 1
  55       * @param mixed $start the first number in the sequence, defaults to 1
  56       * @param mixed $step the amount to increment each subsequent value in the array, defaults to 1
  57       *
  58       * @return array|string The resulting array, or a string containing an error
  59       */
  60      public static function sequence($rows = 1, $columns = 1, $start = 1, $step = 1)
  61      {
  62          try {
  63              $rows = (int) Helpers::validateNumericNullSubstitution($rows, 1);
  64              Helpers::validatePositive($rows);
  65              $columns = (int) Helpers::validateNumericNullSubstitution($columns, 1);
  66              Helpers::validatePositive($columns);
  67              $start = Helpers::validateNumericNullSubstitution($start, 1);
  68              $step = Helpers::validateNumericNullSubstitution($step, 1);
  69          } catch (Exception $e) {
  70              return $e->getMessage();
  71          }
  72  
  73          if ($step === 0) {
  74              return array_chunk(
  75                  array_fill(0, $rows * $columns, $start),
  76                  max($columns, 1)
  77              );
  78          }
  79  
  80          return array_chunk(
  81              range($start, $start + (($rows * $columns - 1) * $step), $step),
  82              max($columns, 1)
  83          );
  84      }
  85  
  86      /**
  87       * MDETERM.
  88       *
  89       * Returns the matrix determinant of an array.
  90       *
  91       * Excel Function:
  92       *        MDETERM(array)
  93       *
  94       * @param mixed $matrixValues A matrix of values
  95       *
  96       * @return float|string The result, or a string containing an error
  97       */
  98      public static function determinant($matrixValues)
  99      {
 100          try {
 101              $matrix = self::getMatrix($matrixValues);
 102  
 103              return $matrix->determinant();
 104          } catch (MatrixException $ex) {
 105              return ExcelError::VALUE();
 106          } catch (Exception $e) {
 107              return $e->getMessage();
 108          }
 109      }
 110  
 111      /**
 112       * MINVERSE.
 113       *
 114       * Returns the inverse matrix for the matrix stored in an array.
 115       *
 116       * Excel Function:
 117       *        MINVERSE(array)
 118       *
 119       * @param mixed $matrixValues A matrix of values
 120       *
 121       * @return array|string The result, or a string containing an error
 122       */
 123      public static function inverse($matrixValues)
 124      {
 125          try {
 126              $matrix = self::getMatrix($matrixValues);
 127  
 128              return $matrix->inverse()->toArray();
 129          } catch (MatrixDiv0Exception $e) {
 130              return ExcelError::NAN();
 131          } catch (MatrixException $e) {
 132              return ExcelError::VALUE();
 133          } catch (Exception $e) {
 134              return $e->getMessage();
 135          }
 136      }
 137  
 138      /**
 139       * MMULT.
 140       *
 141       * @param mixed $matrixData1 A matrix of values
 142       * @param mixed $matrixData2 A matrix of values
 143       *
 144       * @return array|string The result, or a string containing an error
 145       */
 146      public static function multiply($matrixData1, $matrixData2)
 147      {
 148          try {
 149              $matrixA = self::getMatrix($matrixData1);
 150              $matrixB = self::getMatrix($matrixData2);
 151  
 152              return $matrixA->multiply($matrixB)->toArray();
 153          } catch (MatrixException $ex) {
 154              return ExcelError::VALUE();
 155          } catch (Exception $e) {
 156              return $e->getMessage();
 157          }
 158      }
 159  
 160      /**
 161       * MUnit.
 162       *
 163       * @param mixed $dimension Number of rows and columns
 164       *
 165       * @return array|string The result, or a string containing an error
 166       */
 167      public static function identity($dimension)
 168      {
 169          try {
 170              $dimension = (int) Helpers::validateNumericNullBool($dimension);
 171              Helpers::validatePositive($dimension, ExcelError::VALUE());
 172              $matrix = Builder::createIdentityMatrix($dimension, 0)->toArray();
 173  
 174              return $matrix;
 175          } catch (Exception $e) {
 176              return $e->getMessage();
 177          }
 178      }
 179  }