# Kron and shared data, not the right amount of sharing?

**URL:** <https://discourse.julialang.org/t/kron-and-shared-data-not-the-right-amount-of-sharing/48080>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 10, 2020, 12:31am UTC](https://discourse.julialang.org/t/kron-and-shared-data-not-the-right-amount-of-sharing/48080 "2020-10-10T00:31:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![malacroi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/malacroi/32/19745_2.png) [@malacroi](https://discourse.julialang.org/u/malacroi)\
**Post date:** [October 10, 2020, 12:31am UTC](https://discourse.julialang.org/t/kron-and-shared-data-not-the-right-amount-of-sharing/48080/1 "2020-10-10T00:31:04Z")

</div>

The `kron` function on vectors seems to be inappropriately holding on to its input. As a simple example, the input

```julia
s = [2,5]; k=kron(s,[3,1]); push!(s,4)

```

produces the error:

```julia
cannot resize array with shared data

Stacktrace:
 [1] _growend! at ./array.jl:892 [inlined]
 [2] push!(::Array{Int64,1}, ::Int64) at ./array.jl:935
 [3] top-level scope at In[71]:4
 [4] include_string(::Function, ::Module, ::String, ::String) at ./loading.jl:1091

```

while

```julia
s = [2,5]; k=kron(copy(s),[1,0]); push!(s,4)

```

behaves as I would have expected the initial input to. So it would appear that the output of `kron` somehow shares data with it’s input.  
By way of contrast:

```julia
s = [2,5]; k=kron(s,[1,0]); s .= [3,7]; k

```

shows that the output of `kron` only depends on the initial value of `s`. It seems like the restrictions from the sharing of `s` are thus unnecessary.

I would have expected `kron` to either always use a copy of its input, in which case the first input wouldn’t produce an error, or for it’s output to be some clever construct that maintains a reference to the input vectors, in which case the third input produces surprising results.

Is this just an unavoidable side effect of the fact that `kron` for vectors is implemented in terms of `kron` for matrices? It seems like the culprit is that Matrix created by the `reshape(s,length(s),1)` function argument isn’t properly transient.

---

<div class="post-metadata">

**Author:** ![malacroi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/malacroi/32/19745_2.png) [@malacroi](https://discourse.julialang.org/u/malacroi)\
**Post date:** [December 6, 2020, 2:34pm UTC](https://discourse.julialang.org/t/kron-and-shared-data-not-the-right-amount-of-sharing/48080/2 "2020-12-06T14:34:30Z")

</div>

Looks like it’s an example of this issue.  
[https://github.com/JuliaLang/julia/issues/33143](https://github.com/JuliaLang/julia/issues/33143)
