# sum simplification

I have this sum

 Sum[-E^(-j (x kx[p] + y ky[q] + z kz[r])) j g w ky[q] Ux[p, q, r],
{p, -∞, ∞}, {q, -∞, ∞}, {r, -∞, ∞}]


and I would like to pull constants out of the triple sum. I have so many terms that is why I prefer to do it automatic.

This is kind of ugly, but here goes:

factorFromSum[sum_, vars_] :=
Inactivate[sum, Sum] /.
Inactive[Sum][Times[a_, rest_?(Function[{pat}, AllTrue[FreeQ[pat, #] &]@vars])], r___]
:>
rest Inactive[Sum][a, r]
SetAttributes[factorFromSum, HoldFirst];


Example:

factorFromSum[Sum[5 x^2, {x, 1, 10}], {x}]


outputs

5 Inactive[Sum][x^2, {x, 1, 10}]


To reenable the sum, finally perform Activate[#, Sum]&.

Your final output is given by

factorFromSum[
Sum[-E^(-j (x kx[p] + y ky[q] + z kz[r])) j g w ky[q] Ux[p, q, r],
{p, -Infinity, Infinity},
{q, -Infinity, Infinity},
{r, -Infinity, Infinity}],
{p, q, r}]
// Activate[#, Sum]&

• It should be: Inactive[Sum][a, r],not Inactive[Sum][a, r]}? – Mariusz Iwaniuk Mar 13 '18 at 22:34
• @MariuszIwaniuk Quite right, sorry; a legacy of when I had the rule wrapped in a list. – Patrick Stevens Mar 13 '18 at 23:13
• Many thanks for all. – qahtah Mar 14 '18 at 17:22