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 // IWYU pragma: private
14 #include "./InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 
18 namespace internal {
19 template <typename ViewOp, typename MatrixType, typename StrideType>
20 struct traits<CwiseUnaryView<ViewOp, MatrixType, StrideType> > : traits<MatrixType> {
21  typedef typename result_of<ViewOp(typename traits<MatrixType>::Scalar&)>::type1 ScalarRef;
22  static_assert(std::is_reference<ScalarRef>::value, "Views must return a reference type.");
24  typedef typename MatrixType::Nested MatrixTypeNested;
26  enum {
27  FlagsLvalueBit = is_lvalue<MatrixType>::value ? LvalueBit : 0,
28  Flags =
30  (RowMajorBit | FlagsLvalueBit | DirectAccessBit), // FIXME DirectAccessBit should not be handled by expressions
32  // need to cast the sizeof's from size_t to int explicitly, otherwise:
33  // "error: no integral type can represent all of the enumerator values
34  InnerStrideAtCompileTime =
35  StrideType::InnerStrideAtCompileTime == 0
36  ? (MatrixTypeInnerStride == Dynamic
37  ? int(Dynamic)
38  : int(MatrixTypeInnerStride) * int(sizeof(typename traits<MatrixType>::Scalar) / sizeof(Scalar)))
39  : int(StrideType::InnerStrideAtCompileTime),
40 
41  OuterStrideAtCompileTime = StrideType::OuterStrideAtCompileTime == 0
43  ? int(Dynamic)
45  int(sizeof(typename traits<MatrixType>::Scalar) / sizeof(Scalar)))
46  : int(StrideType::OuterStrideAtCompileTime)
47  };
48 };
49 
50 // Generic API dispatcher
51 template <typename ViewOp, typename XprType, typename StrideType, typename StorageKind,
52  bool Mutable = !std::is_const<XprType>::value>
53 class CwiseUnaryViewImpl : public generic_xpr_base<CwiseUnaryView<ViewOp, XprType, StrideType> >::type {
54  public:
56 };
57 
58 template <typename ViewOp, typename MatrixType, typename StrideType>
59 class CwiseUnaryViewImpl<ViewOp, MatrixType, StrideType, Dense, false>
60  : public dense_xpr_base<CwiseUnaryView<ViewOp, MatrixType, StrideType> >::type {
61  public:
66 
67  EIGEN_DEVICE_FUNC inline const Scalar* data() const { return &(this->coeffRef(0)); }
68 
70  return StrideType::InnerStrideAtCompileTime != 0 ? int(StrideType::InnerStrideAtCompileTime)
71  : derived().nestedExpression().innerStride() *
72  sizeof(typename traits<MatrixType>::Scalar) / sizeof(Scalar);
73  }
74 
76  return StrideType::OuterStrideAtCompileTime != 0 ? int(StrideType::OuterStrideAtCompileTime)
77  : derived().nestedExpression().outerStride() *
78  sizeof(typename traits<MatrixType>::Scalar) / sizeof(Scalar);
79  }
80 
81  protected:
83 
84  // Allow const access to coeffRef for the case of direct access being enabled.
85  EIGEN_DEVICE_FUNC inline const Scalar& coeffRef(Index index) const {
86  return internal::evaluator<Derived>(derived()).coeffRef(index);
87  }
88 
90  return internal::evaluator<Derived>(derived()).coeffRef(row, col);
91  }
92 };
93 
94 template <typename ViewOp, typename MatrixType, typename StrideType>
95 class CwiseUnaryViewImpl<ViewOp, MatrixType, StrideType, Dense, true>
96  : public CwiseUnaryViewImpl<ViewOp, MatrixType, StrideType, Dense, false> {
97  public:
102 
103  using Base::data;
104  EIGEN_DEVICE_FUNC inline Scalar* data() { return &(this->coeffRef(0)); }
105 
107  return internal::evaluator<Derived>(derived()).coeffRef(row, col);
108  }
109 
111  return internal::evaluator<Derived>(derived()).coeffRef(index);
112  }
113 
114  protected:
116 };
117 
118 } // namespace internal
119 
133 template <typename ViewOp, typename MatrixType, typename StrideType>
134 class CwiseUnaryView : public internal::CwiseUnaryViewImpl<ViewOp, MatrixType, StrideType,
135  typename internal::traits<MatrixType>::StorageKind> {
136  public:
137  typedef typename internal::CwiseUnaryViewImpl<ViewOp, MatrixType, StrideType,
140  typedef typename internal::ref_selector<MatrixType>::non_const_type MatrixTypeNested;
142 
143  explicit EIGEN_DEVICE_FUNC inline CwiseUnaryView(MatrixType& mat, const ViewOp& func = ViewOp())
144  : m_matrix(mat), m_functor(func) {}
145 
147 
150 
152  EIGEN_DEVICE_FUNC const ViewOp& functor() const { return m_functor; }
153 
156 
158  EIGEN_DEVICE_FUNC std::remove_reference_t<MatrixTypeNested>& nestedExpression() { return m_matrix; }
159 
160  protected:
162  ViewOp m_functor;
163 };
164 
165 } // namespace Eigen
166 
167 #endif // EIGEN_CWISE_UNARY_VIEW_H
#define EIGEN_GENERIC_PUBLIC_INTERFACE(Derived)
Definition: Macros.h:1149
#define EIGEN_DEFAULT_EMPTY_CONSTRUCTOR_AND_DESTRUCTOR(Derived)
Macro to manually define default constructors and destructors. This is necessary when the copy constr...
Definition: Macros.h:1137
#define EIGEN_NOEXCEPT
Definition: Macros.h:1267
#define EIGEN_CONSTEXPR
Definition: Macros.h:758
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:892
#define EIGEN_DENSE_PUBLIC_INTERFACE(Derived)
Definition: Macros.h:1171
#define EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Derived)
Macro to manually inherit assignment operators. This is necessary, because the implicitly defined ass...
Definition: Macros.h:1126
#define EIGEN_STRONG_INLINE
Definition: Macros.h:834
int data[]
Definition: Map_placement_new.cpp:1
m col(1)
m row(1)
int rows
Definition: Tutorial_commainit_02.cpp:1
SCALAR Scalar
Definition: bench_gemm.cpp:45
MatrixXf MatrixType
Definition: benchmark-blocking-sizes.cpp:52
Generic lvalue expression of a coefficient-wise unary operator of a matrix or a vector.
Definition: CwiseUnaryView.h:135
ViewOp m_functor
Definition: CwiseUnaryView.h:162
internal::CwiseUnaryViewImpl< ViewOp, MatrixType, StrideType, typename internal::traits< MatrixType >::StorageKind >::Base Base
Definition: CwiseUnaryView.h:138
EIGEN_DEVICE_FUNC std::remove_reference_t< MatrixTypeNested > & nestedExpression()
Definition: CwiseUnaryView.h:158
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
Definition: CwiseUnaryView.h:149
EIGEN_DEVICE_FUNC const internal::remove_all_t< MatrixTypeNested > & nestedExpression() const
Definition: CwiseUnaryView.h:155
internal::ref_selector< MatrixType >::non_const_type MatrixTypeNested
Definition: CwiseUnaryView.h:140
EIGEN_DEVICE_FUNC const ViewOp & functor() const
Definition: CwiseUnaryView.h:152
MatrixTypeNested m_matrix
Definition: CwiseUnaryView.h:161
internal::remove_all_t< MatrixType > NestedExpression
Definition: CwiseUnaryView.h:141
dense_xpr_base< CwiseUnaryView< ViewOp, MatrixType, StrideType > >::type Base
Definition: CwiseUnaryView.h:63
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index innerStride() const
Definition: CwiseUnaryView.h:69
EIGEN_DEVICE_FUNC const Scalar & coeffRef(Index row, Index col) const
Definition: CwiseUnaryView.h:89
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index outerStride() const
Definition: CwiseUnaryView.h:75
CwiseUnaryView< ViewOp, MatrixType, StrideType > Derived
Definition: CwiseUnaryView.h:62
EIGEN_DEVICE_FUNC Scalar * data()
Definition: CwiseUnaryView.h:104
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar & coeffRef(Index row, Index col)
Definition: CwiseUnaryView.h:106
CwiseUnaryView< ViewOp, MatrixType, StrideType > Derived
Definition: CwiseUnaryView.h:99
CwiseUnaryViewImpl< ViewOp, MatrixType, StrideType, Dense, false > Base
Definition: CwiseUnaryView.h:98
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar & coeffRef(Index index)
Definition: CwiseUnaryView.h:110
Definition: CwiseUnaryView.h:53
generic_xpr_base< CwiseUnaryView< ViewOp, XprType, StrideType > >::type Base
Definition: CwiseUnaryView.h:55
const unsigned int DirectAccessBit
Definition: Constants.h:159
const unsigned int LvalueBit
Definition: Constants.h:148
const unsigned int RowMajorBit
Definition: Constants.h:70
return int(ret)+1
Eigen::DenseIndex ret
Definition: level1_cplx_impl.h:43
typename remove_all< T >::type remove_all_t
Definition: Meta.h:142
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
squared absolute value
Definition: GlobalFunctions.h:87
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
const int Dynamic
Definition: Constants.h:25
Extend namespace for flags.
Definition: fsi_chan_precond_driver.cc:56
type
Definition: compute_granudrum_aor.py:141
Definition: Eigen_Colamd.h:49
CwiseBinaryOp< internal::scalar_sum_op< double, double >, const CpyMatrixXd, const CpyMatrixXd > XprType
Definition: nestbyvalue.cpp:15
Definition: Constants.h:519
Definition: XprHelper.h:558
Definition: CoreEvaluators.h:104
Definition: XprHelper.h:575
Definition: DenseCoeffsBase.h:546
Definition: XprHelper.h:819
Definition: DenseCoeffsBase.h:556
Definition: Meta.h:388
remove_all_t< MatrixTypeNested > MatrixTypeNested_
Definition: CwiseUnaryView.h:25
MatrixType::Nested MatrixTypeNested
Definition: CwiseUnaryView.h:24
remove_all_t< ScalarRef > Scalar
Definition: CwiseUnaryView.h:22
result_of< ViewOp(typename traits< MatrixType >::Scalar &)>::type1 ScalarRef
Definition: CwiseUnaryView.h:21
Definition: ForwardDeclarations.h:21
Definition: benchGeometry.cpp:21