# Tuples of CartesianIndex and broadcasting

**URL:** <https://discourse.julialang.org/t/tuples-of-cartesianindex-and-broadcasting/50093>\
**Category:** General Usage\
**Tags:** broadcasting\
**Created:** [November 13, 2020, 2:24pm UTC](https://discourse.julialang.org/t/tuples-of-cartesianindex-and-broadcasting/50093 "2020-11-13T14:24:18Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sleort](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sleort/32/4875_2.png) [@Sleort](https://discourse.julialang.org/u/Sleort)\
**Post date:** [November 13, 2020, 2:24pm UTC](https://discourse.julialang.org/t/tuples-of-cartesianindex-and-broadcasting/50093/1 "2020-11-13T14:24:18Z")

</div>

While I can broadcast scalars, like

```julia
julia> 1 .+ (2, 3)
(3, 4)

```

I can’t do the same with `CartesianIndex`s:

```julia
julia> CartesianIndex(1,1) .+ (CartesianIndex(2,2), CartesianIndex(3,3))
ERROR: iteration is deliberately unsupported for CartesianIndex. Use `I` rather than `I...`, or use `Tuple(I)...`
Stacktrace:
 [1] error(::String) at ./error.jl:33
 [2] iterate(::CartesianIndex{2}) at ./multidimensional.jl:166
 [3] copyto!(::Array{Int64,1}, ::CartesianIndex{2}) at ./abstractarray.jl:733
 [4] _collect(::UnitRange{Int64}, ::CartesianIndex{2}, ::Base.HasEltype, ::Base.HasLength) at ./array.jl:630
 [5] collect(::CartesianIndex{2}) at ./array.jl:624
 [6] broadcastable(::CartesianIndex{2}) at ./broadcast.jl:682
 [7] broadcasted(::Function, ::CartesianIndex{2}, ::Tuple{CartesianIndex{2},CartesianIndex{2}}) at ./broadcast.jl:1260
 [8] top-level scope at REPL[15]:1

```

I understand that `1 .+ CartesianIndex(1,1)` fails, but since `CartesianIndex(1,1) + CartesianIndex(1,1)` is fine, I would expect the above example to work. Is the behavior intended, or a bug?

(I can of course write

```julia
julia> (CartesianIndex(1,1),) .+ (CartesianIndex(2,2), CartesianIndex(3,3))
(CartesianIndex(3, 3), CartesianIndex(4, 4))

```

instead. But that’s less elegant… and beside the point.)

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [November 13, 2020, 2:34pm UTC](https://discourse.julialang.org/t/tuples-of-cartesianindex-and-broadcasting/50093/2 "2020-11-13T14:34:15Z")

</div>

I think the problem there is that there is a possible ambiguity on whether the broadcast is over the indexes of the tuple or the indexes of the CartesianIndex.

---

<div class="post-metadata">

**Author:** ![Sleort](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sleort/32/4875_2.png) [@Sleort](https://discourse.julialang.org/u/Sleort)\
**Post date:** [November 13, 2020, 2:38pm UTC](https://discourse.julialang.org/t/tuples-of-cartesianindex-and-broadcasting/50093/3 "2020-11-13T14:38:18Z")

</div>

Hmm… But since broadcasting over the indexes of the `CartesianIndex` is not allowed (`1 .+ CartesianIndex(1,1)` errors), I would assume that we are left with only one possibility, no?

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [November 13, 2020, 2:43pm UTC](https://discourse.julialang.org/t/tuples-of-cartesianindex-and-broadcasting/50093/4 "2020-11-13T14:43:44Z")

</div>

You can write `Ref(CartesianIndex(1,1)) .+ stuff` to protect it from the broadcast. (Similar to `(... ,)`, as I see now you said.) I think the way to specify that a type is always scalar with respect to broadcasting is

```julia
Broadcast.broadcastable(I::CartesianIndex) = Ref(I)

```

I wonder if it would break anything to define this?

---

<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:** [November 13, 2020, 2:48pm UTC](https://discourse.julialang.org/t/tuples-of-cartesianindex-and-broadcasting/50093/5 "2020-11-13T14:48:00Z")

</div>

We’ve been hesitant to make CartesianIndexes collection-like (that is, iterable and broadcastable) because their reason for being is the they represent _a single location_. Generally I’ll just convert them to Tuples when I want to treat them as a list of indices.

---

<div class="post-metadata">

**Author:** ![Sleort](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sleort/32/4875_2.png) [@Sleort](https://discourse.julialang.org/u/Sleort)\
**Post date:** [November 13, 2020, 2:51pm UTC](https://discourse.julialang.org/t/tuples-of-cartesianindex-and-broadcasting/50093/6 "2020-11-13T14:51:32Z")

</div>

Maybe I’m misunderstanding, but I’m arguing for the opposite, namely to treat CartesianIndexes as “single locations”/scalars “all the way” (i.e. _not_ collection-like). If that were the case, my example above would not fail…

---

<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:** [November 13, 2020, 2:56pm UTC](https://discourse.julialang.org/t/tuples-of-cartesianindex-and-broadcasting/50093/7 "2020-11-13T14:56:00Z")

</div>

Oh, you’re absolutely right! 90% of the requests we get for this are the other way, and I simply didn’t read closely enough.

Yes, I think making it broadcast like a scalar would be reasonable — and nonbreaking since it errors now.

---

<div class="post-metadata">

**Author:** ![Sleort](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sleort/32/4875_2.png) [@Sleort](https://discourse.julialang.org/u/Sleort)\
**Post date:** [November 13, 2020, 2:59pm UTC](https://discourse.julialang.org/t/tuples-of-cartesianindex-and-broadcasting/50093/8 "2020-11-13T14:59:54Z")

</div>

Well, I’m a 100% with the 10% then 😄

Should I file an issue?

---

<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:** [November 13, 2020, 3:06pm UTC](https://discourse.julialang.org/t/tuples-of-cartesianindex-and-broadcasting/50093/9 "2020-11-13T15:06:30Z")

</div>

Please do!

---

<div class="post-metadata">

**Author:** ![Sleort](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sleort/32/4875_2.png) [@Sleort](https://discourse.julialang.org/u/Sleort)\
**Post date:** [November 13, 2020, 3:15pm UTC](https://discourse.julialang.org/t/tuples-of-cartesianindex-and-broadcasting/50093/10 "2020-11-13T15:15:19Z")

</div>

[Done](https://github.com/JuliaLang/julia/issues/38432)
