ArrayReshape
is implemented using Internal`Deflatten
(at least in version 10.1). This function requires $n \times m$ elements, or at least behaves strangely when this is not provided. I am guessing this relates to memory pointers in the low-level implementation.
Internal`Deflatten[Range@8, {3, 3}]
Internal`Deflatten[Range@8, {3, 3}]
Internal`Deflatten[Range@8, {3, 3}]
Internal`Deflatten[Range@8, {3, 3}]
{{1, 2, 3}, {4, 5, 6}, {7, 8, 281500746514433}}
{{1, 2, 3}, {4, 5, 6}, {7, 8, 300647710721}}
{{1, 2, 3}, {4, 5, 6}, {7, 8, 411062144}}
{{1, 2, 3}, {4, 5, 6}, {7, 8, 7164775599194924370}}
Note: If we unpack the list it doesn't work at all:
list = List @@ Range@8;
Developer`PackedArrayQ[list]
Internal`Deflatten[list, {3, 3}]
False
Internal`Deflatten[{1, 2, 3, 4, 5, 6, 7, 8}, {3, 3}]
This is ultimately why something like this also fails:
ArrayReshape[Range@8, {3, 3}, Unevaluated @ Unevaluated[## &[]] ] (* does not work *)
To avoid this any input to ArrayReshape
is first padded with ArrayPad
or trimmed to $length = n \times m$.
Partition
drops in speed when using {}
padding, at least in some cases, probably due to working around a similar low-level implementation. I used a method similar to kglr's but for Partition
here:
Closely related:
Silly games
Just having fun using the above knowledge to break ArrayReshape
:
ClearAll[foo]
foo /: {x__Integer, foo[] ..} := Developer`ToPackedArray[{x}]
ArrayReshape[{1, 2, 3, 4, 5}, {3, 3}, foo[]]
{{1, 2, 3}, {4, 5, 470483008}, {36571776, 8, 5100273664}}
ArrayReshape[list, {3, 3}, Nothing]
to work, but it does not. $\endgroup$ – Jason B. Jan 6 '20 at 19:54Activate[ArrayReshape[list, {3, 3}, Inactive@Nothing]]
$\endgroup$ – chuy Jan 6 '20 at 20:14