標籤:
1.數學定義
n階行列式定義如下:
2.演算法實現
函數名: GetValue()
功能:返回一個行列式的值
Private Function GetValue() Dim gValue As Double Dim tempResultList As New List(Of Array) Dim tempNumArray(RankLength - 1) As Integer ‘要進行全排列的序列 For i = 0 To RankLength - 1 tempNumArray(i) = i Next GetFullPerm(tempNumArray, 0, tempResultList) Dim temp As Double Dim tempData() As Integer For i = 0 To tempResultList.Count - 1 temp = 1 tempData = tempResultList(i) For j = 0 To RankLength - 1 temp = temp * TableData(j, tempData(j)) Next temp = Math.Pow(-1, GetInverseNum(tempData)) * temp gValue += temp Next Return gValue End Function
3.完整的行列式類(determinant)
使用樣本:
Dim d As New Determinant(3)
d.Item(1, 1) = 1
d.Item(2, 2) = 2
d.Item(3, 3) = 4
Console.write(d.value)
Public Class Determinant Private TableData(,) As Double Private RankLength As Integer ‘行列式的階 Public ReadOnly Property Rank() Get Return RankLength End Get End Property ‘行列式第iRow行第iCol列的元素 Public Property Item(ByVal iRow As Integer, ByVal iCol As Integer) Get Return TableData(iRow - 1, iCol - 1) End Get Set(ByVal value) TableData(iRow - 1, iCol - 1) = value End Set End Property Public ReadOnly Property value() Get Return GetValue() End Get End Property Public Sub New(ByVal nRank As Integer) Dim tempArray(nRank - 1, nRank - 1) As Double TableData = tempArray RankLength = nRank End Sub ‘求行列式的值 Private Function GetValue() Dim gValue As Double Dim tempResultList As New List(Of Array) Dim tempNumArray(RankLength - 1) As Integer ‘要進行全排列的序列 For i = 0 To RankLength - 1 tempNumArray(i) = i Next GetFullPerm(tempNumArray, 0, tempResultList) Dim temp As Double Dim tempData() As Integer For i = 0 To tempResultList.Count - 1 temp = 1 tempData = tempResultList(i) For j = 0 To RankLength - 1 temp = temp * TableData(j, tempData(j)) Next temp = Math.Pow(-1, GetInverseNum(tempData)) * temp gValue += temp Next Return gValue End Function ‘全排列 Private Sub GetFullPerm(ByVal NumArray() As Integer, ByVal LeftIndex As Integer, ByRef Result As List(Of Array)) Dim temp As Integer If LeftIndex = NumArray.Length - 1 Then Dim tempArray(NumArray.Length - 1) As Integer NumArray.CopyTo(tempArray, 0) Result.Add(tempArray) Else temp = NumArray(LeftIndex) For i = LeftIndex To NumArray.Length - 1 NumArray(LeftIndex) = NumArray(i) NumArray(i) = temp ‘對換 GetFullPerm(NumArray, LeftIndex + 1, Result) NumArray(i) = NumArray(LeftIndex) NumArray(LeftIndex) = temp ‘還原對換 Next End If End Sub ‘逆序數 Private Function GetInverseNum(ByVal NumArray() As Integer) Dim Num As Integer = 0 For i = 0 To NumArray.Length - 1 For j = i To NumArray.Length - 1 If NumArray(i) > NumArray(j) Then Num += 1 Next Next Return Num End FunctionEnd Class
View Code
行列式(三):n階行列式