3

I would expect the following code to produce a packed 11x11x11 element array of 64 bit integers, but it doesn't:

A = 10;
V = Table[1, {i, 0, 1, 1/A}, {j, 0, 1, 1/A}, {k, 0, 1, 1/A}] ;
Developer`PackedArrayQ[V] (* returns false *)

However, if we just type the stepwise in explicitly, then the array is packed:

V = Table[1, {i, 0, 1, 0.1}, {j, 0, 1, 0.1}, {k, 0, 1, 0.1}] ;
Developer`PackedArrayQ[V] (* returns true *)

What is going on here?

Bruce Bartlett
  • 455
  • 2
  • 7
  • 2
    Rational numbers cannot be packed. Table[1, {i, 0, 1, 1/10}, {j, 0, 1, 0.1}, {k, 0, 1, 0.1}] is not packed either. The code seems to be analyzed before the subroutine that calculates the table is chosen. With rational or symbolic indices, a non-packing routine is called apparently, at least sometimes. BTW, this seems a better first example: B = 0.1; V = Table[1, {i, 0, 1, 0.1}, {j, 0, 1, 0.1}, {k, 0, 1, B}]; because it is clearly equivalent to the second except for the variable in the iterator. And unexpectedly, Table[1, {i, 0, 1, B}, {j, 0, 1, 0.1}, {k, 0, 1, 0.1}] is packed. – Michael E2 Sep 30 '21 at 15:33
  • Ok, thank you for your analysis. Very strange behaviour. Quite annoying because I need to generate a big array, based on some number of steps (A) that I would like to change and experiment with, so I'd like to fix A in the beginning and then use it in the generation of the array. Hopefully there is some workaround. – Bruce Bartlett Sep 30 '21 at 19:54
  • a workaorund: use V = With[{a=1./A},Table[1, {i, 0,1,a}, {j,0,1,a}, {k,0,1,a}] ];? – kglr Oct 01 '21 at 12:33

0 Answers0