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
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?
tensoralready has the headList, why doesList @@ tensorhave any effect onSetat all, considering it's evaluated beforeSeteven sees it? – m_goldberg Jul 22 '13 at 19:58Applydoes unpack the top level of the packed array. It does it always, even if the head to be applied is also aList. – Leonid Shifrin Jul 22 '13 at 22:59Setis given tensor with its top-level unpacked, the lower levels will not be disturbed. – m_goldberg Jul 23 '13 at 02:16Setgets 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 03:19Applydesigned that way. ThatApplyalways unpacks the first level is not an explanation why it does it when it's unnecessary. I would think that it's becauseList @@ List[..]is not important enough to make a special case. Or it's so that{a, b, c} = List @@ tensoris a workaround forSet's behavior. Or something else like that. – Michael E2 Jul 23 '13 at 03:49Listwas probably considered not important. In fact, the only reason one would want to doList @@ packedwould be if one wants to unpack one level. It also seems to be the only way to do this (i.e. usingApply), if one wants to only unpack one level. – Leonid Shifrin Jul 23 '13 at 09:10#& /@ packed, and even other functions such asOuter, though inelegantly:Outer[# &, packed, {1}, 1][[All, 1]]– Mr.Wizard Jul 24 '13 at 14:01Maponly unpacked because the length of the list was smaller than "MapCompileLength" setting. AndOuteris rather inelegant, I agree. – Leonid Shifrin Jul 24 '13 at 14:22/. h_@x__ :> h@x– Mr.Wizard Aug 03 '14 at 04:42