Commit d6d97b15 authored by Davis King's avatar Davis King

Added the zeros_matrix(), ones_matrix() functions. I also changed the pointwise_multiply()

and complex_matrix() functions so that they are a little more convenient when dealing
with complex types.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%403020
parent 96bbf67b
...@@ -416,7 +416,34 @@ DLIB_MATRIX_SIMPLE_STD_FUNCTION(atan,7) ...@@ -416,7 +416,34 @@ DLIB_MATRIX_SIMPLE_STD_FUNCTION(atan,7)
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
struct op_complex_matrix struct op_complex_matrix
{
template <typename EXP>
struct op : has_nondestructive_aliasing, preserves_dimensions<EXP>
{
const static long cost = EXP::cost+1;
typedef std::complex<typename EXP::type> type;
template <typename M>
static type apply ( const M& m, long r, long c)
{
return type(m(r,c));
}
};
};
template <
typename EXP
>
const matrix_unary_exp<EXP,op_complex_matrix> complex_matrix (
const matrix_exp<EXP>& m
)
{
return matrix_unary_exp<EXP,op_complex_matrix>(m.ref());
}
// ----------------------------------------------------------------------------------------
struct op_complex_matrix2
{ {
template <typename EXP1, typename EXP2> template <typename EXP1, typename EXP2>
struct op : has_nondestructive_aliasing, preserves_dimensions<EXP1,EXP2> struct op : has_nondestructive_aliasing, preserves_dimensions<EXP1,EXP2>
...@@ -434,7 +461,7 @@ DLIB_MATRIX_SIMPLE_STD_FUNCTION(atan,7) ...@@ -434,7 +461,7 @@ DLIB_MATRIX_SIMPLE_STD_FUNCTION(atan,7)
typename EXP1, typename EXP1,
typename EXP2 typename EXP2
> >
const matrix_binary_exp<EXP1,EXP2,op_complex_matrix> complex_matrix ( const matrix_binary_exp<EXP1,EXP2,op_complex_matrix2> complex_matrix (
const matrix_exp<EXP1>& real_part, const matrix_exp<EXP1>& real_part,
const matrix_exp<EXP2>& imag_part const matrix_exp<EXP2>& imag_part
) )
...@@ -452,7 +479,7 @@ DLIB_MATRIX_SIMPLE_STD_FUNCTION(atan,7) ...@@ -452,7 +479,7 @@ DLIB_MATRIX_SIMPLE_STD_FUNCTION(atan,7)
<< "\n\timag_part.nr(): " << imag_part.nr() << "\n\timag_part.nr(): " << imag_part.nr()
<< "\n\timag_part.nc(): " << imag_part.nc() << "\n\timag_part.nc(): " << imag_part.nc()
); );
typedef matrix_binary_exp<EXP1,EXP2,op_complex_matrix> exp; typedef matrix_binary_exp<EXP1,EXP2,op_complex_matrix2> exp;
return exp(real_part.ref(),imag_part.ref()); return exp(real_part.ref(),imag_part.ref());
} }
......
...@@ -369,6 +369,20 @@ namespace dlib ...@@ -369,6 +369,20 @@ namespace dlib
R(r,c) == std::real(m(r,c)) R(r,c) == std::real(m(r,c))
!*/ !*/
// ----------------------------------------------------------------------------------------
const matrix_exp complex_matrix (
const matrix_exp& real_part
);
/*!
ensures
- returns a matrix R such that:
- R::type == std::complex<T> where T is whatever type real_part used.
- R has the same dimensions as real_part.
- for all valid r and c:
R(r,c) == std::complex(real_part(r,c), 0)
!*/
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
const matrix_exp complex_matrix ( const matrix_exp complex_matrix (
......
...@@ -1049,7 +1049,7 @@ namespace dlib ...@@ -1049,7 +1049,7 @@ namespace dlib
) )
{ {
DLIB_ASSERT(nr > 0 && nc > 0, DLIB_ASSERT(nr > 0 && nc > 0,
"\tconst matrix_exp uniform_matrix<T>(nr, nc)" "\tconst matrix_exp uniform_matrix<T>(nr, nc, val)"
<< "\n\tnr and nc have to be bigger than 0" << "\n\tnr and nc have to be bigger than 0"
<< "\n\tnr: " << nr << "\n\tnr: " << nr
<< "\n\tnc: " << nc << "\n\tnc: " << nc
...@@ -1058,6 +1058,46 @@ namespace dlib ...@@ -1058,6 +1058,46 @@ namespace dlib
return exp(nr,nc,val); return exp(nr,nc,val);
} }
// ----------------------------------------------------------------------------------------
template <
typename T
>
const dynamic_matrix_scalar_unary_exp<T,op_uniform_matrix_3<T> > zeros_matrix (
long nr,
long nc
)
{
DLIB_ASSERT(nr > 0 && nc > 0,
"\tconst matrix_exp zeros_matrix<T>(nr, nc)"
<< "\n\tnr and nc have to be bigger than 0"
<< "\n\tnr: " << nr
<< "\n\tnc: " << nc
);
typedef dynamic_matrix_scalar_unary_exp<T,op_uniform_matrix_3<T> > exp;
return exp(nr,nc,0);
}
// ----------------------------------------------------------------------------------------
template <
typename T
>
const dynamic_matrix_scalar_unary_exp<T,op_uniform_matrix_3<T> > ones_matrix (
long nr,
long nc
)
{
DLIB_ASSERT(nr > 0 && nc > 0,
"\tconst matrix_exp ones_matrix<T>(nr, nc)"
<< "\n\tnr and nc have to be bigger than 0"
<< "\n\tnr: " << nr
<< "\n\tnc: " << nc
);
typedef dynamic_matrix_scalar_unary_exp<T,op_uniform_matrix_3<T> > exp;
return exp(nr,nc,1);
}
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
template < template <
...@@ -1224,12 +1264,20 @@ namespace dlib ...@@ -1224,12 +1264,20 @@ namespace dlib
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
struct op_pointwise_multiply struct op_pointwise_multiply
{ {
// A template to tell me if two types can be multiplied together in a sensible way. Here
// I'm saying it is ok if they are both the same type or one is the complex version of the other.
template <typename T, typename U> struct compatible { static const bool value = false; typedef T type; };
template <typename T> struct compatible<T,T> { static const bool value = true; typedef T type; };
template <typename T> struct compatible<std::complex<T>,T> { static const bool value = true; typedef std::complex<T> type; };
template <typename T> struct compatible<T,std::complex<T> > { static const bool value = true; typedef std::complex<T> type; };
template <typename EXP1, typename EXP2> template <typename EXP1, typename EXP2>
struct op : public has_nondestructive_aliasing, public preserves_dimensions<EXP1,EXP2> struct op : public has_nondestructive_aliasing, public preserves_dimensions<EXP1,EXP2>
{ {
typedef typename EXP1::type type; typedef typename compatible<typename EXP1::type, typename EXP2::type>::type type;
const static long cost = EXP1::cost + EXP2::cost + 1; const static long cost = EXP1::cost + EXP2::cost + 1;
template <typename M1, typename M2> template <typename M1, typename M2>
...@@ -1247,7 +1295,7 @@ namespace dlib ...@@ -1247,7 +1295,7 @@ namespace dlib
const matrix_exp<EXP2>& b const matrix_exp<EXP2>& b
) )
{ {
COMPILE_TIME_ASSERT((is_same_type<typename EXP1::type,typename EXP2::type>::value == true)); COMPILE_TIME_ASSERT((op_pointwise_multiply::compatible<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::NR == EXP2::NR || EXP1::NR == 0 || EXP2::NR == 0);
COMPILE_TIME_ASSERT(EXP1::NC == EXP2::NC || EXP1::NC == 0 || EXP2::NC == 0); COMPILE_TIME_ASSERT(EXP1::NC == EXP2::NC || EXP1::NC == 0 || EXP2::NC == 0);
DLIB_ASSERT(a.nr() == b.nr() && DLIB_ASSERT(a.nr() == b.nr() &&
......
...@@ -178,6 +178,38 @@ namespace dlib ...@@ -178,6 +178,38 @@ namespace dlib
- returns an nr by nc matrix with elements of type T and all set to val. - returns an nr by nc matrix with elements of type T and all set to val.
!*/ !*/
// ----------------------------------------------------------------------------------------
template <
typename T
>
const matrix_exp ones_matrix (
long nr,
long nc
);
/*!
requires
- nr > 0 && nc > 0
ensures
- returns uniform_matrix<T>(nr, nc, 1)
!*/
// ----------------------------------------------------------------------------------------
template <
typename T
>
const matrix_exp zeros_matrix (
long nr,
long nc
);
/*!
requires
- nr > 0 && nc > 0
ensures
- returns uniform_matrix<T>(nr, nc, 0)
!*/
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
template < template <
...@@ -493,7 +525,9 @@ namespace dlib ...@@ -493,7 +525,9 @@ namespace dlib
requires requires
- a.nr() == b.nr() - a.nr() == b.nr()
- a.nc() == b.nc() - a.nc() == b.nc()
- a and b both contain the same type of element - a and b both contain the same type of element (one or both
can also be of type std::complex so long as the underlying type
in them is the same)
ensures ensures
- returns a matrix R such that: - returns a matrix R such that:
- R::type == the same type that was in a and b. - R::type == the same type that was in a and b.
......
...@@ -1001,6 +1001,20 @@ namespace ...@@ -1001,6 +1001,20 @@ namespace
} }
{
DLIB_TEST((uniform_matrix<double>(4,5,1) == ones_matrix<double>(4,5)));
DLIB_TEST((uniform_matrix<double>(4,5,0) == zeros_matrix<double>(4,5)));
DLIB_TEST((uniform_matrix<float>(4,5,1) == ones_matrix<float>(4,5)));
DLIB_TEST((uniform_matrix<float>(4,5,0) == zeros_matrix<float>(4,5)));
DLIB_TEST((uniform_matrix<complex<double> >(4,5,1) == ones_matrix<complex<double> >(4,5)));
DLIB_TEST((uniform_matrix<complex<double> >(4,5,0) == zeros_matrix<complex<double> >(4,5)));
DLIB_TEST((uniform_matrix<complex<float> >(4,5,1) == ones_matrix<complex<float> >(4,5)));
DLIB_TEST((uniform_matrix<complex<float> >(4,5,0) == zeros_matrix<complex<float> >(4,5)));
DLIB_TEST((complex_matrix(ones_matrix<double>(3,3), zeros_matrix<double>(3,3)) == complex_matrix(ones_matrix<double>(3,3))));
DLIB_TEST((pointwise_multiply(complex_matrix(ones_matrix<double>(3,3)), ones_matrix<double>(3,3)*2) ==
complex_matrix(2*ones_matrix<double>(3,3))));
}
{ {
DLIB_TEST(( uniform_matrix<double>(303,303, 3)*identity_matrix<double>(303) == uniform_matrix<double,303,303>(3) ) ); DLIB_TEST(( uniform_matrix<double>(303,303, 3)*identity_matrix<double>(303) == uniform_matrix<double,303,303>(3) ) );
DLIB_TEST(( uniform_matrix<double,303,303>(3)*identity_matrix<double,303>() == uniform_matrix<double,303,303>(3) )); DLIB_TEST(( uniform_matrix<double,303,303>(3)*identity_matrix<double,303>() == uniform_matrix<double,303,303>(3) ));
......
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