def test_raise_exception_when_inverse_of_a_noninvertible_matrix(): A = Matrix(-4, 2, -2, -3, 9, 6, 2, 6, 0, -5, 1, -5, 0, 0, 0, 0) assert not A.is_invertible() with pytest.raises(NonInvertibleMatrixError): A.inverse()
def test_calculating_the_inverse_of_a_third_matrix(): # Given A = Matrix(9, 3, 0, 9, -5, -2, -6, -3, -4, 9, 6, 4, -7, 6, 6, 2) # Then assert A.inverse() == Matrix(-0.04074, -0.07778, 0.14444, -0.22222, -0.07778, 0.03333, 0.36667, -0.33333, -0.02901, -0.14630, -0.10926, 0.12963, 0.17778, 0.06667, -0.26667, 0.33333)
def test_calculating_the_inverse_of_another_matrix(): # Given A = Matrix(8, -5, 9, 2, 7, 5, 6, 1, -6, 0, 9, 6, -3, 0, -9, -4) # Then assert A.inverse() == Matrix(-0.15385, -0.15385, -0.28205, -0.53846, -0.07692, 0.12308, 0.02564, 0.03077, 0.35897, 0.35897, 0.43590, 0.92308, -0.69231, -0.69231, -0.76923, -1.92308)
def test_multiplying_a_product_by_its_inverse(): # Given A = Matrix(3, -9, 7, 3, 3, -8, 2, -9, -4, 4, 4, 1, -6, 5, -1, 1) B = Matrix(8, 2, 2, 2, 3, -1, 7, 0, 7, 0, 5, 4, 6, -2, 0, 5) C = A * B # Then assert C * B.inverse() == A
def test_calculating_the_inverse_of_a_matrix(): # Given A = Matrix(-5, 2, 6, -8, 1, -5, 1, 8, 7, 7, -6, -7, 1, -3, 7, 4) B = A.inverse() # Then assert A.determinant() == 532 assert A.cofactor(2, 3) == -160 assert equal(B.at(3, 2), -160/532) assert A.cofactor(3, 2) == 105 assert equal(B.at(2, 3), 105/532) assert B == 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)