Advertisement
xxxdanilion

Untitled

Dec 25th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.62 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3.  
  4. namespace ConsoleApplication3
  5. {
  6. public class Matrix
  7. {
  8. public float[,] matrix = null;
  9.  
  10. public int CountColumn { get; private set; }
  11. public int CountRow { get; private set; }
  12.  
  13. public Matrix(int x = 1, int y = 1, bool autoGeneration = true, int startGen = 1)
  14. {
  15. if (autoGeneration) generator(x, y, startGen);
  16. else
  17. matrix = new float[x, y];
  18.  
  19. CountColumn = y;
  20. CountRow = x;
  21. }
  22.  
  23. public float this[int x, int y]
  24. {
  25. get { return matrix[x, y]; }
  26. set { matrix[x, y] = value; }
  27. }
  28.  
  29. public static Matrix operator *(Matrix x1, Matrix x2)
  30. {
  31. if (x1.CountColumn != x2.CountRow) throw new ArgumentException("Число столбцов матрицы А не равно числу строк матрицы В.");
  32. Matrix ret = new Matrix(x1.CountRow, x2.CountColumn, false);
  33.  
  34. for (int i = 0; i < x1.CountRow; i++)
  35. for (int j = 0; j < x2.CountColumn; j++)
  36. for (int k = 0; k < x2.CountRow; k++)
  37. ret[i, j] += x1[i, k] * x2[k, j];
  38.  
  39. return ret;
  40. }
  41.  
  42. //возведение в степень
  43. public static Matrix operator ^ (Matrix x1, int pow)
  44. {
  45. Matrix matr = new Matrix(x1.CountRow, x1.CountColumn);
  46. matr = x1;
  47. for (int z = 1; z < pow; z++)
  48. {
  49. Matrix bufer = new Matrix(x1.CountRow, x1.CountColumn);
  50. for (int i = 0; i < x1.CountRow; i++)
  51. {
  52. for (int j = 0; j < x1.CountRow; j++)
  53. {
  54. float sum = 0;
  55. for (int r = 0; r < x1.CountRow; r++)
  56. sum += matr[i, r] * x1[r, j];
  57. bufer[i, j] = sum;
  58. }
  59. }
  60. matr = bufer;
  61. }
  62. return matr;
  63. }
  64.  
  65. public static Matrix operator *(Matrix x1, int constNum)
  66. {
  67. Matrix ret = new Matrix(x1.CountRow, x1.CountColumn, false);
  68. for (int i = 0; i < x1.CountColumn * x1.CountRow; i++)
  69. {
  70. int X = i / x1.CountColumn;
  71. int Y = i - x1.CountColumn * X;
  72. ret[X, Y] = x1[X, Y] * constNum;
  73. }
  74.  
  75. return ret;
  76. }
  77.  
  78. public static Matrix operator /(Matrix x1, Matrix x2)
  79. {
  80. if (x1.CountColumn != x2.CountRow) throw new ArgumentException("Число столбцов матрицы А не равно числу строк матрицы В.");
  81. Matrix ret = new Matrix(x1.CountRow, x2.CountColumn, false);
  82.  
  83. ret = x1 * x2.Inverse();
  84.  
  85. return ret;
  86. }
  87.  
  88. public float[] GetRow(int row)
  89. {
  90. if (row >= CountRow) throw new IndexOutOfRangeException("Индекс строки не принадлежит массиву.");
  91. float[] ret = new float[CountColumn];
  92. for (int i = 0; i < CountColumn; i++)
  93. ret[i] = matrix[row, i];
  94.  
  95. return ret;
  96. }
  97.  
  98. public void SetRow(float[] values, int row)
  99. {
  100. if (row >= CountRow) throw new IndexOutOfRangeException("Индекс строки не принадлежит массиву.");
  101. for (int i = 0; i < (CountColumn > values.Length ? values.Length : CountColumn); i++)
  102. matrix[row, i] = values[i];
  103. }
  104.  
  105. public Matrix Inverse()
  106. {
  107. if (CountRow != CountColumn) throw new ArgumentException("Обратная матрица существует только для квадратных, невырожденных, матриц.");
  108. Matrix ret = new Matrix(CountRow, CountColumn, false);
  109. ret.matrix = (float[,])this.matrix.Clone();
  110. float determinant = ret.Determinant();
  111.  
  112. if (determinant == 0) return ret; //Если определитель == 0 - матрица вырожденная
  113.  
  114. for (int i = 0; i < CountRow; i++)
  115. {
  116. for (int t = 0; t < CountColumn; t++)
  117. {
  118. Matrix tmp = ret.Exclude(i, t);
  119. ret[t, i] = tmp.Determinant() / determinant;
  120. }
  121. }
  122. return ret;
  123. }
  124.  
  125.  
  126. public float Determinant()
  127. {
  128. if (CountColumn != CountRow) throw new ArgumentException("Вычисление определителя возможно только для квадратных матриц.");
  129. Matrix _matrix = new Matrix(CountRow, CountColumn, false);
  130. _matrix.matrix = (float[,])this.matrix.Clone();
  131.  
  132. float det = 1;
  133. int order = CountRow;
  134.  
  135. for (int i = 0; i < order - 1; i++)
  136. {
  137. float[] masterRow = _matrix.GetRow(i);
  138. det *= masterRow[i];
  139. if (det == 0) return 0;
  140. for (int t = i + 1; t < order; t++)
  141. {
  142. float[] slaveRow = _matrix.GetRow(t);
  143. float[] tmp = MulArrayConst(masterRow, slaveRow[i] / masterRow[i]);
  144. float[] source = _matrix.GetRow(t);
  145. _matrix.SetRow(SubArray(source, tmp), t);
  146. }
  147. }
  148. det *= _matrix[order - 1, order - 1];
  149. return det;
  150. }
  151.  
  152. public Matrix Exclude(int row, int column)
  153. {
  154. if (row > CountRow || column > CountColumn) throw new IndexOutOfRangeException("Строка или столбец не принадлежат матрице.");
  155. Matrix ret = new Matrix(CountRow - 1, CountColumn - 1, false);
  156. ret.matrix = (float[,])this.matrix.Clone();
  157. int offsetX = 0;
  158. for (int i = 0; i < CountRow; i++)
  159. {
  160. int offsetY = 0;
  161. if (i == row) { offsetX++; continue; }
  162. for (int t = 0; t < CountColumn; t++)
  163. {
  164. if (t == column) { offsetY++; continue; }
  165. ret[i - offsetX, t - offsetY] = this[i, t];
  166. }
  167. }
  168. return ret;
  169. }
  170.  
  171. public override string ToString()
  172. {
  173. StringBuilder ret = new StringBuilder();
  174. if (matrix == null) return ret.ToString();
  175.  
  176. for (int i = 0; i < CountRow; i++)
  177. {
  178. for (int t = 0; t < CountColumn; t++)
  179. {
  180. ret.Append(matrix[i, t]);
  181. ret.Append("\t");
  182. }
  183. ret.Append("\n");
  184. }
  185. return ret.ToString();
  186. }
  187.  
  188.  
  189. float[] SubArray(float[] A, float[] B)
  190. {
  191. float[] ret = (float[])A.Clone();
  192. for (int i = 0; i < (A.Length > B.Length ? A.Length : B.Length); i++)
  193. ret[i] -= B[i];
  194. return ret;
  195. }
  196.  
  197. float[] MulArrayConst(float[] array, float number)
  198. {
  199. float[] ret = (float[])array.Clone();
  200. for (int i = 0; i < ret.Length; i++)
  201. ret[i] *= number;
  202. return ret;
  203. }
  204.  
  205. void generator(int x, int y, int startGen)
  206. {
  207. matrix = new float[x, y];
  208. Random rnd = new Random(startGen);
  209.  
  210. for (int i = 0; i < x * y; i++)
  211. {
  212. int X = i / y;
  213. int Y = i - y * X;
  214. matrix[X, Y] = rnd.Next(-10, 10);
  215. }
  216. }
  217. }
  218. class Program
  219. {
  220. static void Main(string[] args)
  221. {
  222. Matrix m1 = new Matrix(3, 3, true, 22);
  223. Matrix m2 = new Matrix(3, 3, true, 28);
  224.  
  225. Console.WriteLine("{0}\n{1}", m1, m2);
  226. Console.WriteLine("Произведение\n",m1 * m2);
  227. Console.WriteLine("Деление\n", m1 / m2);
  228. Console.WriteLine("Умножение на 4\n", m1 * 4);
  229. Console.WriteLine("Возведение в 3 степень\n", m1 ^ 3);
  230. Console.WriteLine("Произведение с присвоением\n", m1 *= m2);
  231. Console.WriteLine("деление с присвоением\n", m1 /= m2);
  232.  
  233. Console.WriteLine("Детерминант\n", m1.Determinant());
  234. Console.WriteLine(m1.Inverse());
  235.  
  236.  
  237. Console.ReadLine();
  238. }
  239. }
  240.  
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement