Inverse of a square matrix in C

  • Context: Undergrad 
  • Thread starter Thread starter SUDHEER87
  • Start date Start date
  • Tags Tags
    Inverse Matrix Square
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
4 replies · 5K views
SUDHEER87
Messages
25
Reaction score
0
Hello all.
Iam trying to implement an algorithm which involves several matrix inversions.The algorithm is an extended kalman filter which is a recursive one.
Can anybody help me in writing a C program to solve the inversion problem or any good links?
thanks in advance..!
 
Physics news on Phys.org
For performance you're probably going to be better off using an existing library like LAPACK which can be linked to from C.

There are lots of algorithms, and the best one depends on the details of your problem. Is it symmetric? Square? Sparse? Large? Numerically unstable? Requires speed? Constrained by memory usage? Etc. Sorry, I don't know anything about extended Kalman filter, but it sounds hard :P

Check out Wikipedia "Invertible matrix" for a few ideas.
 
Unrest said:
For performance you're probably going to be better off using an existing library like LAPACK which can be linked to from C.

There are lots of algorithms, and the best one depends on the details of your problem. Is it symmetric? Square? Sparse? Large? Numerically unstable? Requires speed? Constrained by memory usage? Etc. Sorry, I don't know anything about extended Kalman filter, but it sounds hard :P

Check out Wikipedia "Invertible matrix" for a few ideas.

Thanks for the reply. The algorithm is pretty tough and confusing enough to break heads.
The matrices are dynamic type,3by3 and symmetric.Thanks for the link.:blushing:
 
Oh, 3x3 narrows it down a lot! Here's a quicky that I use. You can probably make it more efficient for symmetric or if you don't need the determinant.

A is the input matrix
B is its inverse

Code:
        'Find determinant
        Det = A(1, 1) * (A(2, 2) * A(3, 3) - A(3, 2) * A(2, 3)) _
            - A(1, 2) * (A(2, 1) * A(3, 3) - A(3, 1) * A(2, 3)) _
            + A(1, 3) * (A(2, 1) * A(3, 2) - A(3, 1) * A(2, 2))

        If NonZero(Det) Then
            'B = Adj(A) / Det(A)
            B(1, 1) = (A(2, 2) * A(3, 3) - A(3, 2) * A(2, 3)) / Det
            B(1, 2) = (A(1, 3) * A(3, 2) - A(3, 3) * A(1, 2)) / Det
            B(1, 3) = (A(1, 2) * A(2, 3) - A(2, 2) * A(1, 3)) / Det

            B(2, 1) = (A(2, 3) * A(3, 1) - A(3, 3) * A(2, 1)) / Det
            B(2, 2) = (A(1, 1) * A(3, 3) - A(3, 1) * A(1, 3)) / Det
            B(2, 3) = (A(1, 3) * A(2, 1) - A(2, 3) * A(1, 1)) / Det

            B(3, 1) = (A(2, 1) * A(3, 2) - A(3, 1) * A(2, 2)) / Det
            B(3, 2) = (A(1, 2) * A(3, 1) - A(3, 2) * A(1, 1)) / Det
            B(3, 3) = (A(1, 1) * A(2, 2) - A(2, 1) * A(1, 2)) / Det
        End If
 
Unrest said:
Oh, 3x3 narrows it down a lot! Here's a quicky that I use. You can probably make it more efficient for symmetric or if you don't need the determinant.

A is the input matrix
B is its inverse

Code:
        'Find determinant
        Det = A(1, 1) * (A(2, 2) * A(3, 3) - A(3, 2) * A(2, 3)) _
            - A(1, 2) * (A(2, 1) * A(3, 3) - A(3, 1) * A(2, 3)) _
            + A(1, 3) * (A(2, 1) * A(3, 2) - A(3, 1) * A(2, 2))

        If NonZero(Det) Then
            'B = Adj(A) / Det(A)
            B(1, 1) = (A(2, 2) * A(3, 3) - A(3, 2) * A(2, 3)) / Det
            B(1, 2) = (A(1, 3) * A(3, 2) - A(3, 3) * A(1, 2)) / Det
            B(1, 3) = (A(1, 2) * A(2, 3) - A(2, 2) * A(1, 3)) / Det

            B(2, 1) = (A(2, 3) * A(3, 1) - A(3, 3) * A(2, 1)) / Det
            B(2, 2) = (A(1, 1) * A(3, 3) - A(3, 1) * A(1, 3)) / Det
            B(2, 3) = (A(1, 3) * A(2, 1) - A(2, 3) * A(1, 1)) / Det

            B(3, 1) = (A(2, 1) * A(3, 2) - A(3, 1) * A(2, 2)) / Det
            B(3, 2) = (A(1, 2) * A(3, 1) - A(3, 2) * A(1, 1)) / Det
            B(3, 3) = (A(1, 1) * A(2, 2) - A(2, 1) * A(1, 2)) / Det
        End If

thanks a lot..!
i'll try this in microC software which involves some special commands for matrices and let you know if there is a problem.:blushing: