I need to store in memory an array of the form
a=Table[Tuples[{1, 2, 3}, {n}], {n, 1, MaxLength}]
When MaxLength larger than - say - 20, this obviously results in an enormous array. In the ideal application, MaxLength is of the order of 30-40.
Is there a way to store arrays of this type more efficiently? In this example, the values 1,2,3 are the only ones that the array takes. Other arrays in my code would need to store floating-point variables rather than integers, with the same non-rectangular structure.
As a way of focusing the discussion, I am aware of two workarounds that would limit memory usage, but make the code significantly slower: I could generate in memory only the particular tuple n
I need every time; or, I might store these elements in files on disk, and load in memory only the elements I need.
Both solutions would slow down the code significantly - there are loops over n
that access the elements of a
.
Thank you.