Matrix fixes

This commit is contained in:
Dennis Eichhorn 2016-10-22 22:43:15 +02:00
parent 81d20b1d3f
commit 68051600a3

View File

@ -134,7 +134,7 @@ class Matrix implements \ArrayAccess, \Iterator
public function transpose() : Matrix public function transpose() : Matrix
{ {
$matrix = new Matrix($this->n, $this->m); $matrix = new Matrix($this->n, $this->m);
$matrix->setMatrix(array_map(null, $matrix->getMatrix())); $matrix->setMatrix(array_map(null, ...$this->matrix));
return $matrix; return $matrix;
} }
@ -152,23 +152,40 @@ class Matrix implements \ArrayAccess, \Iterator
return $this->matrix; return $this->matrix;
} }
/**
* Get matrix rank.
*
* @return int
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function rank() : int
{
return 0;
}
/** /**
* Set matrix array. * Set matrix array.
* *
* @param array $matrix Matrix * @param array $matrix Matrix
* *
* @return Matrix
*
* @throws \Exception * @throws \Exception
* *
* @since 1.0.0 * @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com> * @author Dennis Eichhorn <d.eichhorn@oms.com>
*/ */
public function setMatrix(array $matrix) public function setMatrix(array $matrix) : Matrix
{ {
if ($this->m !== count($matrix) || $this->n !== count($matrix[0])) { if ($this->m !== count($matrix) || $this->n !== count($matrix[0])) {
throw new DimensionException(count($matrix), count($matrix[0])); throw new DimensionException(count($matrix), count($matrix[0]));
} }
$this->matrix = $matrix; $this->matrix = $matrix;
return $this;
} }
/** /**
@ -356,7 +373,7 @@ class Matrix implements \ArrayAccess, \Iterator
for ($c = 0; $c < $nDim; $c++) { // Column of $matrix for ($c = 0; $c < $nDim; $c++) { // Column of $matrix
$temp = 0; $temp = 0;
for ($j = 0; $j < $mDim; $i++) { // Row of $matrix for ($j = 0; $j < $mDim; $j++) { // Row of $matrix
$temp += $this->matrix[$i][$j] * $matrixArr[$j][$c]; $temp += $this->matrix[$i][$j] * $matrixArr[$j][$c];
} }
@ -557,6 +574,14 @@ class Matrix implements \ArrayAccess, \Iterator
return $newMatrix; return $newMatrix;
} }
public function diagonalize() : Matrix
{
$newMatrix = new Matrix($this->m, $this->n);
$newMatrix->setMatrix($this->diag($this->matrix));
return $newMatrix;
}
/** /**
* Diagonalize matrix. * Diagonalize matrix.
* *