# Table with List iterator return unpacked list

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

list1//DeveloperPackedArrayQ
(*True*)


But now look at this

Table[w + ky, {w, Range[1., 10., 0.01]}, {ky,
Range[1., 10., 0.01]}] //DeveloperPackedArrayQ
(*False*)


it gives unpacked array !! On the other hand

Table[w + ky, {w, 1., 10., 0.01}, {ky,
1., 10., 0.01}] //DeveloperPackedArrayQ
(*True*)


What is wrong with List as iterator?

I am using Mathematica 10.3 on win 8

Update

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]}] //DeveloperPackedArrayQ
(*True*)


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]}] // DeveloperPackedArrayQ
(*True*)

• Mathematica never promised to give you a packed array :D, anyway, it's a interesting observation. – xzczd Feb 9 '16 at 14:24
• @xzczd I think it might be a bug... – matheorem Feb 9 '16 at 14:26
• 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]} – Jason B. Feb 9 '16 at 14:30
• @JasonB If it is not a bug, it is a great pitfall. Unpacking sometimes significantly harm performance. – matheorem Feb 9 '16 at 14:40
• 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, DeveloperPackedArrayQ@Table[w, {w, 1, 10, 0.1}] returns False... – 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]}] // DeveloperPackedArrayQ


True

and then

SetAttributes[Table, HoldAll]

Table[w + ky, {w, Range[1., 10., 0.01]}, {ky,
Range[1., 10., 0.01]}] // DeveloperPackedArrayQ


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

False

• Interesting, another possible evidence: With[{xx = Range[1., 10., 0.01]}, Table[w + ky, {w, Range[1., 10., 0.01]}, {ky, xx}]] // DeveloperPackedArrayQ – xzczd Feb 9 '16 at 15:14
• interesting, +1 – matheorem Feb 9 '16 at 15:19
• 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.. – Daniel Lichtblau Feb 9 '16 at 18:30
• @DanielLichtblau Your comment is important. +1 – matheorem Feb 10 '16 at 0:34
• 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). – Daniel Lichtblau Feb 10 '16 at 1:07