def test_2(self): n = 4 # build matrix with specified first generalized eigenvector A, B = build_lowrank(n, [1, 2, 0.001, 3], seed=0) w, V = eigh(A, B) v0 = speigh(A, B, rho=0)[1][2] vm4 = speigh(A, B, rho=1e-4)[1][2] vm2 = speigh(A, B, rho=0.01)[1][2] # using a low value for `rho`, we should recover the small element # but when rho is higher, it should be truncated to zero. np.testing.assert_almost_equal(np.abs(v0), 0.001) np.testing.assert_almost_equal(np.abs(vm4), 0.001) np.testing.assert_almost_equal(vm2, 0)
def test_1(self): # test with indefinite A matrix, identity B n = 4 x = np.array([1.0, 2.0, 3.0, 0]) A = np.outer(x, x) B = np.eye(n) w, V = eigh(A, B) w0, v0, v0f = speigh(A, B, rho=0.0001, return_x_f=True) self.assert_close(w0, v0, v0f, A, B)
def test_4(self): # test with positive semidefinite A matrix, and general # matrix B n = 4 A = rand_pos_semidef(n) B = rand_pos_semidef(n) + np.eye(n) w, V = eigh(A, B) v_init = V[:, 0] + 0.1*np.random.randn(n) w0, v0, v0f = speigh(A, B, rho=0, return_x_f=True) self.assert_close(w0, v0, v0f, A, B)
def test_3(self): # test with positive semidefinite A matrix, and diagonal # matrix B n = 4 A = rand_pos_semidef(n) B = np.diag(np.random.randn(n)**2) w, V = eigh(A, B) w0, v0, v0f = speigh(A, B, rho=0, return_x_f=True) self.assert_close(w0, v0, v0f, A, B)