Private Sub Command1_Click()
创新互联主要从事成都网站制作、成都做网站、网页设计、企业做网站、公司建网站等业务。立足成都服务舞阳,10多年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18982081108
Label1.Caption = StrReverse(Text1.Text)
End Sub
1、求逆的必要条件是N阶方阵A的行列式不等于0;
2、然后求N阶方阵A的行列式的值|A|;
3、求N阶方阵A的伴随矩阵A*;
4、N阶方阵A的逆矩阵=A*/|A|;
矩阵求逆的VB程序
Private Function MRinv(N As Integer, mtxA() As Double) As Boolean
'****************************************************************************************
' 功能: 实现矩阵求逆的全选主元高斯-约当法
' 参数: n - Integer型变量,矩阵的阶数
' mtxA - Double型二维数组,体积为n x n。存放原矩阵A;返回时存放其逆矩阵A-1。
' 返回值:Boolean型,失败为False,成功为True
'****************************************************************************************
ReDim nIs(N) As Integer, nJs(N) As Integer
Dim i As Integer, j As Integer, k As Integer
Dim D As Double, p As Double
' 全选主元,消元
For k = 1 To N
D = 0#
For i = k To N
For j = k To N
p = Abs(mtxA(i, j))
If (p D) Then
D = p
nIs(k) = i
nJs(k) = j
End If
Next j
Next i
' 求解失败
If (D + 1# = 1#) Then
MRinv = False
Exit Function
End If
If (nIs(k) k) Then
For j = 1 To N
p = mtxA(k, j)
mtxA(k, j) = mtxA(nIs(k), j)
mtxA(nIs(k), j) = p
Next j
End If
If (nJs(k) k) Then
For i = 1 To N
p = mtxA(i, k)
mtxA(i, k) = mtxA(i, nJs(k))
mtxA(i, nJs(k)) = p
Next i
End If
mtxA(k, k) = 1# / mtxA(k, k)
For j = 1 To N
If (j k) Then mtxA(k, j) = mtxA(k, j) * mtxA(k, k)
Next j
For i = 1 To N
If (i k) Then
For j = 1 To N
If (j k) Then mtxA(i, j) = mtxA(i, j) - mtxA(i, k) * mtxA(k, j)
Next j
End If
Next i
For i = 1 To N
If (i k) Then mtxA(i, k) = -mtxA(i, k) * mtxA(k, k)
Next i
Next k
' 调整恢复行列次序
For k = N To 1 Step -1
If (nJs(k) k) Then
For j = 1 To N
p = mtxA(k, j)
mtxA(k, j) = mtxA(nJs(k), j)
mtxA(nJs(k), j) = p
Next j
End If
If (nIs(k) k) Then
For i = 1 To N
p = mtxA(i, k)
mtxA(i, k) = mtxA(i, nIs(k))
mtxA(i, nIs(k)) = p
Next i
End If
Next k
' 求解成功
MRinv = True
End Function
来源:
算法的大致思想是通过行列式初等变换来求。
代码如下:
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] ) );
}
当前名称:vb点虐
逆矩阵代码 vb矩阵输出
本文来源:https://www.cdcxhl.com/article14/ddecjde.html
成都网站建设公司_创新互联,为您提供用户体验、ChatGPT、手机网站建设、网站收录、虚拟主机、微信小程序
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联