Inverse of a square matrix in C

In summary, the conversation discusses the implementation of an algorithm involving matrix inversions, specifically an extended Kalman filter. The individual is seeking help in writing a C program or finding helpful links. Other users suggest using an existing library like LAPACK and offer advice on efficient algorithms for the given problem. The conversation ends with thanks and plans to try out the suggested solution.
  • #1
SUDHEER87
25
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..!
 
Mathematics news on Phys.org
  • #2
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.
 
  • #3
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:
 
  • #4
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
 
  • #5
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 alot..!
i'll try this in microC software which involves some special commands for matrices and let you know if there is a problem.:blushing:
 

FAQ: Inverse of a square matrix in C

1. What is an inverse of a square matrix in C?

An inverse of a square matrix in C is a matrix that, when multiplied by the original matrix, results in the identity matrix. In other words, it "undoes" the original matrix's operations, similar to how division undoes multiplication.

2. Why is finding the inverse of a square matrix important?

Finding the inverse of a square matrix is important because it allows us to solve systems of linear equations and perform other mathematical operations that would otherwise be difficult or impossible. It is also a fundamental concept in linear algebra and has many applications in fields such as engineering, physics, and computer science.

3. How do you find the inverse of a square matrix in C?

To find the inverse of a square matrix in C, you can use various methods such as Gauss-Jordan elimination, Cramer's rule, or the LU decomposition method. There are also built-in functions and libraries in C that can be used to compute the inverse of a matrix.

4. Can every square matrix in C have an inverse?

No, not every square matrix in C has an inverse. For a square matrix to have an inverse, it must be a non-singular matrix, meaning its determinant is non-zero. If the determinant is zero, the matrix is singular and does not have an inverse.

5. What is the use of the inverse of a square matrix in solving linear equations?

The inverse of a square matrix is used to solve systems of linear equations by multiplying it with the column vector of the constant terms of the equations. This results in a solution vector that satisfies all the equations in the system. In essence, the inverse acts as a "division" operation in solving linear equations.

Similar threads

Replies
19
Views
6K
Replies
6
Views
3K
Replies
4
Views
3K
Replies
7
Views
4K
Replies
2
Views
2K
Replies
7
Views
2K
Replies
18
Views
3K
Replies
1
Views
1K
Back
Top