Reap unpacks packed arrays

1

I noticed that Reap unpacks packed arrays. I've looked at previous answers but am still an unsure whether this a serious issue.

E.g. consider Ex 47.3 from EIWL:

Use Sow and Reap to get a list of all cases where #/2 is used in Nest[If[EvenQ[#], #/2, 3#+1]&, 1000, 20].

One solution is

On["Packing"]
Reap @ Nest[If[EvenQ[#], Sow[#]/2, 3#+1]&, 1000, 20]

but one gets multiple (FromPackedArray) messages about unpacking arrays due, I assume, to the fact that intermediate results are irregular structures.

Since other users don't appear to see this, here's a screenshot:

FromPackedArray messages

BTW, since $3x+1$ is always even, a more efficient coding of the Collatz problem is to replace 3#+1 by (3#+1)/2.

TheDoctor

Posted 2017-05-22T14:37:14.813

Reputation: 2 432

1OTOH, coll = Reap[Nest[If[EvenQ[#], Sow[#]/2, 3 # + 1] &, 1000, 20]]; coll[[-1, 1]] // Developer`PackedArrayQ returns True. – J. M.'s ennui – 2017-05-22T14:40:30.723

1I get no messages about packing (or unpacking) from the central block of code in Mathematica 10.1. – Mr.Wizard – 2017-05-22T14:44:32.633

1Ditto no messages in V11.1.1. – Michael E2 – 2017-05-22T14:54:18.683

No message in v9.0.1 – xzczd – 2017-05-22T15:00:54.687

5You are using TraditionalForm as your default output format, and creating those boxes is generating the unpacking messages. For example, adding a ; at the end of the Reap will prevent the messages (until you display the output). – Carl Woll – 2017-05-23T05:24:50.907

So should using TraditionalForm as the default output format do that? Perhaps a more useful FromPackedArray message would have been helpful here. – TheDoctor – 2017-05-24T09:59:32.077

Even in StandardForm I get the similar behaviour with Reap@Plot[x,{x,0,1},EvaluationMonitor :> Sow[x]];. – TheDoctor – 2017-05-25T07:06:26.490

Answers

3

Posting the comment by Carl Woll as an answer:

You are using TraditionalForm as your default output format, and creating those boxes is generating the unpacking messages. For example, adding a ; at the end of the Reap will prevent the messages (until you display the output).

Proof:

On["Packing"]
ToBoxes[RandomReal[1, 10], TraditionalForm];

Developer`FromPackedArray::unpack: Unpacking array in call to LeafCount. >>

Alexey Popkov

Posted 2017-05-22T14:37:14.813

Reputation: 50 220