Sparse non-square linear problem

I would like to solve Ax = b where A is a sparse non-square array.
For example :

using SparseArrays
using LinearSolve

n = 3
m = 4
A = sparse(rand(ComplexF64, m, n))
b = rand(ComplexF64, m)

prob = LinearProblem(A, b)
sol = solve(prob)
sol.u

This works if A is sparse and square, or dense and non-square, but not sparse and non-square.
How can I solve this ?

A\b seems to work just fine. Is there a reason not to use it?

1 Like

Oh right thanks, I think the issue was coming form the fact that A was the transpose of another matrix C in my initial problem. Fixed it doing A=sparse(transpose(C)).

What do you mean by “solve”?

The example you gave is overdetermined (more rows than columns), so there is no solution to Ax = b. A \ b will give the least-squares solution (via sparse QR), which minimizes \Vert Ax - b \Vert but will not generally solve Ax = b.

2 Likes