# Broadcasting and arrays of arrays

**URL:** <https://discourse.julialang.org/t/broadcasting-and-arrays-of-arrays/7444>\
**Category:** General Usage\
**Tags:** broadcast, array\
**Created:** [December 1, 2017, 8:04pm UTC](https://discourse.julialang.org/t/broadcasting-and-arrays-of-arrays/7444 "2017-12-01T20:04:19Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![goretkin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goretkin/32/167_2.png) [@goretkin](https://discourse.julialang.org/u/goretkin)\
**Post date:** [December 1, 2017, 8:04pm UTC](https://discourse.julialang.org/t/broadcasting-and-arrays-of-arrays/7444/1 "2017-12-01T20:04:19Z")

</div>

I was hoping that this would work for centering some data that is e.g. `Array{SVector}`

```julia
d = [rand(2) for i=1:10]
d - mean(d)

```

or even the broadcasting version

```julia
d .- mean(d)

```

But they don’t work. I understand why they don’t work, but I’m wondering if it would be possible to have consistent broadcasting rules that would make it work.

For now, I know I can do

```julia
d .- [mean(d)]

```

It is just that with scalar elements, one can write

```julia
d = rand(10)
d - mean(d)

```

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [December 1, 2017, 8:07pm UTC](https://discourse.julialang.org/t/broadcasting-and-arrays-of-arrays/7444/2 "2017-12-01T20:07:11Z")

</div>

I think the way you typically “scalarize” an object is by enclosing it in a `Ref` so `d .- Ref(mean(d))`

---

<div class="post-metadata">

**Author:** ![goretkin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goretkin/32/167_2.png) [@goretkin](https://discourse.julialang.org/u/goretkin)\
**Post date:** [December 1, 2017, 9:35pm UTC](https://discourse.julialang.org/t/broadcasting-and-arrays-of-arrays/7444/3 "2017-12-01T21:35:07Z")

</div>

Thanks, that is interesting. It doesn’t appear to do the right thing, however:

```julia
julia> vs = [[i, i+1] for i=1:10];

julia> mean(vs .- Ref(mean(vs)))
2-element Array{Float64,1}:
 0.0
 1.0

julia> mean(vs .- [mean(vs)])
2-element Array{Float64,1}:
 0.0
 0.0

julia> [zeros(2)] .+ Ref(mean(vs))
1-element Array{Array{Float64,1},1}:
 [5.5, 5.5]

julia> [zeros(2)] .+ [mean(vs)]
1-element Array{Array{Float64,1},1}:
 [5.5, 6.5]

```

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [December 1, 2017, 10:33pm UTC](https://discourse.julialang.org/t/broadcasting-and-arrays-of-arrays/7444/4 "2017-12-01T22:33:05Z")

</div>

I’m honestly not sure how Ref is supposed to work but I think the reason it doesn’t work here boils down to

```julia
julia> x = Ref([1, 2])
Base.RefArray{Int64,Array{Int64,1},Void}([1, 2], 1, nothing)

julia> x[]
1

```

It does work to instead use Base.RefValue but it’s unexported and undocumented, probably for a reason. Some other options, with or without broadcasting:

```julia
[x - mean(d) for x in d]
map(x -> x - mean(d), d)
(x -> x - mean(d)).(d)

```

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [December 1, 2017, 10:58pm UTC](https://discourse.julialang.org/t/broadcasting-and-arrays-of-arrays/7444/5 "2017-12-01T22:58:34Z")

</div>

Your other options all work, but are going to re-compute `mean(d)` for every element.

I’m also super surprised by the behavior of Ref in this case, but I guess `RefArray`s behave differently in broadcast? In particular, it looks like a RefArray behaves like a scalar but only using the _first_ element of the contained array. A 1-element tuple has the behavior I would expect, as does wrapping the array in a 1-element array.

```julia
julia> [1, 2] .- ([1, 2],)
2-element Array{Array{Int64,1},1}:
 [0, -1]
 [1, 0] 

julia> [1, 2] .- Ref([1, 2]) # behaves like [1, 2] .- [1]
2-element Array{Int64,1}:
 0
 1

julia> [1, 2] .- [[1, 2]]
2-element Array{Array{Int64,1},1}:
 [0, -1]
 [1, 0] 

```

this is surprising enough that I might consider it a broadcast bug.

---

<div class="post-metadata">

**Author:** ![goretkin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goretkin/32/167_2.png) [@goretkin](https://discourse.julialang.org/u/goretkin)\
**Post date:** [December 1, 2017, 11:47pm UTC](https://discourse.julialang.org/t/broadcasting-and-arrays-of-arrays/7444/7 "2017-12-01T23:47:32Z")

</div>

Thanks for the replies. I agree that it’s surprising and can’t think of any reason why it should be like that. We’ll see: [https://github.com/JuliaLang/julia/issues/24880](https://github.com/JuliaLang/julia/issues/24880)

I still hope there is a way to make any expression  
`A - a` or `A .- a` work if `eltype(A) == typeof(a)`, even if `typeof(a)` is an array.

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [December 2, 2017, 1:11am UTC](https://discourse.julialang.org/t/broadcasting-and-arrays-of-arrays/7444/8 "2017-12-02T01:11:52Z")

</div>

> [@goretkin](#):
>
> d .- [mean(d)]

Also `d .- (mean(d),)` works.

---

<div class="post-metadata">

**Author:** ![goretkin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goretkin/32/167_2.png) [@goretkin](https://discourse.julialang.org/u/goretkin)\
**Post date:** [December 3, 2017, 3:01pm UTC](https://discourse.julialang.org/t/broadcasting-and-arrays-of-arrays/7444/9 "2017-12-03T15:01:32Z")

</div>

I just noticed `StaticArrays` has `Scalar`, which seems to do the right thing even for non-StaticArray types:

so yet another option, in the spirit of `Ref` is: `d .- Scalar(mean(d))`

---

<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:** [December 3, 2017, 3:31pm UTC](https://discourse.julialang.org/t/broadcasting-and-arrays-of-arrays/7444/10 "2017-12-03T15:31:24Z")

</div>

Interesting. Having `Scalar` in `Base` would make sense.
