diff --git a/Math/Matrix/Matrix.php b/Math/Matrix/Matrix.php index 08d2929da..93cd57c72 100755 --- a/Math/Matrix/Matrix.php +++ b/Math/Matrix/Matrix.php @@ -884,10 +884,16 @@ class Matrix implements \ArrayAccess, \Iterator } return $matrix; - } else { - // @todo: implement - throw new \Exception('Not yet implemented'); } + + $eig = new EigenvalueDecomposition($this); + + $d = $eig->getD(); + for ($i = 0; $d->getM(); ++$i) { + $d->matrix[$i][$i] = \pow($d->matrix[$i][$i], $exponent); + } + + return $eig->getV()->mult($d)->mult($eig->getV()->inverse()); } /** diff --git a/tests/Math/Matrix/MatrixTest.php b/tests/Math/Matrix/MatrixTest.php index b2cda891f..e6637570e 100755 --- a/tests/Math/Matrix/MatrixTest.php +++ b/tests/Math/Matrix/MatrixTest.php @@ -646,9 +646,25 @@ final class MatrixTest extends \PHPUnit\Framework\TestCase [80.25, 96.75, 113.25], [120.75, 146.25, 171.75], ], - $m->pow(2.5)->toArray(), + $m->pow(2)->toArray(), 0.1 ); + + $m = Matrix::fromArray([ + [1, 1, 1], + [1, 2, 3], + [1, 3, 6], + ]); + + self::assertEqualsWithDelta( + [ + [0.8901, 0.5882, 0.3684], + [0.5882, 1.2035, 1.3799], + [0.3684, 1.3799, 3.1167], + ], + $m->pow(2 / 3)->toArray(), + 0.01 + ); } public function testExp() : void