# How to use ldiv! correctly?

**URL:** <https://discourse.julialang.org/t/how-to-use-ldiv-correctly/81378>\
**Category:** General Usage\
**Tags:** question\
**Created:** [May 20, 2022, 2:52pm UTC](https://discourse.julialang.org/t/how-to-use-ldiv-correctly/81378 "2022-05-20T14:52:08Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [May 20, 2022, 2:52pm UTC](https://discourse.julialang.org/t/how-to-use-ldiv-correctly/81378/1 "2022-05-20T14:52:08Z")

</div>

How to solve the below error when using `ldiv!`?

```julia
julia> Sn=[1 2 3;0 4 0;0 0 1]
3×3 Matrix{Int64}:
 1 2 3
 0 4 0
 0 0 1

julia> Rn =[1,2,3]
3-element Vector{Int64}:
 1
 2
 3

julia> Qn= zeros(3)
3-element Vector{Float64}:
 0.0
 0.0
 0.0

julia> Qn=Sn\Rn
3-element Vector{Float64}:
 -9.0
  0.5
  3.0

julia> ldiv!(Qn, Sn, Rn)
ERROR: MethodError: no method matching ldiv!(::Matrix{Int64}, ::Vector{Float64})

julia> SS=sparse([1 2 3;0 4 0;0 0 1])
3×3 SparseMatrixCSC{Int64, Int64} with 5 stored entries:
 1 2 3
 ⋅ 4 ⋅
 ⋅ ⋅ 1

julia> QS= spzeros(3)
3-element SparseVector{Float64, Int64} with 0 stored entries

julia> RS =sparse([1,2,3])
3-element SparseVector{Int64, Int64} with 3 stored entries:
  [1] = 1
  [2] = 2
  [3] = 3

julia> QS=SS\RS
3-element Vector{Float64}:
 -9.0
  0.5
  3.0

julia> ldiv!(QS, SS, RS)
ERROR: MethodError: no method matching ldiv!(::SparseMatrixCSC{Int64, Int64}, ::SparseVector{Float64, Int64})

```

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [May 20, 2022, 2:59pm UTC](https://discourse.julialang.org/t/how-to-use-ldiv-correctly/81378/2 "2022-05-20T14:59:41Z")

</div>

Have you read the docstring?

```julia
ldiv!(Y, A, B) -> Y

  Compute A \ B in-place and store the result in Y, returning the result.

  The argument A should not be a matrix. Rather, instead of matrices it should be a factorization object (e.g. produced by factorize or cholesky). The reason for this is that factorization itself is both
  expensive and typically allocates memory (although it can also be done in-place via, e.g., lu!), and performance-critical situations requiring ldiv! usually also require fine-grained control over the
  factorization of A.

  Examples
  ≡≡≡≡≡≡≡≡≡≡

  julia> A = [1 2.2 4; 3.1 0.2 3; 4 1 2];

  julia> X = [1; 2.5; 3];

  julia> Y = zero(X);

  julia> ldiv!(Y, qr(A), X);

```

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [May 20, 2022, 3:24pm UTC](https://discourse.julialang.org/t/how-to-use-ldiv-correctly/81378/4 "2022-05-20T15:24:13Z")

</div>

The dosctring says:

```julia
ldiv!(Y, A, B)
(...)
The argument A should not be a matrix. Rather, instead of matrices it should be a factorization object (...)

```

Your code is doing:

```julia
julia> Sn=[1 2 3;0 4 0;0 0 1]
3×3 Matrix{Int64}:

```

and

```julia
julia> SS=sparse([1 2 3;0 4 0;0 0 1])
3×3 SparseMatrixCSC{Int64, Int64} with 5 stored entries:

```

as you can see from the type displayed after construction of your `Sn`/`SS` inputs, both are matrices, so directly contradict what’s in the docstring.

Keeping with your example:

```julia
julia> ldiv!(Qn, qr(Sn), Rn)
3-element Vector{Float64}:
 -9.0
  0.5
  3.0

```

---

<div class="post-metadata">

**Author:** ![MichelJuillard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/micheljuillard/32/10555_2.png) [@MichelJuillard](https://discourse.julialang.org/u/MichelJuillard)\
**Post date:** [May 21, 2022, 4:05am UTC](https://discourse.julialang.org/t/how-to-use-ldiv-correctly/81378/6 "2022-05-21T04:05:11Z")

</div>

A rule of thumb is to only use sparse algorithms for matrices with less than 10% nonzero éléments. You have 45%

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [May 22, 2022, 9:08pm UTC](https://discourse.julialang.org/t/how-to-use-ldiv-correctly/81378/7 "2022-05-22T21:08:43Z")

</div>

Thank you for your note. Aha, is this rule of thumb cited in the literature? Does this because of the complex structure of Sparse Matrix/vector than the normal matrix/vector (thus slower in performance if nonzero éléments is more than 10%)?

---

<div class="post-metadata">

**Author:** ![MichelJuillard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/micheljuillard/32/10555_2.png) [@MichelJuillard](https://discourse.julialang.org/u/MichelJuillard)\
**Post date:** [May 23, 2022, 6:42am UTC](https://discourse.julialang.org/t/how-to-use-ldiv-correctly/81378/8 "2022-05-23T06:42:36Z")

</div>

I quickly searched for the 10% rule but couldn’t find a reference to it. In any case, the benefits of sparse matrix code depends also of the operations you want to conduct. To make a sound decision, you should benchmark your code with dense or sparse matrices.  
You are right, sparse matrix code is more complex and therefore takes more time if you have a lot on non-zero element in the matrix

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [May 23, 2022, 6:25pm UTC](https://discourse.julialang.org/t/how-to-use-ldiv-correctly/81378/9 "2022-05-23T18:25:08Z")

</div>

Thank you! I’ll keep that in my mind.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [May 23, 2022, 6:32pm UTC](https://discourse.julialang.org/t/how-to-use-ldiv-correctly/81378/10 "2022-05-23T18:32:21Z")

</div>

honestly 10% is very low sparsity. I would consider 1% closer to the breakeven point. Of course it depends on how big your matrices are and memory bandwidth vs compute bandwidth etc.

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [May 23, 2022, 6:40pm UTC](https://discourse.julialang.org/t/how-to-use-ldiv-correctly/81378/11 "2022-05-23T18:40:12Z")

</div>

So, you recommend:  
1- Sparsity of 50%∓1%?  
2- With bigger matrices, the previous sparsity could be increased?
