CwiseUnaryView.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) 2009-2010 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_CWISE_UNARY_VIEW_H
11 #define EIGEN_CWISE_UNARY_VIEW_H
12 
13 #include "./InternalHeaderCheck.h"
14 
15 namespace Eigen {
16 
17 namespace internal {
18 template<typename ViewOp, typename MatrixType, typename StrideType>
19 struct traits<CwiseUnaryView<ViewOp, MatrixType, StrideType> >
20  : traits<MatrixType>
21 {
22  typedef typename result_of<
23  ViewOp(const typename traits<MatrixType>::Scalar&)
24  >::type Scalar;
25  typedef typename MatrixType::Nested MatrixTypeNested;
26  typedef remove_all_t<MatrixTypeNested> MatrixTypeNested_;
27  enum {
28  FlagsLvalueBit = is_lvalue<MatrixType>::value ? LvalueBit : 0,
29  Flags = traits<MatrixTypeNested_>::Flags & (RowMajorBit | FlagsLvalueBit | DirectAccessBit), // FIXME DirectAccessBit should not be handled by expressions
30  MatrixTypeInnerStride = inner_stride_at_compile_time<MatrixType>::ret,
31  // need to cast the sizeof's from size_t to int explicitly, otherwise:
32  // "error: no integral type can represent all of the enumerator values
33  InnerStrideAtCompileTime = StrideType::InnerStrideAtCompileTime == 0
34  ? (MatrixTypeInnerStride == Dynamic
35  ? int(Dynamic)
36  : int(MatrixTypeInnerStride) * int(sizeof(typename traits<MatrixType>::Scalar) / sizeof(Scalar)))
37  : int(StrideType::InnerStrideAtCompileTime),
38 
39  OuterStrideAtCompileTime = StrideType::OuterStrideAtCompileTime == 0
40  ? (outer_stride_at_compile_time<MatrixType>::ret == Dynamic
41  ? int(Dynamic)
42  : outer_stride_at_compile_time<MatrixType>::ret * int(sizeof(typename traits<MatrixType>::Scalar) / sizeof(Scalar)))
43  : int(StrideType::OuterStrideAtCompileTime)
44  };
45 };
46 }
47 
48 template<typename ViewOp, typename MatrixType, typename StrideType, typename StorageKind>
49 class CwiseUnaryViewImpl;
50 
64 template<typename ViewOp, typename MatrixType, typename StrideType>
65 class CwiseUnaryView : public CwiseUnaryViewImpl<ViewOp, MatrixType, StrideType, typename internal::traits<MatrixType>::StorageKind>
66 {
67  public:
68 
71  typedef typename internal::ref_selector<MatrixType>::non_const_type MatrixTypeNested;
73 
74  explicit EIGEN_DEVICE_FUNC inline CwiseUnaryView(MatrixType& mat, const ViewOp& func = ViewOp())
75  : m_matrix(mat), m_functor(func) {}
76 
78 
79  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR
80  Index rows() const EIGEN_NOEXCEPT { return m_matrix.rows(); }
81  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR
82  Index cols() const EIGEN_NOEXCEPT { return m_matrix.cols(); }
83 
85  EIGEN_DEVICE_FUNC const ViewOp& functor() const { return m_functor; }
86 
89  nestedExpression() const { return m_matrix; }
90 
92  EIGEN_DEVICE_FUNC std::remove_reference_t<MatrixTypeNested>&
93  nestedExpression() { return m_matrix; }
94 
95  protected:
97  ViewOp m_functor;
98 };
99 
100 // Generic API dispatcher
101 template<typename ViewOp, typename XprType, typename StrideType, typename StorageKind>
103  : public internal::generic_xpr_base<CwiseUnaryView<ViewOp, XprType, StrideType> >::type
104 {
105 public:
106  typedef typename internal::generic_xpr_base<CwiseUnaryView<ViewOp, XprType, StrideType> >::type Base;
107 };
108 
109 template<typename ViewOp, typename MatrixType, typename StrideType>
110 class CwiseUnaryViewImpl<ViewOp,MatrixType,StrideType,Dense>
111  : public internal::dense_xpr_base< CwiseUnaryView<ViewOp, MatrixType, StrideType> >::type
112 {
113  public:
114 
116  typedef typename internal::dense_xpr_base< CwiseUnaryView<ViewOp, MatrixType,StrideType> >::type Base;
117 
120 
121  EIGEN_DEVICE_FUNC inline Scalar* data() { return &(this->coeffRef(0)); }
122  EIGEN_DEVICE_FUNC inline const Scalar* data() const { return &(this->coeff(0)); }
123 
125  {
126  return StrideType::InnerStrideAtCompileTime != 0
127  ? int(StrideType::InnerStrideAtCompileTime)
128  : derived().nestedExpression().innerStride() * sizeof(typename internal::traits<MatrixType>::Scalar) / sizeof(Scalar);
129  }
130 
132  {
133  return StrideType::OuterStrideAtCompileTime != 0
134  ? int(StrideType::OuterStrideAtCompileTime)
135  : derived().nestedExpression().outerStride() * sizeof(typename internal::traits<MatrixType>::Scalar) / sizeof(Scalar);
136  }
137  protected:
139 };
140 
141 } // end namespace Eigen
142 
143 #endif // EIGEN_CWISE_UNARY_VIEW_H
#define EIGEN_GENERIC_PUBLIC_INTERFACE(Derived)
Definition: Macros.h:1149
#define EIGEN_DEFAULT_EMPTY_CONSTRUCTOR_AND_DESTRUCTOR(Derived)
Definition: Macros.h:1133
#define EIGEN_NOEXCEPT
Definition: Macros.h:1260
#define EIGEN_CONSTEXPR
Definition: Macros.h:747
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:883
#define EIGEN_DENSE_PUBLIC_INTERFACE(Derived)
Definition: Macros.h:1168
#define EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Derived)
Definition: Macros.h:1122
int data[]
Matrix< float, 1, Dynamic > MatrixType
CwiseUnaryView< ViewOp, MatrixType, StrideType > Derived
internal::dense_xpr_base< CwiseUnaryView< ViewOp, MatrixType, StrideType > >::type Base
internal::generic_xpr_base< CwiseUnaryView< ViewOp, XprType, StrideType > >::type Base
Generic lvalue expression of a coefficient-wise unary operator of a matrix or a vector.
EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
internal::ref_selector< MatrixType >::non_const_type MatrixTypeNested
MatrixTypeNested m_matrix
internal::remove_all_t< MatrixType > NestedExpression
const ViewOp & functor() const
std::remove_reference_t< MatrixTypeNested > & nestedExpression()
CwiseUnaryViewImpl< ViewOp, MatrixType, StrideType, typename internal::traits< MatrixType >::StorageKind >::Base Base
const internal::remove_all_t< MatrixTypeNested > & nestedExpression() const
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:182
const unsigned int DirectAccessBit
Definition: Constants.h:157
const unsigned int LvalueBit
Definition: Constants.h:146
const unsigned int RowMajorBit
Definition: Constants.h:68
typename remove_all< T >::type remove_all_t
Definition: Meta.h:119
: InteropHeaders
Definition: Core:139
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82
const int Dynamic
Definition: Constants.h:24