Matrix.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. namespace Matrix
  8. {
  9. class MyMatrix : Object
  10. {
  11. public double[,] data; //
  12. public int Rows
  13. {
  14. get { return data.GetLength(0); }
  15. }
  16. public int Columns
  17. {
  18. get { return data.GetLength(1); }
  19. }
  20. public MyMatrix(double[,] data)
  21. {
  22. this.data = data;
  23. }
  24. public static MyMatrix operator +(MyMatrix matrix1, MyMatrix matrix2)
  25. {
  26. if (matrix1.Rows == matrix2.Rows && matrix1.Columns == matrix2.Columns)
  27. {
  28. double[,] array = new double[matrix1.Rows, matrix1.Columns];
  29. for (int i = 0; i < matrix1.Rows; i++)
  30. {
  31. for (int j = 0; j < matrix1.Columns; j++)
  32. {
  33. array[i, j] = matrix1.data[i, j] + matrix2.data[i, j];
  34. }
  35. }
  36. return new MyMatrix(array);
  37. }
  38. else
  39. {
  40. throw new Exception("Размер матриц должен совпадать");
  41. }
  42. }
  43. public static MyMatrix operator -(MyMatrix matrix1, MyMatrix matrix2)
  44. {
  45. if (matrix1.Rows == matrix2.Rows && matrix1.Columns == matrix2.Columns)
  46. {
  47. double[,] array = new double[matrix1.Rows, matrix1.Columns];
  48. for (int i = 0; i < matrix1.Rows; i++)
  49. {
  50. for (int j = 0; j < matrix1.Columns; j++)
  51. {
  52. array[i, j] = matrix1.data[i, j] - matrix2.data[i, j];
  53. }
  54. }
  55. return new MyMatrix(array);
  56. }
  57. else
  58. {
  59. throw new Exception("Размер матриц должен совпадать");
  60. }
  61. }
  62. public static MyMatrix operator *(MyMatrix matrixA, MyMatrix matrixB)
  63. {
  64. if (matrixA.Rows != matrixB.Columns)
  65. {
  66. throw new Exception("Умножение матриц A и B возможно только в том случае, когда число столбцов матрицы A совпадает с числом строк в матрице B");
  67. }
  68. int K = matrixA.Columns;
  69. int M = matrixA.Rows;
  70. int N = matrixB.Columns;
  71. double[,] array = new double[M, N];
  72. for (int i = 0; i < M; i++)
  73. {
  74. for (int j = 0; j < N; j++)
  75. {
  76. double num = 0;
  77. for (int k = 0; k < K; k++)
  78. {
  79. num += matrixA.data[i, k] * matrixB.data[k, j];
  80. }
  81. array[i, j] = num;
  82. }
  83. }
  84. return new MyMatrix(array);
  85. }
  86. public static MyMatrix operator *(MyMatrix matrix1, double num)
  87. {
  88. double[,] array = new double[matrix1.Rows, matrix1.Columns];
  89. for (int i = 0; i < matrix1.Rows; i++)
  90. {
  91. for (int j = 0; j < matrix1.Columns; j++)
  92. {
  93. array[i, j] = matrix1.data[i, j] * num;
  94. }
  95. }
  96. return new MyMatrix(array);
  97. }
  98. public static MyMatrix operator *(double num, MyMatrix matrix1)
  99. {
  100. return matrix1 * num;
  101. }
  102. public static MyMatrix Transpose(MyMatrix matrix)
  103. {
  104. double[,] array = new double[matrix.Columns, matrix.Rows];
  105. for (int i = 0; i < matrix.Rows; i++)
  106. {
  107. for (int j = 0; j < matrix.Columns; j++)
  108. {
  109. array[j, i] = matrix.data[i, j];
  110. }
  111. }
  112. return new MyMatrix(array);
  113. }
  114. public static MyMatrix RowReplace(MyMatrix matrix, int frstIndex, int scndIndex)
  115. {
  116. double[,] array = matrix.data;
  117. double[] boof = new double[matrix.Columns];
  118. for (int i = 0; i < matrix.Columns; i++)
  119. {
  120. boof[i] = matrix.data[frstIndex, i];
  121. }
  122. for (int i = 0; i < matrix.Rows; i++)
  123. {
  124. for (int j = 0; j < matrix.Columns; j++)
  125. {
  126. if (i != frstIndex)
  127. {
  128. array[i, j] = matrix.data[i, j];
  129. }
  130. else
  131. {
  132. array[i, j] = matrix.data[scndIndex, j];
  133. }
  134. }
  135. }
  136. for (int i = 0; i < matrix.Columns; i++)
  137. {
  138. array[scndIndex, i] = boof[i];
  139. }
  140. return new MyMatrix(array);
  141. }
  142. public static MyMatrix RowReplaceByVector(MyMatrix matrix, int[] vector)
  143. {
  144. int[] nowVector = new int[vector.Length]; // 0 1 2 3 4 - 4 1 2 3 0 - 4 1 0 3 2 - 4 1 0 2 3
  145. for (int i = 0; i < nowVector.Length; i++) // 4 1 0 2 3 - 4 1 0 2 3 - 4 1 0 2 3 - 4 1 0 2 3
  146. {
  147. nowVector[i] = i;
  148. }
  149. MyMatrix result = null;
  150. for (int i = 0; i < vector.Length; i++)
  151. {
  152. if (nowVector[i] != vector[i])
  153. {
  154. int index = 0;
  155. for (int j = 0; j < vector.Length; j++)
  156. {
  157. if (nowVector[j] == vector[i])
  158. {
  159. index = j;
  160. }
  161. }
  162. result = MyMatrix.RowReplace(matrix, i, index);
  163. int boof = nowVector[i];
  164. nowVector[i] = nowVector[index];
  165. nowVector[index] = boof;
  166. }
  167. }
  168. return result;
  169. }
  170. public static MyMatrix Inverse(MyMatrix matrix)
  171. {
  172. if (matrix.IsMayInverse())
  173. {
  174. throw new Exception("Матрица не имеет обратной формы");
  175. }
  176. }
  177. public bool IsMayInverse()
  178. {
  179. if (Rows != Columns)
  180. {
  181. return false;
  182. }
  183. return true;
  184. }
  185. public override string ToString()
  186. {
  187. StringBuilder sb = new StringBuilder("", Rows * Columns * 2);
  188. for (int i = 0; i < Rows; i++)
  189. {
  190. for (int j = 0; j < Columns; j++)
  191. {
  192. if (j + 1 != Columns)
  193. {
  194. sb.Append($"{data[i, j]}, ");
  195. }
  196. else
  197. {
  198. sb.Append($"{data[i, j]}");
  199. }
  200. }
  201. sb.Append("\n");
  202. }
  203. return sb.ToString();
  204. }
  205. }
  206. }