Eigen  3.3.9
 
Loading...
Searching...
No Matches
Transpose.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
5// Copyright (C) 2009-2014 Gael Guennebaud <gael.guennebaud@inria.fr>
6//
7// This Source Code Form is subject to the terms of the Mozilla
8// Public License v. 2.0. If a copy of the MPL was not distributed
9// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10
11#ifndef EIGEN_TRANSPOSE_H
12#define EIGEN_TRANSPOSE_H
13
14namespace Eigen {
15
16namespace internal {
17template<typename MatrixType>
18struct traits<Transpose<MatrixType> > : public traits<MatrixType>
19{
20 typedef typename ref_selector<MatrixType>::type MatrixTypeNested;
21 typedef typename remove_reference<MatrixTypeNested>::type MatrixTypeNestedPlain;
22 enum {
23 RowsAtCompileTime = MatrixType::ColsAtCompileTime,
24 ColsAtCompileTime = MatrixType::RowsAtCompileTime,
25 MaxRowsAtCompileTime = MatrixType::MaxColsAtCompileTime,
26 MaxColsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
27 FlagsLvalueBit = is_lvalue<MatrixType>::value ? LvalueBit : 0,
28 Flags0 = traits<MatrixTypeNestedPlain>::Flags & ~(LvalueBit | NestByRefBit),
29 Flags1 = Flags0 | FlagsLvalueBit,
30 Flags = Flags1 ^ RowMajorBit,
31 InnerStrideAtCompileTime = inner_stride_at_compile_time<MatrixType>::ret,
32 OuterStrideAtCompileTime = outer_stride_at_compile_time<MatrixType>::ret
33 };
34};
35}
36
37template<typename MatrixType, typename StorageKind> class TransposeImpl;
38
52template<typename MatrixType> class Transpose
53 : public TransposeImpl<MatrixType,typename internal::traits<MatrixType>::StorageKind>
54{
55 public:
56
57 typedef typename internal::ref_selector<MatrixType>::non_const_type MatrixTypeNested;
58
59 typedef typename TransposeImpl<MatrixType,typename internal::traits<MatrixType>::StorageKind>::Base Base;
60 EIGEN_GENERIC_PUBLIC_INTERFACE(Transpose)
61 typedef typename internal::remove_all<MatrixType>::type NestedExpression;
62
63 EIGEN_DEVICE_FUNC
64 explicit inline Transpose(MatrixType& matrix) : m_matrix(matrix) {}
65
66 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Transpose)
67
68 EIGEN_DEVICE_FUNC inline Index rows() const { return m_matrix.cols(); }
69 EIGEN_DEVICE_FUNC inline Index cols() const { return m_matrix.rows(); }
70
72 EIGEN_DEVICE_FUNC
73 const typename internal::remove_all<MatrixTypeNested>::type&
74 nestedExpression() const { return m_matrix; }
75
77 EIGEN_DEVICE_FUNC
78 typename internal::remove_reference<MatrixTypeNested>::type&
79 nestedExpression() { return m_matrix; }
80
82 void resize(Index nrows, Index ncols) {
83 m_matrix.resize(ncols,nrows);
84 }
85
86 protected:
87 typename internal::ref_selector<MatrixType>::non_const_type m_matrix;
88};
89
90namespace internal {
91
92template<typename MatrixType, bool HasDirectAccess = has_direct_access<MatrixType>::ret>
93struct TransposeImpl_base
94{
95 typedef typename dense_xpr_base<Transpose<MatrixType> >::type type;
96};
97
98template<typename MatrixType>
99struct TransposeImpl_base<MatrixType, false>
100{
101 typedef typename dense_xpr_base<Transpose<MatrixType> >::type type;
102};
103
104} // end namespace internal
105
106// Generic API dispatcher
107template<typename XprType, typename StorageKind>
108class TransposeImpl
109 : public internal::generic_xpr_base<Transpose<XprType> >::type
110{
111public:
112 typedef typename internal::generic_xpr_base<Transpose<XprType> >::type Base;
113};
114
115template<typename MatrixType> class TransposeImpl<MatrixType,Dense>
116 : public internal::TransposeImpl_base<MatrixType>::type
117{
118 public:
119
120 typedef typename internal::TransposeImpl_base<MatrixType>::type Base;
121 using Base::coeffRef;
122 EIGEN_DENSE_PUBLIC_INTERFACE(Transpose<MatrixType>)
123 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(TransposeImpl)
124
125 EIGEN_DEVICE_FUNC inline Index innerStride() const { return derived().nestedExpression().innerStride(); }
126 EIGEN_DEVICE_FUNC inline Index outerStride() const { return derived().nestedExpression().outerStride(); }
127
128 typedef typename internal::conditional<
129 internal::is_lvalue<MatrixType>::value,
130 Scalar,
131 const Scalar
132 >::type ScalarWithConstIfNotLvalue;
133
134 EIGEN_DEVICE_FUNC inline ScalarWithConstIfNotLvalue* data() { return derived().nestedExpression().data(); }
135 EIGEN_DEVICE_FUNC inline const Scalar* data() const { return derived().nestedExpression().data(); }
136
137 // FIXME: shall we keep the const version of coeffRef?
138 EIGEN_DEVICE_FUNC
139 inline const Scalar& coeffRef(Index rowId, Index colId) const
140 {
141 return derived().nestedExpression().coeffRef(colId, rowId);
142 }
143
144 EIGEN_DEVICE_FUNC
145 inline const Scalar& coeffRef(Index index) const
146 {
147 return derived().nestedExpression().coeffRef(index);
148 }
149 protected:
150 EIGEN_DEFAULT_EMPTY_CONSTRUCTOR_AND_DESTRUCTOR(TransposeImpl)
151};
152
172template<typename Derived>
173EIGEN_DEVICE_FUNC inline Transpose<Derived>
175{
176 return TransposeReturnType(derived());
177}
178
184template<typename Derived>
185EIGEN_DEVICE_FUNC inline typename DenseBase<Derived>::ConstTransposeReturnType
187{
188 return ConstTransposeReturnType(derived());
189}
190
202 * m.adjointInPlace();
203 * \endcode
204 * which gives Eigen good opportunities for optimization, or alternatively you can also do:
205 * \code
206 * m = m.adjoint().eval();
207 * \endcode
208 *
209 * \sa adjointInPlace(), transpose(), conjugate(), class Transpose, class internal::scalar_conjugate_op */
210template<typename Derived>
211EIGEN_DEVICE_FUNC inline const typename MatrixBase<Derived>::AdjointReturnType
213{
214 return AdjointReturnType(this->transpose());
215}
216
217/***************************************************************************
218* "in place" transpose implementation
219***************************************************************************/
220
221namespace internal {
222
223template<typename MatrixType,
224 bool IsSquare = (MatrixType::RowsAtCompileTime == MatrixType::ColsAtCompileTime) && MatrixType::RowsAtCompileTime!=Dynamic,
225 bool MatchPacketSize =
226 (int(MatrixType::RowsAtCompileTime) == int(internal::packet_traits<typename MatrixType::Scalar>::size))
227 && (internal::evaluator<MatrixType>::Flags&PacketAccessBit) >
228struct inplace_transpose_selector;
229
230template<typename MatrixType>
231struct inplace_transpose_selector<MatrixType,true,false> { // square matrix
232 static void run(MatrixType& m) {
233 m.matrix().template triangularView<StrictlyUpper>().swap(m.matrix().transpose());
234 }
235};
236
237// TODO: vectorized path is currently limited to LargestPacketSize x LargestPacketSize cases only.
238template<typename MatrixType>
239struct inplace_transpose_selector<MatrixType,true,true> { // PacketSize x PacketSize
240 static void run(MatrixType& m) {
241 typedef typename MatrixType::Scalar Scalar;
242 typedef typename internal::packet_traits<typename MatrixType::Scalar>::type Packet;
243 const Index PacketSize = internal::packet_traits<Scalar>::size;
244 const Index Alignment = internal::evaluator<MatrixType>::Alignment;
245 PacketBlock<Packet> A;
246 for (Index i=0; i<PacketSize; ++i)
247 A.packet[i] = m.template packetByOuterInner<Alignment>(i,0);
248 internal::ptranspose(A);
249 for (Index i=0; i<PacketSize; ++i)
250 m.template writePacket<Alignment>(m.rowIndexByOuterInner(i,0), m.colIndexByOuterInner(i,0), A.packet[i]);
251 }
252};
253
254template<typename MatrixType,bool MatchPacketSize>
255struct inplace_transpose_selector<MatrixType,false,MatchPacketSize> { // non square matrix
256 static void run(MatrixType& m) {
257 if (m.rows()==m.cols())
258 m.matrix().template triangularView<StrictlyUpper>().swap(m.matrix().transpose());
259 else
260 m = m.transpose().eval();
261 }
262};
263
264} // end namespace internal
265
285template<typename Derived>
286EIGEN_DEVICE_FUNC inline void DenseBase<Derived>::transposeInPlace()
287{
288 eigen_assert((rows() == cols() || (RowsAtCompileTime == Dynamic && ColsAtCompileTime == Dynamic))
289 && "transposeInPlace() called on a non-square non-resizable matrix");
290 internal::inplace_transpose_selector<Derived>::run(derived());
291}
292
293/***************************************************************************
294* "in place" adjoint implementation
295***************************************************************************/
296
316template<typename Derived>
317EIGEN_DEVICE_FUNC inline void MatrixBase<Derived>::adjointInPlace()
318{
319 derived() = adjoint().eval();
320}
322#ifndef EIGEN_NO_DEBUG
323
324// The following is to detect aliasing problems in most common cases.
325
326namespace internal {
327
328template<bool DestIsTransposed, typename OtherDerived>
329struct check_transpose_aliasing_compile_time_selector
330{
331 enum { ret = bool(blas_traits<OtherDerived>::IsTransposed) != DestIsTransposed };
332};
333
334template<bool DestIsTransposed, typename BinOp, typename DerivedA, typename DerivedB>
335struct check_transpose_aliasing_compile_time_selector<DestIsTransposed,CwiseBinaryOp<BinOp,DerivedA,DerivedB> >
336{
337 enum { ret = bool(blas_traits<DerivedA>::IsTransposed) != DestIsTransposed
338 || bool(blas_traits<DerivedB>::IsTransposed) != DestIsTransposed
339 };
340};
341
342template<typename Scalar, bool DestIsTransposed, typename OtherDerived>
343struct check_transpose_aliasing_run_time_selector
344{
345 static bool run(const Scalar* dest, const OtherDerived& src)
346 {
347 return (bool(blas_traits<OtherDerived>::IsTransposed) != DestIsTransposed) && (dest!=0 && dest==(const Scalar*)extract_data(src));
348 }
349};
350
351template<typename Scalar, bool DestIsTransposed, typename BinOp, typename DerivedA, typename DerivedB>
352struct check_transpose_aliasing_run_time_selector<Scalar,DestIsTransposed,CwiseBinaryOp<BinOp,DerivedA,DerivedB> >
353{
354 static bool run(const Scalar* dest, const CwiseBinaryOp<BinOp,DerivedA,DerivedB>& src)
355 {
356 return ((blas_traits<DerivedA>::IsTransposed != DestIsTransposed) && (dest!=0 && dest==(const Scalar*)extract_data(src.lhs())))
357 || ((blas_traits<DerivedB>::IsTransposed != DestIsTransposed) && (dest!=0 && dest==(const Scalar*)extract_data(src.rhs())));
358 }
359};
360
361// the following selector, checkTransposeAliasing_impl, based on MightHaveTransposeAliasing,
362// is because when the condition controlling the assert is known at compile time, ICC emits a warning.
363// This is actually a good warning: in expressions that don't have any transposing, the condition is
364// known at compile time to be false, and using that, we can avoid generating the code of the assert again
365// and again for all these expressions that don't need it.
366
367template<typename Derived, typename OtherDerived,
368 bool MightHaveTransposeAliasing
369 = check_transpose_aliasing_compile_time_selector
370 <blas_traits<Derived>::IsTransposed,OtherDerived>::ret
371 >
372struct checkTransposeAliasing_impl
373{
374 static void run(const Derived& dst, const OtherDerived& other)
375 {
376 eigen_assert((!check_transpose_aliasing_run_time_selector
377 <typename Derived::Scalar,blas_traits<Derived>::IsTransposed,OtherDerived>
378 ::run(extract_data(dst), other))
379 && "aliasing detected during transposition, use transposeInPlace() "
380 "or evaluate the rhs into a temporary using .eval()");
381
382 }
383};
384
385template<typename Derived, typename OtherDerived>
386struct checkTransposeAliasing_impl<Derived, OtherDerived, false>
387{
388 static void run(const Derived&, const OtherDerived&)
389 {
390 }
391};
392
393template<typename Dst, typename Src>
394void check_for_aliasing(const Dst &dst, const Src &src)
395{
396 internal::checkTransposeAliasing_impl<Dst, Src>::run(dst, src);
397}
398
399} // end namespace internal
400
401#endif // EIGEN_NO_DEBUG
402
403} // end namespace Eigen
404
405#endif // EIGEN_TRANSPOSE_H
Generic expression where a coefficient-wise binary operator is applied to two expressions.
Definition CwiseBinaryOp.h:84
const _LhsNested & lhs() const
Definition CwiseBinaryOp.h:132
const _RhsNested & rhs() const
Definition CwiseBinaryOp.h:135
internal::traits< ArrayWrapper< ExpressionType > >::Scalar Scalar
Definition DenseBase.h:66
@ ColsAtCompileTime
Definition DenseBase.h:106
@ RowsAtCompileTime
Definition DenseBase.h:100
void transposeInPlace()
Definition Transpose.h:286
TransposeReturnType transpose()
Definition Transpose.h:174
void adjointInPlace()
Definition Transpose.h:317
const AdjointReturnType adjoint() const
Definition Transpose.h:212
const MatrixType & matrix() const
Definition Transform.h:395
Expression of the transpose of a matrix.
Definition Transpose.h:54
const internal::remove_all< MatrixTypeNested >::type & nestedExpression() const
Definition Transpose.h:74
internal::remove_reference< MatrixTypeNested >::type & nestedExpression()
Definition Transpose.h:79
const unsigned int PacketAccessBit
Definition Constants.h:89
const unsigned int LvalueBit
Definition Constants.h:139
const unsigned int RowMajorBit
Definition Constants.h:61
Namespace containing all symbols from the Eigen library.
Definition A05_PortingFrom2To3.dox:1
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition Meta.h:65
const int Dynamic
Definition Constants.h:21
Definition Constants.h:491