# Pseudo-inverse and the backslash operator

**URL:** https://discourse.julialang.org/t/pseudo-inverse-and-the-backslash-operator/27882
**Category:** Optimization (Mathematical)
**Created:** [August 23, 2019, 9:43am UTC](https://discourse.julialang.org/t/pseudo-inverse-and-the-backslash-operator/27882 "2019-08-23T09:43:28Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![gmouts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gmouts/32/9896_2.png) [@gmouts](https://discourse.julialang.org/u/gmouts)
#### Post date: [August 23, 2019, 9:43am UTC](https://discourse.julialang.org/t/pseudo-inverse-and-the-backslash-operator/27882/1 "2019-08-23T09:43:28Z")

</div>

So, I have a sparse matrix `A` and a vector `b` and I want to compute `pinv(A)*b`.

After reading this [post](https://discourse.julialang.org/t/moore-penrose-generalized-inverse-of-sparse-matrix/17414/2) I tried the following:

```julia
    using LinearAlgebra

    julia> A = [1 1 1; 2 2 2; 1 0 1]
    3×3 Array{Int64,2}:
     1 1 1
     2 2 2
     1 0 1
    
    julia> b = [1,0,0]
    3-element Array{Int64,1}:
     1
     0
     0
    
    julia> pinv(A)
    3×3 Array{Float64,2}:
     -2.22045e-16 2.77556e-17 0.5
      0.2 0.4 -1.0
     -1.52656e-16 1.66533e-16 0.5
    
    julia> pinv(A)*b
    3-element Array{Float64,1}:
     -2.220446049250313e-16
      0.20000000000000048
     -1.5265566588595902e-16

```

Everything is fine up to this point, but when I try the backslash operator i get:

```julia
    julia> qr(A)\b
    3-element Array{Float64,1}:
     -1.8385868488076965e15
     -1.8242825833464351e-16
      1.838586848807696e15

```

I’m not sure what happened here. I don’t understand what the result is supposed to be, but clearly it is not `pinv(A)*b`. Am I using this wrongly or is it a bug?

---

<div class="post-metadata">

### Author: ![saschatimme](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/saschatimme/32/10313_2.png) [@saschatimme](https://discourse.julialang.org/u/saschatimme)
#### Post date: [August 23, 2019, 9:52am UTC](https://discourse.julialang.org/t/pseudo-inverse-and-the-backslash-operator/27882/2 "2019-08-23T09:52:35Z")

</div>

Using a pivoted QR factorization will give you the result you are looking for:

```julia
julia> qr(A, Val(true)) \ b
3-element Array{Float64,1}:
 -2.379544922143407e-16 
  0.20000000000000057   
 -2.1619865292617238e-16

```

The standard QR algorithm only gives the same result as a multiplication with the pseudo-inverse if the matrix has full column rank.

---

<div class="post-metadata">

### Author: ![gmouts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gmouts/32/9896_2.png) [@gmouts](https://discourse.julialang.org/u/gmouts)
#### Post date: [August 23, 2019, 10:01am UTC](https://discourse.julialang.org/t/pseudo-inverse-and-the-backslash-operator/27882/3 "2019-08-23T10:01:52Z")

</div>

That works, thanks! However, I’m a bit perplexed by this. What does `qr(A)\b` actually do? What is the result?

---

<div class="post-metadata">

### Author: ![saschatimme](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/saschatimme/32/10313_2.png) [@saschatimme](https://discourse.julialang.org/u/saschatimme)
#### Post date: [August 23, 2019, 10:31am UTC](https://discourse.julialang.org/t/pseudo-inverse-and-the-backslash-operator/27882/4 "2019-08-23T10:31:44Z")

</div>

I think it solve the linear system Rx=Q^Tb but assumes that R has full rank. In the algorithm for the pivoted QR is an explicit check on the rank of R (which is also easier to do there) and then computes a least square solution for the system.
