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
43
44namespace internal {
45template<typename TranspositionType, typename MatrixType, int Side, bool Transposed=false> struct transposition_matrix_product_retval;
46}
47
48template<typename Derived>
49class TranspositionsBase
50{
51 typedef internal::traits<Derived> Traits;
52
53 public:
54
55 typedef typename Traits::IndicesType IndicesType;
56 typedef typename IndicesType::Scalar Index;
57
58 Derived& derived() { return *static_cast<Derived*>(this); }
59 const Derived& derived() const { return *static_cast<const Derived*>(this); }
60
62 template<typename OtherDerived>
63 Derived& operator=(const TranspositionsBase<OtherDerived>& other)
64 {
65 indices() = other.indices();
66 return derived();
67 }
68
69 #ifndef EIGEN_PARSED_BY_DOXYGEN
73 Derived& operator=(const TranspositionsBase& other)
74 {
75 indices() = other.indices();
76 return derived();
77 }
78 #endif
79
81 inline Index size() const { return indices().size(); }
82
84 inline const Index& coeff(Index i) const { return indices().coeff(i); }
86 inline Index& coeffRef(Index i) { return indices().coeffRef(i); }
88 inline const Index& operator()(Index i) const { return indices()(i); }
90 inline Index& operator()(Index i) { return indices()(i); }
92 inline const Index& operator[](Index i) const { return indices()(i); }
94 inline Index& operator[](Index i) { return indices()(i); }
95
97 const IndicesType& indices() const { return derived().indices(); }
99 IndicesType& indices() { return derived().indices(); }
100
102 inline void resize(int size)
103 {
104 indices().resize(size);
105 }
106
108 void setIdentity()
109 {
110 for(int i = 0; i < indices().size(); ++i)
111 coeffRef(i) = i;
112 }
113
114 // FIXME: do we want such methods ?
115 // might be usefull when the target matrix expression is complex, e.g.:
116 // object.matrix().block(..,..,..,..) = trans * object.matrix().block(..,..,..,..);
117 /*
118 template<typename MatrixType>
119 void applyForwardToRows(MatrixType& mat) const
120 {
121 for(Index k=0 ; k<size() ; ++k)
122 if(m_indices(k)!=k)
123 mat.row(k).swap(mat.row(m_indices(k)));
124 }
125
126 template<typename MatrixType>
127 void applyBackwardToRows(MatrixType& mat) const
128 {
129 for(Index k=size()-1 ; k>=0 ; --k)
130 if(m_indices(k)!=k)
131 mat.row(k).swap(mat.row(m_indices(k)));
132 }
133 */
134
136 inline Transpose<TranspositionsBase> inverse() const
137 { return Transpose<TranspositionsBase>(derived()); }
138
140 inline Transpose<TranspositionsBase> transpose() const
141 { return Transpose<TranspositionsBase>(derived()); }
142
143 protected:
144};
145
146namespace internal {
147template<int SizeAtCompileTime, int MaxSizeAtCompileTime, typename IndexType>
148struct traits<Transpositions<SizeAtCompileTime,MaxSizeAtCompileTime,IndexType> >
149{
150 typedef IndexType Index;
151 typedef Matrix<Index, SizeAtCompileTime, 1, 0, MaxSizeAtCompileTime, 1> IndicesType;
152};
153}
154
155template<int SizeAtCompileTime, int MaxSizeAtCompileTime, typename IndexType>
156class Transpositions : public TranspositionsBase<Transpositions<SizeAtCompileTime,MaxSizeAtCompileTime,IndexType> >
157{
158 typedef internal::traits<Transpositions> Traits;
159 public:
160
161 typedef TranspositionsBase<Transpositions> Base;
162 typedef typename Traits::IndicesType IndicesType;
163 typedef typename IndicesType::Scalar Index;
164
165 inline Transpositions() {}
166
168 template<typename OtherDerived>
169 inline Transpositions(const TranspositionsBase<OtherDerived>& other)
170 : m_indices(other.indices()) {}
171
172 #ifndef EIGEN_PARSED_BY_DOXYGEN
175 inline Transpositions(const Transpositions& other) : m_indices(other.indices()) {}
176 #endif
177
179 template<typename Other>
180 explicit inline Transpositions(const MatrixBase<Other>& indices) : m_indices(indices)
181 {}
182
184 template<typename OtherDerived>
185 Transpositions& operator=(const TranspositionsBase<OtherDerived>& other)
186 {
187 return Base::operator=(other);
188 }
189
190 #ifndef EIGEN_PARSED_BY_DOXYGEN
195 {
196 m_indices = other.m_indices;
197 return *this;
198 }
199 #endif
200
203 inline Transpositions(Index size) : m_indices(size)
204 {}
205
207 const IndicesType& indices() const { return m_indices; }
209 IndicesType& indices() { return m_indices; }
210
211 protected:
212
213 IndicesType m_indices;
214};
215
216
217namespace internal {
218template<int SizeAtCompileTime, int MaxSizeAtCompileTime, typename IndexType, int _PacketAccess>
219struct traits<Map<Transpositions<SizeAtCompileTime,MaxSizeAtCompileTime,IndexType>,_PacketAccess> >
220{
221 typedef IndexType Index;
222 typedef Map<const Matrix<Index,SizeAtCompileTime,1,0,MaxSizeAtCompileTime,1>, _PacketAccess> IndicesType;
223};
224}
225
226template<int SizeAtCompileTime, int MaxSizeAtCompileTime, typename IndexType, int PacketAccess>
227class Map<Transpositions<SizeAtCompileTime,MaxSizeAtCompileTime,IndexType>,PacketAccess>
228 : public TranspositionsBase<Map<Transpositions<SizeAtCompileTime,MaxSizeAtCompileTime,IndexType>,PacketAccess> >
229{
230 typedef internal::traits<Map> Traits;
231 public:
232
233 typedef TranspositionsBase<Map> Base;
234 typedef typename Traits::IndicesType IndicesType;
235 typedef typename IndicesType::Scalar Index;
236
237 inline Map(const Index* indices)
238 : m_indices(indices)
239 {}
240
241 inline Map(const Index* indices, Index size)
242 : m_indices(indices,size)
243 {}
244
246 template<typename OtherDerived>
247 Map& operator=(const TranspositionsBase<OtherDerived>& other)
248 {
249 return Base::operator=(other);
250 }
251
252 #ifndef EIGEN_PARSED_BY_DOXYGEN
256 Map& operator=(const Map& other)
257 {
258 m_indices = other.m_indices;
259 return *this;
260 }
261 #endif
262
264 const IndicesType& indices() const { return m_indices; }
265
267 IndicesType& indices() { return m_indices; }
268
269 protected:
270
271 IndicesType m_indices;
272};
273
274namespace internal {
275template<typename _IndicesType>
276struct traits<TranspositionsWrapper<_IndicesType> >
277{
278 typedef typename _IndicesType::Scalar Index;
279 typedef _IndicesType IndicesType;
280};
281}
282
283template<typename _IndicesType>
284class TranspositionsWrapper
285 : public TranspositionsBase<TranspositionsWrapper<_IndicesType> >
286{
287 typedef internal::traits<TranspositionsWrapper> Traits;
288 public:
289
290 typedef TranspositionsBase<TranspositionsWrapper> Base;
291 typedef typename Traits::IndicesType IndicesType;
292 typedef typename IndicesType::Scalar Index;
293
294 inline TranspositionsWrapper(IndicesType& indices)
295 : m_indices(indices)
296 {}
297
299 template<typename OtherDerived>
300 TranspositionsWrapper& operator=(const TranspositionsBase<OtherDerived>& other)
301 {
302 return Base::operator=(other);
303 }
304
305 #ifndef EIGEN_PARSED_BY_DOXYGEN
309 TranspositionsWrapper& operator=(const TranspositionsWrapper& other)
310 {
311 m_indices = other.m_indices;
312 return *this;
313 }
314 #endif
315
317 const IndicesType& indices() const { return m_indices; }
318
320 IndicesType& indices() { return m_indices; }
321
322 protected:
323
324 const typename IndicesType::Nested m_indices;
325};
326
329template<typename Derived, typename TranspositionsDerived>
330inline const internal::transposition_matrix_product_retval<TranspositionsDerived, Derived, OnTheRight>
331operator*(const MatrixBase<Derived>& matrix,
332 const TranspositionsBase<TranspositionsDerived> &transpositions)
333{
334 return internal::transposition_matrix_product_retval
335 <TranspositionsDerived, Derived, OnTheRight>
336 (transpositions.derived(), matrix.derived());
337}
338
341template<typename Derived, typename TranspositionDerived>
342inline const internal::transposition_matrix_product_retval
343 <TranspositionDerived, Derived, OnTheLeft>
344operator*(const TranspositionsBase<TranspositionDerived> &transpositions,
345 const MatrixBase<Derived>& matrix)
346{
347 return internal::transposition_matrix_product_retval
348 <TranspositionDerived, Derived, OnTheLeft>
349 (transpositions.derived(), matrix.derived());
350}
351
352namespace internal {
353
354template<typename TranspositionType, typename MatrixType, int Side, bool Transposed>
355struct traits<transposition_matrix_product_retval<TranspositionType, MatrixType, Side, Transposed> >
356{
357 typedef typename MatrixType::PlainObject ReturnType;
358};
359
360template<typename TranspositionType, typename MatrixType, int Side, bool Transposed>
361struct transposition_matrix_product_retval
362 : public ReturnByValue<transposition_matrix_product_retval<TranspositionType, MatrixType, Side, Transposed> >
363{
364 typedef typename remove_all<typename MatrixType::Nested>::type MatrixTypeNestedCleaned;
365 typedef typename TranspositionType::Index Index;
366
367 transposition_matrix_product_retval(const TranspositionType& tr, const MatrixType& matrix)
368 : m_transpositions(tr), m_matrix(matrix)
369 {}
370
371 inline int rows() const { return m_matrix.rows(); }
372 inline int cols() const { return m_matrix.cols(); }
373
374 template<typename Dest> inline void evalTo(Dest& dst) const
375 {
376 const int size = m_transpositions.size();
377 Index j = 0;
378
379 if(!(is_same<MatrixTypeNestedCleaned,Dest>::value && extract_data(dst) == extract_data(m_matrix)))
380 dst = m_matrix;
381
382 for(int k=(Transposed?size-1:0) ; Transposed?k>=0:k<size ; Transposed?--k:++k)
383 if((j=m_transpositions.coeff(k))!=k)
384 {
385 if(Side==OnTheLeft)
386 dst.row(k).swap(dst.row(j));
387 else if(Side==OnTheRight)
388 dst.col(k).swap(dst.col(j));
389 }
390 }
391
392 protected:
393 const TranspositionType& m_transpositions;
394 typename MatrixType::Nested m_matrix;
395};
396
397} // end namespace internal
398
399/* Template partial specialization for transposed/inverse transpositions */
400
401template<typename TranspositionsDerived>
402class Transpose<TranspositionsBase<TranspositionsDerived> >
403{
404 typedef TranspositionsDerived TranspositionType;
405 typedef typename TranspositionType::IndicesType IndicesType;
406 public:
407
408 Transpose(const TranspositionType& t) : m_transpositions(t) {}
409
410 inline int size() const { return m_transpositions.size(); }
411
414 template<typename Derived> friend
415 inline const internal::transposition_matrix_product_retval<TranspositionType, Derived, OnTheRight, true>
416 operator*(const MatrixBase<Derived>& matrix, const Transpose& trt)
417 {
418 return internal::transposition_matrix_product_retval<TranspositionType, Derived, OnTheRight, true>(trt.m_transpositions, matrix.derived());
419 }
420
423 template<typename Derived>
424 inline const internal::transposition_matrix_product_retval<TranspositionType, Derived, OnTheLeft, true>
425 operator*(const MatrixBase<Derived>& matrix) const
426 {
427 return internal::transposition_matrix_product_retval<TranspositionType, Derived, OnTheLeft, true>(m_transpositions, matrix.derived());
428 }
429
430 protected:
431 const TranspositionType& m_transpositions;
432};
433
434} // end namespace Eigen
435
436#endif // EIGEN_TRANSPOSITIONS_H
A matrix or vector expression mapping an existing array of data.
Definition Map.h:106
Map(PointerArgType data, const StrideType &stride=StrideType())
Definition Map.h:139
Base class for all dense matrices, vectors, and expressions.
Definition MatrixBase.h:50
Expression of the transpose of a matrix.
Definition Transpose.h:59
Represents a sequence of transpositions (row/column interchange)
Definition Transpositions.h:157
IndicesType & indices()
Definition Transpositions.h:209
Transpositions(const TranspositionsBase< OtherDerived > &other)
Definition Transpositions.h:169
Transpositions(const MatrixBase< Other > &indices)
Definition Transpositions.h:180
Transpositions(Index size)
Definition Transpositions.h:203
const IndicesType & indices() const
Definition Transpositions.h:207
Transpositions & operator=(const TranspositionsBase< OtherDerived > &other)
Definition Transpositions.h:185
@ OnTheLeft
Definition Constants.h:270
@ OnTheRight
Definition Constants.h:272
Definition LDLT.h:18