Ask Your Question

Revision history [back]

Here is a way:

sage: var('a,b,c,x')
sage: sinx = function('sinx',nargs=1)
sage: pol =  3*a*x^(-b)*log(x)*b^2 - 6*a*b*c*sinx(x*b) + 3*a*c^2 + 5
sage: R = PolynomialRing(SR, names='a,c')
sage: f = pol.polynomial(ring=R)
sage: dict(zip(f.monomials(), f.coefficients()))
{1: 5, a: 3*b^2*log(x)/x^b, a*c: -6*b*sinx(b*x), a*c^2: 3}

Note the generators of the ring R are not the same as the symbolic variables a and c.

To get the coefficient of c as in your example, you can do:

sage: f.monomial_coefficient(R.gen(1))

Or maybe more conveniently, something like:

sage: A,C = R.gens()
sage: f.monomial_coefficient(C)