行列式(三):n阶行列式

  1.数学定义

    n阶行列式定义如下:

行列式(三):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)

行列式(三):n阶行列式
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 Function

End Class
View Code

 

你可能感兴趣的:(列)