Commit 4103be8b authored by Davis King's avatar Davis King

Added min_pointwise() and max_pointwise().

parent 30c62962
......@@ -314,6 +314,84 @@ namespace dlib
return val;
}
// ----------------------------------------------------------------------------------------
template <typename M1, typename M2>
struct op_binary_min : basic_op_mm<M1,M2>
{
op_binary_min( const M1& m1_, const M2& m2_) : basic_op_mm<M1,M2>(m1_,m2_){}
typedef typename M1::type type;
typedef const type const_ret_type;
const static long cost = M1::cost + M2::cost + 1;
const_ret_type apply ( long r, long c) const
{ return std::min(this->m1(r,c),this->m2(r,c)); }
};
template <
typename EXP1,
typename EXP2
>
inline const matrix_op<op_binary_min<EXP1,EXP2> > min_pointwise (
const matrix_exp<EXP1>& a,
const matrix_exp<EXP2>& b
)
{
COMPILE_TIME_ASSERT((is_same_type<typename EXP1::type,typename EXP2::type>::value == true));
COMPILE_TIME_ASSERT(EXP1::NR == EXP2::NR || EXP1::NR == 0 || EXP2::NR == 0);
COMPILE_TIME_ASSERT(EXP1::NC == EXP2::NC || EXP1::NC == 0 || EXP2::NC == 0);
DLIB_ASSERT(a.nr() == b.nr() &&
a.nc() == b.nc(),
"\t const matrix_exp min_pointwise(const matrix_exp& a, const matrix_exp& b)"
<< "\n\ta.nr(): " << a.nr()
<< "\n\ta.nc(): " << a.nc()
<< "\n\tb.nr(): " << b.nr()
<< "\n\tb.nc(): " << b.nc()
);
typedef op_binary_min<EXP1,EXP2> op;
return matrix_op<op>(op(a.ref(),b.ref()));
}
// ----------------------------------------------------------------------------------------
template <typename M1, typename M2>
struct op_binary_max : basic_op_mm<M1,M2>
{
op_binary_max( const M1& m1_, const M2& m2_) : basic_op_mm<M1,M2>(m1_,m2_){}
typedef typename M1::type type;
typedef const type const_ret_type;
const static long cost = M1::cost + M2::cost + 1;
const_ret_type apply ( long r, long c) const
{ return std::max(this->m1(r,c),this->m2(r,c)); }
};
template <
typename EXP1,
typename EXP2
>
inline const matrix_op<op_binary_max<EXP1,EXP2> > max_pointwise (
const matrix_exp<EXP1>& a,
const matrix_exp<EXP2>& b
)
{
COMPILE_TIME_ASSERT((is_same_type<typename EXP1::type,typename EXP2::type>::value == true));
COMPILE_TIME_ASSERT(EXP1::NR == EXP2::NR || EXP1::NR == 0 || EXP2::NR == 0);
COMPILE_TIME_ASSERT(EXP1::NC == EXP2::NC || EXP1::NC == 0 || EXP2::NC == 0);
DLIB_ASSERT(a.nr() == b.nr() &&
a.nc() == b.nc(),
"\t const matrix_exp max_pointwise(const matrix_exp& a, const matrix_exp& b)"
<< "\n\ta.nr(): " << a.nr()
<< "\n\ta.nc(): " << a.nc()
<< "\n\tb.nr(): " << b.nr()
<< "\n\tb.nc(): " << b.nc()
);
typedef op_binary_max<EXP1,EXP2> op;
return matrix_op<op>(op(a.ref(),b.ref()));
}
// ----------------------------------------------------------------------------------------
template <
......
......@@ -1361,6 +1361,25 @@ namespace dlib
according to std::norm().
!*/
// ----------------------------------------------------------------------------------------
const matrix_exp min_pointwise (
const matrix_exp& a,
const matrix_exp& b
);
/*!
requires
- a.nr() == b.nr()
- a.nc() == b.nc()
- a and b both contain the same type of element
ensures
- returns a matrix R such that:
- R::type == the same type that was in a and b.
- R has the same dimensions as a and b.
- for all valid r and c:
R(r,c) == std::min(a(r,c), b(r,c))
!*/
// ----------------------------------------------------------------------------------------
const matrix_exp::type max (
......@@ -1375,6 +1394,25 @@ namespace dlib
according to std::norm().
!*/
// ----------------------------------------------------------------------------------------
const matrix_exp max_pointwise (
const matrix_exp& a,
const matrix_exp& b
);
/*!
requires
- a.nr() == b.nr()
- a.nc() == b.nc()
- a and b both contain the same type of element
ensures
- returns a matrix R such that:
- R::type == the same type that was in a and b.
- R has the same dimensions as a and b.
- for all valid r and c:
R(r,c) == std::max(a(r,c), b(r,c))
!*/
// ----------------------------------------------------------------------------------------
void find_min_and_max (
......
......@@ -1063,6 +1063,28 @@ namespace
}
{
matrix<double> a = randm(3,4);
matrix<double> b = randm(3,4);
matrix<double> m1, m2;
m1 = max_pointwise(a,b);
m2 = min_pointwise(a,b);
DLIB_TEST(m1.nr() == a.nr());
DLIB_TEST(m1.nc() == a.nc());
DLIB_TEST(m2.nr() == a.nr());
DLIB_TEST(m2.nc() == a.nc());
for (long r = 0; r < a.nr(); ++r)
{
for (long c = 0; c < a.nc(); ++c)
{
DLIB_TEST_MSG(m1(r,c) == std::max(a(r,c), b(r,c)), m1(r,c) << " : " << a(r,c) << " " << b(r,c));
DLIB_TEST(m2(r,c) == std::min(a(r,c), b(r,c)));
}
}
}
{
matrix<double,4,5> m;
set_subm(m, range(0,3), range(0,4)) = 4;
......
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