Eigen  5.0.1
 
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// SPDX-License-Identifier: MPL-2.0
10
11#ifndef EIGEN_TRANSPOSITIONS_H
12#define EIGEN_TRANSPOSITIONS_H
13
14// IWYU pragma: private
15#include "./InternalHeaderCheck.h"
16
17namespace Eigen {
18
19template <typename Derived>
20class TranspositionsBase {
21 using Traits = internal::traits<Derived>;
22
23 public:
24 using IndicesType = typename Traits::IndicesType;
25 using StorageIndex = typename IndicesType::Scalar;
26 using Index = Eigen::Index;
27
28 EIGEN_DEVICE_FUNC Derived& derived() { return *static_cast<Derived*>(this); }
29 EIGEN_DEVICE_FUNC const Derived& derived() const { return *static_cast<const Derived*>(this); }
30
32 template <typename OtherDerived>
33 Derived& operator=(const TranspositionsBase<OtherDerived>& other) {
34 indices() = other.indices();
35 return derived();
36 }
37
39 EIGEN_DEVICE_FUNC Index size() const { return indices().size(); }
41 EIGEN_DEVICE_FUNC Index rows() const { return indices().size(); }
43 EIGEN_DEVICE_FUNC Index cols() const { return indices().size(); }
44
46 EIGEN_DEVICE_FUNC inline const StorageIndex& coeff(Index i) const { return indices().coeff(i); }
48 inline StorageIndex& coeffRef(Index i) { return indices().coeffRef(i); }
50 inline const StorageIndex& operator()(Index i) const { return indices()(i); }
52 inline StorageIndex& operator()(Index i) { return indices()(i); }
54 inline const StorageIndex& operator[](Index i) const { return indices()(i); }
56 inline StorageIndex& operator[](Index i) { return indices()(i); }
57
59 EIGEN_DEVICE_FUNC const IndicesType& indices() const { return derived().indices(); }
61 EIGEN_DEVICE_FUNC IndicesType& indices() { return derived().indices(); }
62
64 inline void resize(Index newSize) { indices().resize(newSize); }
65
67 void setIdentity() {
68 for (StorageIndex i = 0; i < indices().size(); ++i) coeffRef(i) = i;
69 }
70
71 // FIXME: do we want such methods ?
72 // might be useful when the target matrix expression is complex, e.g.:
73 // object.matrix().block(..,..,..,..) = trans * object.matrix().block(..,..,..,..);
74 /*
75 template<typename MatrixType>
76 void applyForwardToRows(MatrixType& mat) const
77 {
78 for(Index k=0 ; k<size() ; ++k)
79 if(m_indices(k)!=k)
80 mat.row(k).swap(mat.row(m_indices(k)));
81 }
82
83 template<typename MatrixType>
84 void applyBackwardToRows(MatrixType& mat) const
85 {
86 for(Index k=size()-1 ; k>=0 ; --k)
87 if(m_indices(k)!=k)
88 mat.row(k).swap(mat.row(m_indices(k)));
89 }
90 */
91
93 inline Transpose<TranspositionsBase> inverse() const { return Transpose<TranspositionsBase>(derived()); }
94
96 inline Transpose<TranspositionsBase> transpose() const { return Transpose<TranspositionsBase>(derived()); }
97
100 Transpose<TranspositionsBase> adjoint() const { return Transpose<TranspositionsBase>(derived()); }
101};
102
103namespace internal {
104template <int SizeAtCompileTime, int MaxSizeAtCompileTime, typename StorageIndex_>
105struct traits<Transpositions<SizeAtCompileTime, MaxSizeAtCompileTime, StorageIndex_> >
106 : traits<PermutationMatrix<SizeAtCompileTime, MaxSizeAtCompileTime, StorageIndex_> > {
107 using IndicesType = Matrix<StorageIndex_, SizeAtCompileTime, 1, 0, MaxSizeAtCompileTime, 1>;
108 using StorageKind = TranspositionsStorage;
109};
110} // namespace internal
111
141
142template <int SizeAtCompileTime, int MaxSizeAtCompileTime, typename StorageIndex_>
143class Transpositions
144 : public TranspositionsBase<Transpositions<SizeAtCompileTime, MaxSizeAtCompileTime, StorageIndex_> > {
145 using Traits = internal::traits<Transpositions>;
146
147 public:
148 using Base = TranspositionsBase<Transpositions>;
149 using IndicesType = typename Traits::IndicesType;
150 using StorageIndex = typename IndicesType::Scalar;
151
152 inline Transpositions() {}
153
155 template <typename OtherDerived>
156 inline Transpositions(const TranspositionsBase<OtherDerived>& other) : m_indices(other.indices()) {}
157
159 template <typename Other>
160 explicit inline Transpositions(const MatrixBase<Other>& indices) : m_indices(indices) {}
161
163 template <typename OtherDerived>
164 Transpositions& operator=(const TranspositionsBase<OtherDerived>& other) {
165 return Base::operator=(other);
166 }
167
170 inline Transpositions(Index size) : m_indices(size) {}
171
173 EIGEN_DEVICE_FUNC const IndicesType& indices() const { return m_indices; }
175 EIGEN_DEVICE_FUNC IndicesType& indices() { return m_indices; }
176
177 protected:
178 IndicesType m_indices;
179};
180
181namespace internal {
182template <int SizeAtCompileTime, int MaxSizeAtCompileTime, typename StorageIndex_, int PacketAccess_>
183struct traits<Map<Transpositions<SizeAtCompileTime, MaxSizeAtCompileTime, StorageIndex_>, PacketAccess_> >
184 : traits<PermutationMatrix<SizeAtCompileTime, MaxSizeAtCompileTime, StorageIndex_> > {
185 using IndicesType = Map<const Matrix<StorageIndex_, SizeAtCompileTime, 1, 0, MaxSizeAtCompileTime, 1>, PacketAccess_>;
186 using StorageIndex = StorageIndex_;
187 using StorageKind = TranspositionsStorage;
188};
189} // namespace internal
190
191template <int SizeAtCompileTime, int MaxSizeAtCompileTime, typename StorageIndex_, int PacketAccess>
192class Map<Transpositions<SizeAtCompileTime, MaxSizeAtCompileTime, StorageIndex_>, PacketAccess>
193 : public TranspositionsBase<
194 Map<Transpositions<SizeAtCompileTime, MaxSizeAtCompileTime, StorageIndex_>, PacketAccess> > {
195 using Traits = internal::traits<Map>;
196
197 public:
198 using Base = TranspositionsBase<Map>;
199 using IndicesType = typename Traits::IndicesType;
200 using StorageIndex = typename IndicesType::Scalar;
201
202 explicit inline Map(const StorageIndex* indicesPtr) : m_indices(indicesPtr) {}
203
204 inline Map(const StorageIndex* indicesPtr, Index size) : m_indices(indicesPtr, size) {}
205
207 template <typename OtherDerived>
208 Map& operator=(const TranspositionsBase<OtherDerived>& other) {
209 return Base::operator=(other);
210 }
211
212#ifndef EIGEN_PARSED_BY_DOXYGEN
216 Map& operator=(const Map& other) {
217 m_indices = other.m_indices;
218 return *this;
219 }
220#endif
221
223 EIGEN_DEVICE_FUNC const IndicesType& indices() const { return m_indices; }
224
226 EIGEN_DEVICE_FUNC IndicesType& indices() { return m_indices; }
227
228 protected:
229 IndicesType m_indices;
230};
231
232namespace internal {
233template <typename IndicesType_>
234struct traits<TranspositionsWrapper<IndicesType_> > : traits<PermutationWrapper<IndicesType_> > {
235 using StorageKind = TranspositionsStorage;
236};
237} // namespace internal
238
239template <typename IndicesType_>
240class TranspositionsWrapper : public TranspositionsBase<TranspositionsWrapper<IndicesType_> > {
241 using Traits = internal::traits<TranspositionsWrapper>;
242
243 public:
244 using Base = TranspositionsBase<TranspositionsWrapper>;
245 using IndicesType = typename Traits::IndicesType;
246 using StorageIndex = typename IndicesType::Scalar;
247
248 explicit inline TranspositionsWrapper(IndicesType& indices) : m_indices(indices) {}
249
251 template <typename OtherDerived>
252 TranspositionsWrapper& operator=(const TranspositionsBase<OtherDerived>& other) {
253 return Base::operator=(other);
254 }
255
257 EIGEN_DEVICE_FUNC const IndicesType& indices() const { return m_indices; }
258
260 EIGEN_DEVICE_FUNC IndicesType& indices() { return m_indices; }
261
262 protected:
263 typename IndicesType::Nested m_indices;
264};
265
268template <typename MatrixDerived, typename TranspositionsDerived>
270 const MatrixBase<MatrixDerived>& matrix, const TranspositionsBase<TranspositionsDerived>& transpositions) {
271 return Product<MatrixDerived, TranspositionsDerived, AliasFreeProduct>(matrix.derived(), transpositions.derived());
272}
273
276template <typename TranspositionsDerived, typename MatrixDerived>
278 const TranspositionsBase<TranspositionsDerived>& transpositions, const MatrixBase<MatrixDerived>& matrix) {
279 return Product<TranspositionsDerived, MatrixDerived, AliasFreeProduct>(transpositions.derived(), matrix.derived());
280}
281
282// Template partial specialization for transposed/inverse transpositions
283
284namespace internal {
285
286template <typename Derived>
287struct traits<Transpose<TranspositionsBase<Derived> > > : traits<Derived> {};
288
289} // end namespace internal
290
291template <typename TranspositionsDerived>
292class Transpose<TranspositionsBase<TranspositionsDerived> > {
293 using TranspositionType = TranspositionsDerived;
294 using IndicesType = typename TranspositionType::IndicesType;
295
296 public:
297 explicit Transpose(const TranspositionType& t) : m_transpositions(t) {}
298
299 EIGEN_DEVICE_FUNC constexpr Index size() const noexcept { return m_transpositions.size(); }
300 EIGEN_DEVICE_FUNC constexpr Index rows() const noexcept { return m_transpositions.size(); }
301 EIGEN_DEVICE_FUNC constexpr Index cols() const noexcept { return m_transpositions.size(); }
302
305 template <typename OtherDerived>
306 friend const Product<OtherDerived, Transpose, AliasFreeProduct> operator*(const MatrixBase<OtherDerived>& matrix,
307 const Transpose& trt) {
308 return Product<OtherDerived, Transpose, AliasFreeProduct>(matrix.derived(), trt);
309 }
310
313 template <typename OtherDerived>
314 const Product<Transpose, OtherDerived, AliasFreeProduct> operator*(const MatrixBase<OtherDerived>& matrix) const {
315 return Product<Transpose, OtherDerived, AliasFreeProduct>(*this, matrix.derived());
316 }
317
318 EIGEN_DEVICE_FUNC const TranspositionType& nestedExpression() const { return m_transpositions; }
319
320 protected:
321 const TranspositionType& m_transpositions;
322};
323
324} // end namespace Eigen
325
326#endif // EIGEN_TRANSPOSITIONS_H
A matrix or vector expression mapping an existing array of data.
Definition Map.h:97
constexpr Map(PointerArgType dataPtr, const StrideType &stride=StrideType())
Definition Map.h:124
Base class for all dense matrices, vectors, and expressions.
Definition MatrixBase.h:53
Expression of the product of two arbitrary matrices or vectors.
Definition Product.h:203
Expression of the transpose of a matrix.
Definition Transpose.h:57
constexpr const internal::remove_all_t< MatrixTypeNested > & nestedExpression() const
Definition Transpose.h:73
Represents a sequence of transpositions (row/column interchange)
Definition Transpositions.h:144
Transpositions(const TranspositionsBase< OtherDerived > &other)
Definition Transpositions.h:156
const IndicesType & indices() const
Definition Transpositions.h:173
Transpositions & operator=(const TranspositionsBase< OtherDerived > &other)
Definition Transpositions.h:164
IndicesType & indices()
Definition Transpositions.h:175
Transpositions(Index size)
Definition Transpositions.h:170
Transpositions(const MatrixBase< Other > &indices)
Definition Transpositions.h:160