Eigen  5.0.1-dev+master
 
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 (Index i = 0; i < indices().size(); ++i) coeffRef(i) = internal::convert_index<StorageIndex>(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
157
158template <int SizeAtCompileTime, int MaxSizeAtCompileTime, typename StorageIndex_>
159class Transpositions
160 : public TranspositionsBase<Transpositions<SizeAtCompileTime, MaxSizeAtCompileTime, StorageIndex_> > {
161 using Traits = internal::traits<Transpositions>;
162
163 public:
164 using Base = TranspositionsBase<Transpositions>;
165 using IndicesType = typename Traits::IndicesType;
166 using StorageIndex = typename IndicesType::Scalar;
167
168 inline Transpositions() {}
169
171 template <typename OtherDerived>
172 inline Transpositions(const TranspositionsBase<OtherDerived>& other) : m_indices(other.indices()) {}
173
175 template <typename Other>
176 explicit inline Transpositions(const MatrixBase<Other>& indices) : m_indices(indices) {}
177
179 template <typename OtherDerived>
180 Transpositions& operator=(const TranspositionsBase<OtherDerived>& other) {
181 return Base::operator=(other);
182 }
183
186 inline Transpositions(Index size) : m_indices(size) {}
187
189 EIGEN_DEVICE_FUNC const IndicesType& indices() const { return m_indices; }
191 EIGEN_DEVICE_FUNC IndicesType& indices() { return m_indices; }
192
193 protected:
194 IndicesType m_indices;
195};
196
197namespace internal {
198template <int SizeAtCompileTime, int MaxSizeAtCompileTime, typename StorageIndex_, int PacketAccess_>
199struct traits<Map<Transpositions<SizeAtCompileTime, MaxSizeAtCompileTime, StorageIndex_>, PacketAccess_> >
200 : traits<PermutationMatrix<SizeAtCompileTime, MaxSizeAtCompileTime, StorageIndex_> > {
201 using IndicesType = Map<const Matrix<StorageIndex_, SizeAtCompileTime, 1, 0, MaxSizeAtCompileTime, 1>, PacketAccess_>;
202 using StorageIndex = StorageIndex_;
203 using StorageKind = TranspositionsStorage;
204};
205} // namespace internal
206
207template <int SizeAtCompileTime, int MaxSizeAtCompileTime, typename StorageIndex_, int PacketAccess>
208class Map<Transpositions<SizeAtCompileTime, MaxSizeAtCompileTime, StorageIndex_>, PacketAccess>
209 : public TranspositionsBase<
210 Map<Transpositions<SizeAtCompileTime, MaxSizeAtCompileTime, StorageIndex_>, PacketAccess> > {
211 using Traits = internal::traits<Map>;
212
213 public:
214 using Base = TranspositionsBase<Map>;
215 using IndicesType = typename Traits::IndicesType;
216 using StorageIndex = typename IndicesType::Scalar;
217
218 explicit inline Map(const StorageIndex* indicesPtr) : m_indices(indicesPtr) {}
219
220 inline Map(const StorageIndex* indicesPtr, Index size) : m_indices(indicesPtr, size) {}
221
223 template <typename OtherDerived>
224 Map& operator=(const TranspositionsBase<OtherDerived>& other) {
225 return Base::operator=(other);
226 }
227
228#ifndef EIGEN_PARSED_BY_DOXYGEN
232 Map& operator=(const Map& other) {
233 m_indices = other.m_indices;
234 return *this;
235 }
236#endif
237
239 EIGEN_DEVICE_FUNC const IndicesType& indices() const { return m_indices; }
240
242 EIGEN_DEVICE_FUNC IndicesType& indices() { return m_indices; }
243
244 protected:
245 IndicesType m_indices;
246};
247
248namespace internal {
249template <typename IndicesType_>
250struct traits<TranspositionsWrapper<IndicesType_> > : traits<PermutationWrapper<IndicesType_> > {
251 using StorageKind = TranspositionsStorage;
252};
253} // namespace internal
254
255template <typename IndicesType_>
256class TranspositionsWrapper : public TranspositionsBase<TranspositionsWrapper<IndicesType_> > {
257 using Traits = internal::traits<TranspositionsWrapper>;
258
259 public:
260 using Base = TranspositionsBase<TranspositionsWrapper>;
261 using IndicesType = typename Traits::IndicesType;
262 using StorageIndex = typename IndicesType::Scalar;
263
264 explicit inline TranspositionsWrapper(IndicesType& indices) : m_indices(indices) {}
265
267 template <typename OtherDerived>
268 TranspositionsWrapper& operator=(const TranspositionsBase<OtherDerived>& other) {
269 return Base::operator=(other);
270 }
271
273 EIGEN_DEVICE_FUNC const IndicesType& indices() const { return m_indices; }
274
276 EIGEN_DEVICE_FUNC IndicesType& indices() { return m_indices; }
277
278 protected:
279 typename IndicesType::Nested m_indices;
280};
281
284template <typename MatrixDerived, typename TranspositionsDerived>
286 const MatrixBase<MatrixDerived>& matrix, const TranspositionsBase<TranspositionsDerived>& transpositions) {
287 return Product<MatrixDerived, TranspositionsDerived, AliasFreeProduct>(matrix.derived(), transpositions.derived());
288}
289
292template <typename TranspositionsDerived, typename MatrixDerived>
294 const TranspositionsBase<TranspositionsDerived>& transpositions, const MatrixBase<MatrixDerived>& matrix) {
295 return Product<TranspositionsDerived, MatrixDerived, AliasFreeProduct>(transpositions.derived(), matrix.derived());
296}
297
298// Template partial specialization for transposed/inverse transpositions
299
300namespace internal {
301
302template <typename Derived>
303struct traits<Transpose<TranspositionsBase<Derived> > > : traits<Derived> {};
304
305} // end namespace internal
306
307template <typename TranspositionsDerived>
308class Transpose<TranspositionsBase<TranspositionsDerived> > {
309 using TranspositionType = TranspositionsDerived;
310 using IndicesType = typename TranspositionType::IndicesType;
311
312 public:
313 explicit Transpose(const TranspositionType& t) : m_transpositions(t) {}
314
315 EIGEN_DEVICE_FUNC constexpr Index size() const noexcept { return m_transpositions.size(); }
316 EIGEN_DEVICE_FUNC constexpr Index rows() const noexcept { return m_transpositions.size(); }
317 EIGEN_DEVICE_FUNC constexpr Index cols() const noexcept { return m_transpositions.size(); }
318
321 template <typename OtherDerived>
322 friend const Product<OtherDerived, Transpose, AliasFreeProduct> operator*(const MatrixBase<OtherDerived>& matrix,
323 const Transpose& trt) {
324 return Product<OtherDerived, Transpose, AliasFreeProduct>(matrix.derived(), trt);
325 }
326
329 template <typename OtherDerived>
330 const Product<Transpose, OtherDerived, AliasFreeProduct> operator*(const MatrixBase<OtherDerived>& matrix) const {
331 return Product<Transpose, OtherDerived, AliasFreeProduct>(*this, matrix.derived());
332 }
333
334 EIGEN_DEVICE_FUNC const TranspositionType& nestedExpression() const { return m_transpositions; }
335
336 protected:
337 const TranspositionType& m_transpositions;
338};
339
340} // end namespace Eigen
341
342#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:225
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:160
Transpositions(const TranspositionsBase< OtherDerived > &other)
Definition Transpositions.h:172
const IndicesType & indices() const
Definition Transpositions.h:189
Transpositions & operator=(const TranspositionsBase< OtherDerived > &other)
Definition Transpositions.h:180
IndicesType & indices()
Definition Transpositions.h:191
Transpositions(Index size)
Definition Transpositions.h:186
Transpositions(const MatrixBase< Other > &indices)
Definition Transpositions.h:176