Manually set lower bound for CompressedMatrix zero value

Issue #65 resolved
BAZfp created an issue

It's useful when performing functions on a CompressedMatrix to be able to "prune" close-to-zero values from it. After a few operations my CompressedMatrix is effectively becoming dense due to the large number of close-to-zero values. Being able to define a threshold and manually prune values from CompressedMatrices would reduce this problem.

Comments (5)

  1. Klaus Iglberger

    Hi!

    Thanks a lot for the good proposal. We will add the functionality as soon as possible. Until we have decided on the final interface for the feature and finished the implementation, you can use the following function to prune close-to-zero elements from a CompressedMatrix:

    template< typename Type, bool SO >
    void prune( CompressedMatrix<Type,SO>& m, const Type& threshold )
    {
       const size_t num( SO == rowMajor ? m.rows() : m.columns() );
       for( size_t i=0UL; i<num; ++i ) {
          m.erase( i
                 , std::remove_if( m.begin(i), m.end(i),
                                   [threshold=threshold]( const auto& element ){ return std::abs( element.value() ) <= threshold; } )
                 , m.end(i) );
       }
    }
    

    Best regards,

    Klaus!

  2. BAZfp reporter

    Thank you Klaus for your response. I look forward to this being implemented. Is there an IRC channel or forums to discuss blaze further with you?

  3. Klaus Iglberger

    Summary

    The feature has been implemented, tested, optimized and documented as required. It is immediately available via cloning the Blaze repository and will be officially released in Blaze 3.1.

    Sparse Vectors

    The interface of all sparse vectors (CompressedVector and all sparse vector views) has been extended by two additional erase() member functions, which accept an unary predicate:

    template< typename Pred, typename = DisableIf_< IsIntegral<Pred> > >
    void erase( Pred predicate );
    
    template< typename Pred >
    void erase( Iterator first, Iterator last, Pred predicate );
    

    Via these functions it is for instance possible to erase all elements with values smaller than a certain threshold:

    blaze::CompressedVector<double> a;
    // ... Resizing and initialization
    
    a.erase( []( double value ){ return value < 1E-8; } );
    

    Note: The predicate is required to be pure, i.e. to produce deterministic results for elements with the same value. The attempt to use an impure predicate leads to undefined behavior!

    Sparse Matrices

    The interface of all sparse matrices (CompressedMatrix, all adaptors, and all sparse matrix views) has been extended by two additional erase() member functions, which accept an unary predicate:

    template< typename Pred >
    void erase( Pred predicate );
    
    template< typename Pred >
    void erase( size_t i, Iterator first, Iterator last, Pred predicate );
    

    Via these functions it is for instance possible to erase all elements with values smaller than a certain threshold:

    blaze::CompressedMatrix<double,blaze::rowMajor> A;
    // ... Resizing and initialization
    
    A.erase( []( double value ){ return value < 1E-8; } );
    

    Note: The predicate is required to be pure, i.e. to produce deterministic results for elements with the same value. The attempt to use an impure predicate leads to undefined behavior!

  4. Log in to comment