Commit 197b36eb authored by Davis King's avatar Davis King

Removed a bug in the sum() and variance() functions that

caused them to seg fault when they were used on certain
matrix of matrix objects.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%402405
parent 184f0ce3
...@@ -2813,7 +2813,10 @@ namespace dlib ...@@ -2813,7 +2813,10 @@ namespace dlib
typedef typename matrix_exp<EXP>::type type; typedef typename matrix_exp<EXP>::type type;
type val; type val;
if (m.size() > 0)
val.set_size(m(0,0).nr(),m(0,0).nc());
set_all_elements(val,0); set_all_elements(val,0);
for (long r = 0; r < m.nr(); ++r) for (long r = 0; r < m.nr(); ++r)
{ {
for (long c = 0; c < m.nc(); ++c) for (long c = 0; c < m.nc(); ++c)
...@@ -2876,6 +2879,9 @@ namespace dlib ...@@ -2876,6 +2879,9 @@ namespace dlib
typedef typename matrix_exp<EXP>::type type; typedef typename matrix_exp<EXP>::type type;
type val; type val;
if (m.size() > 0)
val.set_size(m(0,0).nr(), m(0,0).nc());
set_all_elements(val,0); set_all_elements(val,0);
for (long r = 0; r < m.nr(); ++r) for (long r = 0; r < m.nr(); ++r)
{ {
...@@ -2885,7 +2891,7 @@ namespace dlib ...@@ -2885,7 +2891,7 @@ namespace dlib
} }
} }
if (m.nr() * m.nc() == 1) if (m.nr() * m.nc() <= 1)
return val; return val;
else else
return val/(m.nr()*m.nc() - 1); return val/(m.nr()*m.nc() - 1);
......
...@@ -807,6 +807,14 @@ namespace ...@@ -807,6 +807,14 @@ namespace
m(0,0) = uniform_matrix<double,3,1>(9); m(0,0) = uniform_matrix<double,3,1>(9);
DLIB_CASSERT((round_zeros(variance(m)) == uniform_matrix<double,3,1>(16)),""); DLIB_CASSERT((round_zeros(variance(m)) == uniform_matrix<double,3,1>(16)),"");
DLIB_CASSERT((round_zeros(mean(m)) == uniform_matrix<double,3,1>(3)),""); DLIB_CASSERT((round_zeros(mean(m)) == uniform_matrix<double,3,1>(3)),"");
matrix<matrix<double> > m2(2,2);
set_all_elements(m2,uniform_matrix<double,3,1>(1));
DLIB_CASSERT((round_zeros(variance(m2)) == uniform_matrix<double,3,1>(0)),"");
DLIB_CASSERT((round_zeros(mean(m2)) == uniform_matrix<double,3,1>(1)),"");
m2(0,0) = uniform_matrix<double,3,1>(9);
DLIB_CASSERT((round_zeros(variance(m2)) == uniform_matrix<double,3,1>(16)),"");
DLIB_CASSERT((round_zeros(mean(m2)) == uniform_matrix<double,3,1>(3)),"");
} }
{ {
......
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