Bug introduced in 5.2 or earlier and fixed in 10.1.0

This is another question on the design choices in Mathematica. I understand that without direct reply from the developers it may not be possible to give a definitive and exhaustive answer to "why" questions but such topics have often been fruitful.

Why does list assignment of the form {a, b, c} = tensor where tensor is packed result in unpacked values for a, b, and c?

To illustrate:

packedQ = Developer`PackedArrayQ;

tensor = RandomReal[99, {3, 5, 7}];

tensor // packedQ


{a, b, c} = tensor;

packedQ /@ {a, b, c}

{False, False, False}

It is possible to make the assignments without unpacking the sub-arrays of tensor by manually unpacking the outer list using Apply:

{a, b, c} = List @@ tensor;

packedQ /@ {a, b, c}

{True, True, True}

Why doesn't Set operate like this by default?

That is, why doesn't Set only unpack the right-hand-side as far as necessary, to the level of the left-hand-side?

  • $\begingroup$ Your question raises another in my mind: since tensor already has the head List, why does List @@ tensor have any effect on Set at all, considering it's evaluated before Set even sees it? $\endgroup$
    – m_goldberg
    Jul 22, 2013 at 19:58
  • 2
    $\begingroup$ @m_goldberg Because Apply does unpack the top level of the packed array. It does it always, even if the head to be applied is also a List. $\endgroup$ Jul 22, 2013 at 22:59
  • $\begingroup$ @LeonidShifrin. So, although it's not his main point, one thing Mr.Wizard is telling us is that when Set is given tensor with its top-level unpacked, the lower levels will not be disturbed. $\endgroup$
    – m_goldberg
    Jul 23, 2013 at 2:16
  • $\begingroup$ @m_goldberg Yes, that is what I tried to express that with the final example given. Another way to look at this problem is that apparently when Set gets a packed array on the RHS and a list on the LHS, it fully unpacks the RHS rather that only unpacking it to the level of the LHS. This seems like an unfortunate choice, but I have often learned that there are good reasons for such choices once I asked about them. $\endgroup$
    – Mr.Wizard
    Jul 23, 2013 at 3:19
  • 1
    $\begingroup$ @MichaelE2 I would agree that the special case of List was probably considered not important. In fact, the only reason one would want to do List @@ packed would be if one wants to unpack one level. It also seems to be the only way to do this (i.e. using Apply), if one wants to only unpack one level. $\endgroup$ Jul 23, 2013 at 9:10

1 Answer 1


Thanks Mr.Wizard. I've filed a bug for this, #288440 if you have future correspondence with people inside the company about it...

  • $\begingroup$ This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. $\endgroup$
    – m_goldberg
    Feb 9, 2015 at 19:23
  • $\begingroup$ @m_goldberg Do you think it is reasonable to make an exception for the developers of Mathematica? Given at least some implied authority here this is a form of answer. $\endgroup$
    – Mr.Wizard
    Feb 9, 2015 at 20:04
  • 5
    $\begingroup$ @m_goldberg, since Taliesin has filed a bug one might argue that the question has been answered, albeit indirectly. Why does Mathematica do this? Because it has a bug. (As opposed to it being a deliberate design choice). $\endgroup$ Feb 9, 2015 at 22:22
  • 3
    $\begingroup$ Aside from these meta comments, this has been fixed. Should be in the next release. $\endgroup$ Feb 10, 2015 at 4:15
  • 1
    $\begingroup$ @Mr.Wizard unless we want to decide how many angels can dance on the head of a pin, let's just call it a bug :) $\endgroup$ Apr 22, 2015 at 5:35

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.