9 #ifndef mrpt_math_container_ops_H 10 #define mrpt_math_container_ops_H 62 template <
class CONTAINER>
64 const CONTAINER&
v,
double limit_min,
double limit_max,
size_t number_bins,
65 bool do_normalization =
false,
66 std::vector<double>* out_bin_centers =
nullptr)
69 std::vector<double> ret(number_bins);
70 std::vector<double> dummy_ret_bins;
74 out_bin_centers ? *out_bin_centers : dummy_ret_bins, ret);
77 out_bin_centers ? *out_bin_centers : dummy_ret_bins, ret);
81 template <
class EIGEN_CONTAINER>
89 trg.resize(
src.size());
99 template <
class CONTAINER1,
class CONTAINER2,
typename VALUE>
100 inline void cumsum_tmpl(
const CONTAINER1& in_data, CONTAINER2& out_cumsum)
104 const size_t N = in_data.size();
105 for (
size_t i = 0; i < N; i++) last = out_cumsum[i] = last + in_data[i];
108 template <
class CONTAINER1,
class CONTAINER2>
109 inline void cumsum(
const CONTAINER1& in_data, CONTAINER2& out_cumsum)
113 in_data, out_cumsum);
118 template <
class CONTAINER>
119 inline CONTAINER
cumsum(
const CONTAINER& in_data)
126 template <
class CONTAINER>
131 template <
class CONTAINER>
136 template <
class CONTAINER>
141 template <
class CONTAINER>
147 template <
typename T>
155 template <
typename T>
170 template <
class CONTAINER,
typename VALUE>
173 return total +
v.squaredNorm();
178 template <
size_t N,
class T,
class U>
182 for (
size_t i = 0; i < N; i++)
res +=
square(
v[i]);
187 template <
class CONTAINER1,
class CONTAINER2>
189 const CONTAINER1&
v1,
const CONTAINER1&
v2)
195 template <
size_t N,
class T,
class U,
class V>
199 for (
size_t i = 0; i < N; i++)
res +=
v1[i] *
v2[i];
208 template <
class CONTAINER>
215 template <
typename T>
216 inline T
sum(
const std::vector<T>&
v)
218 return std::accumulate(
v.begin(),
v.end(), T(0));
223 template <
class CONTAINER,
typename RET>
226 return v.template sumRetType<RET>();
231 template <
class CONTAINER>
232 inline double mean(
const CONTAINER&
v)
237 return sum(
v) /
static_cast<double>(
v.size());
241 template <
typename T>
245 const size_t N = V.size();
246 curMin = curMax = V[0];
247 for (
size_t i = 1; i < N; i++)
255 template <
class Derived>
261 V.minimum_maximum(curMin, curMax);
268 template <
class CONTAINER1,
class CONTAINER2>
275 it2 !=
b.end(); ++it2)
276 if ((*it1) == (*it2)) ret++;
282 template <
class CONTAINER>
287 if (
size_t(m.size()) == 0)
return;
291 m -= (curMin + minVal);
292 if (curRan != 0) m *= (maxVal - minVal) / curRan;
303 template <
class VECTORLIKE>
305 const VECTORLIKE&
v,
double& out_mean,
double& out_std,
306 bool unbiased =
true)
311 out_mean = (
v.size() == 1) ? *
v.begin() : 0;
316 const size_t N =
v.size();
319 double vector_std = 0;
320 for (
size_t i = 0; i < N; i++)
323 std::sqrt(vector_std / static_cast<double>(N - (unbiased ? 1 : 0)));
333 template <
class VECTORLIKE>
334 inline double stddev(
const VECTORLIKE&
v,
bool unbiased =
true)
348 template <
class VECTOR_OF_VECTOR,
class VECTORLIKE,
class MATRIXLIKE>
350 const VECTOR_OF_VECTOR&
v, VECTORLIKE& out_mean, MATRIXLIKE& out_cov)
352 const size_t N =
v.size();
353 ASSERTMSG_(N > 0,
"The input vector contains no elements");
354 const double N_inv = 1.0 / N;
356 const size_t M =
v[0].size();
357 ASSERTMSG_(M > 0,
"The input vector contains rows of length 0");
360 out_mean.assign(M, 0);
361 for (
size_t i = 0; i < N; i++)
362 for (
size_t j = 0; j < M; j++) out_mean[j] +=
v[i][j];
364 for (
size_t j = 0; j < M; j++)
365 out_mean[j] *= N_inv;
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 [].
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 resizeLike(EIGEN_CONTAINER &trg, const EIGEN_CONTAINER &src)
T square(const T x)
Inline function for the square of a number.
CONTAINER::Scalar minimum(const CONTAINER &v)
#define ASSERT_(f)
Defines an assertion mechanism.
This base provides a set of functions for maths stuff.
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 add(const double x)
Add an element to the histogram.
CONTAINER::Scalar sum(const CONTAINER &v)
Computes the sum of all the elements.
#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...
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)
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);...
typename CONTAINER::value_type element_t
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 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 ...
GLubyte GLubyte GLubyte a
const Scalar * const_iterator
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)