def jacobi_method(mat_a, epsilon=1e-9, b_verbose=False): n = len(mat_a) mat_a0 = matrix.alloc_mat(n, n) for i in range(n): for j in range(n): mat_a0[i][j] = mat_a[i][j] mat_x = matrix.get_identity_matrix(n) ############################# while True: abs_ars, ars, r, s = find_r_s(mat_a0, n) if abs_ars < epsilon: break if b_verbose: print("ars = %s" % ars) print("r, s = (%g, %g)" % (r, s)) arr = mat_a0[r][r] ass = mat_a0[s][s] theta_rad = calc_theta(ars, arr, ass) if b_verbose: print("theta = %s (deg)" % (theta_rad * 180 / math.pi)) cos = math.cos(theta_rad) sin = math.sin(theta_rad) for k in range(n): if k == r: pass elif k == s: pass else: akr = mat_a0[k][r] aks = mat_a0[k][s] mat_a0[r][k] = akr * cos + aks * sin mat_a0[s][k] = aks * cos - akr * sin mat_a0[k][r] = mat_a0[r][k] mat_a0[k][s] = mat_a0[s][k] xkr = mat_x[k][r] xks = mat_x[k][s] mat_x[k][r] = xkr * cos + xks * sin mat_x[k][s] = xks * cos - xkr * sin mat_a0[r][ r] = arr * cos * cos + 2.0 * ars * sin * cos + ass * sin * sin mat_a0[s][ s] = arr * sin * sin - 2.0 * ars * sin * cos + ass * cos * cos mat_a0[r][s] = mat_a0[s][r] = 0.0 if b_verbose: print("mat_a0") matrix.show_mat(mat_a0) print("mat_x") matrix.show_mat(mat_x) return mat_a0, mat_x
def gauss_elimination(mat_a, b): """ 1차 다원 연립 방정식 Ax = b에서 x를 구함 A : 계수행렬 b : 상수벡터 :param mat_a: :param b: :return: """ m_row, n_col = matrix.shape(mat_a) mat_a_vec_b = matrix.alloc_mat(m_row, n_col + 1) for i in range(m_row): for j in range(n_col): mat_a_vec_b[i][j] = mat_a[i][j] mat_a_vec_b[i][-1] = b[i] upper_triangle(mat_a_vec_b) x = back_substitution(mat_a_vec_b) del mat_a_vec_b[:] del mat_a_vec_b return x
def generate_mat_ai(mat_a): m_row, n_col = matrix.shape(mat_a) mat_ai = matrix.alloc_mat(m_row, n_col + m_row) for i_pivot in range(m_row): for j_col in range(n_col): mat_ai[i_pivot][j_col] = mat_a[i_pivot][j_col] mat_ai[i_pivot][n_col + i_pivot] = 1.0 return mat_ai
def initialize_jacobi_method(mat_a): remove_all_figure_files() n = len(mat_a) mat_a0 = matrix.alloc_mat(n, n) for i in range(n): for j in range(n): mat_a0[i][j] = mat_a[i][j] mat_x = matrix.get_identity_matrix(n) counter = 0 return mat_a0, mat_x, n, counter
def initialize_jacobi_method(mat_a:Matrix, b_plot:bool=False) -> Tuple[Matrix, Matrix, int, int]: if b_plot: matshow.remove_all_figure_files() n = len(mat_a) mat_a0 = matrix.alloc_mat(n, n) for i in range(n): for j in range(n): mat_a0[i][j] = mat_a[i][j] mat_x = matrix.get_identity_matrix(n) counter = 0 return mat_a0, mat_x, n, counter
def gauss_elimination(mat_a, b): m_row, n_col = matrix.shape(mat_a) mat_a_vec_b = matrix.alloc_mat(m_row, n_col + 1) for i in range(m_row): for j in range(n_col): mat_a_vec_b[i][j] = mat_a[i][j] mat_a_vec_b[i][-1] = b[i] upper_triangle(mat_a_vec_b) x = back_substitution(mat_a_vec_b) del mat_a_vec_b[:] del mat_a_vec_b return x
def gauss_jordan(mat_a): mat_ai = generate_mat_ai(mat_a) for i_pivot in range(len(mat_ai)): matrix.row_mul_scalar(mat_ai, i_pivot, 1.0 / mat_ai[i_pivot][i_pivot]) for j_row in range(len(mat_ai)): if i_pivot != j_row: matrix.row_mul_add(mat_ai, j_row, i_pivot, -mat_ai[j_row][i_pivot]) inv_mat = matrix.alloc_mat( len(mat_ai), len(mat_ai), ) for i in range(len(mat_ai)): for j in range(len(mat_ai)): inv_mat[i][j] = mat_ai[i][len(mat_ai) + j] del mat_ai[:] del mat_ai return inv_mat