# Backslash (left divided) of two sparse matrices A\\B return a dense matrix

**URL:** <https://discourse.julialang.org/t/backslash-left-divided-of-two-sparse-matrices-a-b-return-a-dense-matrix/15102>\
**Category:** General Usage\
**Tags:** question\
**Created:** [September 18, 2018, 1:40am UTC](https://discourse.julialang.org/t/backslash-left-divided-of-two-sparse-matrices-a-b-return-a-dense-matrix/15102 "2018-09-18T01:40:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![yuguophys](https://avatars.discourse-cdn.com/v4/letter/y/67e7ee/32.png) [@yuguophys](https://discourse.julialang.org/u/yuguophys)\
**Post date:** [September 18, 2018, 1:40am UTC](https://discourse.julialang.org/t/backslash-left-divided-of-two-sparse-matrices-a-b-return-a-dense-matrix/15102/1 "2018-09-18T01:40:25Z")

</div>

On Julia-0.6.4, I find that

speye(200) \ speye(200)  
returns  
200×200 SparseMatrixCSC{Float64,Int64} with 40000 stored entries  
[1 , 1] = 1.0  
[2 , 1] = 0.0  
[3 , 1] = 0.0  
…  
which is of sparse matrix type but essentially dense.

On Julia-1.0.0, it makes more sense:  
sparse(1.0I, 200, 200) \ sparse(1.0I, 200, 200)  
200×200 SparseMatrixCSC{Float64,Int64} with 200 stored entries  
[1 , 1] = 1.0  
[2 , 2] = 1.0  
[3 , 3] = 1.0

But currently I have to use 0.6.4 due to package incompatible issues of 0.7 and 1.0.

How can I make A\B to return a truely sparse matrix for Julia-0.6.4?  
Thank you!

---

<div class="post-metadata">

**Author:** ![wsshin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wsshin/32/360_2.png) [@wsshin](https://discourse.julialang.org/u/wsshin)\
**Post date:** [September 18, 2018, 4:38am UTC](https://discourse.julialang.org/t/backslash-left-divided-of-two-sparse-matrices-a-b-return-a-dense-matrix/15102/2 "2018-09-18T04:38:30Z")

</div>

How about using `Diagonal`?

```julia
julia> A = Diagonal(ones(10))
10×10 Diagonal{Float64}:
 1.0 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
  ⋅ 1.0 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
  ⋅ ⋅ 1.0 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
  ⋅ ⋅ ⋅ 1.0 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
  ⋅ ⋅ ⋅ ⋅ 1.0 ⋅ ⋅ ⋅ ⋅ ⋅
  ⋅ ⋅ ⋅ ⋅ ⋅ 1.0 ⋅ ⋅ ⋅ ⋅
  ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 1.0 ⋅ ⋅ ⋅
  ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 1.0 ⋅ ⋅
  ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 1.0 ⋅
  ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 1.0

julia> A\A
10×10 Diagonal{Float64}:
 1.0 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
  ⋅ 1.0 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
  ⋅ ⋅ 1.0 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
  ⋅ ⋅ ⋅ 1.0 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
  ⋅ ⋅ ⋅ ⋅ 1.0 ⋅ ⋅ ⋅ ⋅ ⋅
  ⋅ ⋅ ⋅ ⋅ ⋅ 1.0 ⋅ ⋅ ⋅ ⋅
  ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 1.0 ⋅ ⋅ ⋅
  ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 1.0 ⋅ ⋅
  ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 1.0 ⋅
  ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 1.0

```

---

<div class="post-metadata">

**Author:** ![yuguophys](https://avatars.discourse-cdn.com/v4/letter/y/67e7ee/32.png) [@yuguophys](https://discourse.julialang.org/u/yuguophys)\
**Post date:** [September 18, 2018, 4:58am UTC](https://discourse.julialang.org/t/backslash-left-divided-of-two-sparse-matrices-a-b-return-a-dense-matrix/15102/3 "2018-09-18T04:58:51Z")

</div>

Thank you!  
In my use case of A\B, A has off-diagonal terms, for example,  
`A = spdiagm((2*ones(200),ones(199),ones(199)),(0,-1,1))`

But the inverse leads to an error,  
`A\speye(200) ERROR: MethodError: no method matching lufact!(::Hermitian{Float64,SparseMatrixCSC{Float64,Int64}}, ::Type{Val{true}}) Closest candidates are: `  
Not sure why this happens.

Edit: `A\eye(200)` returns a dense matrix.

`spdiagm((2*ones(200),ones(100),ones(100)),(0,-100,100)) \ speye(200)` should be a sparse matrix, but neither Julia-0.6.4 nor Julia-1.0.0 can handle it.

If we know A and A^{-1} are sparse, we may need to construct A^{-1} instead of A \ speye.

---

<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:** [September 18, 2018, 9:40pm UTC](https://discourse.julialang.org/t/backslash-left-divided-of-two-sparse-matrices-a-b-return-a-dense-matrix/15102/4 "2018-09-18T21:40:58Z")

</div>

> [@yuguophys](#):
>
> How can I make A\B to return a truely sparse matrix

Realize that, for almost any sparse matrices `A` and `B`, `A \ B` is not sparse. Only if `A` is extremely special, e.g. a diagonal matrix, will the result be sparse, and in such cases you should probably take advantage of `A`’s special properties rather than using a general sparse solver.
