# Calculating all possible combinations

**URL:** <https://discourse.julialang.org/t/calculating-all-possible-combinations/42789>\
**Category:** Performance\
**Created:** [July 9, 2020, 3:28pm UTC](https://discourse.julialang.org/t/calculating-all-possible-combinations/42789 "2020-07-09T15:28:34Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alex\_Z](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex_z/32/9242_2.png) [@Alex\_Z](https://discourse.julialang.org/u/Alex_Z)\
**Post date:** [July 9, 2020, 3:28pm UTC](https://discourse.julialang.org/t/calculating-all-possible-combinations/42789/1 "2020-07-09T15:28:35Z")

</div>

Hello.  
I’m having trouble finding an efficient way to calculate all possible combinations from a set of vectors.  
When they are small using kron[a,b,c] where a,b and c are vectors yields good results but the problem occurs when the vectors get big or there are simply more of them resulting in overflow.

Simple example for the problem is calculating all possible multiplications of primes lower than 6 up to 100.  
Another example: all possible multiples from set of vectors [1,2,3] , [5,2,7] , [2,4,8,16]

Those problems are easy to brute force but when you scale them up it proved to be sort of a problem to me. so I’m open for suggestion about useful algorithms. And thanks 😃

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [July 9, 2020, 3:38pm UTC](https://discourse.julialang.org/t/calculating-all-possible-combinations/42789/2 "2020-07-09T15:38:38Z")

</div>

Maybe the package [Combinatorics.jl](https://github.com/JuliaMath/Combinatorics.jl) has what you need.

Particularly, the function `combinations` produces an iterator on the original collection of elements that you want to combine, so that you don’t need a large copy of all the possible combinations.

---

<div class="post-metadata">

**Author:** ![Alex\_Z](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex_z/32/9242_2.png) [@Alex\_Z](https://discourse.julialang.org/u/Alex_Z)\
**Post date:** [July 9, 2020, 3:42pm UTC](https://discourse.julialang.org/t/calculating-all-possible-combinations/42789/3 "2020-07-09T15:42:06Z")

</div>

I tried it, It returns the number of combinations and not the actual result or indices.  
Though maybe I just couldn’t use it properly, I’m new to Julia.

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [July 9, 2020, 3:52pm UTC](https://discourse.julialang.org/t/calculating-all-possible-combinations/42789/4 "2020-07-09T15:52:23Z")

</div>

No, it returns an iterator over the possible combinations:

```julia
julia> using Combinatorics

julia> x = [[1,2,3], [4,5,6], [7,8,9]]
3-element Array{Array{Int64,1},1}:
 [1, 2, 3]
 [4, 5, 6]
 [7, 8, 9]

julia> combinations_x = combinations(x);

julia> for c in combinations_x
       println(c)
       end
Array{Int64,1}[[1, 2, 3]]
Array{Int64,1}[[4, 5, 6]]
Array{Int64,1}[[7, 8, 9]]
Array{Int64,1}[[1, 2, 3], [4, 5, 6]]
Array{Int64,1}[[1, 2, 3], [7, 8, 9]]
Array{Int64,1}[[4, 5, 6], [7, 8, 9]]
Array{Int64,1}[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

```

(Or use `combinations(x, 2)` if you only want the pairs, etc.)

---

<div class="post-metadata">

**Author:** ![Alex\_Z](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex_z/32/9242_2.png) [@Alex\_Z](https://discourse.julialang.org/u/Alex_Z)\
**Post date:** [July 9, 2020, 3:59pm UTC](https://discourse.julialang.org/t/calculating-all-possible-combinations/42789/5 "2020-07-09T15:59:02Z")

</div>

Thanks, I really almost pulled some hairs from my head.

---

<div class="post-metadata">

**Author:** ![klaff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klaff/32/7637_2.png) [@klaff](https://discourse.julialang.org/u/klaff)\
**Post date:** [July 9, 2020, 4:01pm UTC](https://discourse.julialang.org/t/calculating-all-possible-combinations/42789/6 "2020-07-09T16:01:31Z")

</div>

There is also the built-in function `Iterators.product`:

```julia
help?> Iterators.product
  product(iters...)

  Return an iterator over the product of several iterators. Each generated element is a tuple whose ith element comes from the ith argument iterator. The first iterator changes the fastest.

  Examples
  ≡≡≡≡≡≡≡≡≡≡

  julia> collect(Iterators.product(1:2, 3:5))
  2×3 Array{Tuple{Int64,Int64},2}:
   (1, 3) (1, 4) (1, 5)
   (2, 3) (2, 4) (2, 5)

julia> 

```

---

<div class="post-metadata">

**Author:** ![Alex\_Z](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex_z/32/9242_2.png) [@Alex\_Z](https://discourse.julialang.org/u/Alex_Z)\
**Post date:** [July 9, 2020, 6:43pm UTC](https://discourse.julialang.org/t/calculating-all-possible-combinations/42789/7 "2020-07-09T18:43:37Z")

</div>

Mind helping me a bit with the syntax?  
I wrote a code and I’m trying to generalize it for future uses but the syntax keeps fighting me.  
my problem is with this line (val is an array of arrays, so `val[i]` is an array):  
`check2 = collect(Iterators.product(val[1],val[2],val[3]))`  
That line works but it’s too specific to be generalized. What I mean is that I got an array of arrays of different sizes and I want to write something like that:  
`check2 = collect(Iterators.product(val))`

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [July 9, 2020, 6:47pm UTC](https://discourse.julialang.org/t/calculating-all-possible-combinations/42789/8 "2020-07-09T18:47:10Z")

</div>

You want to splat: `Iterators.product(val...)`

---

<div class="post-metadata">

**Author:** ![Alex\_Z](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex_z/32/9242_2.png) [@Alex\_Z](https://discourse.julialang.org/u/Alex_Z)\
**Post date:** [July 9, 2020, 6:52pm UTC](https://discourse.julialang.org/t/calculating-all-possible-combinations/42789/9 "2020-07-09T18:52:04Z")

</div>

Damn, never thought it will be that simple. Thanks man! 👌
