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

**URL:** <https://discourse.julialang.org/t/is-there-a-way-to-find-right-inverse-or-left-inverse-of-a-matrix-in-julia/124100>\
**Category:** General Usage\
**Tags:** linearalgebra\
**Created:** [December 22, 2024, 6:44pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-find-right-inverse-or-left-inverse-of-a-matrix-in-julia/124100 "2024-12-22T18:44:02Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![shubh\_b](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shubh_b/32/20697_2.png) [@shubh\_b](https://discourse.julialang.org/u/shubh_b)\
**Post date:** [December 22, 2024, 6:44pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-find-right-inverse-or-left-inverse-of-a-matrix-in-julia/124100/1 "2024-12-22T18:44:02Z")

</div>

Here is the matrix `A`

```julia
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
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.

---

<div class="post-metadata">

**Author:** ![OmarElrefaei](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/omarelrefaei/32/43002_2.png) [@OmarElrefaei](https://discourse.julialang.org/u/OmarElrefaei)\
**Post date:** [December 22, 2024, 6:53pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-find-right-inverse-or-left-inverse-of-a-matrix-in-julia/124100/2 "2024-12-22T18:53:24Z")

</div>

Is `pinv` the function you looking for ?

---

<div class="post-metadata">

**Author:** ![shubh\_b](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shubh_b/32/20697_2.png) [@shubh\_b](https://discourse.julialang.org/u/shubh_b)\
**Post date:** [December 22, 2024, 6:55pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-find-right-inverse-or-left-inverse-of-a-matrix-in-julia/124100/3 "2024-12-22T18:55:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 22, 2024, 7:02pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-find-right-inverse-or-left-inverse-of-a-matrix-in-julia/124100/4 "2024-12-22T19:02:32Z")

</div>

> [@shubh\_b](#):
>
> Not actually!

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

```julia
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.)

---

<div class="post-metadata">

**Author:** ![shubh\_b](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shubh_b/32/20697_2.png) [@shubh\_b](https://discourse.julialang.org/u/shubh_b)\
**Post date:** [December 22, 2024, 7:18pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-find-right-inverse-or-left-inverse-of-a-matrix-in-julia/124100/5 "2024-12-22T19:18:32Z")

</div>

> [@stevengj](#):
>
> `pinv(A)`

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

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

```

and noticed that

```julia
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
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`?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [December 22, 2024, 7:26pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-find-right-inverse-or-left-inverse-of-a-matrix-in-julia/124100/6 "2024-12-22T19:26:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [December 22, 2024, 7:37pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-find-right-inverse-or-left-inverse-of-a-matrix-in-julia/124100/7 "2024-12-22T19:37:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 22, 2024, 7:56pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-find-right-inverse-or-left-inverse-of-a-matrix-in-julia/124100/8 "2024-12-22T19:56:40Z")

</div>

> [@shubh\_b](#):
>
> Is it unique?

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.)

> [@BLI](#):
>
> 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

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.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 22, 2024, 8:08pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-find-right-inverse-or-left-inverse-of-a-matrix-in-julia/124100/9 "2024-12-22T20:08:09Z")

</div>

> [@shubh\_b](#):
>
> Is there a way to find the matrix `B` by doing any operation on `A`?

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,)

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [December 23, 2024, 12:38am UTC](https://discourse.julialang.org/t/is-there-a-way-to-find-right-inverse-or-left-inverse-of-a-matrix-in-julia/124100/10 "2024-12-23T00:38:54Z")

</div>

> [@Oscar\_Smith](#):
>
> in this case there is a 1 dimensional vector-space of solutions

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

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [December 23, 2024, 6:23pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-find-right-inverse-or-left-inverse-of-a-matrix-in-julia/124100/11 "2024-12-23T18:23:41Z")

</div>

So I guess the following function:

```julia
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
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

```
