3
$\begingroup$

In Mathematica 11.1.1, when a packed array is stored within another list, it gets unexpectedly unpacked when Inserting something into the outer list.

Create a packed array:

Needs["Developer`"];
On["Packing"];

packedlist = Range[100000];
Developer`PackedArrayQ[packedlist]

(* True *)

When nesting the packed array in another list, it remains packed:

outerlist = {1, 2, 3, packedlist};
Developer`PackedArrayQ[Last[outerlist]]

(* True *)

But then, when inserting an element into the outerlist, for some reason the packedlist is being unpacked:

Insert[outerlist, 4, 1];

(* During evaluation of In[391]:= FromPackedArray::unpack: Unpacking array in call to HoldForm. *)
(* During evaluation of In[391]:= FromPackedArray::punpack1: Unpacking array with dimensions {100000}. *)

Why does this happen? There is no unpacking with other operations, like:

Append[outerlist, 4];
Prepend[outerlist, 0];
Drop[outerlist, {1}];

(* No unpacking *)
$\endgroup$
5
$\begingroup$

That's indeed weird. It still happens in version 11.3. I don't think that there is a "good" reason for this, because one can use Join (without unpacking) instead:

insert[list_, a_, k_] := Join[
  list[[1 ;; If[k < 0, Length[list] + k + 1, k - 1]]],
  {a},
  list[[If[k < 0, Length[list] + k + 2, k] ;;]]
  ]

By the way, I have made the experience that Join often works better than Prepend and Append. But I have no clue why.

$\endgroup$
  • $\begingroup$ Interesting... thank you for the Join-based insert implementation. I wonder if someone knows why it happens... $\endgroup$ – Theo Tiger Apr 13 '18 at 7:25
  • $\begingroup$ Me too. You should also consider reporting it to the support. $\endgroup$ – Henrik Schumacher Apr 13 '18 at 7:32
  • $\begingroup$ The issue has been confirmed by the Wolfram Support. They said that this functionality has to be implemented for each of the functions (Append, Join, ...) and so far has not been done for Insert. Might happen in future versions. $\endgroup$ – Theo Tiger May 3 '18 at 16:00
  • $\begingroup$ Thanks @TheoTiger. for the feedback. Another step to a better software. $\endgroup$ – Henrik Schumacher May 3 '18 at 16:04

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.