From 68051600a3d07307d4a2f27b7619ef12f32f67dc Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sat, 22 Oct 2016 22:43:15 +0200 Subject: [PATCH] Matrix fixes --- Math/Matrix/Matrix.php | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/Math/Matrix/Matrix.php b/Math/Matrix/Matrix.php index d45a42e0a..b373529b9 100644 --- a/Math/Matrix/Matrix.php +++ b/Math/Matrix/Matrix.php @@ -134,7 +134,7 @@ class Matrix implements \ArrayAccess, \Iterator public function transpose() : Matrix { $matrix = new Matrix($this->n, $this->m); - $matrix->setMatrix(array_map(null, $matrix->getMatrix())); + $matrix->setMatrix(array_map(null, ...$this->matrix)); return $matrix; } @@ -152,23 +152,40 @@ class Matrix implements \ArrayAccess, \Iterator return $this->matrix; } + /** + * Get matrix rank. + * + * @return int + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function rank() : int + { + return 0; + } + /** * Set matrix array. * * @param array $matrix Matrix * + * @return Matrix + * * @throws \Exception * * @since 1.0.0 * @author Dennis Eichhorn */ - public function setMatrix(array $matrix) + public function setMatrix(array $matrix) : Matrix { if ($this->m !== count($matrix) || $this->n !== count($matrix[0])) { throw new DimensionException(count($matrix), count($matrix[0])); } $this->matrix = $matrix; + + return $this; } /** @@ -356,7 +373,7 @@ class Matrix implements \ArrayAccess, \Iterator for ($c = 0; $c < $nDim; $c++) { // Column of $matrix $temp = 0; - for ($j = 0; $j < $mDim; $i++) { // Row of $matrix + for ($j = 0; $j < $mDim; $j++) { // Row of $matrix $temp += $this->matrix[$i][$j] * $matrixArr[$j][$c]; } @@ -557,6 +574,14 @@ class Matrix implements \ArrayAccess, \Iterator return $newMatrix; } + public function diagonalize() : Matrix + { + $newMatrix = new Matrix($this->m, $this->n); + $newMatrix->setMatrix($this->diag($this->matrix)); + + return $newMatrix; + } + /** * Diagonalize matrix. *