Bob103

C#_Gauss

Nov 10th, 2017
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.57 KB | None | 0 0
  1. class Program
  2.     {
  3.         static void Gauss(float[,] M)
  4.         {
  5.  
  6.             Console.WriteLine("Start:");
  7.             for (int i = 0; i < M.GetLength(0); i++)
  8.             {
  9.                 for (int j = 0; j < M.GetLength(1); j++)
  10.                 {
  11.                     Console.Write("| " + Math.Round(M[i, j], 1) + " |");
  12.                     if(j==M.GetLength(1)-2)
  13.                     {
  14.                         Console.Write("=");
  15.                     }
  16.                 }
  17.                 Console.WriteLine();
  18.             }
  19.  
  20.             // size
  21.             int rowCount = M.GetLength(0);
  22.  
  23.             for (int col = 0; col + 1 < rowCount; col++)
  24.                 if (M[col, col] == 0)
  25.                 {
  26.                     // find non-zero coefficient
  27.                     int swapRow = col + 1;
  28.                     for (; swapRow < rowCount; swapRow++)
  29.                         if (M[swapRow, col] != 0) break;
  30.  
  31.                     if (M[swapRow, col] != 0)
  32.                     {
  33.                         // swap it with the above
  34.                         float[] tmp = new float[rowCount + 1];
  35.                         for (int i = 0; i < rowCount + 1; i++)
  36.                         {
  37.                             tmp[i] = M[swapRow, i];
  38.                             M[swapRow, i] = M[col, i];
  39.                             M[col, i] = tmp[i];
  40.                         }
  41.                     }
  42.                 }
  43.  
  44.             // elimination
  45.             for (int sourceRow = 0; sourceRow + 1 < rowCount; sourceRow++)
  46.             {
  47.                 for (int destRow = sourceRow + 1; destRow < rowCount; destRow++)
  48.                 {
  49.                     float df = M[sourceRow, sourceRow];
  50.                     float sf = M[destRow, sourceRow];
  51.                     for (int i = 0; i < rowCount + 1; i++)
  52.                         M[destRow, i] = M[destRow, i] * df - M[sourceRow, i] * sf;//умножаем n-ые элементы n строки на 1 эл-т 1 строки и наоборот,а затем вычитаем
  53.                 }
  54.             }
  55.  
  56.  
  57.             for (int row = rowCount - 1; row >= 0; row--)
  58.             {
  59.                 float f = M[row, row];
  60.  
  61.                 //divide in all rows on first number this row(сокращаем по возможности всю строку)
  62.                 for (int i = 0; i < rowCount + 1; i++)
  63.                     M[row, i] /= f;
  64.  
  65.                 // substiling the value(подставляем значения x1,x2,x3....)
  66.                 for (int destRow = 0; destRow < row; destRow++)
  67.                 {
  68.                     M[destRow, rowCount] -= M[destRow, row] * M[row, rowCount];
  69.                     M[destRow, row] = 0;
  70.                 }
  71.             }
  72.  
  73.             Console.WriteLine("Result:");
  74.             for(int i = 0; i < M.GetLength(0); i++)
  75.             {
  76.                 for(int j = 0; j < M.GetLength(1); j++)
  77.                 {
  78.                     Console.Write("| "+Math.Round(M[i, j],1) + " |");
  79.                     if (j == M.GetLength(1) - 2)
  80.                     {
  81.                         Console.Write("=");
  82.                     }
  83.                 }
  84.                 Console.WriteLine();
  85.             }
  86.             Console.WriteLine();
  87.  
  88.             for(int i = 0; i < M.GetLength(0); i++)
  89.             {
  90.                 Console.WriteLine("X[{0}]" + Math.Round(M[i,rowCount],1),i+1);
  91.             }
  92.            
  93.         }
  94.  
  95.         public static void Main()
  96.         {
  97.             float[,] ar = new float[,] { { 2, 4, 5, 5 }, { 4, 1, 3, 2 }, { 2, 1,3, 1 } };
  98.             Gauss(ar);
  99.         }
  100.     }
Advertisement
Add Comment
Please, Sign In to add comment