user3749697
user3749697

Reputation: 51

Julia : Cartesian product of multiple arrays

I would like to compute a product iterator using Iterators.jl. Let's say I have an array of UnitRanges tab with a priori unknown size.

I would like to compute the cartesian product of the elements of tab.

For example if tab length is 2 and tab[1] = a and tab[2] = b I want to compute product(a,b) from Iterators.jl.

I want to make a generic function that compute the cartesian product of every component in tab.

I tried something like this

prod = tab[1]
for i in tab[2:end]
   prod = product(prod,i)
end

However if tab is length 3, components a,b and c, I obtain in prod elements under the form (1,(3,2)) and not (1,3,2). With 1 element of c, 3 element of b and 2 element of a.

Upvotes: 4

Views: 2978

Answers (1)

Fengyang Wang
Fengyang Wang

Reputation: 12051

In v0.5, there is now Base.product, which is much better than Iterators.product.

It can handle as many arrays as needed, and it even has a shape:

julia> collect(Base.product([1, 2], [3, 4]))
2×2 Array{Tuple{Int64,Int64},2}:
 (1,3)  (1,4)
 (2,3)  (2,4)

julia> collect(Base.product(1:5, 1:3, 1:2, 1:2))
5×3×2×2 Array{NTuple{4,Int64},4}:
[:, :, 1, 1] =
 (1,1,1,1)  (1,2,1,1)  (1,3,1,1)
 (2,1,1,1)  (2,2,1,1)  (2,3,1,1)
 (3,1,1,1)  (3,2,1,1)  (3,3,1,1)
 (4,1,1,1)  (4,2,1,1)  (4,3,1,1)
 (5,1,1,1)  (5,2,1,1)  (5,3,1,1)

[:, :, 2, 1] =
 (1,1,2,1)  (1,2,2,1)  (1,3,2,1)
 (2,1,2,1)  (2,2,2,1)  (2,3,2,1)
 (3,1,2,1)  (3,2,2,1)  (3,3,2,1)
 (4,1,2,1)  (4,2,2,1)  (4,3,2,1)
 (5,1,2,1)  (5,2,2,1)  (5,3,2,1)

[:, :, 1, 2] =
 (1,1,1,2)  (1,2,1,2)  (1,3,1,2)
 (2,1,1,2)  (2,2,1,2)  (2,3,1,2)
 (3,1,1,2)  (3,2,1,2)  (3,3,1,2)
 (4,1,1,2)  (4,2,1,2)  (4,3,1,2)
 (5,1,1,2)  (5,2,1,2)  (5,3,1,2)

[:, :, 2, 2] =
 (1,1,2,2)  (1,2,2,2)  (1,3,2,2)
 (2,1,2,2)  (2,2,2,2)  (2,3,2,2)
 (3,1,2,2)  (3,2,2,2)  (3,3,2,2)
 (4,1,2,2)  (4,2,2,2)  (4,3,2,2)
 (5,1,2,2)  (5,2,2,2)  (5,3,2,2)

The shape is extremely useful for map. For instance, here's how to create a multiplication table using Base.product:

julia> map(prod, Base.product(1:9, 1:9))
9×9 Array{Int64,2}:
 1   2   3   4   5   6   7   8   9
 2   4   6   8  10  12  14  16  18
 3   6   9  12  15  18  21  24  27
 4   8  12  16  20  24  28  32  36
 5  10  15  20  25  30  35  40  45
 6  12  18  24  30  36  42  48  54
 7  14  21  28  35  42  49  56  63
 8  16  24  32  40  48  56  64  72
 9  18  27  36  45  54  63  72  81

Of course, if you don't need the shape, then you are free to ignore it — it will still iterate properly.

And Base.product is fast too!

Upvotes: 9

Related Questions