1
$\begingroup$

I'm trying to evaluate the volume of cut-off simplex, which is $x_i\geq 0$ for all $i\leq n$ and $\sum_{i=1}^{k} x_k\leq\alpha_k$ for all $k$. And there is a restriction $0<\alpha_1\leq\cdots\leq\alpha_n=1$. Note that $n$ can be varied and I code with Module. Here is my implementation:

prob[alpha_] := Module[{xvars, range, expression}, 
    xvars = Table[Symbol["x" <> ToString[i]], {i, 1, Length[alpha]}];
    range = Table[{xvars[[i]], 0, alpha[[i]] - Sum[xvars[[j]], {j, 1, i - 1}]}, {i, 1, Length[alpha]}];
    expression = Factorial[Length[alpha]];
    NIntegrate[expression, Sequence@@range]];

If I run this code with alpha={1}, then the error "NIntegrate: Invalid variable or limit(s) in {x1,0,1}" comes. It I detached "N" from NIntegrate, then it normally runs well. Of course, if I don't use Module, such as

 NIntegrate[1, {x1, 0, 1}, {x2, 0, 1 - x1}]

It also really runs well. I want to find the "fast" evaluation of this cut-off simplex. How can I fix this code to run NIntegrate?

$\endgroup$
0
$\begingroup$

NIntegrate has attribute HoldAll, thus, you have to enforce evaluation, e.g., with Evaluate

prob[alpha_] := 
  Module[{xvars, range, expression}, 
   xvars = Table[Symbol["x" <> ToString[i]], {i, 1, Length[alpha]}];
   range = Table[{xvars[[i]], 0, alpha[[i]] - Sum[xvars[[j]], {j, 1, i - 1}]}, {i, 1, Length[alpha]}];
   expression = Factorial[Length[alpha]];
   NIntegrate[expression, Evaluate[Sequence @@ range]]
   ];
$\endgroup$
  • $\begingroup$ It works! Thank you. $\endgroup$ – wooa0923 Oct 17 '17 at 5:57
  • $\begingroup$ You're welcome! $\endgroup$ – Henrik Schumacher Oct 17 '17 at 5:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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