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_ssqr_inertia_unstable(self, unstable_example): A_hat, D_hat, U, H, b_hat, x_hat, evals, index = unstable_example x, ratio, d_hat = core.SSQR_inertia(D_hat, U, H, b_hat) assert np.allclose(np.dot(A_hat, x), b_hat) in_qr = utils.inertia_qr(ratio, d_hat) result = core.ldl(D_hat, U, H) assert len(result) == len(D_hat) in_ldl = utils.inertia_ldl(result) print in_qr, in_ldl
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
def inertia_fast(D, U, H): D_hat, G = core.ldl(D, U, H) inertia = utils.inertia_ldl(D_hat, G) return inertia