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.
   1  <?php
   2  
   3  namespace Matrix;
   4  
   5  use Matrix\Operators\Addition;
   6  use Matrix\Operators\DirectSum;
   7  use Matrix\Operators\Division;
   8  use Matrix\Operators\Multiplication;
   9  use Matrix\Operators\Subtraction;
  10  
  11  class Operations
  12  {
  13      public static function add(...$matrixValues): Matrix
  14      {
  15          if (count($matrixValues) < 2) {
  16              throw new Exception('Addition operation requires at least 2 arguments');
  17          }
  18  
  19          $matrix = array_shift($matrixValues);
  20  
  21          if (is_array($matrix)) {
  22              $matrix = new Matrix($matrix);
  23          }
  24          if (!$matrix instanceof Matrix) {
  25              throw new Exception('Addition arguments must be Matrix or array');
  26          }
  27  
  28          $result = new Addition($matrix);
  29  
  30          foreach ($matrixValues as $matrix) {
  31              $result->execute($matrix);
  32          }
  33  
  34          return $result->result();
  35      }
  36  
  37      public static function directsum(...$matrixValues): Matrix
  38      {
  39          if (count($matrixValues) < 2) {
  40              throw new Exception('DirectSum operation requires at least 2 arguments');
  41          }
  42  
  43          $matrix = array_shift($matrixValues);
  44  
  45          if (is_array($matrix)) {
  46              $matrix = new Matrix($matrix);
  47          }
  48          if (!$matrix instanceof Matrix) {
  49              throw new Exception('DirectSum arguments must be Matrix or array');
  50          }
  51  
  52          $result = new DirectSum($matrix);
  53  
  54          foreach ($matrixValues as $matrix) {
  55              $result->execute($matrix);
  56          }
  57  
  58          return $result->result();
  59      }
  60  
  61      public static function divideby(...$matrixValues): Matrix
  62      {
  63          if (count($matrixValues) < 2) {
  64              throw new Exception('Division operation requires at least 2 arguments');
  65          }
  66  
  67          $matrix = array_shift($matrixValues);
  68  
  69          if (is_array($matrix)) {
  70              $matrix = new Matrix($matrix);
  71          }
  72          if (!$matrix instanceof Matrix) {
  73              throw new Exception('Division arguments must be Matrix or array');
  74          }
  75  
  76          $result = new Division($matrix);
  77  
  78          foreach ($matrixValues as $matrix) {
  79              $result->execute($matrix);
  80          }
  81  
  82          return $result->result();
  83      }
  84  
  85      public static function divideinto(...$matrixValues): Matrix
  86      {
  87          if (count($matrixValues) < 2) {
  88              throw new Exception('Division operation requires at least 2 arguments');
  89          }
  90  
  91          $matrix = array_pop($matrixValues);
  92          $matrixValues = array_reverse($matrixValues);
  93  
  94          if (is_array($matrix)) {
  95              $matrix = new Matrix($matrix);
  96          }
  97          if (!$matrix instanceof Matrix) {
  98              throw new Exception('Division arguments must be Matrix or array');
  99          }
 100  
 101          $result = new Division($matrix);
 102  
 103          foreach ($matrixValues as $matrix) {
 104              $result->execute($matrix);
 105          }
 106  
 107          return $result->result();
 108      }
 109  
 110      public static function multiply(...$matrixValues): Matrix
 111      {
 112          if (count($matrixValues) < 2) {
 113              throw new Exception('Multiplication operation requires at least 2 arguments');
 114          }
 115  
 116          $matrix = array_shift($matrixValues);
 117  
 118          if (is_array($matrix)) {
 119              $matrix = new Matrix($matrix);
 120          }
 121          if (!$matrix instanceof Matrix) {
 122              throw new Exception('Multiplication arguments must be Matrix or array');
 123          }
 124  
 125          $result = new Multiplication($matrix);
 126  
 127          foreach ($matrixValues as $matrix) {
 128              $result->execute($matrix);
 129          }
 130  
 131          return $result->result();
 132      }
 133  
 134      public static function subtract(...$matrixValues): Matrix
 135      {
 136          if (count($matrixValues) < 2) {
 137              throw new Exception('Subtraction operation requires at least 2 arguments');
 138          }
 139  
 140          $matrix = array_shift($matrixValues);
 141  
 142          if (is_array($matrix)) {
 143              $matrix = new Matrix($matrix);
 144          }
 145          if (!$matrix instanceof Matrix) {
 146              throw new Exception('Subtraction arguments must be Matrix or array');
 147          }
 148  
 149          $result = new Subtraction($matrix);
 150  
 151          foreach ($matrixValues as $matrix) {
 152              $result->execute($matrix);
 153          }
 154  
 155          return $result->result();
 156      }
 157  }