I am trying to compute the following quantity with a dynamic number of loops. The pseudo-code looks like
When k = 1,
for (x1 =0;x1<=nmax[1];x1++){
a = a + x1 * (x1>upper[1]);
}
When k = 2
for (x1 =0;x1<=nmax[1];x1++){
for (x2 =0;x2<=nmax[2];x2++){
a = a + x1 * (x1<upper[1]) * x2 *(x1+x2>upper[2]);
}
}
When k = 3
for (x1 =0;x1<=nmax[1];x1++){
for (x2 =0;x2<=nmax[2];x2++){
for (x3 =0;x3<=nmax[3];x3++){
a = a + x1 * (x1<upper[1]) * x2 *(x1+x2<upper[2]) * x3 *(x1+x2+x3>upper[3]);
}
}
}
nmax and upper are predefined vector.
To illustrate the logic behind the boolean expressions, as an example, as k increases, the boolean expression develops as follows. For the first one to the second before the last, it's <; whereas the last one uses >.
(x1>upper[1])
(x1<upper[1]) AND (x1+x2>upper[2])
(x1<upper[1]) AND (x1+x2<upper[2]) AND (x1+x2+x3>upper[3])
(x1<upper[1]) AND (x1+x2<upper[2]) AND (x1+x2+x3<upper[3]) AND (x1+x2+x3+X4>upper[4])
(x1<upper[1]) AND (x1+x2<upper[2]) AND (x1+x2+x3<upper[3]) AND (x1+x2+x3+x4<upper[4]) AND (x1+x2+x3+x4+x5>upper[5])
Is there a way to write a function that use k as an argument to achieve the above?
vector<int> xof sizekto all zeros. In a loop, add 1 tox[0]; if this causes it to go overnmax, reset it to 0 and add one tox[1](and ifx[1]goes over as a result, reset it to 0 and incrementx[2], and so on). Basically, increment with carry. For each state ofxthus computed, run a second nested loop to calculate the sum and updatea.