- edited description
Add isEmpty() function
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)
-
reporter -
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!
-
reporter Thanks for the quick reply, Klaus!
What you wrote is what I am doing now.
-
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 returnsfalse
only in case bothrows()
andcolumns()
return0
. The solution we have in mind would returnfalse
in case eitherrows()
orcolumns()
returns0
. We feel this is reasonable becauseisEmpty()
would consistently returnfalse
whenever the number of elements is0
. Do you agree that this is reasonable semantics?Best regards,
Klaus!
-
-
assigned issue to
-
assigned issue to
-
- changed status to open
-
- changed status to resolved
Summary
The
isEmpty()
function for both vectors and matrices and thesize()
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
-
reporter Hello Klaus,
thanks for implementing it. I agree that the semantics you proposed are reasonable.
- Log in to comment