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

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!

How about using Diagonal?

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

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.

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.

6 Likes