Questions tagged [matrix-inverse]

The matrix inverse, A^{-1}, is a mathematical relationship such that given a square n x n matrix A, A*A^{-1} = A^{-1}*A = I, where I is the identity matrix. Use this tag with regards to any numerical methods or computations that require the use or calculation of the matrix inverse.

Computation of the inverse of a square matrix, provided it is invertible (i.e., full-rank), is often via LU factorization. When the matrix is positive-definite, Cholesky factorization is often used. In standard numerical linear algebra library , dgesv and dpotrf respectively performs LU and Cholesky factorization.

In reality it is rare that a matrix inverse needs be explicitly formed, and matrix multiplications involving a matrix inverse is done by one of the factorizations above, and a triangular system solving.

509 questions
106
votes
4 answers

Inverse of matrix in R

I was wondering what is your recommended way to compute the inverse of a matrix? The ways I found seem not satisfactory. For example, > c=rbind(c(1, -1/4), c(-1/4, 1)) > c [,1] [,2] [1,] 1.00 -0.25 [2,] -0.25 1.00 > inv(c) …
Tim
  • 1
  • 141
  • 372
  • 590
94
votes
11 answers

Inverting a 4x4 matrix

I am looking for a sample code implementation on how to invert a 4x4 matrix. I know there is Gaussian eleminiation, LU decomposition, etc., but instead of looking at them in detail I am really just looking for the code to do this. Language ideally…
clamp
  • 33,000
  • 75
  • 203
  • 299
62
votes
7 answers

Python Inverse of a Matrix

How do I get the inverse of a matrix in python? I've implemented it myself, but it's pure python, and I suspect there are faster modules out there to do it.
Claudiu
  • 224,032
  • 165
  • 485
  • 680
44
votes
12 answers

Simple 3x3 matrix inverse code (C++)

What's the easiest way to compute a 3x3 matrix inverse? I'm just looking for a short code snippet that'll do the trick for non-singular matrices, possibly using Cramer's rule. It doesn't need to be highly optimized. I'd prefer simplicity over…
batty
  • 7,528
  • 9
  • 31
  • 30
24
votes
4 answers

Computing the inverse of a matrix using lapack in C

I would like to be able to compute the inverse of a general NxN matrix in C/C++ using lapack. My understanding is that the way to do an inversion in lapack is by using the dgetri function, however, I can't figure out what all of its arguments are…
D R
  • 21,936
  • 38
  • 112
  • 149
23
votes
4 answers

Three.js: Show world coordinate axes in corner of scene

I'd like to show arrows indicating the world coordinate directions (x, y, z) in the bottom right hand corner of the camera like is done in Maya, so that when rotating the camera around an object, or moving through a scene, you can still identify the…
22
votes
5 answers

Easiest way to perform modular matrix inversion with Python?

I'd like to take the modular inverse of a matrix like [[1,2],[3,4]] mod 7 in Python. I've looked at numpy (which does matrix inversion but not modular matrix inversion) and I saw a few number theory packages online, but nothing that seems to do this…
John
  • 341
  • 1
  • 2
  • 7
20
votes
3 answers

Improving a badly conditioned matrix

I have a badly conditioned matrix, whose rcond() is close to zero, and therefore, the inverse of that matrix does not come out to be correct. I have tried using pinv() but that does not solve the problem. This is how I am taking the inverse: X =…
user238469
19
votes
5 answers

Large Matrix Inversion

I am looking at taking the inverse of a large matrix, common size of 1000 x 1000, but sometimes exceeds 100000 x 100000 (which is currently failing due to time and memory). I know that the normal sentiment is 'don't take the inverse, find some other…
18
votes
11 answers

Java inverse matrix calculation

I'm trying to calculate the inverse matrix in Java. I'm following the adjoint method (first calculation of the adjoint matrix, then transpose this matrix and finally, multiply it for the inverse of the value of the determinant). It works when the…
dedalo
  • 2,541
  • 12
  • 32
  • 34
16
votes
2 answers

numpy: inverting an upper triangular matrix

In numpy/scipy, what's the canonical way to compute the inverse of an upper triangular matrix? The matrix is stored as 2D numpy array with zero sub-diagonal elements, and the result should also be stored as a 2D array. edit The best I've found so…
NPE
  • 486,780
  • 108
  • 951
  • 1,012
16
votes
1 answer

Moore-Penrose generalized inverse of a large sparse matrix

I have a square matrix with a few tens of thousands rows and columns with only a few 1 beside tons of 0, so I use the Matrix package to store that in R in an efficient way. The base::matrix object cannot handle that amount of cells, as I run out of…
daroczig
  • 28,004
  • 7
  • 90
  • 124
16
votes
3 answers

Why is Matlab's inv slow and inaccurate?

I read at a few places (in the doc and in this blog post : http://blogs.mathworks.com/loren/2007/05/16/purpose-of-inv/ ) that the use of inv in Matlab is not recommended because it is slow and inaccurate. I am trying to find the reason of this…
16
votes
4 answers

Is there a way to efficiently invert an array of matrices with numpy?

Normally I would invert an array of 3x3 matrices in a for loop like in the example below. Unfortunately for loops are slow. Is there a faster, more efficient way to do this? import numpy as np A = np.random.rand(3,3,100) Ainv = np.zeros_like(A) for…
katrasnikj
  • 3,151
  • 3
  • 16
  • 27
15
votes
7 answers

Left inverse in numpy or scipy?

I am trying to obtain the left inverse of a non-square matrix in python using either numpy or scipy. How can I translate the following Matlab code to Python? >> A = [0,1; 0,1; 1,0] A = 0 1 0 1 1 0 >> y = [2;2;1] y = …
D R
  • 21,936
  • 38
  • 112
  • 149
1
2 3
33 34