In Python generally, "X = Y" doesn't modify the *object* which is named X,
it just rebinds the name, saying that "X" now refers to the object given by
the expression Y.  So in general,

for something in someloop:
     something = something_else

isn't going to do much.  (I want to say "nothing" but I guess there could
be side effects of computing something_else.)


You can arrive at your goal in lots of ways.  You could sum the component
monomials, which is short but less general:

sage: R.<x1,x2,x3> = QQbar[]
sage: P = x1^2 + 2*x1*x2 + x2^2 + 2*x1*x3 + 2*x2*x3 + x3^2 + 2*x1 + 2*x2 +
2*x3 + 1
sage: sum(P.monomials())
x1^2 + x1*x2 + x2^2 + x1*x3 + x2*x3 + x3^2 + x1 + x2 + x3 + 1


Another way to get what you want would be to use map_coefficients, which
applies the function you pass it to all the coefficients, which is clear
but doesn't give you access to the corresponding term:

sage: P = x1^2 + 2*x1*x2 + x2^2 + 2*x1*x3 + 2*x2*x3 + x3^2 + 2*x1 + 2*x2 +
2*x3 + 1
sage: P.map_coefficients(lambda x: 1)
x1^2 + x1*x2 + x2^2 + x1*x3 + x2*x3 + x3^2 + x1 + x2 + x3 + 1


Or in the most general way, you could loop over the coefficients and their
monomials and build a new polynomial from each term:

sage: list(P)
[(1, x1^2),
 (2, x1*x2),
 (1, x2^2),
 (2, x1*x3),
 (2, x2*x3),
 (1, x3^2),
 (2, x1),
 (2, x2),
 (2, x3),
 (1, 1)]
sage: sum(mon for coeff, mon in P)
x1^2 + x1*x2 + x2^2 + x1*x3 + x2*x3 + x3^2 + x1 + x2 + x3 + 1


Doug

-- 
You received this message because you are subscribed to the Google Groups 
"sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sage-support.
For more options, visit https://groups.google.com/d/optout.

Reply via email to