Added sign() for matrix objects.

This commit is contained in:
Davis King 2013-11-17 13:24:00 -05:00
parent 31727df741
commit 2a4e62f222
3 changed files with 43 additions and 0 deletions

View File

@ -79,6 +79,15 @@ namespace dlib
return val*val;
}
template <typename type>
inline type sign (const type& val)
{
if (val >= 0)
return +1;
else
return -1;
}
template <typename type>
type cubed (const type& val)
{
@ -153,6 +162,7 @@ namespace dlib
DLIB_DEFINE_FUNCTION_M(op_round_zeros2, round_zeros, impl::round_zeros, 7);
DLIB_DEFINE_FUNCTION_M(op_cubed, cubed, impl::cubed, 7);
DLIB_DEFINE_FUNCTION_M(op_squared, squared, impl::squared, 6);
DLIB_DEFINE_FUNCTION_M(op_sign, sign, impl::sign, 6);
DLIB_DEFINE_FUNCTION_MS(op_pow1, pow, impl::pow1, 7);
DLIB_DEFINE_FUNCTION_SM(op_pow2, pow, impl::pow2, 7);
DLIB_DEFINE_FUNCTION_M(op_reciprocal, reciprocal, impl::reciprocal, 6);

View File

@ -136,6 +136,24 @@ namespace dlib
// ----------------------------------------------------------------------------------------
// Miscellaneous
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
const matrix_exp sign (
const matrix_exp& m
);
/*!
ensures
- returns a matrix that tells the sign of each element in m. In particular:
returns a matrix R such that:
- R::type == the same type that was in m
- R has the same dimensions as m
- for all valid r and c:
- if (m(r,c) >= 0) then
- R(r,c) == +1
- else
- R(r,c) == -1
!*/
// ----------------------------------------------------------------------------------------
const matrix_exp sigmoid (

View File

@ -997,6 +997,21 @@ namespace
DLIB_TEST(sum_rows(a) == c);
}
{
matrix<int> m(3,4), s(3,4);
m = -2, 1, 5, -5,
5, 5, 5, 5,
9, 0, -4, -2;
s = -1, 1, 1, -1,
1, 1, 1, 1,
1, 1, -1, -1;
DLIB_TEST(sign(m) == s);
DLIB_TEST(sign(matrix_cast<double>(m)) == matrix_cast<double>(s));
}
}