Guest User

Untitled

a guest
Apr 24th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.47 KB | None | 0 0
  1. //217CR 1st Assignment
  2. //Brendan Smith
  3. //TMatrix Contructors and Functions
  4.  
  5. #include "TMatrix.h"
  6. #include <iostream>
  7.  
  8. using namespace std;
  9.  
  10. //Constructor
  11. TMatrix::TMatrix(void)
  12. {
  13. Matrix[0][0] = 0.0; Matrix[0][1] = 0.0; Matrix[0][2] = 0.0;
  14. Matrix[1][0] = 0.0; Matrix[1][1] = 0.0; Matrix[1][2] = 0.0;
  15. Matrix[2][0] = 0.0; Matrix[2][1] = 0.0; Matrix[2][2] = 0.0;
  16. }
  17.  
  18. TMatrix::TMatrix(double MX1A,double MX1B,double MX1C,double MX2A,double MX2B,double MX2C,double MX3A,double MX3B,double MX3C)
  19. {
  20. Matrix[0][0] = MX1A; Matrix[0][1] = MX1B; Matrix[0][2] = MX1C;
  21. Matrix[1][0] = MX2A; Matrix[1][1] = MX2B; Matrix[1][2] = MX2C;
  22. Matrix[2][0] = MX3A; Matrix[2][1] = MX3B; Matrix[2][2] = MX3C;
  23. }
  24.  
  25.  
  26.  
  27.  
  28. //Function that adds the TMatrix
  29. TMatrix TMatrix::AddMatrix(TMatrix Ma1, TMatrix Ma2, TMatrix Result)
  30. {
  31. int x, y;
  32. for (x = 0; x < 3; x++) {
  33. for (y = 0; y < 3; y++)
  34. Result.Matrix[x][y]=Ma1.Matrix[x][y] + Ma2.Matrix[x][y]; }
  35.  
  36. for (x = 0; x < 3; x++) {
  37. for (y = 0; y < 3; y++)
  38. cout<<" Addition: "<<Result.Matrix[x][y];}
  39. cout<<" \n";
  40. cout<<" \n";
  41.  
  42.  
  43. return Result;
  44. }
  45.  
  46. //Function that Subtracts the TMatrix
  47. TMatrix TMatrix::Subtract(TMatrix Ma1, TMatrix Ma2, TMatrix Result)
  48. {
  49. int x, y;
  50. for (x = 0; x < 3; x++) {
  51. for (y = 0; y < 3; y++)
  52. Result.Matrix[x][y]=Ma1.Matrix[x][y] - Ma2.Matrix[x][y]; }
  53.  
  54. for (x = 0; x < 3; x++) {
  55. for (y = 0; y < 3; y++)
  56. cout<<" Subtract: "<<Result.Matrix[x][y];}
  57. cout<<" \n";
  58. cout<<" \n";
  59.  
  60. return Result;
  61. }
  62.  
  63. //Function that Multipys the TMatrix
  64. TMatrix TMatrix::MultiTMatrix(TMatrix Ma1, TMatrix Ma2, TMatrix Result)
  65. {
  66. int x, y;
  67.  
  68. Result.Matrix[0][0] = (Ma1.Matrix[0][0] * Ma2.Matrix[0][0]) + (Ma1.Matrix[0][1] * Ma2.Matrix[1][0]) + (Ma1.Matrix[0][2] * Ma2.Matrix[2][0]);
  69. Result.Matrix[0][1] = (Ma1.Matrix[0][0] * Ma2.Matrix[0][1]) + (Ma1.Matrix[0][1] * Ma2.Matrix[1][1]) + (Ma1.Matrix[0][2] * Ma2.Matrix[2][1]);
  70. Result.Matrix[0][2] = (Ma1.Matrix[0][0] * Ma2.Matrix[0][2]) + (Ma1.Matrix[0][1] * Ma2.Matrix[1][2]) + (Ma1.Matrix[0][2] * Ma2.Matrix[2][2]);
  71. Result.Matrix[1][0] = (Ma1.Matrix[1][0] * Ma2.Matrix[0][0]) + (Ma1.Matrix[1][1] * Ma2.Matrix[1][0]) + (Ma1.Matrix[1][2] * Ma2.Matrix[2][0]);
  72. Result.Matrix[1][1] = (Ma1.Matrix[1][0] * Ma2.Matrix[0][1]) + (Ma1.Matrix[1][1] * Ma2.Matrix[1][1]) + (Ma1.Matrix[1][2] * Ma2.Matrix[2][1]);
  73. Result.Matrix[1][2] = (Ma1.Matrix[1][0] * Ma2.Matrix[0][2]) + (Ma1.Matrix[1][1] * Ma2.Matrix[1][2]) + (Ma1.Matrix[1][2] * Ma2.Matrix[2][2]);
  74. Result.Matrix[2][0] = (Ma1.Matrix[2][0] * Ma2.Matrix[0][0]) + (Ma1.Matrix[2][1] * Ma2.Matrix[1][0]) + (Ma1.Matrix[2][2] * Ma2.Matrix[2][0]);
  75. Result.Matrix[2][1] = (Ma1.Matrix[2][0] * Ma2.Matrix[0][1]) + (Ma1.Matrix[2][1] * Ma2.Matrix[1][1]) + (Ma1.Matrix[2][2] * Ma2.Matrix[2][1]);
  76. Result.Matrix[2][2] = (Ma1.Matrix[2][0] * Ma2.Matrix[0][2]) + (Ma1.Matrix[2][1] * Ma2.Matrix[1][2]) + (Ma1.Matrix[2][2] * Ma2.Matrix[2][2]);
  77.  
  78. for (x = 0; x < 3; x++) {
  79. for (y = 0; y < 3; y++)
  80. cout<<" Multiply: "<<Result.Matrix[x][y];}
  81. cout<<" \n";
  82. cout<<" \n";
  83.  
  84.  
  85. return Result;
  86.  
  87. }
  88.  
  89. //Function that Tranpose the TMatrix
  90. TMatrix TMatrix::Transpose(TMatrix Ma1, TMatrix Result)
  91. {
  92. int x, y;
  93. for (x = 0; x < 3; x++) {
  94. for (y = 0; y < 3; y++)
  95. Result.Matrix[y][x]=Ma1.Matrix[x][y]; }
  96.  
  97. for (x = 0; x < 3; x++) {
  98. for (y = 0; y < 3; y++)
  99. cout<<" Tranpose: "<<Result.Matrix[x][y];}
  100. cout<<" \n";
  101. cout<<" \n";
  102.  
  103.  
  104. return Result;
  105. }
  106.  
  107. //Function that Inverses the TMatrix
  108. TMatrix TMatrix::Inverse(TMatrix Ma1, TMatrix Result)
  109. {
  110. int x, y;
  111. double Determinant, amount;
  112. Determinant=Ma1.Matrix[0][0] * ((Ma1.Matrix[1][1] * Ma1.Matrix[2][2]) -(Ma1.Matrix[1][2] * Ma1.Matrix[2][1])) - Ma1.Matrix[0][1] * ((Ma1.Matrix[1][0] * Ma1.Matrix[2][2]) - (Ma1.Matrix[1][2] * Ma1.Matrix[2][1])) + Ma1.Matrix[0][2] * ((Ma1.Matrix[1][0] * Ma1.Matrix[2][1]) - (Ma1.Matrix[1][1] * Ma1.Matrix[2][0]));
  113. amount = 1/Determinant;
  114. Result.Matrix[0][0] = amount * (Ma1.Matrix[1][1] * Ma1.Matrix[2][2]) - (Ma1.Matrix[1][2] * Ma1.Matrix[2][1]);
  115. Result.Matrix[0][1] = amount * (Ma1.Matrix[0][2] * Ma1.Matrix[2][1]) - (Ma1.Matrix[0][1] * Ma1.Matrix[2][2]);
  116. Result.Matrix[0][2] = amount * (Ma1.Matrix[0][1] * Ma1.Matrix[1][2]) - (Ma1.Matrix[0][2] * Ma1.Matrix[1][1]);
  117. Result.Matrix[1][0] = amount * (Ma1.Matrix[1][2] * Ma1.Matrix[2][0]) - (Ma1.Matrix[1][0] * Ma1.Matrix[2][2]);
  118. Result.Matrix[1][1] = amount * (Ma1.Matrix[0][0] * Ma1.Matrix[2][2]) - (Ma1.Matrix[0][2] * Ma1.Matrix[2][0]);
  119. Result.Matrix[1][2] = amount * (Ma1.Matrix[0][2] * Ma1.Matrix[1][0]) - (Ma1.Matrix[0][0] * Ma1.Matrix[1][2]);
  120. Result.Matrix[2][0] = amount * (Ma1.Matrix[1][0] * Ma1.Matrix[2][1]) - (Ma1.Matrix[1][1] * Ma1.Matrix[2][0]);
  121. Result.Matrix[2][1] = amount * (Ma1.Matrix[0][1] * Ma1.Matrix[2][0]) - (Ma1.Matrix[0][0] * Ma1.Matrix[2][1]);
  122. Result.Matrix[2][2] = amount * (Ma1.Matrix[0][0] * Ma1.Matrix[1][1]) - (Ma1.Matrix[0][1] * Ma1.Matrix[1][0]);
  123.  
  124. for (x = 0; x < 3; x++) {
  125. for (y = 0; y < 3; y++)
  126. cout<<" Inverse: "<<Result.Matrix[x][y];}
  127. cout<<" \n";
  128. cout<<" \n";
  129.  
  130.  
  131. return Result;
  132. }
  133.  
  134. //Determinant Function
  135. double TMatrix::Determinant(TMatrix Ma1, double Result)
  136. {
  137. Result=Ma1.Matrix[0][0] * ((Ma1.Matrix[1][1] * Ma1.Matrix[2][2]) -(Ma1.Matrix[1][2] * Ma1.Matrix[2][1])) - Ma1.Matrix[0][1] * ((Ma1.Matrix[1][0] * Ma1.Matrix[2][2]) - (Ma1.Matrix[1][2] * Ma1.Matrix[2][1])) + Ma1.Matrix[0][2] * ((Ma1.Matrix[1][0] * Ma1.Matrix[2][1]) - (Ma1.Matrix[1][1] * Ma1.Matrix[2][0]));
  138.  
  139. cout<<" Determinant: " <<Result;
  140. cout<<" \n";
  141. cout<<" \n";
  142. return Result;
  143. }
Add Comment
Please, Sign In to add comment