# Taking advantage of linearity of integration in Mathematica

I want to evaluate an integral of form given below

$$\int\limits_\alpha^\beta (f(x) + g(x) + h(x) + ...) dx$$

When I give it to Mathematica it takes forever to evaluate. But if I give it in this form

$$\int\limits_\alpha^\beta f(x)dx + \int\limits_\alpha^\beta g(x)dx + \int\limits_\alpha^\beta h(x)dx + ...$$

It takes comparatively lesser time.

integrate[y_ + z_, x_] :=
integrate[y, x] + integrate[z, x]


for two variables. But I want to be able to do this for arbitrary number of variables. How to is the question.

-
Perhaps you could list your functions as $f_1, f_2, \ldots$ instead of $f(x), g(x), \ldots$ and set mathematica up to read it as $\displaystyle\sum_{i=1}^n \displaystyle\int_{\alpha}^{\beta} f_i(x) dx$? I don't have the mathematica skill to tell you the exact code, though. –  tomcuchta Jul 19 '11 at 0:05
I got it integrate[y_ + z_, x_] := integrate[y, x] + integrate[z, x] is recursively defined. It takes care of arbitrary summation number of functions. Now my problem is that integrate does not Integrate. –  Pratik Deoghare Jul 19 '11 at 1:42
I tried integrate := Integrate and wow!! it worked! –  Pratik Deoghare Jul 19 '11 at 1:43

I just noticed this question, so please forgive the (very) late reply.

If you want a function that will automatically split across addition, like you've tried to define, I'd do this

Clear[integrate]
integrate[a_Plus, x_, opts:OptionsPattern[]] :=
integrate[#, x, opts]& /@ a


which with input

integrate[a + b + c, {x, 0, 5}]


gives

integrate[a, {x, 0, 5}] + integrate[b, {x, 0, 5}]
+ integrate[c, {x, 0, 5}]


Then, you can define

integrate[a_, x_, opts:OptionsPattern[]]:= Integrate[a, x, opts]


to map it back to the original function.

-

For $$\int\limits_\alpha^\beta (f(x) + g(x) + h(x) + ...) dx$$

  In[1]:=  f[x_]:= your definition

This does what you want, i.e integrates the $f,g,h\cdots$ and then adds them, rather than adding and then integrating. Tested on Mathematica 7
Problem is I get output in the form of $f(x)+g(x)+h(x)+...$ after doing a lot many operations, say after expanding something. I am not defining the functions. –  Pratik Deoghare Jul 19 '11 at 0:39
@MachineCharmer is that perhaps because you have used the lower case i for Integrate in your question. Mathematica is case sensitive. –  kuch nahi Jul 19 '11 at 0:41
i.e integrate[x,y] just echos the same expression while Integrate[x,y] returns xy Maybe that is why you are getting the sums –  kuch nahi Jul 19 '11 at 0:42
@kuch nahi: I think he means that he has arbitrary number of terms in his sum. You could generalize this answer by automatically splitting the integrand with F = Apply[List, q[x]] where g[x] = f[x] + g[x] + ... (untested). Edit: I missed that a recursive solution was found in the other comments. –  Mikael Öhman Jul 19 '11 at 2:16