Ask Your Question
1

getting a specific Coefficient of a non-commuting monomial in multiple variables

asked 2025-01-07 12:16:11 +0100

levav gravatar image

updated 2025-01-07 12:17:36 +0100

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?

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

answered 2025-01-07 13:41:41 +0100

FrédéricC gravatar image

like this

A.<a,b> = FreeAlgebra(QQ, 2)
f = a + a * b - b * a
aa,bb=A.monoid().gens()
f.coefficient(aa*bb)
edit flag offensive delete link more

Comments

Thank you very much! this worked perfectly. Should I open a bug about this in git, or is this expected behavior?

levav gravatar imagelevav ( 2025-01-07 15:12:50 +0100 )edit
1

Well, this is expected, but maybe things can be enhanced.

FrédéricC gravatar imageFrédéricC ( 2025-01-07 18:08:19 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2025-01-07 12:16:11 +0100

Seen: 19 times

Last updated: 2 days ago