# Use double index in matrix multiplication

I want to run a simulation which involves rates between different states. Each state is label by a pair of indices $(m,n)$, so that a certain rate $R_{(m,n)\rightarrow(m',n')}$ requires four indices in total.

What I try to do is to convert these double indices into a single index $j$. This would allow me to use standard matrix multiplication methods to calculate e.g. the product of two matrices $$A_{j_1,j_2} = (B C)_{j_1,j_2}$$

I do see, how to calculate the 1D index, by defining $$j(n,m) = \sum_{n'=0..n-1}M(n) + m$$ Here $M(n)$ is the number of different $m$'s I have for a given $n$. This is the crucial part, the number of different $m$'s is different for each $n$.

However, i do not see how to get $m$ and $n$ back, in case $j$ is given and for a generic $M(n)$.

Is this a standard problem in computer science?

My question is: what strategy can I use to do matrix multiplications when I have double indices?

Won't this work? You create a map from $(m,n)$, the key-pair, to $j$, the value. For this you could use std::map. The reverse map to go from $j$ to $(m,n)$ is just a vector of pairs. Then you can use $j$-like index to perform the matrix operations.