php-cs-fxier

This commit is contained in:
Arkadiusz Kondas 2016-04-30 00:59:10 +02:00
parent 60c796f5d9
commit 633974fea0
5 changed files with 29 additions and 31 deletions

View File

@ -57,5 +57,4 @@ class InvalidArgumentException extends \Exception
{ {
return new self('Inconsistent matrix aupplied'); return new self('Inconsistent matrix aupplied');
} }
} }

View File

@ -21,5 +21,4 @@ class MatrixException extends \Exception
{ {
return new self('Column out of range'); return new self('Column out of range');
} }
} }

View File

@ -1,5 +1,6 @@
<?php <?php
declare(strict_types = 1);
declare (strict_types = 1);
namespace Phpml\Math; namespace Phpml\Math;
@ -30,7 +31,7 @@ class Matrix
/** /**
* @param array $matrix * @param array $matrix
* @param bool $validate * @param bool $validate
* *
* @throws InvalidArgumentException * @throws InvalidArgumentException
*/ */
@ -39,8 +40,8 @@ class Matrix
$this->rows = count($matrix); $this->rows = count($matrix);
$this->columns = count($matrix[0]); $this->columns = count($matrix[0]);
if($validate) { if ($validate) {
for ($i = 0; $i < $this->rows; $i++) { for ($i = 0; $i < $this->rows; ++$i) {
if (count($matrix[$i]) !== $this->columns) { if (count($matrix[$i]) !== $this->columns) {
throw InvalidArgumentException::matrixDimensionsDidNotMatch(); throw InvalidArgumentException::matrixDimensionsDidNotMatch();
} }
@ -83,12 +84,12 @@ class Matrix
*/ */
public function getColumnValues($column) public function getColumnValues($column)
{ {
if($column >= $this->columns) { if ($column >= $this->columns) {
throw MatrixException::columnOutOfRange(); throw MatrixException::columnOutOfRange();
} }
$values = []; $values = [];
for ($i = 0; $i < $this->rows; $i++) { for ($i = 0; $i < $this->rows; ++$i) {
$values[] = $this->matrix[$i][$column]; $values[] = $this->matrix[$i][$column];
} }
@ -102,7 +103,7 @@ class Matrix
*/ */
public function getDeterminant() public function getDeterminant()
{ {
if($this->determinant) { if ($this->determinant) {
return $this->determinant; return $this->determinant;
} }
@ -113,11 +114,11 @@ class Matrix
$determinant = 0; $determinant = 0;
if ($this->rows == 1 && $this->columns == 1) { if ($this->rows == 1 && $this->columns == 1) {
$determinant = $this->matrix[0][0]; $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] - $determinant = $this->matrix[0][0] * $this->matrix[1][1] -
$this->matrix[0][1] * $this->matrix[1][0]; $this->matrix[0][1] * $this->matrix[1][0];
} else { } else {
for ($j = 0; $j < $this->columns; $j++) { for ($j = 0; $j < $this->columns; ++$j) {
$subMatrix = $this->crossOut(0, $j); $subMatrix = $this->crossOut(0, $j);
if (fmod($j, 2) == 0) { if (fmod($j, 2) == 0) {
$determinant += $this->matrix[0][$j] * $subMatrix->getDeterminant(); $determinant += $this->matrix[0][$j] * $subMatrix->getDeterminant();
@ -130,7 +131,7 @@ class Matrix
return $this->determinant = $determinant; return $this->determinant = $determinant;
} }
/** /**
* @return bool * @return bool
*/ */
public function isSquare() public function isSquare()
@ -144,8 +145,8 @@ class Matrix
public function transpose() public function transpose()
{ {
$newMatrix = []; $newMatrix = [];
for ($i = 0; $i < $this->rows; $i++) { for ($i = 0; $i < $this->rows; ++$i) {
for ($j = 0; $j < $this->columns; $j++) { for ($j = 0; $j < $this->columns; ++$j) {
$newMatrix[$j][$i] = $this->matrix[$i][$j]; $newMatrix[$j][$i] = $this->matrix[$i][$j];
} }
} }
@ -168,14 +169,15 @@ class Matrix
$product = []; $product = [];
$multiplier = $matrix->toArray(); $multiplier = $matrix->toArray();
for ($i = 0; $i < $this->rows; $i++) { for ($i = 0; $i < $this->rows; ++$i) {
for ($j = 0; $j < $matrix->getColumns(); $j++) { for ($j = 0; $j < $matrix->getColumns(); ++$j) {
$product[$i][$j] = 0; $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]; $product[$i][$j] += $this->matrix[$i][$k] * $multiplier[$k][$j];
} }
} }
} }
return new self($product, false); return new self($product, false);
} }
@ -187,8 +189,8 @@ class Matrix
public function divideByScalar($value) public function divideByScalar($value)
{ {
$newMatrix = array(); $newMatrix = array();
for ($i = 0; $i < $this->rows; $i++) { for ($i = 0; $i < $this->rows; ++$i) {
for ($j = 0; $j < $this->columns; $j++) { for ($j = 0; $j < $this->columns; ++$j) {
$newMatrix[$i][$j] = $this->matrix[$i][$j] / $value; $newMatrix[$i][$j] = $this->matrix[$i][$j] / $value;
} }
} }
@ -208,8 +210,8 @@ class Matrix
} }
$newMatrix = array(); $newMatrix = array();
for ($i = 0; $i < $this->rows; $i++) { for ($i = 0; $i < $this->rows; ++$i) {
for ($j = 0; $j < $this->columns; $j++) { for ($j = 0; $j < $this->columns; ++$j) {
$subMatrix = $this->crossOut($i, $j); $subMatrix = $this->crossOut($i, $j);
if (fmod($i + $j, 2) == 0) { if (fmod($i + $j, 2) == 0) {
$newMatrix[$i][$j] = ($subMatrix->getDeterminant()); $newMatrix[$i][$j] = ($subMatrix->getDeterminant());
@ -234,20 +236,19 @@ class Matrix
{ {
$newMatrix = []; $newMatrix = [];
$r = 0; $r = 0;
for ($i = 0; $i < $this->rows; $i++) { for ($i = 0; $i < $this->rows; ++$i) {
$c = 0; $c = 0;
if ($row != $i) { if ($row != $i) {
for ($j = 0; $j < $this->columns; $j++) { for ($j = 0; $j < $this->columns; ++$j) {
if ($column != $j) { if ($column != $j) {
$newMatrix[$r][$c] = $this->matrix[$i][$j]; $newMatrix[$r][$c] = $this->matrix[$i][$j];
$c++; ++$c;
} }
} }
$r++; ++$r;
} }
} }
return new self($newMatrix, false); return new self($newMatrix, false);
} }
} }

View File

@ -64,7 +64,7 @@ class LeastSquares implements Regression
} }
/** /**
* coefficient(b) = (X'X)-1X'Y * coefficient(b) = (X'X)-1X'Y.
*/ */
private function computeCoefficients() private function computeCoefficients()
{ {

View File

@ -22,7 +22,7 @@ class LeastSquaresTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(4.06, $regression->predict([64]), '', $delta); $this->assertEquals(4.06, $regression->predict([64]), '', $delta);
//http://www.stat.wmich.edu/s216/book/node127.html //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]]; $targets = [[7100], [15500], [4400], [4400], [5900], [4600], [8800], [2000], [2750], [2550], [960], [1025]];
$regression = new LeastSquares(); $regression = new LeastSquares();
@ -40,13 +40,12 @@ class LeastSquaresTest extends \PHPUnit_Framework_TestCase
$delta = 1; $delta = 1;
//http://www.stat.wmich.edu/s216/book/node129.html //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]]; $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]]; $targets = [[2000], [2750], [15500], [960], [4400], [8800], [7100], [2550], [1025], [5900], [4600], [4400]];
$regression = new LeastSquares(); $regression = new LeastSquares();
$regression->train($samples, $targets); $regression->train($samples, $targets);
$this->assertEquals(4094, $regression->predict([60000, 1996]), '', $delta); $this->assertEquals(4094, $regression->predict([60000, 1996]), '', $delta);
} }
} }