diff --git a/dlib/matrix/matrix_mat.h b/dlib/matrix/matrix_mat.h index 803d7d999..e83a41515 100644 --- a/dlib/matrix/matrix_mat.h +++ b/dlib/matrix/matrix_mat.h @@ -448,6 +448,32 @@ namespace dlib return matrix_op(op(ptr,nr,nc,stride)); } + template < + typename T, + long NR, + long NC, + typename MM, + typename L + > + typename enable_if>::type matrix_assign ( + matrix& dest, + const matrix_exp>>& src + ) + /*! + An overload to catch statements of the form: + some_matrix = mat(ptr,rows,cols) + and convert them into a memcpy(), which is a faster way to do the copy. + !*/ + { + // If the op_pointer_to_mat is referring to a contiguous block of memory then just memcopy + // it. + if (dest.size() != 0 && src.ref().op.stride == dest.nc()) { + std::memcpy(&dest(0, 0), src.ref().op.ptr, dest.nr() * dest.nc() * sizeof(T)); + } else { + matrix_assign_default(dest, src); + } + } + // ---------------------------------------------------------------------------------------- } diff --git a/dlib/test/matrix.cpp b/dlib/test/matrix.cpp index 2204056ef..50ebcd531 100644 --- a/dlib/test/matrix.cpp +++ b/dlib/test/matrix.cpp @@ -1420,6 +1420,17 @@ namespace DLIB_TEST(mm(3) == 4); } + { + const long n = 5; + matrix m1, m2; + m1 = randm(n,n); + m2 = randm(n,n); + + m2 = mat(&m1(0,0),n,n); + + DLIB_TEST(m1 == m2); + } + { const long n = 5; matrix m1, m2, m3, truth;