I'm confused as to why the following leads to unpacking of a PackedArray:
Needs["Developer`"]
On["Packing"]
a = ToPackedArray@{RandomReal[], RandomReal[]};
a // PackedArrayQ (* returns True*)
b f[a]
b + f[a]
Both of the last commands produce a FromPackedArray::unpack
warning, specifically:
FromPackedArray: Unpacking array in call to Times.
and
FromPackedArray: Unpacking array in call to Plus.
(And one can verify that in the produced output a
is no longer a PackedArray e.g. using PackedArrayQ[(b + f[a])[[2, 1]]]
.)
This was tested in Mathematica 12.1. In Mathematica 12.3, the above code does not lead to unpacking. However, if b
is replaced by a number, for example 2 f[a]
and 2 + f[a]
, then unpacking occurs in version 12.3 as well. No unpacking occurs in 12.3 when b
is replaced by an extact numerical quantity (that is not an integer) such as inSqrt[2] + f[a]
This behaviour to me is unexpected. Since a
is inside the f
wrapper, I would not expect a
to interact with the Plus
and Times
operations. If anybody can shed some light on why this happens, I would be most grateful. Even better would be tips on how to shield the PackedArray from unpacking in this type of situation.
1 + f[a]
but not withb + f[a]
. Additionally,(1 + f[a])[[2, 1]] // PackedArrayQ
isFalse
and(b + f[a])[[2, 1]] // PackedArrayQ
isTrue
in M12.3, consistent with the unpacking message. Can you update the question with this new info @mmeent, or is it okay if I make the edit? $\endgroup$