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

So, I'm working on a project where the number of independent variables is not fixed.

Consider a problem of $N$ independent variables, $\boldsymbol{r}$.

I want to perform different things with them. Amongst them, I want to consider (multidimensional) integration, etc.

Variables definition

My first question regarding this topic, is the definition of the variables to perform algebraic manipulation. My first though was to use


However, in some situations, (e.g. with Block), I cannot use these variables as I use x1,x2,.... e.g.


gives an error.

(my current naive solution is to use):

variables[N_] := Table[ToExpression["x" <> ToString[i]], {i, 1, N}];

Is there any more standard solution?

Sums, integrals

This question also holds for the problem of computing integrals for arbitrary dimensions.

How can I tell Mathematica to compute

Integrate[f[{r1,r2,...,rn}], {r1, 0, 1}, {r2, 0, g[r1]},...,{rN, 0, h[{r1,r2,...,"rN-1"}]}]

Most of the times I will be interested in numerically compute the integral, but nevertheless, how do I tell Mathematica? I tried the simple "naive"

Integrate[1, Table[{i, 0, 1}, {i, variables[3]}]]

but it gives an error.

share|improve this question
Try Integrate[1, Sequence @@ Table[{i, 0, 1}, {i, variables[3]}]]. – b.gatessucks Feb 25 '13 at 10:53
You can use something like Table[Unique["x"], {5}] to create variables. – Silvia Feb 25 '13 at 11:06
You might find some useful ideas in this previous question and its answers. Also, in this one. – m_goldberg Feb 25 '13 at 11:57
Thank you all for the suggestions. @b.gatessucks: The Sequence works for Integrals, but not for sums. – J. C. Leitão Feb 25 '13 at 18:08
@J.C.Leitão: dump: because it has an holdall attribute – J. C. Leitão Feb 25 '13 at 18:09

You might use:

variables[n_, sym_String: "x"] := Unique @ Table[sym, {n}]



variables[3, "Q"]
{x1, x2, x3, x4, x5}

{x6, x7, x8, x9, x10}

{Q1, Q2, Q3}

Note the difference on the second call.

For work in Sum et al. you can leverage the fact that a plain Function evaluates its arguments:

vars = variables[7, "z"];

Sum[Multinomial @@ vars, ##] & @@ ({#, 0, 1} & /@ vars)
share|improve this answer

Your Answer


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.