Iterators.product OutOfMemoryError()

Hi,

I have the following code:


ary_comb_n=collect(Iterators.product(n_max_O...)) 

Where n_max_O is an array with vectors like this [ [0,1,2,3,4] ,[0,1,2] ].

Iterators.product gives me the combinatory of all the vectors in the variable. Ex.:

5×3 Matrix{Tuple{Int64, Int64}}:

(0, 0) (0, 1) (0, 2)

(1, 0) (1, 1) (1, 2)

(2, 0) (2, 1) (2, 2)

(3, 0) (3, 1) (3, 2)

(4, 0) (4, 1) (4, 2)

I use each combination later to multiplicate a vector. Like (0,0)*(x,y)=z and I collect these numbers.

My problem is that n_max_O is variable and can have up to 60 vectors inside it. When I try to generate that I run out of memory.

Ex. for 20 vectors:

[[0, 1], [0, 1, 2], [0, 1], [0, 1, 2], [0, 1, 2, 3], [0, 1], [0, 1, 2], [0, 1], [0, 1, 2], [0, 1, 2, 3], [0, 1],

[0, 1, 2], [0, 1], [0, 1, 2], [0, 1, 2, 3], [0, 1], [0, 1, 2], [0, 1], [0, 1, 2], [0, 1, 2, 3]]

Is there a better way to do this? Or to generate the combinatory as I use it?

Thanks!

That’s exactly what Iterators.product already does. The problem is that you’re calling collect() which tries to collect that sequence into a vector. Just don’t do that:

ary_comb_n = Iterators.product(...)
2 Likes

Thank you!!