Public Function Reverse( _
ByVal Matrix(,) As Double, _
ByVal n As Integer, _
ByRef Reversal(,) As Double _
) As Boolean
Rem 这个函数采用的是高斯消元法
Dim i As Integer, j As Integer, k As Integer
‘ 将Reversal初始化为单位阵(E)
For i = 0 To n - 1
For j = 0 To n - 1
Reversal(i, j) = IIf(i = j, 1, 0)
Next
Next
' 逐行消元求逆(Kernal)
For i = 0 To n - 1
For j = i To n - 1 '往后寻找一个第i列不为0的行,然后把它换到第i行,以备消元
If Matrix(j, i) = 0 Then Return False
Next
Dim tmp As Double
If i <>j Then'交换i, j两行
If j = n Then Return False
For k = 0 To n - 1
tmp = Matrix(i, k) : Matrix(i, k) = Matrix(j, k) : Matrix(j, k) = tmp
tmp = Reversal(i, k) : Reversal(i, k) = Reversal(j, k) : Reversal(j, k) = tmp
Next
End If
For j = 0 To n - 1'消元
If j = i Then Continue For
tmp = Matrix(j, i) / Matrix(i, i)
For k = 0 To n - 1
Matrix(j, k) = Matrix(j, k) - Matrix(i, k) * tmp
Reversal(j, k) = Reversal(j, k) - Reversal(i, k) * tmp
Next
Next
tmp = Matrix(i, i)
For k = 0 To n - 1'将Matrix化为单位阵,得到的Reversal即是它的逆
Reversal(i, k) = Reversal(i, k) / tmp
Matrix(i, k) = Matrix(i, k) / tmp
Next
Next
Return True
End Function
如果Matrix不可逆,那么该函数返回false,Reversal的值无效。
如果可逆,那么该函数返回true,Reversal为其逆阵。
1、求逆的必要条伏谈唤件是N阶方阵A的行列式不等侍盯于02、然后求N阶方阵A的行列式的值|A|
3、求N阶方阵A的伴随缺凯矩阵A*
4、N阶方阵A的逆矩阵=A*/|A|
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)