Assume list
is packed.
I expect Subsets[]
is a structural operation because it depends on the number of elements, not on what the elements are. The output for Subsets[list]
is ragged, and this is why it will not be packed. However, Subsets[list,{n}]
does not have this problem. It seems to be a structural operation whose output is rectangular. Why is the output not packed? Moreover, Subsets[list,{n}]
does not unpack (which can be determined using On["Packing"]
).
The same can be asked about the related function Subsequences[list,{n}]
whose output is not packed, and the output of IntegerPartitions[m,{n}]
($m>n$) is not packed as well.
Subsets[Range[10], {3}] // PackedArrayQ
Subsequences[Range[10], {3}] // PackedArrayQ
IntegerPartitions[10, {3}] // PackedArrayQ
(*False*)
(*False*)
(*False*)
On the other hand, the related functions Tuples[list,n]
and Permutations[list,n]
have packed outputs.
Moreover, it seems like there are many ways to make a program that does what Subsets[list,{n}]
but with an output that is packed (nested loops, Map[]
, Table[]
, removing elements from Tuples[list,n]
or from Permutations[list,{n}]
all come to mind).
list
unpacked? and, Why is the output not packed? The focus of the question seems to be on the second (that is, on the output). To my mind "to unpack" means to transform from packed to not packed and is an expensive operation. Unpacking does not happen in these three cases, so I take the question to be, Why is the output not generated as a packed array? $\endgroup$f[list]
is computed asJoin @@ Table[f[list, {n}], {n, 0, Length@list}]
, then you wouldn't want them packed, since you would waste time unpacking them when joined. One might write a separate function for the special case with a different core routine, as you say, but perhaps they didn't want to. The internal workings and reasonings are probably only known (and perhaps now forgotten) by WRI developers. $\endgroup$list
) to be packed, the output be not packed, and yet the input list is not unpacked by the operation? $\endgroup$list
is unpacked (at least by one of the commands, I forget which). UseOn["Packing"]
and execute your examples to see. As to how the input can be packed and the output not, trylist = Range@5; Table[list[[k ;; k + 2]], {k, 1, Length@list - 2}] // Developer
PackedArrayQ` (with the"Packing"
messages on.). Then try it withlist = Range@500
. The output being packed or not has nothing to do withlist
be unpacked or not. $\endgroup$Developer`FromPackedArray::unpack
message. It refers to transforming a packed array to the same array, but unpacked. The operation involves copying the data from a C-style array, to aList
structure with pointers to expressions representing each number in the array. It seems that the question is, "Why is the output not packed?" My response, not an answer, was, "The internal workings and reasonings are probably only known (and perhaps now forgotten) by WRI developers." $\endgroup$