ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Addition.php
Go to the documentation of this file.
1<?php
2
4
7
8class Addition extends Operator
9{
17 public function execute($value): Operator
18 {
19 if (is_array($value)) {
20 $value = new Matrix($value);
21 }
22
23 if (is_object($value) && ($value instanceof Matrix)) {
24 return $this->addMatrix($value);
25 } elseif (is_numeric($value)) {
26 return $this->addScalar($value);
27 }
28
29 throw new Exception('Invalid argument for addition');
30 }
31
38 protected function addScalar($value): Operator
39 {
40 for ($row = 0; $row < $this->rows; ++$row) {
41 for ($column = 0; $column < $this->columns; ++$column) {
42 $this->matrix[$row][$column] += $value;
43 }
44 }
45
46 return $this;
47 }
48
56 protected function addMatrix(Matrix $value): Operator
57 {
58 $this->validateMatchingDimensions($value);
59
60 for ($row = 0; $row < $this->rows; ++$row) {
61 for ($column = 0; $column < $this->columns; ++$column) {
62 $this->matrix[$row][$column] += $value->getValue($row + 1, $column + 1);
63 }
64 }
65
66 return $this;
67 }
68}
An exception for terminatinating execution or to throw for unit testing.
getValue(int $row, int $column)
Return a value from this matrix, from the "cell" identified by the row and column numbers Note that r...
Definition: Matrix.php:268
addScalar($value)
Execute the addition for a scalar.
Definition: Addition.php:38
addMatrix(Matrix $value)
Execute the addition for a matrix.
Definition: Addition.php:56
execute($value)
Execute the addition.
Definition: Addition.php:17
validateMatchingDimensions(Matrix $matrix)
Compare the dimensions of the matrices being operated on to see if they are valid for addition/subtra...
Definition: Operator.php:49
$rows
Number of rows in the matrix.
Definition: Operator.php:22
$columns
Number of columns in the matrix.
Definition: Operator.php:29
$row
Class for the creating "special" Matrices.
Definition: Builder.php:11