在Matrix类中,我能够为3X3以下的矩阵编写自己的代码
class Matrix
{
public float Determinant()
{
if (!isSquare())
return 0;
else if (_rows == 1)
return this[0, 0];
else if (_rows == 2)
{
/* |a b|
|c d|*/
float a = this[0, 0];
float b = this[0, 1];
float c = this[1, 0];
float d = this[1, 1];
return (a * d) - (b * c);
}
else
{
float sum = 0;
int i = 0;
for (int j = 0; j < _cols; j++)
{
//finding cofactor
float a = (float)Math.Pow(-1, i + j);
float b = (j % 2 == 0) ? - a * this[i,j] : a * this[i,j];
Matrix m = subMatrix(i, j);
//getting determinant by recursion
float d = m.Determinant();
sum += b * d;
}
return sum;
}
}
}
此代码停止对大于3X3的矩阵起作用。我读过一些其他人发布的类似内容,但这些内容并没有真正帮助我。我不需要简单的代码,只需一些解释或描述我需要做什么的文章。