def test_spd_inv_split(spd_matrix, epsilon, method, canonical_signs): split = spd_inv_split(spd_matrix, epsilon=epsilon, method=method, canonical_signs=canonical_signs) spd_matrix_inv = split @ split.T sminv, _ = spd_eig(spd_matrix_inv) sm, _ = spd_eig(spd_matrix) assert_array_almost_equal(np.sort(sm), np.sort(1. / sminv)[-3:]) if canonical_signs: for i in range(3): assert_(np.max(split[:, i]) > 0)
def test_spd_eig(spd_matrix, epsilon, method, canonical_signs): sm, vm = spd_eig(spd_matrix, epsilon=epsilon, method=method, canonical_signs=canonical_signs) assert_(sm[0] >= sm[1] >= sm[2]) assert_array_almost_equal(vm @ np.diag(sm) @ vm.T, spd_matrix) assert_array_almost_equal(vm.T @ vm, np.eye(3)) if canonical_signs or True: # largest element in each column in vm should be positive for col in range(vm.shape[1]): assert_(np.max(vm[:, col]) > 0)
def test_spd_eig_invalid_inputs(spd_matrix): with assert_raises(ValueError): spd_eig(spd_matrix, method='...') # not symmetric with assert_raises(ValueError): spd_eig(np.arange(9).reshape(3, 3), check_sym=True) # zero rank with assert_raises(ZeroRankError): spd_eig(np.zeros((3, 3)))
def test_spd_inv(spd_matrix, epsilon, method): W = spd_inv(spd_matrix, epsilon=epsilon, method=method) sm, _ = spd_eig(spd_matrix) sminv, _ = spd_eig(W) assert_array_almost_equal(np.sort(sm), np.sort(1. / sminv))