2016-04-29 22:58:54 +00:00
|
|
|
<?php
|
2016-04-29 22:59:10 +00:00
|
|
|
|
|
|
|
declare (strict_types = 1);
|
2016-04-29 22:58:54 +00:00
|
|
|
|
|
|
|
namespace Phpml\Math;
|
|
|
|
|
|
|
|
use Phpml\Exception\InvalidArgumentException;
|
|
|
|
use Phpml\Exception\MatrixException;
|
|
|
|
|
|
|
|
class Matrix
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $matrix;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
private $rows;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
private $columns;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var float
|
|
|
|
*/
|
|
|
|
private $determinant;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $matrix
|
2016-04-29 22:59:10 +00:00
|
|
|
* @param bool $validate
|
2016-04-29 22:58:54 +00:00
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function __construct(array $matrix, bool $validate = true)
|
|
|
|
{
|
|
|
|
$this->rows = count($matrix);
|
|
|
|
$this->columns = count($matrix[0]);
|
|
|
|
|
2016-04-29 22:59:10 +00:00
|
|
|
if ($validate) {
|
|
|
|
for ($i = 0; $i < $this->rows; ++$i) {
|
2016-04-29 22:58:54 +00:00
|
|
|
if (count($matrix[$i]) !== $this->columns) {
|
|
|
|
throw InvalidArgumentException::matrixDimensionsDidNotMatch();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->matrix = $matrix;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function toArray()
|
|
|
|
{
|
|
|
|
return $this->matrix;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getRows()
|
|
|
|
{
|
|
|
|
return $this->rows;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getColumns()
|
|
|
|
{
|
|
|
|
return $this->columns;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $column
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*
|
|
|
|
* @throws MatrixException
|
|
|
|
*/
|
|
|
|
public function getColumnValues($column)
|
|
|
|
{
|
2016-04-29 22:59:10 +00:00
|
|
|
if ($column >= $this->columns) {
|
2016-04-29 22:58:54 +00:00
|
|
|
throw MatrixException::columnOutOfRange();
|
|
|
|
}
|
|
|
|
|
|
|
|
$values = [];
|
2016-04-29 22:59:10 +00:00
|
|
|
for ($i = 0; $i < $this->rows; ++$i) {
|
2016-04-29 22:58:54 +00:00
|
|
|
$values[] = $this->matrix[$i][$column];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $values;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return float|int
|
|
|
|
*
|
|
|
|
* @throws MatrixException
|
|
|
|
*/
|
|
|
|
public function getDeterminant()
|
|
|
|
{
|
2016-04-29 22:59:10 +00:00
|
|
|
if ($this->determinant) {
|
2016-04-29 22:58:54 +00:00
|
|
|
return $this->determinant;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$this->isSquare()) {
|
|
|
|
throw MatrixException::notSquareMatrix();
|
|
|
|
}
|
|
|
|
|
|
|
|
$determinant = 0;
|
|
|
|
if ($this->rows == 1 && $this->columns == 1) {
|
|
|
|
$determinant = $this->matrix[0][0];
|
2016-04-29 22:59:10 +00:00
|
|
|
} elseif ($this->rows == 2 && $this->columns == 2) {
|
2016-04-29 22:58:54 +00:00
|
|
|
$determinant = $this->matrix[0][0] * $this->matrix[1][1] -
|
|
|
|
$this->matrix[0][1] * $this->matrix[1][0];
|
|
|
|
} else {
|
2016-04-29 22:59:10 +00:00
|
|
|
for ($j = 0; $j < $this->columns; ++$j) {
|
2016-04-29 22:58:54 +00:00
|
|
|
$subMatrix = $this->crossOut(0, $j);
|
|
|
|
if (fmod($j, 2) == 0) {
|
|
|
|
$determinant += $this->matrix[0][$j] * $subMatrix->getDeterminant();
|
|
|
|
} else {
|
|
|
|
$determinant -= $this->matrix[0][$j] * $subMatrix->getDeterminant();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->determinant = $determinant;
|
|
|
|
}
|
|
|
|
|
2016-04-29 22:59:10 +00:00
|
|
|
/**
|
2016-04-29 22:58:54 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isSquare()
|
|
|
|
{
|
|
|
|
return $this->columns === $this->rows;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Matrix
|
|
|
|
*/
|
|
|
|
public function transpose()
|
|
|
|
{
|
|
|
|
$newMatrix = [];
|
2016-04-29 22:59:10 +00:00
|
|
|
for ($i = 0; $i < $this->rows; ++$i) {
|
|
|
|
for ($j = 0; $j < $this->columns; ++$j) {
|
2016-04-29 22:58:54 +00:00
|
|
|
$newMatrix[$j][$i] = $this->matrix[$i][$j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new self($newMatrix, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Matrix $matrix
|
|
|
|
*
|
|
|
|
* @return Matrix
|
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function multiply(Matrix $matrix)
|
|
|
|
{
|
|
|
|
if ($this->columns != $matrix->getRows()) {
|
|
|
|
throw InvalidArgumentException::inconsistentMatrixSupplied();
|
|
|
|
}
|
|
|
|
|
|
|
|
$product = [];
|
|
|
|
$multiplier = $matrix->toArray();
|
2016-04-29 22:59:10 +00:00
|
|
|
for ($i = 0; $i < $this->rows; ++$i) {
|
|
|
|
for ($j = 0; $j < $matrix->getColumns(); ++$j) {
|
2016-04-29 22:58:54 +00:00
|
|
|
$product[$i][$j] = 0;
|
2016-04-29 22:59:10 +00:00
|
|
|
for ($k = 0; $k < $this->columns; ++$k) {
|
2016-04-29 22:58:54 +00:00
|
|
|
$product[$i][$j] += $this->matrix[$i][$k] * $multiplier[$k][$j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-29 22:59:10 +00:00
|
|
|
|
2016-04-29 22:58:54 +00:00
|
|
|
return new self($product, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $value
|
|
|
|
*
|
|
|
|
* @return Matrix
|
|
|
|
*/
|
|
|
|
public function divideByScalar($value)
|
|
|
|
{
|
|
|
|
$newMatrix = array();
|
2016-04-29 22:59:10 +00:00
|
|
|
for ($i = 0; $i < $this->rows; ++$i) {
|
|
|
|
for ($j = 0; $j < $this->columns; ++$j) {
|
2016-04-29 22:58:54 +00:00
|
|
|
$newMatrix[$i][$j] = $this->matrix[$i][$j] / $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new self($newMatrix, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Matrix
|
|
|
|
*
|
|
|
|
* @throws MatrixException
|
|
|
|
*/
|
|
|
|
public function inverse()
|
|
|
|
{
|
|
|
|
if (!$this->isSquare()) {
|
|
|
|
throw MatrixException::notSquareMatrix();
|
|
|
|
}
|
|
|
|
|
|
|
|
$newMatrix = array();
|
2016-04-29 22:59:10 +00:00
|
|
|
for ($i = 0; $i < $this->rows; ++$i) {
|
|
|
|
for ($j = 0; $j < $this->columns; ++$j) {
|
2016-04-29 22:58:54 +00:00
|
|
|
$subMatrix = $this->crossOut($i, $j);
|
|
|
|
if (fmod($i + $j, 2) == 0) {
|
|
|
|
$newMatrix[$i][$j] = ($subMatrix->getDeterminant());
|
|
|
|
} else {
|
|
|
|
$newMatrix[$i][$j] = -($subMatrix->getDeterminant());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$cofactorMatrix = new self($newMatrix, false);
|
|
|
|
|
|
|
|
return $cofactorMatrix->transpose()->divideByScalar($this->getDeterminant());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $row
|
|
|
|
* @param int $column
|
|
|
|
*
|
|
|
|
* @return Matrix
|
|
|
|
*/
|
|
|
|
public function crossOut(int $row, int $column)
|
|
|
|
{
|
|
|
|
$newMatrix = [];
|
|
|
|
$r = 0;
|
2016-04-29 22:59:10 +00:00
|
|
|
for ($i = 0; $i < $this->rows; ++$i) {
|
2016-04-29 22:58:54 +00:00
|
|
|
$c = 0;
|
|
|
|
if ($row != $i) {
|
2016-04-29 22:59:10 +00:00
|
|
|
for ($j = 0; $j < $this->columns; ++$j) {
|
2016-04-29 22:58:54 +00:00
|
|
|
if ($column != $j) {
|
|
|
|
$newMatrix[$r][$c] = $this->matrix[$i][$j];
|
2016-04-29 22:59:10 +00:00
|
|
|
++$c;
|
2016-04-29 22:58:54 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-29 22:59:10 +00:00
|
|
|
++$r;
|
2016-04-29 22:58:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new self($newMatrix, false);
|
|
|
|
}
|
|
|
|
}
|