3

I am trying to compute symbolically a n-fold integral (n is a parameter of a function) over, say, the cube [0,a]^n. My code looks like this

intCube[n_, a_] := Module[{xvars, range, expression},
xvars = Table[Symbol["x" <> ToString[i]], {i, 1, n}];
range = Table[{xvars[[i]], 0, a}, {i, 1, n}];
range = Row[Riffle[range, ","]];
Print[range];
expression = Product[xvars[[i]], {i, 1, n}];
Print[expression];
Integrate[expression, range]
];

The range and the expression to integrate look both ok, however, I get an awkward result. Any ideas why ?

intCube[2, a]

{x1,0,a},{x2,0,a}
x1 x2
x1 x2 Row[{{x1, 0, a}, ",", {x2, 0, a}}]

Thanks,

Ion

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
Ion Nechita
  • 215
  • 1
  • 5
  • Domnule Nechita, the explanation is that Row is for formatting (display) only. Bine ai venit pe Mathematica.SE! – Szabolcs Mar 14 '12 at 21:59

1 Answers1

4

Use Sequence to paste in the range in Integrate :

Remove[intCube]
intCube[n_, a_] := 
Module[{xvars, range, expression}, 
    xvars = Table[Symbol["x" <> ToString[i]], {i, 1, n}];
    range = Table[{xvars[[i]], 0, a}, {i, 1, n}];
    (* range=Row[Riffle[range,","]]; *)
    Print[range];
    expression = Product[xvars[[i]], {i, 1, n}]; Print[expression]; 
    Integrate[expression, Sequence @@ range]
];

{{x1,0,a},{x2,0,a}}

x1 x2

a^4/4
b.gates.you.know.what
  • 20,103
  • 2
  • 43
  • 84