Transpositions.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2010-2011 Gael Guennebaud <gael.guennebaud@inria.fr>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_TRANSPOSITIONS_H
11 #define EIGEN_TRANSPOSITIONS_H
12 
13 namespace Eigen {
14 
15 template<typename Derived>
17 {
19 
20  public:
21 
22  typedef typename Traits::IndicesType IndicesType;
24  typedef Eigen::Index Index;
25 
27  Derived& derived() { return *static_cast<Derived*>(this); }
29  const Derived& derived() const { return *static_cast<const Derived*>(this); }
30 
32  template<typename OtherDerived>
34  {
35  indices() = other.indices();
36  return derived();
37  }
38 
41  Index size() const { return indices().size(); }
44  Index rows() const { return indices().size(); }
47  Index cols() const { return indices().size(); }
48 
51  inline const StorageIndex& coeff(Index i) const { return indices().coeff(i); }
53  inline StorageIndex& coeffRef(Index i) { return indices().coeffRef(i); }
55  inline const StorageIndex& operator()(Index i) const { return indices()(i); }
57  inline StorageIndex& operator()(Index i) { return indices()(i); }
59  inline const StorageIndex& operator[](Index i) const { return indices()(i); }
61  inline StorageIndex& operator[](Index i) { return indices()(i); }
62 
65  const IndicesType& indices() const { return derived().indices(); }
68  IndicesType& indices() { return derived().indices(); }
69 
71  inline void resize(Index newSize)
72  {
73  indices().resize(newSize);
74  }
75 
77  void setIdentity()
78  {
79  for(StorageIndex i = 0; i < indices().size(); ++i)
80  coeffRef(i) = i;
81  }
82 
83  // FIXME: do we want such methods ?
84  // might be useful when the target matrix expression is complex, e.g.:
85  // object.matrix().block(..,..,..,..) = trans * object.matrix().block(..,..,..,..);
86  /*
87  template<typename MatrixType>
88  void applyForwardToRows(MatrixType& mat) const
89  {
90  for(Index k=0 ; k<size() ; ++k)
91  if(m_indices(k)!=k)
92  mat.row(k).swap(mat.row(m_indices(k)));
93  }
94 
95  template<typename MatrixType>
96  void applyBackwardToRows(MatrixType& mat) const
97  {
98  for(Index k=size()-1 ; k>=0 ; --k)
99  if(m_indices(k)!=k)
100  mat.row(k).swap(mat.row(m_indices(k)));
101  }
102  */
103 
107 
111 
112  protected:
113 };
114 
115 namespace internal {
116 template<int SizeAtCompileTime, int MaxSizeAtCompileTime, typename _StorageIndex>
117 struct traits<Transpositions<SizeAtCompileTime,MaxSizeAtCompileTime,_StorageIndex> >
118  : traits<PermutationMatrix<SizeAtCompileTime,MaxSizeAtCompileTime,_StorageIndex> >
119 {
122 };
123 }
124 
154 template<int SizeAtCompileTime, int MaxSizeAtCompileTime, typename _StorageIndex>
155 class Transpositions : public TranspositionsBase<Transpositions<SizeAtCompileTime,MaxSizeAtCompileTime,_StorageIndex> >
156 {
158  public:
159 
163 
164  inline Transpositions() {}
165 
167  template<typename OtherDerived>
169  : m_indices(other.indices()) {}
170 
172  template<typename Other>
173  explicit inline Transpositions(const MatrixBase<Other>& indices) : m_indices(indices)
174  {}
175 
177  template<typename OtherDerived>
179  {
180  return Base::operator=(other);
181  }
182 
185  inline Transpositions(Index size) : m_indices(size)
186  {}
187 
190  const IndicesType& indices() const { return m_indices; }
193  IndicesType& indices() { return m_indices; }
194 
195  protected:
196 
197  IndicesType m_indices;
198 };
199 
200 
201 namespace internal {
202 template<int SizeAtCompileTime, int MaxSizeAtCompileTime, typename _StorageIndex, int _PacketAccess>
203 struct traits<Map<Transpositions<SizeAtCompileTime,MaxSizeAtCompileTime,_StorageIndex>,_PacketAccess> >
204  : traits<PermutationMatrix<SizeAtCompileTime,MaxSizeAtCompileTime,_StorageIndex> >
205 {
207  typedef _StorageIndex StorageIndex;
209 };
210 }
211 
212 template<int SizeAtCompileTime, int MaxSizeAtCompileTime, typename _StorageIndex, int PacketAccess>
213 class Map<Transpositions<SizeAtCompileTime,MaxSizeAtCompileTime,_StorageIndex>,PacketAccess>
214  : public TranspositionsBase<Map<Transpositions<SizeAtCompileTime,MaxSizeAtCompileTime,_StorageIndex>,PacketAccess> >
215 {
217  public:
218 
220  typedef typename Traits::IndicesType IndicesType;
222 
223  explicit inline Map(const StorageIndex* indicesPtr)
224  : m_indices(indicesPtr)
225  {}
226 
227  inline Map(const StorageIndex* indicesPtr, Index size)
228  : m_indices(indicesPtr,size)
229  {}
230 
232  template<typename OtherDerived>
234  {
235  return Base::operator=(other);
236  }
237 
238  #ifndef EIGEN_PARSED_BY_DOXYGEN
239 
243  {
244  m_indices = other.m_indices;
245  return *this;
246  }
247  #endif
248 
251  const IndicesType& indices() const { return m_indices; }
252 
255  IndicesType& indices() { return m_indices; }
256 
257  protected:
258 
259  IndicesType m_indices;
260 };
261 
262 namespace internal {
263 template<typename _IndicesType>
264 struct traits<TranspositionsWrapper<_IndicesType> >
265  : traits<PermutationWrapper<_IndicesType> >
266 {
268 };
269 }
270 
271 template<typename _IndicesType>
273  : public TranspositionsBase<TranspositionsWrapper<_IndicesType> >
274 {
276  public:
277 
279  typedef typename Traits::IndicesType IndicesType;
281 
282  explicit inline TranspositionsWrapper(IndicesType& indices)
283  : m_indices(indices)
284  {}
285 
287  template<typename OtherDerived>
289  {
290  return Base::operator=(other);
291  }
292 
295  const IndicesType& indices() const { return m_indices; }
296 
299  IndicesType& indices() { return m_indices; }
300 
301  protected:
302 
303  typename IndicesType::Nested m_indices;
304 };
305 
306 
307 
310 template<typename MatrixDerived, typename TranspositionsDerived>
314  const TranspositionsBase<TranspositionsDerived>& transpositions)
315 {
317  (matrix.derived(), transpositions.derived());
318 }
319 
322 template<typename TranspositionsDerived, typename MatrixDerived>
327 {
329  (transpositions.derived(), matrix.derived());
330 }
331 
332 // Template partial specialization for transposed/inverse transpositions
333 
334 namespace internal {
335 
336 template<typename Derived>
338  : traits<Derived>
339 {};
340 
341 } // end namespace internal
342 
343 template<typename TranspositionsDerived>
344 class Transpose<TranspositionsBase<TranspositionsDerived> >
345 {
346  typedef TranspositionsDerived TranspositionType;
347  typedef typename TranspositionType::IndicesType IndicesType;
348  public:
349 
350  explicit Transpose(const TranspositionType& t) : m_transpositions(t) {}
351 
353  Index size() const EIGEN_NOEXCEPT { return m_transpositions.size(); }
355  Index rows() const EIGEN_NOEXCEPT { return m_transpositions.size(); }
357  Index cols() const EIGEN_NOEXCEPT { return m_transpositions.size(); }
358 
361  template<typename OtherDerived> friend
364  {
365  return Product<OtherDerived, Transpose, AliasFreeProduct>(matrix.derived(), trt);
366  }
367 
370  template<typename OtherDerived>
373  {
374  return Product<Transpose, OtherDerived, AliasFreeProduct>(*this, matrix.derived());
375  }
376 
378  const TranspositionType& nestedExpression() const { return m_transpositions; }
379 
380  protected:
381  const TranspositionType& m_transpositions;
382 };
383 
384 } // end namespace Eigen
385 
386 #endif // EIGEN_TRANSPOSITIONS_H
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
SCALAR Scalar
Definition: bench_gemm.cpp:46
internal::traits< Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > >::Scalar Scalar
Traits::IndicesType IndicesType
Expression of the product of two arbitrary matrices or vectors.
Definition: Product.h:71
Derived & operator=(const TranspositionsBase< OtherDerived > &other)
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:94
Expression of the transpose of a matrix.
Definition: Transpose.h:52
Traits::IndicesType IndicesType
EIGEN_DEVICE_FUNC const Derived & derived() const
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
EIGEN_DEVICE_FUNC IndicesType & indices()
IndicesType::Scalar StorageIndex
Transpositions(const TranspositionsBase< OtherDerived > &other)
internal::traits< Transpositions > Traits
Traits::IndicesType IndicesType
EIGEN_DEVICE_FUNC const StorageIndex & coeff(Index i) const
StorageIndex & coeffRef(Index i)
Transpose< TranspositionsBase > inverse() const
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index size() const EIGEN_NOEXCEPT
EIGEN_DEVICE_FUNC const IndicesType & indices() const
const Product< Transpose, OtherDerived, AliasFreeProduct > operator*(const MatrixBase< OtherDerived > &matrix) const
const StorageIndex & operator[](Index i) const
EIGEN_DEVICE_FUNC const TranspositionType & nestedExpression() const
#define EIGEN_NOEXCEPT
Definition: Macros.h:1418
EIGEN_DEVICE_FUNC const IndicesType & indices() const
EIGEN_DEVICE_FUNC const IndicesType & indices() const
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74
Transpositions(Index size)
EIGEN_DEVICE_FUNC IndicesType & indices()
TranspositionsWrapper(IndicesType &indices)
#define EIGEN_CONSTEXPR
Definition: Macros.h:787
EIGEN_DEVICE_FUNC Index size() const
void resize(Index newSize)
EIGEN_DEVICE_FUNC const Product< MatrixDerived, PermutationDerived, AliasFreeProduct > operator*(const MatrixBase< MatrixDerived > &matrix, const PermutationBase< PermutationDerived > &permutation)
Transpositions(const MatrixBase< Other > &indices)
TranspositionsBase< TranspositionsWrapper > Base
IndicesType::Scalar StorageIndex
Transpose< TranspositionsBase > transpose() const
internal::traits< TranspositionsWrapper > Traits
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:976
StorageIndex & operator()(Index i)
Transpositions & operator=(const TranspositionsBase< OtherDerived > &other)
Map< const Matrix< _StorageIndex, SizeAtCompileTime, 1, 0, MaxSizeAtCompileTime, 1 >, _PacketAccess > IndicesType
StorageIndex & operator[](Index i)
Matrix< _StorageIndex, SizeAtCompileTime, 1, 0, MaxSizeAtCompileTime, 1 > IndicesType
TranspositionsWrapper & operator=(const TranspositionsBase< OtherDerived > &other)
EIGEN_DEVICE_FUNC Index rows() const
EIGEN_DEVICE_FUNC IndicesType & indices()
friend const Product< OtherDerived, Transpose, AliasFreeProduct > operator*(const MatrixBase< OtherDerived > &matrix, const Transpose &trt)
EIGEN_DEVICE_FUNC Index cols() const
IndicesType::Scalar StorageIndex
const StorageIndex & operator()(Index i) const
Map< Matrix< T, Dynamic, Dynamic, ColMajor >, 0, OuterStride<> > matrix(T *data, int rows, int cols, int stride)
The matrix class, also used for vectors and row-vectors.
TranspositionsBase< Transpositions > Base
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
EIGEN_DEVICE_FUNC Derived & derived()
Represents a sequence of transpositions (row/column interchange)
IndicesType::Nested m_indices
Point2 t(10, 10)
internal::traits< Derived > Traits


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:40:26