summaryrefslogtreecommitdiffstats
path: root/vendor/markbaker/matrix/classes/src/Operators
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
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')
-rw-r--r--vendor/markbaker/matrix/classes/src/Operators/Addition.php68
-rw-r--r--vendor/markbaker/matrix/classes/src/Operators/DirectSum.php64
-rw-r--r--vendor/markbaker/matrix/classes/src/Operators/Division.php35
-rw-r--r--vendor/markbaker/matrix/classes/src/Operators/Multiplication.php86
-rw-r--r--vendor/markbaker/matrix/classes/src/Operators/Operator.php78
-rw-r--r--vendor/markbaker/matrix/classes/src/Operators/Subtraction.php68
6 files changed, 399 insertions, 0 deletions
diff --git a/vendor/markbaker/matrix/classes/src/Operators/Addition.php b/vendor/markbaker/matrix/classes/src/Operators/Addition.php
new file mode 100644
index 0000000..87bd342
--- /dev/null
+++ b/vendor/markbaker/matrix/classes/src/Operators/Addition.php
@@ -0,0 +1,68 @@
+<?php
+
+namespace Matrix\Operators;
+
+use Matrix\Matrix;
+use Matrix\Exception;
+
+class Addition extends Operator
+{
+ /**
+ * Execute the addition
+ *
+ * @param mixed $value The matrix or numeric value to add to the current base value
+ * @throws Exception If the provided argument is not appropriate for the operation
+ * @return $this The operation object, allowing multiple additions to be chained
+ **/
+ public function execute($value): Operator
+ {
+ if (is_array($value)) {
+ $value = new Matrix($value);
+ }
+
+ if (is_object($value) && ($value instanceof Matrix)) {
+ return $this->addMatrix($value);
+ } elseif (is_numeric($value)) {
+ return $this->addScalar($value);
+ }
+
+ throw new Exception('Invalid argument for addition');
+ }
+
+ /**
+ * Execute the addition for a scalar
+ *
+ * @param mixed $value The numeric value to add to the current base value
+ * @return $this The operation object, allowing multiple additions to be chained
+ **/
+ protected function addScalar($value): Operator
+ {
+ for ($row = 0; $row < $this->rows; ++$row) {
+ for ($column = 0; $column < $this->columns; ++$column) {
+ $this->matrix[$row][$column] += $value;
+ }
+ }
+
+ return $this;
+ }
+
+ /**
+ * Execute the addition for a matrix
+ *
+ * @param Matrix $value The 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
+ **/
+ protected function addMatrix(Matrix $value): Operator
+ {
+ $this->validateMatchingDimensions($value);
+
+ for ($row = 0; $row < $this->rows; ++$row) {
+ for ($column = 0; $column < $this->columns; ++$column) {
+ $this->matrix[$row][$column] += $value->getValue($row + 1, $column + 1);
+ }
+ }
+
+ return $this;
+ }
+}
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;
+ }
+}
diff --git a/vendor/markbaker/matrix/classes/src/Operators/Division.php b/vendor/markbaker/matrix/classes/src/Operators/Division.php
new file mode 100644
index 0000000..3cdedfb
--- /dev/null
+++ b/vendor/markbaker/matrix/classes/src/Operators/Division.php
@@ -0,0 +1,35 @@
+<?php
+
+namespace Matrix\Operators;
+
+use Matrix\Div0Exception;
+use Matrix\Exception;
+use \Matrix\Matrix;
+use \Matrix\Functions;
+
+class Division extends Multiplication
+{
+ /**
+ * Execute the division
+ *
+ * @param mixed $value The matrix or numeric value to divide the current base value by
+ * @throws Exception If the provided argument is not appropriate for the operation
+ * @return $this The operation object, allowing multiple divisions to be chained
+ **/
+ public function execute($value, string $type = 'division'): Operator
+ {
+ if (is_array($value)) {
+ $value = new Matrix($value);
+ }
+
+ if (is_object($value) && ($value instanceof Matrix)) {
+ $value = Functions::inverse($value, $type);
+
+ return $this->multiplyMatrix($value, $type);
+ } elseif (is_numeric($value)) {
+ return $this->multiplyScalar(1 / $value, $type);
+ }
+
+ throw new Exception('Invalid argument for division');
+ }
+}
diff --git a/vendor/markbaker/matrix/classes/src/Operators/Multiplication.php b/vendor/markbaker/matrix/classes/src/Operators/Multiplication.php
new file mode 100644
index 0000000..13d14f0
--- /dev/null
+++ b/vendor/markbaker/matrix/classes/src/Operators/Multiplication.php
@@ -0,0 +1,86 @@
+<?php
+
+namespace Matrix\Operators;
+
+use Matrix\Matrix;
+use \Matrix\Builder;
+use Matrix\Exception;
+use Throwable;
+
+class Multiplication extends Operator
+{
+ /**
+ * Execute the multiplication
+ *
+ * @param mixed $value The matrix or numeric value to multiply the current base value by
+ * @throws Exception If the provided argument is not appropriate for the operation
+ * @return $this The operation object, allowing multiple multiplications to be chained
+ **/
+ public function execute($value, string $type = 'multiplication'): Operator
+ {
+ if (is_array($value)) {
+ $value = new Matrix($value);
+ }
+
+ if (is_object($value) && ($value instanceof Matrix)) {
+ return $this->multiplyMatrix($value, $type);
+ } elseif (is_numeric($value)) {
+ return $this->multiplyScalar($value, $type);
+ }
+
+ throw new Exception("Invalid argument for $type");
+ }
+
+ /**
+ * Execute the multiplication for a scalar
+ *
+ * @param mixed $value The numeric value to multiply with the current base value
+ * @return $this The operation object, allowing multiple mutiplications to be chained
+ **/
+ protected function multiplyScalar($value, string $type = 'multiplication'): Operator
+ {
+ try {
+ for ($row = 0; $row < $this->rows; ++$row) {
+ for ($column = 0; $column < $this->columns; ++$column) {
+ $this->matrix[$row][$column] *= $value;
+ }
+ }
+ } catch (Throwable $e) {
+ throw new Exception("Invalid argument for $type");
+ }
+
+ return $this;
+ }
+
+ /**
+ * Execute the multiplication for a matrix
+ *
+ * @param Matrix $value The numeric value to multiply with the current base value
+ * @return $this The operation object, allowing multiple mutiplications to be chained
+ * @throws Exception If the provided argument is not appropriate for the operation
+ **/
+ protected function multiplyMatrix(Matrix $value, string $type = 'multiplication'): Operator
+ {
+ $this->validateReflectingDimensions($value);
+
+ $newRows = $this->rows;
+ $newColumns = $value->columns;
+ $matrix = Builder::createFilledMatrix(0, $newRows, $newColumns)
+ ->toArray();
+ try {
+ for ($row = 0; $row < $newRows; ++$row) {
+ for ($column = 0; $column < $newColumns; ++$column) {
+ $columnData = $value->getColumns($column + 1)->toArray();
+ foreach ($this->matrix[$row] as $key => $valueData) {
+ $matrix[$row][$column] += $valueData * $columnData[$key][0];
+ }
+ }
+ }
+ } catch (Throwable $e) {
+ throw new Exception("Invalid argument for $type");
+ }
+ $this->matrix = $matrix;
+
+ return $this;
+ }
+}
diff --git a/vendor/markbaker/matrix/classes/src/Operators/Operator.php b/vendor/markbaker/matrix/classes/src/Operators/Operator.php
new file mode 100644
index 0000000..b7f4025
--- /dev/null
+++ b/vendor/markbaker/matrix/classes/src/Operators/Operator.php
@@ -0,0 +1,78 @@
+<?php
+
+namespace Matrix\Operators;
+
+use Matrix\Matrix;
+use Matrix\Exception;
+
+abstract class Operator
+{
+ /**
+ * Stored internally as a 2-dimension array of values
+ *
+ * @property mixed[][] $matrix
+ **/
+ protected $matrix;
+
+ /**
+ * Number of rows in the matrix
+ *
+ * @property integer $rows
+ **/
+ protected $rows;
+
+ /**
+ * Number of columns in the matrix
+ *
+ * @property integer $columns
+ **/
+ protected $columns;
+
+ /**
+ * Create an new handler object for the operation
+ *
+ * @param Matrix $matrix The base Matrix object on which the operation will be performed
+ */
+ public function __construct(Matrix $matrix)
+ {
+ $this->rows = $matrix->rows;
+ $this->columns = $matrix->columns;
+ $this->matrix = $matrix->toArray();
+ }
+
+ /**
+ * Compare the dimensions of the matrices being operated on to see if they are valid for addition/subtraction
+ *
+ * @param Matrix $matrix The second Matrix object on which the operation will be performed
+ * @throws Exception
+ */
+ protected function validateMatchingDimensions(Matrix $matrix): void
+ {
+ if (($this->rows != $matrix->rows) || ($this->columns != $matrix->columns)) {
+ throw new Exception('Matrices have mismatched dimensions');
+ }
+ }
+
+ /**
+ * Compare the dimensions of the matrices being operated on to see if they are valid for multiplication/division
+ *
+ * @param Matrix $matrix The second Matrix object on which the operation will be performed
+ * @throws Exception
+ */
+ protected function validateReflectingDimensions(Matrix $matrix): void
+ {
+ if ($this->columns != $matrix->rows) {
+ throw new Exception('Matrices have mismatched dimensions');
+ }
+ }
+
+ /**
+ * Return the result of the operation
+ *
+ * @return Matrix
+ */
+ public function result(): Matrix
+ {
+ return new Matrix($this->matrix);
+ }
+}
diff --git a/vendor/markbaker/matrix/classes/src/Operators/Subtraction.php b/vendor/markbaker/matrix/classes/src/Operators/Subtraction.php
new file mode 100644
index 0000000..14b3541
--- /dev/null
+++ b/vendor/markbaker/matrix/classes/src/Operators/Subtraction.php
@@ -0,0 +1,68 @@
+<?php
+
+namespace Matrix\Operators;
+
+use Matrix\Matrix;
+use Matrix\Exception;
+
+class Subtraction extends Operator
+{
+ /**
+ * Execute the subtraction
+ *
+ * @param mixed $value The matrix or numeric value to subtract from the current base value
+ * @throws Exception If the provided argument is not appropriate for the operation
+ * @return $this The operation object, allowing multiple subtractions to be chained
+ **/
+ public function execute($value): Operator
+ {
+ if (is_array($value)) {
+ $value = new Matrix($value);
+ }
+
+ if (is_object($value) && ($value instanceof Matrix)) {
+ return $this->subtractMatrix($value);
+ } elseif (is_numeric($value)) {
+ return $this->subtractScalar($value);
+ }
+
+ throw new Exception('Invalid argument for subtraction');
+ }
+
+ /**
+ * Execute the subtraction for a scalar
+ *
+ * @param mixed $value The numeric value to subtracted from the current base value
+ * @return $this The operation object, allowing multiple additions to be chained
+ **/
+ protected function subtractScalar($value): Operator
+ {
+ for ($row = 0; $row < $this->rows; ++$row) {
+ for ($column = 0; $column < $this->columns; ++$column) {
+ $this->matrix[$row][$column] -= $value;
+ }
+ }
+
+ return $this;
+ }
+
+ /**
+ * Execute the subtraction for a matrix
+ *
+ * @param Matrix $value The numeric value to subtract from the current base value
+ * @return $this The operation object, allowing multiple subtractions to be chained
+ * @throws Exception If the provided argument is not appropriate for the operation
+ **/
+ protected function subtractMatrix(Matrix $value): Operator
+ {
+ $this->validateMatchingDimensions($value);
+
+ for ($row = 0; $row < $this->rows; ++$row) {
+ for ($column = 0; $column < $this->columns; ++$column) {
+ $this->matrix[$row][$column] -= $value->getValue($row + 1, $column + 1);
+ }
+ }
+
+ return $this;
+ }
+}