VB icon

Calculate Cofactors, Minors, and value of Determinant of Matrix

Email
Submitted on: 1/1/2015 9:00:00 AM
By: Anonymous (from psc cd)  
Level: Beginner
User Rating: By 6 Users
Compatibility: C, C++ (general)
Views: 11725
 
     This piece of code calculates the cofactors, the minors, and the value of the determinant of a 3x3 matrix with just one line of code each within a loop! Can't make it any shorter!
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Calculate Cofactors, Minors, and value of Determinant of Matrix
// Description:This piece of code calculates the cofactors, the minors, and the value of the determinant of a 3x3 matrix with just one line of code each within a loop! Can't make it any shorter!
// By: Anonymous (from psc cd)
//
// Inputs:matrix[3][3] - containg the values of the matrix
//
// Returns:cofactor[3][3] - containing the value of cofactors corresponding to the values in matrix array.
minor[3][3] - containing the value of minors
determinant - containing the value of the determinant.
//**************************************

/*	The author of this piece of code is Rahul Khanna
	<rahulk@techie.com>
	Use this code at your own risk.
*/
/*	This piece of code demonstrates how you can calculate
	the cofactors, minors and the value of a 3x3 determinant
	itself with the smallest possible code. The cofactor, and
	minors are calculated in one line of code each! Can you
	make it any smaller?
*/
/*	Assuming there is an array "matrix[3][3]" that contains
	the values of the matrix in the format rows x columns.
	The cofactors of the repective matrix element is stored
	in its position values in the array "cofactor". Eg, cofactor
	of array element matrix[1][1] will be stored in cofactor[1][1].
	Same as above with minors.
*/
long row, col;
long matrix[3][3];
long cofactor[3][3], minor[3][3];
long determinant = 0;
for (row = 0; row < 3; row++)
{
	for (col = 0; col < 3; col++)
	{
		cofactor[row][col] = matrix[(row + 1) % 3][(col + 1) % 3] * matrix[(row + 2) % 3][(col + 2) % 3] - matrix[(row + 1) % 3][(col + 2) % 3] * matrix[(row + 2) % 3][(col + 1) % 3];
		minor[row][col] = (row + col) % 2 == 0 ? cofactor : -cofactor;
		if (row == 0)
			determinant += matrix[0][col] * cofactor;
	}
}


Other 20 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.