Exemple #1
0
def test_invertibility():
    m1 = Matrix([[6, 4, 4, 4], [5, 5, 7, 6], [4, -9, 3, -7], [9, 1, 7, -6]])
    assert m1.determinant() == -2120
    assert m1.invertible()

    m2 = Matrix([[-4, 2, -2, -3], [9, 6, 2, 6], [0, -5, 1, -5], [0, 0, 0, 0]])
    assert m2.determinant() == 0
    assert m2.invertible() == False
Exemple #2
0
def test_4x4_determinant():
    m = Matrix([[-2, -8, 3, 5], [-3, 1, 7, 3], [1, 2, -9, 6], [-6, 7, 7, -9]])
    assert m.cofactor(0, 0) == 690
    assert m.cofactor(0, 1) == 447
    assert m.cofactor(0, 2) == 210
    assert m.cofactor(0, 3) == 51
    assert m.determinant() == -4071
Exemple #3
0
def test_inverse():
    a = Matrix([[-5, 2, 6, -8], [1, -5, 1, 8], [7, 7, -6, -7], [1, -3, 7, 4]])
    b = a.inverse()
    assert a.determinant() == 532
    assert a.cofactor(2, 3) == -160
    assert b[3][2] == -160 / 532
    assert a.cofactor(3, 2) == 105
    assert b[2][3] == 105 / 532
    b_exp = Matrix([
        [0.21805, 0.45113, 0.24060, -0.04511],
        [-0.80827, -1.45677, -0.44361, 0.52068],
        [-0.07895, -0.22368, -0.05263, 0.19737],
        [-0.52256, -0.81391, -0.30075, 0.30639],
    ])
    assert b_exp == b
Exemple #4
0
def test_2x2_determinant():
    m = Matrix([[1, 5], [-3, 2]])
    assert m.determinant() == 17
Exemple #5
0
def test_3x3_determinant():
    m = Matrix([[1, 2, 6], [-5, 8, -4], [2, 6, 4]])
    assert m.cofactor(0, 0) == 56
    assert m.cofactor(0, 1) == 12
    assert m.cofactor(0, 2) == -46
    assert m.determinant() == -196