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 310 and 401] [Versions 311 and 401] [Versions 39 and 401]

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