Advertisement
myname0

IterationMethod

Nov 22nd, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.12 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(List<double> b)
  13.         {
  14.             StreamReader fin = new StreamReader("input.txt");
  15.             int n = int.Parse(fin.ReadLine());
  16.  
  17.             List<List<double>> matrix = new List<List<double>>();
  18.  
  19.             for (int i = 0; i < n; i++)
  20.             {
  21.                 List<double> tmp = new List<double>();
  22.                 string[] line = fin.ReadLine().Split(' ');
  23.                 for(int j = 0; j < n; j++)
  24.                     tmp.Add(double.Parse(line[j]));
  25.                 matrix.Add(tmp);
  26.             }
  27.  
  28.             string[] vector = fin.ReadLine().Split(' ');
  29.             for (int j = 0; j < n; j++)
  30.                 b.Add(double.Parse(vector[j]));
  31.  
  32.             fin.Close();
  33.  
  34.             return matrix;
  35.         }
  36.  
  37.         static void IterationMethod()
  38.         {
  39.             List<double> vector = new List<double>();
  40.             List<List<double>> matrix = GetMatrix(vector);
  41.             List<double>bVector = new List<double>();
  42.             List<List<double>> x = new List<List<double>>();
  43.             List<double> steepVector = new List<double>();
  44.             List<List<double>> multiplyMatrix = new List<List<double>>();
  45.             double e = 0.000001;
  46.             multiplyMatrix = matrix;
  47.             for (int i = 0; i < matrix.Count; i++)
  48.             {
  49.                 bVector.Add(vector[i] / matrix[i][i]);
  50.                 for (int j = 0; j < matrix.Count; j++)
  51.                     if (i == j)     multiplyMatrix[i][j] = 0;
  52.                     else    multiplyMatrix[i][j] = -matrix[i][j] / matrix[i][i];
  53.             }
  54.            
  55.             for (int i = 0; i < vector.Count; i++)
  56.                 steepVector.Add(vector[i]);
  57.  
  58.             int count = 0;
  59.  
  60.             x.Add(steepVector);
  61.  
  62.             PrintVector(x[x.Count - 1], ++count);
  63.  
  64.             steepVector = multiplyMatrixWithVector(matrix, x[x.Count - 1]);
  65.             SumVectors(steepVector, vector);
  66.             x.Add(steepVector);
  67.  
  68.             PrintVector(x[x.Count - 1], ++count);
  69.  
  70.             while (vectorsNorm(x[x.Count - 2], x[x.Count - 1]) > e)
  71.             {
  72.                 steepVector = multiplyMatrixWithVector(matrix, x[x.Count - 1]);
  73.                 SumVectors(steepVector, vector);
  74.                 x.Add(steepVector);
  75.                 PrintVector(x[x.Count - 1], ++count);
  76.             }
  77.  
  78.             Console.Write("Finally result: ");
  79.             for(int i = 0; i < x[x.Count - 1].Count; i++)
  80.                 Console.Write("{0} ", x[x.Count - 1][i]);
  81.             Console.WriteLine();
  82.         }
  83.  
  84.         static void PrintVector(List<double> vector, int count)
  85.         {
  86.             Console.Write("Step #{0}: ", count);
  87.             for (int i = 0; i < vector.Count; i++)
  88.                 Console.Write("{0} ", vector[i]);
  89.             Console.WriteLine();
  90.         }
  91.  
  92.         static List<double> multiplyMatrixWithVector(List<List<double>> matrix, List<double> vector)
  93.         {
  94.             List<double> resultVector = new List<double>();
  95.             for (int i = 0; i < matrix.Count; i++)
  96.             {
  97.                 resultVector.Add(0);
  98.                 for (int j = 0; j < vector.Count; j++)
  99.                     resultVector[i] += matrix[i][j] * vector[j];
  100.             }
  101.  
  102.             return resultVector;
  103.         }
  104.  
  105.         static void SumVectors(List<double> firstVector, List<double> secondVector)
  106.         {
  107.             for (int i = 0; i < firstVector.Count; i++)
  108.                 firstVector[i] += secondVector[i];
  109.         }
  110.  
  111.         public static double vectorsNorm(List<double> firstVector, List<double> secondVector)
  112.         {
  113.             double maxNorm = -1;
  114.  
  115.             for (int i = 0; i < firstVector.Count; i++)
  116.                 if (Math.Abs(secondVector[i] - firstVector[i]) > maxNorm)
  117.                     maxNorm = Math.Abs(secondVector[i] - firstVector[i]);
  118.  
  119.             return maxNorm;
  120.         }
  121.         static void Main(string[] args)
  122.         {
  123.             IterationMethod();
  124.         }
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement