Search code examples
c++mathboostsparse-matrixeigen

Sparse matrix class with parameterizable "zero"


I am doing some computations on a sparse matrix of floats in the log domain, so the "empty" entries are actually -Inf (using -FLT_MAX). I'm using a custom sparse matrix class right now but I am eager to swap in an off-the-shelf replacement.

This is in C++. My inclinations were to look at the compressed column matrices in Eigen and Boost uBlas. However it is not clear that either supports a custom value for "zero" (perhaps provided by a template parameter). Does anyone have a suggestion?

Clarification:

What I want is this: for any cell (i,j) that has not been "set" previously, I would like mat[i,j] to return -Inf ... so this is perhaps better described as a "default" value for the "empty" entries of the sparse matrix.

I am using this to perform HMM recursions (Viterbi, sum-product) with probabilities kept in the log domain to avoid underflow.

I am not doing any matrix operations ... I am just filling in a dynamic programming tableau, essentially. I want to use a sparse matrix class because I am only filling in a band of the matrix and I would like efficient memory use. The compressed band matrices would give good performance since I am filling in the matrix "in order."


Solution

  • The solution I have currently cooked up is this. Define a class lfloat:

    class lfloat {
      float value;
    public:
      lfloat(float f=-FLT_MAX)
      {
        value = f;
      }
    
      lfloat& operator=(float f)
      {
        value = f;
        return *this;
      }
    
      operator float()   { return value; }
    };
    

    and use it like so:

    compressed_matrix<lfloat> M1(3,3);
    

    This way we are not rewriting any of the functionality in the boost matrix classes, but we should get the desired result.