It seems that the behaviour of matrix division \ is inconsistent if sparse matrices are used:

using LinearAlgebra, SparseArrays
A1=[1 1 ;0 0.0]
B1=[4; 0.0]
x1=A1\B1 #Error: LAPACKException(2)
x2=sparse(A1)\sparse(B1) #Error: SingularException(2)
x3=sparse(A1)\B1 #Error: SingularException(2)
x4=A1\sparse(B1) #works. Why, if A\B doesn't?
A2=[1 0 ;0 0.0]
B2=[4; 0.0]
x1=A2\B2 #gives 4 and NaN - why if A is still triangular and singular?
x2=sparse(A2)\sparse(B2) #gives 4 and NaN
x3=sparse(A2)\B2 #gives 4 and NaN
x4=A2\sparse(B2) #gives 4 and NaN
A3=[1 1; 1 1]
B3=[4; 4]
x1=A3\B3 #Error: SingularException(2)
x2=sparse(A3)\sparse(B3) #Error: MethodError: no method matching lu! #why a different error?
x3=sparse(A3)\B3 #Error: MethodError: no method matching lu!
x4=A3\sparse(B3) #Error: SingularException(2)

Shouldn’t \ work for sparse matrices? Also, why no least-square solutions for a quadratic matrix (as described in Linear Algebra · The Julia Language)

works, while all other combinations of imposed sparsity do not. The reason is perhaps related to the following issue: Request: SPQR backslash operator for sparse right hand sides · Issue #33637 · JuliaLang/julia · GitHub. It says that Julia does not support sparsity on the right hand side (because the underlying engine - SuitSparse) does not. Therefore in this case the right hand side is kept dense and A1\x1 is solved. The issue is from 2019 and I have not checked if anything changed, but I guess that it still holds.

If x4=A1\sparse(B1) converts B1 into a dense matrix, then A1\sparse(B1) should be equivalent to A1\B1. But in my case, A1\B1 fails whereas A1\sparse(B1) gives a solution. So it seems that the ‘converted B1’ is different somehow - and I don’t know how and why.

I know I can get a solution using other methods, like pinv, the point is that I would like to avoid adjusting my solver to every possible matrix that I could get.

I see. You are right. The inconsistency that you mention initially escaped my attention. Indeed, A\b and A\sparse(b) behave rather inconsistently. For convenience of other readers I include the code again (well, I changed the names of the variables a bit):