# Confusing broadcast behavoir

**URL:** <https://discourse.julialang.org/t/confusing-broadcast-behavoir/64826>\
**Category:** New to Julia\
**Tags:** broadcast, array\
**Created:** [July 17, 2021, 7:03pm UTC](https://discourse.julialang.org/t/confusing-broadcast-behavoir/64826 "2021-07-17T19:03:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nandoconde](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nandoconde/32/19497_2.png) [@nandoconde](https://discourse.julialang.org/u/nandoconde)\
**Post date:** [July 17, 2021, 7:03pm UTC](https://discourse.julialang.org/t/confusing-broadcast-behavoir/64826/1 "2021-07-17T19:03:20Z")

</div>

Hello!

I was experimenting with broadcasting as a way to copy arrays and noticed this:

```julia
# First method: Broadcasting after =
a = [1,2,3]
b = @. a
b === a # true

```

```julia
# Second method: Broadcasting at the beginning
a = [1,2,3]
@. b = a
b === a # false

```

```julia
# Third method: Multiplying identity
a = [1,2,3]
b = @. 1*a
b === a # false

```

Is this desired behaviour? Particularly the first two methods

Thanks!

EDIT: I just saw that the second method errors unless previously initialized. Still, the question holds (more or less 😄 )

---

<div class="post-metadata">

**Author:** ![vavrines](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vavrines/32/8024_2.png) [@vavrines](https://discourse.julialang.org/u/vavrines)\
**Post date:** [July 17, 2021, 7:13pm UTC](https://discourse.julialang.org/t/confusing-broadcast-behavoir/64826/2 "2021-07-17T19:13:09Z")

</div>

The rest question should be straightforward.  
It can be turned to understanding this.

```julia
julia> a=[1,2,3]
3-element Vector{Int64}:
 1
 2
 3

julia> b=a
3-element Vector{Int64}:
 1
 2
 3

julia> c=deepcopy(a)
3-element Vector{Int64}:
 1
 2
 3

julia> b===a
true

julia> c===a
false

julia> c==a
true

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [July 17, 2021, 8:25pm UTC](https://discourse.julialang.org/t/confusing-broadcast-behavoir/64826/3 "2021-07-17T20:25:44Z")

</div>

All of those are expected.

But I’m curious: what were you expecting this

> [@nandoconde](#):
>
> `b = @. a`

to do? The `@.` macro adds dots to all operators and function calls, but here there are none. Clearly, this is identical to

```julia
b = a

```

which is just ordinary assignment.

The second one

> [@nandoconde](#):
>
> `@. b = a`

is equivalent to

```julia
b .= a

```

which does elementwise copying.

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [July 17, 2021, 8:47pm UTC](https://discourse.julialang.org/t/confusing-broadcast-behavoir/64826/4 "2021-07-17T20:47:36Z")

</div>

You can do `b = identity.(a)` to copy.  
Note that unlike `copy`, broadcasting can narrow the container types:

```julia
julia> x = Union{Float64,Missing}[1.0, 2.0, 3.0]
3-element Vector{Union{Missing, Float64}}:
 1.0
 2.0
 3.0

julia> copy(x)
3-element Vector{Union{Missing, Float64}}:
 1.0
 2.0
 3.0

julia> identity.(x)
3-element Vector{Float64}:
 1.0
 2.0
 3.0

```

---

<div class="post-metadata">

**Author:** ![nandoconde](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nandoconde/32/19497_2.png) [@nandoconde](https://discourse.julialang.org/u/nandoconde)\
**Post date:** [July 18, 2021, 8:31am UTC](https://discourse.julialang.org/t/confusing-broadcast-behavoir/64826/5 "2021-07-18T08:31:49Z")

</div>

Thanks!!

This was the kind of alternative that I was seeking after understanding the effect of broadcasting.
