9 #ifndef mrpt_math_container_ops_H 10 #define mrpt_math_container_ops_H 64 template <
class CONTAINER>
66 const CONTAINER&
v,
double limit_min,
double limit_max,
size_t number_bins,
67 bool do_normalization =
false,
68 std::vector<double>* out_bin_centers =
nullptr)
71 std::vector<double> ret(number_bins);
72 std::vector<double> dummy_ret_bins;
76 out_bin_centers ? *out_bin_centers : dummy_ret_bins, ret);
79 out_bin_centers ? *out_bin_centers : dummy_ret_bins, ret);
83 template <
class EIGEN_CONTAINER>
91 trg.resize(
src.size());
101 template <
class CONTAINER1,
class CONTAINER2,
typename VALUE>
102 inline void cumsum_tmpl(
const CONTAINER1& in_data, CONTAINER2& out_cumsum)
106 const size_t N = in_data.size();
107 for (
size_t i = 0; i < N; i++) last = out_cumsum[i] = last + in_data[i];
110 template <
class CONTAINER1,
class CONTAINER2>
111 inline void cumsum(
const CONTAINER1& in_data, CONTAINER2& out_cumsum)
115 in_data, out_cumsum);
120 template <
class CONTAINER>
121 inline CONTAINER
cumsum(
const CONTAINER& in_data)
128 template <
class CONTAINER>
133 template <
class CONTAINER>
138 template <
class CONTAINER>
143 template <
class CONTAINER>
149 template <
typename T>
157 template <
typename T>
172 template <
class CONTAINER,
typename VALUE>
175 return total +
v.squaredNorm();
180 template <
size_t N,
class T,
class U>
184 for (
size_t i = 0; i < N; i++)
res +=
square(
v[i]);
189 template <
class CONTAINER1,
class CONTAINER2>
191 const CONTAINER1&
v1,
const CONTAINER1&
v2)
197 template <
size_t N,
class T,
class U,
class V>
201 for (
size_t i = 0; i < N; i++)
res +=
v1[i] *
v2[i];
210 template <
class CONTAINER>
217 template <
typename T>
218 inline T
sum(
const std::vector<T>&
v)
220 return std::accumulate(
v.begin(),
v.end(), T(0));
225 template <
class CONTAINER,
typename RET>
228 return v.template sumRetType<RET>();
233 template <
class CONTAINER>
234 inline double mean(
const CONTAINER&
v)
239 return sum(
v) /
static_cast<double>(
v.size());
243 template <
typename T>
247 const size_t N = V.size();
248 curMin = curMax = V[0];
249 for (
size_t i = 1; i < N; i++)
257 template <
class Derived>
259 const Eigen::MatrixBase<Derived>& V,
263 V.minimum_maximum(curMin, curMax);
270 template <
class CONTAINER1,
class CONTAINER2>
277 it2 !=
b.end(); ++it2)
278 if ((*it1) == (*it2)) ret++;
284 template <
class CONTAINER>
289 if (
size_t(m.size()) == 0)
return;
293 m -= (curMin + minVal);
294 if (curRan != 0) m *= (maxVal - minVal) / curRan;
305 template <
class VECTORLIKE>
307 const VECTORLIKE&
v,
double& out_mean,
double& out_std,
308 bool unbiased =
true)
313 out_mean = (
v.size() == 1) ? *
v.begin() : 0;
318 const size_t N =
v.size();
321 double vector_std = 0;
322 for (
size_t i = 0; i < N; i++)
325 std::sqrt(vector_std / static_cast<double>(N - (unbiased ? 1 : 0)));
335 template <
class VECTORLIKE>
336 inline double stddev(
const VECTORLIKE&
v,
bool unbiased =
true)
350 template <
class VECTOR_OF_VECTOR,
class VECTORLIKE,
class MATRIXLIKE>
352 const VECTOR_OF_VECTOR&
v, VECTORLIKE& out_mean, MATRIXLIKE& out_cov)
354 const size_t N =
v.size();
355 ASSERTMSG_(N > 0,
"The input vector contains no elements");
356 const double N_inv = 1.0 / N;
358 const size_t M =
v[0].size();
359 ASSERTMSG_(M > 0,
"The input vector contains rows of length 0");
362 out_mean.assign(M, 0);
363 for (
size_t i = 0; i < N; i++)
364 for (
size_t j = 0; j < M; j++) out_mean[j] +=
v[i][j];
371 for (
size_t i = 0; i < N; i++)
373 for (
size_t j = 0; j < M; j++)
374 out_cov.get_unsafe(j, j) +=
square(
v[i][j] - out_mean[j]);
376 for (
size_t j = 0; j < M; j++)
377 for (
size_t k = j + 1; k < M; k++)
378 out_cov.get_unsafe(j, k) +=
379 (
v[i][j] - out_mean[j]) * (
v[i][k] - out_mean[k]);
381 for (
size_t j = 0; j < M; j++)
382 for (
size_t k = j + 1; k < M; k++)
383 out_cov.get_unsafe(k, j) = out_cov.get_unsafe(j, k);
394 template <
class VECTOR_OF_VECTOR,
class RETURN_MATRIX>
397 std::vector<double> m;
409 template <
class CONT1,
class CONT2>
412 ASSERT_(patch1.size() == patch2.size())
414 double numerator = 0, sum_a = 0, sum_b = 0, result, a_mean, b_mean;
415 a_mean = patch1.mean();
416 b_mean = patch2.mean();
418 const size_t N = patch1.size();
419 for (
size_t i = 0; i < N; ++i)
421 numerator += (patch1[i] - a_mean) * (patch2[i] - b_mean);
425 ASSERTMSG_(sum_a * sum_b != 0,
"Divide by zero when normalizing.")
426 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.
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)
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=nullptr)
Computes the normalized or normal histogram of a sequence of numbers given the number of bins and the...
double mean(const CONTAINER &v)
Computes the mean value of a vector.
CONTAINER::value_type element_t
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)