Table support list as iterator, for example

list1=Table[w, {w, Range[1., 10., 0.01]}];

We can confirm that list1 is packed array by


But now look at this

Table[w + ky, {w, Range[1., 10., 0.01]}, {ky, 
   Range[1., 10., 0.01]}] //Developer`PackedArrayQ

it gives unpacked array !! On the other hand

Table[w + ky, {w, 1., 10., 0.01}, {ky, 
   1., 10., 0.01}] //Developer`PackedArrayQ

What is wrong with List as iterator?

I am using Mathematica 10.3 on win 8


egwene sedai provides a reasonable explanation.

The conclusion is that: If there is inner loop, then outer iterator must be numerically explicit to make results packed.

There is several work around to avoid unpacking

1.evaluate iterator explicitly

Table[w + ky, Evaluate@{w, Range[1., 10., 0.01]}, Evaluate@{ky, 
Range[1., 10., 0.01]}] //Developer`PackedArrayQ

2.make sub Table "one dimensional". Don't write nested iterator.

Table[Table[w + ky, {w, Range[1., 10., 0.01]}], {ky, 
Range[1., 10., 0.01]}] // Developer`PackedArrayQ
  • 3
    $\begingroup$ Mathematica never promised to give you a packed array :D, anyway, it's a interesting observation. $\endgroup$ – xzczd Feb 9 '16 at 14:24
  • $\begingroup$ @xzczd I think it might be a bug... $\endgroup$ – matheorem Feb 9 '16 at 14:26
  • $\begingroup$ I don't know, it seems like a bug is when it specifically does something different than what it claims to do. The program seems to decide based on unknown criteria whether to pack an array. You can always use ToPackedArray if you need it packed of course. Even odder I think is this result: DeveloperPackedArrayQ /@ {Table[w, {w, Range[1., 10., 0.1]}], Range[1., 10., 0.1]}` $\endgroup$ – Jason B. Feb 9 '16 at 14:30
  • $\begingroup$ @JasonB If it is not a bug, it is a great pitfall. Unpacking sometimes significantly harm performance. $\endgroup$ – matheorem Feb 9 '16 at 14:40
  • $\begingroup$ Doesn't Table return UNpacked arrays most of the time though? It seems to me that the odd result is the packed array you get first, not the unpacked one. For instance, Developer`PackedArrayQ@Table[w, {w, 1, 10, 0.1}] returns False... $\endgroup$ – MarcoB Feb 9 '16 at 14:40

Just a wild guess (ref.), but Attributes[Table] says Table has HoldAll attribute, which may cause Table to be unable to infer the type and causes the unpacking. This can be seen from turning on the unpacking warning via On["Packing"]. As an experiment,

ClearAttributes[Table, HoldAll]

Table[w + ky, {w, Range[1., 10., 0.01]}, {ky, 
   Range[1., 10., 0.01]}] // Developer`PackedArrayQ


and then

SetAttributes[Table, HoldAll]

Table[w + ky, {w, Range[1., 10., 0.01]}, {ky, 
   Range[1., 10., 0.01]}] // Developer`PackedArrayQ

During evaluation of In[49]:= Developer`FromPackedArray::punpack1: Unpacking array with dimensions {901}. >>


|improve this answer|||||
  • $\begingroup$ Interesting, another possible evidence: With[{xx = Range[1., 10., 0.01]}, Table[w + ky, {w, Range[1., 10., 0.01]}, {ky, xx}]] // Developer`PackedArrayQ $\endgroup$ – xzczd Feb 9 '16 at 15:14
  • $\begingroup$ interesting, +1 $\endgroup$ – matheorem Feb 9 '16 at 15:19
  • 7
    $\begingroup$ Yes, if Table sees a non-explicit iterator range it will go to a general evaluator rather than auto-compile. So auto-compilation is a bit weak but Table gets to be very flexible.. $\endgroup$ – Daniel Lichtblau Feb 9 '16 at 18:30
  • $\begingroup$ @DanielLichtblau Your comment is important. +1 $\endgroup$ – matheorem Feb 10 '16 at 0:34
  • 3
    $\begingroup$ I do not recall the details but I believe it involves knowing there can be no dependency of inner iterator variables on outer ones (since there are no inner iterators in the 1-D case). $\endgroup$ – Daniel Lichtblau Feb 10 '16 at 1:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.