I've been struggling with this for a few days, and so I thought I might ask this here. Part of the problem lies in the fact that I do not even know the mathematical solution, which runs the risk of this question falling out of the scope of this site. Nevertheless, I'll proceed:
Consider this unexpanded polynomial of
Symbols constructed out of heads
poly = ((a + b) (c + d (e + f)) + (g + h) i) j + k
What I'd like to do: Without expanding the polynomial, mark the leaves of the polynomial by integers based on its position in the tree. This labeling of leaves must have the property such that
Expanded, each factor in each term is given a unique label
- No more labels are used than necessary for the whole polynomial (number of distinct labels equals overall order of polynomial in all its variables).
poly, a solution is
((a + b) (c + d (e + f)) + (g + h) i) j + k
The result is not unique, but observe that the expanded form satisfies both requirements 1 and 2.
a c j + b c j + a d e j + b d e j + a d f j + b d f j + g i j + h i j + k
It is not necessary that terms with fewer factors use specific labels in any order: for example a labeling of the polynomial in which the 2nd last term of the expanded form is
h i j (missing label
2) or in which the last term is
k is acceptable.
Moreover I'd like a solution that is faster than just expanding the polynomial and labeling each term.
My original attempt was based on traversing the tree,
and raising/lowering the value of the label based on whether it passes through
Times. Unfortunately, none of my solutions based on this give the correct answer.