9 #ifndef mrpt_math_container_ops_H 10 #define mrpt_math_container_ops_H 18 #define _USE_MATH_DEFINES // (For VS to define M_PI, etc. in cmath) 54 template<
class CONTAINER>
60 bool do_normalization =
false,
61 std::vector<double> *out_bin_centers = NULL)
64 std::vector<double> ret(number_bins);
65 std::vector<double> dummy_ret_bins;
69 else H.
getHistogram( out_bin_centers ? *out_bin_centers : dummy_ret_bins, ret );
73 template <
class EIGEN_CONTAINER>
79 trg.resize(
src.size());
86 template <
class CONTAINER1,
class CONTAINER2,
typename VALUE>
87 inline void cumsum_tmpl(
const CONTAINER1 &in_data, CONTAINER2 &out_cumsum)
91 const size_t N = in_data.size();
92 for (
size_t i=0;i<N;i++)
93 last = out_cumsum[i] = last + in_data[i];
96 template <
class CONTAINER1,
class CONTAINER2>
97 inline void cumsum(
const CONTAINER1 &in_data, CONTAINER2 &out_cumsum) { cumsum_tmpl<CONTAINER1,CONTAINER2,typename mrpt::math::ContainerType<CONTAINER2>::element_t>(in_data,out_cumsum); }
101 template<
class CONTAINER>
102 inline CONTAINER
cumsum(
const CONTAINER &in_data)
114 template <
typename T>
inline T
maximum(
const std::vector<T> &
v)
121 template <
typename T>
inline T
minimum(
const std::vector<T> &
v)
134 template <
class CONTAINER,
typename VALUE>
136 return total+
v.squaredNorm();
141 template<
size_t N,
class T,
class U>
149 template <
class CONTAINER1,
class CONTAINER2>
157 template<
size_t N,
class T,
class U,
class V>
160 for (
size_t i=0;i<N;i++)
res+=
v1[i]*
v2[i];
170 template <
typename T>
inline T
sum(
const std::vector<T> &
v) {
return std::accumulate(
v.begin(),
v.end(),T(0)); }
174 template <
class CONTAINER,
typename RET>
inline RET
sumRetType(
const CONTAINER &
v) {
return v.template sumRetType<RET>(); }
178 template <
class CONTAINER>
179 inline double mean(
const CONTAINER &
v)
183 else return sum(
v)/
static_cast<double>(
v.size());
187 template <
typename T>
191 const size_t N=V.size();
193 for (
size_t i=1;i<N;i++)
201 template <
class Derived>
203 const Eigen::MatrixBase<Derived> &V,
207 V.minimum_maximum(curMin,curMax);
212 template <
class CONTAINER1,
class CONTAINER2>
218 if ( (*it1) == (*it2) )
224 template <
class CONTAINER>
227 if (
size_t(m.size())==0)
return;
231 m -= (curMin+minVal);
232 if (curRan!=0) m *= (maxVal-minVal)/curRan;
243 template<
class VECTORLIKE>
248 bool unbiased =
true)
253 out_mean = (
v.size()==1) ? *
v.begin() : 0;
258 const size_t N =
v.size();
263 out_std = std::sqrt(vector_std / static_cast<double>(N - (unbiased ? 1:0)) );
273 template<
class VECTORLIKE>
274 inline double stddev(
const VECTORLIKE &
v,
bool unbiased =
true)
287 template<
class VECTOR_OF_VECTOR,
class VECTORLIKE,
class MATRIXLIKE>
289 const VECTOR_OF_VECTOR &
v,
290 VECTORLIKE &out_mean,
294 const size_t N =
v.size();
295 ASSERTMSG_(N>0,
"The input vector contains no elements");
296 const double N_inv = 1.0/N;
298 const size_t M =
v[0].size();
299 ASSERTMSG_(M>0,
"The input vector contains rows of length 0");
302 out_mean.assign(M,0);
303 for (
size_t i=0;i<N;i++)
304 for (
size_t j=0;j<M;j++)
305 out_mean[j]+=
v[i][j];
312 for (
size_t i=0;i<N;i++)
314 for (
size_t j=0;j<M;j++)
315 out_cov.get_unsafe(j,j)+=
square(
v[i][j]-out_mean[j]);
317 for (
size_t j=0;j<M;j++)
318 for (
size_t k=j+1;k<M;k++)
319 out_cov.get_unsafe(j,k)+=(
v[i][j]-out_mean[j])*(
v[i][k]-out_mean[k]);
321 for (
size_t j=0;j<M;j++)
322 for (
size_t k=j+1;k<M;k++)
323 out_cov.get_unsafe(k,j) = out_cov.get_unsafe(j,k);
333 template<
class VECTOR_OF_VECTOR,
class RETURN_MATRIX>
336 std::vector<double> m;
349 template <
class CONT1,
class CONT2>
350 double ncc_vector(
const CONT1 &patch1,
const CONT2 &patch2 )
352 ASSERT_( patch1.size()==patch2.size() )
354 double numerator = 0, sum_a = 0, sum_b = 0, result, a_mean, b_mean;
355 a_mean = patch1.mean();
356 b_mean = patch2.mean();
358 const size_t N = patch1.size();
359 for(
size_t i=0;i<N;++i)
361 numerator += (patch1[i]-a_mean)*(patch2[i]-b_mean);
365 ASSERTMSG_(sum_a*sum_b!=0,
"Divide by zero when normalizing.")
366 result=numerator/std::sqrt(sum_a*sum_b);
void cumsum_tmpl(const CONTAINER1 &in_data, CONTAINER2 &out_cumsum)
Computes the cumulative sum of all the elements, saving the result in another container.
This class provides an easy way of computing histograms for unidimensional real valued variables...
size_t countCommonElements(const CONTAINER1 &a, const CONTAINER2 &b)
Counts the number of elements that appear in both STL-like containers (comparison through the == oper...
double stddev(const VECTORLIKE &v, bool unbiased=true)
Computes the standard deviation of a vector.
std::vector< double > histogram(const CONTAINER &v, double limit_min, double limit_max, size_t number_bins, bool do_normalization=false, std::vector< double > *out_bin_centers=NULL)
Computes the normalized or normal histogram of a sequence of numbers given the number of bins and the...
T squareNorm(const U &v)
Compute the square norm of anything implementing [].
const Scalar * const_iterator
void resizeLike(EIGEN_CONTAINER &trg, const EIGEN_CONTAINER &src)
void keep_min(T &var, const K test_val)
If the second argument is below the first one, set the first argument to this lower value...
T square(const T x)
Inline function for the square of a number.
CONTAINER::Scalar minimum(const CONTAINER &v)
void add(const double x)
Add an element to the histogram.
CONTAINER::Scalar sum(const CONTAINER &v)
Computes the sum of all the elements.
VALUE squareNorm_accum(const VALUE total, const CONTAINER &v)
Accumulate the squared-norm of a vector/array/matrix into "total" (this function is compatible with s...
CONTAINER::Scalar maximum(const CONTAINER &v)
void getHistogram(std::vector< double > &x, std::vector< double > &hits) const
Returns the list of bin centers & hit counts.
void minimum_maximum(const std::vector< T > &V, T &curMin, T &curMax)
Return the maximum and minimum values of a std::vector.
void cumsum(const CONTAINER1 &in_data, CONTAINER2 &out_cumsum)
CONTAINER::Scalar norm_inf(const CONTAINER &v)
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void meanAndStd(const VECTORLIKE &v, double &out_mean, double &out_std, bool unbiased=true)
Computes the standard deviation of a vector.
double ncc_vector(const CONT1 &patch1, const CONT2 &patch2)
Normalised Cross Correlation between two vector patches The Matlab code for this is a = a - mean2(a);...
RETURN_MATRIX covVector(const VECTOR_OF_VECTOR &v)
Computes the covariance matrix from a list of values given as a vector of vectors, where each row is a sample.
RET sumRetType(const CONTAINER &v)
Computes the sum of all the elements, with a custom return type.
CONTAINER1::Scalar dotProduct(const CONTAINER1 &v1, const CONTAINER1 &v2)
v1*v2: The dot product of two containers (vectors/arrays/matrices)
double mean(const CONTAINER &v)
Computes the mean value of a vector.
void meanAndCovVec(const VECTOR_OF_VECTOR &v, VECTORLIKE &out_mean, MATRIXLIKE &out_cov)
Computes the mean vector and covariance from a list of values given as a vector of vectors...
GLfloat GLfloat GLfloat v2
void adjustRange(CONTAINER &m, const typename CONTAINER::Scalar minVal, const typename CONTAINER::Scalar maxVal)
Adjusts the range of all the elements such as the minimum and maximum values being those supplied by ...
#define ASSERTMSG_(f, __ERROR_MSG)
GLubyte GLubyte GLubyte a
void keep_max(T &var, const K test_val)
If the second argument is above the first one, set the first argument to this higher value...
void getHistogramNormalized(std::vector< double > &x, std::vector< double > &hits) const
Returns the list of bin centers & hit counts, normalized such as the integral of the histogram...
CONTAINER::Scalar norm(const CONTAINER &v)