13
$\begingroup$

Bug introduced in 8.0.4 or earlier and persisting through 11.0.1


First of all, please look at the below inputs and outputs:

In[1]:= $a = Array[# Range[#] &, {5}]

Out[1]= {{1}, {2, 4}, {3, 6, 9}, {4, 8, 12, 16}, {5, 10, 15, 20, 25}}

In[2]:= $af = Flatten[$a, {{1}, {2}}]

Out[2]= {{1}, {2, 4}, {3, 6, 9}, {4, 8, 12, 16}, {5, 10, 15, 20, 25}}

In[3]:= $a === $af

Out[3]= True

In[4]:= FullForm[$a] === FullForm[$af]

Out[4]= True

In[5]:= TreeForm[$a]

Out[5]:= 

enter image description here

In[6]:= TreeForm[$af]

Out[6]:= 

enter image description here

In[7]:= TreeForm[$a] === TreeForm[$af]

Out[7]= True

Then, I have natural questions:
Why are there the differences between the output of TreeForm[$a] and that of TreeForm[$af]? What makes these differences? Though Mathematica says "True"s for the checking equalities.

$\endgroup$
9
  • $\begingroup$ Post your code as formatted text, not as graphics. $\endgroup$
    – MarcoB
    Jun 30, 2016 at 17:14
  • $\begingroup$ @MarcoB I corrected it, sorry. $\endgroup$ Jun 30, 2016 at 17:28
  • $\begingroup$ This is weird - I would say that TreeForm[Array[ # Range[#] &, {5}]], the first one, is clearly wrong. Also, the output of TreeForm@Array[Range[#] &, {5}] is even weirder... $\endgroup$
    – Jason B.
    Jun 30, 2016 at 17:39
  • 6
    $\begingroup$ Okay, so this is a known bug where TreeForm will fail on arrays where the sublists are packed. Compare the output of Developer`PackedArrayQ /@ a and Developer`PackedArrayQ /@ $af . A workaround is to unpack the subarrays, TreeForm[$a /. {x_?Developer`PackedArrayQ :> Developer`FromPackedArray[x]}] $\endgroup$
    – Jason B.
    Jun 30, 2016 at 17:47
  • 1
    $\begingroup$ @TaikiBessho - that is correct. I give a workaround function below that should suit your needs. $\endgroup$
    – Jason B.
    Jun 30, 2016 at 18:29

1 Answer 1

15
$\begingroup$

The difference between your two examples is that one uses packed arrays and the other doesn't. The lists generated by Range are, by default, packed.

<< Developer`

PackedArrayQ /@ {{1, 2, 3, 4}, Range[4]}
(* {False, True} *)

There is a bug, where if the sublists in the array fed to TreeForm are packed, then the function doesn't behave properly:

TreeForm /@ {{{1, 2, 3, 4}}, {Range[4]}}

Mathematica graphics

As a workaround you can first search for any packed arrays, unpack them, and then feed the result to TreeForm

treeForm[arr_] := 
 TreeForm[arr /. {x_?PackedArrayQ :> FromPackedArray[x]}]

treeForm /@ {{{1, 2, 3, 4}}, {Range[4]}}

Mathematica graphics

Or, if you haven't loaded the Developer package, you can use this:

treeForm[arr_] := 
 TreeForm[arr /. {x_?Developer`PackedArrayQ :> 
     Developer`FromPackedArray[x]}]
$\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.