def test_real(self, random_matrix): """Check permanent(A)=perm_real(A) for a random real matrix. """ A = random_matrix(6) p = perm(A) expected = perm_real(A) assert p == expected A = random_matrix(6) A = np.array(A, dtype=np.complex128) p = perm(A) expected = perm_real(np.float64(A.real)) assert p == expected
def test_complex(self, random_matrix): """Check perm(A)=perm_complex(A) for a random matrix. """ A = random_matrix(6) p = perm(A) expected = perm_complex(A) assert np.allclose(p, expected)
def test_complex_no_imag(self, random_matrix): """Check perm(A)=perm_real(A) for a complex random matrix with zero imaginary parts. """ A = np.complex128(random_matrix(6)) p = perm(A) expected = perm_real(A.real) assert np.allclose(p, expected)
def test_3x3(self, random_matrix): """Check 3x3 permanent""" A = random_matrix(3) p = perm(A) expected = (A[0, 2] * A[1, 1] * A[2, 0] + A[0, 1] * A[1, 2] * A[2, 0] + A[0, 2] * A[1, 0] * A[2, 1] + A[0, 0] * A[1, 2] * A[2, 1] + A[0, 1] * A[1, 0] * A[2, 2] + A[0, 0] * A[1, 1] * A[2, 2]) assert p == expected
def test_2x2(self, random_matrix): """Check 2x2 permanent""" A = random_matrix(2) p = perm(A) assert p == A[0, 0] * A[1, 1] + A[0, 1] * A[1, 0]
def test_nan(self): """Check exception for non-finite matrix""" A = np.array([[2, 1], [1, np.nan]]) with pytest.raises(ValueError): perm(A)
def test_square_exception(self): """Check exception for non-square argument""" A = np.zeros([2, 3]) with pytest.raises(ValueError): perm(A)
def test_array_exception(self): """Check exception for non-matrix argument""" with pytest.raises(TypeError): perm(1)