Pseudo-inverse and the backslash operator

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

After reading this post I tried the following:

    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}:
    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}:

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

    julia> qr(A)\b
    3-element Array{Float64,1}:

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?

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

julia> qr(A, Val(true)) \ b
3-element Array{Float64,1}:

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


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

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.