Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

   1  <?php
   2  
   3  namespace Matrix\Operators;
   4  
   5  use Matrix\Matrix;
   6  use \Matrix\Builder;
   7  use Matrix\Exception;
   8  
   9  class Multiplication extends Operator
  10  {
  11      /**
  12       * Execute the multiplication
  13       *
  14       * @param mixed $value The matrix or numeric value to multiply the current base value by
  15       * @throws Exception If the provided argument is not appropriate for the operation
  16       * @return $this The operation object, allowing multiple multiplications to be chained
  17       **/
  18      public function execute($value)
  19      {
  20          if (is_array($value)) {
  21              $value = new Matrix($value);
  22          }
  23  
  24          if (is_object($value) && ($value instanceof Matrix)) {
  25              return $this->multiplyMatrix($value);
  26          } elseif (is_numeric($value)) {
  27              return $this->multiplyScalar($value);
  28          }
  29  
  30          throw new Exception('Invalid argument for multiplication');
  31      }
  32  
  33      /**
  34       * Execute the multiplication for a scalar
  35       *
  36       * @param mixed $value The numeric value to multiply with the current base value
  37       * @return $this The operation object, allowing multiple mutiplications to be chained
  38       **/
  39      protected function multiplyScalar($value)
  40      {
  41          for ($row = 0; $row < $this->rows; ++$row) {
  42              for ($column = 0; $column < $this->columns; ++$column) {
  43                  $this->matrix[$row][$column] *= $value;
  44              }
  45          }
  46  
  47          return $this;
  48      }
  49  
  50      /**
  51       * Execute the multiplication for a matrix
  52       *
  53       * @param Matrix $value The numeric value to multiply with the current base value
  54       * @return $this The operation object, allowing multiple mutiplications to be chained
  55       * @throws Exception If the provided argument is not appropriate for the operation
  56       **/
  57      protected function multiplyMatrix(Matrix $value)
  58      {
  59          $this->validateReflectingDimensions($value);
  60  
  61          $newRows = $this->rows;
  62          $newColumns = $value->columns;
  63          $matrix = Builder::createFilledMatrix(0, $newRows, $newColumns)
  64              ->toArray();
  65          for ($row = 0; $row < $newRows; ++$row) {
  66              for ($column = 0; $column < $newColumns; ++$column) {
  67                  $columnData = $value->getColumns($column + 1)->toArray();
  68                  foreach ($this->matrix[$row] as $key => $valueData) {
  69                      $matrix[$row][$column] += $valueData * $columnData[$key][0];
  70                  }
  71              }
  72          }
  73          $this->matrix = $matrix;
  74  
  75          return $this;
  76      }
  77  }