1
$\begingroup$

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.

$\endgroup$
7
  • 1
    $\begingroup$ OTOH, coll = Reap[Nest[If[EvenQ[#], Sow[#]/2, 3 # + 1] &, 1000, 20]]; coll[[-1, 1]] // Developer`PackedArrayQ returns True. $\endgroup$ May 22, 2017 at 14:40
  • 1
    $\begingroup$ I get no messages about packing (or unpacking) from the central block of code in Mathematica 10.1. $\endgroup$
    – Mr.Wizard
    May 22, 2017 at 14:44
  • 1
    $\begingroup$ Ditto no messages in V11.1.1. $\endgroup$
    – Michael E2
    May 22, 2017 at 14:54
  • $\begingroup$ No message in v9.0.1 $\endgroup$
    – xzczd
    May 22, 2017 at 15:00
  • 5
    $\begingroup$ 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). $\endgroup$
    – Carl Woll
    May 23, 2017 at 5:24

1 Answer 1

3
$\begingroup$

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. >>

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.