ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Multiplication.php
Go to the documentation of this file.
1<?php
2
3namespace Matrix\Operators;
4
6use \Matrix\Builder;
8use Throwable;
9
11{
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
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
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}
An exception for terminatinating execution or to throw for unit testing.
static createFilledMatrix($fillValue, $rows, $columns=null)
Create a new matrix of specified dimensions, and filled with a specified value If the column argument...
Definition: Builder.php:30
getColumns(int $column, int $columnCount=1)
Return a new matrix as a subset of columns from this matrix, starting at column number $column,...
Definition: Matrix.php:187
columns()
Returns a Generator that will yield each column of the matrix in turn as a vector matrix or the value...
Definition: Matrix.php:297
multiplyScalar($value, string $type='multiplication')
Execute the multiplication for a scalar.
multiplyMatrix(Matrix $value, string $type='multiplication')
Execute the multiplication for a matrix.
execute($value, string $type='multiplication')
Execute the multiplication.
validateReflectingDimensions(Matrix $matrix)
Compare the dimensions of the matrices being operated on to see if they are valid for multiplication/...
Definition: Operator.php:62
$rows
Number of rows in the matrix.
Definition: Operator.php:22
$columns
Number of columns in the matrix.
Definition: Operator.php:29
$key
Definition: croninfo.php:18
$row
Class for the creating "special" Matrices.
Definition: Builder.php:11
$type