Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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//Developer`PackedArrayQ
(*True*)

But now look at this

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

it gives unpacked array !! On the other hand

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

What is wrong with List as iterator?

I am using Mathematica 10.3 on win 8


Update

egwene sedai provides a reasonable explanation.

Here is another work around to avoid unpacking

Table[w + ky, Evaluate@{w, Range[1., 10., 0.01]}, Evaluate@{ky, 
   Range[1., 10., 0.01]}] //Developer`PackedArrayQ
(*True*)
share|improve this question
3  
Mathematica never promised to give you a packed array :D, anyway, it's a interesting observation. – xzczd Feb 9 at 14:24
    
@xzczd I think it might be a bug... – matheorem Feb 9 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]}` – JasonB Feb 9 at 14:30
    
@JasonB If it is not a bug, it is a great pitfall. Unpacking sometimes significantly harm performance. – matheorem Feb 9 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, Developer`PackedArrayQ@Table[w, {w, 1, 10, 0.1}] returns False... – MarcoB Feb 9 at 14:40
up vote 9 down vote accepted

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

True

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}. >>

False

share|improve this answer
    
Interesting, another possible evidence: With[{xx = Range[1., 10., 0.01]}, Table[w + ky, {w, Range[1., 10., 0.01]}, {ky, xx}]] // Developer`PackedArrayQ – xzczd Feb 9 at 15:14
    
interesting, +1 – matheorem Feb 9 at 15:19
5  
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 at 18:30
    
@DanielLichtblau Your comment is important. +1 – matheorem Feb 10 at 0:34
2  
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 at 1:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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