CompiledFunction
s usually appear very compactly, for instance:
comp = With[{d1 = {{1, 2}, {3, 4}}},
Compile[{{x, _Real, 1}},
Module[{y},
y = x^2;
d1.x + y]
]
]
However, sometimes this gets "unpacked". One obvious way is:
FullForm[comp]
However this also does it:
Hold[Evaluate[comp]] // Replace[#1, {Hold[x_] :> x}, Infinity] &
Hold[CompiledFunction[{10, 11., 5468}, {{_Real, 1}}, {{3, 1, 0}, {3, 1, 4}}, {{{{1, 2}, {3, 4}}, {2, 2, 2}}, {12, {2, 0, 1}}, {3, {2, 0, 0}}}, {0, 2, 0, 0, 5}, {{40, 56, 3, 1, 0, 3, 1, 1}, {42, "CoerceTensor", 2, 0, 0, 2, 2, 2, 3, 2, 3}, {42, "Dot", 3, 2, 3, 3, 1, 0, 2, 0, 1, 3, 1, 4}, {44, 4, 1, 4}, {1}}, Function[{x}, Module[{y\$}, y$ = x^2; {{1, 2}, {3, 4}}.x + y$]], Evaluate]]
Why is this? There is no Hold
in the original expression for this to act on.
Furthermore, if we replace the levelspec Infinity
by All
(side question: what is the difference? All
doesn't seem to be documented in this case), it doesn't unpack.
And if we replace d1
by e.g. 3.
and the Dot
by multiplication, or get rid of d1
entirely, it also doesn't unpack.
Is this a bug (I'm using "11.0.1 for Mac OS X x86 (64-bit) (September 21, 2016)"), or can I find somewhere under what conditions this happens?
(The line of code above is useless ofcourse, but in reality instead of comp
I have a bigger expression, that includes something like comp
, that is being put together. The "unpacked" form of the CompiledFunction
still works, but makes the code unreadable.)