27 template <
typename MAT_H,
typename MAT_C,
typename MAT_R>
29 const MAT_H& H,
const MAT_C& C, MAT_R&
R,
bool accumResultInOutput =
false)
32 if (accumResultInOutput)
36 R.resize(
res.rows(),
res.cols());
42 template <std::
size_t H_ROWS, std::
size_t H_COLS,
typename Scalar>
55 template <
typename VECTOR_H,
typename MAT_C>
63 template <
typename VECTOR_H,
typename MAT_C>
78 template <
class MAT_IN,
class VECTOR,
class MAT_OUT>
81 const size_t N =
v.rows();
82 ASSERTMSG_(N > 0,
"The input matrix contains no elements");
83 const double N_inv = 1.0 / N;
85 const size_t M =
v.cols();
86 ASSERTMSG_(M > 0,
"The input matrix contains rows of length 0");
89 out_mean.assign(M, 0);
90 for (
size_t i = 0; i < N; i++)
91 for (
size_t j = 0; j < M; j++) out_mean[j] +=
v.coeff(i, j);
97 out_cov.setZero(M, M);
98 for (
size_t i = 0; i < N; i++)
100 for (
size_t j = 0; j < M; j++)
101 out_cov(j, j) +=
square(
v(i, j) - out_mean[j]);
103 for (
size_t j = 0; j < M; j++)
104 for (
size_t k = j + 1; k < M; k++)
106 (
v(i, j) - out_mean[j]) * (
v(i, k) - out_mean[k]);
108 for (
size_t j = 0; j < M; j++)
109 for (
size_t k = j + 1; k < M; k++) out_cov(k, j) = out_cov(j, k);
120 template <
class MAT_IN,
class VEC>
122 const MAT_IN& m, VEC& outMeanVector, VEC& outStdVector,
123 const bool unbiased_variance =
true)
125 const auto N = m.rows(), M = m.cols();
126 if (N == 0)
throw std::runtime_error(
"meanAndStdColumns: Empty container.");
127 const double N_inv = 1.0 / N;
129 unbiased_variance ? (N > 1 ? 1.0 / (N - 1) : 1.0) : 1.0 / N;
130 outMeanVector.resize(M);
131 outStdVector.resize(M);
132 for (decltype(m.cols()) i = 0; i < M; i++)
134 outMeanVector[i] = m.asEigen().col(i).array().sum() * N_inv;
135 outStdVector[i] = std::sqrt(
136 (m.asEigen().col(i).array() - outMeanVector[i]).
square().sum() *
147 template <
class MATRIX>
157 #define SAVE_MATRIX(M) M.saveToTextFile(#M ".txt"); 163 template <
class MAT_A,
class SKEW_3VECTOR,
class MAT_OUT>
169 const size_t N =
A.rows();
171 for (
size_t i = 0; i < N; i++)
173 out(i, 0) =
A(i, 1) *
v[2] -
A(i, 2) *
v[1];
174 out(i, 1) = -
A(i, 0) *
v[2] +
A(i, 2) *
v[0];
175 out(i, 2) =
A(i, 0) *
v[1] -
A(i, 1) *
v[0];
184 template <
class SKEW_3VECTOR,
class MAT_A,
class MAT_OUT>
190 const size_t N =
A.cols();
192 for (
size_t i = 0; i < N; i++)
194 out(0, i) = -
A(1, i) *
v[2] +
A(2, i) *
v[1];
195 out(1, i) =
A(0, i) *
v[2] -
A(2, i) *
v[0];
196 out(2, i) = -
A(0, i) *
v[1] +
A(1, i) *
v[0];
205 template <
typename MATIN,
typename MATOUT>
208 if (
g.rows() !=
g.cols())
209 throw std::runtime_error(
"laplacian: Defined for square matrixes only");
210 const auto N =
g.rows();
213 for (
typename MATIN::Index i = 0; i < N; i++)
216 for (
typename MATIN::Index j = 0; j < N; j++) deg +=
g(j, i);
228 template <std::
size_t BLOCKSIZE,
typename MAT,
typename MATRIX>
230 const MAT& m,
const std::vector<size_t>& block_indices, MATRIX& out)
233 throw std::runtime_error(
234 "extractSubmatrixSymmetricalBlocks: BLOCKSIZE must be >=1");
235 if (m.cols() != m.rows())
236 throw std::runtime_error(
237 "extractSubmatrixSymmetricalBlocks: Matrix is not square.");
239 const size_t N = block_indices.size();
241 out.resize(nrows_out, nrows_out);
243 for (
size_t dst_row_blk = 0; dst_row_blk < N; ++dst_row_blk)
245 for (
size_t dst_col_blk = 0; dst_col_blk < N; ++dst_col_blk)
250 throw std::runtime_error(
251 "extractSubmatrixSymmetricalBlocks: Indices out of " 254 out.asEigen().template block<BLOCKSIZE, BLOCKSIZE>(
256 m.asEigen().template block<BLOCKSIZE, BLOCKSIZE>(
264 template <
typename MAT,
typename MATRIX>
266 const MAT& m,
const std::size_t
BLOCKSIZE,
267 const std::vector<size_t>& block_indices, MATRIX& out)
270 throw std::runtime_error(
271 "extractSubmatrixSymmetricalBlocks: BLOCKSIZE must be >=1");
272 if (m.cols() != m.rows())
273 throw std::runtime_error(
274 "extractSubmatrixSymmetricalBlocks: Matrix is not square.");
276 const size_t N = block_indices.size();
278 out.resize(nrows_out, nrows_out);
280 for (
size_t dst_row_blk = 0; dst_row_blk < N; ++dst_row_blk)
282 for (
size_t dst_col_blk = 0; dst_col_blk < N; ++dst_col_blk)
287 throw std::runtime_error(
288 "extractSubmatrixSymmetricalBlocks: Indices out of " 308 template <
typename MAT,
typename MATRIX>
310 const MAT& m,
const std::vector<size_t>&
indices, MATRIX& out)
312 if (m.cols() != m.rows())
313 throw std::runtime_error(
314 "extractSubmatrixSymmetrical: Matrix is not square.");
316 const size_t N =
indices.size();
317 const size_t nrows_out = N;
318 out.resize(nrows_out, nrows_out);
320 for (
size_t dst_row_blk = 0; dst_row_blk < N; ++dst_row_blk)
321 for (
size_t dst_col_blk = 0; dst_col_blk < N; ++dst_col_blk)
322 out(dst_row_blk, dst_col_blk) =
MAT_C::Scalar multiply_HtCH_scalar(const VECTOR_H &H, const MAT_C &C)
r (a scalar) = H * C * H^t (with a column vector H and a symmetric matrix C)
A compile-time fixed-size numeric matrix container.
const Derived & mat2eig(const Eigen::EigenBase< Derived > &m)
Returns an Eigen-compatible type, despite its argument already is an Eigen matrix, or an mrpt-math matrix/vector.
void laplacian(const MATIN &g, MATOUT &ret)
Computes the Laplacian of a square graph weight matrix.
This file implements several operations that operate element-wise on individual or pairs of container...
void multiply_A_skew3(const MAT_A &A, const SKEW_3VECTOR &v, MAT_OUT &out)
Only for vectors/arrays "v" of length3, compute out = A * Skew(v), where Skew(v) is the skew symmetri...
GLuint GLuint GLsizei GLenum const GLvoid * indices
void extractSubmatrixSymmetricalBlocks(const MAT &m, const std::vector< size_t > &block_indices, MATRIX &out)
Get a submatrix from a square matrix, by collecting the elements M(idxs,idxs), where idxs is a sequen...
MAT_C::Scalar multiply_HCHt_scalar(const VECTOR_H &H, const MAT_C &C)
r (a scalar) = H^t * C * H (with a row vector H and a symmetric matrix C)
void meanAndCovMat(const MAT_IN &v, VECTOR &out_mean, MAT_OUT &out_cov)
Computes the mean vector and covariance from a list of samples in an NxM matrix, where each row is a ...
T square(const T x)
Inline function for the square of a number.
#define ASSERT_EQUAL_(__A, __B)
Assert comparing two values, reporting their actual values upon failure.
#define ASSERTMSG_(f, __ERROR_MSG)
Defines an assertion mechanism.
CMatrixDouble cov(const MATRIX &v)
Computes the covariance matrix from a list of samples in an NxM matrix, where each row is a sample...
void meanAndStdColumns(const MAT_IN &m, VEC &outMeanVector, VEC &outStdVector, const bool unbiased_variance=true)
Computes a row with the mean values of each column in the matrix and the associated vector with the s...
void extractSubmatrixSymmetrical(const MAT &m, const std::vector< size_t > &indices, MATRIX &out)
Get a submatrix from a square matrix, by collecting the elements M(idxs,idxs), where idxs is the sequ...
GLsizei GLboolean transpose
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void extractSubmatrixSymmetricalBlocksDyn(const MAT &m, const std::size_t BLOCKSIZE, const std::vector< size_t > &block_indices, MATRIX &out)
void multiply_skew3_A(const SKEW_3VECTOR &v, const MAT_A &A, MAT_OUT &out)
Only for vectors/arrays "v" of length3, compute out = Skew(v) * A, where Skew(v) is the skew symmetri...
void multiply_HCHt(const MAT_H &H, const MAT_C &C, MAT_R &R, bool accumResultInOutput=false)
R = H * C * H^t.