def test_cg_sparse(self): # Create sparse problem. num_unknowns = 100 A = self._create_sparse_hpd_matrix( num_unknowns ) rhs = np.ones( (num_unknowns,1) ) x0 = np.zeros( (num_unknowns,1) ) # Solve using CG. tol = 1.0e-11 out = nm.cg(A, rhs, x0, tol=tol) # Make sure the method converged. #self.assertEqual(info, 0) # Check the residual. res = rhs - A * out['xk'] self.assertAlmostEqual( np.linalg.norm(res)/np.linalg.norm(rhs), 0.0, delta=tol )
def test_cg_dense(self): # Create dense problem. num_unknowns = 5 A = self._create_hpd_matrix( num_unknowns ) rhs = np.ones( (num_unknowns,1) ) x0 = np.zeros( (num_unknowns,1) ) # Solve using CG. tol = 1.0e-13 out = nm.cg( A, rhs, x0, tol=tol ) # Make sure the method converged. self.assertEqual(out['info'], 0) # Check the residual. res = rhs - np.dot(A, out['xk']) self.assertAlmostEqual( np.linalg.norm(res)/np.linalg.norm(rhs), 0.0, delta=tol )
def test_cg_sparse(self): # Create sparse problem. num_unknowns = 100 A = self._create_sparse_hpd_matrix(num_unknowns) rhs = np.ones((num_unknowns, 1)) x0 = np.zeros((num_unknowns, 1)) # Solve using CG. tol = 1.0e-11 out = nm.cg(A, rhs, x0, tol=tol) # Make sure the method converged. #self.assertEqual(info, 0) # Check the residual. res = rhs - A * out['xk'] self.assertAlmostEqual(np.linalg.norm(res) / np.linalg.norm(rhs), 0.0, delta=tol)
def test_cg_dense(self): # Create dense problem. num_unknowns = 5 A = self._create_hpd_matrix(num_unknowns) rhs = np.ones((num_unknowns, 1)) x0 = np.zeros((num_unknowns, 1)) # Solve using CG. tol = 1.0e-13 out = nm.cg(A, rhs, x0, tol=tol) # Make sure the method converged. self.assertEqual(out['info'], 0) # Check the residual. res = rhs - np.dot(A, out['xk']) self.assertAlmostEqual(np.linalg.norm(res) / np.linalg.norm(rhs), 0.0, delta=tol)