1
$\begingroup$

Consider the following;

Pack some integers.

In[287]:= Clear[p5i, p5ic, p5icu, p5r, p5rc, p5rcu]
In[288]:= p5i = ToPackedArray@Range@5

Out[288]= {1, 2, 3, 4, 5}

Pack some reals.

In[289]:= p5r = ToPackedArray@N@Range@5

Out[289]= {1., 2., 3., 4., 5.}

In[290]:= PackedArrayQ@# & /@ {p5i, p5r}

Out[290]= {True, True}

Now let's compress and decompress.

In[298]:= p5ic = Compress@p5i
          p5rc = Compress@p5r

Out[298]= "1:eJxTTMoPSmNlYGAoZgESPpnFJZ6MQIYhmDQCk8Zg0gRMmgIAufkGNA=="

Out[299]= "1:eJxTTMoPSmVkYGBgZYCBD/ZQhgOE4oDSAlBaxAEAi8sECw=="

In[300]:= p5icu = Uncompress@p5ic
          p5rcu = Uncompress@p5rc

Out[300]= {1, 2, 3, 4, 5}

Out[301]= {1., 2., 3., 4., 5.}

In[302]:= PackedArrayQ@# & /@ {p5ic, p5rc, p5icu, p5rcu}

Out[302]= {False, False, False, True}

Now the fact that the compressed versions are not packed isn't too surprising, given that they are essentially ASCII strings (that's my understanding). However, it appears that packing is preserved for reals on compression and decompression but not for integers.

Please could someone explain to me why this should be the case.

$\endgroup$
2
  • $\begingroup$ Unrelated to your main issue, but you don’t need to build a pure function to map a function with one argument over a list as in PackedArrayQ@# & /@ {...}. Instead PackedArrayQ /@ {...} will do. $\endgroup$
    – MarcoB
    Jan 15 at 13:33
  • $\begingroup$ @MarcoB - it may not be related, but nonetheless I am grateful to you for pointing this out as I am always looking to improve my WL skills. Previously I'd thought that a function needed to be Listable for this to be true. Thanks once again. $\endgroup$ Jan 15 at 14:47
3
$\begingroup$

There is an undocumented Method option to Compress that you can use to control this. A packed integer array:

p = Range[10];
Developer`PackedArrayQ @ p

True

Using Compress:

c = Compress[p, Method -> {Version -> 6}];
u = Uncompress[c];
Developer`PackedArrayQ @ u

True

$\endgroup$
1
  • $\begingroup$ Thanks for this. I wonder why this changed between versions. $\endgroup$ Jan 15 at 18:45

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.