When extracting coefficients using patterns, I am having problems when the coefficient is 1 - see attached notebook. It appears that the coefficient of 1 is being ignored completed and instead the unknown is being selected.
Am I doing something incorrectly or should I report this as a possible programming problem?
One thing is that you are tacitly assuming the first factor will be the integer, and also using (perhaps intentionally) the fact that since m precedes n alphabetically, canonical ordering places it first in the pattern. It would be more reliable to use a pattern that forces m to be an integer and allows for the possibility that it “collapses” to one. Also I think you want to restrict the pattern to operate on level 1 of the expression. While there may be nicer/shorter/slicker ways to achieve this, I think Cases as shown below is a good function for this purpose. Since it returns a list we need to post-process to get a sum at the end.