Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class Mat22//the class for 2 by 2 matrices
- {
- public:
- Mat22(){// default constructor
- input(0,0,0,0);
- };
- Mat22(int ma,int mb, int mc, int md):// set the matrix as the input
- ma(ma),
- mb(mb),
- mc(mc),
- md(md)
- {};
- Mat22(const Mat22 &b):// copy constructor
- ma(b.ma),
- mb(b.mb),
- mc(b.mc),
- md(b.md)
- {};
- Mat22 operator=(const Mat22 &b){// assignment operator
- ma = b.ma;
- mb = b.mb;
- mc = b.mc;
- md = b.md;
- return *this;
- };
- Mat22 operator+(const Mat22 &b){// add two matrices
- Mat22 temp;
- temp.ma = ma + b.ma;
- temp.mb = mb + b.mb;
- temp.mc = mc + b.mc;
- temp.md = md + b.md;
- return temp;
- };
- Mat22 operator-(const Mat22 &b){//subtract two matrices
- Mat22 temp;
- temp.ma = ma - b.ma;
- temp.mb = mb - b.mb;
- temp.mc = mc - b.mc;
- temp.md = md - b.md;
- return temp;
- };
- Mat22 operator*(const Mat22 &b){//multiply two matrices
- Mat22 temp = *this; //creating temporary Mat22 obj and assigning value of LHS to it
- Mat22 temp2; //creating second temporary Mat22 obj to avoid lvalue and rvalue issues
- temp2.ma = (temp.ma * b.ma) + (temp.mb * b.mc);
- temp2.mb = (temp.ma * b.mb) + (temp.mb * b.md);
- temp2.mc = (temp.mc * b.ma) + (temp.md * b.mc);
- temp2.md = (temp.mc * b.mb) + (temp.md * b.md);
- return temp2;
- };
- Mat22 FastExp(Mat22 &b, int n){// compute matrix to the power of n
- Mat22 temp(1,0,0,1); //creating temp matrix with value of one (in matrix form)
- if(n==0){ //base case, returns "one" (in matrix)
- return temp;
- }
- else if(n%2==0){ //even n conditional, recursive call
- temp = FastExp(b, n/2);
- temp = temp*temp; //squaring temp prior to return
- return temp;
- }
- else if(n%2==1){ //odd n conditional, also recursive
- temp = FastExp(b, (n-1)/2);
- temp = temp*temp;
- return temp*b; //multiplying by b to compensate for the (n-1)/2 recursive call
- }
- };
- void display(ostream &out) const{// print the matrix, whatever way you like as long as it makes sense
- out << "Values: " << endl;
- out << "a: " << ma << endl;
- out << "b: " << mb << endl;
- out << "c: " << mc << endl;
- out << "d: " << md << endl;
- };
- int display(int n){ //overload display to return specified private int from matrix obj
- if(n==0){ //conditionals for each int
- return ma;
- }
- else if(n==1){
- return mb;
- }
- else if(n==2){
- return mc;
- }
- else if(n==3){
- return md;
- }
- };
- void input(istream &in){//function to ask for input for 2x2 matrices variables
- cout << "Please enter values one a time for your matrix, starting with a:" << endl;
- in >> ma >> mb >> mc >> md;
- };
- Mat22 input(int a, int b, int c, int d){//overloaded input function for individual variable explicit input
- ma = a, mb = b, mc = c, md = d;
- return *this;
- };
- private:
- int ma,mb,mc,md;// store matrix [ma,mb]
- // [mc,md]
- };
- ostream& operator<<(ostream &out, const Mat22 &b){ //chainable << operator
- b.display(out); //uses display fx, passing first param of << op to display
- return out;
- };
- istream& operator>>(istream &in, Mat22 &b){ //chainable >> operator
- b.input(in); //input fx is passed first param of >> op
- return in;
- };
- int FastFib(int n){ //retrieve the nth term of the fibonacci sequence
- Mat22 FibMat(1,1,1,0); //using one of default constructors to build an appropriate matrix for sequence
- FibMat = FibMat.FastExp(FibMat, n);
- return FibMat.display(1); //using overloaded display function to access private int
- };
- int main()
- {
- //some test matrices
- Mat22 T1(1,2,3,4);
- Mat22 T2(4,3,2,1);
- Mat22 T3(5,5,5,5);
- Mat22 T4(1234,5678,9101,1121);
- //retrieving n value from user for testing
- int n;
- cout << "Please enter your value for n (used in both FastExp and FastFib): ";
- cin >> n;
- //testing FastExp
- cout << "***FASTEXP TEST***" << endl << "Starting Matrix " << T1;
- T1 = T1.FastExp(T1, n);
- cout << "To the power of " << n << ":" << endl << "Final " << T1 << endl;
- //testing FastFib
- cout << endl << "***FASTFIB TEST***" << endl;
- int g;
- g = FastFib(n);
- cout << "Term " << n << " of the Fibonacci sequence is : " << g << endl;
- //testing overloads
- cout << endl << "***OVERLOAD TEST***" << endl;
- //testing assignment
- T4 = T3;
- cout << T4;
- //testing subtraction and modification of only LHS
- T3 = T2-T3;
- cout << T2;
- //testing chained >> and <<
- cin >> T2 >> T3;
- cout << T2 << T3;
- return 0;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement