9 #ifndef mrpt_math_container_ops_H
10 #define mrpt_math_container_ops_H
18 #define _USE_MATH_DEFINES
53 template<
class CONTAINER>
59 bool do_normalization =
false,
60 std::vector<double> *out_bin_centers = NULL)
63 std::vector<double> ret(number_bins);
64 std::vector<double> dummy_ret_bins;
68 else H.
getHistogram( out_bin_centers ? *out_bin_centers : dummy_ret_bins, ret );
72 template <
class EIGEN_CONTAINER>
73 void resizeLike(EIGEN_CONTAINER &trg,
const EIGEN_CONTAINER&src) {
77 void resizeLike(std::vector<T> &trg,
const std::vector<T> &src) {
78 trg.resize(src.size());
85 template <
class CONTAINER1,
class CONTAINER2,
typename VALUE>
86 inline void cumsum_tmpl(
const CONTAINER1 &in_data, CONTAINER2 &out_cumsum)
90 const size_t N = in_data.size();
91 for (
size_t i=0;i<N;i++)
92 last = out_cumsum[i] = last + in_data[i];
95 template <
class CONTAINER1,
class CONTAINER2>
96 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); }
100 template<
class CONTAINER>
101 inline CONTAINER
cumsum(
const CONTAINER &in_data)
108 template <
class CONTAINER>
inline typename CONTAINER::Scalar
norm_inf(
const CONTAINER &v) {
return v.norm_inf(); }
109 template <
class CONTAINER>
inline typename CONTAINER::Scalar
norm(
const CONTAINER &v) {
return v.norm(); }
110 template <
class CONTAINER>
inline typename CONTAINER::Scalar
maximum(
const CONTAINER &v) {
return v.maxCoeff(); }
111 template <
class CONTAINER>
inline typename CONTAINER::Scalar
minimum(
const CONTAINER &v) {
return v.minimum(); }
113 template <
typename T>
inline T
maximum(
const std::vector<T> &v)
120 template <
typename T>
inline T
minimum(
const std::vector<T> &v)
133 template <
class CONTAINER,
typename VALUE>
135 return total+v.squaredNorm();
140 template<
size_t N,
class T,
class U>
143 for (
size_t i=0;i<N;i++) res+=
square(v[i]);
148 template <
class CONTAINER1,
class CONTAINER2>
149 inline typename CONTAINER1::Scalar
156 template<
size_t N,
class T,
class U,
class V>
159 for (
size_t i=0;i<N;i++) res+=v1[i]*v2[i];
166 template <
class CONTAINER>
inline typename CONTAINER::Scalar
sum(
const CONTAINER &v) {
return v.sum(); }
169 template <
typename T>
inline T
sum(
const std::vector<T> &v) {
return std::accumulate(v.begin(),v.end(),T(0)); }
173 template <
class CONTAINER,
typename RET>
inline RET
sumRetType(
const CONTAINER &v) {
return v.template sumRetType<RET>(); }
177 template <
class CONTAINER>
178 inline double mean(
const CONTAINER &v)
182 else return sum(v)/
static_cast<double>(v.size());
186 template <
typename T>
190 const size_t N=V.size();
192 for (
size_t i=1;i<N;i++)
200 template <
class Derived>
202 const Eigen::MatrixBase<Derived> &V,
203 typename Eigen::MatrixBase<Derived>::Scalar &curMin,
204 typename Eigen::MatrixBase<Derived>::Scalar &curMax)
206 V.minimum_maximum(curMin,curMax);
211 template <
class CONTAINER1,
class CONTAINER2>
217 if ( (*it1) == (*it2) )
223 template <
class CONTAINER>
224 void adjustRange(CONTAINER &m,
const typename CONTAINER::Scalar minVal,
const typename CONTAINER::Scalar maxVal)
226 if (
size_t(m.size())==0)
return;
227 typename CONTAINER::Scalar curMin,curMax;
229 const typename CONTAINER::Scalar curRan = curMax-curMin;
230 m -= (curMin+minVal);
231 if (curRan!=0) m *= (maxVal-minVal)/curRan;
242 template<
class VECTORLIKE>
247 bool unbiased =
true)
252 out_mean = (v.size()==1) ? *v.begin() : 0;
257 const size_t N = v.size();
262 out_std = std::sqrt(vector_std /
static_cast<double>(N - (unbiased ? 1:0)) );
272 template<
class VECTORLIKE>
273 inline double stddev(
const VECTORLIKE &v,
bool unbiased =
true)
286 template<
class VECTOR_OF_VECTOR,
class VECTORLIKE,
class MATRIXLIKE>
288 const VECTOR_OF_VECTOR &v,
289 VECTORLIKE &out_mean,
293 const size_t N = v.size();
294 ASSERTMSG_(N>0,
"The input vector contains no elements");
295 const double N_inv = 1.0/N;
297 const size_t M = v[0].size();
298 ASSERTMSG_(M>0,
"The input vector contains rows of length 0");
301 out_mean.assign(M,0);
302 for (
size_t i=0;i<N;i++)
303 for (
size_t j=0;j<M;j++)
304 out_mean[j]+=v[i][j];
305 out_mean= out_mean * N_inv;
311 for (
size_t i=0;i<N;i++)
313 for (
size_t j=0;j<M;j++)
314 out_cov.get_unsafe(j,j)+=
square(v[i][j]-out_mean[j]);
316 for (
size_t j=0;j<M;j++)
317 for (
size_t k=j+1;k<M;k++)
318 out_cov.get_unsafe(j,k)+=(v[i][j]-out_mean[j])*(v[i][k]-out_mean[k]);
320 for (
size_t j=0;j<M;j++)
321 for (
size_t k=j+1;k<M;k++)
322 out_cov.get_unsafe(k,j) = out_cov.get_unsafe(j,k);
323 out_cov= out_cov * N_inv;
332 template<
class VECTOR_OF_VECTOR,
class RETURN_MATRIX>
333 inline RETURN_MATRIX
covVector(
const VECTOR_OF_VECTOR &v )
348 template <
class CONT1,
class CONT2>
349 double ncc_vector(
const CONT1 &patch1,
const CONT2 &patch2 )
351 ASSERT_( patch1.size()==patch2.size() )
353 double numerator = 0, sum_a = 0, sum_b = 0, result, a_mean, b_mean;
354 a_mean = patch1.mean();
355 b_mean = patch2.mean();
357 const size_t N = patch1.size();
358 for(
size_t i=0;i<N;++i)
360 numerator += (patch1[i]-a_mean)*(patch2[i]-b_mean);
364 ASSERTMSG_(sum_a*sum_b!=0,
"Divide by zero when normalizing.")
365 result=numerator/std::sqrt(sum_a*sum_b);
This class provides an easy way of computing histograms for unidimensional real valued variables.
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,...
void getHistogram(std::vector< double > &x, std::vector< double > &hits) const
Returns the list of bin centers & hit counts.
void add(const double x)
Add an element to the histogram.
Column vector, like Eigen::MatrixX*, but automatically initialized to zeros since construction.
const Scalar * const_iterator
#define ASSERTMSG_(f, __ERROR_MSG)
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,...
CONTAINER1::Scalar dotProduct(const CONTAINER1 &v1, const CONTAINER1 &v2)
v1*v2: The dot product of two containers (vectors/arrays/matrices)
CONTAINER::Scalar minimum(const CONTAINER &v)
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,...
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...
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...
double stddev(const VECTORLIKE &v, bool unbiased=true)
Computes the standard deviation of a vector.
double mean(const CONTAINER &v)
Computes the mean value of a vector.
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 norm_inf(const CONTAINER &v)
CONTAINER::Scalar norm(const CONTAINER &v)
T squareNorm(const U &v)
Compute the square norm of anything implementing [].
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 ...
void resizeLike(EIGEN_CONTAINER &trg, const EIGEN_CONTAINER &src)
void cumsum_tmpl(const CONTAINER1 &in_data, CONTAINER2 &out_cumsum)
Computes the cumulative sum of all the elements, saving the result in another container.
CONTAINER::Scalar sum(const CONTAINER &v)
Computes the sum of all the elements.
RET sumRetType(const CONTAINER &v)
Computes the sum of all the elements, with a custom return type.
void meanAndStd(const VECTORLIKE &v, double &out_mean, double &out_std, bool unbiased=true)
Computes the standard deviation of a vector.
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 maximum(const CONTAINER &v)
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.
T square(const T x)
Inline function for the square of a number.
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.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.