Introduction
This function will accept one 3x3 matrix as datatable, validate it and give back the inverse as another datatable. I had intentionally used datatable for easy databinding with gridview
controls, this is a continuation of Matrix multiplication in my previous tip.
Background
In my new engineering project, I came across the requirement of implementing a lot of matrix arithmetic that was previously done using Excel.
Using the Code
For demonstration purposes, I had created a simple form with a gridview
to input 3x3 matrix (please check the image attached).
Then add a Button
to the form and on the button click event, send the data of gridview
to Calculate Inverse as datatable:
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
Dim dt1mat As DataTable = CType((grdMatrix1.DataSource), DataTable)
grdMatrix3.DataSource = MatrixInverse3x3(dt1mat)
End Sub
Please find the Inverse
function below. Please go through the comments for better understanding.
Public Function MatrixInverse3x3(dt As DataTable) As DataTable
Dim dtmyinverse As DataTable = New DataTable
Dim imyasci As Integer = 65
For index As Integer = 0 To 2
dtmyinverse.Columns.Add(Chr(imyasci))
imyasci += 1
Next
For index As Integer = 0 To 2
dtmyinverse.Rows.Add()
Next
Dim a As Decimal = Decimal.Parse(dt.Rows(0)(0).ToString())
Dim b As Decimal = Decimal.Parse(dt.Rows(0)(1).ToString())
Dim c As Decimal = Decimal.Parse(dt.Rows(0)(2).ToString())
Dim d As Decimal = Decimal.Parse(dt.Rows(1)(0).ToString())
Dim e As Decimal = Decimal.Parse(dt.Rows(1)(1).ToString())
Dim f As Decimal = Decimal.Parse(dt.Rows(1)(2).ToString())
Dim g As Decimal = Decimal.Parse(dt.Rows(2)(0).ToString())
Dim h As Decimal = Decimal.Parse(dt.Rows(2)(1).ToString())
Dim i As Decimal = Decimal.Parse(dt.Rows(2)(2).ToString())
Dim min1 As Decimal = a * ((e * i) - (f * h))
Dim min2 As Decimal = b * ((d * i) - (f * g))
Dim min3 As Decimal = c * ((d * h) - (e * g))
Dim det As Decimal = min1 - min2 + min3
det = 1 / det
dtmyinverse.Rows(0)(0) = (dt.Rows(1)(1) * dt.Rows(2)(2) - dt.Rows(2)(1) * dt.Rows(1)(2)) * det
dtmyinverse.Rows(0)(1) = (dt.Rows(0)(2) * dt.Rows(2)(1) - dt.Rows(0)(1) * dt.Rows(2)(2)) * det
dtmyinverse.Rows(0)(2) = (dt.Rows(0)(1) * dt.Rows(1)(2) - dt.Rows(0)(2) * dt.Rows(1)(1)) * det
dtmyinverse.Rows(1)(0) = (dt.Rows(1)(2) * dt.Rows(2)(0) - dt.Rows(1)(0) * dt.Rows(2)(2)) * det
dtmyinverse.Rows(1)(1) = (dt.Rows(0)(0) * dt.Rows(2)(2) - dt.Rows(0)(2) * dt.Rows(2)(0)) * det
dtmyinverse.Rows(1)(2) = (dt.Rows(1)(0) * dt.Rows(0)(2) - dt.Rows(0)(0) * dt.Rows(1)(2)) * det
dtmyinverse.Rows(2)(0) = (dt.Rows(1)(0) * dt.Rows(2)(1) - dt.Rows(2)(0) * dt.Rows(1)(1)) * det
dtmyinverse.Rows(2)(1) = (dt.Rows(2)(0) * dt.Rows(0)(1) - dt.Rows(0)(0) * dt.Rows(2)(1)) * det
dtmyinverse.Rows(2)(2) = (dt.Rows(0)(0) * dt.Rows(1)(1) - dt.Rows(1)(0) * dt.Rows(0)(1)) * det
Return dtmyinverse
End Function
Points of Interest
It will be interesting to do the same for non 3x3 matrix also and I am currently trying to do the same and will upload once completed.
History
- 23rd September, 2018: Initial version