getting a specific Coefficient of a non-commuting monomial in multiple variables
Example:
A.<a,b> = FreeAlgebra(QQ, 2)
f = a + a * b - b * a
f.coefficient(a*b)
Throws:
AssertionError: a*b should be an element of Free monoid on 2 generators (a, b)
After some investigation:
c = f.monomial_coefficients()
type(list(c.keys())[0])
<class 'sage.monoids.free_monoid.FreeMonoid_with_category.element_class'>
But:
type(a*b)
<class 'sage.algebras.free_algebra.FreeAlgebra_generic_with_category.element_class'>
This might be a bug? I'm not sure. For now, the only 'solution' I have is something like this:
mon_keys = list(c.keys())
str_keys = [str(k) for k in c.keys()]
i = str_keys.index(str(a*b))
print(f[mon_keys[i]])
Which prints:
1
as expected.
Note that this is a playground example, in practice I need to get this result from a Lyndon word in the generators, hence some of the generalities that would otherwise seem slightly unneeded. Anyhow, Is there a way to avoid this ugly hack?