Skip to content

Commit

Permalink
php-cs-fxier
Browse files Browse the repository at this point in the history
  • Loading branch information
akondas committed Apr 29, 2016
1 parent 60c796f commit 633974f
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 31 deletions.
1 change: 0 additions & 1 deletion src/Phpml/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,4 @@ public static function inconsistentMatrixSupplied()
{
return new self('Inconsistent matrix aupplied');
}

}
1 change: 0 additions & 1 deletion src/Phpml/Exception/MatrixException.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,4 @@ public static function columnOutOfRange()
{
return new self('Column out of range');
}

}
49 changes: 25 additions & 24 deletions src/Phpml/Math/Matrix.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
declare(strict_types = 1);

declare (strict_types = 1);

namespace Phpml\Math;

Expand Down Expand Up @@ -30,7 +31,7 @@ class Matrix

/**
* @param array $matrix
* @param bool $validate
* @param bool $validate
*
* @throws InvalidArgumentException
*/
Expand All @@ -39,8 +40,8 @@ public function __construct(array $matrix, bool $validate = true)
$this->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();
}
Expand Down Expand Up @@ -83,12 +84,12 @@ public function getColumns()
*/
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];
}

Expand All @@ -102,7 +103,7 @@ public function getColumnValues($column)
*/
public function getDeterminant()
{
if($this->determinant) {
if ($this->determinant) {
return $this->determinant;
}

Expand All @@ -113,11 +114,11 @@ public function getDeterminant()
$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();
Expand All @@ -130,7 +131,7 @@ public function getDeterminant()
return $this->determinant = $determinant;
}

/**
/**
* @return bool
*/
public function isSquare()
Expand All @@ -144,8 +145,8 @@ public function isSquare()
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];
}
}
Expand All @@ -168,14 +169,15 @@ public function multiply(Matrix $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);
}

Expand All @@ -187,8 +189,8 @@ public function multiply(Matrix $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;
}
}
Expand All @@ -208,8 +210,8 @@ public function inverse()
}

$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());
Expand All @@ -234,20 +236,19 @@ public function crossOut(int $row, int $column)
{
$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);
}

}
2 changes: 1 addition & 1 deletion src/Phpml/Regression/LeastSquares.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function getCoefficients()
}

/**
* coefficient(b) = (X'X)-1X'Y
* coefficient(b) = (X'X)-1X'Y.
*/
private function computeCoefficients()
{
Expand Down
7 changes: 3 additions & 4 deletions tests/Phpml/Regression/LeastSquaresTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function testPredictSingleFeatureSamples()
$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();
Expand All @@ -40,13 +40,12 @@ public function testPredictMultiFeaturesSamples()
$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);
}

}

0 comments on commit 633974f

Please sign in to comment.