ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
DirectSum.php
Go to the documentation of this file.
1<?php
2
3namespace Matrix\Operators;
4
7
8class DirectSum extends Operator
9{
17 public function execute($value): Operator
18 {
19 if (is_array($value)) {
20 $value = new Matrix($value);
21 }
22
23 if ($value instanceof Matrix) {
24 return $this->directSumMatrix($value);
25 }
26
27 throw new Exception('Invalid argument for addition');
28 }
29
36 private function directSumMatrix($value): Operator
37 {
38 $originalColumnCount = count($this->matrix[0]);
39 $originalRowCount = count($this->matrix);
40 $valColumnCount = $value->columns;
41 $valRowCount = $value->rows;
42 $value = $value->toArray();
43
44 for ($row = 0; $row < $this->rows; ++$row) {
45 $this->matrix[$row] = array_merge($this->matrix[$row], array_fill(0, $valColumnCount, 0));
46 }
47
48 $this->matrix = array_merge(
49 $this->matrix,
50 array_fill(0, $valRowCount, array_fill(0, $originalColumnCount, 0))
51 );
52
53 for ($row = $originalRowCount; $row < $originalRowCount + $valRowCount; ++$row) {
54 array_splice(
55 $this->matrix[$row],
56 $originalColumnCount,
57 $valColumnCount,
58 $value[$row - $originalRowCount]
59 );
60 }
61
62 return $this;
63 }
64}
An exception for terminatinating execution or to throw for unit testing.
execute($value)
Execute the addition.
Definition: DirectSum.php:17
directSumMatrix($value)
Execute the direct sum for a matrix.
Definition: DirectSum.php:36
$rows
Number of rows in the matrix.
Definition: Operator.php:22
$row
Class for the creating "special" Matrices.
Definition: Builder.php:11