• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

如何在C#中去求矩阵的逆矩阵(转载)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

代码如下:

 

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] ) );
}

Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=650142


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
16c554的头文件发布时间:2022-07-14
下一篇:
C++调用shell脚本发布时间:2022-07-14
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap