For some expressions, TreeForm
may grow very long. I'm only interested in the top levels of the expression. How can I get the tree form of only the first levels an expression?
For example: how to get the first levels of this expression in tree form?
Nest[1/(1 + #) (1 - #) &, w, 5]
Answer
Can use something like this:
ClearAll[showTopTree];
showTopTree[expr_, level_] :=
Module[{myHold},
SetAttributes[myHold, HoldAll];
Function[code,
TreeForm[Unevaluated@Unevaluated@code],
HoldAll] @@
(Hold[#] &@
DeleteCases[MapAll[myHold, expr], _, {2*level, Infinity}] //.
myHold[x__] :> x)];
Pretty ugly, but seems to work:
expr = Nest[1/(1 + #) (1 - #) &, w, 5]
Manipulate[showTopTree[expr, n], {n, 1, Depth[expr], 1}]
GraphicsGrid[Partition[showTopTree[expr, #] & /@ Range[6], 3]]
Comments
Post a Comment