# Collect coefficients in a polynomial defined by symbolic summation?

Posted 8 months ago
867 Views
|
3 Replies
|
1 Total Likes
|
 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?
Answer
3 Replies
Sort By:
Posted 8 months 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?
Answer
Posted 8 months 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?
Answer
Posted 8 months 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] 
Answer
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments