Medial Code Documentation
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 <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
13namespace Eigen {
14
44template<typename Derived>
46{
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>
61 {
62 indices() = other.indices();
63 return derived();
64 }
65
66 #ifndef EIGEN_PARSED_BY_DOXYGEN
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
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
138 { return Transpose<TranspositionsBase>(derived()); }
139
143
144 protected:
145};
146
147namespace internal {
148template<int SizeAtCompileTime, int MaxSizeAtCompileTime, typename _StorageIndex>
149struct traits<Transpositions<SizeAtCompileTime,MaxSizeAtCompileTime,_StorageIndex> >
150 : traits<PermutationMatrix<SizeAtCompileTime,MaxSizeAtCompileTime,_StorageIndex> >
151{
154};
155}
156
157template<int SizeAtCompileTime, int MaxSizeAtCompileTime, typename _StorageIndex>
158class Transpositions : public TranspositionsBase<Transpositions<SizeAtCompileTime,MaxSizeAtCompileTime,_StorageIndex> >
159{
161 public:
162
164 typedef typename Traits::IndicesType IndicesType;
165 typedef typename IndicesType::Scalar StorageIndex;
166
167 inline Transpositions() {}
168
170 template<typename OtherDerived>
172 : m_indices(other.indices()) {}
173
174 #ifndef EIGEN_PARSED_BY_DOXYGEN
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>
188 {
189 return Base::operator=(other);
190 }
191
192 #ifndef EIGEN_PARSED_BY_DOXYGEN
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
219namespace internal {
220template<int SizeAtCompileTime, int MaxSizeAtCompileTime, typename _StorageIndex, int _PacketAccess>
221struct traits<Map<Transpositions<SizeAtCompileTime,MaxSizeAtCompileTime,_StorageIndex>,_PacketAccess> >
222 : traits<PermutationMatrix<SizeAtCompileTime,MaxSizeAtCompileTime,_StorageIndex> >
223{
225 typedef _StorageIndex StorageIndex;
227};
228}
229
230template<int SizeAtCompileTime, int MaxSizeAtCompileTime, typename _StorageIndex, int PacketAccess>
231class Map<Transpositions<SizeAtCompileTime,MaxSizeAtCompileTime,_StorageIndex>,PacketAccess>
232 : public TranspositionsBase<Map<Transpositions<SizeAtCompileTime,MaxSizeAtCompileTime,_StorageIndex>,PacketAccess> >
233{
235 public:
236
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>
252 {
253 return Base::operator=(other);
254 }
255
256 #ifndef EIGEN_PARSED_BY_DOXYGEN
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
278namespace internal {
279template<typename _IndicesType>
281 : traits<PermutationWrapper<_IndicesType> >
282{
284};
285}
286
287template<typename _IndicesType>
289 : public TranspositionsBase<TranspositionsWrapper<_IndicesType> >
290{
292 public:
293
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>
305 {
306 return Base::operator=(other);
307 }
308
309 #ifndef EIGEN_PARSED_BY_DOXYGEN
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
335template<typename MatrixDerived, typename TranspositionsDerived>
336EIGEN_DEVICE_FUNC
337const Product<MatrixDerived, TranspositionsDerived, AliasFreeProduct>
338operator*(const MatrixBase<MatrixDerived> &matrix,
339 const TranspositionsBase<TranspositionsDerived>& transpositions)
340{
341 return Product<MatrixDerived, TranspositionsDerived, AliasFreeProduct>
342 (matrix.derived(), transpositions.derived());
343}
344
347template<typename TranspositionsDerived, typename MatrixDerived>
348EIGEN_DEVICE_FUNC
349const Product<TranspositionsDerived, MatrixDerived, AliasFreeProduct>
350operator*(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
359namespace internal {
360
361template<typename Derived>
363 : traits<Derived>
364{};
365
366} // end namespace internal
367
368template<typename 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
386 {
387 return Product<OtherDerived, Transpose, AliasFreeProduct>(matrix.derived(), trt.derived());
388 }
389
392 template<typename OtherDerived>
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
Map & operator=(const Map &other)
This is a special case of the templated operator=.
Definition Transpositions.h:260
const IndicesType & indices() const
const version of indices().
Definition Transpositions.h:268
Map & operator=(const TranspositionsBase< OtherDerived > &other)
Copies the other transpositions into *this.
Definition Transpositions.h:251
A matrix or vector expression mapping an existing array of data.
Definition Map.h:91
Pseudo expression representing a solving operation.
Definition Solve.h:63
friend const Product< OtherDerived, Transpose, AliasFreeProduct > operator*(const MatrixBase< OtherDerived > &matrix, const Transpose &trt)
Definition Transpositions.h:385
const Product< Transpose, OtherDerived, AliasFreeProduct > operator*(const MatrixBase< OtherDerived > &matrix) const
Definition Transpositions.h:394
Expression of the transpose of a matrix.
Definition Transpose.h:55
EIGEN_DEVICE_FUNC const internal::remove_all< typenameMatrixType::Nested >::type & nestedExpression() const
Definition Transpose.h:73
Definition Transpositions.h:46
const IndicesType & indices() const
const version of indices().
Definition Transpositions.h:98
Transpose< TranspositionsBase > transpose() const
Definition Transpositions.h:141
IndicesType & indices()
Definition Transpositions.h:100
void resize(Index newSize)
Resizes to given size.
Definition Transpositions.h:103
Eigen::Index Index
Definition Transpositions.h:53
const StorageIndex & operator[](Index i) const
Direct access to the underlying index vector.
Definition Transpositions.h:93
Index size() const
Definition Transpositions.h:78
const StorageIndex & operator()(Index i) const
Direct access to the underlying index vector.
Definition Transpositions.h:89
Derived & operator=(const TranspositionsBase &other)
This is a special case of the templated operator=.
Definition Transpositions.h:70
Index rows() const
Definition Transpositions.h:80
void setIdentity()
Sets *this to represents an identity transformation.
Definition Transpositions.h:109
StorageIndex & operator[](Index i)
Direct access to the underlying index vector.
Definition Transpositions.h:95
const StorageIndex & coeff(Index i) const
Direct access to the underlying index vector.
Definition Transpositions.h:85
Derived & operator=(const TranspositionsBase< OtherDerived > &other)
Copies the other transpositions into *this.
Definition Transpositions.h:60
Index cols() const
Definition Transpositions.h:82
StorageIndex & coeffRef(Index i)
Direct access to the underlying index vector.
Definition Transpositions.h:87
StorageIndex & operator()(Index i)
Direct access to the underlying index vector.
Definition Transpositions.h:91
Transpose< TranspositionsBase > inverse() const
Definition Transpositions.h:137
Definition Transpositions.h:290
TranspositionsWrapper & operator=(const TranspositionsWrapper &other)
This is a special case of the templated operator=.
Definition Transpositions.h:313
TranspositionsWrapper & operator=(const TranspositionsBase< OtherDerived > &other)
Copies the other transpositions into *this.
Definition Transpositions.h:304
IndicesType & indices()
Definition Transpositions.h:324
const IndicesType & indices() const
const version of indices().
Definition Transpositions.h:321
Represents a sequence of transpositions (row/column interchange)
Definition Transpositions.h:159
Transpositions & operator=(const Transpositions &other)
This is a special case of the templated operator=.
Definition Transpositions.h:196
Transpositions(const MatrixBase< Other > &indices)
Generic constructor from expression of the transposition indices.
Definition Transpositions.h:182
IndicesType & indices()
Definition Transpositions.h:211
const IndicesType & indices() const
const version of indices().
Definition Transpositions.h:209
Transpositions(Index size)
Constructs an uninitialized permutation matrix of given size.
Definition Transpositions.h:205
Transpositions & operator=(const TranspositionsBase< OtherDerived > &other)
Copies the other transpositions into *this.
Definition Transpositions.h:187
Transpositions(const TranspositionsBase< OtherDerived > &other)
Copy constructor.
Definition Transpositions.h:171
Transpositions(const Transpositions &other)
Standard copy constructor.
Definition Transpositions.h:177
The type used to identify a permutation storage.
Definition Constants.h:502
Definition ForwardDeclarations.h:17