2

Similar to this question: Turn while loop into math equation?, I have the following nested loop that I am trying to convert into a math equation as I need to write this up in a format that doesn't look like code. I believe I am going to need some type of summation equation.

Here is the code:

int num = 0;
for (int i = nr - 1; i >= 0; i--) {
    for (int j = nc - 1; j >= 0; j--) {
        ela[i][j] = num;
        eha[i][j] = num + ea[i][j] - 1;
        num += ea[i][j];                    
    }
}

I know that summations start from a lower bound and continue to a higher bound, so I'm not quite sure how to apply a summation here since I start from a higher index and continue to a lower index.

I'm not sure why I'm getting downvoted, as the question I referenced is very similar to mine, has the same tags and is upvoted 14 times. Please comment below if I can improve my question somehow.

Update:

I was able to update the formula as follows:

nr = 50;
nc = 10;

num = sum[ea[i,j],i=0,nr-1,j=0,nc-1]; // correct based upon nr, nc and ea
for (int i = 0; i < nr; i) {
    for (int j = 0; j < nc; j++) {
        num = num - ea[i,j];
        ela[i][j] = num;
        eha[i][j] = num + ea[i,j] - 1;                              
    }
}
Community
  • 1
  • 1
Veridian
  • 3,531
  • 12
  • 46
  • 80

3 Answers3

1

If the problem is just with how to express the sum when you're looping the other direction, you can change your code to:

int num = 0;
for (int i = 0; i < nr; i++) {
    for (int j = 0; j < nc; j++) {
        ela[nr - i][nc - j] = num;
        eha[nr - i][nc - i] = num + ea[nr - i][nc - j] - 1;
        num += ea[nr - i][nc - j];                    
    }
}

I'm not saying you have to change your code to this, but from here it should be more obvious how to change this to use summation notation.

SirGuy
  • 10,660
  • 2
  • 36
  • 66
1

If I am right, you can transcribe the effect as

enter image description here

You can describe this as the matrix ela being a 2D suffix sum of the matrix ea (for every element, sum of the elements that follow in the lexicographical ordering), while eha is the sum of matrices ela and ea minus all ones.

-1

It's hard to tell without any context, but the code in question becomes more intelligible if you think of the arrays as vectors enumerating the elements in reverse order, row-major. The code below is functionally equivalent to the original one posted, but arguably easier to follow.

// n.b. ela[nr - 1 - i][nc - 1 - j] == rela(nc * i + j);
int &rela(int k) { return ela[nr - 1 - k / nc][nc - 1 - k % nc]; }
int &reha(int k) { return elh[nr - 1 - k / nc][nc - 1 - k % nc]; }
int &rea(int k)  { return  ea[nr - 1 - k / nc][nc - 1 - k % nc]; }

for (int k = 0, sum = 0; k < nr * nc - 1; k++) {
    rela(k) = sum;
    sum += rea(k);
    reha(k) = sum - 1;
}

In plain English, rela(k) is the partial sum of rea elements 0 ... k-1 and reha(k) is one less than the partial sum of rea elements 0 ... k (also, rela(k) == reha(k - 1) + 1 for k > 0).

Technically, this description could be translated back in terms of the 2d arrays, but it becomes rather messy quickly.

dxiv
  • 16,984
  • 2
  • 27
  • 49