Ask Your Question

Revision history [back]

what about using coefficient():

sage: expr = sol3[0].rhs()
sage: expr.coefficient(D*H)
1/4

let me mention that wild cards could also be useful in this type of task:

sage: var('DH'); w0 = SR.wild();
sage: expr.subs({D*H*w0 : DH*w0}).coefficient(DH)
-1/4

what about using coefficient():

sage: expr = sol3[0].rhs()
sage: expr.coefficient(D*H)
1/4
-1/4

let me mention that wild cards could also be useful in this type of task:

sage: var('DH'); w0 = SR.wild();
sage: expr.subs({D*H*w0 : DH*w0}).coefficient(DH)
-1/4

what about using coefficient():

sage: expr = sol3[0].rhs()
sage: expr.coefficient(D*H)
-1/4

let me mention that wild cards could also be useful in this type of task:

sage: var('DH'); w0 = SR.wild();
sage: expr.subs({D*H*w0 : DH*w0}).coefficient(DH)
-1/4

Edit: there is the following trick of adding/removing the term of interest:

sage: DHterm = expr.coefficient(D*H)*D*H
sage: ((expr-DHterm).factor() + DHterm).collect(D)
-1/4*D*(H - 2*Nb*Pmean/((alpha + 1)*c))

... but it would be nice to know if there is a more "direct" (=one command) method!

what about using coefficient():

sage: expr = sol3[0].rhs()
sage: expr.coefficient(D*H)
-1/4

let me mention that wild cards could also be useful in this type of task:

sage: var('DH'); w0 = SR.wild();
sage: expr.subs({D*H*w0 : DH*w0}).coefficient(DH)
-1/4

Edit: there is the following trick of adding/removing the term of interest:interest which works here:

sage: DHterm = expr.coefficient(D*H)*D*H
sage: ((expr-DHterm).factor() + DHterm).collect(D)
-1/4*D*(H - 2*Nb*Pmean/((alpha + 1)*c))

... but it would be nice to know if there is a more "direct" (=one command) method!