VB.Net矩阵求秩函数

 Public Function Math_Matrix_Rank(ByVal K(,) As Integer, ByVal error_ As Integer, GetList As Integer) As Integer  '返回矩阵的秩.
        Dim n As Integer = GetList
        Dim m As Integer = K.Length \ n
        Dim i As Integer = 0
        Dim i1 As Integer
        Dim j As Integer = 0
        Dim j1 As Integer
        Dim temp1 As Single
        If m > n Then '保证m≤n
            i = m
            m = n
            n = i
            i = 1
        End If
        m -= 1
        n -= 1
        Dim temp(m, n) As Single
        If i = 0 Then
            For i = 0 To m
                For j = 0 To n
                    temp(i, j) = K(i, j)
                Next
            Next
        Else
            For i = 0 To m
                For j = 0 To n
                    temp(i, j) = K(j, i)
                Next
            Next
        End If
        If m = 0 Then
            i = 0
            While i <= n
                If K(0, i) <> 0 Then
                    Return 1
                End If
                i += 1
            End While
            Return 0
        End If
        Dim error0 As Double
        If error_ = -1 Then
            error0 = System.Math.Pow(0.1, 10)
        Else
            error0 = System.Math.Pow(0.1, error_)
        End If
        i = 0
        While i <= m '保证误差可控制
            j = 0
            While j <= n
                If temp(i, j) <> 0 Then
                    error0 *= temp(i, j)
                    i = m
                    Exit While
                End If
                j += 1
            End While
            i += 1
        End While
        Dim error1 As Double
        For i = 0 To m '消0过程
            j = 0
            While j <= n
                If temp(i, j) <> 0 Then
                    Exit While
                End If
                j += 1
            End While
            If j <= n Then
                i1 = 0
                While i1 <= m
                    If temp(i1, j) <> 0 And i1 <> i Then
                        temp1 = temp(i, j) / temp(i1, j)
                        error1 = System.Math.Abs((temp(i, j) - temp(i1, j) * temp1)) * 100 '误差控制。因为有时候temp(i, j) - temp(i1, j) * (temp(i, j) / temp(i1, j))≠0
                        error1 += error0
                        For j1 = 0 To n
                            temp(i1, j1) = temp(i, j1) - temp(i1, j1) * temp1
                            If System.Math.Abs(temp(i1, j1)) < error1 Then
                                temp(i1, j1) = 0
                            End If
                        Next
                    End If
                    i1 += 1
                End While
            End If
        Next
        i1 = 0 '作为返回值的临时变量
        For i = 0 To m
            For j = 0 To n
                If temp(i, j) <> 0 Then
                    i1 += 1
                    Exit For
                End If
            Next
        Next
        Return i1
    End Function


推荐文章:那些年,做的几个应用


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值