# Possible bug in broadcasting operation on array of tuples

**URL:** <https://discourse.julialang.org/t/possible-bug-in-broadcasting-operation-on-array-of-tuples/6547>\
**Category:** General Usage\
**Tags:** broadcast, tuple\
**Created:** [October 19, 2017, 6:18am UTC](https://discourse.julialang.org/t/possible-bug-in-broadcasting-operation-on-array-of-tuples/6547 "2017-10-19T06:18:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![colintbowers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/colintbowers/32/8033_2.png) [@colintbowers](https://discourse.julialang.org/u/colintbowers)\
**Post date:** [October 19, 2017, 6:18am UTC](https://discourse.julialang.org/t/possible-bug-in-broadcasting-operation-on-array-of-tuples/6547/1 "2017-10-19T06:18:01Z")

</div>

Hi all,

I just encountered the following behaviour in v0.6 on Ubuntu:

```julia
julia> (1,1) .!= [(1,1), (2,1)]
2-element BitArray{1}:
 true
 true

```

but if I add one more element to the RHS vector I get an error:

```julia
julia> (1,1) .!= [(1,1), (2,1), (1,1)]
ERROR: DimensionMismatch("arrays could not be broadcast to a common size")
Stacktrace:
 [1] _bcs1(::Base.OneTo{Int64}, ::Base.OneTo{Int64}) at ./broadcast.jl:70
 [2] _bcs at ./broadcast.jl:63 [inlined]
 [3] broadcast_shape at ./broadcast.jl:57 [inlined] (repeats 2 times)
 [4] broadcast_indices at ./broadcast.jl:53 [inlined]
 [5] broadcast_c at ./broadcast.jl:311 [inlined]
 [6] broadcast(::Function, ::Tuple{Int64,Int64}, ::Array{Tuple{Int64,Int64},1}) at ./broadcast.jl:434

```

Note, you can get around this by making the LHS an array too, ie:

```julia
julia> [(1,1)] .!= [(1,1), (2,1), (1,1)]
3-element BitArray{1}:
 false
  true
 false

```

It feels like a bug to me, but I’ve been completely wrong about this sort of stuff before, so I thought I’d ask here before filing an issue.

Cheers,

Colin

---

<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:** [October 19, 2017, 8:38am UTC](https://discourse.julialang.org/t/possible-bug-in-broadcasting-operation-on-array-of-tuples/6547/2 "2017-10-19T08:38:58Z")

</div>

The problem is that tuples themselves broadcast:

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

```

See `?broadcast` for protecting arguments, eg

```julia
julia> Ref((1,1)) .!= [(1,1), (2,1), (1,1)]
3-element BitArray{1}:
 false
  true
 false

```

---

<div class="post-metadata">

**Author:** ![anon61610682](https://avatars.discourse-cdn.com/v4/letter/a/ad7895/32.png) [@anon61610682](https://discourse.julialang.org/u/anon61610682)\
**Post date:** [October 19, 2017, 8:41am UTC](https://discourse.julialang.org/t/possible-bug-in-broadcasting-operation-on-array-of-tuples/6547/3 "2017-10-19T08:41:04Z")

</div>

I think you will understand the behaviour when you read the `?broadcast` REPL documentation and possibly check the code at `base/broadcast.jl`.

You can try this:

```julia
julia> (1,1) .!= [(1,1)]
2-element BitArray{1}:
 true
 true

```

As you can see, this behaviour is different from what you are expecting. `Tuple` is not treated as a single element in `broadcast`, and hence, you see a call in `base/broadcast.jl` (`broadcast_indices`) to `Base.OneTo(length(A))` for `::Type{Tuple}`. The above example broadcasts the operation as in:

```julia
[1 != (1,1);
 1 != (1,1)]

```

You can check the below to persuade yourself:

```julia
julia> (1,1) .!= [(1,1),1]
2-element BitArray{1}:
  true
 false

```

Please also note the sentence in `?broadcast`:

```julia
In this context, anything that is not a subtype of AbstractArray, Ref (except for Ptrs), Tuple, or
  Nullable is considered a scalar.

```

---

<div class="post-metadata">

**Author:** ![colintbowers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/colintbowers/32/8033_2.png) [@colintbowers](https://discourse.julialang.org/u/colintbowers)\
**Post date:** [October 19, 2017, 9:45am UTC](https://discourse.julialang.org/t/possible-bug-in-broadcasting-operation-on-array-of-tuples/6547/4 "2017-10-19T09:45:37Z")

</div>

Ah, I get it. Thanks for the explanation. This is why I asked here instead of filing an issue 🙂

Cheers,

Colin

---

<div class="post-metadata">

**Author:** ![colintbowers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/colintbowers/32/8033_2.png) [@colintbowers](https://discourse.julialang.org/u/colintbowers)\
**Post date:** [October 19, 2017, 9:46am UTC](https://discourse.julialang.org/t/possible-bug-in-broadcasting-operation-on-array-of-tuples/6547/5 "2017-10-19T09:46:20Z")

</div>

Thanks, this was a helpful addition to the other responder. I understand how it works now.

Cheers,

Colin

---

<div class="post-metadata">

**Author:** ![anon61610682](https://avatars.discourse-cdn.com/v4/letter/a/ad7895/32.png) [@anon61610682](https://discourse.julialang.org/u/anon61610682)\
**Post date:** [October 19, 2017, 10:02am UTC](https://discourse.julialang.org/t/possible-bug-in-broadcasting-operation-on-array-of-tuples/6547/6 "2017-10-19T10:02:10Z")

</div>

Actually I hadn’t even noticed @Tamas_Papp’s response while writing mine. Sorry for double posting. I am glad that mine also added to the clarity, though.
