3
$\begingroup$

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

$\endgroup$
1
  • $\begingroup$ Domnule Nechita, the explanation is that Row is for formatting (display) only. Bine ai venit pe Mathematica.SE! $\endgroup$
    – Szabolcs
    Mar 14, 2012 at 21:59

1 Answer 1

4
$\begingroup$

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
$\endgroup$
3
  • $\begingroup$ Thanks mate, works like a charm. I also learned about @@, I am new to mathematica so these tips are useful to me $\endgroup$ Mar 14, 2012 at 12:15
  • $\begingroup$ @Ion Well, then you can go ahead and change expression using @@ $\endgroup$ Mar 14, 2012 at 12:17
  • 1
    $\begingroup$ :) expression = Times @@ xvars; right ? $\endgroup$ Mar 14, 2012 at 12:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.