Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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

True

{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?

share|improve this question
    
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? –  m_goldberg Jul 22 '13 at 19:58
2  
@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. –  Leonid Shifrin Jul 22 '13 at 22:59
    
@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. –  m_goldberg Jul 23 '13 at 2:16
    
@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. –  Mr.Wizard Jul 23 '13 at 3:19
1  
@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. –  Leonid Shifrin Jul 23 '13 at 9:10

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.