You can get a coefficient of a specific term by using coeff();
x, a = symbols("x, a")
expr = 3 + x + x**2 + a*x*2
expr.coeff(x)
# 2*a + 1
Here I want to extract all the coefficients of x, x**2 (and so on), like;
# for example
expr.coefficients(x)
# want {1: 3, x: (2*a + 1), x**2: 1}
There is a method as_coefficients_dict(), but it seems this doesn't work in the way I want;
expr.as_coefficients_dict()
# {1: 3, x: 1, x**2: 1, a*x: 2}
expr.collect(x).as_coefficients_dict()
# {1: 3, x**2: 1, x*(2*a + 1): 1}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…