I wrote a function for determining the matrix determinant. After testing, I will see an example function later.
Matrix_det.h // This header file will be used later.
// Put it in the header file
Namespace jks
{
//////////////////////////////////////// //////////////////////////////////
Inline long factorial (int n)
{
// Assert (n> 0 );
Long result = 1;
While (n> 0)
Result * = n --;
Return result;
}
Inline int inversionnum (int A [], int N)
{
Int I, J;
Int T = 0;
For (I = 0; I <n-1; I ++)
For (j = I + 1; j <n; j ++)
If (A [I]> A [J])
T ++;
Return T;
}
Template <typename T>
Double matrix_det (T * P, int N)
{
Int I, j, count;
Int * array;
Array = new int [N];
Double T = 1.0;
Double result = 0;
// To initiate the array/
For (I = 0; I <n; I ++)
Array [I] = I;
For (COUNT = 0; count <factorial (n); count ++)
{
T = 1;
For (I = 0; I <n; I ++)
{
J = array [I];
T = T * P [I * n + J];
}
If (inversionnum (array, n) % 2 = 1)
Result + =-T;
Else
Result + = T;
Int I1, J1;
Int temp;
For (I1 = n-2; I1> = 0; I1 --)
If (array [I1] <array [I1 + 1]) break;
For (J1 = n-1; J1> I1; J1 --)
If (array [J1]> array [I1]) break;
Int start;
If (I1 <0 | J1 <0)
Goto out;
Temp = array [I1];
Array [I1] = array [J1];
Array [J1] = temp;
Start = I1 + 1;
Int I2, J2;
For (I2 = start; I2 <n; I2 ++)
For (J2 = start; J2 <n-1; J2 ++)
If (array [J2]> array [J2 + 1])
{
Int T = array [J2];
Array [J2] = array [J2 + 1];
Array [J2 + 1] = T;
}
Out :;
}
Delete [] array;
Return result;
}
//////////////////////////////////////// //////////////////////////////////
}
++ Example
Int example ()
{
Double P [3] [3] = {0, 3, 0, 3, 0, 3, 0 };
Cerr <jks: matrix_det (P [0], 3) <Endl;
}
A collection of programming technologies in the rumor of begtostudy