18 #include <type_traits> 40 template <
typename CONTAINER>
44 template <
typename Derived>
50 template <
typename Scalar,
typename Derived>
63 template <
class CONTAINER>
65 const CONTAINER& v,
double limit_min,
double limit_max,
size_t number_bins,
66 bool do_normalization =
false,
67 std::vector<double>* out_bin_centers =
nullptr)
70 std::vector<double> ret(number_bins);
71 std::vector<double> dummy_ret_bins;
75 out_bin_centers ? *out_bin_centers : dummy_ret_bins, ret);
78 out_bin_centers ? *out_bin_centers : dummy_ret_bins, ret);
82 template <
class EIGEN_CONTAINER>
83 void resizeLike(EIGEN_CONTAINER& trg,
const EIGEN_CONTAINER& src)
88 void resizeLike(std::vector<T>& trg,
const std::vector<T>& src)
90 trg.resize(src.size());
100 template <
class CONTAINER1,
class CONTAINER2>
101 inline void cumsum_tmpl(
const CONTAINER1& in_data, CONTAINER2& out_cumsum)
105 std::remove_const_t<std::remove_reference_t<decltype(in_data[0])>>;
107 const size_t N = in_data.size();
108 for (
size_t i = 0; i < N; i++) last = out_cumsum[i] = last + in_data[i];
111 template <
class CONTAINER1,
class CONTAINER2>
112 inline void cumsum(
const CONTAINER1& in_data, CONTAINER2& out_cumsum)
114 cumsum_tmpl<CONTAINER1, CONTAINER2>(in_data, out_cumsum);
119 template <
class CONTAINER>
120 inline CONTAINER
cumsum(
const CONTAINER& in_data)
127 template <
class CONTAINER>
132 template <
class CONTAINER>
137 template <
class CONTAINER,
int = CONTAINER::is_mrpt_type>
142 template <
class CONTAINER,
int = CONTAINER::is_mrpt_type>
148 template <
class Derived>
153 template <
class Derived>
159 template <
typename T>
167 template <
typename T>
182 template <
class CONTAINER,
typename VALUE>
185 return total + v.squaredNorm();
190 template <
size_t N,
class T,
class U>
194 for (
size_t i = 0; i < N; i++) res +=
square(v[i]);
199 template <
class CONTAINER1,
class CONTAINER2>
201 const CONTAINER1& v1,
const CONTAINER1& v2)
207 template <
size_t N,
class T,
class U,
class V>
211 for (
size_t i = 0; i < N; i++) res += v1[i] * v2[i];
220 template <
class CONTAINER>
227 template <
typename T>
228 inline T
sum(
const std::vector<T>& v)
230 return std::accumulate(v.begin(), v.end(), T(0));
235 template <
class CONTAINER,
typename RET>
238 return v.template sumRetType<RET>();
243 template <
class CONTAINER>
244 inline double mean(
const CONTAINER& v)
249 return sum(v) /
static_cast<double>(v.size());
253 template <
typename T>
257 const size_t N = V.size();
258 curMin = curMax = V[0];
259 for (
size_t i = 1; i < N; i++)
267 template <
class Derived>
273 V.minimum_maximum(curMin, curMax);
278 template <
class CONTAINER,
typename Scalar>
281 if (!c.size())
return;
282 const Scalar curMin = c.minCoeff();
283 const Scalar curMax = c.maxCoeff();
284 Scalar minMaxDelta = curMax - curMin;
285 if (minMaxDelta == 0) minMaxDelta = 1;
286 const Scalar minMaxDelta_ = (valMax - valMin) / minMaxDelta;
287 c.array() = (c.array() - curMin) * minMaxDelta_ + valMin;
294 template <
class CONTAINER1,
class CONTAINER2>
298 for (
auto it1 = a.begin(); it1 != a.end(); ++it1)
299 for (
auto it2 = b.begin(); it2 != b.end(); ++it2)
300 if ((*it1) == (*it2)) ret++;
306 template <
class CONTAINER>
311 if (
size_t(m.size()) == 0)
return;
315 m -= (curMin + minVal);
316 if (curRan != 0) m *= (maxVal - minVal) / curRan;
328 template <
class VECTORLIKE>
330 const VECTORLIKE& v,
double& out_mean,
double& out_std,
331 bool unbiased =
true)
336 out_mean = (v.size() == 1) ? *v.begin() : 0;
341 const size_t N = v.size();
344 double vector_std = 0;
345 for (
size_t i = 0; i < N; i++)
348 std::sqrt(vector_std / static_cast<double>(N - (unbiased ? 1 : 0)));
358 template <
class VECTORLIKE>
359 inline double stddev(
const VECTORLIKE& v,
bool unbiased =
true)
373 template <
class VECTOR_OF_VECTOR,
class VECTORLIKE,
class MATRIXLIKE>
375 const VECTOR_OF_VECTOR& v, VECTORLIKE& out_mean, MATRIXLIKE& out_cov)
377 const size_t N = v.size();
378 ASSERTMSG_(N > 0,
"The input vector contains no elements");
379 const double N_inv = 1.0 / N;
381 const size_t M = v[0].size();
382 ASSERTMSG_(M > 0,
"The input vector contains rows of length 0");
385 out_mean.assign(M, 0);
386 for (
size_t i = 0; i < N; i++)
387 for (
size_t j = 0; j < M; j++) out_mean[j] += v[i][j];
389 for (
size_t j = 0; j < M; j++) out_mean[j] *= N_inv;
394 out_cov.setZero(M, M);
395 for (
size_t i = 0; i < N; i++)
397 for (
size_t j = 0; j < M; j++)
398 out_cov(j, j) +=
square(v[i][j] - out_mean[j]);
400 for (
size_t j = 0; j < M; j++)
401 for (
size_t k = j + 1; k < M; k++)
403 (v[i][j] - out_mean[j]) * (v[i][k] - out_mean[k]);
405 for (
size_t j = 0; j < M; j++)
406 for (
size_t k = j + 1; k < M; k++) out_cov(k, j) = out_cov(j, k);
417 template <
class VECTOR_OF_VECTOR,
class RETURN_MATRIX>
418 inline RETURN_MATRIX
covVector(
const VECTOR_OF_VECTOR& v)
420 std::vector<double> m;
432 template <
class CONT1,
class CONT2>
435 ASSERT_(patch1.size() == patch2.size());
437 double numerator = 0, sum_a = 0, sum_b = 0, result, a_mean, b_mean;
438 a_mean = patch1.mean();
439 b_mean = patch2.mean();
441 const size_t N = patch1.size();
442 for (
size_t i = 0; i < N; ++i)
444 numerator += (patch1[i] - a_mean) * (patch2[i] - b_mean);
448 ASSERTMSG_(sum_a * sum_b != 0,
"Divide by zero when normalizing.");
449 result = numerator / std::sqrt(sum_a * sum_b);
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 class provides an easy way of computing histograms for unidimensional real valued variables...
typename Derived::Scalar element_t
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 [].
void resizeLike(EIGEN_CONTAINER &trg, const EIGEN_CONTAINER &src)
#define ASSERT_(f)
Defines an assertion mechanism.
This base provides a set of functions for maths stuff.
void normalize(CONTAINER &c, Scalar valMin, Scalar valMax)
Scales all elements such as the minimum & maximum values are shifted to the given values...
void add(const double x)
Add an element to the histogram.
CONTAINER::Scalar sum(const CONTAINER &v)
Computes the sum of all the elements.
CONTAINER::Scalar maximum(const CONTAINER &v)
#define ASSERTMSG_(f, __ERROR_MSG)
Defines an assertion mechanism.
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...
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)
ContainerType<T>::element_t exposes the value of any STL or Eigen container.
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...
CONTAINER::Scalar norm_inf(const CONTAINER &v)
return_t square(const num_t x)
Inline function for the square of a number.
CONTAINER::Scalar minimum(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 (or all elements of a matrix)
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.
void cumsum_tmpl(const CONTAINER1 &in_data, CONTAINER2 &out_cumsum)
Computes the cumulative sum of all the elements, saving the result in another container.
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...
Base CRTP class for all MRPT vectors and matrices.
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 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)