Eigen  3.4.90 (git rev a4098ac676528a83cfb73d4d26ce1b42ec05f47c)
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#include "./InternalHeaderCheck.h"
14
15namespace Eigen {
16
17template<typename Derived>
18class TranspositionsBase
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
28 EIGEN_DEVICE_FUNC
29 Derived& derived() { return *static_cast<Derived*>(this); }
30 EIGEN_DEVICE_FUNC
31 const Derived& derived() const { return *static_cast<const Derived*>(this); }
32
34 template<typename OtherDerived>
35 Derived& operator=(const TranspositionsBase<OtherDerived>& other)
36 {
37 indices() = other.indices();
38 return derived();
39 }
40
42 EIGEN_DEVICE_FUNC
43 Index size() const { return indices().size(); }
45 EIGEN_DEVICE_FUNC
46 Index rows() const { return indices().size(); }
48 EIGEN_DEVICE_FUNC
49 Index cols() const { return indices().size(); }
50
52 EIGEN_DEVICE_FUNC
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
66 EIGEN_DEVICE_FUNC
67 const IndicesType& indices() const { return derived().indices(); }
69 EIGEN_DEVICE_FUNC
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
107 inline Transpose<TranspositionsBase> inverse() const
108 { return Transpose<TranspositionsBase>(derived()); }
109
111 inline Transpose<TranspositionsBase> transpose() const
112 { return Transpose<TranspositionsBase>(derived()); }
113
114 protected:
115};
116
117namespace internal {
118template<int SizeAtCompileTime, int MaxSizeAtCompileTime, typename StorageIndex_>
119struct 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
156template<int SizeAtCompileTime, int MaxSizeAtCompileTime, typename StorageIndex_>
157class Transpositions : public TranspositionsBase<Transpositions<SizeAtCompileTime,MaxSizeAtCompileTime,StorageIndex_> >
158{
159 typedef internal::traits<Transpositions> Traits;
160 public:
161
162 typedef TranspositionsBase<Transpositions> Base;
163 typedef typename Traits::IndicesType IndicesType;
164 typedef typename IndicesType::Scalar StorageIndex;
165
166 inline Transpositions() {}
167
169 template<typename OtherDerived>
170 inline Transpositions(const TranspositionsBase<OtherDerived>& other)
171 : m_indices(other.indices()) {}
172
174 template<typename Other>
175 explicit inline Transpositions(const MatrixBase<Other>& indices) : m_indices(indices)
176 {}
177
179 template<typename OtherDerived>
180 Transpositions& operator=(const TranspositionsBase<OtherDerived>& other)
181 {
182 return Base::operator=(other);
183 }
184
187 inline Transpositions(Index size) : m_indices(size)
188 {}
189
191 EIGEN_DEVICE_FUNC
192 const IndicesType& indices() const { return m_indices; }
194 EIGEN_DEVICE_FUNC
195 IndicesType& indices() { return m_indices; }
196
197 protected:
198
199 IndicesType m_indices;
200};
201
202
203namespace internal {
204template<int SizeAtCompileTime, int MaxSizeAtCompileTime, typename StorageIndex_, int _PacketAccess>
205struct traits<Map<Transpositions<SizeAtCompileTime,MaxSizeAtCompileTime,StorageIndex_>,_PacketAccess> >
206 : traits<PermutationMatrix<SizeAtCompileTime,MaxSizeAtCompileTime,StorageIndex_> >
207{
208 typedef Map<const Matrix<StorageIndex_,SizeAtCompileTime,1,0,MaxSizeAtCompileTime,1>, _PacketAccess> IndicesType;
209 typedef StorageIndex_ StorageIndex;
210 typedef TranspositionsStorage StorageKind;
211};
212}
213
214template<int SizeAtCompileTime, int MaxSizeAtCompileTime, typename StorageIndex_, int PacketAccess>
215class 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
221 typedef TranspositionsBase<Map> Base;
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>
235 Map& operator=(const TranspositionsBase<OtherDerived>& other)
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
252 EIGEN_DEVICE_FUNC
253 const IndicesType& indices() const { return m_indices; }
254
256 EIGEN_DEVICE_FUNC
257 IndicesType& indices() { return m_indices; }
258
259 protected:
260
261 IndicesType m_indices;
262};
263
264namespace internal {
265template<typename IndicesType_>
266struct traits<TranspositionsWrapper<IndicesType_> >
267 : traits<PermutationWrapper<IndicesType_> >
268{
269 typedef TranspositionsStorage StorageKind;
270};
271}
272
273template<typename IndicesType_>
274class TranspositionsWrapper
275 : public TranspositionsBase<TranspositionsWrapper<IndicesType_> >
276{
277 typedef internal::traits<TranspositionsWrapper> Traits;
278 public:
279
280 typedef TranspositionsBase<TranspositionsWrapper> Base;
281 typedef typename Traits::IndicesType IndicesType;
282 typedef typename IndicesType::Scalar StorageIndex;
283
284 explicit inline TranspositionsWrapper(IndicesType& indices)
285 : m_indices(indices)
286 {}
287
289 template<typename OtherDerived>
290 TranspositionsWrapper& operator=(const TranspositionsBase<OtherDerived>& other)
291 {
292 return Base::operator=(other);
293 }
294
296 EIGEN_DEVICE_FUNC
297 const IndicesType& indices() const { return m_indices; }
298
300 EIGEN_DEVICE_FUNC
301 IndicesType& indices() { return m_indices; }
302
303 protected:
304
305 typename IndicesType::Nested m_indices;
306};
307
308
309
312template<typename MatrixDerived, typename TranspositionsDerived>
313EIGEN_DEVICE_FUNC
314const Product<MatrixDerived, TranspositionsDerived, AliasFreeProduct>
316 const TranspositionsBase<TranspositionsDerived>& transpositions)
317{
319 (matrix.derived(), transpositions.derived());
320}
321
324template<typename TranspositionsDerived, typename MatrixDerived>
325EIGEN_DEVICE_FUNC
326const Product<TranspositionsDerived, MatrixDerived, AliasFreeProduct>
327operator*(const TranspositionsBase<TranspositionsDerived> &transpositions,
328 const MatrixBase<MatrixDerived>& matrix)
329{
331 (transpositions.derived(), matrix.derived());
332}
333
334// Template partial specialization for transposed/inverse transpositions
335
336namespace internal {
337
338template<typename Derived>
339struct traits<Transpose<TranspositionsBase<Derived> > >
340 : traits<Derived>
341{};
342
343} // end namespace internal
344
345template<typename TranspositionsDerived>
346class 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
354 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
355 Index size() const EIGEN_NOEXCEPT { return m_transpositions.size(); }
356 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
357 Index rows() const EIGEN_NOEXCEPT { return m_transpositions.size(); }
358 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
359 Index cols() const EIGEN_NOEXCEPT { return m_transpositions.size(); }
360
363 template<typename OtherDerived> friend
364 const Product<OtherDerived, Transpose, AliasFreeProduct>
365 operator*(const MatrixBase<OtherDerived>& matrix, const Transpose& trt)
366 {
367 return Product<OtherDerived, Transpose, AliasFreeProduct>(matrix.derived(), trt);
368 }
369
372 template<typename OtherDerived>
373 const Product<Transpose, OtherDerived, AliasFreeProduct>
374 operator*(const MatrixBase<OtherDerived>& matrix) const
375 {
376 return Product<Transpose, OtherDerived, AliasFreeProduct>(*this, matrix.derived());
377 }
378
379 EIGEN_DEVICE_FUNC
380 const TranspositionType& nestedExpression() const { return m_transpositions; }
381
382 protected:
383 const TranspositionType& m_transpositions;
384};
385
386} // end namespace Eigen
387
388#endif // EIGEN_TRANSPOSITIONS_H
Derived & derived()
Definition: EigenBase.h:48
Map(PointerArgType dataPtr, const StrideType &stride=StrideType())
Definition: Map.h:131
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
const internal::remove_all< MatrixTypeNested >::type & nestedExpression() const
Definition: Transpose.h:78
Represents a sequence of transpositions (row/column interchange)
Definition: Transpositions.h:158
Transpositions(const TranspositionsBase< OtherDerived > &other)
Definition: Transpositions.h:170
const IndicesType & indices() const
Definition: Transpositions.h:192
Transpositions & operator=(const TranspositionsBase< OtherDerived > &other)
Definition: Transpositions.h:180
IndicesType & indices()
Definition: Transpositions.h:195
Transpositions(Index size)
Definition: Transpositions.h:187
Transpositions(const MatrixBase< Other > &indices)
Definition: Transpositions.h:175
Namespace containing all symbols from the Eigen library.
Definition: B01_Experimental.dox:1
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_inverse_op< typename Derived::Scalar >, const Derived > inverse(const Eigen::ArrayBase< Derived > &x)
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:59
const Product< MatrixDerived, PermutationDerived, AliasFreeProduct > operator*(const MatrixBase< MatrixDerived > &matrix, const PermutationBase< PermutationDerived > &permutation)
Definition: PermutationMatrix.h:517