求矩阵的逆【用C#语言编程】

各位大神,求矩阵的逆,用C#语言编。急。。。。。。。求代码啊!... 各位大神,求矩阵的逆,用C#语言编。急。。。。。。。求代码啊! 展开
 我来答
熊_
推荐于2016-12-01 · TA获得超过114个赞
知道答主
回答量:106
采纳率:0%
帮助的人:67.4万
展开全部
算法的大致思想是通过行列式初等变换来求。
代码如下:

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] ) );
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式