Commit f07b00ce authored by Davis King's avatar Davis King

Added the sum_rows() and sum_cols() functions.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%403587
parent 7d85f183
...@@ -1417,6 +1417,96 @@ namespace dlib ...@@ -1417,6 +1417,96 @@ namespace dlib
return val; return val;
} }
// ----------------------------------------------------------------------------------------
struct op_sumr
{
template <typename EXP>
struct op : has_destructive_aliasing
{
const static long cost = EXP::cost;
const static long NR = 1;
const static long NC = EXP::NC;
typedef typename EXP::type type;
typedef const typename EXP::type const_ret_type;
typedef typename EXP::mem_manager_type mem_manager_type;
template <typename M>
static const_ret_type apply ( const M& m, long , long c)
{
type temp = m(0,c);
for (long r = 1; r < m.nr(); ++r)
temp += m(r,c);
return temp;
}
template <typename M>
static long nr (const M& ) { return 1; }
template <typename M>
static long nc (const M& m) { return m.nc(); }
};
};
template <
typename EXP
>
const matrix_unary_exp<EXP,op_sumr> sum_rows (
const matrix_exp<EXP>& m
)
{
DLIB_ASSERT(m.size() > 0 ,
"\tconst matrix_exp sum_rows(m)"
<< "\n\t The matrix can't be empty"
<< "\n\t m.size(): " << m.size()
);
typedef matrix_unary_exp<EXP,op_sumr> exp;
return exp(m.ref());
}
// ----------------------------------------------------------------------------------------
struct op_sumc
{
template <typename EXP>
struct op : has_destructive_aliasing
{
const static long cost = EXP::cost;
const static long NR = EXP::NR;
const static long NC = 1;
typedef typename EXP::type type;
typedef const typename EXP::type const_ret_type;
typedef typename EXP::mem_manager_type mem_manager_type;
template <typename M>
static const_ret_type apply ( const M& m, long r, long )
{
type temp = m(r,0);
for (long c = 1; c < m.nc(); ++c)
temp += m(r,c);
return temp;
}
template <typename M>
static long nr (const M& m) { return m.nr(); }
template <typename M>
static long nc (const M& ) { return 1; }
};
};
template <
typename EXP
>
const matrix_unary_exp<EXP,op_sumc> sum_cols (
const matrix_exp<EXP>& m
)
{
DLIB_ASSERT(m.size() > 0 ,
"\tconst matrix_exp sum_cols(m)"
<< "\n\t The matrix can't be empty"
<< "\n\t m.size(): " << m.size()
);
typedef matrix_unary_exp<EXP,op_sumc> exp;
return exp(m.ref());
}
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
template < template <
......
...@@ -1267,6 +1267,42 @@ namespace dlib ...@@ -1267,6 +1267,42 @@ namespace dlib
- returns the sum of all elements in m - returns the sum of all elements in m
!*/ !*/
// ----------------------------------------------------------------------------------------
const matrix_exp sum_rows (
const matrix_exp& m
);
/*!
requires
- m.size() > 0
ensures
- returns a row matrix that contains the sum of all the rows in m.
- returns a matrix M such that
- M::type == the same type that was in m
- M.nr() == 1
- M.nc() == m.nc()
- for all valid i:
- M(i) == sum(colm(m,i))
!*/
// ----------------------------------------------------------------------------------------
const matrix_exp sum_cols (
const matrix_exp& m
);
/*!
requires
- m.size() > 0
ensures
- returns a column matrix that contains the sum of all the columns in m.
- returns a matrix M such that
- M::type == the same type that was in m
- M.nr() == m.nr()
- M.nc() == 1
- for all valid i:
- M(i) == sum(rowm(m,i))
!*/
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
const matrix_exp::type prod ( const matrix_exp::type prod (
......
...@@ -938,6 +938,50 @@ namespace ...@@ -938,6 +938,50 @@ namespace
DLIB_TEST(pointer_to_matrix(&v[0], 2, 3) == a); DLIB_TEST(pointer_to_matrix(&v[0], 2, 3) == a);
} }
{
matrix<int> a(3,4);
matrix<int> b(3,1), c(1,4);
a = 1, 2, 3, 6,
4, 5, 6, 9,
1, 1, 1, 3;
b(0) = sum(rowm(a,0));
b(1) = sum(rowm(a,1));
b(2) = sum(rowm(a,2));
c(0) = sum(colm(a,0));
c(1) = sum(colm(a,1));
c(2) = sum(colm(a,2));
c(3) = sum(colm(a,3));
DLIB_TEST(sum_cols(a) == b);
DLIB_TEST(sum_rows(a) == c);
}
{
matrix<int,3,4> a;
matrix<int> b(3,1), c(1,4);
a = 1, 2, 3, 6,
4, 5, 6, 9,
1, 1, 1, 3;
b(0) = sum(rowm(a,0));
b(1) = sum(rowm(a,1));
b(2) = sum(rowm(a,2));
c(0) = sum(colm(a,0));
c(1) = sum(colm(a,1));
c(2) = sum(colm(a,2));
c(3) = sum(colm(a,3));
DLIB_TEST(sum_cols(a) == b);
DLIB_TEST(sum_rows(a) == c);
}
} }
......
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