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?
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?
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?
Thank your for your reply Gianluca! However, I was looking for a way to do it programmatically. Would you know if this is possible?
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]