### Advice in writing a function that raises a matrix to a certain power

Hello everyone,
I'm having trouble with my project. I have most of the code already written but am confused as how to make a new class where it takes my matrix and raises it to a certain power. I know I will need a for loop to make the matrix keep multiplying itself but am confused as to how to do this. If someone could help me out, that'd be great. I have posted the project below as well as the code I have already written.

Here is the project.
Revise class matrix from figure 8.9 by adding member function power. This member function should take an integer input parameter n and a matrix output parameter mToTheN. If the default object is a square matrix (same number of rows as columns), power should store in mToTheN the default matrix raised to the nth power. Raising a square matrix M to a power n is defined as the product of n copies of M.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130`` ``````#include #include #include #include using namespace std; const int MAX_ROWS = 6; const int MAX_COLS = 6; class Matrix // This class was created to represent a matrix { public: Matrix() {} Matrix( int, int, int ); Matrix& operator*=( const Matrix& ); private: int rows; int cols; int mat[MAX_ROWS][MAX_COLS]; friend ostream& operator<< ( ostream&, const Matrix& ); friend istream& operator>> ( istream&, Matrix& ); }; // The main routine will input two identical matrices from a file designated by the user. int main() { int n; // The variable that will be used to represent the power the matrix will be raised to int i; // Variable used to evaluate matrix when raised to power of 1 Matrix m1; Matrix m2; string file1Name; cout << "Name of file containing first matrix => "; cin >> file1Name; ifstream infilOne( file1Name.c_str(), ios::in ); infilOne >> m1; infilOne.close(); ifstream infilTwo( file1Name.c_str(), ios::in ); infilOne >> m2; infilOne.close(); cout << " Enter the value of n that the matrix should be raised to " << endl; cin >> n; if ( n==1 ) return m1; if (!infilOne.fail() && ! infilOne.fail()) { cout << endl << "Matrix 1" << endl << endl << m1 << endl << endl; cout << "Matrix 2" << endl << endl << m2 << endl << endl; m1 *= m2; cout << "Matrix 1 x Matrix 2 = " << endl << endl << m1 << endl << endl; } else { cout << "Error in input" << endl; } } return 0; } // Multiplies m1 by m1 if the matrices are conformable. // Otherwise, displays an error message and sets m1's size to 0 x 0 Matrix& Matrix :: operator*=(const Matrix& m2 ) // input { int val, i, j, k; Matrix prod; if (cols != m2.rows) { cout << "Matrices are not conformable." << endl; prod.rows = 0; prod.cols = 0; } else { prod.rows = rows; prod.cols = m2.cols; for (i = 0; i < prod.rows; ++i) for (j = 0; j < prod.cols; ++j) { val = 0; for (k = 0; k < cols; ++k) val += mat[i][k] * m2.mat[k][j]; prod.mat[i][j] = val; } } *this = prod; return *this; } // Prints out the contents of matrix m1 ostream& operator<< ( ostream& os, const Matrix& m ) { for (int i = 0; i < m.rows; ++i) { for (int j = 0; j < m.cols; ++j) cout << setw( 5 ) << m.mat[i][j]; cout << endl; } return os; } istream& operator>> ( istream& is, Matrix& m ) { int i, j; is >> m.rows >> m.cols; if (is.fail()) { m.rows = 0; m.cols = 0; } else if (m.rows > MAX_ROWS || m.cols > MAX_COLS) { is.setstate( ios::failbit); m.rows = 0; m.cols = 0; } else { for (i = 0; i < m.rows; ++i) for (j = 0; j < m.cols; ++j) is >> m.mat[i][j]; if (is.fail()) { m.rows = 0; m.cols = 0; } } return is; }``````
Last edited on
 ``12345678`` ``````Matrix( int &rows, int &columns, int power) { for(int a=0; a

This will make you a matrix raise to any power
Last edited on
@eaglesfan

Your post is rather hard to read with all the unformatted code. Could you please go back and edit it to use code tags.

How to use code tags
http://www.cplusplus.com/articles/jEywvCM9/

But if your operator* is correctly defined, then raising the matrix to a power should work like any other type. So you could get it to work for ints and then convert the funtion to use matrices.

(Your matrix class does look rather incomplete at the moment?!)

Andy

PS @ Albo Coder

1. you're not storing the return from the pow function
2. I'm not sure that raising the elements individually to a given power is what eaglesfan is after
Last edited on
 ``123456789`` ``````void Matrix( int &rows, int &columns, int power) { for(int a=0; a

This one prints every elements in x power
If that is what you want...
How a matrix mathematically rises in x power??

 How a matrix mathematically rises in x power??

Using matrix multiplication.

Andy

For

 ```Matrix 1 2 3 4```

you get

 ```Matrix ^ 0 1 0 0 1 Matrix ^ 1 1 2 3 4 Matrix ^ 2 7 10 15 22 Matrix ^ 3 37 54 81 118 Matrix ^ 4 199 290 435 634 Matrix ^ 5 1069 1558 2337 3406```
Last edited on
How do we get them mathematically?
What crap do we multiply?
Okay I edited the code and used code tags this time, thanks for showing me that. Maybe you can get a better look at it now andywestken.
You don't need new class. A function which works with your matrix class.

`Matrix pow(Matrix m, unsigned int n);`

And as I said before, this should work just like a function which raises an int to an (unsigned) int power.

But I can see issues with your code:
1. you're trying to read m2 from infilOne (after closing it!) rather than infilTwo
2. you're trying to return a matrix variable from main

(the second means your code won't even compile!)

The list of powers I posted above was created using your code after:
1. I got rid of m2 and its associated file
2. I got rid of the broken return
3. I added code which used your operator* to multiply the matrix (using a while loop), starting off with a unit matrix of the right size.
4. I also had to fix the constructors and add methods to set and get elements to I could set a matrix to the required values for it to be a unit matrix. Plus methods to get the size of the input matrix.

Andy

@ Albo Coder

See: Matrix multiplication
http://en.wikipedia.org/wiki/Matrix_multiplication
Last edited on
Okay great with your suggestions, I got it to compile. Thanks Andy.
Topic archived. No new replies allowed.