# How to multiply tuples and how to change a format of summation?

**URL:** <https://discourse.julialang.org/t/how-to-multiply-tuples-and-how-to-change-a-format-of-summation/82928>\
**Category:** General Usage\
**Tags:** question, jump, tuple, vector, sum\
**Created:** [June 17, 2022, 5:03pm UTC](https://discourse.julialang.org/t/how-to-multiply-tuples-and-how-to-change-a-format-of-summation/82928 "2022-06-17T17:03:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kevin\_12](https://avatars.discourse-cdn.com/v4/letter/k/ba8739/32.png) [@Kevin\_12](https://discourse.julialang.org/u/Kevin_12)\
**Post date:** [June 17, 2022, 5:03pm UTC](https://discourse.julialang.org/t/how-to-multiply-tuples-and-how-to-change-a-format-of-summation/82928/1 "2022-06-17T17:03:59Z")

</div>

If you have a set `A = [(1,4,7), (2,5,3), (5,4,9), (2,8,7), (8,6,7), (2,3,5)] `.Is it possible to derive:

```julia
#1 another set A' such that A' = 1.2*A  
each element of A' is the scaled version of A 

```

```julia
#2 define a variable like x and a summation like the following
 
x[1,4,7] + x[2,5,3]+ x[5,4,9]+x[2,8,7]+x[8,6,7]+x[2,3,5] = 5
# not like the following
x[(1,4,7)] + x[(2,5,3)]+ x[(5,4,9)]+x[(2,8,7)]+x[(8,6,7)]+x[(2,3,5)] = 5

```

The only thing operator I know for this case is `...` but this is not useful either.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [June 17, 2022, 10:32pm UTC](https://discourse.julialang.org/t/how-to-multiply-tuples-and-how-to-change-a-format-of-summation/82928/3 "2022-06-17T22:32:45Z")

</div>

```julia
#with ...
formsum(A,x :: Array{T,2}) where T<: Number =sum([x*[t...] for t in A])

#without ...
formsumf(A,x :: Array{T,2}) where T<: Number = sum(x*Base.splat(hcat)(Base.BroadcastFunction(Base.splat(Base.vect))(A)))

```

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [June 20, 2022, 3:03pm UTC](https://discourse.julialang.org/t/how-to-multiply-tuples-and-how-to-change-a-format-of-summation/82928/4 "2022-06-20T15:03:24Z")

</div>

It looks like [`CartesianIndex`](https://docs.julialang.org/en/v1/manual/arrays/#Cartesian-indices) might be useful to you.

`sum(z->x[CartesianIndex(z)], A)`

To save a step (or rather to do it earlier), you could make `A` an array of `CartesianIndex{3}` rather than 3-tuples.

As for question #1, it’s not pretty but if you want to multiply every element of `A` every tuple by a constant you can use nested broadcasting with carefully placed dots (although it isn’t pretty):  
`(.*).(A, 1.2)`  
Note that this won’t work if you make `A` an array of `CartesianIndex` like I’d suggested above, at least not for float factors like your `1.2` since `CartesianIndex` only supports integers.

Also, don’t be afraid to write `for` loops in Julia if you can’t figure out how to write something using fancy functions. They aren’t performance traps like in some other languages.
