static void RelaxationMethodCheckConvergence_SpecialMatrix(SpecialMatrix M, double w) { double left_upper = 1 - w; double left_lower = ((-w * M.B) / M.A) * (1 - w); double right_upper = (1 / M.A) * (-w * M.B); double right_lower = Math.Pow((w * M.B) / M.A, 2) + 1 - w; double max_column_sum = Math.Max ( Math.Abs(Math.Abs(left_upper) + Math.Abs(left_lower)), Math.Abs(Math.Abs(right_upper) + Math.Abs(right_lower)) ); double max_row_sum = Math.Max ( Math.Abs(Math.Abs(left_upper) + Math.Abs(right_upper)), Math.Abs(Math.Abs(left_lower) + Math.Abs(right_lower)) ); if (max_row_sum >= 1 && max_column_sum >= 1) { throw new ArgumentException("Метод не сходится"); } }
static Matrix <double> ToFullMatrix(SpecialMatrix M) { var M_Full = MatrixBuilder.Dense(M.N, M.N); for (int i = 0; i < M.N; i++) { M_Full[i, i] = M.A; M_Full[i, M.N - i - 1] = M.B; } return(M_Full); }
static Vector <double> RelaxationMethod_SpecialMatrix(SpecialMatrix A, Vector <double> b, Vector <double> x_0, double w, int K) { RelaxationMethodCheckConvergence_SpecialMatrix(A, w); Vector <double> x = x_0.Clone(); for (int k = 0; k < K; k++) { x = RelaxationMethodIteration_SpecialMatrix(A, b, x, w); } return(x); }
static double SpecialMatrix_L2Norm(SpecialMatrix M, Vector <double> x, Vector <double> b) { int n = b.Count(); double A = M.A; double B = M.B; double sum = 0; for (int i = 0; i < n; i++) { sum += Math.Pow(A * x[i] + B * x[n - i - 1] - b[i], 2); } return(Math.Sqrt(sum)); }
static Vector <double> RelaxationMethodIteration_SpecialMatrix(SpecialMatrix M, Vector <double> b, Vector <double> x_k, double w) { int n = b.Count(); double A = M.A; double B = M.B; double w_inv = 1 - w; double w_divided = w / A; for (int i = 0; i < n; i++) { x_k[i] = w_inv * x_k[i] + w_divided * (b[i] - x_k[n - i - 1] * B); } return(x_k); }
static Vector <double> RelaxationMethod_SpecialMatrix(SpecialMatrix A, Vector <double> b, Vector <double> x_0, double w, double e, out int iterations, out List <Tuple <int, double> > residuals) { RelaxationMethodCheckConvergence_SpecialMatrix(A, w); Vector <double> x = x_0.Clone(); iterations = 0; residuals = new List <Tuple <int, double> >(); while (e < SpecialMatrix_L2Norm(A, x, b)) { residuals.Add(new Tuple <int, double>(iterations, SpecialMatrix_L2Norm(A, x, b))); x = RelaxationMethodIteration_SpecialMatrix(A, b, x, w); iterations++; } return(x); }