【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>


分享到:


相關文章: