Eigen  3.2.92
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 
44 template<typename Derived>
45 class TranspositionsBase
46 {
47  typedef internal::traits<Derived> Traits;
48 
49  public:
50 
51  typedef typename Traits::IndicesType IndicesType;
52  typedef typename IndicesType::Scalar StorageIndex;
53  typedef Eigen::Index Index;
54 
55  Derived& derived() { return *static_cast<Derived*>(this); }
56  const Derived& derived() const { return *static_cast<const Derived*>(this); }
57 
59  template<typename OtherDerived>
60  Derived& operator=(const TranspositionsBase<OtherDerived>& other)
61  {
62  indices() = other.indices();
63  return derived();
64  }
65 
66  #ifndef EIGEN_PARSED_BY_DOXYGEN
67 
70  Derived& operator=(const TranspositionsBase& other)
71  {
72  indices() = other.indices();
73  return derived();
74  }
75  #endif
76 
78  Index size() const { return indices().size(); }
80  Index rows() const { return indices().size(); }
82  Index cols() const { return indices().size(); }
83 
85  inline const StorageIndex& coeff(Index i) const { return indices().coeff(i); }
87  inline StorageIndex& coeffRef(Index i) { return indices().coeffRef(i); }
89  inline const StorageIndex& operator()(Index i) const { return indices()(i); }
91  inline StorageIndex& operator()(Index i) { return indices()(i); }
93  inline const StorageIndex& operator[](Index i) const { return indices()(i); }
95  inline StorageIndex& operator[](Index i) { return indices()(i); }
96 
98  const IndicesType& indices() const { return derived().indices(); }
100  IndicesType& indices() { return derived().indices(); }
101 
103  inline void resize(Index newSize)
104  {
105  indices().resize(newSize);
106  }
107 
109  void setIdentity()
110  {
111  for(StorageIndex i = 0; i < indices().size(); ++i)
112  coeffRef(i) = i;
113  }
114 
115  // FIXME: do we want such methods ?
116  // might be usefull when the target matrix expression is complex, e.g.:
117  // object.matrix().block(..,..,..,..) = trans * object.matrix().block(..,..,..,..);
118  /*
119  template<typename MatrixType>
120  void applyForwardToRows(MatrixType& mat) const
121  {
122  for(Index k=0 ; k<size() ; ++k)
123  if(m_indices(k)!=k)
124  mat.row(k).swap(mat.row(m_indices(k)));
125  }
126 
127  template<typename MatrixType>
128  void applyBackwardToRows(MatrixType& mat) const
129  {
130  for(Index k=size()-1 ; k>=0 ; --k)
131  if(m_indices(k)!=k)
132  mat.row(k).swap(mat.row(m_indices(k)));
133  }
134  */
135 
137  inline Transpose<TranspositionsBase> inverse() const
138  { return Transpose<TranspositionsBase>(derived()); }
139 
141  inline Transpose<TranspositionsBase> transpose() const
142  { return Transpose<TranspositionsBase>(derived()); }
143 
144  protected:
145 };
146 
147 namespace internal {
148 template<int SizeAtCompileTime, int MaxSizeAtCompileTime, typename _StorageIndex>
149 struct traits<Transpositions<SizeAtCompileTime,MaxSizeAtCompileTime,_StorageIndex> >
150  : traits<PermutationMatrix<SizeAtCompileTime,MaxSizeAtCompileTime,_StorageIndex> >
151 {
152  typedef Matrix<_StorageIndex, SizeAtCompileTime, 1, 0, MaxSizeAtCompileTime, 1> IndicesType;
153  typedef TranspositionsStorage StorageKind;
154 };
155 }
156 
157 template<int SizeAtCompileTime, int MaxSizeAtCompileTime, typename _StorageIndex>
158 class Transpositions : public TranspositionsBase<Transpositions<SizeAtCompileTime,MaxSizeAtCompileTime,_StorageIndex> >
159 {
160  typedef internal::traits<Transpositions> Traits;
161  public:
162 
163  typedef TranspositionsBase<Transpositions> Base;
164  typedef typename Traits::IndicesType IndicesType;
165  typedef typename IndicesType::Scalar StorageIndex;
166 
167  inline Transpositions() {}
168 
170  template<typename OtherDerived>
171  inline Transpositions(const TranspositionsBase<OtherDerived>& other)
172  : m_indices(other.indices()) {}
173 
174  #ifndef EIGEN_PARSED_BY_DOXYGEN
175 
177  inline Transpositions(const Transpositions& other) : m_indices(other.indices()) {}
178  #endif
179 
181  template<typename Other>
182  explicit inline Transpositions(const MatrixBase<Other>& indices) : m_indices(indices)
183  {}
184 
186  template<typename OtherDerived>
187  Transpositions& operator=(const TranspositionsBase<OtherDerived>& other)
188  {
189  return Base::operator=(other);
190  }
191 
192  #ifndef EIGEN_PARSED_BY_DOXYGEN
193 
197  {
198  m_indices = other.m_indices;
199  return *this;
200  }
201  #endif
202 
205  inline Transpositions(Index size) : m_indices(size)
206  {}
207 
209  const IndicesType& indices() const { return m_indices; }
211  IndicesType& indices() { return m_indices; }
212 
213  protected:
214 
215  IndicesType m_indices;
216 };
217 
218 
219 namespace internal {
220 template<int SizeAtCompileTime, int MaxSizeAtCompileTime, typename _StorageIndex, int _PacketAccess>
221 struct traits<Map<Transpositions<SizeAtCompileTime,MaxSizeAtCompileTime,_StorageIndex>,_PacketAccess> >
222  : traits<PermutationMatrix<SizeAtCompileTime,MaxSizeAtCompileTime,_StorageIndex> >
223 {
224  typedef Map<const Matrix<_StorageIndex,SizeAtCompileTime,1,0,MaxSizeAtCompileTime,1>, _PacketAccess> IndicesType;
225  typedef _StorageIndex StorageIndex;
226  typedef TranspositionsStorage StorageKind;
227 };
228 }
229 
230 template<int SizeAtCompileTime, int MaxSizeAtCompileTime, typename _StorageIndex, int PacketAccess>
231 class Map<Transpositions<SizeAtCompileTime,MaxSizeAtCompileTime,_StorageIndex>,PacketAccess>
232  : public TranspositionsBase<Map<Transpositions<SizeAtCompileTime,MaxSizeAtCompileTime,_StorageIndex>,PacketAccess> >
233 {
234  typedef internal::traits<Map> Traits;
235  public:
236 
237  typedef TranspositionsBase<Map> Base;
238  typedef typename Traits::IndicesType IndicesType;
239  typedef typename IndicesType::Scalar StorageIndex;
240 
241  explicit inline Map(const StorageIndex* indicesPtr)
242  : m_indices(indicesPtr)
243  {}
244 
245  inline Map(const StorageIndex* indicesPtr, Index size)
246  : m_indices(indicesPtr,size)
247  {}
248 
250  template<typename OtherDerived>
251  Map& operator=(const TranspositionsBase<OtherDerived>& other)
252  {
253  return Base::operator=(other);
254  }
255 
256  #ifndef EIGEN_PARSED_BY_DOXYGEN
257 
260  Map& operator=(const Map& other)
261  {
262  m_indices = other.m_indices;
263  return *this;
264  }
265  #endif
266 
268  const IndicesType& indices() const { return m_indices; }
269 
271  IndicesType& indices() { return m_indices; }
272 
273  protected:
274 
275  IndicesType m_indices;
276 };
277 
278 namespace internal {
279 template<typename _IndicesType>
280 struct traits<TranspositionsWrapper<_IndicesType> >
281  : traits<PermutationWrapper<_IndicesType> >
282 {
283  typedef TranspositionsStorage StorageKind;
284 };
285 }
286 
287 template<typename _IndicesType>
288 class TranspositionsWrapper
289  : public TranspositionsBase<TranspositionsWrapper<_IndicesType> >
290 {
291  typedef internal::traits<TranspositionsWrapper> Traits;
292  public:
293 
294  typedef TranspositionsBase<TranspositionsWrapper> Base;
295  typedef typename Traits::IndicesType IndicesType;
296  typedef typename IndicesType::Scalar StorageIndex;
297 
298  explicit inline TranspositionsWrapper(IndicesType& indices)
299  : m_indices(indices)
300  {}
301 
303  template<typename OtherDerived>
304  TranspositionsWrapper& operator=(const TranspositionsBase<OtherDerived>& other)
305  {
306  return Base::operator=(other);
307  }
308 
309  #ifndef EIGEN_PARSED_BY_DOXYGEN
310 
313  TranspositionsWrapper& operator=(const TranspositionsWrapper& other)
314  {
315  m_indices = other.m_indices;
316  return *this;
317  }
318  #endif
319 
321  const IndicesType& indices() const { return m_indices; }
322 
324  IndicesType& indices() { return m_indices; }
325 
326  protected:
327 
328  const typename IndicesType::Nested m_indices;
329 };
330 
331 
332 
335 template<typename MatrixDerived, typename TranspositionsDerived>
336 EIGEN_DEVICE_FUNC
337 const Product<MatrixDerived, TranspositionsDerived, AliasFreeProduct>
338 operator*(const MatrixBase<MatrixDerived> &matrix,
339  const TranspositionsBase<TranspositionsDerived>& transpositions)
340 {
341  return Product<MatrixDerived, TranspositionsDerived, AliasFreeProduct>
342  (matrix.derived(), transpositions.derived());
343 }
344 
347 template<typename TranspositionsDerived, typename MatrixDerived>
348 EIGEN_DEVICE_FUNC
349 const Product<TranspositionsDerived, MatrixDerived, AliasFreeProduct>
350 operator*(const TranspositionsBase<TranspositionsDerived> &transpositions,
351  const MatrixBase<MatrixDerived>& matrix)
352 {
353  return Product<TranspositionsDerived, MatrixDerived, AliasFreeProduct>
354  (transpositions.derived(), matrix.derived());
355 }
356 
357 // Template partial specialization for transposed/inverse transpositions
358 
359 namespace internal {
360 
361 template<typename Derived>
362 struct traits<Transpose<TranspositionsBase<Derived> > >
363  : traits<Derived>
364 {};
365 
366 } // end namespace internal
367 
368 template<typename TranspositionsDerived>
369 class Transpose<TranspositionsBase<TranspositionsDerived> >
370 {
371  typedef TranspositionsDerived TranspositionType;
372  typedef typename TranspositionType::IndicesType IndicesType;
373  public:
374 
375  explicit Transpose(const TranspositionType& t) : m_transpositions(t) {}
376 
377  Index size() const { return m_transpositions.size(); }
378  Index rows() const { return m_transpositions.size(); }
379  Index cols() const { return m_transpositions.size(); }
380 
383  template<typename OtherDerived> friend
384  const Product<OtherDerived, Transpose, AliasFreeProduct>
385  operator*(const MatrixBase<OtherDerived>& matrix, const Transpose& trt)
386  {
387  return Product<OtherDerived, Transpose, AliasFreeProduct>(matrix.derived(), trt.derived());
388  }
389 
392  template<typename OtherDerived>
393  const Product<Transpose, OtherDerived, AliasFreeProduct>
394  operator*(const MatrixBase<OtherDerived>& matrix) const
395  {
396  return Product<Transpose, OtherDerived, AliasFreeProduct>(*this, matrix.derived());
397  }
398 
399  const TranspositionType& nestedExpression() const { return m_transpositions; }
400 
401  protected:
402  const TranspositionType& m_transpositions;
403 };
404 
405 } // end namespace Eigen
406 
407 #endif // EIGEN_TRANSPOSITIONS_H
const IndicesType & indices() const
Definition: Transpositions.h:209
Definition: LDLT.h:16
Transpositions(const TranspositionsBase< OtherDerived > &other)
Definition: Transpositions.h:171
Map(PointerArgType dataPtr, const StrideType &stride=StrideType())
Definition: Map.h:123
Transpositions(Index size)
Definition: Transpositions.h:205
Transpositions(const MatrixBase< Other > &indices)
Definition: Transpositions.h:182
Definition: Eigen_Colamd.h:54
Transpositions & operator=(const TranspositionsBase< OtherDerived > &other)
Definition: Transpositions.h:187
const internal::remove_all< typename MatrixType::Nested >::type & nestedExpression() const
Definition: Transpose.h:73
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
Represents a sequence of transpositions (row/column interchange)
Definition: Transpositions.h:158
IndicesType & indices()
Definition: Transpositions.h:211