From 2952557028dba99afac9c9dd5425b6665fb0634f Mon Sep 17 00:00:00 2001 From: Arkadiusz Kondas Date: Sat, 30 Apr 2016 23:47:35 +0200 Subject: [PATCH] improve matrix inverse --- src/Phpml/Math/Matrix.php | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/Phpml/Math/Matrix.php b/src/Phpml/Math/Matrix.php index aeab3bc..ee8fd3c 100644 --- a/src/Phpml/Math/Matrix.php +++ b/src/Phpml/Math/Matrix.php @@ -53,7 +53,7 @@ class Matrix /** * @param array $array - * + * * @return Matrix */ public static function fromFlatArray(array $array) @@ -225,12 +225,8 @@ class Matrix $newMatrix = array(); for ($i = 0; $i < $this->rows; ++$i) { for ($j = 0; $j < $this->columns; ++$j) { - $subMatrix = $this->crossOut($i, $j); - if (fmod($i + $j, 2) == 0) { - $newMatrix[$i][$j] = ($subMatrix->getDeterminant()); - } else { - $newMatrix[$i][$j] = -($subMatrix->getDeterminant()); - } + $minor = $this->crossOut($i, $j)->getDeterminant(); + $newMatrix[$i][$j] = fmod($i + $j, 2) == 0 ? $minor : -$minor; } }