Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Thank you for your answer and sorry for my late answer.

'collect' works pretty well in the example I gave :

var('A x y')
f1 = A*x + x + A^2*exp(y) + exp(y)
print f1.collect(A).collect(x).collect(e^y)

--> (A + 1)*x + (A^2 + 1)*e^y

but unfortunately it also performs factorization with cross-products

var('A x y')
f2 = A*x^2 + x^2 + A^2*x + 2*x + x*y
print f2.collect(A).collect(x*y).collect(x)

--> (A + 1)*x^2 + (A^2 + y + 2)*x

whereas I would like to get the coefficient of each monomial x^i*y^j, as obtained with the following example

R.<A> = QQ[]
R.<x,y> = PolynomialRing(QQ[A],order='deglex')
f3 = A*x^2 + x^2 + A^2*x + 2*x + x*y
print f3

--> (A + 1)*x^2 + x*y + (A^2 + 2)*x

Is there a solution to do so ? Thanks