Go to the documentation of this file.
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>
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];
366 for (
size_t j = 0; j < M; j++)
367 out_mean[j] *= N_inv;
373 for (
size_t i = 0; i < N; i++)
375 for (
size_t j = 0; j < M; j++)
376 out_cov.get_unsafe(j, j) +=
square(
v[i][j] - out_mean[j]);
378 for (
size_t j = 0; j < M; j++)
379 for (
size_t k = j + 1; k < M; k++)
380 out_cov.get_unsafe(j, k) +=
381 (
v[i][j] - out_mean[j]) * (
v[i][k] - out_mean[k]);
383 for (
size_t j = 0; j < M; j++)
384 for (
size_t k = j + 1; k < M; k++)
385 out_cov.get_unsafe(k, j) = out_cov.get_unsafe(j, k);
396 template <
class VECTOR_OF_VECTOR,
class RETURN_MATRIX>
399 std::vector<double> m;
411 template <
class CONT1,
class CONT2>
414 ASSERT_(patch1.size() == patch2.size());
416 double numerator = 0, sum_a = 0, sum_b = 0, result, a_mean, b_mean;
417 a_mean = patch1.mean();
418 b_mean = patch2.mean();
420 const size_t N = patch1.size();
421 for (
size_t i = 0; i < N; ++i)
423 numerator += (patch1[i] - a_mean) * (patch2[i] - b_mean);
427 ASSERTMSG_(sum_a * sum_b != 0,
"Divide by zero when normalizing.");
428 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.
void minimum_maximum(const std::vector< T > &V, T &curMin, T &curMax)
Return the maximum and minimum values of a std::vector.
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...
const Scalar * const_iterator
CONTAINER::Scalar norm_inf(const CONTAINER &v)
CONTAINER::Scalar maximum(const CONTAINER &v)
This class provides an easy way of computing histograms for unidimensional real valued variables.
void cumsum(const CONTAINER1 &in_data, CONTAINER2 &out_cumsum)
RET sumRetType(const CONTAINER &v)
Computes the sum of all the elements, with a custom return type.
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)
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 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);...
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,...
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
#define ASSERT_(f)
Defines an assertion mechanism.
T square(const T x)
Inline function for the square of a number.
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 ...
CONTAINER::Scalar norm(const CONTAINER &v)
typename CONTAINER::value_type element_t
GLfloat GLfloat GLfloat v2
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 cumsum_tmpl(const CONTAINER1 &in_data, CONTAINER2 &out_cumsum)
Computes the cumulative sum of all the elements, saving the result in another container.
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...
T squareNorm(const U &v)
Compute the square norm of anything implementing [].
#define ASSERTMSG_(f, __ERROR_MSG)
Defines an assertion mechanism.
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,...
double stddev(const VECTORLIKE &v, bool unbiased=true)
Computes the standard deviation of a vector.
void getHistogram(std::vector< double > &x, std::vector< double > &hits) const
Returns the list of bin centers & hit counts.
void resizeLike(EIGEN_CONTAINER &trg, const EIGEN_CONTAINER &src)
void add(const double x)
Add an element to the histogram.
CONTAINER::Scalar minimum(const CONTAINER &v)
GLubyte GLubyte GLubyte a
Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST | |