# A\_mul\_B! overwrites elements in an array

**URL:** <https://discourse.julialang.org/t/a-mul-b-overwrites-elements-in-an-array/20421>\
**Category:** General Usage\
**Created:** [February 4, 2019, 11:31am UTC](https://discourse.julialang.org/t/a-mul-b-overwrites-elements-in-an-array/20421 "2019-02-04T11:31:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pjgorski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pjgorski/32/4802_2.png) [@pjgorski](https://discourse.julialang.org/u/pjgorski)\
**Post date:** [February 4, 2019, 11:31am UTC](https://discourse.julialang.org/t/a-mul-b-overwrites-elements-in-an-array/20421/1 "2019-02-04T11:31:38Z")

</div>

I am using DifferentialEquations and I have following preallocated arrays of the type Array{Array{Float64,2},1}:

```julia
lay1mul = fill(zeros(n,n),g+1);
views = fill(zeros(n,n),g+1);

```

Then, in the derivative function I am filling those arrays:

```julia
for i=1:1:g+1
  views[i] = view(x,:,:,i)
  A_mul_B!(lay1mul[i], views[i], views[i])
end

```

(x is generated with ‘rand(n,n,g+1)’ )

I expected lay1mul to contain g+1 possibly different arrays of type Array{Float64,2}.  
But all g+1 elements are the same.  
It means that running ‘A\_mul\_B!(lay1mul[1], views[1], views[1])’ fills all elements of lay1mul to the same value.

P.S. I am still using Julia 0.6.4.

---

<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:** [February 4, 2019, 11:44am UTC](https://discourse.julialang.org/t/a-mul-b-overwrites-elements-in-an-array/20421/2 "2019-02-04T11:44:10Z")

</div>

See [https://github.com/JuliaLang/julia/issues/29168](https://github.com/JuliaLang/julia/issues/29168), `views = fill(zeros(n,n),g+1)` creates a vector with the _same_ matrix in each position, e.g.

```julia
julia> A = fill(zeros(2,2), 2);

julia> A[1] === A[2]
true

```

and thus when you change `A[1]` `A[2]` (which is the _same_ object) will also change:

```julia
julia> A[1][1] = 42; A
2-element Array{Array{Float64,2},1}:
 [42.0 0.0; 0.0 0.0]
 [42.0 0.0; 0.0 0.0]

```

---

<div class="post-metadata">

**Author:** ![yha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yha/32/3502_2.png) [@yha](https://discourse.julialang.org/u/yha)\
**Post date:** [February 4, 2019, 4:50pm UTC](https://discourse.julialang.org/t/a-mul-b-overwrites-elements-in-an-array/20421/3 "2019-02-04T16:50:04Z")

</div>

To get a vector of `g+1` separate arrays you can use

```julia
lay1mul = [zeros(n,n) for _=1:g+1]

```

---

<div class="post-metadata">

**Author:** ![pjgorski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pjgorski/32/4802_2.png) [@pjgorski](https://discourse.julialang.org/u/pjgorski)\
**Post date:** [February 4, 2019, 9:40pm UTC](https://discourse.julialang.org/t/a-mul-b-overwrites-elements-in-an-array/20421/4 "2019-02-04T21:40:50Z")

</div>

Ow, you are right, that initialization was a problem.

But I have one more question. Looking more at the variables I noticed that

```julia
julia> lay1mul[1]===lay1mul[2]
true

```

But views (after for loop) contains different elements and

```julia
julia> views[1]===views[2]
false

```

What is the reason for this behavior? Is it because after using view() elements of views point at different parts of memory and they are not linked anymore?

@yha Thanks! I’ve been looking for 15 minutes for one-liner to do it.

---

<div class="post-metadata">

**Author:** ![lwabeke](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lwabeke/32/4005_2.png) [@lwabeke](https://discourse.julialang.org/u/lwabeke)\
**Post date:** [February 5, 2019, 7:48am UTC](https://discourse.julialang.org/t/a-mul-b-overwrites-elements-in-an-array/20421/5 "2019-02-05T07:48:19Z")

</div>

This line is maybe not doing what you are thinking it is doing.  
Unless I am mistaken, this is actually doing a deepcopy:

```julia
views[i] = view(x,:,:,i)

```

It is creating a view, but then a new array is created in views, to which the data from the view is deepcopied:

```julia
julia> n = 500
500

julia> g = 3
3

julia> x = rand(n,n,g+1);

julia> views = fill(zeros(n,n),g+1);

julia> i = 1
1

julia> @time views[i] = view(x,:,:,i);
  0.000760 seconds (10 allocations: 1.908 MiB)

julia> @time views[i] = view(x,:,:,i);
  0.000387 seconds (10 allocations: 1.908 MiB)

julia> @time v = view(x,:,:,i);
  0.000016 seconds (8 allocations: 304 bytes)

julia> @time v = view(x,:,:,i);
  0.000014 seconds (8 allocations: 304 bytes)

julia> @time views[i] = v;
  0.000451 seconds (6 allocations: 1.908 MiB)

julia> @time views[i] = v;
  0.000561 seconds (6 allocations: 1.908 MiB)

```

Even if you actually created the views, by doing something like

```julia
views = Array{Any}(undef, g+1)

```

You would still end up with distinct views which might be pointing to the same underlying memory due to the issue listed above.
