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 #include "./InternalHeaderCheck.h"
14 
15 namespace Eigen {
16 
17 template<typename Derived>
19 {
20  typedef internal::traits<Derived> Traits;
21 
22  public:
23 
24  typedef typename Traits::IndicesType IndicesType;
25  typedef typename IndicesType::Scalar StorageIndex;
26  typedef Eigen::Index Index;
27 
29  Derived& derived() { return *static_cast<Derived*>(this); }
31  const Derived& derived() const { return *static_cast<const Derived*>(this); }
32 
34  template<typename OtherDerived>
36  {
37  indices() = other.indices();
38  return derived();
39  }
40 
43  Index size() const { return indices().size(); }
46  Index rows() const { return indices().size(); }
49  Index cols() const { return indices().size(); }
50 
53  inline const StorageIndex& coeff(Index i) const { return indices().coeff(i); }
55  inline StorageIndex& coeffRef(Index i) { return indices().coeffRef(i); }
57  inline const StorageIndex& operator()(Index i) const { return indices()(i); }
59  inline StorageIndex& operator()(Index i) { return indices()(i); }
61  inline const StorageIndex& operator[](Index i) const { return indices()(i); }
63  inline StorageIndex& operator[](Index i) { return indices()(i); }
64 
67  const IndicesType& indices() const { return derived().indices(); }
70  IndicesType& indices() { return derived().indices(); }
71 
73  inline void resize(Index newSize)
74  {
75  indices().resize(newSize);
76  }
77 
79  void setIdentity()
80  {
81  for(StorageIndex i = 0; i < indices().size(); ++i)
82  coeffRef(i) = i;
83  }
84 
85  // FIXME: do we want such methods ?
86  // might be useful when the target matrix expression is complex, e.g.:
87  // object.matrix().block(..,..,..,..) = trans * object.matrix().block(..,..,..,..);
88  /*
89  template<typename MatrixType>
90  void applyForwardToRows(MatrixType& mat) const
91  {
92  for(Index k=0 ; k<size() ; ++k)
93  if(m_indices(k)!=k)
94  mat.row(k).swap(mat.row(m_indices(k)));
95  }
96 
97  template<typename MatrixType>
98  void applyBackwardToRows(MatrixType& mat) const
99  {
100  for(Index k=size()-1 ; k>=0 ; --k)
101  if(m_indices(k)!=k)
102  mat.row(k).swap(mat.row(m_indices(k)));
103  }
104  */
105 
109 
113 
114  protected:
115 };
116 
117 namespace internal {
118 template<int SizeAtCompileTime, int MaxSizeAtCompileTime, typename StorageIndex_>
119 struct traits<Transpositions<SizeAtCompileTime,MaxSizeAtCompileTime,StorageIndex_> >
120  : traits<PermutationMatrix<SizeAtCompileTime,MaxSizeAtCompileTime,StorageIndex_> >
121 {
122  typedef Matrix<StorageIndex_, SizeAtCompileTime, 1, 0, MaxSizeAtCompileTime, 1> IndicesType;
123  typedef TranspositionsStorage StorageKind;
124 };
125 }
126 
156 template<int SizeAtCompileTime, int MaxSizeAtCompileTime, typename StorageIndex_>
157 class Transpositions : public TranspositionsBase<Transpositions<SizeAtCompileTime,MaxSizeAtCompileTime,StorageIndex_> >
158 {
159  typedef internal::traits<Transpositions> Traits;
160  public:
161 
163  typedef typename Traits::IndicesType IndicesType;
164  typedef typename IndicesType::Scalar StorageIndex;
165 
166  inline Transpositions() {}
167 
169  template<typename OtherDerived>
171  : m_indices(other.indices()) {}
172 
174  template<typename Other>
176  {}
177 
179  template<typename OtherDerived>
181  {
182  return Base::operator=(other);
183  }
184 
188  {}
189 
192  const IndicesType& indices() const { return m_indices; }
196 
197  protected:
198 
200 };
201 
202 
203 namespace internal {
204 template<int SizeAtCompileTime, int MaxSizeAtCompileTime, typename StorageIndex_, int PacketAccess_>
205 struct traits<Map<Transpositions<SizeAtCompileTime,MaxSizeAtCompileTime,StorageIndex_>,PacketAccess_> >
206  : traits<PermutationMatrix<SizeAtCompileTime,MaxSizeAtCompileTime,StorageIndex_> >
207 {
209  typedef StorageIndex_ StorageIndex;
210  typedef TranspositionsStorage StorageKind;
211 };
212 }
213 
214 template<int SizeAtCompileTime, int MaxSizeAtCompileTime, typename StorageIndex_, int PacketAccess>
215 class Map<Transpositions<SizeAtCompileTime,MaxSizeAtCompileTime,StorageIndex_>,PacketAccess>
216  : public TranspositionsBase<Map<Transpositions<SizeAtCompileTime,MaxSizeAtCompileTime,StorageIndex_>,PacketAccess> >
217 {
218  typedef internal::traits<Map> Traits;
219  public:
220 
222  typedef typename Traits::IndicesType IndicesType;
223  typedef typename IndicesType::Scalar StorageIndex;
224 
225  explicit inline Map(const StorageIndex* indicesPtr)
226  : m_indices(indicesPtr)
227  {}
228 
229  inline Map(const StorageIndex* indicesPtr, Index size)
230  : m_indices(indicesPtr,size)
231  {}
232 
234  template<typename OtherDerived>
236  {
237  return Base::operator=(other);
238  }
239 
240  #ifndef EIGEN_PARSED_BY_DOXYGEN
244  Map& operator=(const Map& other)
245  {
246  m_indices = other.m_indices;
247  return *this;
248  }
249  #endif
250 
253  const IndicesType& indices() const { return m_indices; }
254 
257  IndicesType& indices() { return m_indices; }
258 
259  protected:
260 
262 };
263 
264 namespace internal {
265 template<typename IndicesType_>
266 struct traits<TranspositionsWrapper<IndicesType_> >
267  : traits<PermutationWrapper<IndicesType_> >
268 {
269  typedef TranspositionsStorage StorageKind;
270 };
271 }
272 
273 template<typename IndicesType_>
275  : public TranspositionsBase<TranspositionsWrapper<IndicesType_> >
276 {
277  typedef internal::traits<TranspositionsWrapper> Traits;
278  public:
279 
281  typedef typename Traits::IndicesType IndicesType;
282  typedef typename IndicesType::Scalar StorageIndex;
283 
285  : m_indices(indices)
286  {}
287 
289  template<typename OtherDerived>
291  {
292  return Base::operator=(other);
293  }
294 
297  const IndicesType& indices() const { return m_indices; }
298 
302 
303  protected:
304 
305  typename IndicesType::Nested m_indices;
306 };
307 
308 
309 
312 template<typename MatrixDerived, typename TranspositionsDerived>
316  const TranspositionsBase<TranspositionsDerived>& transpositions)
317 {
319  (matrix.derived(), transpositions.derived());
320 }
321 
324 template<typename TranspositionsDerived, typename MatrixDerived>
326 const Product<TranspositionsDerived, MatrixDerived, AliasFreeProduct>
328  const MatrixBase<MatrixDerived>& matrix)
329 {
331  (transpositions.derived(), matrix.derived());
332 }
333 
334 // Template partial specialization for transposed/inverse transpositions
335 
336 namespace internal {
337 
338 template<typename Derived>
339 struct traits<Transpose<TranspositionsBase<Derived> > >
340  : traits<Derived>
341 {};
342 
343 } // end namespace internal
344 
345 template<typename TranspositionsDerived>
346 class Transpose<TranspositionsBase<TranspositionsDerived> >
347 {
348  typedef TranspositionsDerived TranspositionType;
349  typedef typename TranspositionType::IndicesType IndicesType;
350  public:
351 
352  explicit Transpose(const TranspositionType& t) : m_transpositions(t) {}
353 
355  Index size() const EIGEN_NOEXCEPT { return m_transpositions.size(); }
357  Index rows() const EIGEN_NOEXCEPT { return m_transpositions.size(); }
359  Index cols() const EIGEN_NOEXCEPT { return m_transpositions.size(); }
360 
363  template<typename OtherDerived> friend
365  operator*(const MatrixBase<OtherDerived>& matrix, const Transpose& trt)
366  {
368  }
369 
372  template<typename OtherDerived>
374  operator*(const MatrixBase<OtherDerived>& matrix) const
375  {
377  }
378 
380  const TranspositionType& nestedExpression() const { return m_transpositions; }
381 
382  protected:
384 };
385 
386 } // end namespace Eigen
387 
388 #endif // EIGEN_TRANSPOSITIONS_H
#define EIGEN_NOEXCEPT
Definition: Macros.h:1260
#define EIGEN_CONSTEXPR
Definition: Macros.h:747
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:883
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:98
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:52
Expression of the product of two arbitrary matrices or vectors.
Definition: Product.h:77
EIGEN_CONSTEXPR Index size() const EIGEN_NOEXCEPT
const Product< Transpose, OtherDerived, AliasFreeProduct > operator*(const MatrixBase< OtherDerived > &matrix) const
EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
friend const Product< OtherDerived, Transpose, AliasFreeProduct > operator*(const MatrixBase< OtherDerived > &matrix, const Transpose &trt)
Expression of the transpose of a matrix.
Definition: Transpose.h:56
const StorageIndex & operator()(Index i) const
const Derived & derived() const
Transpose< TranspositionsBase > transpose() const
void resize(Index newSize)
Derived & operator=(const TranspositionsBase< OtherDerived > &other)
const StorageIndex & operator[](Index i) const
IndicesType::Scalar StorageIndex
StorageIndex & operator[](Index i)
StorageIndex & operator()(Index i)
const IndicesType & indices() const
StorageIndex & coeffRef(Index i)
Traits::IndicesType IndicesType
internal::traits< Derived > Traits
const StorageIndex & coeff(Index i) const
Transpose< TranspositionsBase > inverse() const
TranspositionsWrapper(IndicesType &indices)
TranspositionsBase< TranspositionsWrapper > Base
IndicesType::Scalar StorageIndex
Traits::IndicesType IndicesType
IndicesType::Nested m_indices
TranspositionsWrapper & operator=(const TranspositionsBase< OtherDerived > &other)
internal::traits< TranspositionsWrapper > Traits
const IndicesType & indices() const
Represents a sequence of transpositions (row/column interchange)
Transpositions(const TranspositionsBase< OtherDerived > &other)
TranspositionsBase< Transpositions > Base
internal::traits< Transpositions > Traits
IndicesType & indices()
Transpositions & operator=(const TranspositionsBase< OtherDerived > &other)
IndicesType::Scalar StorageIndex
Traits::IndicesType IndicesType
Transpositions(Index size)
Transpositions(const MatrixBase< Other > &indices)
const IndicesType & indices() const
: InteropHeaders
Definition: Core:139
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82
const Product< MatrixDerived, PermutationDerived, AliasFreeProduct > operator*(const MatrixBase< MatrixDerived > &matrix, const PermutationBase< PermutationDerived > &permutation)