# Algorithm for parts integration

Sorry if this is a duplicate, I've searched how to do this to no avail.

What I'd like to do is a function that integrates by parts $n$ times, i.e

$$\int u(x) v(x) dx = u \left(\textstyle{\int}v\right) - \displaystyle{\int} u' \left(\textstyle{\int}v\right)dx$$ where $\textstyle{\int}v$ is the primitive of $v$.

I've done a very rustic function that does this,

parts[u_,v_]:=(#1 Integrate[#2,x] - Integrate[D[#1,x] Integrate[#2,x],x]&[u,v];


which performs well but, as you all can see has -at least- the mayor limitation that u and v should be given as functions of x.

At least it works, for example

In=  parts[Exp[-x],1/x^2]
Out= -Exp[-x]/x - ExpIntegralEi[-x]


The thing is, I'd like to tell parts to operate $n$ times, for example

\begin{align} \mbox{parts}\big(u,v,2\big) &= u \textstyle{\int}v - \mbox{parts}\left(u',\textstyle{\int}v,1\right) \\ \\ \mbox{parts}\big(u(x),v(x),3\big) &= u(x)\textstyle{\int}v(x) - u'\left(\textstyle{\int}\textstyle{\int}v\right) + \mbox{parts}\big(u''(x),\textstyle{\iint}v,1\big) \end{align} and so on.

I hope my question is clear.

You can specify the cases for when $u$ and $v$ are free of variable.

ByParts[u_, v_, t_] :=
With[{w = Integrate[v, t]}, u w - Integrate[D[u, t] w, t]]
ByParts[u_, v_, t_] := u Integrate[v, t] /; FreeQ[u, t]
ByParts[u_, v_, t_] := v Integrate[u, t] /; FreeQ[v, t]


I think I can answer my question.

Mathematically, it makes sense to tell Mma which variable is the one to integrate by parts, like LaplaceTransform or D. Taking this into account, I redefine parts like this

parts[u_,v_,{x_,n_}]:= Sum[(-1)^m D[u,{x,m}] Nest[Integrate[#,x]&,v,m+1],{m,0,n-1}] +
(-1)^n Integrate[D[u,{x,n}] Nest[Integrate[#,x]&,v,n],x]


Again, I believe is rather amateurish, and I'd appreciate comments and other answers.

• D[parts[u[x], v[x], {x, 3}], x] != u[x] v[x] Nov 15, 2013 at 19:58
• @Hector Right. I had an error on the last term. I hadn't multiplied it by $(-1)^n$. Besides that, I don't know why it behaves as you are pointing out. Nov 15, 2013 at 20:10
• Great, you fixed it. Nov 15, 2013 at 20:29

Here is my solution:

Clear[int, integrate];

integrate[integrate[expr_, var1__], var2__] := integrate[expr, var1, var2]

Format@integrate[expr_, var__] := Integrate[expr, var]

int[deri_ rest_, var_, varrest___, deri_ -> oldtarget_] /; deri =!= oldtarget :=
With[{intderi = Integrate[deri, var] /. Integrate -> integrate,
target = oldtarget /. Integrate -> integrate},
int[intderi rest, varrest, intderi -> target] -
int[D[rest, var] intderi, var, varrest, intderi -> target]]

int[expr_, deri_] := expr

Format@int[expr_, var__, deri_] := Integrate[expr, var]


The endpoint of integration is specified by the last argument of int. For example, if I want v[t] to be integrated to Integrate[v[t], t], I just need to write:

int[u[t] v[t], t, v[t] -> Integrate[v[t], t]] If I want to integrate by parts twice:

int[u[t] v[t], t, v[t] -> Integrate[v[t], t, t]] Three times:

int[u[t] v[t], t, v[t] -> Integrate[v[t], t, t, t]] 