# Dot broadcast macro not working as expected

**URL:** <https://discourse.julialang.org/t/dot-broadcast-macro-not-working-as-expected/12472>\
**Category:** General Usage\
**Created:** [July 18, 2018, 4:40pm UTC](https://discourse.julialang.org/t/dot-broadcast-macro-not-working-as-expected/12472 "2018-07-18T16:40:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ssfrr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ssfrr/32/3736_2.png) [@ssfrr](https://discourse.julialang.org/u/ssfrr)\
**Post date:** [July 18, 2018, 4:40pm UTC](https://discourse.julialang.org/t/dot-broadcast-macro-not-working-as-expected/12472/1 "2018-07-18T16:40:54Z")

</div>

I’m not sure if this is a bug in my understanding or an issue with the macro:

I’m creating a complex array and then scrambling the phases. Here `rand(N)` is the only vector-valued thing, so I’d expect all the rest of the operations to automatically dot-broadcast

```julia
julia> N = 15
15

julia> X = ones(Complex128, N)
15-element Array{Complex{Float64},1}:
 1.0+0.0im
    ⋮

julia> @. X *= exp(im*π*(rand(N)*2-1))
WARNING: exp(x::AbstractArray{T}) where T <: Number is deprecated, use exp.(x) instead.
Stacktrace:
 [1] depwarn(::String, ::Symbol) at .\deprecated.jl:70
 [2] exp(::Array{Complex{Float64},1}) at .\deprecated.jl:57
 [3] (::##71#72)(::Complex{Float64}, ::Complex{Bool}, ::Irrational{:π}, ::Int64) at .\<missing>:0
 [4] macro expansion at .\broadcast.jl:155 [inlined]
    ⋮

ERROR: MethodError: Cannot `convert` an object of type Array{Complex{Float64},1} to an object of type Complex{Float64}
This may have arisen from a call to the constructor Complex{Float64}(...),
since type constructors fall back to convert methods.
Stacktrace:
 [1] setindex! at .\multidimensional.jl:300 [inlined]
 [2] macro expansion at .\broadcast.jl:156 [inlined]
 [3] macro expansion at .\simdloop.jl:73 [inlined]
 [4] macro expansion at .\broadcast.jl:149 [inlined]
 [5] _broadcast!(::##71#72, ::Array{Complex{Float64},1}, 
    ⋮

```

Explicitly putting the dots: `.*=` and in `exp.(...)` works, but I’d thought that the `@.` macro should do it for me.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [July 18, 2018, 4:45pm UTC](https://discourse.julialang.org/t/dot-broadcast-macro-not-working-as-expected/12472/2 "2018-07-18T16:45:06Z")

</div>

> [@ssfrr](#):
>
> `@. X *= exp(im*π*(rand(N)*2-1))`

I think you want `@. X *= cis(π*(rand()*2-1))`. This will avoid allocating the random vector and will just call `rand()` separately for each element, and `cis(x)` should be faster than `exp(im*x)`.

The problem with `@.` and `rand(N)` is that it is equivalent `rand.(N)`, which calls `rand(N)` for _each element_ of your `X` array.

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [July 18, 2018, 4:50pm UTC](https://discourse.julialang.org/t/dot-broadcast-macro-not-working-as-expected/12472/3 "2018-07-18T16:50:27Z")

</div>

Some people may disagree with this advice, but I prefer not to use the `@.` macro unless it is abundantly obvious to me exactly what it’s going to do. In this case the presence of the `rand(N)` would probably be enough to stop me from using it here (as I wouldn’t necessarily expect `rand.(N)` to produce the same class of results as `rand(N)` even though it does). In my opinion putting in a few extra `.` characters is a small sacrifice for clarity.

---

<div class="post-metadata">

**Author:** ![ssfrr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ssfrr/32/3736_2.png) [@ssfrr](https://discourse.julialang.org/u/ssfrr)\
**Post date:** [July 18, 2018, 5:11pm UTC](https://discourse.julialang.org/t/dot-broadcast-macro-not-working-as-expected/12472/4 "2018-07-18T17:11:56Z")

</div>

Ah, that’s right - for some reason I wasn’t thinking about the fact that I’d get `rand.(...)` and was thinking about `rand(...)` as a unit. Probably supporting evidence for @ExpandingMan’s point. 🙂

Good point about not allocating the random vector, and thanks for introducing me to `cis`.

-s

---

<div class="post-metadata">

**Author:** ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)\
**Post date:** [July 18, 2018, 10:19pm UTC](https://discourse.julialang.org/t/dot-broadcast-macro-not-working-as-expected/12472/5 "2018-07-18T22:19:58Z")

</div>

Just in case you didn’t know, you also have the option of `@. X *= cis(π*($rand(N)*2-1))` . The `$` stops the `.` that `@.` would have added there. That said, the other solution is definitely better here.
