Can somebody help me with a function f
such that it extract all sub expressions (i.e. looking down the expression tree) from any expression given to it and also a list of heads it encounters as it moves down the expression tree.
For example, f@(Sin[Log[Cos[a], Tan[Log[a, b]]]] // Inactivate)
gives { { Inactive[Sin][Inactive[Log][Inactive[Cos][a], Inactive[Tan][Inactive[Log][a, b]]]], Inactive[Log][Inactive[Cos][a], Inactive[Tan][Inactive[Log][a, b]]], Inactive[Cos][a], Inactive[Tan][Inactive[Log][a, b]], a, Inactive[Log][a,b], a, b }, { Inactive[Sin], Inactive[Log], Inactive[Cos], Inactive[Tan], Inactive[Log] } }
Note: order of occurrence and duplicacy must be maintained
Explanation of Order of sub expressions (also the order in which heads are removed to move to the next sub expression)
first the full expression
next the expression starting from the second level (Head Inactive[Sin]
is removed)
next the two expressions at level 3 (Head Inactive[Log]
is removed)
next the two expression at level 4 from left to right (next the Head Inactive[Cos]
and then Inactive[Tan]
is removed moving from left to right)
finally the two expressions at the last level from left to right (lastly the Head Inactive[Log]
is removed to arrive at the last level)
This is the general order i want to have for sub expressions and the also the order of removal of Heads.