Search code examples
pythonnumpyscipysparse-matrixrotational-matrices

Sparse Rotation Matrix Python SciPy


I'm working with quite large rotation matrices, which have the inherent property to have a large number of zeros. In order to reduce memory use and possibly reduce computation cost when multiplying these rotation matrices with other matrices/vectors, I would like to use a sparse matrix data structure. I have found documentation on the SciPy sparse matrices, but I don't quite understand how these work and what the differences are. (SciPy docs)

What would be the best sparse data structure to use for rotation matrices in Python?


Solution

  • If you already have the rotation matrix as a dense array you can simply do

    m = csr_matrix(dense_rot_matrix)
    

    One of the two types csr_matrix or csc_matrix should be used.

    A better option would be to populate already the sparse matrix which can be easily accomplished using the coo_matrix type, which has efficient methods to convert to csr_matrix or csc_matrix. I've been using Cython to create sparse matrices in this way very efficiently.