Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- namespace Gauss
- {
- class Program
- {
- static List<List<double>> GetMatrix()
- {
- StreamReader fin = new StreamReader("input.txt");
- int n = int.Parse(fin.ReadLine());
- int m = int.Parse(fin.ReadLine());
- List<List<double>> matrix = new List<List<double>>();
- for (int i = 0; i < n; i++)
- {
- List<double> tmp = new List<double>();
- string[] line = fin.ReadLine().Split(' ');
- for (int j = 0; j < m; j++)
- tmp.Add(double.Parse(line[j]));
- matrix.Add(tmp);
- }
- fin.Close();
- return matrix;
- }
- static void SweepMethod()
- {
- List<List<double>> matrix = GetMatrix();
- List<double> p = new List<double>();
- List<double> Q = new List<double>();
- RightSweep(matrix, p, Q);
- BackSweep(p, Q);
- }
- static void RightSweep(List<List<double>> matrix, List<double> p, List<double> Q)
- {
- p.Add(matrix[0][1] / (-matrix[0][0]));
- Q.Add(-(matrix[0][matrix.Count] / (-matrix[0][0])));
- for (int i = 1, j = 0; i < matrix.Count - 1; i++, j++)
- {
- p.Add(matrix[i][j + 2] / (-matrix[i][j + 1] - (matrix[i][j] * p[i - 1])));
- Q.Add((matrix[i][j] * Q[i - 1] - matrix[i][matrix.Count]) / (-matrix[i][j + 1] - (matrix[i][j] * p[i - 1])));
- }
- p.Add(0);
- Q.Add((matrix[matrix.Count - 1][matrix.Count - 2] * Q[Q.Count - 1] - matrix[matrix.Count - 1][matrix.Count])
- / ((-matrix[matrix.Count - 1][matrix.Count - 1]) - (matrix[matrix.Count - 1][matrix.Count - 2] * p[p.Count - 1])));
- }
- static void BackSweep(List<double> p, List<double> Q)
- {
- List<double> x = new List<double>();
- x.Add(Q[Q.Count - 1]);
- for (int i = Q.Count - 2; i >= 0; i--)
- {
- x.Add(p[i] * x[x.Count - 1] + Q[i]);
- }
- x.Reverse();
- foreach (double item in x)
- Console.Write("{0} ", item);
- Console.WriteLine();
- }
- static void Main(string[] args)
- {
- SweepMethod();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement