Skip to content

Commit ce3ed65

Browse files
committed
Added overloads of max_pointwise() and min_pointwise() that take 3 arguments.
1 parent 678728d commit ce3ed65

2 files changed

Lines changed: 122 additions & 0 deletions

File tree

dlib/matrix/matrix_utilities.h

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,58 @@ namespace dlib
353353
return matrix_op<op>(op(a.ref(),b.ref()));
354354
}
355355

356+
// ----------------------------------------------------------------------------------------
357+
358+
template <typename M1, typename M2, typename M3>
359+
struct op_min_pointwise3 : basic_op_mmm<M1,M2,M3>
360+
{
361+
op_min_pointwise3( const M1& m1_, const M2& m2_, const M3& m3_) :
362+
basic_op_mmm<M1,M2,M3>(m1_,m2_,m3_){}
363+
364+
typedef typename M1::type type;
365+
typedef const typename M1::type const_ret_type;
366+
const static long cost = M1::cost + M2::cost + M3::cost + 2;
367+
368+
const_ret_type apply (long r, long c) const
369+
{ return std::min(this->m1(r,c),std::min(this->m2(r,c),this->m3(r,c))); }
370+
};
371+
372+
template <
373+
typename EXP1,
374+
typename EXP2,
375+
typename EXP3
376+
>
377+
inline const matrix_op<op_min_pointwise3<EXP1,EXP2,EXP3> >
378+
min_pointwise (
379+
const matrix_exp<EXP1>& a,
380+
const matrix_exp<EXP2>& b,
381+
const matrix_exp<EXP3>& c
382+
)
383+
{
384+
COMPILE_TIME_ASSERT((is_same_type<typename EXP1::type,typename EXP2::type>::value == true));
385+
COMPILE_TIME_ASSERT((is_same_type<typename EXP2::type,typename EXP3::type>::value == true));
386+
COMPILE_TIME_ASSERT(EXP1::NR == EXP2::NR || EXP1::NR == 0 || EXP2::NR == 0);
387+
COMPILE_TIME_ASSERT(EXP1::NC == EXP2::NC || EXP1::NR == 0 || EXP2::NC == 0);
388+
COMPILE_TIME_ASSERT(EXP2::NR == EXP3::NR || EXP2::NR == 0 || EXP3::NR == 0);
389+
COMPILE_TIME_ASSERT(EXP2::NC == EXP3::NC || EXP2::NC == 0 || EXP3::NC == 0);
390+
DLIB_ASSERT(a.nr() == b.nr() &&
391+
a.nc() == b.nc() &&
392+
b.nr() == c.nr() &&
393+
b.nc() == c.nc(),
394+
"\tconst matrix_exp min_pointwise(a,b,c)"
395+
<< "\n\tYou can only make a do a pointwise min between equally sized matrices"
396+
<< "\n\ta.nr(): " << a.nr()
397+
<< "\n\ta.nc(): " << a.nc()
398+
<< "\n\tb.nr(): " << b.nr()
399+
<< "\n\tb.nc(): " << b.nc()
400+
<< "\n\tc.nr(): " << c.nr()
401+
<< "\n\tc.nc(): " << c.nc()
402+
);
403+
404+
typedef op_min_pointwise3<EXP1,EXP2,EXP3> op;
405+
return matrix_op<op>(op(a.ref(),b.ref(),c.ref()));
406+
}
407+
356408
// ----------------------------------------------------------------------------------------
357409

358410
template <typename M1, typename M2>
@@ -392,6 +444,58 @@ namespace dlib
392444
return matrix_op<op>(op(a.ref(),b.ref()));
393445
}
394446

