Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
    
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
up vote 4 down vote accepted

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
share|improve this answer
    
Thanks mate, works like a charm. I also learned about @@, I am new to mathematica so these tips are useful to me – Ion Nechita Mar 14 '12 at 12:15
    
@Ion Well, then you can go ahead and change expression using @@ – b.gatessucks Mar 14 '12 at 12:17
1  
:) expression = Times @@ xvars; right ? – Ion Nechita Mar 14 '12 at 12:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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