Is there an obvious way to force Mathematica to separately integrate the terms in a sum? According to the docs,

When part of a sum cannot be integrated explicitly, the whole sum will stay unintegrated.

But I want to do this because some of the terms have a nice closed form, and some don't, but I want to compute the former.

Here's what I've done; it works, but it seems inelegant:

integrand = m^2 + mp^2 + mp f[mp] (* the actual integrand is much more complicated! *)

(myint[integrand // Expand] 
    //.  myint[a_ + b_] :> myint[a] + myint[b]
    //.  myint[i_] :> Integrate[i, {mp, -1, 1}]) // FullSimplify

Which gives the correct

2/3 +  2*m^2 + Integrate[mp*f[mp], {mp, -1, 1}]

You could also use Distribute:

Integrate[integrand, mp] // Distribute

$m^2 \text{mp}+\frac{\text{mp}^3}{3}+\int \text{mp} f[\text{mp}] \, d\text{mp}$

  • $\begingroup$ Aha! That seems like the real right answer... $\endgroup$ – Andrew Jaffe Dec 11 '12 at 17:27
  • 1
    $\begingroup$ Nice application of Distribute - +1. $\endgroup$ – Leonid Shifrin Dec 11 '12 at 17:29
  • 1
    $\begingroup$ @AndrewJaffe That depends on the situation. The version of the Jens's answer will not attempt to compute the full integral, breaking it to pieces first. This one will first attempt to compute the full integral, and Distribute will work on the result. You probably do want this behavior in most cases, but I can also imagine cases where you don't want the full integration to be attempted. You could of course use this one with Unevaluated in those cases too. $\endgroup$ – Leonid Shifrin Dec 11 '12 at 17:32
  • 1
    $\begingroup$ But beware: neither Map nor Distribute absolve you from using common sense. For example, just like Map can be misused on non-sums, you can equally misuse Distribute with non-distributable operations! For example, don't try this at home: Power[integrand, 2] // Distribute $\endgroup$ – Jens Dec 11 '12 at 18:06

You can use Map because it works with expressions of Head other than List, too:

integrand = m^2 + mp^2 + mp f[mp];
Map[Integrate[#, mp] &, integrand]

$\int \text{mp} f(\text{mp}) \, d\text{mp}+m^2 \text{mp}+\frac{\text{mp}^3}{3}$

Or the definitie integral:

Map[Integrate[#, {mp, -1, 1}] &, integrand]

$\int_{-1}^1 \text{mp} f(\text{mp}) \, d\text{mp}+2 m^2+\frac{2}{3}$


It's true that Distribute is more appropriate for distributing over sums (so I upvoted that too), but one potential advantage of Map is that it lets you do neat things like this:

integrand = m^2 + mp^2 == mp f[mp];    
Map[Integrate[#, {mp, -1, 1}] &, integrand]

$2 m^2+\frac{2}{3}=\int_{-1}^1 \text{mp} f(\text{mp}) \, d\text{mp}$

In other words, I've integrated an equation on both sides. This could also be combined with distribute - so I think both have their uses.

  • $\begingroup$ Of course (I've been using mathematica for long enough I should have figured this out)! $\endgroup$ – Andrew Jaffe Dec 11 '12 at 17:13
  • $\begingroup$ Accepted, although in retrospect I guess this is dangerous if the head of the integrand isn't Sum... $\endgroup$ – Andrew Jaffe Dec 11 '12 at 17:22
  • $\begingroup$ @AndrewJaffe You can use a version like Replace[integrand, Plus[terms__]:>Plus@@Map[Integrate[#, mp] &,{terms}]. Since Replace only works on the level 0 by default, this should work fine. Yet another option is to create a custom function where you use pattern-matching to test the argument, like integrate[integrand_Plus]:=Map[...]. This will also effectively protect from inappropriate inputs. $\endgroup$ – Leonid Shifrin Dec 11 '12 at 17:24
  • $\begingroup$ Regarding the dangers, see my comment below the answer by @Simon Woods. $\endgroup$ – Jens Dec 11 '12 at 18:08

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.