|
|
Namespace MathEx.MatrixOps |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Public Class Determinant |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Public Shared Function rmatrixdet(ByVal a As Double(,), ByVal n As Integer) As Double |
|
|
Dim pivots As Integer() = New Integer(0) {} |
|
|
Dim a2 = DirectCast(a.Clone, Double(,)) |
|
|
MathEx.SysLin.lu.rmatrixlu(a2, n, n, pivots) |
|
|
Return MathEx.MatrixOps.Determinant.rmatrixludet(a2, pivots, n) |
|
|
End Function |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Public Shared Function rmatrixludet(ByRef a As Double(,), ByRef pivots As Integer(), ByVal n As Integer) As Double |
|
|
Dim num As Double = 0 |
|
|
Dim index As Integer = 0 |
|
|
Dim num3 As Integer = 0 |
|
|
num = 1 |
|
|
num3 = 1 |
|
|
index = 0 |
|
|
Do While (index <= (n - 1)) |
|
|
num = (num * a(index, index)) |
|
|
If (pivots(index) <> index) Then |
|
|
num3 = -num3 |
|
|
End If |
|
|
index += 1 |
|
|
Loop |
|
|
Return (num * num3) |
|
|
End Function |
|
|
|
|
|
Public Shared Function determinant(ByVal a As Double(,), ByVal n As Integer) As Double |
|
|
Dim pivots As Integer() = New Integer(0) {} |
|
|
a = DirectCast(a.Clone, Double(,)) |
|
|
MathEx.SysLin.lu.ludecomposition(a, n, n, pivots) |
|
|
Return MathEx.MatrixOps.Determinant.determinantlu(a, pivots, n) |
|
|
End Function |
|
|
|
|
|
Public Shared Function determinantlu(ByRef a As Double(,), ByRef pivots As Integer(), ByVal n As Integer) As Double |
|
|
Dim num As Double = 0 |
|
|
Dim index As Integer = 0 |
|
|
Dim num3 As Integer = 0 |
|
|
num = 1 |
|
|
num3 = 1 |
|
|
index = 1 |
|
|
Do While (index <= n) |
|
|
num = (num * a(index, index)) |
|
|
If (pivots(index) <> index) Then |
|
|
num3 = -num3 |
|
|
End If |
|
|
index += 1 |
|
|
Loop |
|
|
Return (num * num3) |
|
|
End Function |
|
|
|
|
|
End Class |
|
|
|
|
|
Public Class Inverse |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Public Shared Function rmatrixinverse(ByRef a As Double(,), ByVal n As Integer) As Boolean |
|
|
Dim pivots As Integer() = New Integer() {} |
|
|
MathEx.SysLin.lu.rmatrixlu(a, n, n, pivots) |
|
|
Return rmatrixluinverse(a, pivots, n) |
|
|
End Function |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Public Shared Function rmatrixluinverse(ByRef a As Double(,), ByRef pivots As Integer(), ByVal n As Integer) As Boolean |
|
|
Dim flag As Boolean = False |
|
|
Dim numArray As Double() = New Double() {} |
|
|
Dim index As Integer = 0 |
|
|
Dim num2 As Integer = 0 |
|
|
Dim num3 As Integer = 0 |
|
|
Dim num4 As Double = 0 |
|
|
Dim num5 As Integer = 0 |
|
|
flag = True |
|
|
If (n <> 0) Then |
|
|
numArray = New Double(((n - 1) + 1)) {} |
|
|
If Not TRInverse.rmatrixtrinverse(a, n, True, False) Then |
|
|
Return False |
|
|
End If |
|
|
num2 = (n - 1) |
|
|
Do While (num2 >= 0) |
|
|
index = (num2 + 1) |
|
|
Do While (index <= (n - 1)) |
|
|
numArray(index) = a(index, num2) |
|
|
a(index, num2) = 0 |
|
|
index += 1 |
|
|
Loop |
|
|
If (num2 < (n - 1)) Then |
|
|
index = 0 |
|
|
Do While (index <= (n - 1)) |
|
|
num4 = 0 |
|
|
num5 = (num2 + 1) |
|
|
Do While (num5 <= (n - 1)) |
|
|
num4 = (num4 + (a(index, num5) * numArray(num5))) |
|
|
num5 += 1 |
|
|
Loop |
|
|
a(index, num2) = (a(index, num2) - num4) |
|
|
index += 1 |
|
|
Loop |
|
|
End If |
|
|
num2 -= 1 |
|
|
Loop |
|
|
num2 = (n - 2) |
|
|
Do While (num2 >= 0) |
|
|
num3 = pivots(num2) |
|
|
If (num3 <> num2) Then |
|
|
num5 = 0 |
|
|
Do While (num5 <= (n - 1)) |
|
|
numArray(num5) = a(num5, num2) |
|
|
num5 += 1 |
|
|
Loop |
|
|
num5 = 0 |
|
|
Do While (num5 <= (n - 1)) |
|
|
a(num5, num2) = a(num5, num3) |
|
|
num5 += 1 |
|
|
Loop |
|
|
num5 = 0 |
|
|
Do While (num5 <= (n - 1)) |
|
|
a(num5, num3) = numArray(num5) |
|
|
num5 += 1 |
|
|
Loop |
|
|
End If |
|
|
num2 -= 1 |
|
|
Loop |
|
|
End If |
|
|
Return flag |
|
|
End Function |
|
|
|
|
|
Public Shared Function inverse(ByRef a As Double(,), ByVal n As Integer) As Boolean |
|
|
Dim pivots As Integer() = New Integer() {} |
|
|
MathEx.SysLin.lu.ludecomposition(a, n, n, pivots) |
|
|
Return MathEx.MatrixOps.Inverse.inverselu(a, pivots, n) |
|
|
End Function |
|
|
|
|
|
Public Shared Function inverselu(ByRef a As Double(,), ByRef pivots As Integer(), ByVal n As Integer) As Boolean |
|
|
Dim flag As Boolean = False |
|
|
Dim numArray As Double() = New Double() {} |
|
|
Dim index As Integer = 0 |
|
|
Dim num2 As Integer = 0 |
|
|
Dim num3 As Integer = 0 |
|
|
Dim num4 As Integer = 0 |
|
|
Dim num5 As Double = 0 |
|
|
Dim num6 As Integer = 0 |
|
|
flag = True |
|
|
If (n <> 0) Then |
|
|
numArray = New Double((n + 1)) {} |
|
|
If Not TRInverse.invtriangular(a, n, True, False) Then |
|
|
Return False |
|
|
End If |
|
|
num2 = n |
|
|
Do While (num2 >= 1) |
|
|
index = (num2 + 1) |
|
|
Do While (index <= n) |
|
|
numArray(index) = a(index, num2) |
|
|
a(index, num2) = 0 |
|
|
index += 1 |
|
|
Loop |
|
|
If (num2 < n) Then |
|
|
num4 = (num2 + 1) |
|
|
index = 1 |
|
|
Do While (index <= n) |
|
|
num5 = 0 |
|
|
num6 = num4 |
|
|
Do While (num6 <= n) |
|
|
num5 = (num5 + (a(index, num6) * numArray(num6))) |
|
|
num6 += 1 |
|
|
Loop |
|
|
a(index, num2) = (a(index, num2) - num5) |
|
|
index += 1 |
|
|
Loop |
|
|
End If |
|
|
num2 -= 1 |
|
|
Loop |
|
|
num2 = (n - 1) |
|
|
Do While (num2 >= 1) |
|
|
num3 = pivots(num2) |
|
|
If (num3 <> num2) Then |
|
|
num6 = 1 |
|
|
Do While (num6 <= n) |
|
|
numArray(num6) = a(num6, num2) |
|
|
num6 += 1 |
|
|
Loop |
|
|
num6 = 1 |
|
|
Do While (num6 <= n) |
|
|
a(num6, num2) = a(num6, num3) |
|
|
num6 += 1 |
|
|
Loop |
|
|
num6 = 1 |
|
|
Do While (num6 <= n) |
|
|
a(num6, num3) = numArray(num6) |
|
|
num6 += 1 |
|
|
Loop |
|
|
End If |
|
|
num2 -= 1 |
|
|
Loop |
|
|
End If |
|
|
Return flag |
|
|
End Function |
|
|
|
|
|
End Class |
|
|
|
|
|
Public Class TRInverse |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Public Shared Function rmatrixtrinverse(ByRef a As Double(,), ByVal n As Integer, ByVal isupper As Boolean, ByVal isunittriangular As Boolean) As Boolean |
|
|
Dim flag As Boolean = False |
|
|
Dim flag2 As Boolean = False |
|
|
Dim index As Integer = 0 |
|
|
Dim num2 As Integer = 0 |
|
|
Dim num3 As Double = 0 |
|
|
Dim num4 As Double = 0 |
|
|
Dim numArray As Double() = New Double() {} |
|
|
Dim num5 As Integer = 0 |
|
|
flag = True |
|
|
numArray = New Double(((n - 1) + 1)) {} |
|
|
flag2 = Not isunittriangular |
|
|
If isupper Then |
|
|
num2 = 0 |
|
|
Do While (num2 <= (n - 1)) |
|
|
If flag2 Then |
|
|
If (a(num2, num2) = 0) Then |
|
|
Return False |
|
|
End If |
|
|
a(num2, num2) = (1 / a(num2, num2)) |
|
|
num4 = -a(num2, num2) |
|
|
Else |
|
|
num4 = -1 |
|
|
End If |
|
|
If (num2 > 0) Then |
|
|
num5 = 0 |
|
|
Do While (num5 <= (num2 - 1)) |
|
|
numArray(num5) = a(num5, num2) |
|
|
num5 += 1 |
|
|
Loop |
|
|
index = 0 |
|
|
Do While (index <= (num2 - 1)) |
|
|
If (index < (num2 - 1)) Then |
|
|
num3 = 0 |
|
|
num5 = (index + 1) |
|
|
Do While (num5 <= (num2 - 1)) |
|
|
num3 = (num3 + (a(index, num5) * numArray(num5))) |
|
|
num5 += 1 |
|
|
Loop |
|
|
Else |
|
|
num3 = 0 |
|
|
End If |
|
|
If flag2 Then |
|
|
a(index, num2) = (num3 + (a(index, index) * numArray(index))) |
|
|
Else |
|
|
a(index, num2) = (num3 + numArray(index)) |
|
|
End If |
|
|
index += 1 |
|
|
Loop |
|
|
num5 = 0 |
|
|
Do While (num5 <= (num2 - 1)) |
|
|
a(num5, num2) = (num4 * a(num5, num2)) |
|
|
num5 += 1 |
|
|
Loop |
|
|
End If |
|
|
num2 += 1 |
|
|
Loop |
|
|
Return flag |
|
|
End If |
|
|
num2 = (n - 1) |
|
|
Do While (num2 >= 0) |
|
|
If flag2 Then |
|
|
If (a(num2, num2) = 0) Then |
|
|
Return False |
|
|
End If |
|
|
a(num2, num2) = (1 / a(num2, num2)) |
|
|
num4 = -a(num2, num2) |
|
|
Else |
|
|
num4 = -1 |
|
|
End If |
|
|
If (num2 < (n - 1)) Then |
|
|
num5 = (num2 + 1) |
|
|
Do While (num5 <= (n - 1)) |
|
|
numArray(num5) = a(num5, num2) |
|
|
num5 += 1 |
|
|
Loop |
|
|
index = (num2 + 1) |
|
|
Do While (index <= (n - 1)) |
|
|
If (index > (num2 + 1)) Then |
|
|
num3 = 0 |
|
|
num5 = (num2 + 1) |
|
|
Do While (num5 <= (index - 1)) |
|
|
num3 = (num3 + (a(index, num5) * numArray(num5))) |
|
|
num5 += 1 |
|
|
Loop |
|
|
Else |
|
|
num3 = 0 |
|
|
End If |
|
|
If flag2 Then |
|
|
a(index, num2) = (num3 + (a(index, index) * numArray(index))) |
|
|
Else |
|
|
a(index, num2) = (num3 + numArray(index)) |
|
|
End If |
|
|
index += 1 |
|
|
Loop |
|
|
num5 = (num2 + 1) |
|
|
Do While (num5 <= (n - 1)) |
|
|
a(num5, num2) = (num4 * a(num5, num2)) |
|
|
num5 += 1 |
|
|
Loop |
|
|
End If |
|
|
num2 -= 1 |
|
|
Loop |
|
|
Return flag |
|
|
End Function |
|
|
|
|
|
Public Shared Function invtriangular(ByRef a As Double(,), ByVal n As Integer, ByVal isupper As Boolean, ByVal isunittriangular As Boolean) As Boolean |
|
|
Dim flag As Boolean = False |
|
|
Dim flag2 As Boolean = False |
|
|
Dim index As Integer = 0 |
|
|
Dim num2 As Integer = 0 |
|
|
Dim num3 As Integer = 0 |
|
|
Dim num4 As Integer = 0 |
|
|
Dim num5 As Double = 0 |
|
|
Dim num6 As Double = 0 |
|
|
Dim numArray As Double() = New Double() {} |
|
|
Dim num7 As Integer = 0 |
|
|
flag = True |
|
|
numArray = New Double((n + 1)) {} |
|
|
flag2 = Not isunittriangular |
|
|
If isupper Then |
|
|
num2 = 1 |
|
|
Do While (num2 <= n) |
|
|
If flag2 Then |
|
|
If (a(num2, num2) = 0) Then |
|
|
Return False |
|
|
End If |
|
|
a(num2, num2) = (1 / a(num2, num2)) |
|
|
num6 = -a(num2, num2) |
|
|
Else |
|
|
num6 = -1 |
|
|
End If |
|
|
If (num2 > 1) Then |
|
|
num3 = (num2 - 1) |
|
|
num7 = 1 |
|
|
Do While (num7 <= num3) |
|
|
numArray(num7) = a(num7, num2) |
|
|
num7 += 1 |
|
|
Loop |
|
|
index = 1 |
|
|
Do While (index <= (num2 - 1)) |
|
|
If (index < (num2 - 1)) Then |
|
|
num5 = 0 |
|
|
num7 = (index + 1) |
|
|
Do While (num7 <= num3) |
|
|
num5 = (num5 + (a(index, num7) * numArray(num7))) |
|
|
num7 += 1 |
|
|
Loop |
|
|
Else |
|
|
num5 = 0 |
|
|
End If |
|
|
If flag2 Then |
|
|
a(index, num2) = (num5 + (a(index, index) * numArray(index))) |
|
|
Else |
|
|
a(index, num2) = (num5 + numArray(index)) |
|
|
End If |
|
|
index += 1 |
|
|
Loop |
|
|
num7 = 1 |
|
|
Do While (num7 <= num3) |
|
|
a(num7, num2) = (num6 * a(num7, num2)) |
|
|
num7 += 1 |
|
|
Loop |
|
|
End If |
|
|
num2 += 1 |
|
|
Loop |
|
|
Return flag |
|
|
End If |
|
|
num2 = n |
|
|
Do While (num2 >= 1) |
|
|
If flag2 Then |
|
|
If (a(num2, num2) = 0) Then |
|
|
Return False |
|
|
End If |
|
|
a(num2, num2) = (1 / a(num2, num2)) |
|
|
num6 = -a(num2, num2) |
|
|
Else |
|
|
num6 = -1 |
|
|
End If |
|
|
If (num2 < n) Then |
|
|
num4 = (num2 + 1) |
|
|
num7 = num4 |
|
|
Do While (num7 <= n) |
|
|
numArray(num7) = a(num7, num2) |
|
|
num7 += 1 |
|
|
Loop |
|
|
index = (num2 + 1) |
|
|
Do While (index <= n) |
|
|
If (index > (num2 + 1)) Then |
|
|
num5 = 0 |
|
|
num7 = num4 |
|
|
Do While (num7 <= (index - 1)) |
|
|
num5 = (num5 + (a(index, num7) * numArray(num7))) |
|
|
num7 += 1 |
|
|
Loop |
|
|
Else |
|
|
num5 = 0 |
|
|
End If |
|
|
If flag2 Then |
|
|
a(index, num2) = (num5 + (a(index, index) * numArray(index))) |
|
|
Else |
|
|
a(index, num2) = (num5 + numArray(index)) |
|
|
End If |
|
|
index += 1 |
|
|
Loop |
|
|
num7 = num4 |
|
|
Do While (num7 <= n) |
|
|
a(num7, num2) = (num6 * a(num7, num2)) |
|
|
num7 += 1 |
|
|
Loop |
|
|
End If |
|
|
num2 -= 1 |
|
|
Loop |
|
|
Return flag |
|
|
End Function |
|
|
|
|
|
End Class |
|
|
|
|
|
End Namespace |