Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.78 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ConsoleApplication1
  7. {
  8. class Matrix
  9. {
  10. public double[,] matice;
  11.  
  12.  
  13. public Matrix(double[,] matice)
  14. {
  15. this.matice = matice;
  16. }
  17. private int RowSize
  18. {
  19.  
  20.  
  21. get { return matice.GetLength(0); }
  22.  
  23.  
  24. }
  25.  
  26. private int ColumnSize
  27. {
  28.  
  29. get { return matice.GetLength(1); }
  30. }
  31.  
  32.  
  33.  
  34. // operatory +-*
  35.  
  36. public static Matrix operator +(Matrix a, Matrix b)
  37. {
  38.  
  39. if (a.RowSize != b.RowSize || a.ColumnSize != b.ColumnSize)
  40. {
  41. throw new Exception("Nenašlo");
  42.  
  43. }
  44. double[,] result = new double[a.RowSize, a.ColumnSize];
  45. for (int i = 0; i < a.RowSize; i++)
  46. {
  47. for (int j = 0; j < a.ColumnSize; j++)
  48. {
  49. result[i, j] = a.matice[i, j] + b.matice[i, j];
  50.  
  51. }
  52.  
  53.  
  54. }
  55. return new Matrix(result);
  56. }
  57.  
  58. public static Matrix operator -(Matrix a, Matrix b)
  59. {
  60.  
  61. if (a.RowSize != b.RowSize || a.ColumnSize != b.ColumnSize)
  62. {
  63. throw new Exception("Nenašlo");
  64.  
  65. }
  66. double[,] result = new double[a.RowSize, a.ColumnSize];
  67. for (int i = 0; i < a.RowSize; i++)
  68. {
  69. for (int j = 0; j < a.ColumnSize; j++)
  70. {
  71. result[i, j] = a.matice[i, j] - b.matice[i, j];
  72.  
  73. }
  74.  
  75.  
  76. }
  77. return new Matrix(result);
  78. }
  79.  
  80. public static Matrix operator -(Matrix a)
  81. {
  82.  
  83.  
  84. double[,] result = new double[a.RowSize, a.ColumnSize];
  85. for (int i = 0; i < a.RowSize; i++)
  86. {
  87. for (int j = 0; j < a.ColumnSize; j++)
  88. {
  89. result[i, j] = -a.matice[i, j];
  90.  
  91. }
  92.  
  93.  
  94. }
  95. return new Matrix(result);
  96. }
  97.  
  98.  
  99.  
  100.  
  101. public static Matrix operator *(Matrix a, Matrix b)
  102. {
  103.  
  104. if (a.ColumnSize != b.RowSize)
  105. {
  106. throw new Exception("Nenašlo");
  107.  
  108. }
  109. double sum = 0;
  110. double[,] result = new double[a.RowSize, a.ColumnSize];
  111. for (int i = 0; i < a.RowSize; i++)
  112. {
  113. for (int j = 0; j < b.ColumnSize; j++)
  114. {
  115.  
  116. for (int sumI = 0; sumI<a.ColumnSize; sumI++) {
  117. sum += a.matice[i, sumI] * b.matice[sumI, j];
  118.  
  119.  
  120.  
  121.  
  122. }
  123. result[i,j] = sum;
  124. sum = 0;
  125.  
  126. }
  127.  
  128. }
  129. return new Matrix(result);
  130. }
  131. // logické operace
  132.  
  133.  
  134. public static bool operator ==(Matrix a, Matrix b) {
  135.  
  136. if (a.RowSize != b.RowSize || a.ColumnSize != b.ColumnSize)
  137. {
  138. return false;
  139. }
  140. for (int i = 0; i < a.RowSize; i++) {
  141.  
  142.  
  143. for (int j = 0; j < a.ColumnSize; j++) {
  144. if(a.matice[i,j] != b.matice[i,j]){
  145.  
  146. return false;
  147. }
  148. }
  149. }
  150. return true;
  151. }
  152. public static bool operator !=(Matrix a,Matrix b){
  153.  
  154. return!(a==b);
  155.  
  156.  
  157. }
  158. public override string ToString()
  159. { string result= "";
  160. for (int i = 0; i < RowSize; i++) {
  161.  
  162.  
  163. for (int j = 0; j <ColumnSize; j++) {
  164.  
  165. result += string.Format("{0,10:F2}",matice[i,j]);
  166.  
  167.  
  168.  
  169. }
  170. result += Environment.NewLine;}
  171. return result;
  172. }
  173.  
  174.  
  175.  
  176.  
  177.  
  178. public double Determinant()
  179. {
  180. if (ColumnSize != RowSize) {
  181. throw new Exception("NOT a square matrix");
  182.  
  183. }
  184. if (ColumnSize == 1)
  185. { return matice[0, 0]; }
  186. else if (ColumnSize == 2) {
  187. return matice[0, 0] * matice[1, 1] - matice[1, 0] * matice[0, 1];
  188.  
  189.  
  190. }
  191. else if (ColumnSize == 3)
  192. {
  193.  
  194. return matice[0, 0] * matice[1, 1] * matice[2, 2]
  195. + matice[0, 1] * matice[1, 2] * matice[2, 0]
  196. + matice[0, 2] * matice[1, 0] * matice[2, 1]
  197. - matice[0, 2] * matice[1, 1] * matice[2, 0]
  198. - matice[0, 0] * matice[1, 2] * matice[2, 1]
  199. - matice[0, 1] * matice[1, 0] * matice[2, 2];
  200.  
  201.  
  202. }
  203. else { throw new Exception("Neumim vetsi ne 3"); }
  204.  
  205. }
  206.  
  207.  
  208. }
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement