ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Subtraction.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Matrix\Operators;
4 
5 use Matrix\Matrix;
7 
8 class Subtraction 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->subtractMatrix($value);
25  } elseif (is_numeric($value)) {
26  return $this->subtractScalar($value);
27  }
28 
29  throw new Exception('Invalid argument for subtraction');
30  }
31 
38  protected function subtractScalar($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 subtractMatrix(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 }
subtractScalar($value)
Execute the subtraction for a scalar.
Definition: Subtraction.php:38
subtractMatrix(Matrix $value)
Execute the subtraction for a matrix.
Definition: Subtraction.php:56
$rows
Number of rows in the matrix.
Definition: Operator.php:22
execute($value)
Execute the subtraction.
Definition: Subtraction.php:17
$columns
Number of columns in the matrix.
Definition: Operator.php:29
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
$row
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
Class for the creating "special" Matrices.
Definition: Builder.php:11