# "Scalarizing" an element in broadcast or dot fused calls

**URL:** <https://discourse.julialang.org/t/scalarizing-an-element-in-broadcast-or-dot-fused-calls/4805>\
**Category:** General Usage\
**Created:** [July 12, 2017, 1:31pm UTC](https://discourse.julialang.org/t/scalarizing-an-element-in-broadcast-or-dot-fused-calls/4805 "2017-07-12T13:31:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![TsurHerman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tsurherman/32/1234_2.png) [@TsurHerman](https://discourse.julialang.org/u/TsurHerman)\
**Post date:** [July 12, 2017, 1:31pm UTC](https://discourse.julialang.org/t/scalarizing-an-element-in-broadcast-or-dot-fused-calls/4805/1 "2017-07-12T13:31:12Z")

</div>

Just thought I might share this … even if its probably trivial for most of you.

Let assume you have a 3x3 matrix and vector of n length 3 vectors

```julia
K = rand(3,3)
V = [rand(3) for i=1:1000]

```

and you want to multiply K with each element of V

`K .* V`  
or  
`broadcast(*,K,V)`

will not work due to DimensionMismatch error

but this does work:  
[K] .\* V  
broadcast(\*,[K],V)

trivial but nice

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [July 12, 2017, 1:37pm UTC](https://discourse.julialang.org/t/scalarizing-an-element-in-broadcast-or-dot-fused-calls/4805/2 "2017-07-12T13:37:21Z")

</div>

The slightly cheaper way is `(K,)`. `Base.RefValue` is also expected to work.

---

<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 12, 2017, 2:26pm UTC](https://discourse.julialang.org/t/scalarizing-an-element-in-broadcast-or-dot-fused-calls/4805/3 "2017-07-12T14:26:22Z")

</div>

> [@yuyichao](#):
>
> Base.RefValue is also expected to work.

i.e. `Ref(K) .* V`

---

<div class="post-metadata">

**Author:** ![TsurHerman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tsurherman/32/1234_2.png) [@TsurHerman](https://discourse.julialang.org/u/TsurHerman)\
**Post date:** [July 12, 2017, 6:04pm UTC](https://discourse.julialang.org/t/scalarizing-an-element-in-broadcast-or-dot-fused-calls/4805/4 "2017-07-12T18:04:24Z")

</div>

You gotta admit that [K] .\* V  
Is better on the eyes than (K,) .\* V which is slightly more cryptic.  
I’ll keep in mind the slight performance gain though.

---

<div class="post-metadata">

**Author:** ![musm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/musm/32/3675_2.png) [@musm](https://discourse.julialang.org/u/musm)\
**Post date:** [July 14, 2017, 5:17am UTC](https://discourse.julialang.org/t/scalarizing-an-element-in-broadcast-or-dot-fused-calls/4805/5 "2017-07-14T05:17:40Z")

</div>

Interestingly using Ref is about 3 times faster

```julia
julia> @btime Ref($K) .* $(V);
  26.449 μs (1001 allocations: 117.31 KiB)

julia> @btime [$K] .* $(V);
  81.320 μs (1003 allocations: 117.42 KiB)

julia> @btime ($K,) .* $(V);
  85.267 μs (2001 allocations: 132.94 KiB)

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [July 14, 2017, 6:00am UTC](https://discourse.julialang.org/t/scalarizing-an-element-in-broadcast-or-dot-fused-calls/4805/6 "2017-07-14T06:00:57Z")

</div>

I thought `Ref` was something for the foreign function interface (based on [this](https://docs.julialang.org/en/latest/manual/calling-c-and-fortran-code/#When-to-use-T,-Ptr%7BT%7D-and-Ref%7BT%7D-1)), but apparently it has other roles. Where is this documented? I would like to read about it.

---

<div class="post-metadata">

**Author:** ![TsurHerman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tsurherman/32/1234_2.png) [@TsurHerman](https://discourse.julialang.org/u/TsurHerman)\
**Post date:** [July 14, 2017, 7:46am UTC](https://discourse.julialang.org/t/scalarizing-an-element-in-broadcast-or-dot-fused-calls/4805/7 "2017-07-14T07:46:54Z")

</div>

It is really interesting because I would expect broadcast to “peel” the outer layer, and that the only performance difference would come from that.

But apparently this scales with the size of V.  
Weird
