for my C++ course at may university, we were asked to write a small jet analysis for some LHC data. While writing mine, I was confronted with this: How do you store a symmetrical N×N matrix efficiently in C++?
If you just store it as a normal matrix, the needed space will go with N2. Also, every update has to be done twice. Not very nice (and that does not even take any problem with multidimensional arrays in C++ in to account)
Which does not look like its much but for simplicity sake I am only using an N=4-matrix. For my analysis, N was around 10000. So how much elements do we really need?
The first row has 1 element, the second 2 and so on up to the last row which has N elements. Therefore we have a total of ∑_i=1Ni=2N2+N elements. Great! We basically need only half the elements and therefore only half the RAM!
But how do we store it? We need a way to transform this multidimensional array into a single dimensional array and then use a simple function which transforms the 2D coordinates i,j to an index p of our single dimensional array. Lets take a look at the matrix again: