Results 1 to 8 of 8

Thread: How to find Matrix Inverse in VB6?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2008
    Posts
    1,238

    How to find Matrix Inverse in VB6?

    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.

  2. #2
    New Member
    Join Date
    Feb 2011
    Posts
    13

    Re: How to find Matrix Inverse in VB6?

    Ben,
    I really don't know what you are doing mathematically, but as I look at the code, this is the section I'd check on:
    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)

    All of your other references are for matrices from 0 to 2, and the first two lines have a pattern of 1,0 -> 0,1; 0,1 -> 1,0
    But the third line has 2,1 -> 1,2; 1,1 -> 2,1
    seems like that line to follow the pattern would be 2,1 -> 1,2; 1,2 -> 2,1
    but then I would also expect one of the lines to be 0,0 -> 0,0; 0,0 -> 0,0 or something...
    like i said, I don't understand the math, I'm just doing the monkey see, monkey do look at your code. If I'm totally off base please just ignore me...
    Thanks
    Steve
    Ben - I just tried to read the article on the matrix and I think my head broke... 40 years ago this probably made sense to me...
    Last edited by Stevevb99; Nov 23rd, 2012 at 03:59 PM. Reason: expansion of explanation...

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2008
    Posts
    1,238

    Re: How to find Matrix Inverse in VB6?

    Thanks. I think there must have been a typo there.

    Update:
    Hmm. Just tried it and it still isn't working. Yet I can't see what I did wrong.
    Last edited by Ben321; Nov 23rd, 2012 at 06:22 PM.

  4. #4
    Frenzied Member
    Join Date
    May 2006
    Location
    some place in the cloud
    Posts
    1,886

    Re: How to find Matrix Inverse in VB6?

    Maybe the attached project could help
    I downloaded several years ago from internet
    Inside the code come the credits
    Attached Files Attached Files
    JG


    ... If your problem is fixed don't forget to mark your threads as resolved using the Thread Tools menu ...

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    2

    Re: How to find Matrix Inverse in VB6?

    Guys, I am new to VB6. Basically, I need to find the inverse of a matrix(taking the input from a 2D array), and also performing matrix multiplication. Later, I would again store it in an array and perform further calculations. Can someone help me out to do that.

    P.S: The input matrix is taken from an array, not from the user.

    I would be glad if someone could help me out with some code snippet.

  6. #6
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: How to find Matrix Inverse in VB6?

    The code to do this, if your started from scratch, would likely be enormous, especially if the dimensions of the original matrix were left as a variable. Consider the Maths forum and look for an existing function. I imagine one was written years ago.

    Surprisingly, during my entire career as a programmer, I never was required to write this code.
    Doctor Ed

  7. #7
    Registered User
    Join Date
    Apr 2013
    Posts
    2

    Re: How to find Matrix Inverse in VB6?

    Thanks. Code Doc.

    I have one another problem.

    "Private Sub cmdCalculate_Click()
    Dim xl As New Excel.Application, xlwbook As Excel.Workbook, xlsheet As Excel.Worksheet"

    I get an error that says "User-defined type not defined" on the above mentioned line. Basically in my project I need to take in values from an excel file (.xls) into an arrayand after manipulation, copy them back to an excel file.

    How could I resolve this error?

  8. #8
    Frenzied Member
    Join Date
    May 2006
    Location
    some place in the cloud
    Posts
    1,886

    Re: How to find Matrix Inverse in VB6?

    Quote Originally Posted by Vikash08 View Post
    Guys, I am new to VB6. Basically, I need to find the inverse of a matrix(taking the input from a 2D array), and also performing matrix multiplication. Later, I would again store it in an array and perform further calculations. Can someone help me out to do that.

    P.S: The input matrix is taken from an array, not from the user.

    I would be glad if someone could help me out with some code snippet.
    Have You tried the attached project in post #4 ?
    JG


    ... If your problem is fixed don't forget to mark your threads as resolved using the Thread Tools menu ...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width