MRPT  1.9.9
eigen_plugins.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 // -------------------------------------------------------------------------
11 // Note: This file will be included within the body of Eigen::MatrixBase
12 // -------------------------------------------------------------------------
13 public:
14 /** @name MRPT plugin: Types
15  * @{ */
16 // size is constant
17 enum
18 {
19  static_size = RowsAtCompileTime * ColsAtCompileTime
20 };
21 /** @} */
22 
23 /** @name MRPT plugin: Basic iterators. These iterators are intended for 1D
24  * matrices only, i.e. column or row vectors.
25  * @{ */
26 using iterator = Scalar*;
27 using const_iterator = const Scalar*;
28 
29 EIGEN_STRONG_INLINE iterator begin() { return derived().data(); }
30 EIGEN_STRONG_INLINE iterator end()
31 {
32  return (&(derived().data()[size() - 1])) + 1;
33 }
34 EIGEN_STRONG_INLINE const_iterator begin() const { return derived().data(); }
35 EIGEN_STRONG_INLINE const_iterator end() const
36 {
37  return (&(derived().data()[size() - 1])) + 1;
38 }
39 
40 /** @} */
41 
42 /** @name MRPT plugin: Set/get/load/save and other miscelaneous methods
43  * @{ */
44 
45 /*! Fill all the elements with a given value */
46 EIGEN_STRONG_INLINE void fill(const Scalar v) { derived().setConstant(v); }
47 /*! Fill all the elements with a given value */
48 EIGEN_STRONG_INLINE void assign(const Scalar v) { derived().setConstant(v); }
49 /*! Resize to N and set all the elements to a given value */
50 EIGEN_STRONG_INLINE void assign(size_t N, const Scalar v)
51 {
52  derived().resize(N);
53  derived().setConstant(v);
54 }
55 
56 /** Make the matrix an identity matrix (the diagonal values can be 1.0 or any
57  * other value) */
58 EIGEN_STRONG_INLINE void unit(const size_t nRows, const Scalar diag_vals)
59 {
60  if (diag_vals == 1)
61  derived().setIdentity(nRows, nRows);
62  else
63  {
64  derived().setZero(nRows, nRows);
65  derived().diagonal().setConstant(diag_vals);
66  }
67 }
68 
69 /** Make the matrix an identity matrix */
70 EIGEN_STRONG_INLINE void unit() { derived().setIdentity(); }
71 /** Make the matrix an identity matrix */
72 EIGEN_STRONG_INLINE void eye() { derived().setIdentity(); }
73 /** Set all elements to zero */
74 EIGEN_STRONG_INLINE void zeros() { derived().setZero(); }
75 /** Resize and set all elements to zero */
76 EIGEN_STRONG_INLINE void zeros(const size_t row, const size_t col)
77 {
78  derived().setZero(row, col);
79 }
80 
81 /** Resize matrix and set all elements to one */
82 EIGEN_STRONG_INLINE void ones(const size_t row, const size_t col)
83 {
84  derived().setOnes(row, col);
85 }
86 /** Set all elements to one */
87 EIGEN_STRONG_INLINE void ones() { derived().setOnes(); }
88 /** Fast but unsafe method to obtain a pointer to a given row of the matrix (Use
89  * only in time critical applications)
90  * VERY IMPORTANT WARNING: You must be aware of the memory layout, either
91  * Column or Row-major ordering.
92  */
93 EIGEN_STRONG_INLINE Scalar* get_unsafe_row(size_t row)
94 {
95  return &derived().coeffRef(row, 0);
96 }
97 EIGEN_STRONG_INLINE const Scalar* get_unsafe_row(size_t row) const
98 {
99  return &derived().coeff(row, 0);
100 }
101 
102 /** Read-only access to one element (Use with caution, bounds are not checked!)
103  */
104 EIGEN_STRONG_INLINE Scalar get_unsafe(const size_t row, const size_t col) const
105 {
106 #ifdef _DEBUG
107  return derived()(row, col);
108 #else
109  return derived().coeff(row, col);
110 #endif
111 }
112 /** Reference access to one element (Use with caution, bounds are not checked!)
113  */
114 EIGEN_STRONG_INLINE Scalar& get_unsafe(const size_t row, const size_t col)
115 { //-V659
116 #ifdef _DEBUG
117  return derived()(row, col);
118 #else
119  return derived().coeffRef(row, col);
120 #endif
121 }
122 /** Sets an element (Use with caution, bounds are not checked!) */
123 EIGEN_STRONG_INLINE void set_unsafe(
124  const size_t row, const size_t col, const Scalar val)
125 {
126 #ifdef _DEBUG
127  derived()(row, col) = val;
128 #else
129  derived().coeffRef(row, col) = val;
130 #endif
131 }
132 
133 /** Insert an element at the end of the container (for 1D vectors/arrays) */
134 EIGEN_STRONG_INLINE void push_back(Scalar val)
135 {
136  const Index N = size();
137  derived().conservativeResize(N + 1);
138  coeffRef(N) = val;
139 }
140 
141 EIGEN_STRONG_INLINE bool isSquare() const { return cols() == rows(); }
142 EIGEN_STRONG_INLINE bool isSingular(const Scalar absThreshold = 0) const
143 {
144  return std::abs(derived().determinant()) < absThreshold;
145 }
146 
147 /** Read a matrix from a string in Matlab-like format, for example "[1 0 2; 0 4
148  * -1]"
149  * The string must start with '[' and end with ']'. Rows are separated by
150  * semicolons ';' and
151  * columns in each row by one or more whitespaces ' ', commas ',' or tabs
152  * '\t'.
153  *
154  * This format is also used for CConfigFile::read_matrix.
155  *
156  * This template method can be instantiated for matrices of the types: int,
157  * long, unsinged int, unsigned long, float, double, long double
158  *
159  * \return true on success. false if the string is malformed, and then the
160  * matrix will be resized to 0x0.
161  * \sa inMatlabFormat, CConfigFile::read_matrix
162  */
164  const std::string& s, std::ostream* dump_errors_here = nullptr);
165 // Method implemented in eigen_plugins_impl.h
166 
167 /** Dump matrix in matlab format.
168  * This template method can be instantiated for matrices of the types: int,
169  * long, unsinged int, unsigned long, float, double, long double
170  * \sa fromMatlabStringFormat
171  */
172 std::string inMatlabFormat(const size_t decimal_digits = 6) const;
173 // Method implemented in eigen_plugins_impl.h
174 
175 /** Save matrix to a text file, compatible with MATLAB text format (see also the
176  * methods of matrix classes themselves).
177  * \param theMatrix It can be a CMatrixTemplate or a CMatrixFixedNumeric.
178  * \param file The target filename.
179  * \param fileFormat See TMatrixTextFileFormat. The format of the numbers in
180  * the text file.
181  * \param appendMRPTHeader Insert this header to the file "% File generated
182  * by MRPT. Load with MATLAB with: VAR=load(FILENAME);"
183  * \param userHeader Additional text to be written at the head of the file.
184  * Typically MALAB comments "% This file blah blah". Final end-of-line is not
185  * needed.
186  * \sa loadFromTextFile, CMatrixTemplate::inMatlabFormat, SAVE_MATRIX
187  */
189  const std::string& file, mrpt::math::TMatrixTextFileFormat fileFormat =
191  bool appendMRPTHeader = false,
192  const std::string& userHeader = std::string()) const;
193 // Method implemented in eigen_plugins_impl.h
194 
195 /** Load matrix from a text file, compatible with MATLAB text format.
196  * Lines starting with '%' or '#' are interpreted as comments and ignored.
197  * \sa saveToTextFile, fromMatlabStringFormat
198  */
199 void loadFromTextFile(const std::string& file);
200 // Method implemented in eigen_plugins_impl.h
201 
202 //! \overload
203 void loadFromTextFile(std::istream& _input_text_stream);
204 // Method implemented in eigen_plugins_impl.h
205 
206 EIGEN_STRONG_INLINE void multiplyColumnByScalar(size_t c, Scalar s)
207 {
208  derived().col(c) *= s;
209 }
210 EIGEN_STRONG_INLINE void multiplyRowByScalar(size_t r, Scalar s)
211 {
212  derived().row(r) *= s;
213 }
214 
215 EIGEN_STRONG_INLINE void swapCols(size_t i1, size_t i2)
216 {
217  derived().col(i1).swap(derived().col(i2));
218 }
219 EIGEN_STRONG_INLINE void swapRows(size_t i1, size_t i2)
220 {
221  derived().row(i1).swap(derived().row(i2));
222 }
223 
224 EIGEN_STRONG_INLINE size_t countNonZero() const
225 {
226  return ((*static_cast<const Derived*>(this)) != 0).count();
227 }
228 
229 /** [VECTORS OR MATRICES] Finds the maximum value
230  * \exception std::exception On an empty input container
231  */
232 EIGEN_STRONG_INLINE Scalar maximum() const
233 {
234  if (size() == 0) throw std::runtime_error("maximum: container is empty");
235  return derived().maxCoeff();
236 }
237 
238 /** [VECTORS OR MATRICES] Finds the minimum value
239  * \sa maximum, minimum_maximum
240  * \exception std::exception On an empty input container */
241 EIGEN_STRONG_INLINE Scalar minimum() const
242 {
243  if (size() == 0) throw std::runtime_error("minimum: container is empty");
244  return derived().minCoeff();
245 }
246 
247 /** [VECTORS OR MATRICES] Compute the minimum and maximum of a container at once
248  * \sa maximum, minimum
249  * \exception std::exception On an empty input container */
250 EIGEN_STRONG_INLINE void minimum_maximum(Scalar& out_min, Scalar& out_max) const
251 {
252  out_min = minimum();
253  out_max = maximum();
254 }
255 
256 /** [VECTORS ONLY] Finds the maximum value (and the corresponding zero-based
257  * index) from a given container.
258  * \exception std::exception On an empty input vector
259  */
260 EIGEN_STRONG_INLINE Scalar maximum(size_t* maxIndex) const
261 {
262  if (size() == 0) throw std::runtime_error("maximum: container is empty");
263  Index idx;
264  const Scalar m = derived().maxCoeff(&idx);
265  if (maxIndex) *maxIndex = idx;
266  return m;
267 }
268 
269 /** [VECTORS OR MATRICES] Finds the maximum value (and the corresponding
270  * zero-based index) from a given container.
271  * \exception std::exception On an empty input vector
272  */
273 void find_index_max_value(size_t& u, size_t& v, Scalar& valMax) const
274 {
275  if (cols() == 0 || rows() == 0)
276  throw std::runtime_error("find_index_max_value: container is empty");
277  Index idx1, idx2;
278  valMax = derived().maxCoeff(&idx1, &idx2);
279  u = idx1;
280  v = idx2;
281 }
282 
283 /** [VECTORS ONLY] Finds the minimum value (and the corresponding zero-based
284  * index) from a given container.
285  * \sa maximum, minimum_maximum
286  * \exception std::exception On an empty input vector */
287 EIGEN_STRONG_INLINE Scalar minimum(size_t* minIndex) const
288 {
289  if (size() == 0) throw std::runtime_error("minimum: container is empty");
290  Index idx;
291  const Scalar m = derived().minCoeff(&idx);
292  if (minIndex) *minIndex = idx;
293  return m;
294 }
295 
296 /** [VECTORS ONLY] Compute the minimum and maximum of a container at once
297  * \sa maximum, minimum
298  * \exception std::exception On an empty input vector */
299 EIGEN_STRONG_INLINE void minimum_maximum(
300  Scalar& out_min, Scalar& out_max, size_t* minIndex, size_t* maxIndex) const
301 {
302  out_min = minimum(minIndex);
303  out_max = maximum(maxIndex);
304 }
305 
306 /** Compute the norm-infinite of a vector ($f[ ||\mathbf{v}||_\infnty $f]), ie
307  * the maximum absolute value of the elements. */
308 EIGEN_STRONG_INLINE Scalar norm_inf() const
309 {
310  return lpNorm<Eigen::Infinity>();
311 }
312 
313 /** Compute the square norm of a vector/array/matrix (the Euclidean distance to
314  * the origin, taking all the elements as a single vector). \sa norm */
315 EIGEN_STRONG_INLINE Scalar squareNorm() const { return squaredNorm(); }
316 /*! Sum all the elements, returning a value of the same type than the container
317  */
318 EIGEN_STRONG_INLINE Scalar sumAll() const { return derived().sum(); }
319 /** Computes the laplacian of this square graph weight matrix.
320  * The laplacian matrix is L = D - W, with D a diagonal matrix with the degree
321  * of each node, W the
322  */
323 template <typename OtherDerived>
324 EIGEN_STRONG_INLINE void laplacian(Eigen::MatrixBase<OtherDerived>& ret) const
325 {
326  if (rows() != cols())
327  throw std::runtime_error("laplacian: Defined for square matrixes only");
328  const Index N = rows();
329  ret = -(*this);
330  for (Index i = 0; i < N; i++)
331  {
332  Scalar deg = 0;
333  for (Index j = 0; j < N; j++) deg += derived().coeff(j, i);
334  ret.coeffRef(i, i) += deg;
335  }
336 }
337 
338 /** Changes the size of matrix, maintaining its previous content as possible and
339  * padding with zeros where applicable.
340  * **WARNING**: MRPT's add-on method \a setSize() pads with zeros, while
341  * Eigen's \a resize() does NOT (new elements are undefined).
342  */
343 EIGEN_STRONG_INLINE void setSize(size_t row, size_t col)
344 {
345 #ifdef _DEBUG
346  if ((Derived::RowsAtCompileTime != Eigen::Dynamic &&
347  Derived::RowsAtCompileTime != int(row)) ||
348  (Derived::ColsAtCompileTime != Eigen::Dynamic &&
349  Derived::ColsAtCompileTime != int(col)))
350  {
351  std::stringstream ss;
352  ss << "setSize: Trying to change a fixed sized matrix from " << rows()
353  << "x" << cols() << " to " << row << "x" << col;
354  throw std::runtime_error(ss.str());
355  }
356 #endif
357  const Index oldCols = cols();
358  const Index oldRows = rows();
359  const int nNewCols = int(col) - int(cols());
360  const int nNewRows = int(row) - int(rows());
363  SizeAtCompileTime>::internal_resize(*this, row, col);
364  if (nNewCols > 0) derived().block(0, oldCols, row, nNewCols).setZero();
365  if (nNewRows > 0) derived().block(oldRows, 0, nNewRows, col).setZero();
366 }
367 
368 /** Efficiently computes only the biggest eigenvector of the matrix using the
369  * Power Method, and returns it in the passed vector "x". */
370 template <class OUTVECT>
372  OUTVECT& x, Scalar resolution = Scalar(0.01), size_t maxIterations = 6,
373  int* out_Iterations = nullptr,
374  float* out_estimatedResolution = nullptr) const
375 {
376  // Apply the iterative Power Method:
377  size_t iter = 0;
378  const Index n = rows();
379  x.resize(n);
380  x.setConstant(1); // Initially, set to all ones, for example...
381  Scalar dif;
382  do // Iterative loop:
383  {
384  Eigen::Matrix<Scalar, Derived::RowsAtCompileTime, 1> xx = (*this) * x;
385  xx *= Scalar(1.0 / xx.norm());
386  dif = (x - xx)
387  .array()
388  .abs()
389  .sum(); // Compute diference between iterations:
390  x = xx; // Set as current estimation:
391  iter++; // Iteration counter:
392  } while (iter < maxIterations && dif > resolution);
393  if (out_Iterations) *out_Iterations = static_cast<int>(iter);
394  if (out_estimatedResolution) *out_estimatedResolution = dif;
395 }
396 
397 /** Combined matrix power and assignment operator */
398 MatrixBase<Derived>& operator^=(const unsigned int pow)
399 {
400  if (pow == 0)
401  derived().setIdentity();
402  else
403  for (unsigned int i = 1; i < pow; i++) derived() *= derived();
404  return *this;
405 }
406 
407 /** Scalar power of all elements to a given power, this is diferent of ^
408  * operator. */
409 EIGEN_STRONG_INLINE void scalarPow(const Scalar s) { (*this) = array().pow(s); }
410 /** Checks for matrix type */
411 EIGEN_STRONG_INLINE bool isDiagonal() const
412 {
413  for (Index c = 0; c < cols(); c++)
414  for (Index r = 0; r < rows(); r++)
415  if (r != c && coeff(r, c) != 0) return false;
416  return true;
417 }
418 
419 /** Finds the maximum value in the diagonal of the matrix. */
420 EIGEN_STRONG_INLINE Scalar maximumDiagonal() const
421 {
422  return diagonal().maxCoeff();
423 }
424 
425 /** Computes the mean of the entire matrix
426  * \sa meanAndStdAll */
427 EIGEN_STRONG_INLINE double mean() const
428 {
429  if (size() == 0) throw std::runtime_error("mean: Empty container.");
430  return derived().sum() / static_cast<double>(size());
431 }
432 
433 /** Computes a row with the mean values of each column in the matrix and the
434  * associated vector with the standard deviation of each column.
435  * \sa mean,meanAndStdAll \exception std::exception If the matrix/vector is
436  * empty.
437  * \param unbiased_variance Standard deviation is sum(vals-mean)/K, with K=N-1
438  * or N for unbiased_variance=true or false, respectively.
439  */
440 template <class VEC>
442  VEC& outMeanVector, VEC& outStdVector,
443  const bool unbiased_variance = true) const
444 {
445  const size_t N = rows();
446  if (N == 0) throw std::runtime_error("meanAndStd: Empty container.");
447  const double N_inv = 1.0 / N;
448  const double N_ =
449  unbiased_variance ? (N > 1 ? 1.0 / (N - 1) : 1.0) : 1.0 / N;
450  outMeanVector.resize(cols());
451  outStdVector.resize(cols());
452  for (decltype(cols()) i = 0; i < cols(); i++)
453  {
454  outMeanVector[i] = this->col(i).array().sum() * N_inv;
455  outStdVector[i] = std::sqrt(
456  (this->col(i).array() - outMeanVector[i]).square().sum() * N_);
457  }
458 }
459 
460 /** Computes the mean and standard deviation of all the elements in the matrix
461  * as a whole.
462  * \sa mean,meanAndStd \exception std::exception If the matrix/vector is
463  * empty.
464  * \param unbiased_variance Standard deviation is sum(vals-mean)/K, with K=N-1
465  * or N for unbiased_variance=true or false, respectively.
466  */
468  double& outMean, double& outStd, const bool unbiased_variance = true) const
469 {
470  const size_t N = size();
471  if (N == 0) throw std::runtime_error("meanAndStdAll: Empty container.");
472  const double N_ =
473  unbiased_variance ? (N > 1 ? 1.0 / (N - 1) : 1.0) : 1.0 / N;
474  outMean = derived().array().sum() / static_cast<double>(size());
475  outStd = std::sqrt((this->array() - outMean).square().sum() * N_);
476 }
477 
478 /** Insert matrix "m" into this matrix at indices (r,c), that is,
479  * (*this)(r,c)=m(0,0) and so on */
480 template <typename MAT>
481 EIGEN_STRONG_INLINE void insertMatrix(size_t r, size_t c, const MAT& m)
482 {
483  derived().block(r, c, m.rows(), m.cols()) = m;
484 }
485 
486 template <typename MAT>
487 EIGEN_STRONG_INLINE void insertMatrixTranspose(size_t r, size_t c, const MAT& m)
488 {
489  derived().block(r, c, m.cols(), m.rows()) = m.adjoint();
490 }
491 
492 template <typename MAT>
493 EIGEN_STRONG_INLINE void insertRow(size_t nRow, const MAT& aRow)
494 {
495  this->row(nRow) = aRow;
496 }
497 template <typename MAT>
498 EIGEN_STRONG_INLINE void insertCol(size_t nCol, const MAT& aCol)
499 {
500  this->col(nCol) = aCol;
501 }
502 
503 template <typename R>
504 void insertRow(size_t nRow, const std::vector<R>& aRow)
505 {
506  if (static_cast<Index>(aRow.size()) != cols())
507  throw std::runtime_error(
508  "insertRow: Row size doesn't fit the size of this matrix.");
509  for (decltype(cols()) j = 0; j < cols(); j++) coeffRef(nRow, j) = aRow[j];
510 }
511 template <typename R>
512 void insertCol(size_t nCol, const std::vector<R>& aCol)
513 {
514  if (static_cast<Index>(aCol.size()) != rows())
515  throw std::runtime_error(
516  "insertRow: Row size doesn't fit the size of this matrix.");
517  for (decltype(cols()) j = 0; j < rows(); j++) coeffRef(j, nCol) = aCol[j];
518 }
519 
520 /** Remove columns of the matrix.*/
521 EIGEN_STRONG_INLINE void removeColumns(const std::vector<size_t>& idxsToRemove)
522 {
523  std::vector<size_t> idxs = idxsToRemove;
524  std::sort(idxs.begin(), idxs.end());
525  std::vector<size_t>::iterator itEnd = std::unique(idxs.begin(), idxs.end());
526  idxs.resize(itEnd - idxs.begin());
527 
528  unsafeRemoveColumns(idxs);
529 }
530 
531 /** Remove columns of the matrix. The unsafe version assumes that, the indices
532  * are sorted in ascending order. */
533 EIGEN_STRONG_INLINE void unsafeRemoveColumns(const std::vector<size_t>& idxs)
534 {
535  size_t k = 1;
536  for (std::vector<size_t>::const_reverse_iterator it = idxs.rbegin();
537  it != idxs.rend(); ++it, ++k)
538  {
539  const size_t nC = cols() - *it - k;
540  if (nC > 0)
541  derived().block(0, *it, rows(), nC) =
542  derived().block(0, *it + 1, rows(), nC).eval();
543  }
544  derived().conservativeResize(NoChange, cols() - idxs.size());
545 }
546 
547 /** Remove rows of the matrix. */
548 EIGEN_STRONG_INLINE void removeRows(const std::vector<size_t>& idxsToRemove)
549 {
550  std::vector<size_t> idxs = idxsToRemove;
551  std::sort(idxs.begin(), idxs.end());
552  std::vector<size_t>::iterator itEnd = std::unique(idxs.begin(), idxs.end());
553  idxs.resize(itEnd - idxs.begin());
554 
555  unsafeRemoveRows(idxs);
556 }
557 
558 /** Remove rows of the matrix. The unsafe version assumes that, the indices are
559  * sorted in ascending order. */
560 EIGEN_STRONG_INLINE void unsafeRemoveRows(const std::vector<size_t>& idxs)
561 {
562  size_t k = 1;
563  for (auto it = idxs.rbegin();
564  it != idxs.rend(); ++it, ++k)
565  {
566  const size_t nR = rows() - *it - k;
567  if (nR > 0)
568  derived().block(*it, 0, nR, cols()) =
569  derived().block(*it + 1, 0, nR, cols()).eval();
570  }
571  derived().conservativeResize(rows() - idxs.size(), NoChange);
572 }
573 
574 /** Transpose */
575 EIGEN_STRONG_INLINE const AdjointReturnType t() const
576 {
577  return derived().adjoint();
578 }
579 
580 EIGEN_STRONG_INLINE PlainObject inv() const
581 {
582  PlainObject outMat = derived().inverse().eval();
583  return outMat;
584 }
585 template <class MATRIX>
586 EIGEN_STRONG_INLINE void inv(MATRIX& outMat) const
587 {
588  outMat = derived().inverse().eval();
589 }
590 template <class MATRIX>
591 EIGEN_STRONG_INLINE void inv_fast(MATRIX& outMat) const
592 {
593  outMat = derived().inverse().eval();
594 }
595 EIGEN_STRONG_INLINE Scalar det() const { return derived().determinant(); }
596 /** @} */ // end miscelaneous
597 
598 /** @name MRPT plugin: Multiply and extra addition functions
599  @{ */
600 
601 EIGEN_STRONG_INLINE bool empty() const
602 {
603  return this->cols() == 0 || this->rows() == 0;
604 }
605 
606 /*! Add c (scalar) times A to this matrix: this += A * c */
607 template <typename OTHERMATRIX>
608 EIGEN_STRONG_INLINE void add_Ac(const OTHERMATRIX& m, const Scalar c)
609 {
610  (*this) += c * m;
611 }
612 /*! Substract c (scalar) times A to this matrix: this -= A * c */
613 template <typename OTHERMATRIX>
614 EIGEN_STRONG_INLINE void substract_Ac(const OTHERMATRIX& m, const Scalar c)
615 {
616  (*this) -= c * m;
617 }
618 
619 /*! Substract A transposed to this matrix: this -= A.adjoint() */
620 template <typename OTHERMATRIX>
621 EIGEN_STRONG_INLINE void substract_At(const OTHERMATRIX& m)
622 {
623  (*this) -= m.adjoint();
624 }
625 
626 /*! Substract n (integer) times A to this matrix: this -= A * n */
627 template <typename OTHERMATRIX>
628 EIGEN_STRONG_INLINE void substract_An(const OTHERMATRIX& m, const size_t n)
629 {
630  this->noalias() -= n * m;
631 }
632 
633 /*! this += A + A<sup>T</sup> */
634 template <typename OTHERMATRIX>
635 EIGEN_STRONG_INLINE void add_AAt(const OTHERMATRIX& A)
636 {
637  this->noalias() += A;
638  this->noalias() += A.adjoint();
639 }
640 
641 /*! this -= A + A<sup>T</sup> */
642 template <typename OTHERMATRIX>
643 EIGEN_STRONG_INLINE void substract_AAt(const OTHERMATRIX& A)
644 {
645  this->noalias() -= A;
646  this->noalias() -= A.adjoint();
647 }
648 
649 template <class MATRIX1, class MATRIX2>
650 EIGEN_STRONG_INLINE void multiply(
651  const MATRIX1& A, const MATRIX2& B) /*!< this = A * B */
652 {
653  (*this) = A * B;
654 }
655 
656 template <class MATRIX1, class MATRIX2>
657 EIGEN_STRONG_INLINE void multiply_AB(
658  const MATRIX1& A, const MATRIX2& B) /*!< this = A * B */
659 {
660  (*this) = A * B;
661 }
662 
663 template <typename MATRIX1, typename MATRIX2>
664 EIGEN_STRONG_INLINE void multiply_AtB(
665  const MATRIX1& A, const MATRIX2& B) /*!< this=A^t * B */
666 {
667  *this = A.adjoint() * B;
668 }
669 
670 /*! Computes the vector vOut = this * vIn, where "vIn" is a column vector of the
671  * appropriate length. */
672 template <typename OTHERVECTOR1, typename OTHERVECTOR2>
673 EIGEN_STRONG_INLINE void multiply_Ab(
674  const OTHERVECTOR1& vIn, OTHERVECTOR2& vOut,
675  bool accumToOutput = false) const
676 {
677  if (accumToOutput)
678  vOut.noalias() += (*this) * vIn;
679  else
680  vOut = (*this) * vIn;
681 }
682 
683 /*! Computes the vector vOut = this<sup>T</sup> * vIn, where "vIn" is a column
684  * vector of the appropriate length. */
685 template <typename OTHERVECTOR1, typename OTHERVECTOR2>
686 EIGEN_STRONG_INLINE void multiply_Atb(
687  const OTHERVECTOR1& vIn, OTHERVECTOR2& vOut,
688  bool accumToOutput = false) const
689 {
690  if (accumToOutput)
691  vOut.noalias() += this->adjoint() * vIn;
692  else
693  vOut = this->adjoint() * vIn;
694 }
695 
696 template <typename MAT_C, typename MAT_R>
697 EIGEN_STRONG_INLINE void multiply_HCHt(
698  const MAT_C& C, MAT_R& R, bool accumResultInOutput = false)
699  const /*!< R = this * C * this<sup>T</sup> */
700 {
701  if (accumResultInOutput)
702  R.noalias() += (*this) * C * this->adjoint();
703  else
704  R.noalias() = (*this) * C * this->adjoint();
705 }
706 
707 template <typename MAT_C, typename MAT_R>
708 EIGEN_STRONG_INLINE void multiply_HtCH(
709  const MAT_C& C, MAT_R& R, bool accumResultInOutput = false)
710  const /*!< R = this<sup>T</sup> * C * this */
711 {
712  if (accumResultInOutput)
713  R.noalias() += this->adjoint() * C * (*this);
714  else
715  R.noalias() = this->adjoint() * C * (*this);
716 }
717 
718 /*! R = H * C * H<sup>T</sup> (with a vector H and a symmetric matrix C) In fact
719  * when H is a vector, multiply_HCHt_scalar and multiply_HtCH_scalar are exactly
720  * equivalent */
721 template <typename MAT_C>
722 EIGEN_STRONG_INLINE Scalar multiply_HCHt_scalar(const MAT_C& C) const
723 {
724  return ((*this) * C * this->adjoint()).eval()(0, 0);
725 }
726 
727 /*! R = H<sup>T</sup> * C * H (with a vector H and a symmetric matrix C) In fact
728  * when H is a vector, multiply_HCHt_scalar and multiply_HtCH_scalar are exactly
729  * equivalent */
730 template <typename MAT_C>
731 EIGEN_STRONG_INLINE Scalar multiply_HtCH_scalar(const MAT_C& C) const
732 {
733  return (this->adjoint() * C * (*this)).eval()(0, 0);
734 }
735 
736 /*! this = C * C<sup>T</sup> * f (with a matrix C and a scalar f). */
737 template <typename MAT_A>
738 EIGEN_STRONG_INLINE void multiply_AAt_scalar(
739  const MAT_A& A, typename MAT_A::Scalar f)
740 {
741  *this = (A * A.adjoint()) * f;
742 }
743 
744 /*! this = C<sup>T</sup> * C * f (with a matrix C and a scalar f). */
745 template <typename MAT_A>
746 EIGEN_STRONG_INLINE void multiply_AtA_scalar(
747  const MAT_A& A, typename MAT_A::Scalar f)
748 {
749  *this = (A.adjoint() * A) * f;
750 }
751 
752 /*! this = A * skew(v), with \a v being a 3-vector (or 3-array) and skew(v) the
753  * skew symmetric matrix of v (see mrpt::math::skew_symmetric3) */
754 template <class MAT_A, class SKEW_3VECTOR>
755 void multiply_A_skew3(const MAT_A& A, const SKEW_3VECTOR& v)
756 {
757  mrpt::math::multiply_A_skew3(A, v, *this);
758 }
759 
760 /*! this = skew(v)*A, with \a v being a 3-vector (or 3-array) and skew(v) the
761  * skew symmetric matrix of v (see mrpt::math::skew_symmetric3) */
762 template <class SKEW_3VECTOR, class MAT_A>
763 void multiply_skew3_A(const SKEW_3VECTOR& v, const MAT_A& A)
764 {
765  mrpt::math::multiply_skew3_A(v, A, *this);
766 }
767 
768 /** outResult = this * A
769  */
770 template <class MAT_A, class MAT_OUT>
771 EIGEN_STRONG_INLINE void multiply_subMatrix(
772  const MAT_A& A, MAT_OUT& outResult, const size_t A_cols_offset,
773  const size_t A_rows_offset, const size_t A_col_count) const
774 {
775  outResult =
776  derived() *
777  A.block(A_rows_offset, A_cols_offset, derived().cols(), A_col_count);
778 }
779 
780 template <class MAT_A, class MAT_B, class MAT_C>
782  const MAT_A& A, const MAT_B& B, const MAT_C& C) /*!< this = A*B*C */
783 {
784  *this = A * B * C;
785 }
786 
787 template <class MAT_A, class MAT_B, class MAT_C>
789  const MAT_A& A, const MAT_B& B,
790  const MAT_C& C) /*!< this = A*B*(C<sup>T</sup>) */
791 {
792  *this = A * B * C.adjoint();
793 }
794 
795 template <class MAT_A, class MAT_B, class MAT_C>
797  const MAT_A& A, const MAT_B& B,
798  const MAT_C& C) /*!< this = A(<sup>T</sup>)*B*C */
799 {
800  *this = A.adjoint() * B * C;
801 }
802 
803 template <class MAT_A, class MAT_B>
804 EIGEN_STRONG_INLINE void multiply_ABt(
805  const MAT_A& A, const MAT_B& B) /*!< this = A * B<sup>T</sup> */
806 {
807  *this = A * B.adjoint();
808 }
809 
810 template <class MAT_A>
811 EIGEN_STRONG_INLINE void multiply_AAt(
812  const MAT_A& A) /*!< this = A * A<sup>T</sup> */
813 {
814  *this = A * A.adjoint();
815 }
816 
817 template <class MAT_A>
818 EIGEN_STRONG_INLINE void multiply_AtA(
819  const MAT_A& A) /*!< this = A<sup>T</sup> * A */
820 {
821  *this = A.adjoint() * A;
822 }
823 
824 template <class MAT_A, class MAT_B>
825 EIGEN_STRONG_INLINE void multiply_result_is_symmetric(
826  const MAT_A& A, const MAT_B& B) /*!< this = A * B (result is symmetric) */
827 {
828  *this = A * B;
829 }
830 
831 /** @} */ // end multiply functions
832 
833 /** @name MRPT plugin: Eigenvalue / Eigenvectors
834  @{ */
835 
836 /** [For square matrices only] Compute the eigenvectors and eigenvalues
837  * (sorted), both returned as matrices: eigenvectors are the columns in "eVecs",
838  * and eigenvalues in ascending order as the diagonal of "eVals".
839  * \note Warning: Only the real part of complex eigenvectors and eigenvalues
840  * are returned.
841  * \sa eigenVectorsSymmetric, eigenVectorsVec
842  * \return false on error
843  */
844 template <class MATRIX1, class MATRIX2>
845 EIGEN_STRONG_INLINE bool eigenVectors(MATRIX1& eVecs, MATRIX2& eVals) const;
846 // Implemented in eigen_plugins_impl.h (can't be here since
847 // Eigen::SelfAdjointEigenSolver isn't defined yet at this point.
848 
849 /** [For square matrices only] Compute the eigenvectors and eigenvalues
850  * (sorted), eigenvectors are the columns in "eVecs", and eigenvalues are
851  * returned in in ascending order in the vector "eVals".
852  * \note Warning: Only the real part of complex eigenvectors and eigenvalues
853  * are returned.
854  * \sa eigenVectorsSymmetric, eigenVectorsVec
855  * \return false on error
856  */
857 template <class MATRIX1, class VECTOR1>
858 EIGEN_STRONG_INLINE bool eigenVectorsVec(MATRIX1& eVecs, VECTOR1& eVals) const;
859 // Implemented in eigen_plugins_impl.h
860 
861 /** [For square matrices only] Compute the eigenvectors and eigenvalues
862  * (sorted), and return only the eigenvalues in the vector "eVals".
863  * \note Warning: Only the real part of complex eigenvectors and eigenvalues
864  * are returned.
865  * \sa eigenVectorsSymmetric, eigenVectorsVec
866  */
867 template <class VECTOR>
868 EIGEN_STRONG_INLINE void eigenValues(VECTOR& eVals) const
869 {
870  PlainObject eVecs;
871  eVecs.resizeLike(*this);
872  this->eigenVectorsVec(eVecs, eVals);
873 }
874 
875 /** [For symmetric matrices only] Compute the eigenvectors and eigenvalues (in
876  * no particular order), both returned as matrices: eigenvectors are the
877  * columns, and eigenvalues \sa eigenVectors
878  */
879 template <class MATRIX1, class MATRIX2>
880 EIGEN_STRONG_INLINE void eigenVectorsSymmetric(
881  MATRIX1& eVecs, MATRIX2& eVals) const;
882 // Implemented in eigen_plugins_impl.h (can't be here since
883 // Eigen::SelfAdjointEigenSolver isn't defined yet at this point.
884 
885 /** [For symmetric matrices only] Compute the eigenvectors and eigenvalues (in
886  * no particular order), both returned as matrices: eigenvectors are the
887  * columns, and eigenvalues \sa eigenVectorsVec
888  */
889 template <class MATRIX1, class VECTOR1>
890 EIGEN_STRONG_INLINE void eigenVectorsSymmetricVec(
891  MATRIX1& eVecs, VECTOR1& eVals) const;
892 // Implemented in eigen_plugins_impl.h
893 
894 /** @} */ // end eigenvalues
895 
896 /** @name MRPT plugin: Linear algebra & decomposition-based methods
897  @{ */
898 
899 /** Cholesky M=U<sup>T</sup> * U decomposition for simetric matrix (upper-half
900  * of the matrix will be actually ignored) */
901 template <class MATRIX>
902 EIGEN_STRONG_INLINE bool chol(MATRIX& U) const
903 {
904  Eigen::LLT<PlainObject> Chol =
905  derived().template selfadjointView<Eigen::Lower>().llt();
906  if (Chol.info() == Eigen::NoConvergence) return false;
907  U = PlainObject(Chol.matrixU());
908  return true;
909 }
910 
911 /** Gets the rank of the matrix via the Eigen::ColPivHouseholderQR method
912  * \param threshold If set to >0, it's used as threshold instead of Eigen's
913  * default one.
914  */
915 EIGEN_STRONG_INLINE size_t rank(double threshold = 0) const
916 {
917  Eigen::ColPivHouseholderQR<PlainObject> QR = this->colPivHouseholderQr();
918  if (threshold > 0) QR.setThreshold(threshold);
919  return QR.rank();
920 }
921 
922 /** @} */ // end linear algebra
923 
924 /** @name MRPT plugin: Scalar and element-wise extra operators
925  @{ */
926 
927 /** Scales all elements such as the minimum & maximum values are shifted to the
928  * given values */
929 void normalize(Scalar valMin, Scalar valMax)
930 {
931  if (size() == 0) return;
932  Scalar curMin, curMax;
933  minimum_maximum(curMin, curMax);
934  Scalar minMaxDelta = curMax - curMin;
935  if (minMaxDelta == 0) minMaxDelta = 1;
936  const Scalar minMaxDelta_ = (valMax - valMin) / minMaxDelta;
937  this->array() = (this->array() - curMin) * minMaxDelta_ + valMin;
938 }
939 //! \overload
940 inline void adjustRange(Scalar valMin, Scalar valMax)
941 {
942  normalize(valMin, valMax);
943 }
944 
945 /** @} */ // end Scalar
946 
947 /** Extract one row from the matrix into a row vector */
948 template <class OtherDerived>
949 EIGEN_STRONG_INLINE void extractRow(
950  size_t nRow, Eigen::EigenBase<OtherDerived>& v,
951  size_t startingCol = 0) const
952 {
953  v = derived().block(nRow, startingCol, 1, cols() - startingCol);
954 }
955 //! \overload
956 template <typename T>
957 inline void extractRow(
958  size_t nRow, std::vector<T>& v, size_t startingCol = 0) const
959 {
960  const size_t N = cols() - startingCol;
961  v.resize(N);
962  for (size_t i = 0; i < N; i++) v[i] = (*this)(nRow, startingCol + i);
963 }
964 /** Extract one row from the matrix into a column vector */
965 template <class VECTOR>
966 EIGEN_STRONG_INLINE void extractRowAsCol(
967  size_t nRow, VECTOR& v, size_t startingCol = 0) const
968 {
969  v = derived().adjoint().block(startingCol, nRow, cols() - startingCol, 1);
970 }
971 
972 /** Extract one column from the matrix into a column vector */
973 template <class VECTOR>
974 EIGEN_STRONG_INLINE void extractCol(
975  size_t nCol, VECTOR& v, size_t startingRow = 0) const
976 {
977  v = derived().block(startingRow, nCol, rows() - startingRow, 1);
978 }
979 //! \overload
980 template <typename T>
981 inline void extractCol(
982  size_t nCol, std::vector<T>& v, size_t startingRow = 0) const
983 {
984  const size_t N = rows() - startingRow;
985  v.resize(N);
986  for (size_t i = 0; i < N; i++) v[i] = (*this)(startingRow + i, nCol);
987 }
988 
989 template <class MATRIX>
990 EIGEN_STRONG_INLINE void extractMatrix(
991  const size_t firstRow, const size_t firstCol, MATRIX& m) const
992 {
993  m = derived().block(firstRow, firstCol, m.rows(), m.cols());
994 }
995 template <class MATRIX>
996 EIGEN_STRONG_INLINE void extractMatrix(
997  const size_t firstRow, const size_t firstCol, const size_t nRows,
998  const size_t nCols, MATRIX& m) const
999 {
1000  m.resize(nRows, nCols);
1001  m = derived().block(firstRow, firstCol, nRows, nCols);
1002 }
1003 
1004 /** Get a submatrix, given its bounds: first & last column and row (inclusive).
1005  */
1006 template <class MATRIX>
1007 EIGEN_STRONG_INLINE void extractSubmatrix(
1008  const size_t row_first, const size_t row_last, const size_t col_first,
1009  const size_t col_last, MATRIX& out) const
1010 {
1011  out.resize(row_last - row_first + 1, col_last - col_first + 1);
1012  out = derived().block(
1013  row_first, col_first, row_last - row_first + 1,
1014  col_last - col_first + 1);
1015 }
1016 
1017 /** Get a submatrix from a square matrix, by collecting the elements
1018  * M(idxs,idxs), where idxs is a sequence
1019  * {block_indices(i):block_indices(i)+block_size-1} for all "i" up to the size
1020  * of block_indices.
1021  * A perfect application of this method is in extracting covariance matrices
1022  * of a subset of variables from the full covariance matrix.
1023  * \sa extractSubmatrix, extractSubmatrixSymmetrical
1024  */
1025 template <class MATRIX>
1027  const size_t block_size, const std::vector<size_t>& block_indices,
1028  MATRIX& out) const
1029 {
1030  if (block_size < 1)
1031  throw std::runtime_error(
1032  "extractSubmatrixSymmetricalBlocks: block_size must be >=1");
1033  if (cols() != rows())
1034  throw std::runtime_error(
1035  "extractSubmatrixSymmetricalBlocks: Matrix is not square.");
1036 
1037  const size_t N = block_indices.size();
1038  const size_t nrows_out = N * block_size;
1039  out.resize(nrows_out, nrows_out);
1040  if (!N) return; // Done
1041  for (size_t dst_row_blk = 0; dst_row_blk < N; ++dst_row_blk)
1042  {
1043  for (size_t dst_col_blk = 0; dst_col_blk < N; ++dst_col_blk)
1044  {
1045 #if defined(_DEBUG)
1046  if (block_indices[dst_col_blk] * block_size + block_size - 1 >=
1047  size_t(cols()))
1048  throw std::runtime_error(
1049  "extractSubmatrixSymmetricalBlocks: Indices out of range!");
1050 #endif
1051  out.block(
1052  dst_row_blk * block_size, dst_col_blk * block_size, block_size,
1053  block_size) =
1054  derived().block(
1055  block_indices[dst_row_blk] * block_size,
1056  block_indices[dst_col_blk] * block_size, block_size,
1057  block_size);
1058  }
1059  }
1060 }
1061 
1062 /** Get a submatrix from a square matrix, by collecting the elements
1063  * M(idxs,idxs), where idxs is the sequence of indices passed as argument.
1064  * A perfect application of this method is in extracting covariance matrices
1065  * of a subset of variables from the full covariance matrix.
1066  * \sa extractSubmatrix, extractSubmatrixSymmetricalBlocks
1067  */
1068 template <class MATRIX>
1070  const std::vector<size_t>& indices, MATRIX& out) const
1071 {
1072  if (cols() != rows())
1073  throw std::runtime_error(
1074  "extractSubmatrixSymmetrical: Matrix is not square.");
1075 
1076  const size_t N = indices.size();
1077  const size_t nrows_out = N;
1078  out.resize(nrows_out, nrows_out);
1079  if (!N) return; // Done
1080  for (size_t dst_row_blk = 0; dst_row_blk < N; ++dst_row_blk)
1081  for (size_t dst_col_blk = 0; dst_col_blk < N; ++dst_col_blk)
1082  out.coeffRef(dst_row_blk, dst_col_blk) =
1083  this->coeff(indices[dst_row_blk], indices[dst_col_blk]);
1084 }
const float R
double Scalar
Definition: KmUtils.h:44
@ static_size
Definition: eigen_plugins.h:19
void find_index_max_value(size_t &u, size_t &v, Scalar &valMax) const
[VECTORS OR MATRICES] Finds the maximum value (and the corresponding zero-based index) from a given c...
EIGEN_STRONG_INLINE void eye()
Make the matrix an identity matrix
Definition: eigen_plugins.h:72
EIGEN_STRONG_INLINE Scalar multiply_HtCH_scalar(const MAT_C &C) const
EIGEN_STRONG_INLINE Scalar det() const
EIGEN_STRONG_INLINE void multiply_AAt_scalar(const MAT_A &A, typename MAT_A::Scalar f)
void multiply_ABCt(const MAT_A &A, const MAT_B &B, const MAT_C &C)
EIGEN_STRONG_INLINE Scalar maximum() const
[VECTORS OR MATRICES] Finds the maximum value
void multiply_ABC(const MAT_A &A, const MAT_B &B, const MAT_C &C)
EIGEN_STRONG_INLINE void eigenValues(VECTOR &eVals) const
[For square matrices only] Compute the eigenvectors and eigenvalues (sorted), and return only the eig...
void adjustRange(Scalar valMin, Scalar valMax)
This is an overloaded member function, provided for convenience. It differs from the above function o...
EIGEN_STRONG_INLINE iterator end()
Definition: eigen_plugins.h:30
EIGEN_STRONG_INLINE void zeros()
Set all elements to zero.
Definition: eigen_plugins.h:74
EIGEN_STRONG_INLINE size_t rank(double threshold=0) const
Gets the rank of the matrix via the Eigen::ColPivHouseholderQR method.
EIGEN_STRONG_INLINE void multiply(const MATRIX1 &A, const MATRIX2 &B)
void saveToTextFile(const std::string &file, mrpt::math::TMatrixTextFileFormat fileFormat=mrpt::math::MATRIX_FORMAT_ENG, bool appendMRPTHeader=false, const std::string &userHeader=std::string()) const
Save matrix to a text file, compatible with MATLAB text format (see also the methods of matrix classe...
EIGEN_STRONG_INLINE void multiply_AAt(const MAT_A &A)
EIGEN_STRONG_INLINE void inv_fast(MATRIX &outMat) const
EIGEN_STRONG_INLINE void swapRows(size_t i1, size_t i2)
EIGEN_STRONG_INLINE bool isDiagonal() const
Checks for matrix type.
EIGEN_STRONG_INLINE double mean() const
Computes the mean of the entire matrix.
EIGEN_STRONG_INLINE void substract_An(const OTHERMATRIX &m, const size_t n)
EIGEN_STRONG_INLINE void assign(const Scalar v)
Definition: eigen_plugins.h:48
EIGEN_STRONG_INLINE bool eigenVectorsVec(MATRIX1 &eVecs, VECTOR1 &eVals) const
[For square matrices only] Compute the eigenvectors and eigenvalues (sorted), eigenvectors are the co...
EIGEN_STRONG_INLINE bool empty() const
EIGEN_STRONG_INLINE void set_unsafe(const size_t row, const size_t col, const Scalar val)
Sets an element (Use with caution, bounds are not checked!)
EIGEN_STRONG_INLINE void extractSubmatrix(const size_t row_first, const size_t row_last, const size_t col_first, const size_t col_last, MATRIX &out) const
Get a submatrix, given its bounds: first & last column and row (inclusive).
EIGEN_STRONG_INLINE bool eigenVectors(MATRIX1 &eVecs, MATRIX2 &eVals) const
[For square matrices only] Compute the eigenvectors and eigenvalues (sorted), both returned as matric...
EIGEN_STRONG_INLINE void insertMatrixTranspose(size_t r, size_t c, const MAT &m)
bool fromMatlabStringFormat(const std::string &s, std::ostream *dump_errors_here=nullptr)
Read a matrix from a string in Matlab-like format, for example "[1 0 2; 0 4 -1]" The string must star...
void largestEigenvector(OUTVECT &x, Scalar resolution=Scalar(0.01), size_t maxIterations=6, int *out_Iterations=nullptr, float *out_estimatedResolution=nullptr) const
Efficiently computes only the biggest eigenvector of the matrix using the Power Method,...
EIGEN_STRONG_INLINE Scalar multiply_HCHt_scalar(const MAT_C &C) const
EIGEN_STRONG_INLINE void extractRowAsCol(size_t nRow, VECTOR &v, size_t startingCol=0) const
Extract one row from the matrix into a column vector.
EIGEN_STRONG_INLINE void fill(const Scalar v)
Definition: eigen_plugins.h:46
MatrixBase< Derived > & operator^=(const unsigned int pow)
Combined matrix power and assignment operator.
void meanAndStd(VEC &outMeanVector, VEC &outStdVector, const bool unbiased_variance=true) const
Computes a row with the mean values of each column in the matrix and the associated vector with the s...
void multiply_A_skew3(const MAT_A &A, const SKEW_3VECTOR &v)
EIGEN_STRONG_INLINE void scalarPow(const Scalar s)
Scalar power of all elements to a given power, this is diferent of ^ operator.
EIGEN_STRONG_INLINE void unsafeRemoveRows(const std::vector< size_t > &idxs)
Remove rows of the matrix.
EIGEN_STRONG_INLINE Scalar sumAll() const
EIGEN_STRONG_INLINE void multiplyRowByScalar(size_t r, Scalar s)
EIGEN_STRONG_INLINE bool chol(MATRIX &U) const
Cholesky M=UT * U decomposition for simetric matrix (upper-half of the matrix will be actually ignore...
void multiply_skew3_A(const SKEW_3VECTOR &v, const MAT_A &A)
EIGEN_STRONG_INLINE void add_AAt(const OTHERMATRIX &A)
EIGEN_STRONG_INLINE bool isSquare() const
EIGEN_STRONG_INLINE void ones(const size_t row, const size_t col)
Resize matrix and set all elements to one.
Definition: eigen_plugins.h:82
EIGEN_STRONG_INLINE Scalar * get_unsafe_row(size_t row)
Fast but unsafe method to obtain a pointer to a given row of the matrix (Use only in time critical ap...
Definition: eigen_plugins.h:93
EIGEN_STRONG_INLINE Scalar minimum() const
[VECTORS OR MATRICES] Finds the minimum value
EIGEN_STRONG_INLINE void add_Ac(const OTHERMATRIX &m, const Scalar c)
EIGEN_STRONG_INLINE void unit(const size_t nRows, const Scalar diag_vals)
Make the matrix an identity matrix (the diagonal values can be 1.0 or any other value)
Definition: eigen_plugins.h:58
EIGEN_STRONG_INLINE void multiply_Atb(const OTHERVECTOR1 &vIn, OTHERVECTOR2 &vOut, bool accumToOutput=false) const
EIGEN_STRONG_INLINE void extractRow(size_t nRow, Eigen::EigenBase< OtherDerived > &v, size_t startingCol=0) const
Extract one row from the matrix into a row vector.
EIGEN_STRONG_INLINE void multiply_AtA_scalar(const MAT_A &A, typename MAT_A::Scalar f)
EIGEN_STRONG_INLINE void eigenVectorsSymmetric(MATRIX1 &eVecs, MATRIX2 &eVals) const
[For symmetric matrices only] Compute the eigenvectors and eigenvalues (in no particular order),...
EIGEN_STRONG_INLINE void insertMatrix(size_t r, size_t c, const MAT &m)
Insert matrix "m" into this matrix at indices (r,c), that is, (*this)(r,c)=m(0,0) and so on.
EIGEN_STRONG_INLINE void minimum_maximum(Scalar &out_min, Scalar &out_max) const
[VECTORS OR MATRICES] Compute the minimum and maximum of a container at once
EIGEN_STRONG_INLINE void multiply_AB(const MATRIX1 &A, const MATRIX2 &B)
EIGEN_STRONG_INLINE void multiply_HCHt(const MAT_C &C, MAT_R &R, bool accumResultInOutput=false) const
EIGEN_STRONG_INLINE void multiply_AtB(const MATRIX1 &A, const MATRIX2 &B)
EIGEN_STRONG_INLINE void setSize(size_t row, size_t col)
Changes the size of matrix, maintaining its previous content as possible and padding with zeros where...
EIGEN_STRONG_INLINE void unsafeRemoveColumns(const std::vector< size_t > &idxs)
Remove columns of the matrix.
EIGEN_STRONG_INLINE PlainObject inv() const
void meanAndStdAll(double &outMean, double &outStd, const bool unbiased_variance=true) const
Computes the mean and standard deviation of all the elements in the matrix as a whole.
EIGEN_STRONG_INLINE void insertRow(size_t nRow, const MAT &aRow)
EIGEN_STRONG_INLINE Scalar squareNorm() const
Compute the square norm of a vector/array/matrix (the Euclidean distance to the origin,...
EIGEN_STRONG_INLINE void extractMatrix(const size_t firstRow, const size_t firstCol, MATRIX &m) const
EIGEN_STRONG_INLINE void multiplyColumnByScalar(size_t c, Scalar s)
EIGEN_STRONG_INLINE void push_back(Scalar val)
Insert an element at the end of the container (for 1D vectors/arrays)
void normalize(Scalar valMin, Scalar valMax)
Scales all elements such as the minimum & maximum values are shifted to the given values.
EIGEN_STRONG_INLINE Scalar maximumDiagonal() const
Finds the maximum value in the diagonal of the matrix.
EIGEN_STRONG_INLINE void eigenVectorsSymmetricVec(MATRIX1 &eVecs, VECTOR1 &eVals) const
[For symmetric matrices only] Compute the eigenvectors and eigenvalues (in no particular order),...
EIGEN_STRONG_INLINE void multiply_Ab(const OTHERVECTOR1 &vIn, OTHERVECTOR2 &vOut, bool accumToOutput=false) const
EIGEN_STRONG_INLINE Scalar norm_inf() const
Compute the norm-infinite of a vector ($f[ ||\mathbf{v}||_\infnty $f]), ie the maximum absolute value...
Scalar * iterator
Definition: eigen_plugins.h:26
EIGEN_STRONG_INLINE const AdjointReturnType t() const
Transpose.
void extractSubmatrixSymmetricalBlocks(const size_t block_size, const std::vector< size_t > &block_indices, MATRIX &out) const
Get a submatrix from a square matrix, by collecting the elements M(idxs,idxs), where idxs is a sequen...
EIGEN_STRONG_INLINE void removeRows(const std::vector< size_t > &idxsToRemove)
Remove rows of the matrix.
EIGEN_STRONG_INLINE void multiply_ABt(const MAT_A &A, const MAT_B &B)
EIGEN_STRONG_INLINE void substract_At(const OTHERMATRIX &m)
EIGEN_STRONG_INLINE iterator begin()
Definition: eigen_plugins.h:29
std::string inMatlabFormat(const size_t decimal_digits=6) const
Dump matrix in matlab format.
const Scalar * const_iterator
Definition: eigen_plugins.h:27
EIGEN_STRONG_INLINE void substract_AAt(const OTHERMATRIX &A)
EIGEN_STRONG_INLINE Scalar get_unsafe(const size_t row, const size_t col) const
Read-only access to one element (Use with caution, bounds are not checked!)
void loadFromTextFile(const std::string &file)
Load matrix from a text file, compatible with MATLAB text format.
EIGEN_STRONG_INLINE void substract_Ac(const OTHERMATRIX &m, const Scalar c)
EIGEN_STRONG_INLINE void removeColumns(const std::vector< size_t > &idxsToRemove)
Remove columns of the matrix.
EIGEN_STRONG_INLINE void multiply_HtCH(const MAT_C &C, MAT_R &R, bool accumResultInOutput=false) const
void multiply_AtBC(const MAT_A &A, const MAT_B &B, const MAT_C &C)
EIGEN_STRONG_INLINE void swapCols(size_t i1, size_t i2)
EIGEN_STRONG_INLINE void multiply_result_is_symmetric(const MAT_A &A, const MAT_B &B)
EIGEN_STRONG_INLINE void insertCol(size_t nCol, const MAT &aCol)
EIGEN_STRONG_INLINE void multiply_subMatrix(const MAT_A &A, MAT_OUT &outResult, const size_t A_cols_offset, const size_t A_rows_offset, const size_t A_col_count) const
outResult = this * A
EIGEN_STRONG_INLINE void laplacian(Eigen::MatrixBase< OtherDerived > &ret) const
Computes the laplacian of this square graph weight matrix.
EIGEN_STRONG_INLINE void extractCol(size_t nCol, VECTOR &v, size_t startingRow=0) const
Extract one column from the matrix into a column vector.
EIGEN_STRONG_INLINE bool isSingular(const Scalar absThreshold=0) const
void extractSubmatrixSymmetrical(const std::vector< size_t > &indices, MATRIX &out) const
Get a submatrix from a square matrix, by collecting the elements M(idxs,idxs), where idxs is the sequ...
EIGEN_STRONG_INLINE size_t countNonZero() const
EIGEN_STRONG_INLINE void multiply_AtA(const MAT_A &A)
GLenum GLsizei n
Definition: glext.h:5074
GLenum GLenum GLvoid * row
Definition: glext.h:3576
const GLdouble * v
Definition: glext.h:3678
const GLubyte * c
Definition: glext.h:6313
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: glext.h:3547
GLenum GLint x
Definition: glext.h:3538
GLdouble GLdouble GLdouble r
Definition: glext.h:3705
GLsizeiptr size
Definition: glext.h:3923
GLdouble s
Definition: glext.h:3676
GLuint GLuint GLsizei GLenum const GLvoid * indices
Definition: glext.h:3529
GLsizei const GLchar ** string
Definition: glext.h:4101
int val
Definition: mrpt_jpeglib.h:955
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...
Definition: ops_matrices.h:190
TMatrixTextFileFormat
Definition: math_frwds.h:61
@ MATRIX_FORMAT_ENG
engineering format 'e'
Definition: math_frwds.h:63
CONTAINER::Scalar sum(const CONTAINER &v)
Computes the sum of all the elements.
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...
Definition: ops_matrices.h:166
T square(const T x)
Inline function for the square of a number.
Internal resize which compiles to nothing on fixed-size matrices.
Definition: math_frwds.h:40



Page generated by Doxygen 1.9.1 for MRPT 1.9.9 Git: 814d80880 Fri Aug 24 01:51:28 2018 +0200 at mar 26 may 2026 12:30:59 CEST