def test_lin_solve_without_last_entry(self, normal_example): n, r, D, U, H, A, b = normal_example D_hat, G = core.ldl_fast(D, U, H) x = core.lin_solve(D_hat, G, U, b) G[-1, :] = 0 x_hat = core.lin_solve(D_hat, G, U, b) print np.linalg.norm(x - x_hat)
def test_evec_with_eval(self, normal_example): n, r, D, U, H, A, b = normal_example evals, evecs = np.linalg.eig(A) index = np.random.randint(n) D_hat = D - evals[index] A_hat = A - np.eye(n) * evals[index] D_hat, G, status = core.ldl_fast(D_hat, U, H) assert status is True b = np.zeros(n) x = core.lin_solve(D_hat, G, U, b) assert np.allclose(np.dot(A_hat, x), evals[index] * x)
def compute_inertia(D, U, H): # Return the inertia of the matrix try: result = core.ldl_fast(D, U, H) if len(result) == 1: inertia = utils.inertia_ldl(result) else: D_hat, G = result inertia = utils.inertia_ldl(D_hat) except ValueError as e: print("Turning to stable method.") x, ratio, D_hat = core.SSQR_inertia(D, U, H, np.random.randn(len(D))) inertia = utils.inertia_qr(ratio, D_hat) return inertia
def test_rqi_converge(self, normal_example): n, r, D, U, H, A, b = normal_example b = b / np.linalg.norm(b) mu = np.dot(b.T, np.dot(A, b)) for i in range(10): mu, b, error, n_mid = alg.RQI_step(D, U, H, mu, b) print error if error < 1e-10: print("Converged in %d iterations." % (i+1)) break D_hat = core.ldl_fast(D-mu, U, H) inertia = utils.inertia_ldl(D_hat) print inertia assert np.allclose(np.dot(A, b), mu*b)
def test_rqi_fast_converge(self, normal_example): # RQI_fast has a problem where the eigen vector has not converged n, r, D, U, H, A, b = normal_example b = b / np.linalg.norm(b) mu = np.dot(b.T, np.dot(A, b)) old_mu = mu for i in range(20): mu, b, error, n_mid = alg.RQI_fast(D, U, H, mu, b) ratio = abs(old_mu - mu) / abs(old_mu) print "Relative error between guesses: %e, error = %e" % (ratio, error) old_mu = mu if ratio < 1e-8: print("Converged in %d iterations." %(i+1)) break D_hat = core.ldl_fast(D-mu, U, H) inertia = utils.inertia_ldl(D_hat) print inertia