# Indexing Nested Loops in C [closed]

I am having trouble indexing correctly the below statement in C inside a function and then returning it as a pointer. The returning part should not be confusing - hopefully - however the indexing is a nuisance. P can be just about any real number greater or equal to 0 and $O_{j-n}^{k-m}$ is supposed to be a pointer to a 2D array of size: [p+1][2*p+1].

Also, this condition holds: $O_{j-n}^{k-m}=0$ for $|k-m|>(j-n)$. This is no big deal since a simple if else statement can take care of it, however the nested loops, pointer and indexing situation stated below is the real deal. Any ideas how to take care of this?

$$\sum_{j=0}^p\sum_{k=-j}^{+j}\sum_{n=0}^{j}\sum_{m=-n}^{n}O_{j-n}^{k-m}$$

Additionally, so far I am stuck with this algorithm which is a nightmare trying to debug.

for (j=0; j<=p; j++){
for (k=-j; k<=j; k++){
for (n=0; n<=j; n++){
for (m=-n; m<=n; m++){
if ( abs(k-m) <= (j-n) ){
printf("\nIndex[%d][%d]:\t %d", j-n, k-m, (2*p+1)*(j-n) + k+j-m-n );
}
}
}
}}


Thanks,

• consistent indenting will make it MUCH easier to read/understand. Suggest using 4 spaces for each level of indenting. (no one wants to look at }}) – user3629249 Feb 6 '16 at 6:59
• suggest, for easier debugging, make each level be a sub function that is called by the higher level function.. Then there will be significant room to debug each sub function separately. As with all very complex problems the better the problem can be broken down into very simple pieces, the easier it is to debug – user3629249 Feb 6 '16 at 7:02
• In the actual code, which is much more complicated and uses other functions and parameters with different indexing, I use consistent and easily identifiable indentations. I simply posted this simple extract this way because this was the only way it fits in the code box (gray). I have no idea why, probably a bug. Anyway, the code by itself uses different indexing for different functions interelated between different loop levels. Calling functions inside functions 4 times creates a bit of another issue when I need to use interrelated indexing for certain functions no? – Inquisitor101 Feb 6 '16 at 11:18

First in your last sum, you have a mistake in the limits (I guess the upper limit would be $n$). Here is the corrected statement:

$$\sum_{j=0}^p\sum_{k=-j}^{+j}\sum_{n=0}^{j}\sum_{m=-n}^{n}O_{j-n}^{k-m}$$

If this is not the case, please update. Next, in the second sum, I believe that upper limit is $k$, not $j$, for the proper nest operation (other-wise it's not a nest, and you could decouple the loops). Correct me if I'm wrong:

$$\sum_{j=0}^p\sum_{k=-j}^{+j}\sum_{n=0}^{k}\sum_{m=-n}^{n}O_{j-n}^{k-m}$$

Next, in all cases, your indices have impossible to occur conditions, which might be unnecessary, so, if I re-write, this is the same thing:

$$\sum_{j=0}^p\sum_{k=0}^{+j}\sum_{n=0}^{k}\sum_{m=-n}^{n}O_{j-n}^{k-m}$$

And here is the code:

for (j=0; j<=p; j++){
for (k=0; k<=j; k++){
for (n=0; n<=k; n++){
for (m=-n; m<=n; m++){
if ( abs(k-m) <= (j-n) ){
printf("\nIndex[%d][%d]:\t %d", j-n, k-m, (2*p+1)*(j-n) + k+j-m-n );
}
}
}
}
}

• Thanks. I corrected the inner-most loop's upper limit. As for the second question, yes it can be decoupled since the rest of the statement is correct. However, this whole term is an expansion for a coefficient with degree 'j' and order 'k' i.e. similar to a 2D array [j][k]. So far, I suppose the statement has a correct indexing output. Currently, I am checking for other errors... Interestingly though, why did you state that my indexing has impossible to occur conditions ? and so why did you choose to start k's summation from 0 and not -j ? – Inquisitor101 Feb 7 '16 at 15:22
• That's because if n start from 0 it can never go up to a value such as -j. This of course only true if the loop upper bound were k, not j. – Tolga Birdal Feb 7 '16 at 17:01
• Not really. I think you misunderstood. If you take a value for let's say: 'j=2' then at that instant: '-2<= k<= +2' and '0<= n <=+2' and '-n<= m <= +n ' and so forth... These are the summations used for translation operators in the fast multipole algorithm in 3D utilizing spherical harmonics. – Inquisitor101 Feb 7 '16 at 20:28
• That's why I said, if it were : ) – Tolga Birdal Feb 7 '16 at 22:01