# Vectorized math

**URL:** <https://discourse.julialang.org/t/vectorized-math/55476>\
**Category:** Performance\
**Created:** [February 17, 2021, 5:26pm UTC](https://discourse.julialang.org/t/vectorized-math/55476 "2021-02-17T17:26:34Z")\
**Posts on this page:** 13\
**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:** [February 17, 2021, 5:26pm UTC](https://discourse.julialang.org/t/vectorized-math/55476/1 "2021-02-17T17:26:34Z")

</div>

Is there a need to use the “.” for vector math when adding and multiplying?

```julia
a = [1 2 3]
b = [3 4 5]
# Which is better for performance? They both do the same thing.
c = 1/2 * a + b
d = 1/2 .* a .+ b

```

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [February 17, 2021, 5:37pm UTC](https://discourse.julialang.org/t/vectorized-math/55476/2 "2021-02-17T17:37:11Z")

</div>

The second should be about 2x faster. The second will compile to roughly

```julia
d=Vector{Float64}(undef, length(a))
for i in eachindex(a,b)
    d[i] = .5*a[i]+b[i]
end

```

while the first will use 2 loops and allocate an intermediate vector.

If you don’t want to have ot use lots of dots, you could write this as  
`@. d = 1/2 * a+b`

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [February 17, 2021, 6:16pm UTC](https://discourse.julialang.org/t/vectorized-math/55476/3 "2021-02-17T18:16:33Z")

</div>

(or `d = @. 1/2 * a+b` if `d` is not preallocated)

---

<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:** [February 17, 2021, 6:32pm UTC](https://discourse.julialang.org/t/vectorized-math/55476/4 "2021-02-17T18:32:11Z")

</div>

Awesome. Thank

---

<div class="post-metadata">

**Author:** ![fgerick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fgerick/32/13228_2.png) [@fgerick](https://discourse.julialang.org/u/fgerick)\
**Post date:** [February 17, 2021, 7:02pm UTC](https://discourse.julialang.org/t/vectorized-math/55476/5 "2021-02-17T19:02:38Z")

</div>

And here I am after using Julia for quite a while now, not knowing that `1/2 .* a` is better than `1/2*a` … Always room for improvement.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [February 17, 2021, 7:07pm UTC](https://discourse.julialang.org/t/vectorized-math/55476/6 "2021-02-17T19:07:48Z")

</div>

Note that this only matters when `a` is a collection, and the `.` can be fused with another operation. `1/2 * a` will be just as fast as `1/2 .* a`. The only difference is if you add another operation that broadcasting can fuse.

---

<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:** [February 17, 2021, 7:09pm UTC](https://discourse.julialang.org/t/vectorized-math/55476/7 "2021-02-17T19:09:17Z")

</div>

> [@Oscar\_Smith](#):
>
> Note that this only matters when `a` is a collection, and the `.` can be fused with another operation. `1/2 * a` will be just as fast as `1/2 .* a` . The only difference is if you add another operation that broadcasting can fuse.

You’re not saying that this:

` c = 1/2 * a .+ b`

will be as fast as this:

` c = 1/2 .* a .+ b`

are you?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [February 17, 2021, 7:10pm UTC](https://discourse.julialang.org/t/vectorized-math/55476/8 "2021-02-17T19:10:47Z")

</div>

No.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [February 17, 2021, 7:11pm UTC](https://discourse.julialang.org/t/vectorized-math/55476/9 "2021-02-17T19:11:19Z")

</div>

Basically in any expression with dots, you can imagine all dots on the same level being fused together into one element-wise function going over all involved iterables just once. It’s a syntax transformation basically. You can do the same if you write `broadcast(f, iterables...)` but that’s not nice syntax

---

<div class="post-metadata">

**Author:** ![fgerick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fgerick/32/13228_2.png) [@fgerick](https://discourse.julialang.org/u/fgerick)\
**Post date:** [February 17, 2021, 7:26pm UTC](https://discourse.julialang.org/t/vectorized-math/55476/10 "2021-02-17T19:26:26Z")

</div>

When is `(*)(x::Real, y::AbstractArray)` not equivalent to its broadcasted version? Shouldn’t `1/2 * a .+ b` fuse automatically? I believe there are always cases where this shouldn’t be done, but for the standard number types this should be okay, no?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [February 17, 2021, 7:33pm UTC](https://discourse.julialang.org/t/vectorized-math/55476/11 "2021-02-17T19:33:53Z")

</div>

The problem is that broadcasting happens on the syntax level. When you write `1/2 *a .+b` it gets lowered to

```julia
temp1 = 1/2 *a
return broadcast(+, a, b)

```

The reason broadcasting needs to be explicit is that lowering happens before type inference, so decisions about broadcast fusing can’t depend on types.

---

<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:** [February 17, 2021, 7:38pm UTC](https://discourse.julialang.org/t/vectorized-math/55476/12 "2021-02-17T19:38:25Z")

</div>

> [@fgerick](#):
>
> Shouldn’t `1/2 * a .+ b` fuse automatically?

That’s explained here: [More Dots: Syntactic Loop Fusion in Julia](https://julialang.org/blog/2017/01/moredots/#why_does_julia_need_dots_to_fuse_the_loops)

---

<div class="post-metadata">

**Author:** ![fgerick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fgerick/32/13228_2.png) [@fgerick](https://discourse.julialang.org/u/fgerick)\
**Post date:** [February 17, 2021, 8:02pm UTC](https://discourse.julialang.org/t/vectorized-math/55476/13 "2021-02-17T20:02:28Z")

</div>

Thanks! I guess I read that already at the time, but forgot about it again.
