Eigen  3.4.90 (git rev 5a9f66fb35d03a4da9ef8976e67a61b30aa16dcf)
 
Loading...
Searching...
No Matches
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 <[email protected]>
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
16namespace Eigen {
17
18template <typename Derived>
19class 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
100namespace internal {
101template <int SizeAtCompileTime, int MaxSizeAtCompileTime, typename StorageIndex_>
102struct 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
139template <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
178namespace internal {
179template <int SizeAtCompileTime, int MaxSizeAtCompileTime, typename StorageIndex_, int PacketAccess_>
180struct 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
188template <int SizeAtCompileTime, int MaxSizeAtCompileTime, typename StorageIndex_, int PacketAccess>
189class 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
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
229namespace internal {
230template <typename IndicesType_>
231struct traits<TranspositionsWrapper<IndicesType_> > : traits<PermutationWrapper<IndicesType_> > {
232 typedef TranspositionsStorage StorageKind;
233};
234} // namespace internal
235
236template <typename IndicesType_>
237class 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
265template <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
273template <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
281namespace internal {
282
283template <typename Derived>
284struct traits<Transpose<TranspositionsBase<Derived> > > : traits<Derived> {};
285
286} // end namespace internal
287
288template <typename TranspositionsDerived>
289class 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 EIGEN_CONSTEXPR Index size() const EIGEN_NOEXCEPT { return m_transpositions.size(); }
297 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT { return m_transpositions.size(); }
298 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index cols() const EIGEN_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
Derived & derived()
Definition EigenBase.h:49
Map(PointerArgType dataPtr, const StrideType &stride=StrideType())
Definition Map.h:123
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:202
const internal::remove_all_t< MatrixTypeNested > & nestedExpression() const
Definition Transpose.h:72
Represents a sequence of transpositions (row/column interchange)
Definition Transpositions.h:141
Transpositions(const TranspositionsBase< OtherDerived > &other)
Definition Transpositions.h:153
const IndicesType & indices() const
Definition Transpositions.h:170
Transpositions & operator=(const TranspositionsBase< OtherDerived > &other)
Definition Transpositions.h:161
IndicesType & indices()
Definition Transpositions.h:172
Transpositions(Index size)
Definition Transpositions.h:167
Transpositions(const MatrixBase< Other > &indices)
Definition Transpositions.h:157
Namespace containing all symbols from the Eigen library.
Definition Core:137
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition Meta.h:83
const Product< MatrixDerived, PermutationDerived, AliasFreeProduct > operator*(const MatrixBase< MatrixDerived > &matrix, const PermutationBase< PermutationDerived > &permutation)
Definition PermutationMatrix.h:471