2018-02-23 22:05:46 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Phpml\Tests\Math\LinearAlgebra;
|
|
|
|
|
|
|
|
use Phpml\Exception\MatrixException;
|
|
|
|
use Phpml\Math\LinearAlgebra\LUDecomposition;
|
|
|
|
use Phpml\Math\Matrix;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* LUDecomposition is used and tested in Matrix::inverse method so not all tests are required
|
|
|
|
*/
|
|
|
|
final class LUDecompositionTest extends TestCase
|
|
|
|
{
|
|
|
|
public function testNotSquareMatrix(): void
|
|
|
|
{
|
|
|
|
$this->expectException(MatrixException::class);
|
|
|
|
|
|
|
|
new LUDecomposition(new Matrix([1, 2, 3, 4, 5]));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSolveWithInvalidMatrix(): void
|
|
|
|
{
|
|
|
|
$this->expectException(MatrixException::class);
|
|
|
|
|
|
|
|
$lu = new LUDecomposition(new Matrix([[1, 2], [3, 4]]));
|
|
|
|
$lu->solve(new Matrix([1, 2, 3]));
|
|
|
|
}
|
2018-03-04 16:05:25 +00:00
|
|
|
|
|
|
|
public function testLowerTriangularFactor(): void
|
|
|
|
{
|
|
|
|
$lu = new LUDecomposition(new Matrix([[1, 2], [3, 4]]));
|
|
|
|
$L = $lu->getL();
|
|
|
|
|
2018-10-28 06:44:52 +00:00
|
|
|
self::assertSame([[1.0, 0.0], [0.3333333333333333, 1.0]], $L->toArray());
|
2018-03-04 16:05:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testUpperTriangularFactor(): void
|
|
|
|
{
|
|
|
|
$lu = new LUDecomposition(new Matrix([[1, 2], [3, 4]]));
|
|
|
|
$U = $lu->getU();
|
|
|
|
|
2018-10-28 06:44:52 +00:00
|
|
|
self::assertSame([[3.0, 4.0], [0.0, 0.6666666666666667]], $U->toArray());
|
2018-03-04 16:05:25 +00:00
|
|
|
}
|
2018-02-23 22:05:46 +00:00
|
|
|
}
|