resetting a sparse matrix without loosing its sparsity

Issue #87 wontfix
R L created an issue

Hi,

My code spends a lot of time on ordering sparse matrices at each iteration.

Is there a way to set the sparse matrix coefficients to zero without erasing their indexes?

Thank you,

Comments (6)

  1. Klaus Iglberger

    Hi Raounak!

    The easiest and most efficient way to reset the value of each non-zero elements to 0 is to use the forEach() function:

    template< typename T, bool SO >
    void setToZero( blaze::CompressedMatrix<T,SO>& mat )
    {
       mat = blaze::forEach( mat, []( int ){ return 0; } );
    }
    

    The following example demonstrates that via this approach all elements are set to 0 and not erased from the matrix:

    CompressedMatrix<int> A( 3UL, 3UL, 5UL );
    A(0,0) = 1;
    A(0,2) = 2;
    A(1,1) = 3;
    A(2,0) = 4;
    A(2,2) = 5;
    
    std::cerr << "\n A =\n" << A << "\n"
              << " non-zeros = " << A.nonZeros() << "\n";
    
    setToZero( A );
    
    std::cerr << "\n A =\n" << A << "\n"
              << " non-zeros = " << A.nonZeros() << "\n";
    
    Output:
    
    A =
    (            1            0            2 )
    (            0            3            0 )
    (            4            0            5 )
    
    non-zeros = 5
    
    A =
    (            0            0            0 )
    (            0            0            0 )
    (            0            0            0 )
    
    non-zeros = 5
    

    Hopefully this is the solution you are looking for.

    Best regards,

    Klaus!

  2. Evan Berkowitz

    Along the same lines, is it possible to delete a particular entry in a sparse matrix? To start from above example, is it possible to wind up with

    > A(0,0)=0
    
    A =
    (            0            0            2 )
    (            0            3            0 )
    (            4            0            5 )
    
    nonzeros = 4
    

    I can't seem to find whether this is supported in the wiki...

  3. Klaus Iglberger

    Hi Evan!

    This task is performed by the erase() functions, which are available for all sparse vectors and sparse matrices. Via these functions it is possible to erase single elements, ranges of elements, and elements adhering to a given predicate. As an example and overview, the following shows the erase() member functions of CompressedVector and CompressedMatrix:

    template< typename Type, ... >
    class CompressedVector
    {
     public:
       // ...
       inline void     erase( size_t index );
       inline Iterator erase( Iterator pos );
       inline Iterator erase( Iterator first, Iterator last );
    
       template< typename Pred, typename = DisableIf_< IsIntegral<Pred> > >
       inline void erase( Pred predicate );
    
       template< typename Pred >
       inline void erase( Iterator first, Iterator last, Pred predicate );
       // ...
    };
    
    template< typename Type, ... >
    class CompressedMatrix
    {
     public:
       // ...
       inline void     erase( size_t i, size_t j );
       inline Iterator erase( size_t i, Iterator pos );
       inline Iterator erase( size_t i, Iterator first, Iterator last );
    
       template< typename Pred >
       inline void erase( Pred predicate );
    
       template< typename Pred >
       inline void erase( size_t i, Iterator first, Iterator last, Pred predicate );
       // ...
    };
    

    Admittedly, this functionality is not explained in the wiki. We will make sure to update both the tutorial and wiki accordingly.

    Best regards,

    Klaus!

  4. Log in to comment