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