【VB.net矩陣函數】求行列式Math

函數說明:Math_Matrix_Det(A,n)

A:為n階方陣

n:為矩陣A的階數

函數說明:函數執行成功返回其行列式大小.其原理是按行列式定義依次展開求解.不適合大於5階的方陣.

源代碼:

<code>Public Function Math_Matrix_Det(ByVal k(,) As Double, ByVal N As Integer) As Double '求矩陣的行列式,K的數組大小為N*N的,不然程序出錯.
        Dim i As Integer
        If N = 1 Then
            Return k(0, 0)
        End If
        Dim j As Integer = 0
        Dim m As Integer = 0
        Dim l As Integer = 0
        Dim t(N - 2, N - 2) As Double
        Dim temp As Double = 0
        Dim is1 As Boolean = True
        For i = 0 To N - 1
            If k(0, i) <> 0 Then
                For m = 0 To N - 2
                    l = 0
                    For j = 0 To N - 1
                        If j <> i Then
                            t(m, l) = k(m + 1, j)
                            l += 1
                        End If
                    Next
                Next
                If is1 Then
                    temp += k(0, i) * Math_Matrix_Det(t, N - 1)
                Else
                    temp -= k(0, i) * Math_Matrix_Det(t, N - 1)
                End If
            End If
            is1 = Not is1
        Next
        Return temp
    End Function/<code>


分享到:


相關文章: