I read the previous messages , but was unable to comprehend them in terms of programming. Kindly, guide me through proper and orgranize way.
Help in finding determinant of a matrix
Collapse
X
-
Hi,Originally posted by AanzI read the previous messages , but was unable to comprehend them in terms of programming. Kindly, guide me through proper and orgranize way.
Check this thread...
determinant of a matrix
Regards,
M.Sivadhas. -
A determinant is defined recursively. So, you need to expand along a row and recursively call your algorithm. Something like this:Originally posted by sivadhas2006
You'll have to do your own matrix handling (preferably some sort of object with a good constructor and destructor); I just used a matrix-like code to illustrate the point: the determinant of an n x n matrix is broken down into a sum of determinants of (n-1) x (n-1) matrices. The determinant of a 1 x 1 matrix is defined. So, the algorithm just keeps decreasing until it hits a combination of 1 x 1 determinants. -- PaulCode:int SignOfTerm( int i, int j ) { if( i+j % 2 == 0 ) { return 1; } return -1; } Matrix CreateSmallerMatrix( Matrix& Input, int i, int j) { Matrix Output( Input.Rows-1, Input.Cols-1 ); for( int ii=0 ; ii < i ; ii++ ) { for( int jj=0 ; jj < j ; jj++ ) { Output(ii,jj) = Input(ii,jj); } for( int jj=j+1 ; jj < Input.Cols ; jj++ ) { Output(ii,jj-1) = Input(ii,jj); } } for( int ii=i+1 ; ii < Input.Rows ; ii++ ) { for( int jj=0 ; jj < j ; jj++ ) { Output(ii-1,jj) = Input(ii,jj); } for( int jj=j+1 ; jj < Input.Cols ; jj++ ) { Output(ii-1,jj-1) = Input(ii,jj); } } return Output; } double Determinant( Matrix& Input ) { if( Input.rows != Input.cols ) { cout << "You can only take determinants of square matrices!\n"; return 0; } if( Input.rows == 1 && Input.cols == 1 ) { return Input(0,0); } double output = 0.0; for( int j=0; j < cols ; j++ ) { Matrix Temp = SmallerMatrix( Input, 0, j); output += ( SignOfTerm(0,j) * Determinant(Temp) ); } return output; }Comment
-
k, since i have to use an array to find the determinant of a matrix, so then how should i start?Originally posted by macklin01A determinant is defined recursively. So, you need to expand along a row and recursively call your algorithm. Something like this:
You'll have to do your own matrix handling (preferably some sort of object with a good constructor and destructor); I just used a matrix-like code to illustrate the point: the determinant of an n x n matrix is broken down into a sum of determinants of (n-1) x (n-1) matrices. The determinant of a 1 x 1 matrix is defined. So, the algorithm just keeps decreasing until it hits a combination of 1 x 1 determinants. -- PaulCode:int SignOfTerm( int i, int j ) { if( i+j % 2 == 0 ) { return 1; } return -1; } Matrix CreateSmallerMatrix( Matrix& Input, int i, int j) { Matrix Output( Input.Rows-1, Input.Cols-1 ); for( int ii=0 ; ii < i ; ii++ ) { for( int jj=0 ; jj < j ; jj++ ) { Output(ii,jj) = Input(ii,jj); } for( int jj=j+1 ; jj < Input.Cols ; jj++ ) { Output(ii,jj-1) = Input(ii,jj); } } for( int ii=i+1 ; ii < Input.Rows ; ii++ ) { for( int jj=0 ; jj < j ; jj++ ) { Output(ii-1,jj) = Input(ii,jj); } for( int jj=j+1 ; jj < Input.Cols ; jj++ ) { Output(ii-1,jj-1) = Input(ii,jj); } } return Output; } double Determinant( Matrix& Input ) { if( Input.rows != Input.cols ) { cout << "You can only take determinants of square matrices!\n"; return 0; } if( Input.rows == 1 && Input.cols == 1 ) { return Input(0,0); } double output = 0.0; for( int j=0; j < cols ; j++ ) { Matrix Temp = SmallerMatrix( Input, 0, j); output += ( SignOfTerm(0,j) * Determinant(Temp) ); } return output; }Comment
-
Sorry to miss your response. my instant email notifications don't seem to be working. Are you still having difficulty?Originally posted by Aanzk, since i have to use an array to find the determinant of a matrix, so then how should i start?
You may not need to make any array class. (Although I'm surprised you didn't already have something implemented, since you're asking about functions that act on matrices/arrays.) Do something like this:
So, you would temporarily allocate memory for the sub-forum, pass the memory address recursively, and then clean up after the temporary array is no longer needed. -- PaulCode:int ArraySize = 15; // (or whatever the size is, equal to rows * columns) // allocate memory for a new temporary array. double* TemporaryArray; TemporaryArray = new double [ArraySize]; // Copy the necessary values into the temprary array here. // Use a row-ordering form of some sort: ( a11, a12, ... a1n, a21, a22, ... , amn) // run the determinant on this smaller array double temp = Determinant( TemporaryArray , ArraySize ); // free the memory allocated to that temporary array delete [] TemporaryArray;
Comment
-
*edit*
Sorry for the double-post, but I only thought of this after the 5-minute edit window.
It had slipped my mind, but I have an open source SimpleArray class that could probably work for you. (Although you may need to fill it out with necessary copy constructors, etc.)
Check out SimpleArray add-on for EasyBMP. You'll need to download it as a part of the extensions package, but it's fully independent of EasyBMP. That should do well enough to get you started. Make sure to pass by reference, and not pass data copies with that class, though!!! -- Paul
*/edit*Comment
Comment