Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Just an other way, extract a version of dPart below adapted for the own needs...

Let us fix some test data.

sage: var( 'x,y,z' );
sage: F = 27*x^5 + x^2*y^2 + 19*z^4 + 55*x*y*z + 6
sage: F = F.polynomial( QQ )

(1)

sage: def dPart( F, d ):
....:     return sum( [ F.monomial_coefficient(m) * m
....:                   for m in F.monomials() 
....:                   if  m.total_degree() == d ] )
....: 

sage: for d in range(6):
....:     print "total degree %s -> %s" % ( d, dPart( F, d ) )
....:     
total degree 0 -> 6
total degree 1 -> 0
total degree 2 -> 0
total degree 3 -> 55*x*y*z
total degree 4 -> x^2*y^2 + 19*z^4
total degree 5 -> 27*x^5

(2) "Same", but not so easy to digest:

dPart = lambda F, d: sum( [ c*m for c,m in F if m.total_degree() == d ] )

(3) Or parse correspondingly the information in F.dict(), but this would be the too explicit solution:

sage: def dPart( F, d ):
....:     vars = F.variables()
....:     return sum( [ coeff * prod( [ vars[k]^degrees[k] for k in range(len(vars)) ] )
....:                   for degrees, coeff in F.dict().items()
....:                   if  sum(degrees) == d ] )