WPILibC++ 2023.4.3-108-ge5452e3
Transpositions.h
Go to the documentation of this file.
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
15template<typename Derived>
17{
19
20 public:
21
22 typedef typename Traits::IndicesType IndicesType;
23 typedef typename IndicesType::Scalar StorageIndex;
24 typedef Eigen::Index Index; ///< \deprecated since Eigen 3.3
25
27 Derived& derived() { return *static_cast<Derived*>(this); }
29 const Derived& derived() const { return *static_cast<const Derived*>(this); }
30
31 /** Copies the \a other transpositions into \c *this */
32 template<typename OtherDerived>
34 {
35 indices() = other.indices();
36 return derived();
37 }
38
39 /** \returns the number of transpositions */
41 Index size() const { return indices().size(); }
42 /** \returns the number of rows of the equivalent permutation matrix */
44 Index rows() const { return indices().size(); }
45 /** \returns the number of columns of the equivalent permutation matrix */
47 Index cols() const { return indices().size(); }
48
49 /** Direct access to the underlying index vector */
51 inline const StorageIndex& coeff(Index i) const { return indices().coeff(i); }
52 /** Direct access to the underlying index vector */
53 inline StorageIndex& coeffRef(Index i) { return indices().coeffRef(i); }
54 /** Direct access to the underlying index vector */
55 inline const StorageIndex& operator()(Index i) const { return indices()(i); }
56 /** Direct access to the underlying index vector */
57 inline StorageIndex& operator()(Index i) { return indices()(i); }
58 /** Direct access to the underlying index vector */
59 inline const StorageIndex& operator[](Index i) const { return indices()(i); }
60 /** Direct access to the underlying index vector */
61 inline StorageIndex& operator[](Index i) { return indices()(i); }
62
63 /** const version of indices(). */
65 const IndicesType& indices() const { return derived().indices(); }
66 /** \returns a reference to the stored array representing the transpositions. */
68 IndicesType& indices() { return derived().indices(); }
69
70 /** Resizes to given size. */
71 inline void resize(Index newSize)
72 {
73 indices().resize(newSize);
74 }
75
76 /** Sets \c *this to represents an identity transformation */
78 {
79 for(StorageIndex i = 0; i < indices().size(); ++i)
80 coeffRef(i) = i;
81 }
82
83 // FIXME: do we want such methods ?
84 // might be useful when the target matrix expression is complex, e.g.:
85 // object.matrix().block(..,..,..,..) = trans * object.matrix().block(..,..,..,..);
86 /*
87 template<typename MatrixType>
88 void applyForwardToRows(MatrixType& mat) const
89 {
90 for(Index k=0 ; k<size() ; ++k)
91 if(m_indices(k)!=k)
92 mat.row(k).swap(mat.row(m_indices(k)));
93 }
94
95 template<typename MatrixType>
96 void applyBackwardToRows(MatrixType& mat) const
97 {
98 for(Index k=size()-1 ; k>=0 ; --k)
99 if(m_indices(k)!=k)
100 mat.row(k).swap(mat.row(m_indices(k)));
101 }
102 */
103
104 /** \returns the inverse transformation */
107
108 /** \returns the tranpose transformation */
111
112 protected:
113};
114
115namespace internal {
116template<int SizeAtCompileTime, int MaxSizeAtCompileTime, typename _StorageIndex>
117struct traits<Transpositions<SizeAtCompileTime,MaxSizeAtCompileTime,_StorageIndex> >
118 : traits<PermutationMatrix<SizeAtCompileTime,MaxSizeAtCompileTime,_StorageIndex> >
119{
122};
123}
124
125/** \class Transpositions
126 * \ingroup Core_Module
127 *
128 * \brief Represents a sequence of transpositions (row/column interchange)
129 *
130 * \tparam SizeAtCompileTime the number of transpositions, or Dynamic
131 * \tparam MaxSizeAtCompileTime the maximum number of transpositions, or Dynamic. This optional parameter defaults to SizeAtCompileTime. Most of the time, you should not have to specify it.
132 *
133 * This class represents a permutation transformation as a sequence of \em n transpositions
134 * \f$[T_{n-1} \ldots T_{i} \ldots T_{0}]\f$. It is internally stored as a vector of integers \c indices.
135 * Each transposition \f$ T_{i} \f$ applied on the left of a matrix (\f$ T_{i} M\f$) interchanges
136 * the rows \c i and \c indices[i] of the matrix \c M.
137 * A transposition applied on the right (e.g., \f$ M T_{i}\f$) yields a column interchange.
138 *
139 * Compared to the class PermutationMatrix, such a sequence of transpositions is what is
140 * computed during a decomposition with pivoting, and it is faster when applying the permutation in-place.
141 *
142 * To apply a sequence of transpositions to a matrix, simply use the operator * as in the following example:
143 * \code
144 * Transpositions tr;
145 * MatrixXf mat;
146 * mat = tr * mat;
147 * \endcode
148 * In this example, we detect that the matrix appears on both side, and so the transpositions
149 * are applied in-place without any temporary or extra copy.
150 *
151 * \sa class PermutationMatrix
152 */
153
154template<int SizeAtCompileTime, int MaxSizeAtCompileTime, typename _StorageIndex>
155class Transpositions : public TranspositionsBase<Transpositions<SizeAtCompileTime,MaxSizeAtCompileTime,_StorageIndex> >
156{
158 public:
159
161 typedef typename Traits::IndicesType IndicesType;
162 typedef typename IndicesType::Scalar StorageIndex;
163
164 inline Transpositions() {}
165
166 /** Copy constructor. */
167 template<typename OtherDerived>
169 : m_indices(other.indices()) {}
170
171 /** Generic constructor from expression of the transposition indices. */
172 template<typename Other>
174 {}
175
176 /** Copies the \a other transpositions into \c *this */
177 template<typename OtherDerived>
179 {
180 return Base::operator=(other);
181 }
182
183 /** Constructs an uninitialized permutation matrix of given size.
184 */
186 {}
187
188 /** const version of indices(). */
190 const IndicesType& indices() const { return m_indices; }
191 /** \returns a reference to the stored array representing the transpositions. */
194
195 protected:
196
198};
199
200
201namespace internal {
202template<int SizeAtCompileTime, int MaxSizeAtCompileTime, typename _StorageIndex, int _PacketAccess>
203struct traits<Map<Transpositions<SizeAtCompileTime,MaxSizeAtCompileTime,_StorageIndex>,_PacketAccess> >
204 : traits<PermutationMatrix<SizeAtCompileTime,MaxSizeAtCompileTime,_StorageIndex> >
205{
207 typedef _StorageIndex StorageIndex;
209};
210}
211
212template<int SizeAtCompileTime, int MaxSizeAtCompileTime, typename _StorageIndex, int PacketAccess>
213class Map<Transpositions<SizeAtCompileTime,MaxSizeAtCompileTime,_StorageIndex>,PacketAccess>
214 : public TranspositionsBase<Map<Transpositions<SizeAtCompileTime,MaxSizeAtCompileTime,_StorageIndex>,PacketAccess> >
215{
217 public:
218
220 typedef typename Traits::IndicesType IndicesType;
221 typedef typename IndicesType::Scalar StorageIndex;
222
223 explicit inline Map(const StorageIndex* indicesPtr)
224 : m_indices(indicesPtr)
225 {}
226
227 inline Map(const StorageIndex* indicesPtr, Index size)
228 : m_indices(indicesPtr,size)
229 {}
230
231 /** Copies the \a other transpositions into \c *this */
232 template<typename OtherDerived>
234 {
235 return Base::operator=(other);
236 }
237
238 #ifndef EIGEN_PARSED_BY_DOXYGEN
239 /** This is a special case of the templated operator=. Its purpose is to
240 * prevent a default operator= from hiding the templated operator=.
241 */
242 Map& operator=(const Map& other)
243 {
244 m_indices = other.m_indices;
245 return *this;
246 }
247 #endif
248
249 /** const version of indices(). */
251 const IndicesType& indices() const { return m_indices; }
252
253 /** \returns a reference to the stored array representing the transpositions. */
255 IndicesType& indices() { return m_indices; }
256
257 protected:
258
260};
261
262namespace internal {
263template<typename _IndicesType>
264struct traits<TranspositionsWrapper<_IndicesType> >
265 : traits<PermutationWrapper<_IndicesType> >
266{
268};
269}
270
271template<typename _IndicesType>
273 : public TranspositionsBase<TranspositionsWrapper<_IndicesType> >
274{
276 public:
277
279 typedef typename Traits::IndicesType IndicesType;
280 typedef typename IndicesType::Scalar StorageIndex;
281
284 {}
285
286 /** Copies the \a other transpositions into \c *this */
287 template<typename OtherDerived>
289 {
290 return Base::operator=(other);
291 }
292
293 /** const version of indices(). */
295 const IndicesType& indices() const { return m_indices; }
296
297 /** \returns a reference to the stored array representing the transpositions. */
300
301 protected:
302
303 typename IndicesType::Nested m_indices;
304};
305
306
307
308/** \returns the \a matrix with the \a transpositions applied to the columns.
309 */
310template<typename MatrixDerived, typename TranspositionsDerived>
314 const TranspositionsBase<TranspositionsDerived>& transpositions)
315{
317 (matrix.derived(), transpositions.derived());
318}
319
320/** \returns the \a matrix with the \a transpositions applied to the rows.
321 */
322template<typename TranspositionsDerived, typename MatrixDerived>
324const Product<TranspositionsDerived, MatrixDerived, AliasFreeProduct>
326 const MatrixBase<MatrixDerived>& matrix)
327{
329 (transpositions.derived(), matrix.derived());
330}
331
332// Template partial specialization for transposed/inverse transpositions
333
334namespace internal {
335
336template<typename Derived>
338 : traits<Derived>
339{};
340
341} // end namespace internal
342
343template<typename TranspositionsDerived>
344class Transpose<TranspositionsBase<TranspositionsDerived> >
345{
346 typedef TranspositionsDerived TranspositionType;
347 typedef typename TranspositionType::IndicesType IndicesType;
348 public:
349
350 explicit Transpose(const TranspositionType& t) : m_transpositions(t) {}
351
353 Index size() const EIGEN_NOEXCEPT { return m_transpositions.size(); }
355 Index rows() const EIGEN_NOEXCEPT { return m_transpositions.size(); }
357 Index cols() const EIGEN_NOEXCEPT { return m_transpositions.size(); }
358
359 /** \returns the \a matrix with the inverse transpositions applied to the columns.
360 */
361 template<typename OtherDerived> friend
364 {
365 return Product<OtherDerived, Transpose, AliasFreeProduct>(matrix.derived(), trt);
366 }
367
368 /** \returns the \a matrix with the inverse transpositions applied to the rows.
369 */
370 template<typename OtherDerived>
373 {
374 return Product<Transpose, OtherDerived, AliasFreeProduct>(*this, matrix.derived());
375 }
376
378 const TranspositionType& nestedExpression() const { return m_transpositions; }
379
380 protected:
381 const TranspositionType& m_transpositions;
382};
383
384} // end namespace Eigen
385
386#endif // EIGEN_TRANSPOSITIONS_H
#define EIGEN_NOEXCEPT
Definition: Macros.h:1428
#define EIGEN_CONSTEXPR
Definition: Macros.h:797
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:986
Map & operator=(const Map &other)
This is a special case of the templated operator=.
Definition: Transpositions.h:242
EIGEN_DEVICE_FUNC IndicesType & indices()
Definition: Transpositions.h:255
EIGEN_DEVICE_FUNC const IndicesType & indices() const
const version of indices().
Definition: Transpositions.h:251
Map & operator=(const TranspositionsBase< OtherDerived > &other)
Copies the other transpositions into *this.
Definition: Transpositions.h:233
Map(const StorageIndex *indicesPtr, Index size)
Definition: Transpositions.h:227
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:96
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:50
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:180
Expression of the product of two arbitrary matrices or vectors.
Definition: Product.h:75
EIGEN_DEVICE_FUNC const TranspositionType & nestedExpression() const
Definition: Transpositions.h:378
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
Definition: Transpositions.h:357
friend const Product< OtherDerived, Transpose, AliasFreeProduct > operator*(const MatrixBase< OtherDerived > &matrix, const Transpose &trt)
Definition: Transpositions.h:363
const TranspositionType & m_transpositions
Definition: Transpositions.h:381
Transpose(const TranspositionType &t)
Definition: Transpositions.h:350
const Product< Transpose, OtherDerived, AliasFreeProduct > operator*(const MatrixBase< OtherDerived > &matrix) const
Definition: Transpositions.h:372
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
Definition: Transpositions.h:355
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index size() const EIGEN_NOEXCEPT
Definition: Transpositions.h:353
Expression of the transpose of a matrix.
Definition: Transpose.h:54
Definition: Transpositions.h:17
Transpose< TranspositionsBase > transpose() const
Definition: Transpositions.h:109
void resize(Index newSize)
Resizes to given size.
Definition: Transpositions.h:71
Eigen::Index Index
Definition: Transpositions.h:24
EIGEN_DEVICE_FUNC Index size() const
Definition: Transpositions.h:41
const StorageIndex & operator[](Index i) const
Direct access to the underlying index vector.
Definition: Transpositions.h:59
EIGEN_DEVICE_FUNC const Derived & derived() const
Definition: Transpositions.h:29
EIGEN_DEVICE_FUNC Index cols() const
Definition: Transpositions.h:47
EIGEN_DEVICE_FUNC Index rows() const
Definition: Transpositions.h:44
const StorageIndex & operator()(Index i) const
Direct access to the underlying index vector.
Definition: Transpositions.h:55
EIGEN_DEVICE_FUNC const IndicesType & indices() const
const version of indices().
Definition: Transpositions.h:65
IndicesType::Scalar StorageIndex
Definition: Transpositions.h:23
void setIdentity()
Sets *this to represents an identity transformation.
Definition: Transpositions.h:77
StorageIndex & operator[](Index i)
Direct access to the underlying index vector.
Definition: Transpositions.h:61
Traits::IndicesType IndicesType
Definition: Transpositions.h:22
EIGEN_DEVICE_FUNC const StorageIndex & coeff(Index i) const
Direct access to the underlying index vector.
Definition: Transpositions.h:51
Derived & operator=(const TranspositionsBase< OtherDerived > &other)
Copies the other transpositions into *this.
Definition: Transpositions.h:33
StorageIndex & coeffRef(Index i)
Direct access to the underlying index vector.
Definition: Transpositions.h:53
StorageIndex & operator()(Index i)
Direct access to the underlying index vector.
Definition: Transpositions.h:57
EIGEN_DEVICE_FUNC Derived & derived()
Definition: Transpositions.h:27
EIGEN_DEVICE_FUNC IndicesType & indices()
Definition: Transpositions.h:68
Transpose< TranspositionsBase > inverse() const
Definition: Transpositions.h:105
Represents a sequence of transpositions (row/column interchange)
Definition: Transpositions.h:156
TranspositionsBase< Transpositions > Base
Definition: Transpositions.h:160
IndicesType m_indices
Definition: Transpositions.h:197
Transpositions(const MatrixBase< Other > &indices)
Generic constructor from expression of the transposition indices.
Definition: Transpositions.h:173
Transpositions()
Definition: Transpositions.h:164
EIGEN_DEVICE_FUNC const IndicesType & indices() const
const version of indices().
Definition: Transpositions.h:190
Transpositions(Index size)
Constructs an uninitialized permutation matrix of given size.
Definition: Transpositions.h:185
Traits::IndicesType IndicesType
Definition: Transpositions.h:161
Transpositions & operator=(const TranspositionsBase< OtherDerived > &other)
Copies the other transpositions into *this.
Definition: Transpositions.h:178
Transpositions(const TranspositionsBase< OtherDerived > &other)
Copy constructor.
Definition: Transpositions.h:168
IndicesType::Scalar StorageIndex
Definition: Transpositions.h:162
EIGEN_DEVICE_FUNC IndicesType & indices()
Definition: Transpositions.h:193
Definition: Transpositions.h:274
EIGEN_DEVICE_FUNC const IndicesType & indices() const
const version of indices().
Definition: Transpositions.h:295
TranspositionsWrapper & operator=(const TranspositionsBase< OtherDerived > &other)
Copies the other transpositions into *this.
Definition: Transpositions.h:288
TranspositionsWrapper(IndicesType &indices)
Definition: Transpositions.h:282
TranspositionsBase< TranspositionsWrapper > Base
Definition: Transpositions.h:278
EIGEN_DEVICE_FUNC IndicesType & indices()
Definition: Transpositions.h:299
IndicesType::Scalar StorageIndex
Definition: Transpositions.h:280
Traits::IndicesType IndicesType
Definition: Transpositions.h:279
IndicesType::Nested m_indices
Definition: Transpositions.h:303
EIGEN_CONSTEXPR Index size(const T &x)
Definition: Meta.h:479
Namespace containing all symbols from the Eigen library.
Definition: Core:141
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74
const Product< SparseDerived, PermDerived, AliasFreeProduct > operator*(const SparseMatrixBase< SparseDerived > &matrix, const PermutationBase< PermDerived > &perm)
Definition: SparsePermutation.h:147
Definition: Eigen_Colamd.h:50
The type used to identify a permutation storage.
Definition: Constants.h:519
Map< const Matrix< _StorageIndex, SizeAtCompileTime, 1, 0, MaxSizeAtCompileTime, 1 >, _PacketAccess > IndicesType
Definition: Transpositions.h:206
Matrix< _StorageIndex, SizeAtCompileTime, 1, 0, MaxSizeAtCompileTime, 1 > IndicesType
Definition: Transpositions.h:120
TranspositionsStorage StorageKind
Definition: Transpositions.h:267