447+
// ----------------------------------------------------------------------------------------
448+
449+
template <typename M1, typename M2, typename M3>
450+
struct op_max_pointwise3 : basic_op_mmm<M1,M2,M3>
451+
{
452+
op_max_pointwise3( const M1& m1_, const M2& m2_, const M3& m3_) :
453+
basic_op_mmm<M1,M2,M3>(m1_,m2_,m3_){}
454+
455+
typedef typename M1::type type;
456+
typedef const typename M1::type const_ret_type;
457+
const static long cost = M1::cost + M2::cost + M3::cost + 2;
458+
459+
const_ret_type apply (long r, long c) const
460+
{ return std::max(this->m1(r,c),std::max(this->m2(r,c),this->m3(r,c))); }
461+
};
462+
463+
template <
464+
typename EXP1,
465+
typename EXP2,
466+
typename EXP3
467+
>
468+
inline const matrix_op<op_max_pointwise3<EXP1,EXP2,EXP3> >
469+
max_pointwise (
470+
const matrix_exp<EXP1>& a,
471+
const matrix_exp<EXP2>& b,
472+
const matrix_exp<EXP3>& c
473+
)
474+
{
475+
COMPILE_TIME_ASSERT((is_same_type<typename EXP1::type,typename EXP2::type>::value == true));
476+
COMPILE_TIME_ASSERT((is_same_type<typename EXP2::type,typename EXP3::type>::value == true));
477+
COMPILE_TIME_ASSERT(EXP1::NR == EXP2::NR || EXP1::NR == 0 || EXP2::NR == 0);
478+
COMPILE_TIME_ASSERT(EXP1::NC == EXP2::NC || EXP1::NR == 0 || EXP2::NC == 0);
479+
COMPILE_TIME_ASSERT(EXP2::NR == EXP3::NR || EXP2::NR == 0 || EXP3::NR == 0);
480+
COMPILE_TIME_ASSERT(EXP2::NC == EXP3::NC || EXP2::NC == 0 || EXP3::NC == 0);
481+
DLIB_ASSERT(a.nr() == b.nr() &&
482+
a.nc() == b.nc() &&
483+
b.nr() == c.nr() &&
484+
b.nc() == c.nc(),
485+
"\tconst matrix_exp max_pointwise(a,b,c)"
486+
<< "\n\tYou can only make a do a pointwise max between equally sized matrices"
487+
<< "\n\ta.nr(): " << a.nr()
488+
<< "\n\ta.nc(): " << a.nc()
489+
<< "\n\tb.nr(): " << b.nr()
490+
<< "\n\tb.nc(): " << b.nc()
491+
<< "\n\tc.nr(): " << c.nr()
492+
<< "\n\tc.nc(): " << c.nc()
493+
);
494+
495+
typedef op_max_pointwise3<EXP1,EXP2,EXP3> op;
496+
return matrix_op<op>(op(a.ref(),b.ref(),c.ref()));
497+
}
498+
395499
// ----------------------------------------------------------------------------------------
396500

397501
template <

dlib/matrix/matrix_utilities_abstract.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1380,6 +1380,15 @@ namespace dlib
13801380
R(r,c) == std::min(a(r,c), b(r,c))
13811381
!*/
13821382

1383+
const matrix_exp min_pointwise (
1384+
const matrix_exp& a,
1385+
const matrix_exp& b,
1386+
const matrix_exp& c
1387+
);
1388+
/*!
1389+
performs min_pointwise(a,min_pointwise(b,c));
1390+
!*/
1391+
13831392
// ----------------------------------------------------------------------------------------
13841393

13851394
const matrix_exp::type max (
@@ -1413,6 +1422,15 @@ namespace dlib
14131422
R(r,c) == std::max(a(r,c), b(r,c))
14141423
!*/
14151424

1425+
const matrix_exp max_pointwise (
1426+
const matrix_exp& a,
1427+
const matrix_exp& b,
1428+
const matrix_exp& c
1429+
);
1430+
/*!
1431+
performs max_pointwise(a,max_pointwise(b,c));
1432+
!*/
1433+
14161434
// ----------------------------------------------------------------------------------------
14171435

14181436
void find_min_and_max (

0 commit comments

Comments
 (0)