行列式(四):上三角行列式

1.数学定义

  主对角线以下元素全为零的行列式称为上三角行列式。  


 

2.求行列式  

  把一个行列式转换为上三角行列式,然后求出主对角线乘积即可(定义法求行列式参照:行列式(三):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() As Double
        Dim gValue As Double = 1.0
        Dim temp As Double
        For i = 0 To RankLength - 2
            For j = i + 1 To RankLength - 1
                '判断主对角线元素是否为0
                If TableData(i, i) = 0 Then
                    If i = RankLength - 1 Then Return 0 '行列式值为0
                    For p = 0 To RankLength - 1
                        If Not p = i Then '同行不可相加
                            For q = 0 To RankLength - 1
                                TableData(i, q) += TableData(p, q)
                            Next
                        End If
                        If Not TableData(i, i) = 0 Then Exit For
                    Next
                End If
                If TableData(i, i) = 0 Then Return 0 '行列式值为0
                temp = TableData(j, i) / TableData(i, i)
                TableData(j, i) = 0
                For c = i + 1 To RankLength - 1
                    TableData(j, c) = TableData(j, c) - temp * TableData(i, c)
                Next
            Next
        Next
        For i = 0 To RankLength - 1
            gValue = gValue * TableData(i, i)
        Next
        Return gValue
    End Function
  End Class

 

你可能感兴趣的:(列)