3
$\begingroup$

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?

$\endgroup$
3
  • 2
    $\begingroup$ 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. $\endgroup$ Commented Sep 30, 2021 at 15:33
  • $\begingroup$ 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. $\endgroup$ Commented Sep 30, 2021 at 19:54
  • $\begingroup$ a workaorund: use V = With[{a=1./A},Table[1, {i, 0,1,a}, {j,0,1,a}, {k,0,1,a}] ];? $\endgroup$ Commented Oct 1, 2021 at 12:33

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.