From 633974fea06d469e55d3456341c035cb18605bbf Mon Sep 17 00:00:00 2001 From: Arkadiusz Kondas Date: Sat, 30 Apr 2016 00:59:10 +0200 Subject: [PATCH] php-cs-fxier --- .../Exception/InvalidArgumentException.php | 1 - src/Phpml/Exception/MatrixException.php | 1 - src/Phpml/Math/Matrix.php | 49 ++++++++++--------- src/Phpml/Regression/LeastSquares.php | 2 +- tests/Phpml/Regression/LeastSquaresTest.php | 7 ++- 5 files changed, 29 insertions(+), 31 deletions(-) diff --git a/src/Phpml/Exception/InvalidArgumentException.php b/src/Phpml/Exception/InvalidArgumentException.php index c852ecf..9e88250 100644 --- a/src/Phpml/Exception/InvalidArgumentException.php +++ b/src/Phpml/Exception/InvalidArgumentException.php @@ -57,5 +57,4 @@ class InvalidArgumentException extends \Exception { return new self('Inconsistent matrix aupplied'); } - } diff --git a/src/Phpml/Exception/MatrixException.php b/src/Phpml/Exception/MatrixException.php index 8186f0a..440a0ac 100644 --- a/src/Phpml/Exception/MatrixException.php +++ b/src/Phpml/Exception/MatrixException.php @@ -21,5 +21,4 @@ class MatrixException extends \Exception { return new self('Column out of range'); } - } diff --git a/src/Phpml/Math/Matrix.php b/src/Phpml/Math/Matrix.php index 20d6561..a071fd9 100644 --- a/src/Phpml/Math/Matrix.php +++ b/src/Phpml/Math/Matrix.php @@ -1,5 +1,6 @@ rows = count($matrix); $this->columns = count($matrix[0]); - if($validate) { - for ($i = 0; $i < $this->rows; $i++) { + if ($validate) { + for ($i = 0; $i < $this->rows; ++$i) { if (count($matrix[$i]) !== $this->columns) { throw InvalidArgumentException::matrixDimensionsDidNotMatch(); } @@ -83,12 +84,12 @@ class Matrix */ public function getColumnValues($column) { - if($column >= $this->columns) { + if ($column >= $this->columns) { throw MatrixException::columnOutOfRange(); } $values = []; - for ($i = 0; $i < $this->rows; $i++) { + for ($i = 0; $i < $this->rows; ++$i) { $values[] = $this->matrix[$i][$column]; } @@ -102,7 +103,7 @@ class Matrix */ public function getDeterminant() { - if($this->determinant) { + if ($this->determinant) { return $this->determinant; } @@ -113,11 +114,11 @@ class Matrix $determinant = 0; if ($this->rows == 1 && $this->columns == 1) { $determinant = $this->matrix[0][0]; - } else if ($this->rows == 2 && $this->columns == 2) { + } elseif ($this->rows == 2 && $this->columns == 2) { $determinant = $this->matrix[0][0] * $this->matrix[1][1] - $this->matrix[0][1] * $this->matrix[1][0]; } else { - for ($j = 0; $j < $this->columns; $j++) { + for ($j = 0; $j < $this->columns; ++$j) { $subMatrix = $this->crossOut(0, $j); if (fmod($j, 2) == 0) { $determinant += $this->matrix[0][$j] * $subMatrix->getDeterminant(); @@ -130,7 +131,7 @@ class Matrix return $this->determinant = $determinant; } - /** + /** * @return bool */ public function isSquare() @@ -144,8 +145,8 @@ class Matrix public function transpose() { $newMatrix = []; - for ($i = 0; $i < $this->rows; $i++) { - for ($j = 0; $j < $this->columns; $j++) { + for ($i = 0; $i < $this->rows; ++$i) { + for ($j = 0; $j < $this->columns; ++$j) { $newMatrix[$j][$i] = $this->matrix[$i][$j]; } } @@ -168,14 +169,15 @@ class Matrix $product = []; $multiplier = $matrix->toArray(); - for ($i = 0; $i < $this->rows; $i++) { - for ($j = 0; $j < $matrix->getColumns(); $j++) { + for ($i = 0; $i < $this->rows; ++$i) { + for ($j = 0; $j < $matrix->getColumns(); ++$j) { $product[$i][$j] = 0; - for ($k = 0; $k < $this->columns; $k++) { + for ($k = 0; $k < $this->columns; ++$k) { $product[$i][$j] += $this->matrix[$i][$k] * $multiplier[$k][$j]; } } } + return new self($product, false); } @@ -187,8 +189,8 @@ class Matrix public function divideByScalar($value) { $newMatrix = array(); - for ($i = 0; $i < $this->rows; $i++) { - for ($j = 0; $j < $this->columns; $j++) { + for ($i = 0; $i < $this->rows; ++$i) { + for ($j = 0; $j < $this->columns; ++$j) { $newMatrix[$i][$j] = $this->matrix[$i][$j] / $value; } } @@ -208,8 +210,8 @@ class Matrix } $newMatrix = array(); - for ($i = 0; $i < $this->rows; $i++) { - for ($j = 0; $j < $this->columns; $j++) { + 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()); @@ -234,20 +236,19 @@ class Matrix { $newMatrix = []; $r = 0; - for ($i = 0; $i < $this->rows; $i++) { + for ($i = 0; $i < $this->rows; ++$i) { $c = 0; if ($row != $i) { - for ($j = 0; $j < $this->columns; $j++) { + for ($j = 0; $j < $this->columns; ++$j) { if ($column != $j) { $newMatrix[$r][$c] = $this->matrix[$i][$j]; - $c++; + ++$c; } } - $r++; + ++$r; } } return new self($newMatrix, false); } - } diff --git a/src/Phpml/Regression/LeastSquares.php b/src/Phpml/Regression/LeastSquares.php index 34dc745..0b7f4cc 100644 --- a/src/Phpml/Regression/LeastSquares.php +++ b/src/Phpml/Regression/LeastSquares.php @@ -64,7 +64,7 @@ class LeastSquares implements Regression } /** - * coefficient(b) = (X'X)-1X'Y + * coefficient(b) = (X'X)-1X'Y. */ private function computeCoefficients() { diff --git a/tests/Phpml/Regression/LeastSquaresTest.php b/tests/Phpml/Regression/LeastSquaresTest.php index 8859ba9..148cc4b 100644 --- a/tests/Phpml/Regression/LeastSquaresTest.php +++ b/tests/Phpml/Regression/LeastSquaresTest.php @@ -22,7 +22,7 @@ class LeastSquaresTest extends \PHPUnit_Framework_TestCase $this->assertEquals(4.06, $regression->predict([64]), '', $delta); //http://www.stat.wmich.edu/s216/book/node127.html - $samples = [[1 ,9300], [1, 10565], [1, 15000], [1, 15000], [1, 17764], [1, 57000], [1, 65940], [1, 73676], [1, 77006], [1, 93739], [1, 146088], [1, 153260]]; + $samples = [[1, 9300], [1, 10565], [1, 15000], [1, 15000], [1, 17764], [1, 57000], [1, 65940], [1, 73676], [1, 77006], [1, 93739], [1, 146088], [1, 153260]]; $targets = [[7100], [15500], [4400], [4400], [5900], [4600], [8800], [2000], [2750], [2550], [960], [1025]]; $regression = new LeastSquares(); @@ -40,13 +40,12 @@ class LeastSquaresTest extends \PHPUnit_Framework_TestCase $delta = 1; //http://www.stat.wmich.edu/s216/book/node129.html - $samples = [[1, 73676, 1996],[1, 77006, 1998],[1, 10565, 2000],[1, 146088, 1995],[1, 15000, 2001],[1, 65940, 2000],[1, 9300, 2000],[1, 93739, 1996],[1, 153260, 1994],[1, 17764, 2002],[1, 57000, 1998],[1, 15000, 2000]]; - $targets = [[2000], [ 2750], [15500], [ 960], [ 4400], [ 8800], [ 7100], [ 2550], [ 1025], [ 5900], [ 4600], [ 4400]]; + $samples = [[1, 73676, 1996], [1, 77006, 1998], [1, 10565, 2000], [1, 146088, 1995], [1, 15000, 2001], [1, 65940, 2000], [1, 9300, 2000], [1, 93739, 1996], [1, 153260, 1994], [1, 17764, 2002], [1, 57000, 1998], [1, 15000, 2000]]; + $targets = [[2000], [2750], [15500], [960], [4400], [8800], [7100], [2550], [1025], [5900], [4600], [4400]]; $regression = new LeastSquares(); $regression->train($samples, $targets); $this->assertEquals(4094, $regression->predict([60000, 1996]), '', $delta); } - }