dcsimg

Working with Linear Equations in Visual Basic.NET

WEBINAR:
On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


Part 1: Gaussian Elimination

Introduction

Our good friend, math…

With everything else, Math can get quite complicated, causing Visual Basic to become complicated. Today, you will learn how to do Linear algebra with Visual Basic.NET. There is a ton of work, so let's jump right in!

Linear Algebra

Linear algebra is a branch of mathematics containing linear equations and linear functions, along with their representations through matrices and vector spaces. Linear algebra is used for defining basic objects such as planes, lines, and rotations, making it useful for modern presentations of geometry. Linear algebra is used in science and engineering areas as well, because it allows modeling many natural phenomena.

Linear Equations

Linear Equations are algebraic equations in which each term is either a constant or the product of a constant and a single variable. A very basic example of a linear equation with only one variable is as follows: ax + b = 0. Here, a and b are constants and a ≠ 0. Constants can be parameters or numbers, or non-linear functions of parameters.

Gaussian Elimination

Gaussian Elimination, or row reduction, is an algorithm for solving linear equations. Gaussian Elimination can be used to find the rank of a matrix, to calculate the determinant of a matrix, and to calculate the inverse of an invertible square matrix.

Practical

Create a new Visual Basic Windows Forms project and design your form to resemble Figure 1. Remember: My names might be different than yours, so the onus is on you to rename your objects or amend your code accordingly.

Design
Figure 1: Design

Add the Fraction Class.

This class is responsible for handling each fraction (input). It's a lot of code…

