# Broadcasting question

**URL:** <https://discourse.julialang.org/t/broadcasting-question/122092>\
**Category:** New to Julia\
**Tags:** broadcasting\
**Created:** [October 31, 2024, 6:31pm UTC](https://discourse.julialang.org/t/broadcasting-question/122092 "2024-10-31T18:31:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lancejnelson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lancejnelson/32/21898_2.png) [@lancejnelson](https://discourse.julialang.org/u/lancejnelson)\
**Post date:** [October 31, 2024, 6:31pm UTC](https://discourse.julialang.org/t/broadcasting-question/122092/1 "2024-10-31T18:31:45Z")

</div>

I’m trying to calculate the following:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/1/d1dcdeb89885eb90ff82c6fe49abf67b41cb0c24.png)

where v and \hat{n} vectors of length 3. But I need to do this to a bunch of vectors stored in a `Vector{Vector{SVector{3,Float64}}}`

I ended up doing it like this:

```julia
dots = [[velocities[j][i]' * nHat[j][i] * nHat[j][i] for i =1:length(config.atomicBasis[j])] for j = 1:config.order]
velocities = velocities - 2 * dots

```

but wondering if there is a better way.

Thanks in advance

---

<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:** [October 31, 2024, 6:41pm UTC](https://discourse.julialang.org/t/broadcasting-question/122092/2 "2024-10-31T18:41:11Z")

</div>

You’re allocating a bunch of temporary arrays, e.g. `dots = [...]` is allocating lots of little arrays, `2 * dots` allocates new arrays, then `velocities - 2 * dots` allocates more arrays.

Assuming you want to update `velocities` in-place, it would be a lot clearer (and faster, with no temporary arrays) to just write a loop:

```julia
for j = 1:config.order, i = 1:length(config.atomicBasis[j])
   @inbounds velocities[j][i] -= (2 * (velocities[j][i]' * nHat[j][i])) * nHat[j][i]
end

```

(Hopefully the compiler can hoist the `velocities[j]` and `nHat[j]` indexing out of the innermost loop?)

Loops (in local scope) are fast in Julia. Don’t twist yourself into a pretzel trying to use “vectorized” operations.

---

<div class="post-metadata">

**Author:** ![JonasWickman](https://avatars.discourse-cdn.com/v4/letter/j/9de0a6/32.png) [@JonasWickman](https://discourse.julialang.org/u/JonasWickman)\
**Post date:** [October 31, 2024, 7:15pm UTC](https://discourse.julialang.org/t/broadcasting-question/122092/3 "2024-10-31T19:15:19Z")

</div>

You could take the advice of stevengj (which is what you actually should do), _or_ you could go julia-nuts:

```julia
using StaticArrays
using LinearAlgebra

new_vec(v::SVector, n̂::SVector) = v - 2*(v⋅n̂)*n̂
new_vec(v::Vector{<:SVector}, n̂::Vector{<:SVector}) = new_vec.(v, n̂)
new_vec(v::Vector{<:Vector{<:SVector}}, n̂::Vector{<:Vector{<:SVector}}) = new_vec.(v, n̂)
new_vec!(v::Vector{<:SVector}, n̂::Vector{<:SVector}) = v .= new_vec.(v, n̂)
new_vec!(v::Vector{<:Vector{<:SVector}}, n̂::Vector{<:Vector{<:SVector}}) = v .= new_vec!.(v, n̂)

vs = [rand(SVector{3, Float64}, 4) for _ ∈ 1:2]
n̂s = [rand(SVector{3, Float64}, 4) for _ ∈ 1:2]

vs = new_vec!(vs, n̂s)

```

---

<div class="post-metadata">

**Author:** ![lancejnelson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lancejnelson/32/21898_2.png) [@lancejnelson](https://discourse.julialang.org/u/lancejnelson)\
**Post date:** [October 31, 2024, 8:09pm UTC](https://discourse.julialang.org/t/broadcasting-question/122092/5 "2024-10-31T20:09:07Z")

</div>

I’ve noticed this theme emerge for me: I’ve been coding in python too long and always think I should write complicated code in the fewest number of lines possible. Would it be fair to say that the more julian thing do is often to just write out the dang loop?

---

<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:** [October 31, 2024, 8:15pm UTC](https://discourse.julialang.org/t/broadcasting-question/122092/6 "2024-10-31T20:15:14Z")

</div>

Yes, Python trains you that your code is slow, and libraries are fast, so you should contort yourself to make all loops occur in libraries. To _some_ extent, this is good advice in any language — re-using highly optimized, robust libraries is good!

But it has two limitations. First, you sometimes need to write really convoluted, opaque code to express your problem in terms of the library API. Second, you often pay a performance price for using a sequence of generic library functions instead of code that is _specialized_ for your problem. (For example, you might need _several_ “vectorized” calls instead of a _single_ loop, which [slows things down a lot](https://julialang.org/blog/2017/01/moredots/#isnt_vectorized_code_already_fast). Julia’s “dot call” loop fusion can help with this, but not so much with nested data structures like yours.)

So, it’s a judgement call, but in Julia “writing out the dang loop” is a good alternative to writing convoluted “vectorized” code.
