Triangular matrix object

In a C++ simulation I have an n*n matrix, it’s so large that it can’t fit my memory, but this matrix is symmetrical, How could I generate only one of the triangular parts and free the other triangle from being held by the memory, this should clear half of the memory. I use basic vector or Eigen Library

Answer

You could create a vector of different sized vectors, like this:

int main() {
 const int size = 10;
 vector<vector<int>> matrix;

 for(int i = 1; i <= size; i++) {
  // Each vector is of size i 
  matrix.push_back(vector<int>(i));
 }

 return 0;
}

Just wrap this in a ‘symmetrical matrix’ class and add appropriate accessor methods.

You could do something similar with raw arrays.

Leave a Reply

Your email address will not be published. Required fields are marked *