# Unexpected solution for fewer equations than unknowns

**URL:** <https://discourse.julialang.org/t/unexpected-solution-for-fewer-equations-than-unknowns/32310>\
**Category:** General Usage\
**Created:** [December 15, 2019, 8:44pm UTC](https://discourse.julialang.org/t/unexpected-solution-for-fewer-equations-than-unknowns/32310 "2019-12-15T20:44:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![omortis](https://avatars.discourse-cdn.com/v4/letter/o/da6949/32.png) [@omortis](https://discourse.julialang.org/u/omortis)\
**Post date:** [December 15, 2019, 8:44pm UTC](https://discourse.julialang.org/t/unexpected-solution-for-fewer-equations-than-unknowns/32310/1 "2019-12-15T20:44:57Z")

</div>

Hello,

The following system has many solutions, per simple Gaussian reduction:

x - 3y + z = 1  
x + y + 2z = 14

Yet Julia actually gives me a unique solution:

```julia
julia> A = [1 -3 1; 1 1 2]; u = [1, 14];

julia> A
2×3 Array{Int64,2}:
 1 -3 1
 1 1 2

julia> u
2-element Array{Int64,1}:
  1
 14

julia> A \ u
3-element Array{Float64,1}:
 2.4242424242424248
 2.0606060606060614
 4.75757575757576

```

`numpy` errors out, as expected.

Am I entering the vectors incorrectly? What’s going on?

---

<div class="post-metadata">

**Author:** ![dpo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpo/32/3335_2.png) [@dpo](https://discourse.julialang.org/u/dpo)\
**Post date:** [December 15, 2019, 8:47pm UTC](https://discourse.julialang.org/t/unexpected-solution-for-fewer-equations-than-unknowns/32310/2 "2019-12-15T20:47:56Z")

</div>

You’re getting the minimum-norm solution.

---

<div class="post-metadata">

**Author:** ![omortis](https://avatars.discourse-cdn.com/v4/letter/o/da6949/32.png) [@omortis](https://discourse.julialang.org/u/omortis)\
**Post date:** [December 15, 2019, 9:00pm UTC](https://discourse.julialang.org/t/unexpected-solution-for-fewer-equations-than-unknowns/32310/3 "2019-12-15T21:00:38Z")

</div>

Oh. Thank you for pointing that out. This is my first time back in linear algebra in 20 years, I guess I haven’t gotten that far yet.

The `left-division` doc didn’t mention that at all. I think I’d prefer an error.

Thanks again!

---

<div class="post-metadata">

**Author:** ![dpo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpo/32/3335_2.png) [@dpo](https://discourse.julialang.org/u/dpo)\
**Post date:** [December 15, 2019, 9:45pm UTC](https://discourse.julialang.org/t/unexpected-solution-for-fewer-equations-than-unknowns/32310/4 "2019-12-15T21:45:33Z")

</div>

I’m not sure if/where that is explained in the documentation. It’s a side effect of calling the underlying LAPACK functions. When `A` is rectangular, `\` performs a QR factorization. If `A` has more columns than rows, you receive the minimum-norm solution (in the Euclidean norm). If `A` has more rows than columns and has full column rank, you receive the least-squares solution. If `A` has more rows than columns but is column-rank deficient, you receive the minimum-norm least-squares solution. That is often the desired behavior in numerical computations.

However, that behavior goes away when you work with sparse matrices, which is an unfortunate inconsistency:

```julia
julia> using SparseArrays

julia> A = [1. -3 1; 1 1 2]; u = [1, 14]; # A must have real elements

julia> B = sparse(A);

julia> B \ u
3-element Array{Float64,1}:
 10.749999999999998
  3.2499999999999996
  0.0

```

In this case, you receive a _basic_ solution, i.e., one with as many zeros as the column-rank deficiency of `A`. It’s not the minimum-norm solution.

---

<div class="post-metadata">

**Author:** ![omortis](https://avatars.discourse-cdn.com/v4/letter/o/da6949/32.png) [@omortis](https://discourse.julialang.org/u/omortis)\
**Post date:** [December 15, 2019, 10:58pm UTC](https://discourse.julialang.org/t/unexpected-solution-for-fewer-equations-than-unknowns/32310/5 "2019-12-15T22:58:38Z")

</div>

@dpo thanks for the pointer to LAPACK. The main thing I am taking away from this is that what I am seeing is “expected behavior” and the details are best left as an exercise. I am ok with that.
