Odd behaviour with kron, transpose and sparse matrices

This is related to missing sparse matrix transpose methods (performance regression in 0.7.0beta2 vs 0.6) · Issue #28432 · JuliaLang/julia · GitHub

kron returns a dense matrix if the adjoint is provided, example

id = spzeros(2,2)+I
kron(id,id')

this returns

4×4 Array{Float64,2}:
 1.0  0.0  0.0  0.0
 0.0  1.0  0.0  0.0
 0.0  0.0  1.0  0.0
 0.0  0.0  0.0  1.0

while it should output

4×4 SparseMatrixCSC{Float64,Int64} with 4 stored entries:
  [1, 1]  =  1.0
  [2, 2]  =  1.0
  [3, 3]  =  1.0
  [4, 4]  =  1.0

is this behaviour expected?

Probably just no one wrote the relevant method specialized for sparse matrices yet. You might want to make a PR.

Consider materializing the lazy adjoint with copy:

julia> using LinearAlgebra, SparseArrays

julia> id = sparse(1.0I, 2, 2);

julia> kron(id, copy(id'))
4×4 SparseMatrixCSC{Float64,Int64} with 4 stored entries:
  [1, 1]  =  1.0
  [2, 2]  =  1.0
  [3, 3]  =  1.0
  [4, 4]  =  1.0
2 Likes