summaryrefslogtreecommitdiffstats
path: root/vendor/markbaker/matrix/classes/src/Operators/DirectSum.php
diff options
context:
space:
mode:
authorAnton Luka Šijanec <anton@sijanec.eu>2024-05-27 13:08:29 +0200
committerAnton Luka Šijanec <anton@sijanec.eu>2024-05-27 13:08:29 +0200
commit75160b12821f7f4299cce7f0b69c83c1502ae071 (patch)
tree27e25e4ccaef45f0c58b22831164050d1af1d4db /vendor/markbaker/matrix/classes/src/Operators/DirectSum.php
parentprvi-commit (diff)
download1ka-75160b12821f7f4299cce7f0b69c83c1502ae071.tar
1ka-75160b12821f7f4299cce7f0b69c83c1502ae071.tar.gz
1ka-75160b12821f7f4299cce7f0b69c83c1502ae071.tar.bz2
1ka-75160b12821f7f4299cce7f0b69c83c1502ae071.tar.lz
1ka-75160b12821f7f4299cce7f0b69c83c1502ae071.tar.xz
1ka-75160b12821f7f4299cce7f0b69c83c1502ae071.tar.zst
1ka-75160b12821f7f4299cce7f0b69c83c1502ae071.zip
Diffstat (limited to 'vendor/markbaker/matrix/classes/src/Operators/DirectSum.php')
-rw-r--r--vendor/markbaker/matrix/classes/src/Operators/DirectSum.php64
1 files changed, 64 insertions, 0 deletions
diff --git a/vendor/markbaker/matrix/classes/src/Operators/DirectSum.php b/vendor/markbaker/matrix/classes/src/Operators/DirectSum.php
new file mode 100644
index 0000000..e729a43
--- /dev/null
+++ b/vendor/markbaker/matrix/classes/src/Operators/DirectSum.php
@@ -0,0 +1,64 @@
+<?php
+
+namespace Matrix\Operators;
+
+use Matrix\Matrix;
+use Matrix\Exception;
+
+class DirectSum extends Operator
+{
+ /**
+ * Execute the addition
+ *
+ * @param mixed $value The matrix or numeric value to add to the current base value
+ * @return $this The operation object, allowing multiple additions to be chained
+ * @throws Exception If the provided argument is not appropriate for the operation
+ */
+ public function execute($value): Operator
+ {
+ if (is_array($value)) {
+ $value = new Matrix($value);
+ }
+
+ if ($value instanceof Matrix) {
+ return $this->directSumMatrix($value);
+ }
+
+ throw new Exception('Invalid argument for addition');
+ }
+
+ /**
+ * Execute the direct sum for a matrix
+ *
+ * @param Matrix $value The numeric value to concatenate/direct sum with the current base value
+ * @return $this The operation object, allowing multiple additions to be chained
+ **/
+ private function directSumMatrix($value): Operator
+ {
+ $originalColumnCount = count($this->matrix[0]);
+ $originalRowCount = count($this->matrix);
+ $valColumnCount = $value->columns;
+ $valRowCount = $value->rows;
+ $value = $value->toArray();
+
+ for ($row = 0; $row < $this->rows; ++$row) {
+ $this->matrix[$row] = array_merge($this->matrix[$row], array_fill(0, $valColumnCount, 0));
+ }
+
+ $this->matrix = array_merge(
+ $this->matrix,
+ array_fill(0, $valRowCount, array_fill(0, $originalColumnCount, 0))
+ );
+
+ for ($row = $originalRowCount; $row < $originalRowCount + $valRowCount; ++$row) {
+ array_splice(
+ $this->matrix[$row],
+ $originalColumnCount,
+ $valColumnCount,
+ $value[$row - $originalRowCount]
+ );
+ }
+
+ return $this;
+ }
+}