# How to do "((1,2), 3) .+ ((1,2), 3)"?

**URL:** <https://discourse.julialang.org/t/how-to-do-1-2-3-1-2-3/43582>\
**Category:** General Usage\
**Created:** [July 23, 2020, 7:57pm UTC](https://discourse.julialang.org/t/how-to-do-1-2-3-1-2-3/43582 "2020-07-23T19:57:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pablosanjose](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pablosanjose/32/7006_2.png) [@pablosanjose](https://discourse.julialang.org/u/pablosanjose)\
**Post date:** [July 23, 2020, 7:57pm UTC](https://discourse.julialang.org/t/how-to-do-1-2-3-1-2-3/43582/1 "2020-07-23T19:57:32Z")

</div>

TL;DR: How can we “broadcast” a function over several nested Tuples such as `((1,2), 3) .+ ((1,2), 3)`?

So, say I have several objects `x_i` that are tuples of tuples and numbers (each of the tuples itself a tuple of tuples and numbers). For example `x_1 = ((1,2), 3)` or `x_1 = (1, (2,3,(3,4)), (2,3))`, etc… Now assume all `x_i` have the same structure, just different numbers. How can we apply a function `f` elementwise recursively, i.e. to arbitrary Tuple depth to a collection of `x_i`?

Currently `((1,2), 3) .+ ((1,2), 3)` fails because it does `+` only at the first level, and tuples themselves cannot be summed. I know broadcast is what it is, and it’s not exactly this, so I’m looking for something different. I’d like it to yield `((1+1, 2+2), 3+3)` etc. Is this possible? Can we make it work it with arbitrary tuple nesting?

Hopefully the question is clear, please ask otherwise.

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [July 23, 2020, 8:33pm UTC](https://discourse.julialang.org/t/how-to-do-1-2-3-1-2-3/43582/2 "2020-07-23T20:33:01Z")

</div>

If you’re doing math with tuples, consider using the `StaticArrays` packages instead. It gives you immutable arrays with arithmetic and linear algebra defined on them.

That said:

```julia
_f(x, y) = x + y
_f(x::Tuple, y::Tuple) = _f.(x, y)

```

```julia
julia> t1 = ((1,2), (3, (5,6)), (7, 8), 9);

julia> _f(t1, t1)
((2, 4), (6, (10, 12)), (14, 16), 18)

```

For numbers and Tuples of numbers, the above will work. Use at your own risk!

---

<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:** [July 23, 2020, 8:39pm UTC](https://discourse.julialang.org/t/how-to-do-1-2-3-1-2-3/43582/3 "2020-07-23T20:39:12Z")

</div>

Here’s a crazy hack:

```nohighlight
julia> ⊕(x, y) = x .⊕ y
       ⊕(x::Number, y::Number) = x + y
⊕ (generic function with 2 methods)

julia> ((1, 2), 3) .⊕ ((4, 5), 6)
((5, 7), 9)

```

It may behave suprisingly with unmatched tuple lengths and will stack-overflow with non-number-containing things.

---

<div class="post-metadata">

**Author:** ![pablosanjose](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pablosanjose/32/7006_2.png) [@pablosanjose](https://discourse.julialang.org/u/pablosanjose)\
**Post date:** [July 23, 2020, 8:50pm UTC](https://discourse.julialang.org/t/how-to-do-1-2-3-1-2-3/43582/4 "2020-07-23T20:50:41Z")

</div>

Thanks! Yeah, I was rather looking for a solution more like broadcast or map that does not require to touch the methods of the function f. But I guess these options are probably the simplest

---

<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:** [July 23, 2020, 8:53pm UTC](https://discourse.julialang.org/t/how-to-do-1-2-3-1-2-3/43582/5 "2020-07-23T20:53:41Z")

</div>

If you only need to go down one level, you could just broadcast a broadcasted operator. Hopefully someday soon you’ll be able to do `(.+).(((1, 2), 3), ((4, 5), 6))` or `broadcast(.+, ((1, 2), 3), ((4, 5), 6))`, but until then you could do:

```julia
julia> broadcast((args...)->(+).(args...), ((1, 2), 3), ((4, 5), 6))
((5, 7), 9)

```

---

<div class="post-metadata">

**Author:** ![pablosanjose](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pablosanjose/32/7006_2.png) [@pablosanjose](https://discourse.julialang.org/u/pablosanjose)\
**Post date:** [July 23, 2020, 8:55pm UTC](https://discourse.julialang.org/t/how-to-do-1-2-3-1-2-3/43582/6 "2020-07-23T20:55:46Z")

</div>

Very nice! I think I’ll mark @tomerarnon’s reply as solution, thanks again to both.
