A function calculating a multiple integral and taking multiplicity as a parameter

I have a function of the following form:
\varphi_n(x)= \underbrace{\int\limits_\mathbb{R}\ldots\int\limits_\mathbb{R}}_{n}\exp\left[-\sum\limits_{j=2}^{n}\left(x_{j}-x_{j-1}\right)^2-(x-x_{n})^2\right]dx_1\ldots dx_{n}\quad \left(\varphi_n(x)\equiv \pi^{n/2}\right),

and I’m trying to define a function Phi[x, n] that will calculate the above integral. I know, how to define this function for fixed nn, for example
\varphi_1(x)= \int\limits_\mathbb{R}e^{-(x-x_{1})^2}dx_1,

defines as

Phi1 = Integrate[Exp[-(x – #)^2], {x, -Infinity, Infinity}] &;

\varphi_2(x)= \int\limits_\mathbb{R}\int\limits_\mathbb{R}e^{-(x_2-x_1)^2-(x-x_2)^2}dx_1dx_2,

defines as

Phi2 = Integrate[Integrate[Exp[-(x2 – x1)^2 – (# – x2)^2], {x1, -Infinity, Infinity}], {x2, -Infinity, Infinity}] &;

How to define a function, that would take nn as an argument?



3 Answers


I bet you could do this more elegantly with Fold, but this gets you there,

Phi[n_] :=
Integrate[Exp[-Sum[(x[j] – x[j – 1])^2, {j, 2, n}] – (x – x[n])^2],
Sequence @@ ({x[#], -∞, ∞} & /@ Range[n])]

Phi /@ Range[5]
(* {Sqrt[π], π, π^(3/2), π^2, π^(5/2)} *)

You could make it a function of x, but as you write, the answer is independent of x so I don’t see why you would.



@Glinka In order to obtain a true dependence on x you could change the integration interval. (Although the integral might not be solved symbolically anymore).
– Dr. Wolfgang Hintze
Mar 16 at 15:47



@JasonB, thank you for your answer! Your method worked on the example from the question, and it worked when I used a function that actually depended on xx. The problem though that it works very slow when n>3n>3 (for n=3n=3 the process lasts several minutes and for n=4n=4 the process doesn’t seem to end). I figured that I use NIntegrate instead of Integrate, it would speed up a process. But when I just change Integrate to NIntegrate in your code, it show the following error: Invalid integration variable or limit(s) in {x[1],-[Infinity],[Infinity]}. What do I do wrong?
– Glinka
Mar 16 at 19:03



@Glinka It’s true that multidimensional integrals can take a while, and can be sped up by using numerical methods. But it’s hard to fix an error without having the code to reproduce it. You could post it here as a comment or as another question.
– JasonB
Mar 16 at 19:07



@Dr.WolfgangHintz, in our task we always consider integrals over R\mathbb{R}, and a true dependence on x occurs when we consider more complex integrands. In the example from the question xx variable is dummy by accident.
– Glinka
Mar 16 at 19:12



@JasonB, I posted it as a new question: mathematica.stackexchange.com/questions/110197/…
– Glinka
Mar 16 at 19:36

My take:

With[{n = 6},
Integrate[Exp[-#.# & @ Differences[Append[Array[C, n], x]]], ##] & @@
Array[{C[#], -∞, ∞} &, n]]

Here’s some silliness along the lines of Jason B’s answer:

int[n_]:= Integrate[
Exp[-#^2] &@*Subtract @@@ Partition[Array[x, n + 1, 0], 2, 1] // Times @@ # &,
Sequence @@ Array[{x[#], -∞, ∞} &, 3]