Skip to content

Commit 6072bf2

Browse files
committed
Add matrix det axiom tests.
1 parent 8886d15 commit 6072bf2

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

tests/LinearAlgebra/Matrix/Numeric/MatrixAxiomsTest.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@
4848
* - Determinant
4949
* - det(A) = det(Aᵀ)
5050
* - det(AB) = det(A)det(B)
51+
* - A is invertible if and only if det A ≠ 0
52+
* - A is not invertible if det A = 0
53+
* - A is triangular, det(A) is product of entries on main diagonal
5154
* - LU Decomposition (PA = LU)
5255
* - PA = LU
5356
* - A = P⁻¹LU
@@ -1240,6 +1243,74 @@ public function testDeterminantProductSameAsProductOfDeterminants(array $A, arra
12401243
$this->assertEqualsWithDelta($det⟮AB⟯, $det⟮A⟯det⟮B⟯, 0.000001);
12411244
}
12421245

1246+
1247+
/**
1248+
* @test Axiom: A is invertible if and only if det A ≠ 0
1249+
*
1250+
* @dataProvider dataProviderForNonsingularMatrix
1251+
* @param array $A
1252+
* @throws \Exception
1253+
*/
1254+
public function testMatrixInvertibleIfAndOnlyIfDetANotEqualsZero(array $A)
1255+
{
1256+
// Given
1257+
$A = MatrixFactory::create($A);
1258+
1259+
// When
1260+
$det⟮A⟯ = $A->det();
1261+
$isInvertible = $A->isInvertible();
1262+
1263+
// Then
1264+
$this->assertNotEquals(0, $det⟮A⟯);
1265+
$this->assertTrue($isInvertible);
1266+
}
1267+
1268+
/**
1269+
* @test Axiom: A is not invertible if det A = 0
1270+
*
1271+
* @dataProvider dataProviderForSingularMatrix
1272+
* @param array $A
1273+
* @throws \Exception
1274+
*/
1275+
public function testMatrixNotInvertibleIfDetEqualsZero(array $A)
1276+
{
1277+
// Given
1278+
$A = MatrixFactory::create($A);
1279+
1280+
// When
1281+
$det⟮A⟯ = $A->det();
1282+
$isInvertible = $A->isInvertible();
1283+
1284+
// Then
1285+
$this->assertEquals(0, $det⟮A⟯);
1286+
$this->assertFalse($isInvertible);
1287+
}
1288+
1289+
/**
1290+
* @test Axiom: A is triangular, det(A) is product of entries on main diagonal
1291+
*
1292+
* @dataProvider dataProviderForUpperTriangularMatrix
1293+
* @param array $A
1294+
* @throws \Exception
1295+
*/
1296+
public function testAIsTriangularDetAIsProductOfEntriesOnMainDiagonal(array $A)
1297+
{
1298+
// Given
1299+
$A = MatrixFactory::create($A);
1300+
1301+
// When
1302+
$det⟮A⟯ = $A->det();
1303+
1304+
// And
1305+
$product = 1;
1306+
for ($i = 0; $i < $A->getM(); $i++) {
1307+
$product *= $A[$i][$i];
1308+
}
1309+
1310+
// Then
1311+
$this->assertEquals($det⟮A⟯, $product);
1312+
}
1313+
12431314
/**
12441315
* @test Axiom: PA = LU
12451316
* Basic LU decomposition property that permutation matrix times the matrix is the product of the lower and upper decomposition matrices.

0 commit comments

Comments
 (0)