Quantcast
Channel: VBForums - Visual Basic 6 and Earlier
Viewing all articles
Browse latest Browse all 21881

How to find Matrix Inverse in VB6?

$
0
0
I was trying to make a matrix inverse function that calculates the inverse of a matrix (when used with a matrix used in transforms, the inverse matrix can be used to the produce inverse transformation). Here's the code I wrote for finding a 3x3 matrix's inverse. But the resulting inverse matrix doesn't correctly produce an inverse transformation when used in a transformation function. Please help.

Code:

Private Function Inverse3x3(ByRef Matrix() As Double) As Double()
Dim MIn() As Double
Dim MMinors(2, 2) As Double
Dim MMinCof(2, 2) As Double
Dim MMinCofAdj(2, 2) As Double
Dim MOut(2, 2) As Double
Dim Determ As Double
MIn() = Matrix()

MMinors(0, 0) = MIn(1, 1) * MIn(2, 2) - MIn(2, 1) * MIn(1, 2)
MMinors(1, 0) = MIn(0, 1) * MIn(2, 2) - MIn(2, 1) * MIn(0, 2)
MMinors(2, 0) = MIn(0, 1) * MIn(1, 2) - MIn(1, 1) * MIn(0, 2)
MMinors(0, 1) = MIn(1, 0) * MIn(2, 2) - MIn(2, 0) * MIn(1, 2)
MMinors(1, 1) = MIn(0, 0) * MIn(2, 2) - MIn(2, 0) * MIn(0, 2)
MMinors(2, 1) = MIn(0, 0) * MIn(1, 2) - MIn(1, 1) * MIn(0, 2)
MMinors(0, 2) = MIn(1, 0) * MIn(2, 1) - MIn(2, 0) * MIn(1, 1)
MMinors(1, 2) = MIn(0, 0) * MIn(2, 1) - MIn(2, 0) * MIn(0, 1)
MMinors(2, 2) = MIn(0, 0) * MIn(1, 1) - MIn(1, 0) * MIn(0, 1)

For Y = 0 To 2
For X = 0 To 2
If ((X + Y) And 1) = 0 Then MMinCof(X, Y) = MMinors(X, Y) Else MMinCof(X, Y) = -MMinors(X, Y)
Next X
Next Y

MMinCofAdj(1, 0) = MMinCof(0, 1): MMinCofAdj(0, 1) = MMinCof(1, 0)
MMinCofAdj(2, 0) = MMinCof(0, 2): MMinCofAdj(0, 2) = MMinCof(2, 0)
MMinCofAdj(2, 1) = MMinCof(1, 2): MMinCofAdj(1, 1) = MMinCof(2, 1)

Determ = MIn(0, 0) * MMinors(0, 0) - MIn(1, 0) * MMinors(1, 0) + MIn(2, 0) * MMinors(2, 0)

For Y = 0 To 2
For X = 0 To 2
MOut(X, Y) = MMinCofAdj(X, Y) / Determ
Next X
Next Y
Inverse3x3 = MOut()
End Function

My function is based off of the mathematical description of matrix inverses that I found at http://www.mathsisfun.com/algebra/ma...-adjugate.html
No pseudo code was found on that web page, so I had to write mine from scratch. There may be bugs in it, but I thought I did it right. Can someone here please check over my above posted code to see if you can find what's wrong with it. Thanks in advance.

Viewing all articles
Browse latest Browse all 21881

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>