Advertisement
Bunny83

GaussMatrixF.cs

Feb 22nd, 2020
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.22 KB | None | 0 0
  1. using System;
  2.  
  3. public class GaussMatrixF
  4. {
  5.     public class Row
  6.     {
  7.         public float[] cols;
  8.         public Row(int aCols)
  9.         {
  10.             cols = new float[aCols];
  11.         }
  12.         public void Multiply(float aVal)
  13.         {
  14.             for (int i = 0; i < cols.Length; i++)
  15.                 cols[i] *= aVal;
  16.         }
  17.         public void Add(Row aRow, float aVal)
  18.         {
  19.             for (int i = 0; i < cols.Length; i++)
  20.                 cols[i] = cols[i] + aRow.cols[i] * aVal;
  21.         }
  22.         public override string ToString()
  23.         {
  24.             var sb = new System.Text.StringBuilder();
  25.             sb.Append(cols[0]);
  26.             for (int i = 1; i < cols.Length; i++)
  27.                 sb.Append(",\t").Append(cols[i]);
  28.             return sb.ToString();
  29.         }
  30.     }
  31.  
  32.     public Row[] rows;
  33.  
  34.     public GaussMatrixF(int aCols, int aRows)
  35.     {
  36.         rows = new Row[aRows];
  37.         for (int i = 0; i < aRows; i++)
  38.             rows[i] = new Row(aCols);
  39.     }
  40.  
  41.     public bool CreateUpperTriangle(out float det)
  42.     {
  43.         det = 1;
  44.         for(int i = 0; i < rows.Length; i++)
  45.         {
  46.             var index = FindPivot(i);
  47.             if (index == -1)
  48.             {
  49.                 det = 0;
  50.                 return false;
  51.             }
  52.             if (index != i)
  53.             {
  54.                 Swap(i, index);
  55.                 det = -det;
  56.             }
  57.             var r = rows[i];
  58.             if (r.cols[i] != 1)
  59.             {
  60.                 det *= r.cols[i];
  61.                 r.Multiply(1 / r.cols[i]);
  62.                 r.cols[i] = 1;
  63.             }
  64.             for (int n = i + 1; n < rows.Length; n++)
  65.             {
  66.                 if (rows[n].cols[i] != 0)
  67.                 {
  68.                     rows[n].Add(r, -rows[n].cols[i]);
  69.                     rows[n].cols[i] = 0;
  70.                 }
  71.             }
  72.         }
  73.         return true;
  74.     }
  75.  
  76.     public bool Solve()
  77.     {
  78.         float det;
  79.         if (!CreateUpperTriangle(out det))
  80.             return false;
  81.         for (int i = 1; i < rows.Length; i++)
  82.         {
  83.             var r = rows[i];
  84.             for (int n = 0; n < i; n++)
  85.             {
  86.                 if (rows[n].cols[i] != 0)
  87.                 {
  88.                     rows[n].Add(r, -rows[n].cols[i]);
  89.                     rows[n].cols[i] = 0;
  90.                 }
  91.             }
  92.         }
  93.         return true;
  94.     }
  95.  
  96.  
  97.     void Swap(int a, int b)
  98.     {
  99.         var tmp = rows[a];
  100.         rows[a] = rows[b];
  101.         rows[b] = tmp;
  102.     }
  103.  
  104.     private int FindPivot(int aCol)
  105.     {
  106.         int index = -1;
  107.         float val = 0;
  108.         for(int i = aCol; i < rows.Length; i++)
  109.         {
  110.             var v = rows[i].cols[aCol];
  111.             if (v < 0)
  112.                 v = -v;
  113.             if (v > 1)
  114.                 v = 1 / v;
  115.                
  116.             if (v > val )
  117.             {
  118.                 val = v;
  119.                 index = i;
  120.             }
  121.         }
  122.         return index;
  123.     }
  124.     public override string ToString()
  125.     {
  126.         var sb = new System.Text.StringBuilder();
  127.         sb.Append(rows[0]);
  128.         for (int i = 1; i < rows.Length; i++)
  129.             sb.AppendLine().Append(rows[i]);
  130.         return sb.ToString();
  131.     }
  132.  
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement