code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
Terms of Agreement:
By using this code, you agree to the following terms...
1) You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for langauges that allow it) freely and with no charge.
2) You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.
3) You may link to this code from another website, but ONLY if it is not wrapped in a frame.
4) You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
/* The author of this piece of code is Rahul Khanna
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;
}
}