Public Class Fraction

   Private biNumerator As Numerics.BigInteger
   Private biDenumerator As Numerics.BigInteger

   Sub New()

      Numerator = 0
      Denumerator = 1

   End Sub

   Sub New(ByVal dFract As Decimal)

      Dim decIntput As Decimal
      decIntput = (dFract)

      Dim fFract As New Fraction

      Dim intCount As Integer = BitConverter.GetBytes(Decimal _
         .GetBits(dFract)(3))(2)

      If intCount < 6 Then

         fFract.Numerator = CInt(dFract * 10 ^ intCount)
         fFract.Denumerator = CInt(10 ^ intCount)

      Else

         Dim dblTemp As Double = 1

         If dFract < 0 Then

            dblTemp = -1

         End If

         fFract = GetFraction(Math.Abs(dFract))

         fFract.Numerator *= dblTemp

      End If

      Numerator = fFract.Numerator
      Denumerator = fFract.Denumerator

   End Sub

   Private Function GetFraction(ByVal dFract As Decimal)

      Dim decTemp As Decimal = dFract

      Dim lstFract As New List(Of Numerics.BigInteger)

      For i As Integer = 0 To 20

         lstFract.Add(New Numerics.BigInteger(decTemp))

         Dim fFract As Double = GetFraction(lstFract).ToDouble

         If dFract = fFract Then

            Exit For

         End If

         Try

            decTemp = 1 / (decTemp - CInt(decTemp))

         Catch ex As Exception

            Exit For

         End Try

      Next

      Return GetFraction(lstFract)

   End Function
   Private Function GetFraction(ByVal lstFract As List(Of _
         Numerics.BigInteger)) As Fraction

      Dim fmRes As New Fraction

      fmRes.Numerator = lstFract(lstFract.Count - 1)
      fmRes.Denumerator = 1

      For i As Integer = lstFract.Count - 2 To 0 Step -1

         If Not fmRes.Denumerator = 0 Then

            fmRes = New Fraction(lstFract(i), 1) + 1 / fmRes

         End If

      Next

      Return fmRes

   End Function

   Sub New(ByVal Val1 As Numerics.BigInteger, ByVal Val2 As _
         Numerics.BigInteger)

      Numerator = Val1
      Denumerator = Val2

      If Denumerator < 0 Then

         Numerator *= -1
         Denumerator *= -1

      End If

      Dim fFract As New Fraction

      fFract.Numerator = Numerator
      fFract.Denumerator = Denumerator

      fFract = Min(fFract)

      Numerator = fFract.Numerator
      Denumerator = fFract.Denumerator

   End Sub

   Public Property Numerator() As Numerics.BigInteger

      Get
         Return biNumerator

      End Get

      Set(ByVal value As Numerics.BigInteger)

         biNumerator = value

      End Set

   End Property


   Public Property Denumerator() As Numerics.BigInteger

      Get

         Return biDenumerator

      End Get

      Set(ByVal value As Numerics.BigInteger)

         biDenumerator = value

      End Set

   End Property


   Public Overloads Shared Operator -(ByVal Op1 As Fraction)

      Dim fRes As New Fraction(-1 * Op1.Numerator, Op1.Denumerator)

      fRes = Min(fRes)

      Return fRes
   End Operator

   Public Overloads Shared Operator +(ByVal Op1 As Fraction, _
         ByVal Op2 As Fraction)

      Dim fNumer As New Fraction
      Dim fDenumer As New Fraction

      fNumer.Numerator = Op1.Numerator * Op2.Denumerator
      fNumer.Denumerator = Op1.Denumerator * Op2.Denumerator

      fDenumer.Numerator = Op2.Numerator * Op1.Denumerator
      fDenumer.Denumerator = Op2.Denumerator * Op1.Denumerator

      Dim fRes As New Fraction(fNumer.Numerator + _
         fDenumer.Numerator, fNumer.Denumerator)

      fRes = Min(fRes)

      Return fRes

   End Operator

   Public Overloads Shared Operator +(ByVal Op1 As _
         System.Numerics.BigInteger, ByVal Op2 As Fraction)

      Dim fNumer As New Fraction
      Dim fDenumer As New Fraction

      fNumer.Numerator = Op1 * Op2.Denumerator
      fNumer.Denumerator = Op2.Denumerator

      fDenumer.Numerator = Op2.Numerator
      fDenumer.Denumerator = Op2.Denumerator

      Dim fRes As New Fraction(fNumer.Numerator + _
         fDenumer.Numerator, fNumer.Denumerator)

      fRes = Min(fRes)

      Return fRes

   End Operator

   Public Overloads Shared Operator ^(ByVal Op1 As Fraction, _
         ByVal Op2 As Integer)

      Dim fRes As New Fraction(1, 1)

      For i As Integer = 0 To Op2 - 1

         fRes *= Op1

      Next

      fRes = Min(fRes)

      Return fRes

   End Operator

   Public Overloads Shared Operator -(ByVal Op1 As Fraction, _
         ByVal Op2 As Fraction)

      Dim fNumer As New Fraction
      Dim fDenumer As New Fraction

      fNumer.Numerator = Op1.Numerator * Op2.Denumerator
      fNumer.Denumerator = Op1.Denumerator * Op2.Denumerator

      fDenumer.Numerator = Op2.Numerator * Op1.Denumerator
      fDenumer.Denumerator = Op2.Denumerator * Op1.Denumerator

      Dim fRes As New Fraction(fNumer.Numerator - _
         fDenumer.Numerator, fNumer.Denumerator)

      fRes = Min(fRes)

      Return fRes

   End Operator

   Public Overloads Shared Operator -(ByVal Op1 As _
         Numerics.BigInteger, ByVal Op2 As Fraction)

      Dim fNumer As New Fraction
      Dim fDenumer As New Fraction

      fNumer.Numerator = Op1 * Op2.Denumerator
      fNumer.Denumerator = 1 * Op2.Denumerator

      fDenumer.Numerator = Op2.Numerator
      fDenumer.Denumerator = Op2.Denumerator

      Dim fRes As New Fraction(fNumer.Numerator - _
         fDenumer.Numerator, fNumer.Denumerator)

      fRes = Min(fRes)

      Return fRes

   End Operator

   Public Overloads Shared Operator -(ByVal Op1 As Fraction, _
         ByVal Op2 As Numerics.BigInteger)

      Dim fNumer As New Fraction
      Dim fDenumer As New Fraction

      fNumer.Numerator = Op1.Numerator
      fNumer.Denumerator = Op1.Denumerator

      fDenumer.Numerator = Op2 * Op1.Denumerator
      fDenumer.Denumerator = Op1.Denumerator

      Dim fRes As New Fraction(fNumer.Numerator - _
         fDenumer.Numerator, fNumer.Denumerator)

      fRes = Min(fRes)

      Return fRes

   End Operator

   Public Overloads Shared Operator *(ByVal Op1 As Fraction, _
         ByVal Op2 As Fraction)

      Dim fRes As New Fraction(Op1.Numerator * Op2.Numerator, _
         Op1.Denumerator * Op2.Denumerator)

      fRes = Min(fRes)

      Return fRes

   End Operator

   Public Overloads Shared Operator /(ByVal Op1 As Fraction, _
         ByVal Op2 As Fraction)

      Dim intTemp As Integer = 1

      Dim biTemp As New Numerics.BigInteger

      If Op2.Numerator < 0 Then

         intTemp = -1
         biTemp = Op2.Numerator * -1

      Else

         biTemp = Op2.Numerator

      End If

      Dim fRes As New Fraction(intTemp * Op1.Numerator * _
         Op2.Denumerator, Op1.Denumerator * biTemp)

      Return fRes

   End Operator

   Public Overloads Shared Operator /(ByVal Op1 As Double, _
         ByVal Op2 As Fraction)

      Dim intTemp As Integer = 1

      Dim biTemp As New Numerics.BigInteger

      If Op2.Numerator < 0 Then

         intTemp = -1
         biTemp = Op2.Numerator * -1

      Else

         biTemp = Op2.Numerator

      End If

      Dim fRes As New Fraction(intTemp * Op1 * Op2.Denumerator, _
         biTemp)

      Return fRes

   End Operator

   Private Shared Function Min(ByVal fFract As Fraction) _
         As Fraction

      Dim biDivisor As New Numerics.BigInteger

      Do

         biDivisor = Numerics.BigInteger.GreatestCommonDivisor _
            (fFract.Numerator, fFract.Denumerator)

         If biDivisor = 0 Then Exit Do

         fFract.Numerator /= biDivisor
         fFract.Denumerator /= biDivisor

      Loop Until biDivisor = 1

      Return fFract

   End Function

   Public Function ToDouble() As Double

      Dim intNumDigits As Integer
      Dim intDenumDigits As Integer
      Dim intMax As Integer

      intNumDigits = GetDigits(Numerator)
      intDenumDigits = GetDigits(Denumerator)

      intMax = CInt(Math.Log10(Double.MaxValue)) - 1

      Dim dblResult As Double

      If intMax > intDenumDigits And intMax > intNumDigits Then

         dblResult = CDbl(Numerator) / CDbl(Denumerator)

      Else

         If intMax < intNumDigits And intMax < intDenumDigits Then

            Dim intOver, intBelow As Integer

            intOver = intNumDigits - intMax
            intBelow = intDenumDigits - intMax

            Numerator /= 10 ^ intOver
            Denumerator /= 10 ^ intBelow

            dblResult = CDbl(Numerator) / CDbl(Denumerator)

            dblResult *= 10 ^ (intOver - intBelow)

         ElseIf intMax < intDenumDigits Then

            Dim intBelow As Integer

            intBelow = intDenumDigits - intMax
            Denumerator /= 10 ^ intBelow

            dblResult = CDbl(Numerator) / CDbl(Denumerator)
            dblResult *= 10 ^ (intBelow)

         ElseIf intMax < intNumDigits Then

            Dim intOver As Integer

            intOver = intNumDigits - intMax

            Numerator /= 10 ^ intOver

            dblResult = CDbl(Numerator) / CDbl(Denumerator)

            dblResult *= 10 ^ (intOver)

         End If

      End If

      Return dblResult

   End Function

   Private Function GetDigits(ByVal biDigit As _
         Numerics.BigInteger) As Integer

      Dim intDigits As Integer

      If biDigit < 0 Then

         biDigit *= -1

      End If

      If biDigit = 0 Then

         intDigits = 1

      Else
         intDigits = Numerics.BigInteger.Log10(biDigit) + 1

      End If

      Return intDigits

   End Function

   Public Overrides Function ToString() As String

      Dim strRes As String

      If Numerator = 0 Then

         strRes = "0"

      ElseIf Me.Denumerator = 1 Then

         strRes = Numerator.ToString

      Else

         strRes = Numerator.ToString & "/" & _
            Denumerator.ToString

      End If

      Return strRes

   End Function

