Broadcast macro "breaks" norm

Hello,

I found this rather interesting behaviour

julia> using LinearAlgebra
julia> a = [0, 0, 1]
3-element Array{Int64,1}:
 0
 0
 1

julia> @. a/norm(a)
3-element Array{Float64,1}:
 NaN  
 NaN  
   1.0

julia> a./norm(a)
3-element Array{Float64,1}:
 0.0
 0.0
 1.0

Only when i tried with a=[0, 4, 4] and got out @. a/norm(a) returned [NaN, 1.0, 1.0] that i realised that the broadcasting was being passed to norm as well.

This is a rather simplistic case but this renders using @. over expressions that use the norm completely useless, so we can’t take advantage of the performances of fusing broadcasting calls.
I assumed that norm would only work for vectors and up, since for scalars one could just use abs.

The quotes over breaks are because i realise that the norm behaving this way make sense, but still, as i said above one could just use abs to take the norm of a scalar value.

Is there a reason it is this way?

1 Like

@. will add a dot everywhere unless there is a $ sign before the function. @. a / $norm(a) does what you want.

6 Likes