Advertisement
Guest User

Untitled

a guest
Oct 12th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.84 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Mat22//the class for 2 by 2 matrices
  5. {
  6.  
  7. public:
  8.  
  9. Mat22(){// default constructor
  10. input(0,0,0,0);
  11. };
  12.  
  13. Mat22(int ma,int mb, int mc, int md):// set the matrix as the input
  14. ma(ma),
  15. mb(mb),
  16. mc(mc),
  17. md(md)
  18. {};
  19.  
  20. Mat22(const Mat22 &b):// copy constructor
  21. ma(b.ma),
  22. mb(b.mb),
  23. mc(b.mc),
  24. md(b.md)
  25. {};
  26.  
  27. Mat22 operator=(const Mat22 &b){// assignment operator
  28. ma = b.ma;
  29. mb = b.mb;
  30. mc = b.mc;
  31. md = b.md;
  32. return *this;
  33. };
  34.  
  35. Mat22 operator+(const Mat22 &b){// add two matrices
  36. Mat22 temp;
  37. temp.ma = ma + b.ma;
  38. temp.mb = mb + b.mb;
  39. temp.mc = mc + b.mc;
  40. temp.md = md + b.md;
  41. return temp;
  42. };
  43.  
  44. Mat22 operator-(const Mat22 &b){//subtract two matrices
  45. Mat22 temp;
  46. temp.ma = ma - b.ma;
  47. temp.mb = mb - b.mb;
  48. temp.mc = mc - b.mc;
  49. temp.md = md - b.md;
  50. return temp;
  51. };
  52.  
  53. Mat22 operator*(const Mat22 &b){//multiply two matrices
  54. Mat22 temp = *this; //creating temporary Mat22 obj and assigning value of LHS to it
  55. Mat22 temp2; //creating second temporary Mat22 obj to avoid lvalue and rvalue issues
  56. temp2.ma = (temp.ma * b.ma) + (temp.mb * b.mc);
  57. temp2.mb = (temp.ma * b.mb) + (temp.mb * b.md);
  58. temp2.mc = (temp.mc * b.ma) + (temp.md * b.mc);
  59. temp2.md = (temp.mc * b.mb) + (temp.md * b.md);
  60. return temp2;
  61. };
  62.  
  63. Mat22 FastExp(Mat22 &b, int n){// compute matrix to the power of n
  64. Mat22 temp(1,0,0,1); //creating temp matrix with value of one (in matrix form)
  65.  
  66. if(n==0){ //base case, returns "one" (in matrix)
  67. return temp;
  68. }
  69.  
  70. else if(n%2==0){ //even n conditional, recursive call
  71. temp = FastExp(b, n/2);
  72. temp = temp*temp; //squaring temp prior to return
  73. return temp;
  74. }
  75.  
  76. else if(n%2==1){ //odd n conditional, also recursive
  77. temp = FastExp(b, (n-1)/2);
  78. temp = temp*temp;
  79. return temp*b; //multiplying by b to compensate for the (n-1)/2 recursive call
  80. }
  81. };
  82.  
  83. void display(ostream &out) const{// print the matrix, whatever way you like as long as it makes sense
  84. out << "Values: " << endl;
  85. out << "a: " << ma << endl;
  86. out << "b: " << mb << endl;
  87. out << "c: " << mc << endl;
  88. out << "d: " << md << endl;
  89. };
  90.  
  91. int display(int n){ //overload display to return specified private int from matrix obj
  92. if(n==0){ //conditionals for each int
  93. return ma;
  94. }
  95. else if(n==1){
  96. return mb;
  97. }
  98. else if(n==2){
  99. return mc;
  100. }
  101. else if(n==3){
  102. return md;
  103. }
  104. };
  105.  
  106. void input(istream &in){//function to ask for input for 2x2 matrices variables
  107. cout << "Please enter values one a time for your matrix, starting with a:" << endl;
  108. in >> ma >> mb >> mc >> md;
  109. };
  110.  
  111. Mat22 input(int a, int b, int c, int d){//overloaded input function for individual variable explicit input
  112. ma = a, mb = b, mc = c, md = d;
  113. return *this;
  114. };
  115.  
  116. private:
  117.  
  118. int ma,mb,mc,md;// store matrix [ma,mb]
  119. // [mc,md]
  120. };
  121.  
  122.  
  123. ostream& operator<<(ostream &out, const Mat22 &b){ //chainable << operator
  124. b.display(out); //uses display fx, passing first param of << op to display
  125. return out;
  126. };
  127.  
  128. istream& operator>>(istream &in, Mat22 &b){ //chainable >> operator
  129. b.input(in); //input fx is passed first param of >> op
  130. return in;
  131. };
  132.  
  133. int FastFib(int n){ //retrieve the nth term of the fibonacci sequence
  134. Mat22 FibMat(1,1,1,0); //using one of default constructors to build an appropriate matrix for sequence
  135. FibMat = FibMat.FastExp(FibMat, n);
  136. return FibMat.display(1); //using overloaded display function to access private int
  137. };
  138.  
  139.  
  140. int main()
  141. {
  142. //some test matrices
  143. Mat22 T1(1,2,3,4);
  144. Mat22 T2(4,3,2,1);
  145. Mat22 T3(5,5,5,5);
  146. Mat22 T4(1234,5678,9101,1121);
  147.  
  148. //retrieving n value from user for testing
  149. int n;
  150. cout << "Please enter your value for n (used in both FastExp and FastFib): ";
  151. cin >> n;
  152.  
  153. //testing FastExp
  154. cout << "***FASTEXP TEST***" << endl << "Starting Matrix " << T1;
  155. T1 = T1.FastExp(T1, n);
  156. cout << "To the power of " << n << ":" << endl << "Final " << T1 << endl;
  157.  
  158. //testing FastFib
  159. cout << endl << "***FASTFIB TEST***" << endl;
  160. int g;
  161. g = FastFib(n);
  162. cout << "Term " << n << " of the Fibonacci sequence is : " << g << endl;
  163.  
  164. //testing overloads
  165. cout << endl << "***OVERLOAD TEST***" << endl;
  166. //testing assignment
  167. T4 = T3;
  168. cout << T4;
  169. //testing subtraction and modification of only LHS
  170. T3 = T2-T3;
  171.  
  172. cout << T2;
  173. //testing chained >> and <<
  174. cin >> T2 >> T3;
  175. cout << T2 << T3;
  176.  
  177. return 0;
  178. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement