// x - решение уравнения, полученное методом Гаусса private void GaussDiscrepancy() { for (int i = 0; i < size; ++i) { ComplexDouble actual_b_i = (ComplexDouble)0.0; for (int j = 0; j < size; ++j) { actual_b_i += initial_a_matrix[i, j] * x_vector[j]; } u_vector[i] = initial_b_vector[i] - actual_b_i; } }
// Обратный ход метода Гаусса private void GaussBackwardStroke(int[] index) { for (int i = size - 1; i >= 0; --i) { ComplexDouble x_i = b_vector[i]; for (int j = i + 1; j < size; ++j) { x_i -= x_vector[index[j]] * a_matrix[i, index[j]]; } x_vector[index[i]] = x_i; } }
// Прямой ход метода Гаусса private void GaussForwardStroke(int[] index) { for (int i = 0; i < size; ++i) { ComplexDouble r = FindR(i, index); for (int j = 0; j < size; ++j) { a_matrix[i, j] /= r; } b_vector[i] /= r; for (int k = i + 1; k < size; ++k) { ComplexDouble p = a_matrix[k, index[i]]; for (int j = i; j < size; ++j) { a_matrix[k, index[j]] -= a_matrix[i, index[j]] * p; } b_vector[k] -= b_vector[i] * p; a_matrix[k, index[i]] = (ComplexDouble)0.0; } } }
// поиск главного элемента в матрице private ComplexDouble FindR(int row, int[] index) { int max_index = row; ComplexDouble max = a_matrix[row, index[max_index]]; ComplexDouble max_abs = max; for (int cur_index = row + 1; cur_index < size; ++cur_index) { ComplexDouble cur = a_matrix[row, index[cur_index]]; ComplexDouble cur_abs = cur; if (cur_abs.Abs > max_abs.Abs) { max_index = cur_index; max = cur; max_abs = cur_abs; } } int temp = index[row]; index[row] = index[max_index]; index[max_index] = temp; return(max); }