Function GaussElimPivot(a() As Double, b() As Double) As Boolean
' --------------------------------------------------
' Solves a system of linear equations with the
' Gaussian elimination.
' Kim Sivonen 28.6.2001
' --------------------------------------------------
'
' Partial pivoting is being used for to make the
' algorithm stable. This means that the order of the
' equations is changed so that the biggest values appear
' on the diagonal. New pivot is searched on each row.
'
' The equations are reordered by switching indexes
' in an index table.
'
' Parameters:
' A() = coifficient matrix
' B() = constant vector
'
' Return:
' Gauss_elim = 'true' tai 'false'
' B() = solved values
'
Dim i As Integer, j As Integer, k As Integer, n As Integer
Dim D As Double, c As Double, x() As Double
Dim Indeksi() As Integer
n = UBound(b)
ReDim Indeksi(1 To n)
ReDim x(1 To n)
' format the index table
For i = 1 To n
Indeksi(i) = i
x(i) = b(i)
Next i
' elimination of the lower triangular matrix
Dim Luku As Integer, E As Double, Suurin As Double
For i = 1 To n - 1
' pivot is being searched and swithed
D = a(Indeksi(i), i)
Suurin = i
For j = i + 1 To n
E = a(Indeksi(j), i)
If Abs(E) > Abs(D) Then
D = E
Suurin = j
End If
Next j
Luku = Indeksi(i)
Indeksi(i) = Indeksi(Suurin)
Indeksi(Suurin) = Luku
' elimination
For j = i + 1 To n
c = a(Indeksi(j), i) / D
For k = i To n
a(Indeksi(j), k) = a(Indeksi(j), k) - c * a(Indeksi(i), k)
Next k
x(Indeksi(j)) = x(Indeksi(j)) - c * x(Indeksi(i))
Next j
Next i
' back substitution
For i = n To 1 Step -1
x(Indeksi(i)) = x(Indeksi(i)) / a(Indeksi(i), i)
For j = i - 1 To 1 Step -1
x(Indeksi(j)) = x(Indeksi(j)) - a(Indeksi(j), i) * x(Indeksi(i))
Next j
Next i
For i = 1 To n
b(i) = x(Indeksi(i))
Next i
GaussElimPivot = True
End Function