# Odd behaviour with kron, transpose and sparse matrices

**URL:** <https://discourse.julialang.org/t/odd-behaviour-with-kron-transpose-and-sparse-matrices/18212>\
**Category:** General Usage\
**Created:** [December 2, 2018, 12:57am UTC](https://discourse.julialang.org/t/odd-behaviour-with-kron-transpose-and-sparse-matrices/18212 "2018-12-02T00:57:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![benkj](https://avatars.discourse-cdn.com/v4/letter/b/848f3c/32.png) [@benkj](https://discourse.julialang.org/u/benkj)\
**Post date:** [December 2, 2018, 12:57am UTC](https://discourse.julialang.org/t/odd-behaviour-with-kron-transpose-and-sparse-matrices/18212/1 "2018-12-02T00:57:25Z")

</div>

This is related to [missing sparse matrix transpose methods (performance regression in 0.7.0beta2 vs 0.6) · Issue #28432 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/28432)

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

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

```

this returns

```julia
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

```julia
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?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [December 2, 2018, 7:00am UTC](https://discourse.julialang.org/t/odd-behaviour-with-kron-transpose-and-sparse-matrices/18212/2 "2018-12-02T07:00:57Z")

</div>

> [@benkj](#):
>
> is this behaviour expected?

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

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [December 2, 2018, 10:43am UTC](https://discourse.julialang.org/t/odd-behaviour-with-kron-transpose-and-sparse-matrices/18212/3 "2018-12-02T10:43:08Z")

</div>

Consider materializing the lazy adjoint with `copy`:

```julia
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

```
