$darkmode
Eigen  5.0.1-dev
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 // IWYU pragma: private
14 #include "./InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 
18 template <typename Derived>
19 class TranspositionsBase {
20  typedef internal::traits<Derived> Traits;
21 
22  public:
23  typedef typename Traits::IndicesType IndicesType;
24  typedef typename IndicesType::Scalar StorageIndex;
25  typedef Eigen::Index Index;
26 
27  EIGEN_DEVICE_FUNC Derived& derived() { return *static_cast<Derived*>(this); }
28  EIGEN_DEVICE_FUNC const Derived& derived() const { return *static_cast<const Derived*>(this); }
29 
31  template <typename OtherDerived>
32  Derived& operator=(const TranspositionsBase<OtherDerived>& other) {
33  indices() = other.indices();
34  return derived();
35  }
36 
38  EIGEN_DEVICE_FUNC Index size() const { return indices().size(); }
40  EIGEN_DEVICE_FUNC Index rows() const { return indices().size(); }
42  EIGEN_DEVICE_FUNC Index cols() const { return indices().size(); }
43 
45  EIGEN_DEVICE_FUNC 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  EIGEN_DEVICE_FUNC const IndicesType& indices() const { return derived().indices(); }
60  EIGEN_DEVICE_FUNC IndicesType& indices() { return derived().indices(); }
61 
63  inline void resize(Index newSize) { indices().resize(newSize); }
64 
66  void setIdentity() {
67  for (StorageIndex i = 0; i < indices().size(); ++i) coeffRef(i) = i;
68  }
69 
70  // FIXME: do we want such methods ?
71  // might be useful when the target matrix expression is complex, e.g.:
72  // object.matrix().block(..,..,..,..) = trans * object.matrix().block(..,..,..,..);
73  /*
74  template<typename MatrixType>
75  void applyForwardToRows(MatrixType& mat) const
76  {
77  for(Index k=0 ; k<size() ; ++k)
78  if(m_indices(k)!=k)
79  mat.row(k).swap(mat.row(m_indices(k)));
80  }
81 
82  template<typename MatrixType>
83  void applyBackwardToRows(MatrixType& mat) const
84  {
85  for(Index k=size()-1 ; k>=0 ; --k)
86  if(m_indices(k)!=k)
87  mat.row(k).swap(mat.row(m_indices(k)));
88  }
89  */
90 
92  inline Transpose<TranspositionsBase> inverse() const { return Transpose<TranspositionsBase>(derived()); }
93 
95  inline Transpose<TranspositionsBase> transpose() const { return Transpose<TranspositionsBase>(derived()); }
96 
97  protected:
98 };
99 
100 namespace internal {
101 template <int SizeAtCompileTime, int MaxSizeAtCompileTime, typename StorageIndex_>
102 struct traits<Transpositions<SizeAtCompileTime, MaxSizeAtCompileTime, StorageIndex_> >
103  : traits<PermutationMatrix<SizeAtCompileTime, MaxSizeAtCompileTime, StorageIndex_> > {
104  typedef Matrix<StorageIndex_, SizeAtCompileTime, 1, 0, MaxSizeAtCompileTime, 1> IndicesType;
105  typedef TranspositionsStorage StorageKind;
106 };
107 } // namespace internal
108 
139 template <int SizeAtCompileTime, int MaxSizeAtCompileTime, typename StorageIndex_>
141  : public TranspositionsBase<Transpositions<SizeAtCompileTime, MaxSizeAtCompileTime, StorageIndex_> > {
142  typedef internal::traits<Transpositions> Traits;
143 
144  public:
145  typedef TranspositionsBase<Transpositions> Base;
146  typedef typename Traits::IndicesType IndicesType;
147  typedef typename IndicesType::Scalar StorageIndex;
148 
149  inline Transpositions() {}
150 
152  template <typename OtherDerived>
153  inline Transpositions(const TranspositionsBase<OtherDerived>& other) : m_indices(other.indices()) {}
154 
156  template <typename Other>
157  explicit inline Transpositions(const MatrixBase<Other>& indices) : m_indices(indices) {}
158 
160  template <typename OtherDerived>
161  Transpositions& operator=(const TranspositionsBase<OtherDerived>& other) {
162  return Base::operator=(other);
163  }
164 
167  inline Transpositions(Index size) : m_indices(size) {}
168 
170  EIGEN_DEVICE_FUNC const IndicesType& indices() const { return m_indices; }
172  EIGEN_DEVICE_FUNC IndicesType& indices() { return m_indices; }
173 
174  protected:
175  IndicesType m_indices;
176 };
177 
178 namespace internal {
179 template <int SizeAtCompileTime, int MaxSizeAtCompileTime, typename StorageIndex_, int PacketAccess_>
180 struct traits<Map<Transpositions<SizeAtCompileTime, MaxSizeAtCompileTime, StorageIndex_>, PacketAccess_> >
181  : traits<PermutationMatrix<SizeAtCompileTime, MaxSizeAtCompileTime, StorageIndex_> > {
182  typedef Map<const Matrix<StorageIndex_, SizeAtCompileTime, 1, 0, MaxSizeAtCompileTime, 1>, PacketAccess_> IndicesType;
183  typedef StorageIndex_ StorageIndex;
184  typedef TranspositionsStorage StorageKind;
185 };
186 } // namespace internal
187 
188 template <int SizeAtCompileTime, int MaxSizeAtCompileTime, typename StorageIndex_, int PacketAccess>
189 class Map<Transpositions<SizeAtCompileTime, MaxSizeAtCompileTime, StorageIndex_>, PacketAccess>
190  : public TranspositionsBase<
191  Map<Transpositions<SizeAtCompileTime, MaxSizeAtCompileTime, StorageIndex_>, PacketAccess> > {
192  typedef internal::traits<Map> Traits;
193 
194  public:
195  typedef TranspositionsBase<Map> Base;
196  typedef typename Traits::IndicesType IndicesType;
197  typedef typename IndicesType::Scalar StorageIndex;
198 
199  explicit inline Map(const StorageIndex* indicesPtr) : m_indices(indicesPtr) {}
200 
201  inline Map(const StorageIndex* indicesPtr, Index size) : m_indices(indicesPtr, size) {}
202 
204  template <typename OtherDerived>
205  Map& operator=(const TranspositionsBase<OtherDerived>& other) {
206  return Base::operator=(other);
207  }
208 
209 #ifndef EIGEN_PARSED_BY_DOXYGEN
210 
213  Map& operator=(const Map& other) {
214  m_indices = other.m_indices;
215  return *this;
216  }
217 #endif
218 
220  EIGEN_DEVICE_FUNC const IndicesType& indices() const { return m_indices; }
221 
223  EIGEN_DEVICE_FUNC IndicesType& indices() { return m_indices; }
224 
225  protected:
226  IndicesType m_indices;
227 };
228 
229 namespace internal {
230 template <typename IndicesType_>
231 struct traits<TranspositionsWrapper<IndicesType_> > : traits<PermutationWrapper<IndicesType_> > {
232  typedef TranspositionsStorage StorageKind;
233 };
234 } // namespace internal
235 
236 template <typename IndicesType_>
237 class TranspositionsWrapper : public TranspositionsBase<TranspositionsWrapper<IndicesType_> > {
238  typedef internal::traits<TranspositionsWrapper> Traits;
239 
240  public:
241  typedef TranspositionsBase<TranspositionsWrapper> Base;
242  typedef typename Traits::IndicesType IndicesType;
243  typedef typename IndicesType::Scalar StorageIndex;
244 
245  explicit inline TranspositionsWrapper(IndicesType& indices) : m_indices(indices) {}
246 
248  template <typename OtherDerived>
249  TranspositionsWrapper& operator=(const TranspositionsBase<OtherDerived>& other) {
250  return Base::operator=(other);
251  }
252 
254  EIGEN_DEVICE_FUNC const IndicesType& indices() const { return m_indices; }
255 
257  EIGEN_DEVICE_FUNC IndicesType& indices() { return m_indices; }
258 
259  protected:
260  typename IndicesType::Nested m_indices;
261 };
262 
265 template <typename MatrixDerived, typename TranspositionsDerived>
267  const MatrixBase<MatrixDerived>& matrix, const TranspositionsBase<TranspositionsDerived>& transpositions) {
268  return Product<MatrixDerived, TranspositionsDerived, AliasFreeProduct>(matrix.derived(), transpositions.derived());
269 }
270 
273 template <typename TranspositionsDerived, typename MatrixDerived>
275  const TranspositionsBase<TranspositionsDerived>& transpositions, const MatrixBase<MatrixDerived>& matrix) {
276  return Product<TranspositionsDerived, MatrixDerived, AliasFreeProduct>(transpositions.derived(), matrix.derived());
277 }
278 
279 // Template partial specialization for transposed/inverse transpositions
280 
281 namespace internal {
282 
283 template <typename Derived>
284 struct traits<Transpose<TranspositionsBase<Derived> > > : traits<Derived> {};
285 
286 } // end namespace internal
287 
288 template <typename TranspositionsDerived>
289 class Transpose<TranspositionsBase<TranspositionsDerived> > {
290  typedef TranspositionsDerived TranspositionType;
291  typedef typename TranspositionType::IndicesType IndicesType;
292 
293  public:
294  explicit Transpose(const TranspositionType& t) : m_transpositions(t) {}
295 
296  EIGEN_DEVICE_FUNC constexpr Index size() const noexcept { return m_transpositions.size(); }
297  EIGEN_DEVICE_FUNC constexpr Index rows() const noexcept { return m_transpositions.size(); }
298  EIGEN_DEVICE_FUNC constexpr Index cols() const noexcept { return m_transpositions.size(); }
299 
302  template <typename OtherDerived>
303  friend const Product<OtherDerived, Transpose, AliasFreeProduct> operator*(const MatrixBase<OtherDerived>& matrix,
304  const Transpose& trt) {
305  return Product<OtherDerived, Transpose, AliasFreeProduct>(matrix.derived(), trt);
306  }
307 
310  template <typename OtherDerived>
311  const Product<Transpose, OtherDerived, AliasFreeProduct> operator*(const MatrixBase<OtherDerived>& matrix) const {
312  return Product<Transpose, OtherDerived, AliasFreeProduct>(*this, matrix.derived());
313  }
314 
315  EIGEN_DEVICE_FUNC const TranspositionType& nestedExpression() const { return m_transpositions; }
316 
317  protected:
318  const TranspositionType& m_transpositions;
319 };
320 
321 } // end namespace Eigen
322 
323 #endif // EIGEN_TRANSPOSITIONS_H
Transpositions(const MatrixBase< Other > &indices)
Definition: Transpositions.h:157
const Product< MatrixDerived, PermutationDerived, DefaultProduct > operator*(const MatrixBase< MatrixDerived > &matrix, const PermutationBase< PermutationDerived > &permutation)
Definition: PermutationMatrix.h:474
constexpr Derived & derived()
Definition: EigenBase.h:49
Transpositions(Index size)
Definition: Transpositions.h:167
Expression of the product of two arbitrary matrices or vectors.
Definition: Product.h:198
Namespace containing all symbols from the Eigen library.
Definition: B01_Experimental.dox:1
Map(PointerArgType dataPtr, const StrideType &stride=StrideType())
Definition: Map.h:123
Transpositions & operator=(const TranspositionsBase< OtherDerived > &other)
Definition: Transpositions.h:161
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_inverse_op< typename Derived::Scalar >, const Derived > inverse(const Eigen::ArrayBase< Derived > &x)
Transpositions(const TranspositionsBase< OtherDerived > &other)
Definition: Transpositions.h:153
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82
IndicesType & indices()
Definition: Transpositions.h:172
const internal::remove_all_t< MatrixTypeNested > & nestedExpression() const
Definition: Transpose.h:72
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:52
Represents a sequence of transpositions (row/column interchange)
Definition: Transpositions.h:140
const IndicesType & indices() const
Definition: Transpositions.h:170