Is there a way to find right-inverse (or left-inverse) of a matrix in Julia?

Here is the matrix A

julia> A = [1 2 3; 0 1 2]
2×3 Matrix{Int64}:
 1  2  3
 0  1  2

Matrix A has full row-rank

julia> size(A, 1) == rank(A)
true

and it has a right-inverse. Can we find the right-inverse of this matrix A in Julia?
Thank you in advance for any suggestion possible.

Is pinv the function you looking for ?

Not actually! Can we find here the matrix B in Julia which can satisfy the definition of right-inverse only? :slight_smile:
i.e. A*B = I

The pseudo-inverse, returned by pinv, is equal to a right inverse (or left inverse), if one exists:

julia> using LinearAlgebra

julia> A = [1 2 3; 0 1 2]
2×3 Matrix{Int64}:
 1  2  3
 0  1  2

julia> A * pinv(A) ≈ I
true

(Edit: Note that right or left inverses are not generally unique, however. Also, in most practical applications, it is a truism of numerical linear algebra that one should avoid computing inverses explicitly if possible.)

3 Likes

Is it unique? Because I was doing just some calculations by hand with an example where found this following matrix

julia> B = [0 1; 2 -5; -1 3]
3×2 Matrix{Int64}:
  0   1
  2  -5
 -1   3

and noticed that

julia> A * B
2×2 Matrix{Int64}:
 1  0
 0  1

julia> A * B == I
true

Now it satisfies the definition of right-inverse and B is actually the right-inverse of the matrix A and quite different from

julia> pinv(A)
3×2 Matrix{Float64}:
  0.833333  -1.33333
  0.333333  -0.333333
 -0.166667   0.666667

Is there a way to find the matrix B by doing any operation on A?

from the rank-nullity theorem, in this case there is a 1 dimensional vector-space of solutions.

With A*X = I, I would think that X = A\I should give the right inverse??

Is it unique? As far as I remember one of Gilbert Strang’s books on linear algebra, if the system has a non-trivial null space, the solution is chosen such that some norm of X is minimized (at least in the pseudoinverse case). This minimal norm criterion probably makes the solution unique, but I don’t know whether this carries over to right/left inverses.

2 Likes

The pseudo inverse is unique. Right inverses (for non-square matrices) are not. (That’s because Ax=b does not have unique solutions for a “wide” matrix: the problem is underdetermined.)

If you additionally request the minimum-norm solution (which is a choice, and may or may not be the best choice for a given application), that gives a unique solution and a unique right inverse equal to the pseudo inverse.

Or just use x = A \ b, which gives the minimum norm solution without explicitly computing the right/pseudo inverse matrix.

4 Likes

You first have to precisely define which right inverse you want, if you don’t want the pseudo inverse. (Equivalently, define which solution of Ax=b you want, if you don’t want the minimum norm solution,)

This would apply to each column of the resulting matrix, right? So together the solution matrix space would have 2 dims?

1 Like

So I guess the following function:

julia> function rinv(A)
              m,n = size(A)
              iA0 = A\I(m)
              nA = nullspace(A)
              return iA0, nA
         end

returns the pseudo inverse and the nullspace basis for A.

Thus in this simple case, iA0 + nA*z' where z is an arbitrary vector of compatible length should give a right inverse. E.g.,

julia> using LinearAlgebra
julia> A = [1 2 3; 0 1 2]
2×3 Matrix{Int64}:
 1  2  3
 0  1  2
julia> iA0,nA = rinv(A);
julia> A*iA0
2×2 Matrix{Float64}:
  1.0          5.55112e-16
 -1.66533e-16  1.0

julia> A*(iA0 + nA*[1 3])
2×2 Matrix{Float64}:
  1.0          1.33227e-15
 -2.77556e-16  1.0
2 Likes