Please, help us to better know about our user community by answering the following short survey: https://forms.gle/wpyrxWi18ox9Z5ae9
Eigen  3.3.9
Transpositions.h
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>
16 class TranspositionsBase
17 {
18  typedef internal::traits<Derived> Traits;
19 
20  public:
21 
22  typedef typename Traits::IndicesType IndicesType;
23  typedef typename IndicesType::Scalar StorageIndex;
24  typedef Eigen::Index Index;
25 
26  Derived& derived() { return *static_cast<Derived*>(this); }
27  const Derived& derived() const { return *static_cast<const Derived*>(this); }
28 
30  template<typename OtherDerived>
31  Derived& operator=(const TranspositionsBase<OtherDerived>& other)
32  {
33  indices() = other.indices();
34  return derived();
35  }
36 
38  Index size() const { return indices().size(); }
40  Index rows() const { return indices().size(); }
42  Index cols() const { return indices().size(); }
43 
45  inline const StorageIndex& coeff(Index i) const { return indices().coeff(i); }
47  inline StorageIndex& coeffRef(Index i) { return indices().coeffRef(i); }
49  inline const StorageIndex& operator()(Index i) const { return indices()(i); }
51  inline StorageIndex& operator()(Index i) { return indices()(i); }
53  inline const StorageIndex& operator[](Index i) const { return indices()(i); }
55  inline StorageIndex& operator[](Index i) { return indices()(i); }
56 
58  const IndicesType& indices() const { return derived().indices(); }
60  IndicesType& indices() { return derived().indices(); }
61 
63  inline void resize(Index newSize)
64  {
65  indices().resize(newSize);
66  }
67 
69  void setIdentity()
70  {
71  for(StorageIndex i = 0; i < indices().size(); ++i)
72  coeffRef(i) = i;
73  }
74 
75  // FIXME: do we want such methods ?
76  // might be usefull when the target matrix expression is complex, e.g.:
77  // object.matrix().block(..,..,..,..) = trans * object.matrix().block(..,..,..,..);
78  /*
79  template<typename MatrixType>
80  void applyForwardToRows(MatrixType& mat) const
81  {
82  for(Index k=0 ; k<size() ; ++k)
83  if(m_indices(k)!=k)
84  mat.row(k).swap(mat.row(m_indices(k)));
85  }
86 
87  template<typename MatrixType>
88  void applyBackwardToRows(MatrixType& mat) const
89  {
90  for(Index k=size()-1 ; k>=0 ; --k)
91  if(m_indices(k)!=k)
92  mat.row(k).swap(mat.row(m_indices(k)));
93  }
94  */
95 
97  inline Transpose<TranspositionsBase> inverse() const
98  { return Transpose<TranspositionsBase>(derived()); }
99 
101  inline Transpose<TranspositionsBase> transpose() const
102  { return Transpose<TranspositionsBase>(derived()); }
103 
104  protected:
105 };
106 
107 namespace internal {
108 template<int SizeAtCompileTime, int MaxSizeAtCompileTime, typename _StorageIndex>
109 struct traits<Transpositions<SizeAtCompileTime,MaxSizeAtCompileTime,_StorageIndex> >
110  : traits<PermutationMatrix<SizeAtCompileTime,MaxSizeAtCompileTime,_StorageIndex> >
111 {
112  typedef Matrix<_StorageIndex, SizeAtCompileTime, 1, 0, MaxSizeAtCompileTime, 1> IndicesType;
113  typedef TranspositionsStorage StorageKind;
114 };
115 }
116 
146 template<int SizeAtCompileTime, int MaxSizeAtCompileTime, typename _StorageIndex>
147 class Transpositions : public TranspositionsBase<Transpositions<SizeAtCompileTime,MaxSizeAtCompileTime,_StorageIndex> >
148 {
149  typedef internal::traits<Transpositions> Traits;
150  public:
151 
152  typedef TranspositionsBase<Transpositions> Base;
153  typedef typename Traits::IndicesType IndicesType;
154  typedef typename IndicesType::Scalar StorageIndex;
155 
156  inline Transpositions() {}
157 
159  template<typename OtherDerived>
160  inline Transpositions(const TranspositionsBase<OtherDerived>& other)
161  : m_indices(other.indices()) {}
162 
164  template<typename Other>
165  explicit inline Transpositions(const MatrixBase<Other>& indices) : m_indices(indices)
166  {}
167 
169  template<typename OtherDerived>
170  Transpositions& operator=(const TranspositionsBase<OtherDerived>& other)
171  {
172  return Base::operator=(other);
173  }
174 
177  inline Transpositions(Index size) : m_indices(size)
178  {}
179 
181  const IndicesType& indices() const { return m_indices; }
183  IndicesType& indices() { return m_indices; }
184 
185  protected:
186 
187  IndicesType m_indices;
188 };
189 
190 
191 namespace internal {
192 template<int SizeAtCompileTime, int MaxSizeAtCompileTime, typename _StorageIndex, int _PacketAccess>
193 struct traits<Map<Transpositions<SizeAtCompileTime,MaxSizeAtCompileTime,_StorageIndex>,_PacketAccess> >
194  : traits<PermutationMatrix<SizeAtCompileTime,MaxSizeAtCompileTime,_StorageIndex> >
195 {
196  typedef Map<const Matrix<_StorageIndex,SizeAtCompileTime,1,0,MaxSizeAtCompileTime,1>, _PacketAccess> IndicesType;
197  typedef _StorageIndex StorageIndex;
198  typedef TranspositionsStorage StorageKind;
199 };
200 }
201 
202 template<int SizeAtCompileTime, int MaxSizeAtCompileTime, typename _StorageIndex, int PacketAccess>
203 class Map<Transpositions<SizeAtCompileTime,MaxSizeAtCompileTime,_StorageIndex>,PacketAccess>
204  : public TranspositionsBase<Map<Transpositions<SizeAtCompileTime,MaxSizeAtCompileTime,_StorageIndex>,PacketAccess> >
205 {
206  typedef internal::traits<Map> Traits;
207  public:
208 
209  typedef TranspositionsBase<Map> Base;
210  typedef typename Traits::IndicesType IndicesType;
211  typedef typename IndicesType::Scalar StorageIndex;
212 
213  explicit inline Map(const StorageIndex* indicesPtr)
214  : m_indices(indicesPtr)
215  {}
216 
217  inline Map(const StorageIndex* indicesPtr, Index size)
218  : m_indices(indicesPtr,size)
219  {}
220 
222  template<typename OtherDerived>
223  Map& operator=(const TranspositionsBase<OtherDerived>& other)
224  {
225  return Base::operator=(other);
226  }
227 
228  #ifndef EIGEN_PARSED_BY_DOXYGEN
229 
232  Map& operator=(const Map& other)
233  {
234  m_indices = other.m_indices;
235  return *this;
236  }
237  #endif
238 
240  const IndicesType& indices() const { return m_indices; }
241 
243  IndicesType& indices() { return m_indices; }
244 
245  protected:
246 
247  IndicesType m_indices;
248 };
249 
250 namespace internal {
251 template<typename _IndicesType>
252 struct traits<TranspositionsWrapper<_IndicesType> >
253  : traits<PermutationWrapper<_IndicesType> >
254 {
255  typedef TranspositionsStorage StorageKind;
256 };
257 }
258 
259 template<typename _IndicesType>
260 class TranspositionsWrapper
261  : public TranspositionsBase<TranspositionsWrapper<_IndicesType> >
262 {
263  typedef internal::traits<TranspositionsWrapper> Traits;
264  public:
265 
266  typedef TranspositionsBase<TranspositionsWrapper> Base;
267  typedef typename Traits::IndicesType IndicesType;
268  typedef typename IndicesType::Scalar StorageIndex;
269 
270  explicit inline TranspositionsWrapper(IndicesType& indices)
271  : m_indices(indices)
272  {}
273 
275  template<typename OtherDerived>
276  TranspositionsWrapper& operator=(const TranspositionsBase<OtherDerived>& other)
277  {
278  return Base::operator=(other);
279  }
280 
282  const IndicesType& indices() const { return m_indices; }
283 
285  IndicesType& indices() { return m_indices; }
286 
287  protected:
288 
289  typename IndicesType::Nested m_indices;
290 };
291 
292 
293 
296 template<typename MatrixDerived, typename TranspositionsDerived>
297 EIGEN_DEVICE_FUNC
298 const Product<MatrixDerived, TranspositionsDerived, AliasFreeProduct>
300  const TranspositionsBase<TranspositionsDerived>& transpositions)
301 {
303  (matrix.derived(), transpositions.derived());
304 }
305 
308 template<typename TranspositionsDerived, typename MatrixDerived>
309 EIGEN_DEVICE_FUNC
310 const Product<TranspositionsDerived, MatrixDerived, AliasFreeProduct>
311 operator*(const TranspositionsBase<TranspositionsDerived> &transpositions,
312  const MatrixBase<MatrixDerived>& matrix)
313 {
315  (transpositions.derived(), matrix.derived());
316 }
317 
318 // Template partial specialization for transposed/inverse transpositions
319 
320 namespace internal {
321 
322 template<typename Derived>
323 struct traits<Transpose<TranspositionsBase<Derived> > >
324  : traits<Derived>
325 {};
326 
327 } // end namespace internal
328 
329 template<typename TranspositionsDerived>
330 class Transpose<TranspositionsBase<TranspositionsDerived> >
331 {
332  typedef TranspositionsDerived TranspositionType;
333  typedef typename TranspositionType::IndicesType IndicesType;
334  public:
335 
336  explicit Transpose(const TranspositionType& t) : m_transpositions(t) {}
337 
338  Index size() const { return m_transpositions.size(); }
339  Index rows() const { return m_transpositions.size(); }
340  Index cols() const { return m_transpositions.size(); }
341 
344  template<typename OtherDerived> friend
345  const Product<OtherDerived, Transpose, AliasFreeProduct>
346  operator*(const MatrixBase<OtherDerived>& matrix, const Transpose& trt)
347  {
348  return Product<OtherDerived, Transpose, AliasFreeProduct>(matrix.derived(), trt);
349  }
350 
353  template<typename OtherDerived>
354  const Product<Transpose, OtherDerived, AliasFreeProduct>
355  operator*(const MatrixBase<OtherDerived>& matrix) const
356  {
357  return Product<Transpose, OtherDerived, AliasFreeProduct>(*this, matrix.derived());
358  }
359 
360  const TranspositionType& nestedExpression() const { return m_transpositions; }
361 
362  protected:
363  const TranspositionType& m_transpositions;
364 };
365 
366 } // end namespace Eigen
367 
368 #endif // EIGEN_TRANSPOSITIONS_H
Expression of the product of two arbitrary matrices or vectors.
Definition: Product.h:71
Namespace containing all symbols from the Eigen library.
Definition: Core:309
Transpositions(const TranspositionsBase< OtherDerived > &other)
Definition: Transpositions.h:160
Map(PointerArgType dataPtr, const StrideType &stride=StrideType())
Definition: Map.h:129
const IndicesType & indices() const
Definition: Transpositions.h:181
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_inverse_op< typename Derived::Scalar >, const Derived > inverse(const Eigen::ArrayBase< Derived > &x)
Derived & derived()
Definition: EigenBase.h:45
const Product< MatrixDerived, PermutationDerived, AliasFreeProduct > operator*(const MatrixBase< MatrixDerived > &matrix, const PermutationBase< PermutationDerived > &permutation)
Definition: PermutationMatrix.h:515
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:33
Transpositions(Index size)
Definition: Transpositions.h:177
const internal::remove_all< MatrixTypeNested >::type & nestedExpression() const
Definition: Transpose.h:74
Transpositions(const MatrixBase< Other > &indices)
Definition: Transpositions.h:165
Definition: Eigen_Colamd.h:50
Transpositions & operator=(const TranspositionsBase< OtherDerived > &other)
Definition: Transpositions.h:170
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
Represents a sequence of transpositions (row/column interchange)
Definition: Transpositions.h:147
IndicesType & indices()
Definition: Transpositions.h:183