Commit d9a93fdc authored by Davis King's avatar Davis King

Added is_finite() for matrix objects.

parent fe410de2
......@@ -451,26 +451,6 @@ namespace dlib
template <> struct is_float_type<double> { const static bool value = true; };
template <> struct is_float_type<long double> { const static bool value = true; };
// ----------------------------------------------------------------------------------------
template <
typename T
>
bool is_finite (
const T& value
)
/*!
ensures
- returns true if value is a finite value (e.g. not infinity or NaN) and false
otherwise.
!*/
{
if (is_float_type<T>::value)
return -std::numeric_limits<T>::infinity() < value && value < std::numeric_limits<T>::infinity();
else
return true;
}
// ----------------------------------------------------------------------------------------
/*!A is_convertible
......@@ -652,6 +632,28 @@ namespace dlib
// ----------------------------------------------------------------------------------------
template <
typename T
>
typename enable_if<is_built_in_scalar_type<T>,bool>::type is_finite (
const T& value
)
/*!
requires
- value must be some kind of scalar type such as int or double
ensures
- returns true if value is a finite value (e.g. not infinity or NaN) and false
otherwise.
!*/
{
if (is_float_type<T>::value)
return -std::numeric_limits<T>::infinity() < value && value < std::numeric_limits<T>::infinity();
else
return true;
}
// ----------------------------------------------------------------------------------------
/*!A promote
This is a template that takes one of the built in scalar types and gives you another
......
......@@ -63,6 +63,24 @@ namespace dlib
const matrix_exp<EXP>& m
) { return is_row_vector(m) || is_col_vector(m); }
// ----------------------------------------------------------------------------------------
template <typename EXP>
inline bool is_finite (
const matrix_exp<EXP>& m
)
{
for (long r = 0; r < m.nr(); ++r)
{
for (long c = 0; c < m.nc(); ++c)
{
if (!is_finite(m(r,c)))
return false;
}
}
return true;
}
// ----------------------------------------------------------------------------------------
namespace impl
......
......@@ -1041,6 +1041,17 @@ namespace dlib
- returns false
!*/
// ----------------------------------------------------------------------------------------
bool is_finite (
const matrix_exp& m
);
/*!
ensures
- returns true if all the values in m are finite values and also not any kind
of NaN value.
!*/
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// Thresholding relational operators
......
......@@ -1208,6 +1208,7 @@ namespace
m1 = 1;
m2 = 1;
m1 = m1*subm(m2,0,0,3,3);
DLIB_TEST(is_finite(m1));
}
{
matrix<double,3,1> m1;
......@@ -1253,7 +1254,21 @@ namespace
DLIB_TEST(m(0) == 6);
DLIB_TEST(m(1) == 6);
DLIB_TEST(is_finite(m));
}
{
matrix<double> m(3,3);
m = 3;
m(1,1) = std::numeric_limits<double>::infinity();
DLIB_TEST(is_finite(m) == false);
m(1,1) = -std::numeric_limits<double>::infinity();
DLIB_TEST(is_finite(m) == false);
m(1,1) = 2;
DLIB_TEST(is_finite(m));
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment