1
$\begingroup$

Let expr contain a sum of powers of x with some coefficients ci. The exponents of x can contain variables and/or integers. I would like to define a function MyCollect in Mathematica which would first collect all powers containing variables and would then collect all powers containing integers within the first collection. For example, such that if:

expr = c0 x^(a+b)+c1 x^(a+b+1)+c2 x^(a+b+2)+c3 x^(a+b+2)+c4 x^(a+b+2)+c5 x^(a)+c6 x^(a+1)+c7 x^(a+1)

the result would be:

MyCollect[expr]
x^(a)(c5 + x(c6+c7)) + x^(a+b)(c0 + x c1 + x^2(c2 + c3 + c4))

Is there a convenient way to do this? Thanks for any suggestion!

EDIT

Here is a second, more complicated example

expr = c0 x^(a+b)+c1 x^(a+b+1)+c2 x^(a+b+2)+c3 x^(2a+b+2)+c4 x^(a+b+2)+c5 x^(a)+c6 x^(a+1)+c7 x^(a+1)+c8 x^(a+c+1)+c9 x^(a+c)

Desired output of MyCollect[expr]:

x^(a)(c5 +x(c6+c7)) + x^(a+b)(c0 +x c1+x^2(c2+c4)) + x^(2a+b)(x^2 c3) + x^(a+c)(c9+x c8)

All unique combinations of powers involving variables are collected first, and then the remaining integer powers are collected at a second level, so to say.

$\endgroup$

1 Answer 1

2
$\begingroup$

Update: This matches the evaluated form of your desired output for each example:

fn = Collect[#, x^y_ /; FreeQ[y, _Integer], Apart] &;

Test:

expr1 = c0 x^(a + b) + c1 x^(a + b + 1) + c2 x^(a + b + 2) + c3 x^(a + b + 2) + 
   c4 x^(a + b + 2) + c5 x^(a) + c6 x^(a + 1) + c7 x^(a + 1);

target1 = x^(a) (c5 + x (c6 + c7)) + x^(a + b) (c0 + x c1 + x^2 (c2 + c3 + c4));


expr2 = c0 x^(a + b) + c1 x^(a + b + 1) + c2 x^(a + b + 2) + c3 x^(2 a + b + 2) + 
   c4 x^(a + b + 2) + c5 x^(a) + c6 x^(a + 1) + c7 x^(a + 1) + c8 x^(a + c + 1) + 
   c9 x^(a + c);

target2 = x^(a) (c5 + x (c6 + c7)) + x^(a + b) (c0 + x c1 + x^2 (c2 + c4)) + 
   x^(2 a + b) (x^2 c3) + x^(a + c) (c9 + x c8);

fn[expr1]

fn[expr2]

fn[expr1] === target1

fn[expr2] === target2
x^a (c5 + (c6 + c7) x) + x^(a + b) (c0 + c1 x + (c2 + c3 + c4) x^2)

c3 x^(2 + 2 a + b) + x^a (c5 + (c6 + c7) x) + x^(a + c) (c9 + c8 x) + 
 x^(a + b) (c0 + c1 x + (c2 + c4) x^2)

True

True
$\endgroup$
7
  • $\begingroup$ Yes, it seems to do the right thing at least for the short example above. I'll have to test if it will still work accordingly in more complicated cases. But I see no reason why it should not. $\endgroup$
    – Kagaratsch
    Oct 28, 2014 at 0:25
  • $\begingroup$ I just wonder what the syntax /@ means? $\endgroup$
    – Kagaratsch
    Oct 28, 2014 at 0:26
  • $\begingroup$ Now I tried a slightly more complicated example and unfortunately the result was not correct. See my edit above. $\endgroup$
    – Kagaratsch
    Oct 28, 2014 at 0:33
  • $\begingroup$ @Kagaratsch Please include the desired output for the second example. $\endgroup$
    – Mr.Wizard
    Oct 28, 2014 at 0:41
  • 1
    $\begingroup$ /@ is a short for map[],you can search it in the "Help" document of Mathematic $\endgroup$
    – user21730
    Oct 28, 2014 at 1:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.