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,4 +1,5 @@
<?php <?php
declare (strict_types = 1); declare (strict_types = 1);
namespace Phpml\Math; namespace Phpml\Math;
@ -40,7 +41,7 @@ class 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();
} }
@ -88,7 +89,7 @@ class Matrix
} }
$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];
} }
@ -117,7 +118,7 @@ class Matrix
$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();
@ -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

@ -48,5 +48,4 @@ class LeastSquaresTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(4094, $regression->predict([60000, 1996]), '', $delta); $this->assertEquals(4094, $regression->predict([60000, 1996]), '', $delta);
} }
} }