Commit e3307db4 authored by Davis King's avatar Davis King

Added some more convenient overloads of the zeros_matrix(), ones_matrix(),

and identity_matrix() functions.
parent 17565cfb
......@@ -1882,6 +1882,26 @@ namespace dlib
return matrix_op<op>(op(nr, nc, 0));
}
// ----------------------------------------------------------------------------------------
template <
typename EXP
>
const matrix_op<op_uniform_matrix_3<typename EXP::type> > zeros_matrix (
const matrix_exp<EXP>& mat
)
{
DLIB_ASSERT(mat.nr() > 0 && mat.nc() > 0,
"\tconst matrix_exp zeros_matrix(mat)"
<< "\n\t nr and nc have to be bigger than 0"
<< "\n\t mat.nr(): " << mat.nr()
<< "\n\t mat.nc(): " << mat.nc()
);
typedef typename EXP::type T;
typedef op_uniform_matrix_3<T> op;
return matrix_op<op>(op(mat.nr(), mat.nc(), 0));
}
// ----------------------------------------------------------------------------------------
template <
......@@ -1902,6 +1922,26 @@ namespace dlib
return matrix_op<op>(op(nr, nc, 1));
}
// ----------------------------------------------------------------------------------------
template <
typename EXP
>
const matrix_op<op_uniform_matrix_3<typename EXP::type> > ones_matrix (
const matrix_exp<EXP>& mat
)
{
DLIB_ASSERT(mat.nr() > 0 && mat.nc() > 0,
"\tconst matrix_exp ones_matrix(mat)"
<< "\n\t nr and nc have to be bigger than 0"
<< "\n\t mat.nr(): " << mat.nr()
<< "\n\t mat.nc(): " << mat.nc()
);
typedef typename EXP::type T;
typedef op_uniform_matrix_3<T> op;
return matrix_op<op>(op(mat.nr(), mat.nc(), 1));
}
// ----------------------------------------------------------------------------------------
template <
......@@ -2051,6 +2091,24 @@ namespace dlib
return matrix_diag_op<op>(op(size));
}
template <
typename EXP
>
const matrix_diag_op<op_identity_matrix_2<typename EXP::type> > identity_matrix (
const matrix_exp<EXP>& mat
)
{
DLIB_ASSERT(mat.nr() == mat.nc(),
"\tconst matrix_exp identity_matrix(mat)"
<< "\n\t mat must be a square matrix."
<< "\n\t mat.nr(): " << mat.nr()
<< "\n\t mat.nc(): " << mat.nc()
);
typedef typename EXP::type T;
typedef op_identity_matrix_2<T> op;
return matrix_diag_op<op>(op(mat.nr()));
}
// ----------------------------------------------------------------------------------------
template <
......
......@@ -245,6 +245,19 @@ namespace dlib
- returns an nr by nc matrix with elements of type T and all set to val.
!*/
// ----------------------------------------------------------------------------------------
const matrix_exp ones_matrix (
const matrix_exp& mat
);
/*!
requires
- mat.nr() > 0 && mat.nc() > 0
ensures
- Let T denote the type of element in mat. Then this function
returns uniform_matrix<T>(mat.nr(), mat.nc(), 1)
!*/
// ----------------------------------------------------------------------------------------
template <
......@@ -261,6 +274,19 @@ namespace dlib
- returns uniform_matrix<T>(nr, nc, 1)
!*/
// ----------------------------------------------------------------------------------------
const matrix_exp zeros_matrix (
const matrix_exp& mat
);
/*!
requires
- mat.nr() > 0 && mat.nc() > 0
ensures
- Let T denote the type of element in mat. Then this function
returns uniform_matrix<T>(mat.nr(), mat.nc(), 0)
!*/
// ----------------------------------------------------------------------------------------
template <
......@@ -277,6 +303,19 @@ namespace dlib
- returns uniform_matrix<T>(nr, nc, 0)
!*/
// ----------------------------------------------------------------------------------------
const matrix_exp identity_matrix (
const matrix_exp& mat
);
/*!
requires
- mat.nr() == mat.nc()
ensures
- returns an identity matrix with the same dimensions as mat and
containing the same type of elements as mat.
!*/
// ----------------------------------------------------------------------------------------
template <
......
......@@ -65,6 +65,8 @@ namespace
DLIB_TEST(mi.nc() == m.nr());
DLIB_TEST((equal(round_zeros(mi*m,0.000001) , identity_matrix<double,5>())));
DLIB_TEST((equal(round_zeros(m*mi,0.000001) , identity_matrix<double,5>())));
DLIB_TEST((equal(round_zeros(mi*m,0.000001) , identity_matrix(m))));
DLIB_TEST((equal(round_zeros(m*mi,0.000001) , identity_matrix(m))));
}
{
matrix<double,5,0,MM> m(5,5);
......@@ -1041,8 +1043,11 @@ namespace
}
{
matrix<double> mat(4,5);
DLIB_TEST((uniform_matrix<double>(4,5,1) == ones_matrix<double>(4,5)));
DLIB_TEST((uniform_matrix<double>(4,5,1) == ones_matrix(mat)));
DLIB_TEST((uniform_matrix<double>(4,5,0) == zeros_matrix<double>(4,5)));
DLIB_TEST((uniform_matrix<double>(4,5,0) == zeros_matrix(mat)));
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)));
......
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