VB Code:
Public Enum MatOperator
vbSum = 0
vbSubstraction = 1
vbMultiplication = 2
vbDivision = 3
vbPower = 4
vbRoot = 5
End Enum
Public Sub MatInterchange(M As Variant, i1 As Long, j1 As Long, Optional i2 As Long = -1, Optional j2 As Long = -1)
Dim x As Double
If i2 = -1 Then i2 = i1
If j2 = -1 Then j2 = j1
x = M(i1, j1)
M(i1, j1) = M(i2, j2)
M(i2, j2) = x
End Sub
Public Sub MatInterChng(M As Variant, j1 As Long, j2 As Long)
Dim i As Long
For i = LBound(M, 1) To UBound(M, 1)
MatInterchange M, i, j1, i, j2
Next i
End Sub
Public Sub MatOperateRow(M As Variant, j As Long, Operator As MatOperator, ToOperate As Long)
Dim i As Long
For i = LBound(M, 1) To UBound(M, 1)
MatApOperation M, i, j, Operator, ToOperate
Next i
End Sub
Public Sub MatSubstitutePlusK(M As Variant, K As Long, j1 As Long, j2 As Long)
Dim i As Long
For i = LBound(M, 1) To UBound(M, 1)
MatApOperation M, i, j1, vbSum, M(i, j2) * K
Next i
End Sub
Public Sub MatApOperation(M As Variant, i As Long, j As Long, Operator As MatOperator, ToOperate As Long)
Select Case Operator
Case 0:
M(i, j) = M(i, j) + ToOperate
Case 1:
M(i, j) = M(i, j) - ToOperate
Case 2:
M(i, j) = M(i, j) * ToOperate
Case 3:
M(i, j) = M(i, j) / ToOperate
Case 4:
M(i, j) = M(i, j) ^ ToOperate
Case 5:
M(i, j) = M(i, j) ^ -ToOperate
End Select
End Sub
Public Function GaussJordan(A As Variant, B As Variant) As Variant
Dim GJ() As Double
Dim i As Long
Dim j As Long
Dim n As Long
Dim Cons As Double
LnFd = ChR$(13) & ChR$(10)
DLnFd = LnFd & LnFd
ReDim GJ(UBound(A, 1) + 1, UBound(A, 2))
For i = LBound(GJ, 1) To UBound(GJ, 1)
For j = LBound(GJ, 2) To UBound(GJ, 2)
If i = UBound(GJ, 1) Then
GJ(i, j) = B(LBound(B, 1), j)
Else
GJ(i, j) = A(i, j)
End If
Next j
Next i
For i = LBound(GJ, 2) To UBound(GJ, 2)
For n = LBound(GJ, 2) To UBound(GJ, 2)
If n <> i Then
MatSubstitutePlusK GJ, -GJ(i, n), n, i
MsgBox " i = " & i & LnFd & "GJ(" & i & ", " & n & ") = " & GJ(i, n) & " n = " & n & LnFd & "GJ(" & i & ", " & i & ") = " & GJ(i, i), vbExclamation, "Iteration " & i
End If
Next n
Next i
'Turn the main diagonal into 1
For j = LBound(GJ, 2) To UBound(GJ, 2)
If GJ(j, j) <> 1 Then
MatOperateRow GJ, j, vbDivision, CLng(GJ(j, j))
End If
Next j
GaussJordan = GJ
End Function