4
$\begingroup$

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.

Thanks in advance

$\endgroup$
3
$\begingroup$

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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