4
$\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$

1 Answer 1

6
$\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$
6
  • $\begingroup$ Interesting... thank you for the Join-based insert implementation. I wonder if someone knows why it happens... $\endgroup$
    – Theo Tiger
    Apr 13, 2018 at 7:25
  • $\begingroup$ Me too. You should also consider reporting it to the support. $\endgroup$ Apr 13, 2018 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, 2018 at 16:00
  • $\begingroup$ Thanks @TheoTiger. for the feedback. Another step to a better software. $\endgroup$ May 3, 2018 at 16:04
  • 2
    $\begingroup$ The problem doesn't occur anymore with V 13.3 $\endgroup$
    – eldo
    Jan 20 at 16:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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