Search
How to dynamically calculate an index?

Given a 3D matrix of shape[3, 5, 7] that is implemented using a 1D array, I can calculate the index of any element (e.g. [1, 0, 6]) as follows:

``````index = 1 * 5 * 7 + 0 * 7 + 6
``````

In general, if I have a K x L x M matrix, I can access index[i][j][k] by calculating iLM + jM + k to yield the index. Likewise, when working with a 4D matrix[h][i][j][k], I can achieve the same by calculating hLMN + iMN + j*N + k.

If I have both the index ([i, j, k]) and the shape ([K, L, M]) stored in two separate arrays of equal length, how could I dynamically generate the above calculations for any two such arrays of arbitrary length 3 or greater?

Been racking my brain on this for hours. Please point me in the right direction.

Thanks,
--anonymouse

On Tuesday, 12 July 2022 at 14:17:36 UTC, ananymouse wrote:

>

Been racking my brain on this for hours. Please point me in the right direction.

Thanks,
--anonymouse

My current implementation:

````````` d
// Allow for indexing to read a value, e.g a[0,0,0]
T opIndex(A...)(A a) {
static if(a.length == 3) {
int index = a[0] * dims[1] * dims[2] + a[2];
return data[index];
}
else static if(a.length == 2) {
return data[a[0] * dims[1] + a[1];
}
else static if (a.length == 1) {
return data[0]
}
assert(0);
}
```
``````

This does not scale well. Therefore, I'd like to change the first if statement to work with any array.length > 2 and dynamically generate the code. I've tried using a foreach loop to achieve this but failed miserably.

--anonymouse

On Tuesday, 12 July 2022 at 14:55:47 UTC, anonymouse wrote:

>

I've tried using a foreach loop to achieve this but failed miserably.

--anonymouse

Wait, I think I've got it.

This little snippet does the trick:

``````int index;
foreach(i, v; a) {
int t = v;
foreach(d; dims[i+1 .. a.length])
tmp *= d;
index += tmp;
}
``````

Thanks, everyone.
--anonymouse