i want to find determinant of 4x4 matrix in c# [duplicate]
Possible Duplicate:
Calculating an NxN matrix determinant in C#
i want to find determinant of 4x4 matrix in c#
int ss = 4; int count = 0;
int[,] matrix=new int[ss,ss];
ArrayList al = new ArrayList() {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 };
for (int i = 0; i < ss; i++)
{
for (int j = 0; j < ss; j++)
{
matrix[i, j] =Convert.ToInt32( al[count]);
++count;
Response.Write(matrix[i, j] + " ");
开发者_运维百科}
Response.Write("<br/>");
}
If you're fixed to 4x4, the simplest solution would be to just hardcode the formula.
// assumes matrix indices start from 0 (0,1,2 and 3)
public double determinant(int[,] m) {
return
m[0,3] * m[1,2] * m[2,1] * m[3,0] - m[0,2] * m[1,3] * m[2,1] * m[3,0] -
m[0,3] * m[1,1] * m[2,2] * m[3,0] + m[0,1] * m[1,3] * m[2,2] * m[3,0] +
m[0,2] * m[1,1] * m[2,3] * m[3,0] - m[0,1] * m[1,2] * m[2,3] * m[3,0] -
m[0,3] * m[1,2] * m[2,0] * m[3,1] + m[0,2] * m[1,3] * m[2,0] * m[3,1] +
m[0,3] * m[1,0] * m[2,2] * m[3,1] - m[0,0] * m[1,3] * m[2,2] * m[3,1] -
m[0,2] * m[1,0] * m[2,3] * m[3,1] + m[0,0] * m[1,2] * m[2,3] * m[3,1] +
m[0,3] * m[1,1] * m[2,0] * m[3,2] - m[0,1] * m[1,3] * m[2,0] * m[3,2] -
m[0,3] * m[1,0] * m[2,1] * m[3,2] + m[0,0] * m[1,3] * m[2,1] * m[3,2] +
m[0,1] * m[1,0] * m[2,3] * m[3,2] - m[0,0] * m[1,1] * m[2,3] * m[3,2] -
m[0,2] * m[1,1] * m[2,0] * m[3,3] + m[0,1] * m[1,2] * m[2,0] * m[3,3] +
m[0,2] * m[1,0] * m[2,1] * m[3,3] - m[0,0] * m[1,2] * m[2,1] * m[3,3] -
m[0,1] * m[1,0] * m[2,2] * m[3,3] + m[0,0] * m[1,1] * m[2,2] * m[3,3];
}
References
- Wikipedia/Determinant
- EuclidianSpace.com/Determinant 4x4 formula
You might look at The Answer given the last time you posted this exact same question.
I'm not a big fan of doing what appears to be someone else's homework, so instead I'll summarise some thoughts on your problem, which will hopefully be more enlightening than simply posting the solution.
You've probably done 3x3 determinants before, and noticed that the method relies on using the individual 2x2 determinants left over from crossing out a row and a column. You then multiply by the doubly crossed number, and +/- alternately.
So, for a 4x4 matrix, you would simply extend this algorithm. This would then require you to find the determinants of the remaining 3x3 matrices after crossing out a col+row.
Basically, you need a recursive program that does this.
Also, it sounds this question is part of a larger algorithm design course, where you're meant to appreciate that this algorithm isn't very scalable, ie for larger matrices it takes a huge number of calculations.
From what I remember, LU-decomposition is a good alternative that allows matrix inversion with good scaling properties.
What you need is implementation of LU-decomposition.
It decomposes matrix into two triangular matrices L and U such that A = L*U. L is lower triangular matrix and U is upper triangular matrix.
Since A = L*U, then det(A) = det(L)*det(U). Now the fact that determinant of a triangular matrix is equal to product od elements on the diagonal allows to compute det(L) and det(U) easy.
det(L) = diag_prod(L) same for U
so
det(A) = diag_prod(L) * diag_prod(U)
as for actual algorithm for LU-Decomposition I recommend Doolittle algorithm. It is easy to understand and wikipedia has a description.
http://en.wikipedia.org/wiki/LU_decomposition
精彩评论