On Thu, Jun 21, 2012 at 3:34 PM, Aaron Meurer <[email protected]> wrote:
> I don't think there's an efficient function to do it, if that's what you're
> looking for (i.e., one that doesn't require full expansion). It's not too
> hard to come up with, though.

(I gave up when I started chasing the recursion rabbit...I don't think
it's a trivial problem to handle it in a general way.)

>

Putting together some of the suggestions so far gives something like this:

>>> def order(p, n):
...  p = Poly(p, p.free_symbols or Dummy())
...  gens = p.gens
...  ok = []
...  for m, c in p.terms():
...   if sum(m) == n:
...     ok.append(Mul(*[g**e for g, e in zip(gens, m)]))
...  return ok
...
>>> order(p, 2)
[x*y, y**2]
>>> p
(x*y + 1)*(x + y**2 + 1)

Note that making a Poly out of something automatically expands it. And
using a Dummy in case there is no generator is necessary in case p is
a constant.

Regarding the question about getting the terms...If you don't use Poly
and want the terms of an expression, Add.make_args(expr) will give you
the terms that add together to give expr (even if expr isn't an Add).
If you know expr is an Add then you could simply do expr.args to get
the terms.

/chris

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sympy?hl=en.

Reply via email to