def loop(self, d_idx, d_m_mat, DWIJ, HIJ): i, j, n, nt = declare('int', 4) n = self.dim nt = n + 1 # Note that we allocate enough for a 3D case but may only use a # part of the matrix. temp = declare('matrix(12)') res = declare('matrix(3)') eps = 1.0e-04 * HIJ for i in range(n): for j in range(n): temp[nt * i + j] = d_m_mat[9 * d_idx + 3 * i + j] # Augmented part of matrix temp[nt * i + n] = DWIJ[i] gj_solve(temp, n, 1, res) res_mag = 0.0 dwij_mag = 0.0 for i in range(n): res_mag += abs(res[i]) dwij_mag += abs(DWIJ[i]) change = abs(res_mag - dwij_mag) / (dwij_mag + eps) if change < self.tol: for i in range(n): DWIJ[i] = res[i]
def test_symmetric_matrix(self): n = 3 mat = [[0.96, 4.6, -3.7], [4.6, 4.3, -0.67], [-3.7, -0.67, -5.]] b = [2.4, 3.6, -5.8] result = [0.0] * n gj_solve(np.ravel(mat), b, n, result) mat = np.matrix(mat) new_b = mat * np.transpose(np.matrix(result)) new_b = np.ravel(np.array(new_b)) assert np.allclose(new_b, np.array(b))
def test_band_matrix(self): n = 3 mat = [[1., -2., 0.], [1., -1., 3.], [2., 5., 0.]] b = [-3., 1., 0.5] result = [0.0] * n gj_solve(np.ravel(mat), b, n, result) mat = np.matrix(mat) new_b = mat * np.transpose(np.matrix(result)) new_b = np.ravel(np.array(new_b)) assert np.allclose(new_b, np.array(b))
def test_symmetric_positivedefinite_Matrix(self): n = 4 mat = [[1., 1., 4., -1.], [1., 5., 0., -1.], [4., 0., 21., -4.], [-1., -1., -4., 10.]] b = [2.4, 3.6, -5.8, 0.5] result = [0.0] * n gj_solve(np.ravel(mat), b, n, result) mat = np.matrix(mat) new_b = mat * np.transpose(np.matrix(result)) new_b = np.ravel(np.array(new_b)) assert np.allclose(new_b, np.array(b))
def test_tridiagonal_matrix(self): n = 4 mat = [[-2., 1., 0., 0.], [1., -2., 1., 0.], [0., 1., -2., 0.], [0., 0., 1., -2.]] b = [-1., 0., 0., -5.] result = [0.0] * n gj_solve(np.ravel(mat), b, n, result) mat = np.matrix(mat) new_b = mat * np.transpose(np.matrix(result)) new_b = np.ravel(np.array(new_b)) assert np.allclose(new_b, np.array(b))
def loop(self, d_idx, d_m_mat, DWIJ, HIJ): i, j, n = declare('int', 3) n = self.dim temp = declare('matrix(9)') res = declare('matrix(3)') eps = 1.0e-04 * HIJ for i in range(n): for j in range(n): temp[n * i + j] = d_m_mat[9 * d_idx + 3 * i + j] gj_solve(temp, DWIJ, n, res) change = 0.0 for i in range(n): change += abs(DWIJ[i] - res[i]) / (abs(DWIJ[i]) + eps) if change <= self.tol: for i in range(n): DWIJ[i] = res[i]
def test_general_matrix(self): """ This is a general matrix which needs partial pivoting to be solved. References ---------- http://web.mit.edu/10.001/Web/Course_Notes/GaussElimPivoting.html """ n = 4 mat = [[0.02, 0.01, 0., 0.], [1., 2., 1., 0.], [0., 1., 2., 1.], [0., 0., 100., 200.]] b = [0.02, 1., 4., 800.] result = [0.0] * n gj_solve(np.ravel(mat), b, n, result) mat = np.matrix(mat) new_b = mat * np.transpose(np.matrix(result)) new_b = np.ravel(np.array(new_b)) assert np.allclose(new_b, np.array(b))
def loop(self, d_idx, d_m_mat, d_dw_gamma, d_cwij, DWIJ, HIJ): i, j, n, nt = declare('int', 4) n = self.dim nt = n + 1 temp = declare('matrix(12)') # The augmented matrix res = declare('matrix(3)') dwij = declare('matrix(3)') eps = 1.0e-04 * HIJ for i in range(n): dwij[i] = (DWIJ[i] - d_dw_gamma[3 * d_idx + i]) / d_cwij[d_idx] for j in range(n): temp[nt * i + j] = d_m_mat[9 * d_idx + 3 * i + j] temp[nt * i + n] = dwij[i] gj_solve(temp, n, 1, res) res_mag = 0.0 dwij_mag = 0.0 for i in range(n): res_mag += abs(res[i]) dwij_mag += abs(dwij[i]) change = abs(res_mag - dwij_mag) / (dwij_mag + eps) if change < self.tol: for i in range(n): DWIJ[i] = res[i]