End Class

Add the FunctionMatrix Class that tests for supplied equations.

Public Structure FractionsMatrix

   Public intRows As Integer
   Public intCols As Integer

   Private fMatrix(,) As Fraction

   Private Shared intDeterminant As Integer = 1

   Public Sub New(ByVal iRows As Integer, ByVal iCols As Integer)

      intRows = iRows
      intCols = iCols

      Dim fFraction(iRows - 1, iCols - 1) As Fraction

      fMatrix = fFraction

      For i As Integer = 0 To iRows - 1

         For j As Integer = 0 To iCols - 1

            fMatrix(i, j) = New Fraction(0, 1)

         Next

      Next

   End Sub

   Public Shared Function Sort(ByVal A As FractionsMatrix, B As _
         FractionsMatrix, ByVal iIndex As Integer) As Fraction

      Dim iRows As Integer
      iRows = A.intRows

      Dim dblMax As Double = 0

      Dim j As Integer = iIndex

      Do

         If Math.Abs(A(j, iIndex).ToDouble) < Math.Abs(A(j + 1, _
               iIndex).ToDouble) Then

            A.SwapRows(j, j + 1)
            B.SwapRows(j, j + 1)

            j = iIndex

         Else

            j += 1

         End If

      Loop Until j = iRows - 1

      Return A(iIndex, iIndex)

   End Function

   Public Shared Function Sort(ByVal A As FractionsMatrix, ByVal 
         iIndex As Integer) As Fraction

      Dim iRows As Integer
      iRows = A.intRows

      Dim dblMax As Double = 0

      Dim j As Integer = iIndex

      Do

         If Math.Abs(A(j, iIndex).ToDouble) < Math.Abs(A(j + 1, _
               iIndex).ToDouble) Then

            A.SwapRows(j, j + 1)

            intDeterminant *= -1

            j = iIndex

         Else

            j += 1

         End If

      Loop Until j = iRows - 1

      Return A(iIndex, iIndex)

   End Function

   Default Public Property Item(ByVal iRow As Integer, ByVal iCol _
         As Integer) As Object

      Get

         Return fMatrix(iRow, iCol)

      End Get

      Set(ByVal value As Object)

         If TypeOf (value) Is Fraction Then

            fMatrix(iRow, iCol) = value

         ElseIf TypeOf (value) Is Double Then

            Dim fFract As New Fraction(DirectCast(value, Double))

            fMatrix(iRow, iCol) = fFract

         ElseIf TypeOf (value) Is Decimal Then

            Dim fFract As New Fraction(DirectCast(value, Decimal))

            fMatrix(iRow, iCol) = fFract

         ElseIf TypeOf (value) Is Integer Then

            Dim fFract As New Fraction(DirectCast(value, _
               Integer), 1)

            fMatrix(iRow, iCol) = fFract

         ElseIf TypeOf (value) Is System.Numerics.BigInteger Then

            Dim fFract As New Fraction(DirectCast(value, _
               System.Numerics.BigInteger), 1)

            fMatrix(iRow, iCol) = fFract

         Else

            Throw New Exception("Unsupported")

         End If

      End Set

   End Property

   Public Sub SwapRows(ByVal iOld As Integer, iNew As Integer)

      For i As Integer = 0 To intCols - 1

         Dim fFract As New Fraction

         fFract = Item(iNew, i)

         Item(iNew, i) = Item(iOld, i)

         Item(iOld, i) = fFract

      Next

   End Sub

   Public Shared Operator /(ByVal Op1 As Double, ByVal Op2 As _
         FractionsMatrix) As FractionsMatrix

      Dim fmRes As New FractionsMatrix(Op2.intRows, Op2.intCols)

      Dim fFract As New Fraction(Op1)

      For i As Integer = 0 To Op2.intRows - 1

         For j As Integer = 0 To Op2.intCols - 1

            fmRes.Item(i, j) = fFract / Op2.Item(i, j)

         Next

      Next

      Return fmRes

   End Operator

   Public Function GaussElimination(ByVal fmVector As 
         FractionsMatrix) As FractionsMatrix

      If Not (intCols = fmVector.intRows And fmVector.intCols 
            = 1) Then

         If intCols <> fmVector.intRows And fmVector.intCols 
               <> 1 Then

            Throw New Exception("Too many columns in the matrix.")

         ElseIf intCols <> fmVector.intRows Then

            Throw New Exception("The number of columns is _
               different from the number of rows.")

         End If

      End If

      Dim fmFract As New FractionsMatrix(intRows, intCols)

      For i As Integer = 0 To Me.intRows - 2

         Sort(Me, fmVector, i)

      Next

      For i As Integer = 0 To intRows - 1

         For j As Integer = 0 To intCols - 1

            fmFract.Item(i, j) = Me.Item(i, j)

         Next

      Next

      Dim fmResult, fmMatrix As New FractionsMatrix(intRows, 1)

      For i As Integer = 0 To intRows - 1

         fmMatrix.Item(i, 0) = fmVector(i, 0)

      Next

      Dim tmpFract, fSum As New Fraction

      Dim blnZero As Boolean = False

      For f As Integer = 0 To intRows - 1

         If f <> 0 Then

            For i As Integer = 0 To intRows - 1

               For j As Integer = 0 To intCols - 1

                  fmFract.Item(i, j) = (Me.Item(i, j))

               Next

            Next

            For i As Integer = 0 To intRows - 1

               fmMatrix.Item(i, 0) = (fmVector(i, 0))

            Next

            fmFract.SwapRows(0, f)
            fmMatrix.SwapRows(0, f)

            blnZero = False

         End If

         For k As Integer = 0 To intRows - 2

            For i As Integer = k + 1 To intRows - 1

               tmpFract = fmFract.Item(i, k) / fmFract.Item(k, k)

               For j As Integer = 0 To intRows - 1

                  fmFract(i, j) -= tmpFract * fmFract.Item(k, j)

               Next

               fmMatrix(i, 0) = fmMatrix(i, 0) - tmpFract * _
                  fmMatrix(k, 0)

            Next

            If Sort(fmFract, fmMatrix, k).ToDouble = 0 Then

               blnZero = True

               Exit For

            End If
         Next

         If blnZero = False Then

            Exit For

         End If

     Next

     For i As Integer = intRows - 1 To 0 Step -1

        fSum = New Fraction()
        fmResult(i, 0) = New Fraction

        For k As Integer = i + 1 To intRows - 1

           fSum += fmFract(i, k) * fmResult(k, 0)

        Next

        fmResult(i, 0) = (fmMatrix(i, 0) - fSum) / fmFract(i, i)

      Next

      Return fmResult

   End Function


   Public Function Determinate() As Double

      intDeterminant = 1

      If Not (intCols = intRows) Then

         Throw New Exception("Too many columns in the matrix.")

      End If

      If IsZero(Me) Then

         Return 0

      End If

      Dim fFract As New FractionsMatrix(intRows, intCols)

      For i As Integer = 0 To Me.intRows - 2

         Sort(Me, i)

      Next

      For i As Integer = 0 To intRows - 1

         For j As Integer = 0 To intCols - 1

            fFract.Item(i, j) = Me.Item(i, j)

         Next

      Next

      Dim tmpFract, fSum As New Fraction

      Dim blnZero As Boolean = False

      For f As Integer = 0 To intRows - 1

         If f <> 0 Then

            For i As Integer = 0 To intRows - 1

               For j As Integer = 0 To intCols - 1

                  fFract.Item(i, j) = (Me.Item(i, j))

               Next

            Next

            intDeterminant *= -1
            fFract.SwapRows(0, f)

            blnZero = False

         End If

         For k As Integer = 0 To intRows - 2

            For i As Integer = k + 1 To intRows - 1

               tmpFract = fFract.Item(i, k) / fFract.Item(k, k)

               For j As Integer = 0 To intRows - 1

                  fFract(i, j) -= tmpFract * fFract.Item(k, j)

               Next

            Next

            If Sort(fFract, k).ToDouble = 0 Then

               blnZero = True

               Exit For

            End If

         Next

         If blnZero = False Then

            Exit For

         End If

      Next

      Dim fRes As New Fraction(1, 1)

      For i As Integer = 0 To intCols - 1

         fRes *= fFract.Item(i, i)

      Next

      Return fRes.ToDouble * intDeterminant

   End Function

   Public Function IsZero(ByVal a As FractionsMatrix) As Boolean

      Dim blnRes As Boolean = True

      For i As Integer = 0 To a.intCols - 1

         blnRes = True

         For j As Integer = 0 To a.intRows - 1

            If a.Item(j, i).todouble <> 0 Then

               blnRes = False

            End If

         Next

         If blnRes Then

            Return True

         End If

      Next

      For i As Integer = 0 To a.intRows - 1

         blnRes = True

         For j As Integer = 0 To a.intCols - 1

            If a.Item(i, j).todouble <> 0 Then

               blnRes = False

            End If

         Next

         If blnRes Then

            Return True

         End If

      Next

      Return blnRes

   End Function

   Public Shared Operator /(ByVal Op2 As FractionsMatrix, _
         ByVal Op1 As Double) As FractionsMatrix

      Dim fmRes As New FractionsMatrix(Op2.intRows, Op2.intCols)

      Dim fmFract As New Fraction(Op1)

      For i As Integer = 0 To Op2.intRows - 1

         For j As Integer = 0 To Op2.intCols - 1

            fmRes.Item(i, j) = Op2.Item(i, j) / fmFract

         Next

      Next

      Return fmRes

   End Operator

   Public Shared Operator *(ByVal Op1 As Double, ByVal Op2 As _
         FractionsMatrix) As FractionsMatrix

      Dim fmRes As New FractionsMatrix(Op2.intRows, Op2.intCols)

      Dim fFract As New Fraction(Op1)

      For i As Integer = 0 To Op2.intRows - 1

         For j As Integer = 0 To Op2.intCols - 1

            fmRes.Item(i, j) = fFract * Op2.Item(i, j)

         Next

      Next

      Return fmRes

   End Operator

   Public Shared Operator *(ByVal Op2 As FractionsMatrix, ByVal _
         Op1 As Double) As FractionsMatrix

      Dim fmRes As New FractionsMatrix(Op2.intRows, Op2.intCols)

      Dim fFract As New Fraction(Op1)

      For i As Integer = 0 To Op2.intRows - 1

         For j As Integer = 0 To Op2.intCols - 1

            fmRes.Item(i, j) = fFract * Op2.Item(i, j)

         Next

      Next

      Return fmRes

   End Operator

   Public Shared Operator *(ByVal Op1 As FractionsMatrix, ByVal _
         Op2 As FractionsMatrix) As FractionsMatrix

      If Op1.intCols <> Op2.intRows Then

         Throw New Exception("Size Mismatch")

      End If

      Dim fmRes As New FractionsMatrix(Op1.intRows, Op2.intCols)

      For i As Integer = 0 To Op1.intRows - 1

         For j As Integer = 0 To Op2.intCols - 1

            For k As Integer = 0 To Op2.intRows - 1

               fmRes.Item(i, j) += Op1.Item(i, k) * Op2.Item(k, j)

            Next

         Next

      Next

      Return fmRes

   End Operator

   Public Overloads Overrides Function ToString() As String

      Dim strMatrix As String = "("

      For i As Integer = 0 To intRows - 1

         Dim strTemp As String = ""

         For j As Integer = 0 To intCols - 2

            strTemp &= fMatrix(i, j).ToString() & ", "

         Next

         strTemp &= fMatrix(i, intCols - 1).ToString() & _
            Environment.NewLine

         If i <> intRows - 1 AndAlso i = 0 Then

            strMatrix &= strTemp & Environment.NewLine

         ElseIf i <> intRows - 1 AndAlso i <> 0 Then

            strMatrix &= " " & strTemp & Environment.NewLine

         Else

            strMatrix &= " " & strTemp & ")"

         End If

      Next

      Return strMatrix

   End Function

End Structure

Lastly, feed the matrix on Form 1:

Public Class Form1
   Private Sub btnDeterminant_Click(sender As Object, e As _
         EventArgs) Handles btnDeterminant.Click

      Dim dblRes As Double

      Dim fmEquation As New FractionsMatrix(3, 3)


      fmEquation.Item(0, 0) = CDbl(txtInput1_1.Text)
      fmEquation.Item(0, 1) = CDbl(txtInput1_2.Text)
      fmEquation.Item(0, 2) = CDbl(txtInput1_3.Text)

      fmEquation.Item(1, 0) = CDbl(txtInput2_1.Text)
      fmEquation.Item(1, 1) = CDbl(txtInput2_2.Text)
      fmEquation.Item(1, 2) = CDbl(txtInput2_3.Text)

      fmEquation.Item(2, 0) = CDbl(txtInput3_1.Text)
      fmEquation.Item(2, 1) = CDbl(txtInput3_2.Text)
      fmEquation.Item(2, 2) = CDbl(txtInput3_3.Text)

      dblRes = fmEquation.Determinate

      MessageBox.Show(dblRes)

   End Sub

   Private Sub btnCalculate_Click(sender As Object, e As _
         EventArgs) Handles btnCalculate.Click

      Dim fmEquation As New FractionsMatrix(3, 3)
      Dim fmVector As New FractionsMatrix(3, 1)

      Dim fmRes As New FractionsMatrix

      fmEquation.Item(0, 0) = CDbl(txtInput1_1.Text)
      fmEquation.Item(0, 1) = CDbl(txtInput1_2.Text)
      fmEquation.Item(0, 2) = CDbl(txtInput1_3.Text)

      fmEquation.Item(1, 0) = CDbl(txtInput2_1.Text)
      fmEquation.Item(1, 1) = CDbl(txtInput2_2.Text)
      fmEquation.Item(1, 2) = CDbl(txtInput2_3.Text)

      fmEquation.Item(2, 0) = CDbl(txtInput3_1.Text)
      fmEquation.Item(2, 1) = CDbl(txtInput3_2.Text)
      fmEquation.Item(2, 2) = CDbl(txtInput3_3.Text)

      fmVector.Item(0, 0) = CDbl(txtVector1.Text)
      fmVector.Item(1, 0) = CDbl(txtVector2.Text)
      fmVector.Item(2, 0) = CDbl(txtVector3.Text)

      fmRes = fmEquation.GaussElimination(fmVector)

      txtResults1.Text = fmRes.Item(0, 0).ToString
      txtResults2.Text = fmRes.Item(1, 0).ToString
      txtResults3.Text = fmRes.Item(2, 0).ToString

   End Sub

End Class

Run this app and enter some test values. A resulting Window resembling Figure 2 will be shown.

Running
Figure 2: Running

Conclusion

In this article, you have learned how to set up linear equations by using the Gaussian Elimination method. There are many more ways to deal with linear equations; hopefully, I can show them to you in future articles, but until then, happy coding!



About the Author

Hannes DuPreez

Hannes du Preez is an ex MVP for Visual Basic from 2008 to 2017. He loves technology and loves Visual Basic and C#. He loves writing articles and proving that Visual Basic is more powerful than what most believe. You are most welcome to reach him at: ojdupreez1978[at]gmail[dot]com

Related Articles

Comments

  • There are no comments yet. Be the first to comment!

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date