Commit c7f27517 authored by Davis King's avatar Davis King

Added is_row_major(). Also made sum() run over the matrix in column major

order if that's the memory layout of the argument.
parent ff6bd2dd
...@@ -1415,6 +1415,16 @@ namespace dlib ...@@ -1415,6 +1415,16 @@ namespace dlib
return typename matrix_exp<EXP>::matrix_type (m); return typename matrix_exp<EXP>::matrix_type (m);
} }
// ----------------------------------------------------------------------------------------
template <typename EXP>
constexpr bool is_row_major (
const matrix_exp<EXP>&
)
{
return is_same_type<typename EXP::layout_type,row_major_layout>::value;
}
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
template < template <
...@@ -1427,11 +1437,24 @@ namespace dlib ...@@ -1427,11 +1437,24 @@ namespace dlib
typedef typename matrix_exp<EXP>::type type; typedef typename matrix_exp<EXP>::type type;
type val = 0; type val = 0;
for (long r = 0; r < m.nr(); ++r) if (is_row_major(m))
{
for (long r = 0; r < m.nr(); ++r)
{
for (long c = 0; c < m.nc(); ++c)
{
val += m(r,c);
}
}
}
else
{ {
for (long c = 0; c < m.nc(); ++c) for (long c = 0; c < m.nc(); ++c)
{ {
val += m(r,c); for (long r = 0; r < m.nr(); ++r)
{
val += m(r,c);
}
} }
} }
return val; return val;
......
...@@ -16,6 +16,17 @@ namespace dlib ...@@ -16,6 +16,17 @@ namespace dlib
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
// Simple matrix utilities // Simple matrix utilities
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
template <typename EXP>
constexpr bool is_row_major (
const matrix_exp<EXP>&
);
/*!
ensures
- returns true if and only if the given matrix expression uses the row_major_layout.
!*/
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
const matrix_exp diag ( const matrix_exp diag (
......
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