9 #ifndef mrpt_math_matrix_ops_H 10 #define mrpt_math_matrix_ops_H 32 template <
class Derived>
33 inline const typename Eigen::MatrixBase<Derived>::AdjointReturnType
operator~(
34 const Eigen::MatrixBase<Derived>& m)
40 template <
class Derived>
41 inline typename Eigen::MatrixBase<Derived>::PlainObject
operator!(
42 const Eigen::MatrixBase<Derived>& m)
50 template <
typename MAT_H,
typename MAT_C,
typename MAT_R>
52 const MAT_H& H,
const MAT_C& C, MAT_R&
R,
53 bool accumResultInOutput)
55 if (accumResultInOutput)
56 R += ((H * C.template selfadjointView<Eigen::Lower>()).eval() *
59 .template selfadjointView<Eigen::Lower>();
61 R = ((H * C.template selfadjointView<Eigen::Lower>()).eval() *
64 .template selfadjointView<Eigen::Lower>();
68 template <
typename VECTOR_H,
typename MAT_C>
71 return (H.matrix().adjoint() * C * H.matrix()).eval()(0, 0);
75 template <
typename MAT_H,
typename MAT_C,
typename MAT_R>
77 const MAT_H& H,
const MAT_C& C, MAT_R&
R,
78 bool accumResultInOutput)
80 if (accumResultInOutput)
82 ((H.adjoint() * C.template selfadjointView<Eigen::Lower>()).eval() *
85 .template selfadjointView<Eigen::Lower>();
87 R = ((H.adjoint() * C.template selfadjointView<Eigen::Lower>()).eval() *
90 .template selfadjointView<Eigen::Lower>();
102 template <
class MAT_IN,
class VECTOR,
class MAT_OUT>
105 const size_t N =
v.rows();
106 ASSERTMSG_(N > 0,
"The input matrix contains no elements");
107 const double N_inv = 1.0 / N;
109 const size_t M =
v.cols();
110 ASSERTMSG_(M > 0,
"The input matrix contains rows of length 0");
113 out_mean.assign(M, 0);
114 for (
size_t i = 0; i < N; i++)
115 for (
size_t j = 0; j < M; j++) out_mean[j] +=
v.coeff(i, j);
122 for (
size_t i = 0; i < N; i++)
124 for (
size_t j = 0; j < M; j++)
125 out_cov.get_unsafe(j, j) +=
126 square(
v.get_unsafe(i, j) - out_mean[j]);
128 for (
size_t j = 0; j < M; j++)
129 for (
size_t k = j + 1; k < M; k++)
130 out_cov.get_unsafe(j, k) += (
v.get_unsafe(i, j) - out_mean[j]) *
131 (
v.get_unsafe(i, k) - out_mean[k]);
133 for (
size_t j = 0; j < M; j++)
134 for (
size_t k = j + 1; k < M; k++)
135 out_cov.get_unsafe(k, j) = out_cov.get_unsafe(j, k);
145 template <
class MATRIX>
146 inline Eigen::Matrix<
typename MATRIX::Scalar, MATRIX::ColsAtCompileTime,
147 MATRIX::ColsAtCompileTime>
150 Eigen::Matrix<double, MATRIX::ColsAtCompileTime, 1> m;
152 MATRIX::ColsAtCompileTime>
159 #define SAVE_MATRIX(M) M.saveToTextFile(#M ".txt"); 165 template <
class MAT_A,
class SKEW_3VECTOR,
class MAT_OUT>
171 const size_t N =
size(A, 1);
173 for (
size_t i = 0; i < N; i++)
176 i, 0, A.get_unsafe(i, 1) *
v[2] - A.get_unsafe(i, 2) *
v[1]);
178 i, 1, -A.get_unsafe(i, 0) *
v[2] + A.get_unsafe(i, 2) *
v[0]);
180 i, 2, A.get_unsafe(i, 0) *
v[1] - A.get_unsafe(i, 1) *
v[0]);
189 template <
class SKEW_3VECTOR,
class MAT_A,
class MAT_OUT>
195 const size_t N =
size(A, 2);
197 for (
size_t i = 0; i < N; i++)
200 0, i, -A.get_unsafe(1, i) *
v[2] + A.get_unsafe(2, i) *
v[1]);
202 1, i, A.get_unsafe(0, i) *
v[2] - A.get_unsafe(2, i) *
v[0]);
204 2, i, -A.get_unsafe(0, i) *
v[1] + A.get_unsafe(1, i) *
v[0]);
214 template <
class MATORG,
class MATDEST>
216 const MATORG& M,
const size_t first_row,
const size_t first_col,
219 const size_t NR = outMat.getRowCount();
220 const size_t NC = outMat.getColCount();
223 for (
size_t r = 0;
r < NR;
r++)
224 for (
size_t c = 0;
c < NC;
c++)
225 outMat.get_unsafe(
r,
c) =
226 M.get_unsafe(first_row +
r, first_col +
c);
#define ASSERT_EQUAL_(__A, __B)
void multiply_HCHt(const MAT_H &H, const MAT_C &C, MAT_R &R, bool accumResultInOutput)
R = H * C * H^t (with C symmetric)
#define ASSERT_BELOWEQ_(__A, __B)
size_t size(const MATRIXLIKE &m, const int dim)
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...
MAT_C::Scalar multiply_HCHt_scalar(const VECTOR_H &H, const MAT_C &C)
r (a scalar) = H * C * H^t (with a 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.
void extractMatrix(const MATORG &M, const size_t first_row, const size_t first_col, MATDEST &outMat)
Extract a submatrix - The output matrix must be set to the required size before call.
void multiply_HtCH(const MAT_H &H, const MAT_C &C, MAT_R &R, bool accumResultInOutput)
R = H^t * C * H (with C symmetric)
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
GLdouble GLdouble GLdouble r
Eigen::MatrixBase< Derived >::PlainObject operator!(const Eigen::MatrixBase< Derived > &m)
Unary inversion operator.
Eigen::Matrix< typename MATRIX::Scalar, MATRIX::ColsAtCompileTime, MATRIX::ColsAtCompileTime > cov(const MATRIX &v)
Computes the covariance matrix from a list of samples in an NxM matrix, where each row is a sample...
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...
const Eigen::MatrixBase< Derived >::AdjointReturnType operator~(const Eigen::MatrixBase< Derived > &m)
Transpose operator for matrices.
#define ASSERTMSG_(f, __ERROR_MSG)