Commit 1b992baa authored by Davis King's avatar Davis King

Added some MATLAB style thresholding relational operators.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%403288
parent 4f8ff2c3
...@@ -938,6 +938,322 @@ namespace dlib ...@@ -938,6 +938,322 @@ namespace dlib
return exp(m.ref()); return exp(m.ref());
} }
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
struct op_lessthan
{
template <typename EXP>
struct op : has_nondestructive_aliasing, preserves_dimensions<EXP>
{
const static long cost = EXP::cost+1;
typedef typename EXP::type type;
template <typename M, typename S>
static type apply ( const M& m, const S& s, long r, long c)
{
if (m(r,c) < s)
return 1;
else
return 0;
}
};
};
template <
typename EXP,
typename S
>
const typename enable_if<is_built_in_scalar_type<S>,matrix_scalar_binary_exp<EXP,S,op_lessthan> >::type operator< (
const matrix_exp<EXP>& m,
const S& s
)
{
// you can only use this relational operator with the built in scalar types like
// long, float, etc...
COMPILE_TIME_ASSERT(is_built_in_scalar_type<typename EXP::type>::value);
return matrix_scalar_binary_exp<EXP,S, op_lessthan>(m.ref(),s);
}
template <
typename EXP,
typename S
>
const typename enable_if<is_built_in_scalar_type<S>,matrix_scalar_binary_exp<EXP,S,op_lessthan> >::type operator> (
const S& s,
const matrix_exp<EXP>& m
)
{
// you can only use this relational operator with the built in scalar types like
// long, float, etc...
COMPILE_TIME_ASSERT(is_built_in_scalar_type<typename EXP::type>::value);
return matrix_scalar_binary_exp<EXP,S, op_lessthan>(m.ref(),s);
}
// ----------------------------------------------------------------------------------------
struct op_lessthan_eq
{
template <typename EXP>
struct op : has_nondestructive_aliasing, preserves_dimensions<EXP>
{
const static long cost = EXP::cost+1;
typedef typename EXP::type type;
template <typename M, typename S>
static type apply ( const M& m, const S& s, long r, long c)
{
if (m(r,c) <= s)
return 1;
else
return 0;
}
};
};
template <
typename EXP,
typename S
>
const typename enable_if<is_built_in_scalar_type<S>,matrix_scalar_binary_exp<EXP,S,op_lessthan_eq> >::type operator<= (
const matrix_exp<EXP>& m,
const S& s
)
{
// you can only use this relational operator with the built in scalar types like
// long, float, etc...
COMPILE_TIME_ASSERT( is_built_in_scalar_type<typename EXP::type>::value);
return matrix_scalar_binary_exp<EXP,S, op_lessthan_eq>(m.ref(),s);
}
template <
typename EXP,
typename S
>
const typename enable_if<is_built_in_scalar_type<S>,matrix_scalar_binary_exp<EXP,S,op_lessthan_eq> >::type operator>= (
const S& s,
const matrix_exp<EXP>& m
)
{
// you can only use this relational operator with the built in scalar types like
// long, float, etc...
COMPILE_TIME_ASSERT( is_built_in_scalar_type<typename EXP::type>::value);
return matrix_scalar_binary_exp<EXP,S, op_lessthan_eq>(m.ref(),s);
}
// ----------------------------------------------------------------------------------------
struct op_greaterthan
{
template <typename EXP>
struct op : has_nondestructive_aliasing, preserves_dimensions<EXP>
{
const static long cost = EXP::cost+1;
typedef typename EXP::type type;
template <typename M, typename S>
static type apply ( const M& m, const S& s, long r, long c)
{
if (m(r,c) > s)
return 1;
else
return 0;
}
};
};
template <
typename EXP,
typename S
>
const typename enable_if<is_built_in_scalar_type<S>,matrix_scalar_binary_exp<EXP,S,op_greaterthan> >::type operator> (
const matrix_exp<EXP>& m,
const S& s
)
{
// you can only use this relational operator with the built in scalar types like
// long, float, etc...
COMPILE_TIME_ASSERT(is_built_in_scalar_type<typename EXP::type>::value);
return matrix_scalar_binary_exp<EXP,S, op_greaterthan>(m.ref(),s);
}
template <
typename EXP,
typename S
>
const typename enable_if<is_built_in_scalar_type<S>,matrix_scalar_binary_exp<EXP,S,op_greaterthan> >::type operator< (
const S& s,
const matrix_exp<EXP>& m
)
{
// you can only use this relational operator with the built in scalar types like
// long, float, etc...
COMPILE_TIME_ASSERT(is_built_in_scalar_type<typename EXP::type>::value);
return matrix_scalar_binary_exp<EXP,S, op_greaterthan>(m.ref(),s);
}
// ----------------------------------------------------------------------------------------
struct op_greaterthan_eq
{
template <typename EXP>
struct op : has_nondestructive_aliasing, preserves_dimensions<EXP>
{
const static long cost = EXP::cost+1;
typedef typename EXP::type type;
template <typename M, typename S>
static type apply ( const M& m, const S& s, long r, long c)
{
if (m(r,c) >= s)
return 1;
else
return 0;
}
};
};
template <
typename EXP,
typename S
>
const typename enable_if<is_built_in_scalar_type<S>,matrix_scalar_binary_exp<EXP,S,op_greaterthan_eq> >::type operator>= (
const matrix_exp<EXP>& m,
const S& s
)
{
// you can only use this relational operator with the built in scalar types like
// long, float, etc...
COMPILE_TIME_ASSERT( is_built_in_scalar_type<typename EXP::type>::value);
return matrix_scalar_binary_exp<EXP,S, op_greaterthan_eq>(m.ref(),s);
}
template <
typename EXP,
typename S
>
const typename enable_if<is_built_in_scalar_type<S>,matrix_scalar_binary_exp<EXP,S,op_greaterthan_eq> >::type operator<= (
const S& s,
const matrix_exp<EXP>& m
)
{
// you can only use this relational operator with the built in scalar types like
// long, float, etc...
COMPILE_TIME_ASSERT( is_built_in_scalar_type<typename EXP::type>::value);
return matrix_scalar_binary_exp<EXP,S, op_greaterthan_eq>(m.ref(),s);
}
// ----------------------------------------------------------------------------------------
struct op_equal_to
{
template <typename EXP>
struct op : has_nondestructive_aliasing, preserves_dimensions<EXP>
{
const static long cost = EXP::cost+1;
typedef typename EXP::type type;
template <typename M, typename S>
static type apply ( const M& m, const S& s, long r, long c)
{
if (m(r,c) == s)
return 1;
else
return 0;
}
};
};
template <
typename EXP,
typename S
>
const typename enable_if<is_built_in_scalar_type<S>,matrix_scalar_binary_exp<EXP,S,op_equal_to> >::type operator== (
const matrix_exp<EXP>& m,
const S& s
)
{
// you can only use this relational operator with the built in scalar types like
// long, float, etc...
COMPILE_TIME_ASSERT( is_built_in_scalar_type<typename EXP::type>::value);
return matrix_scalar_binary_exp<EXP,S, op_equal_to>(m.ref(),s);
}
template <
typename EXP,
typename S
>
const typename enable_if<is_built_in_scalar_type<S>,matrix_scalar_binary_exp<EXP,S,op_equal_to> >::type operator== (
const S& s,
const matrix_exp<EXP>& m
)
{
// you can only use this relational operator with the built in scalar types like
// long, float, etc...
COMPILE_TIME_ASSERT( is_built_in_scalar_type<typename EXP::type>::value);
return matrix_scalar_binary_exp<EXP,S, op_equal_to>(m.ref(),s);
}
// ----------------------------------------------------------------------------------------
struct op_not_equal_to
{
template <typename EXP>
struct op : has_nondestructive_aliasing, preserves_dimensions<EXP>
{
const static long cost = EXP::cost+1;
typedef typename EXP::type type;
template <typename M, typename S>
static type apply ( const M& m, const S& s, long r, long c)
{
if (m(r,c) != s)
return 1;
else
return 0;
}
};
};
template <
typename EXP,
typename S
>
const typename enable_if<is_built_in_scalar_type<S>,matrix_scalar_binary_exp<EXP,S,op_not_equal_to> >::type operator!= (
const matrix_exp<EXP>& m,
const S& s
)
{
// you can only use this relational operator with the built in scalar types like
// long, float, etc...
COMPILE_TIME_ASSERT(is_built_in_scalar_type<typename EXP::type>::value);
return matrix_scalar_binary_exp<EXP,S, op_not_equal_to>(m.ref(),s);
}
template <
typename EXP,
typename S
>
const typename enable_if<is_built_in_scalar_type<S>,matrix_scalar_binary_exp<EXP,S,op_not_equal_to> >::type operator!= (
const S& s,
const matrix_exp<EXP>& m
)
{
// you can only use this relational operator with the built in scalar types like
// long, float, etc...
COMPILE_TIME_ASSERT(is_built_in_scalar_type<typename EXP::type>::value);
return matrix_scalar_binary_exp<EXP,S, op_not_equal_to>(m.ref(),s);
}
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
template < template <
......
...@@ -804,6 +804,286 @@ namespace dlib ...@@ -804,6 +804,286 @@ namespace dlib
- returns false - returns false
!*/ !*/
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// Thresholding relational operators
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
template <typename S>
const matrix_exp operator< (
const matrix_exp& m,
const S& s
);
/*!
requires
- is_built_in_scalar_type<S>::value == true
- is_built_in_scalar_type<matrix_exp::type>::value == true
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) < s) then
- R(r,c) == 1
- else
- R(r,c) == 0
- i.e. R is a binary matrix of all 1s or 0s.
!*/
// ----------------------------------------------------------------------------------------
template <typename S>
const matrix_exp operator< (
const S& s,
const matrix_exp& m
);
/*!
requires
- is_built_in_scalar_type<S>::value == true
- is_built_in_scalar_type<matrix_exp::type>::value == true
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 (s < m(r,c)) then
- R(r,c) == 1
- else
- R(r,c) == 0
- i.e. R is a binary matrix of all 1s or 0s.
!*/
// ----------------------------------------------------------------------------------------
template <typename S>
const matrix_exp operator<= (
const matrix_exp& m,
const S& s
);
/*!
requires
- is_built_in_scalar_type<S>::value == true
- is_built_in_scalar_type<matrix_exp::type>::value == true
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) <= s) then
- R(r,c) == 1
- else
- R(r,c) == 0
- i.e. R is a binary matrix of all 1s or 0s.
!*/
// ----------------------------------------------------------------------------------------
template <typename S>
const matrix_exp operator<= (
const S& s,
const matrix_exp& m
);
/*!
requires
- is_built_in_scalar_type<S>::value == true
- is_built_in_scalar_type<matrix_exp::type>::value == true
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 (s <= m(r,c)) then
- R(r,c) == 1
- else
- R(r,c) == 0
- i.e. R is a binary matrix of all 1s or 0s.
!*/
// ----------------------------------------------------------------------------------------
template <typename S>
const matrix_exp operator> (
const matrix_exp& m,
const S& s
);
/*!
requires
- is_built_in_scalar_type<S>::value == true
- is_built_in_scalar_type<matrix_exp::type>::value == true
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) > s) then
- R(r,c) == 1
- else
- R(r,c) == 0
- i.e. R is a binary matrix of all 1s or 0s.
!*/
// ----------------------------------------------------------------------------------------
template <typename S>
const matrix_exp operator> (
const S& s,
const matrix_exp& m
);
/*!
requires
- is_built_in_scalar_type<S>::value == true
- is_built_in_scalar_type<matrix_exp::type>::value == true
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 (s > m(r,c)) then
- R(r,c) == 1
- else
- R(r,c) == 0
- i.e. R is a binary matrix of all 1s or 0s.
!*/
// ----------------------------------------------------------------------------------------
template <typename S>
const matrix_exp operator>= (
const matrix_exp& m,
const S& s
);
/*!
requires
- is_built_in_scalar_type<S>::value == true
- is_built_in_scalar_type<matrix_exp::type>::value == true
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) >= s) then
- R(r,c) == 1
- else
- R(r,c) == 0
- i.e. R is a binary matrix of all 1s or 0s.
!*/
// ----------------------------------------------------------------------------------------
template <typename S>
const matrix_exp operator>= (
const S& s,
const matrix_exp& m
);
/*!
requires
- is_built_in_scalar_type<S>::value == true
- is_built_in_scalar_type<matrix_exp::type>::value == true
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 (s >= m(r,c)) then
- R(r,c) == 1
- else
- R(r,c) == 0
- i.e. R is a binary matrix of all 1s or 0s.
!*/
// ----------------------------------------------------------------------------------------
template <typename S>
const matrix_exp operator== (
const matrix_exp& m,
const S& s
);
/*!
requires
- is_built_in_scalar_type<S>::value == true
- is_built_in_scalar_type<matrix_exp::type>::value == true
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) == s) then
- R(r,c) == 1
- else
- R(r,c) == 0
- i.e. R is a binary matrix of all 1s or 0s.
!*/
// ----------------------------------------------------------------------------------------
template <typename S>
const matrix_exp operator== (
const S& s,
const matrix_exp& m
);
/*!
requires
- is_built_in_scalar_type<S>::value == true
- is_built_in_scalar_type<matrix_exp::type>::value == true
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 (s == m(r,c)) then
- R(r,c) == 1
- else
- R(r,c) == 0
- i.e. R is a binary matrix of all 1s or 0s.
!*/
// ----------------------------------------------------------------------------------------
template <typename S>
const matrix_exp operator!= (
const matrix_exp& m,
const S& s
);
/*!
requires
- is_built_in_scalar_type<S>::value == true
- is_built_in_scalar_type<matrix_exp::type>::value == true
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) != s) then
- R(r,c) == 1
- else
- R(r,c) == 0
- i.e. R is a binary matrix of all 1s or 0s.
!*/
// ----------------------------------------------------------------------------------------
template <typename S>
const matrix_exp operator!= (
const S& s,
const matrix_exp& m
);
/*!
requires
- is_built_in_scalar_type<S>::value == true
- is_built_in_scalar_type<matrix_exp::type>::value == true
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 (s != m(r,c)) then
- R(r,c) == 1
- else
- R(r,c) == 0
- i.e. R is a binary matrix of all 1s or 0s.
!*/
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
// Statistics // Statistics
......
...@@ -698,6 +698,47 @@ namespace ...@@ -698,6 +698,47 @@ namespace
DLIB_TEST(pointer_to_column_vector(&v2[0], v.size()) != vector_to_matrix(v)); DLIB_TEST(pointer_to_column_vector(&v2[0], v.size()) != vector_to_matrix(v));
} }
{
matrix<double> a(3,3), b(3,3);
a = 1, 2.5, 1,
3, 4, 5,
0.5, 2.2, 3;
b = 0, 1, 0,
1, 1, 1,
0, 1, 1;
DLIB_TEST((a>1) == b);
DLIB_TEST((1<a) == b);
b = 1, 1, 1,
1, 1, 1,
0, 1, 1;
DLIB_TEST((a>=1) == b);
DLIB_TEST((1<=a) == b);
b = 0, 0, 0,
0, 0, 0,
0, 1, 0;
DLIB_TEST((a==2.2) == b);
DLIB_TEST((a!=2.2) == (b==0));
DLIB_TEST((2.2==a) == b);
DLIB_TEST((2.2!=a) == (0==b));
b = 0, 0, 0,
0, 0, 0,
1, 0, 0;
DLIB_TEST((a<1) == b);
DLIB_TEST((1>a) == b);
b = 1, 0, 1,
0, 0, 0,
1, 0, 0;
DLIB_TEST((a<=1) == b);
DLIB_TEST((1>=a) == b);
}
} }
......
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