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[1]= parts[Exp[-x],1/x^2]
Out[1]= -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.