Advertisement
myname0

SweepMethod

Nov 15th, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6.  
  7.  
  8. namespace Gauss
  9. {
  10.     class Program
  11.     {
  12.        static List<List<double>> GetMatrix()
  13.         {
  14.             StreamReader fin = new StreamReader("input.txt");
  15.             int n = int.Parse(fin.ReadLine());
  16.             int m = int.Parse(fin.ReadLine());
  17.  
  18.             List<List<double>> matrix = new List<List<double>>();
  19.  
  20.             for (int i = 0; i < n; i++)
  21.             {
  22.                 List<double> tmp = new List<double>();
  23.                 string[] line = fin.ReadLine().Split(' ');
  24.                 for (int j = 0; j < m; j++)
  25.                     tmp.Add(double.Parse(line[j]));
  26.                 matrix.Add(tmp);
  27.             }
  28.  
  29.             fin.Close();
  30.  
  31.             return matrix;
  32.         }
  33.        
  34.         static void SweepMethod()
  35.         {
  36.             List<List<double>> matrix = GetMatrix();
  37.             List<double> p = new List<double>();
  38.             List<double> Q = new List<double>();
  39.             RightSweep(matrix, p, Q);
  40.             BackSweep(p, Q);
  41.         }
  42.  
  43.         static void RightSweep(List<List<double>> matrix, List<double> p, List<double> Q)
  44.         {
  45.             p.Add(matrix[0][1] / (-matrix[0][0]));
  46.             Q.Add(-(matrix[0][matrix.Count] / (-matrix[0][0])));
  47.  
  48.             for (int i = 1, j = 0; i < matrix.Count - 1; i++, j++)
  49.             {
  50.                 p.Add(matrix[i][j + 2] / (-matrix[i][j + 1] - (matrix[i][j] * p[i - 1])));
  51.                 Q.Add((matrix[i][j] * Q[i - 1] - matrix[i][matrix.Count]) / (-matrix[i][j + 1] - (matrix[i][j] * p[i - 1])));
  52.             }
  53.  
  54.             p.Add(0);
  55.             Q.Add((matrix[matrix.Count - 1][matrix.Count - 2] * Q[Q.Count - 1] - matrix[matrix.Count - 1][matrix.Count])
  56.                 / ((-matrix[matrix.Count - 1][matrix.Count - 1]) - (matrix[matrix.Count - 1][matrix.Count - 2] * p[p.Count - 1])));
  57.  
  58.         }
  59.  
  60.         static void BackSweep(List<double> p, List<double> Q)
  61.         {
  62.             List<double> x = new List<double>();
  63.             x.Add(Q[Q.Count - 1]);
  64.             for (int i = Q.Count - 2; i >= 0; i--)
  65.             {
  66.                 x.Add(p[i] * x[x.Count - 1] + Q[i]);
  67.             }
  68.  
  69.             x.Reverse();
  70.             foreach (double item in x)
  71.                 Console.Write("{0} ", item);
  72.             Console.WriteLine();
  73.         }
  74.  
  75.         static void Main(string[] args)
  76.         {
  77.             SweepMethod();
  78.         }
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement