# Surprising dot broadcast result when indices are repeated?

**URL:** <https://discourse.julialang.org/t/surprising-dot-broadcast-result-when-indices-are-repeated/61062>\
**Category:** General Usage\
**Tags:** question, indexing, broadcast, arrays\
**Created:** [May 13, 2021, 6:21am UTC](https://discourse.julialang.org/t/surprising-dot-broadcast-result-when-indices-are-repeated/61062 "2021-05-13T06:21:30Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![hsgg](https://avatars.discourse-cdn.com/v4/letter/h/838e76/32.png) [@hsgg](https://discourse.julialang.org/u/hsgg)\
**Post date:** [May 13, 2021, 6:21am UTC](https://discourse.julialang.org/t/surprising-dot-broadcast-result-when-indices-are-repeated/61062/1 "2021-05-13T06:21:30Z")

</div>

I am confused about the following code. I have an indexing array `pixmap` to select which elements to add to:

```julia
julia> n = 10
julia> pixmap = fill(1, n)
julia> v = fill(0, n)
julia> @. v[pixmap] += 1
julia> v
10-element Vector{Int64}:
 1
 0
 0
 0
 0
 0
 0
 0
 0
 0

```

I find this outcome surprising. I would have expected `v[1]=10`. Can [`@ __dot__ `](https://docs.julialang.org/en/v1/base/arrays/#Base.Broadcast.@ __dot__ ) not handle indexing to the same element multiple times?

I am on julia-1.6.1 on WSL2/Ubuntu.

---

<div class="post-metadata">

**Author:** ![tomaklutfu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomaklutfu/32/2411_2.png) [@tomaklutfu](https://discourse.julialang.org/u/tomaklutfu)\
**Post date:** [May 13, 2021, 7:25am UTC](https://discourse.julialang.org/t/surprising-dot-broadcast-result-when-indices-are-repeated/61062/2 "2021-05-13T07:25:10Z")

</div>

Since you directly indexing with array you get a temporary Array for `v[pixmap]` in rhs so it is adding 1 to 1 10 times and stores at location 1. You need to use views to affect both sides of the assignment.

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [May 13, 2021, 7:26am UTC](https://discourse.julialang.org/t/surprising-dot-broadcast-result-when-indices-are-repeated/61062/3 "2021-05-13T07:26:47Z")

</div>

The reason is that:

```julia
v[pixmap] += 1

```

is in this case the same as:

```julia
copyto!(view(v, pixmap), v[pixmap] .+ 1)

```

so you have `1` set to index `1` of `v` 10 times.

For the same reason you have:

```julia
julia> v = fill(0, n)
10-element Vector{Int64}:
 0
 0
 0
 0
 0
 0
 0
 0
 0
 0

julia> @. v[pixmap] += 1:10
10-element view(::Vector{Int64}, [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]) with eltype Int64:
 10
 10
 10
 10
 10
 10
 10
 10
 10
 10

julia> v
10-element Vector{Int64}:
 10
  0
  0
  0
  0
  0
  0
  0
  0
  0

```

(i.e. the last computed value is eventually stored in `v`)

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [May 13, 2021, 7:33am UTC](https://discourse.julialang.org/t/surprising-dot-broadcast-result-when-indices-are-repeated/61062/4 "2021-05-13T07:33:31Z")

</div>

> [@tomaklutfu](#):
>
> You need to use views to affect both sides of the assignment.

I do not think using views would resolve the issue as broadcasting will do unaliasing and the result would be the same. Could you post what you exactly meant? Thank you!

---

<div class="post-metadata">

**Author:** ![tomaklutfu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomaklutfu/32/2411_2.png) [@tomaklutfu](https://discourse.julialang.org/u/tomaklutfu)\
**Post date:** [May 13, 2021, 8:39am UTC](https://discourse.julialang.org/t/surprising-dot-broadcast-result-when-indices-are-repeated/61062/5 "2021-05-13T08:39:03Z")

</div>

When I use `@views @. v[pixmap] += 1`. I get 10 as expected for this case but you are right there are cases where there is alias check but not for this one though.

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [May 13, 2021, 9:06am UTC](https://discourse.julialang.org/t/surprising-dot-broadcast-result-when-indices-are-repeated/61062/6 "2021-05-13T09:06:45Z")

</div>

Ah, you are right:

```julia
julia> n = 10;

julia> pixmap = fill(1, n);

julia> v = fill(0, n);

julia> v1 = @view v[pixmap];

julia> v2 = @view v[pixmap];

julia> @. v1 .= v2 + 1;

julia> v
10-element Vector{Int64}:
 10
  0
  0
  0
  0
  0
  0
  0
  0
  0

julia> v = fill(0, n);

julia> v1 = @view v[pixmap];

julia> v2 = @view v[copy(pixmap)];

julia> @. v1 .= v2 + 1;

julia> v
10-element Vector{Int64}:
 1
 0
 0
 0
 0
 0
 0
 0
 0
 0

```

and this difference is caused by:

```julia
broadcast_unalias(dest, src) = dest === src ? src : unalias(dest, src)

```

definition (the alias check is triggered, but in this case Julia Base decides not to perform unaliasing).

@mbauman - this is a corner case, but the question is if we want to accept it as intended, or something should be fixed here?

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [May 13, 2021, 12:43pm UTC](https://discourse.julialang.org/t/surprising-dot-broadcast-result-when-indices-are-repeated/61062/7 "2021-05-13T12:43:29Z")

</div>

I believe this might be fixed by [RFC: Fix #31392, unaliasing of broadcast arguments against destinations with repeated indices by mbauman · Pull Request #31407 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/31407)

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [May 13, 2021, 2:01pm UTC](https://discourse.julialang.org/t/surprising-dot-broadcast-result-when-indices-are-repeated/61062/8 "2021-05-13T14:01:33Z")

</div>

Yes - this should fix this.

@hsgg - after this fix what @tomaklutfu proposes will not work as it does now and you will get `1` always (if I understand things correctly).

---

<div class="post-metadata">

**Author:** ![hsgg](https://avatars.discourse-cdn.com/v4/letter/h/838e76/32.png) [@hsgg](https://discourse.julialang.org/u/hsgg)\
**Post date:** [May 13, 2021, 4:44pm UTC](https://discourse.julialang.org/t/surprising-dot-broadcast-result-when-indices-are-repeated/61062/9 "2021-05-13T16:44:34Z")

</div>

Thanks @bkamins @mbauman @tomaklutfu ! Writing it as ([#31392](https://github.com/JuliaLang/julia/issues/31392))

```julia
v[[1,1]] .= [1,2]

```

makes it obvious that the correct answer is not obvious.

> [@mbauman](#):
>
> I believe this might be fixed by [https://github.com/JuliaLang/julia/pull/31407](https://github.com/JuliaLang/julia/pull/31407)

I think this is an acceptable fix, and people like me will have to write the loop explicitly.

EDIT: Made the example even simpler.

---

<div class="post-metadata">

**Author:** ![hsgg](https://avatars.discourse-cdn.com/v4/letter/h/838e76/32.png) [@hsgg](https://discourse.julialang.org/u/hsgg)\
**Post date:** [May 13, 2021, 6:40pm UTC](https://discourse.julialang.org/t/surprising-dot-broadcast-result-when-indices-are-repeated/61062/10 "2021-05-13T18:40:02Z")

</div>

Thinking about this some more, the semantics I am really after is something between `map()` and `mapreduce()`, like “`mapsemireduce()`”. That is, consider an array of size n. Then, `map()` maps this array onto an array of the same size, and `mapreduce()` maps it onto an array of size 1, and `mapsemireduce()` would map it to an array of size m\<n. Really, it is `mapreduce()` on subarrays. I don’t suppose such a programming paradigm already exists?
