# Sparse non-square linear problem

**URL:** <https://discourse.julialang.org/t/sparse-non-square-linear-problem/122650>\
**Category:** General Usage\
**Tags:** linearalgebra, sparse, linearsolve, sparsearrays, solver\
**Created:** [November 14, 2024, 8:25pm UTC](https://discourse.julialang.org/t/sparse-non-square-linear-problem/122650 "2024-11-14T20:25:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nichola](https://avatars.discourse-cdn.com/v4/letter/n/f0a364/32.png) [@Nichola](https://discourse.julialang.org/u/Nichola)\
**Post date:** [November 14, 2024, 8:25pm UTC](https://discourse.julialang.org/t/sparse-non-square-linear-problem/122650/1 "2024-11-14T20:25:41Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![fgerick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fgerick/32/13228_2.png) [@fgerick](https://discourse.julialang.org/u/fgerick)\
**Post date:** [November 14, 2024, 8:33pm UTC](https://discourse.julialang.org/t/sparse-non-square-linear-problem/122650/2 "2024-11-14T20:33:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Nichola](https://avatars.discourse-cdn.com/v4/letter/n/f0a364/32.png) [@Nichola](https://discourse.julialang.org/u/Nichola)\
**Post date:** [November 14, 2024, 8:48pm UTC](https://discourse.julialang.org/t/sparse-non-square-linear-problem/122650/3 "2024-11-14T20:48:57Z")

</div>

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

---

<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:** [November 15, 2024, 1:00am UTC](https://discourse.julialang.org/t/sparse-non-square-linear-problem/122650/4 "2024-11-15T01:00:17Z")

</div>

> [@Nichola](#):
>
> This works if `A` is sparse and square, or dense and non-square, but not sparse and non-square.  
> How can I solve this ?

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.
