13

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.

Taiki Bessho
  • 886
  • 5
  • 15
  • Post your code as formatted text, not as graphics. – MarcoB Jun 30 '16 at 17:14
  • @MarcoB I corrected it, sorry. – Taiki Bessho Jun 30 '16 at 17:28
  • 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... – Jason B. Jun 30 '16 at 17:39
  • 6
    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]}] – Jason B. Jun 30 '16 at 17:47
  • @JasonB I was very surprised with your suggestion, TreeForm@Array[Range[#] &, {5}], I got a very big figure... – Taiki Bessho Jun 30 '16 at 17:47
  • @JasonB That second comment looks like an answer to me. – Emilio Pisanty Jun 30 '16 at 17:52
  • @JasonB I'm a beginner in Mathematica and not sure what your codes mean... Anyway, here are the result of the execution for your suggestions: DeveloperPackedArrayQ /@ $a = {True, True, True, True, True}andDeveloperPackedArrayQ /@ $af = {False, False, False, False, False} – Taiki Bessho Jun 30 '16 at 18:02
  • @JasonB Regarding above results for your suggesting commands, the sublists are packed in "a" whereas not packed in "af". Then, TreeForm doesn't work well when it acts to arrays where the sublists are packed, right? – Taiki Bessho Jun 30 '16 at 18:17
  • 1
    @TaikiBessho - that is correct. I give a workaround function below that should suit your needs. – Jason B. Jun 30 '16 at 18:29

1 Answers1

15

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]}]
Jason B.
  • 68,381
  • 3
  • 139
  • 286