Provide isFinite() and isNan() functions

Issue #332 resolved
Mikhail Katliar created an issue

It would be convenient to have functions that check whether a matrix/vector is finite or NaN, analogous to std::isfinite() and std::isnan():

blaze::DynamicVector<double> v {std::numeric_limits<double>::infinity(), 42.};
blaze::DynamicVector<double> v1 {0., 42.};
blaze::DynamicMatrix<double> m {{1., 2.}, {std::numeric_limits<double>::quiet_NaN(), 3.}};

assert(!isFinite(v));
assert(!isNaN(v));

assert(isFinite(v));
assert(!isNaN(v));

assert(!isFinite(m));
assert(isNaN(m));

Comments (6)

  1. Klaus Iglberger

    Hi Misha!

    Thanks for creating this issue. I have good news with respect to a isnan() function: Such a function already exists for both vectors and matrices. I admit that the name is not chosen according to Blaze naming conventions, but it is consistent with the C++ standard function isnan().

    What is still missing is a function based on the C++ standard function isinf(). We will make sure to also add this function for both vectors and matrices. Thanks for pointing out this missing puzzle piece,

    Best regards,

    Klaus!

  2. Klaus Iglberger

    Hi Misha!

    After doing some research we decided to also add the two functions isfinite() (named after the C++ isfinite() function; the function you initially asked for) and isnormal() (named after C++ isnormal()). Thanks again,

    Best regards,

    Klaus!

  3. Klaus Iglberger

    Summary

    The isinf() and isfinite() functions have been added for dense and sparse vectors and dense and sparse matrices. The feature is immediately available via cloning the Blaze repository and will be officially released in Blaze 3.8.

    isinf()

    The isinf() function checks the given dense or sparse vector or dense and sparse matrix for infinite (inf) elements:

    blaze::DynamicVector<double> a;
    // ... Resizing and initialization
    if( isinf( a ) ) { ... }
    
    blaze::DynamicMatrix<double> A;
    // ... Resizing and initialization
    if( isinf( A ) ) { ... }
    

    If at least one element of the vector or matrix is infinite, the function returns true, otherwise it returns false.

    isfinite()

    The isfinite() function checks if all elements of the given dense or sparse vector or dense or sparse matrix are finite elements (i.e. normal, subnormal or zero elements, but not infinite or NaN):

    blaze::DynamicVector<double> a;
    // ... Resizing and initialization
    if( isfinite( a ) ) { ... }
    
    blaze::DynamicMatrix<double> A;
    // ... Resizing and initialization
    if( isfinite( A ) ) { ... }
    

    If all elements of the vector are finite, the function returns true, otherwise it returns false.

  4. Log in to comment