Advertisement
Guest User

Gaus Method

a guest
Oct 14th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.36 KB | None | 0 0
  1.    
  2.         class GausMethod
  3.         {
  4.             public uint RowCount;
  5.             public uint ColumCount;
  6.             public double[][] Matrix { get; set; }
  7.             public double[] RightPart { get; set; }
  8.             public double[] Answer { get; set; }
  9.  
  10.             public GausMethod(uint Row, uint Colum)
  11.             {
  12.                 RightPart = new double[Row];
  13.                 Answer = new double[Row];
  14.                 Matrix = new double[Row][];
  15.                 for (int i = 0; i < Row; i++)
  16.                     Matrix[i] = new double[Colum];
  17.                 RowCount = Row;
  18.                 ColumCount = Colum;
  19.  
  20.          
  21.                 for (int i = 0; i < Row; i++)
  22.                 {
  23.                     Answer[i] = 0;
  24.                     RightPart[i] = 0;
  25.                     for (int j = 0; j < Colum; j++)
  26.                         Matrix[i][j] = 0;
  27.                 }
  28.             }
  29.  
  30.             private void SortRows(int SortIndex)
  31.             {
  32.  
  33.                 double MaxElement = Matrix[SortIndex][SortIndex];
  34.                 int MaxElementIndex = SortIndex;
  35.                 for (int i = SortIndex + 1; i < RowCount; i++)
  36.                 {
  37.                     if (Matrix[i][SortIndex] > MaxElement)
  38.                     {
  39.                         MaxElement = Matrix[i][SortIndex];
  40.                         MaxElementIndex = i;
  41.                     }
  42.                 }
  43.  
  44.              
  45.                 if (MaxElementIndex > SortIndex)
  46.                 {
  47.                     double Temp;
  48.  
  49.                     Temp = RightPart[MaxElementIndex];
  50.                     RightPart[MaxElementIndex] = RightPart[SortIndex];
  51.                     RightPart[SortIndex] = Temp;
  52.  
  53.                     for (int i = 0; i < ColumCount; i++)
  54.                     {
  55.                         Temp = Matrix[MaxElementIndex][i];
  56.                         Matrix[MaxElementIndex][i] = Matrix[SortIndex][i];
  57.                         Matrix[SortIndex][i] = Temp;
  58.                     }
  59.                 }
  60.             }
  61.  
  62.             public int SolveMatrix()
  63.             {
  64.                 if (RowCount != ColumCount)
  65.                     return 1;
  66.  
  67.                 for (int i = 0; i < RowCount - 1; i++)
  68.                 {
  69.                     SortRows(i);
  70.                     for (int j = i + 1; j < RowCount; j++)
  71.                     {
  72.                         if (Matrix[i][i] != 0)
  73.                         {
  74.                             double MultElement = Matrix[j][i] / Matrix[i][i];
  75.                             for (int k = i; k < ColumCount; k++)
  76.                                 Matrix[j][k] -= Matrix[i][k] * MultElement;
  77.                             RightPart[j] -= RightPart[i] * MultElement;
  78.                         }
  79.                        
  80.                     }
  81.                 }
  82.  
  83.                 for (int i = (int)(RowCount - 1); i >= 0; i--)
  84.                 {
  85.                     Answer[i] = RightPart[i];
  86.  
  87.                     for (int j = (int)(RowCount - 1); j > i; j--)
  88.                         Answer[i] -= Matrix[i][j] * Answer[j];
  89.  
  90.                     if (Matrix[i][i] == 0)
  91.                         if (RightPart[i] == 0)
  92.                             return 2;
  93.                         else
  94.                             return 1;
  95.  
  96.                     Answer[i] /= Matrix[i][i];
  97.  
  98.                 }
  99.                 return 0;
  100.             }
  101.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement