Skip to content

Add Fixed-Size Sparse Matrices Support

Reference issue

#2207

What does this implement/fix?

This merge request adds support for fixed-size sparse matrices to Eigen, enabling compile-time-defined dimensions and non-zero elements. The key changes are the following:

  • Added new template parameters to the SparseMatrix to specify rows, cols, non-zero at compile time
  • Restructured CompressedStorage to support fixed-size storage with the new template parameter MaxSize
  • Added InnerStorage to store outer indices, with template specializations for both dynamic and fixed sizes
  • Specialize CompressedStorage and InnerStorage for static array storage for values and indices when the max size is known at compile time
  • Used SFINAE (std::enable_if) to conditionally compile different methods for static vs. dynamic matrices
  • Added assertions that prevent calling dynamic methods (like resize()) on static matrices
  • Added new test file specifically for testing static sparse matrices sparse_nomalloc.cpp

Example Usage

// Fixed-size sparse matrix with maximum 4 non-zeros
SparseMatrix<double, ColMajor, int, 4, 4, 4> matrix;

// Fixed-size, non-square matrix with maximum 7 non-zeros
SparseMatrix<float, RowMajor, int, 3, 7, 7> nonsquare_matrix;

// Complex number support with 8 non-zeros maximum
SparseMatrix<std::complex<double>, RowMajor, int, 10, 10, 8> complex_matrix;

These changes maintain complete backward compatibility with existing dynamically-sized SparseMatrix.

Additional information

Some if-s could be if constexpr-s, but it all depends on which version of Eigen this change would go if accepted, eventually.

Merge request reports

Loading