ITPub博客

首页 > Linux操作系统 > Linux操作系统 > [转载]如何在C#去求矩阵的逆矩阵

[转载]如何在C#去求矩阵的逆矩阵

原创 Linux操作系统 作者:dinner1007 时间:2019-01-22 17:42:05 0 删除 编辑
如何在C#去求矩阵的逆矩阵
如何在C#去求矩阵的逆矩阵

最 近有网友问我,怎么去求矩阵的逆,当时就按照以前线性代数上面方法告诉他算法,例如先求矩阵行列式的值,然后再求每一项的代数余子式,然后按照矩阵逆的公 式去计算。但是等他向我求代码的时候,发现做法并不是那么简单,甚至用上面的思路,很难做出来。于是我参考网上求行列式值的算法,在上面的基础上完成了矩 阵逆的算法。

算法的大致思想是通过行列式初等变换来求。

大致的代码如下:

private double[,] ReverseMatrix( double[,] dMatrix, int Level )

{

double dMatrixValue = MatrixValue( dMatrix, Level );

if( dMatrixValue == 0 ) return null;

double[,] dReverseMatrix = new double[Level,2*Level];

double x, c;

// Init Reverse matrix

for( int i = 0; i < Level; i++ )

{

for( int j = 0; j < 2 * Level; j++ )

{

if( j < Level )

dReverseMatrix[i,j] = dMatrix[i,j];

else

dReverseMatrix[i,j] = 0;

}

dReverseMatrix[i,Level + i ] = 1;

}

for( int i = 0, j = 0; i < Level && j < Level; i++, j++ )

{

if( dReverseMatrix[i,j] == 0 )

{

int m = i;

for( ; dMatrix[m,j] == 0; m++ );

if( m == Level )

return null;

else

{

// Add i-row with m-row

for( int n = j; n < 2 * Level; n++ )

dReverseMatrix[i,n] += dReverseMatrix[m,n];

}

}

// Format the i-row with "1" start

x = dReverseMatrix[i,j];

if( x != 1 )

{

for( int n = j; n < 2 * Level; n++ )

if( dReverseMatrix[i,n] != 0 )

dReverseMatrix[i,n] /= x;

}

// Set 0 to the current column in the rows after current row

for( int s = Level - 1; s > i;s-- )

{

x = dReverseMatrix[s,j];

for( int t = j; t < 2 * Level; t++ )

dReverseMatrix[s,t] -= ( dReverseMatrix[i,t]* x );

}

}

// Format the first matrix into unit-matrix

for( int i = Level - 2; i >= 0; i-- )

{

for( int j = i + 1 ; j < Level; j++ )

if( dReverseMatrix[i,j] != 0 )

{

c = dReverseMatrix[i,j];

for( int n = j; n < 2*Level; n++ )

dReverseMatrix[i,n] -= ( c * dReverseMatrix[j,n] );

}

}

double[,] dReturn = new double[Level, Level];

for( int i = 0; i < Level; i++ )

for( int j = 0; j < Level; j++ )

dReturn[i,j] = dReverseMatrix[i,j+Level];

return dReturn;

}

private double MatrixValue( double[,] MatrixList, int Level )

{

double[,] dMatrix = new double[Level, Level];

for( int i = 0; i < Level; i++ )

for( int j = 0; j < Level; j++ )

dMatrix[i,j] = MatrixList[i,j];

double c, x;

int k = 1;

for( int i = 0, j = 0; i < Level && j < Level; i++, j++ )

{

if( dMatrix[i,j] == 0 )

{

int m = i;

for( ; dMatrix[m,j] == 0; m++ );

if( m == Level )

return 0;

else

{

// Row change between i-row and m-row

for( int n = j; n < Level; n++ )

{

c = dMatrix[i,n];

dMatrix[i,n] = dMatrix[m,n];

dMatrix[m,n] = c;

}

// Change value pre-value

k *= (-1);

}

}

// Set 0 to the current column in the rows after current row

for( int s = Level - 1; s > i;s-- )

{

x = dMatrix[s,j];

for( int t = j; t < Level; t++ )

dMatrix[s,t] -= dMatrix[i,t]* ( x/dMatrix[i,j] );

}

}

double sn = 1;

for( int i = 0; i < Level; i++ )

{

if( dMatrix[i,i] != 0 )

sn *= dMatrix[i,i];

else

return 0;

}

return k*sn;

}

函数的调用如下:

double[,] dMatrix = new double[3,3]{{0,1,2},{1,0,1},{4,2,1}};

double[,] dReturn = ReverseMatrix( dMatrix, 3 );

if( dReturn != null )

{

for( int i=0; i < 3; i++ )

Debug.WriteLine( string.Format( "{0} {1} {2}",

dReturn[i,0], dReturn[i,1],dReturn[i,2] ) );

}

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/374079/viewspace-130567/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2018-08-23

  • 博文量
    392
  • 访问量
    249086