MathGroup Archive 2008

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: How to collect terms based the total power of x and y in (x +


> In[(x + y + 1)^3 // Expand
> The above command gives me the following results.
> 1 + 3 x + 3 x^2 + x^3 + 3 y + 6 x y + 3 x^2 y + 3 y^2 + 3 x y^2 + y^3
> I'm wondering how to collect terms based on the sum of the power
> coefficients of both x and y. For example, I want the following
> 1 + (3 x + 3 y) + (3 x^2 + 6 x y + 3 y^2) + (x^3 + 3 x^2 y + 3 x y^2 +
> y^3)

I think the best thing you can do is to introduce an artificial symbol
which counts powers of x and y:

Collect[expr /. {x -> x*h, y -> y*h}, h]

This will just collapse to what it was before if you replace h with 1.
If it is only to get a better understanding of how your expression looks
like, the h might even make the analysis easier. If you don't like the h
to appear in the result and it is for printout only, you could do
something like:

Collect[expr /. {x -> x*h, y -> y*h}, h] /. h^_. -> Spacer[0]

If you want to reuse the expression, you shouldn't care to much about
how mathematica orders and collects the terms, it must transform
expressions to an unique order (canonical order) for some of its
functionality to work. Here I think the attributes Flat and Orderless of
Plus are the deeper reason that mathematica will (needs to?) always
reorder and expand your polynom.

There are some more or less involved tricks to avoid this, but it would
be necessary to know what you try to achieve to pick one of these...



  • Prev by Date: Re: Changing names of variables in a loop
  • Next by Date: Finding Nash Equilibrium in a 2 person game with continuous action space
  • Previous by thread: Truncated MathPanel Output
  • Next by thread: Finding Nash Equilibrium in a 2 person game with continuous action space