Add isEmpty() function

Issue #198 resolved
Mikhail Katliar created an issue

Sometimes it is necessary to check whether a matrix or vector is empty. For a vector it can be done by comparing its size() to 0; for matrices there is no size() and one needs to check that both rows() and columns() are 0.

It would be convenient to have a function which checks if a matrix or vector has 0 elements in a uniform way.

Also, it would be useful to have a function returning the total number of elements for a matrix.

Comments (8)

  1. Klaus Iglberger

    Hi Mikhail!

    Thanks for the proposal. This sound like a reasonable addition to Blaze. Until we have come to a decision about the details, you can use the following two functions to achieve your goal:

    template< typename VT, bool TF >
    bool isEmpty( const Vector<VT,TF>& v )
    {
       return (~v).size() == 0UL;
    }
    
    template< typename MT, bool SO >
    bool isEmpty( const Matrix<MT,SO>& m )
    {
       return (~m).rows() == 0UL && (~m).columns() == 0UL;
    }
    

    Thanks for sharing the idea,

    Best regards,

    Klaus!

  2. Klaus Iglberger

    Hi Mikhail!

    We would introduce a size() function for matrices that returns the total number of elements:

    template< typename MT, bool SO >
    constexpr size_t size( const Matrix<MT,SO>& m ) noexcept
    {
       return (~m).rows() * (~m).columns();
    }
    

    Based on this function we could provide the isEmpty() function:

    template< typename MT, bool SO >
    constexpr bool isEmpty( const Matrix<MT,SO>& m ) noexcept
    {
       return size( ~m ) == 0UL;
    }
    

    However, the semantics of this isEmpty() function is different from the one you propose. Your function returns false only in case both rows() and columns() return 0. The solution we have in mind would return false in case either rows() or columns() returns 0. We feel this is reasonable because isEmpty() would consistently return false whenever the number of elements is 0. Do you agree that this is reasonable semantics?

    Best regards,

    Klaus!

  3. Klaus Iglberger

    Summary

    The isEmpty() function for both vectors and matrices and the size() function for matrices have 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.5.

    size()

    The size() function returns the total number of elements of a matrix:

    // Instantiating a dynamic matrix with 6 rows and 8 columns
    blaze::DynamicMatrix<int> M1( 6UL, 8UL );
    size( M1 );   // Returns 48
    
    // Instantiating a compressed matrix with 8 rows and 7 columns
    blaze::CompressedMatrix<double> M2( 8UL, 7UL );
    size( M2 );  // Returns 56
    

    isEmpty()

    The isEmpty() function returns whether the total number of elements of the vector or matrix is zero:

    blaze::DynamicVector<int> a;  // Create an empty vector
    isEmpty( a );                 // Returns true
    a.resize( 10 );               // Resize to 10 elements
    isEmpty( a );                 // Returns false
    
    blaze::DynamicMatrix<int> A;  // Create an empty matrix
    isEmpty( A );                 // Returns true
    A.resize( 5, 0 );             // Resize to a 5x0 matrix
    isEmpty( A );                 // Returns true
    A.resize( 5, 3 );             // Resize to a 5x3 matrix
    isEmpty( A );                 // Returns false
    
  4. Mikhail Katliar reporter

    Hello Klaus,

    thanks for implementing it. I agree that the semantics you proposed are reasonable.

  5. Log in to comment