def test_inverse_iteration(self): M = random_symmetric(3) expected_eigval, expected_eigvec = self.np_eig(M, largest=False) actual_eigval, actual_eigvec = single.inverse_iteration(M) self.assertTrue(self.absallclose(actual_eigvec, expected_eigvec)) self.assertTrue(self.absallclose(actual_eigval, expected_eigval))
def test_factorization_equals_initial_symmetric(self): A = utils.random_symmetric(3) U, S, V = SVD(A).decompose() actual = U @ S @ V.T expected = A self.assertTrue(np.allclose(actual, expected))
def test_power_iteration(self): M = random_symmetric(3) expected_eigval, expected_eigvec = self.np_eig(M) actual_eigval, actual_eigvec = single.power_iteration(M) self.assertTrue(self.absallclose(actual_eigval, expected_eigval)) self.assertTrue(self.absallclose(actual_eigvec, expected_eigvec))
def test_rayleigh_smallest(self): M = random_symmetric(3) expected_eigval, expected_eigvec = self.np_eig(M, largest=False) initial_eigval = single.inverse_iteration(M)[0] actual_eigval, actual_eigvec = single.rayleigh_quotient_iteration( M, initial_eigval) self.assertTrue(self.absallclose(actual_eigvec, expected_eigvec)) self.assertTrue(self.absallclose(actual_eigval, expected_eigval))
def test_projected_iteration(self): M = random_symmetric(3) eigvals, eigvecs = LA.eig(M) idx = np.abs(eigvals).argsort()[::-1] expected_eigvecs = eigvecs[:, idx] expected_eigvals = eigvals[idx] actual_eigvals, actual_eigvecs = multi.projected_iteration(M, 3) self.assertTrue(self.absallclose(actual_eigvecs, expected_eigvecs)) self.assertTrue(self.absallclose(actual_eigvals, expected_eigvals))
def test_qr_algorithm_with_hessenberg(self): M = random_symmetric(4) eigvals, eigvecs = LA.eig(M) idx = np.abs(eigvals).argsort()[::-1] expected_eigvecs = eigvecs[:, idx] expected_eigvals = eigvals[idx] actual_eigvals, actual_eigvecs = multi.qr_algorithm(M) self.assertTrue(self.absallclose(actual_eigvecs, expected_eigvecs)) self.assertTrue(self.absallclose(actual_eigvals, expected_eigvals))
def test_hessenberg_symmetric_stays_symmetric(self): M = random_symmetric(4) hess = multi.hessenberg(M) self.assertTrue(is_symmetric(hess))