Guest User

Untitled

a guest
Jul 13th, 2020
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. public class Matrix
  2. {
  3. private float[] _Values;
  4. private int _Width;
  5. private int _Height;
  6.  
  7. public int Width => _Width;
  8. public int Height => _Height;
  9.  
  10. public float this[int x, int y]
  11. {
  12. get
  13. {
  14. return data[x + y * _Width];
  15. }
  16. set
  17. {
  18. data[x + y * _Width] = value;
  19. }
  20. }
  21. public Matrix operator+(Matrix left, Matrix right)
  22. {
  23.  
  24. if(left.width == right.Width && left.Height == right.Height)
  25. {
  26. Matrix result = new Matrix(left.Width, right.Height);
  27. var elementsCount = left.Width * left.Height;
  28. for(int i = 0; i < elementsCount; i++)
  29. {
  30. result._Values[i] = right._Values[i] + left._Values[i];
  31. }
  32. return result;
  33.  
  34. }
  35. else
  36. {
  37. return null;
  38. }
  39. }
  40. public Matrix operator*(Matrix left, Matrix right)
  41. {
  42.  
  43. if(left.Height == right.Width)
  44. {
  45. Matrix result = new Matrix(left.Width, right.Height);
  46. for(int x = 0; x < left.Width; x++)
  47. {
  48. for(int y = 0; y < right.Height; y++)
  49. {
  50. result[x, y] = 0;
  51. for(int i = 0; i < left.Height; i++)
  52. {
  53. result[x, y] += left[x, i] * right[i, y];
  54. }
  55. }
  56. }
  57. return result;
  58.  
  59. }
  60. else
  61. {
  62. return null;
  63. }
  64. }
  65.  
  66. public Matrix(int width, int height)
  67. {
  68.  
  69. _Width = width;
  70. _Height = height;
  71. _Values = new float[width * height];
  72. }
Add Comment
Please, Sign In to add comment