File indexing completed on 2025-01-18 09:56:18
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef EIGEN_TRANSPOSITIONS_H
0011 #define EIGEN_TRANSPOSITIONS_H
0012
0013 namespace Eigen {
0014
0015 template<typename Derived>
0016 class TranspositionsBase
0017 {
0018 typedef internal::traits<Derived> Traits;
0019
0020 public:
0021
0022 typedef typename Traits::IndicesType IndicesType;
0023 typedef typename IndicesType::Scalar StorageIndex;
0024 typedef Eigen::Index Index;
0025
0026 EIGEN_DEVICE_FUNC
0027 Derived& derived() { return *static_cast<Derived*>(this); }
0028 EIGEN_DEVICE_FUNC
0029 const Derived& derived() const { return *static_cast<const Derived*>(this); }
0030
0031
0032 template<typename OtherDerived>
0033 Derived& operator=(const TranspositionsBase<OtherDerived>& other)
0034 {
0035 indices() = other.indices();
0036 return derived();
0037 }
0038
0039
0040 EIGEN_DEVICE_FUNC
0041 Index size() const { return indices().size(); }
0042
0043 EIGEN_DEVICE_FUNC
0044 Index rows() const { return indices().size(); }
0045
0046 EIGEN_DEVICE_FUNC
0047 Index cols() const { return indices().size(); }
0048
0049
0050 EIGEN_DEVICE_FUNC
0051 inline const StorageIndex& coeff(Index i) const { return indices().coeff(i); }
0052
0053 inline StorageIndex& coeffRef(Index i) { return indices().coeffRef(i); }
0054
0055 inline const StorageIndex& operator()(Index i) const { return indices()(i); }
0056
0057 inline StorageIndex& operator()(Index i) { return indices()(i); }
0058
0059 inline const StorageIndex& operator[](Index i) const { return indices()(i); }
0060
0061 inline StorageIndex& operator[](Index i) { return indices()(i); }
0062
0063
0064 EIGEN_DEVICE_FUNC
0065 const IndicesType& indices() const { return derived().indices(); }
0066
0067 EIGEN_DEVICE_FUNC
0068 IndicesType& indices() { return derived().indices(); }
0069
0070
0071 inline void resize(Index newSize)
0072 {
0073 indices().resize(newSize);
0074 }
0075
0076
0077 void setIdentity()
0078 {
0079 for(StorageIndex i = 0; i < indices().size(); ++i)
0080 coeffRef(i) = i;
0081 }
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105 inline Transpose<TranspositionsBase> inverse() const
0106 { return Transpose<TranspositionsBase>(derived()); }
0107
0108
0109 inline Transpose<TranspositionsBase> transpose() const
0110 { return Transpose<TranspositionsBase>(derived()); }
0111
0112 protected:
0113 };
0114
0115 namespace internal {
0116 template<int SizeAtCompileTime, int MaxSizeAtCompileTime, typename _StorageIndex>
0117 struct traits<Transpositions<SizeAtCompileTime,MaxSizeAtCompileTime,_StorageIndex> >
0118 : traits<PermutationMatrix<SizeAtCompileTime,MaxSizeAtCompileTime,_StorageIndex> >
0119 {
0120 typedef Matrix<_StorageIndex, SizeAtCompileTime, 1, 0, MaxSizeAtCompileTime, 1> IndicesType;
0121 typedef TranspositionsStorage StorageKind;
0122 };
0123 }
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154 template<int SizeAtCompileTime, int MaxSizeAtCompileTime, typename _StorageIndex>
0155 class Transpositions : public TranspositionsBase<Transpositions<SizeAtCompileTime,MaxSizeAtCompileTime,_StorageIndex> >
0156 {
0157 typedef internal::traits<Transpositions> Traits;
0158 public:
0159
0160 typedef TranspositionsBase<Transpositions> Base;
0161 typedef typename Traits::IndicesType IndicesType;
0162 typedef typename IndicesType::Scalar StorageIndex;
0163
0164 inline Transpositions() {}
0165
0166
0167 template<typename OtherDerived>
0168 inline Transpositions(const TranspositionsBase<OtherDerived>& other)
0169 : m_indices(other.indices()) {}
0170
0171
0172 template<typename Other>
0173 explicit inline Transpositions(const MatrixBase<Other>& indices) : m_indices(indices)
0174 {}
0175
0176
0177 template<typename OtherDerived>
0178 Transpositions& operator=(const TranspositionsBase<OtherDerived>& other)
0179 {
0180 return Base::operator=(other);
0181 }
0182
0183
0184
0185 inline Transpositions(Index size) : m_indices(size)
0186 {}
0187
0188
0189 EIGEN_DEVICE_FUNC
0190 const IndicesType& indices() const { return m_indices; }
0191
0192 EIGEN_DEVICE_FUNC
0193 IndicesType& indices() { return m_indices; }
0194
0195 protected:
0196
0197 IndicesType m_indices;
0198 };
0199
0200
0201 namespace internal {
0202 template<int SizeAtCompileTime, int MaxSizeAtCompileTime, typename _StorageIndex, int _PacketAccess>
0203 struct traits<Map<Transpositions<SizeAtCompileTime,MaxSizeAtCompileTime,_StorageIndex>,_PacketAccess> >
0204 : traits<PermutationMatrix<SizeAtCompileTime,MaxSizeAtCompileTime,_StorageIndex> >
0205 {
0206 typedef Map<const Matrix<_StorageIndex,SizeAtCompileTime,1,0,MaxSizeAtCompileTime,1>, _PacketAccess> IndicesType;
0207 typedef _StorageIndex StorageIndex;
0208 typedef TranspositionsStorage StorageKind;
0209 };
0210 }
0211
0212 template<int SizeAtCompileTime, int MaxSizeAtCompileTime, typename _StorageIndex, int PacketAccess>
0213 class Map<Transpositions<SizeAtCompileTime,MaxSizeAtCompileTime,_StorageIndex>,PacketAccess>
0214 : public TranspositionsBase<Map<Transpositions<SizeAtCompileTime,MaxSizeAtCompileTime,_StorageIndex>,PacketAccess> >
0215 {
0216 typedef internal::traits<Map> Traits;
0217 public:
0218
0219 typedef TranspositionsBase<Map> Base;
0220 typedef typename Traits::IndicesType IndicesType;
0221 typedef typename IndicesType::Scalar StorageIndex;
0222
0223 explicit inline Map(const StorageIndex* indicesPtr)
0224 : m_indices(indicesPtr)
0225 {}
0226
0227 inline Map(const StorageIndex* indicesPtr, Index size)
0228 : m_indices(indicesPtr,size)
0229 {}
0230
0231
0232 template<typename OtherDerived>
0233 Map& operator=(const TranspositionsBase<OtherDerived>& other)
0234 {
0235 return Base::operator=(other);
0236 }
0237
0238 #ifndef EIGEN_PARSED_BY_DOXYGEN
0239
0240
0241
0242 Map& operator=(const Map& other)
0243 {
0244 m_indices = other.m_indices;
0245 return *this;
0246 }
0247 #endif
0248
0249
0250 EIGEN_DEVICE_FUNC
0251 const IndicesType& indices() const { return m_indices; }
0252
0253
0254 EIGEN_DEVICE_FUNC
0255 IndicesType& indices() { return m_indices; }
0256
0257 protected:
0258
0259 IndicesType m_indices;
0260 };
0261
0262 namespace internal {
0263 template<typename _IndicesType>
0264 struct traits<TranspositionsWrapper<_IndicesType> >
0265 : traits<PermutationWrapper<_IndicesType> >
0266 {
0267 typedef TranspositionsStorage StorageKind;
0268 };
0269 }
0270
0271 template<typename _IndicesType>
0272 class TranspositionsWrapper
0273 : public TranspositionsBase<TranspositionsWrapper<_IndicesType> >
0274 {
0275 typedef internal::traits<TranspositionsWrapper> Traits;
0276 public:
0277
0278 typedef TranspositionsBase<TranspositionsWrapper> Base;
0279 typedef typename Traits::IndicesType IndicesType;
0280 typedef typename IndicesType::Scalar StorageIndex;
0281
0282 explicit inline TranspositionsWrapper(IndicesType& indices)
0283 : m_indices(indices)
0284 {}
0285
0286
0287 template<typename OtherDerived>
0288 TranspositionsWrapper& operator=(const TranspositionsBase<OtherDerived>& other)
0289 {
0290 return Base::operator=(other);
0291 }
0292
0293
0294 EIGEN_DEVICE_FUNC
0295 const IndicesType& indices() const { return m_indices; }
0296
0297
0298 EIGEN_DEVICE_FUNC
0299 IndicesType& indices() { return m_indices; }
0300
0301 protected:
0302
0303 typename IndicesType::Nested m_indices;
0304 };
0305
0306
0307
0308
0309
0310 template<typename MatrixDerived, typename TranspositionsDerived>
0311 EIGEN_DEVICE_FUNC
0312 const Product<MatrixDerived, TranspositionsDerived, AliasFreeProduct>
0313 operator*(const MatrixBase<MatrixDerived> &matrix,
0314 const TranspositionsBase<TranspositionsDerived>& transpositions)
0315 {
0316 return Product<MatrixDerived, TranspositionsDerived, AliasFreeProduct>
0317 (matrix.derived(), transpositions.derived());
0318 }
0319
0320
0321
0322 template<typename TranspositionsDerived, typename MatrixDerived>
0323 EIGEN_DEVICE_FUNC
0324 const Product<TranspositionsDerived, MatrixDerived, AliasFreeProduct>
0325 operator*(const TranspositionsBase<TranspositionsDerived> &transpositions,
0326 const MatrixBase<MatrixDerived>& matrix)
0327 {
0328 return Product<TranspositionsDerived, MatrixDerived, AliasFreeProduct>
0329 (transpositions.derived(), matrix.derived());
0330 }
0331
0332
0333
0334 namespace internal {
0335
0336 template<typename Derived>
0337 struct traits<Transpose<TranspositionsBase<Derived> > >
0338 : traits<Derived>
0339 {};
0340
0341 }
0342
0343 template<typename TranspositionsDerived>
0344 class Transpose<TranspositionsBase<TranspositionsDerived> >
0345 {
0346 typedef TranspositionsDerived TranspositionType;
0347 typedef typename TranspositionType::IndicesType IndicesType;
0348 public:
0349
0350 explicit Transpose(const TranspositionType& t) : m_transpositions(t) {}
0351
0352 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0353 Index size() const EIGEN_NOEXCEPT { return m_transpositions.size(); }
0354 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0355 Index rows() const EIGEN_NOEXCEPT { return m_transpositions.size(); }
0356 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0357 Index cols() const EIGEN_NOEXCEPT { return m_transpositions.size(); }
0358
0359
0360
0361 template<typename OtherDerived> friend
0362 const Product<OtherDerived, Transpose, AliasFreeProduct>
0363 operator*(const MatrixBase<OtherDerived>& matrix, const Transpose& trt)
0364 {
0365 return Product<OtherDerived, Transpose, AliasFreeProduct>(matrix.derived(), trt);
0366 }
0367
0368
0369
0370 template<typename OtherDerived>
0371 const Product<Transpose, OtherDerived, AliasFreeProduct>
0372 operator*(const MatrixBase<OtherDerived>& matrix) const
0373 {
0374 return Product<Transpose, OtherDerived, AliasFreeProduct>(*this, matrix.derived());
0375 }
0376
0377 EIGEN_DEVICE_FUNC
0378 const TranspositionType& nestedExpression() const { return m_transpositions; }
0379
0380 protected:
0381 const TranspositionType& m_transpositions;
0382 };
0383
0384 }
0385
0386 #endif