resetting a sparse matrix without loosing its sparsity
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)
-
-
- changed status to wontfix
The
forEach()
function has been introduced in Blaze 3.0 (see the Release Archive). -
reporter Hi Klaus,
Thank you very much, it is exactly what I am looking for,
Best regards,
-
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...
-
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 theerase()
member functions ofCompressedVector
andCompressedMatrix
: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!
-
OK, great. Thanks for the quick turnaround!
- Log in to comment
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:The following example demonstrates that via this approach all elements are set to 0 and not erased from the matrix:
Hopefully this is the solution you are looking for.
Best regards,
Klaus!