From c5ff6a7461a63af9174e0ea70d0bc4a7b1b1c7cf Mon Sep 17 00:00:00 2001 From: Davis King Date: Mon, 17 Mar 2014 18:00:44 -0400 Subject: [PATCH] Improved speed of reshape_to_column_vector() --- dlib/matrix/matrix_utilities.h | 45 ++++++++++++++++++++++++++++++++++ dlib/test/matrix3.cpp | 2 ++ 2 files changed, 47 insertions(+) diff --git a/dlib/matrix/matrix_utilities.h b/dlib/matrix/matrix_utilities.h index 107f21bca..3b6108cc2 100644 --- a/dlib/matrix/matrix_utilities.h +++ b/dlib/matrix/matrix_utilities.h @@ -3901,6 +3901,51 @@ namespace dlib return matrix_op(op(m.ref())); } +// ---------------------------------------------------------------------------------------- + + template < + typename T, + long NR_, + long NC_, + typename MM + > + struct op_mat_to_vect2 + { + typedef matrix M; + op_mat_to_vect2(const M& m_) : m(m_) {} + const M& m; + + const static long cost = M::cost+2; + const static long NR = M::NC*M::NR; + const static long NC = 1; + typedef typename M::type type; + typedef typename M::const_ret_type const_ret_type; + typedef typename M::mem_manager_type mem_manager_type; + typedef typename M::layout_type layout_type; + + const_ret_type apply ( long r, long ) const { return (&m(0,0))[r]; } + + long nr () const { return m.size(); } + long nc () const { return 1; } + + template bool aliases ( const matrix_exp& item) const { return m.aliases(item); } + template bool destructively_aliases ( const matrix_exp& item) const { return m.aliases(item); } + }; + + template < + typename T, + long NR, + long NC, + typename MM + > + const matrix_op > reshape_to_column_vector ( + const matrix& m + ) + { + typedef op_mat_to_vect2 op; + return matrix_op(op(m.ref())); + } + // ---------------------------------------------------------------------------------------- template diff --git a/dlib/test/matrix3.cpp b/dlib/test/matrix3.cpp index 15847ab80..46dde12a9 100644 --- a/dlib/test/matrix3.cpp +++ b/dlib/test/matrix3.cpp @@ -610,6 +610,7 @@ namespace m2 = 1,2,3,4,5,6; DLIB_TEST(reshape_to_column_vector(m) == m2); + DLIB_TEST(reshape_to_column_vector(m+m) == m2+m2); } { @@ -622,6 +623,7 @@ namespace m2 = 1,2,3,4,5,6; DLIB_TEST(reshape_to_column_vector(m) == m2); + DLIB_TEST(reshape_to_column_vector(m+m) == m2+m2); } }