Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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}]
share|improve this question
add comment

2 Answers

up vote 9 down vote accepted

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}$

share|improve this answer
    
Aha! That seems like the real right answer... –  Andrew Jaffe Dec 11 '12 at 17:27
1  
Nice application of Distribute - +1. –  Leonid Shifrin Dec 11 '12 at 17:29
1  
@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. –  Leonid Shifrin Dec 11 '12 at 17:32
1  
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 –  Jens Dec 11 '12 at 18:06
add comment

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}$

Edit

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.

share|improve this answer
    
Of course (I've been using mathematica for long enough I should have figured this out)! –  Andrew Jaffe Dec 11 '12 at 17:13
    
Accepted, although in retrospect I guess this is dangerous if the head of the integrand isn't Sum... –  Andrew Jaffe Dec 11 '12 at 17:22
    
@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. –  Leonid Shifrin Dec 11 '12 at 17:24
    
Regarding the dangers, see my comment below the answer by @Simon Woods. –  Jens Dec 11 '12 at 18:08
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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