Added lowerbound() and upperbound() routines.

This commit is contained in:
Davis King 2012-03-29 21:13:33 -04:00
parent 28215b40de
commit 8c1d1b08fa
3 changed files with 135 additions and 0 deletions

View File

@ -2872,6 +2872,80 @@ namespace dlib
return matrix_op<op>(op(m.ref(),lower, upper));
}
// ----------------------------------------------------------------------------------------
template <typename M>
struct op_lowerbound : basic_op_m<M>
{
typedef typename M::type type;
op_lowerbound( const M& m_, const type& thresh_) :
basic_op_m<M>(m_), thresh(thresh_){}
const type& thresh;
typedef const typename M::type const_ret_type;
const static long cost = M::cost + 2;
const_ret_type apply ( long r, long c) const
{
const type temp = this->m(r,c);
if (temp >= thresh)
return temp;
else
return thresh;
}
};
template <
typename EXP
>
const matrix_op<op_lowerbound<EXP> > lowerbound (
const matrix_exp<EXP>& m,
const typename EXP::type& thresh
)
{
typedef op_lowerbound<EXP> op;
return matrix_op<op>(op(m.ref(), thresh));
}
// ----------------------------------------------------------------------------------------
template <typename M>
struct op_upperbound : basic_op_m<M>
{
typedef typename M::type type;
op_upperbound( const M& m_, const type& thresh_) :
basic_op_m<M>(m_), thresh(thresh_){}
const type& thresh;
typedef const typename M::type const_ret_type;
const static long cost = M::cost + 2;
const_ret_type apply ( long r, long c) const
{
const type temp = this->m(r,c);
if (temp <= thresh)
return temp;
else
return thresh;
}
};
template <
typename EXP
>
const matrix_op<op_upperbound<EXP> > upperbound (
const matrix_exp<EXP>& m,
const typename EXP::type& thresh
)
{
typedef op_upperbound<EXP> op;
return matrix_op<op>(op(m.ref(), thresh));
}
// ----------------------------------------------------------------------------------------
template <typename M>

View File

@ -1707,6 +1707,42 @@ namespace dlib
- R(r,c) == m(r,c)
!*/
// ----------------------------------------------------------------------------------------
const matrix_exp lowerbound (
const matrix_exp& m,
const matrix_exp::type& thresh
);
/*!
ensures
- 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) >= thresh) then
- R(r,c) == m(r,c)
- else
- R(r,c) == thresh
!*/
// ----------------------------------------------------------------------------------------
const matrix_exp upperbound (
const matrix_exp& m,
const matrix_exp::type& thresh
);
/*!
ensures
- 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) <= thresh) then
- R(r,c) == m(r,c)
- else
- R(r,c) == thresh
!*/
// ----------------------------------------------------------------------------------------
}

View File

@ -607,6 +607,31 @@ namespace
DLIB_TEST(equal(0.0/m , zeros_matrix<double>(3,4)));
}
}
{
matrix<int> m(2,3);
m = 1,2,3,
4,5,6;
matrix<int> M(2,3);
M = m;
DLIB_TEST(upperbound(m,6) == M);
DLIB_TEST(upperbound(m,60) == M);
DLIB_TEST(lowerbound(m,-2) == M);
DLIB_TEST(lowerbound(m,0) == M);
M = 2,2,3,
4,5,6;
DLIB_TEST(lowerbound(m,2) == M);
M = 0,0,0,
0,0,0;
DLIB_TEST(upperbound(m,0) == M);
M = 1,2,3,
3,3,3;
DLIB_TEST(upperbound(m,3) == M);
}
}