Message Boards Message Boards

Collect coefficients in a polynomial defined by symbolic summation?

GROUPS:

Hi all, I'd like to distribute the squared sum and collect the coefficients in front of n and n^2 in the following expression:

Sum[a[i] (n (n - 1) + 2 n), {i, 1, K}] - Sum[a[i] n, {i, 1, K}]^2,

Would anyone know how to do this?

POSTED BY: Aurelien Bibaut
Answer
9 days ago

Maybe something like this

Collect[(n (n - 1) + 2 n) Sum[a[i] , {i, 1, K}] -
  n^2 Sum[a[i]
     Sum[a[j], {j, 1, K}], {i, 1, K}],
 n]

Or did you want to do it programmatically?

POSTED BY: Gianluca Gorni
Answer
9 days ago

Thank your for your reply Gianluca! However, I was looking for a way to do it programmatically. Would you know if this is possible?

POSTED BY: Aurelien Bibaut
Answer
8 days ago

You can do it with replacement patterns:

extractCommonFactor = 
  Sum[r_*s_, {sumIndex_, lmts__}] /; FreeQ[s, sumIndex] :>
   s*Sum[r, {sumIndex, lmts}];
expandSquare = Sum[r_, {sumIndex_, lmts__}]^2 :>
   Sum[r*(Sum[r, {sumIndex, lmts}] /. sumIndex -> sumIndex2),
    {sumIndex, lmts}];
Sum[a[i] (n (n - 1) + 2 n), {i, 1, K}] - Sum[a[i] n, {i, 1, K}]^2 /.

   extractCommonFactor /. expandSquare
Collect[%, n]
POSTED BY: Gianluca Gorni
Answer
7 days ago

Group Abstract Group Abstract