$darkmode
Eigen  5.0.1-dev
CwiseUnaryView.h
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.");
23  typedef remove_all_t<ScalarRef> Scalar;
24  typedef typename MatrixType::Nested MatrixTypeNested;
25  typedef remove_all_t<MatrixTypeNested> MatrixTypeNested_;
26  enum {
27  FlagsLvalueBit = is_lvalue<MatrixType>::value ? LvalueBit : 0,
28  Flags =
29  traits<MatrixTypeNested_>::Flags &
30  (RowMajorBit | FlagsLvalueBit | DirectAccessBit), // FIXME DirectAccessBit should not be handled by expressions
31  MatrixTypeInnerStride = inner_stride_at_compile_time<MatrixType>::ret,
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
42  ? (outer_stride_at_compile_time<MatrixType>::ret == Dynamic
43  ? int(Dynamic)
44  : outer_stride_at_compile_time<MatrixType>::ret *
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:
55  typedef typename generic_xpr_base<CwiseUnaryView<ViewOp, XprType, StrideType> >::type Base;
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:
62  typedef CwiseUnaryView<ViewOp, MatrixType, StrideType> Derived;
63  typedef typename dense_xpr_base<CwiseUnaryView<ViewOp, MatrixType, StrideType> >::type Base;
64  EIGEN_DENSE_PUBLIC_INTERFACE(Derived)
65  EIGEN_INHERIT_ASSIGNMENT_OPERATORS(CwiseUnaryViewImpl)
66 
67  EIGEN_DEVICE_FUNC inline const Scalar* data() const { return &(this->coeffRef(0)); }
68 
69  EIGEN_DEVICE_FUNC constexpr Index innerStride() const {
70  return StrideType::InnerStrideAtCompileTime != 0 ? int(StrideType::InnerStrideAtCompileTime)
71  : derived().nestedExpression().innerStride() *
72  sizeof(typename traits<MatrixType>::Scalar) / sizeof(Scalar);
73  }
74 
75  EIGEN_DEVICE_FUNC constexpr Index outerStride() const {
76  return StrideType::OuterStrideAtCompileTime != 0 ? int(StrideType::OuterStrideAtCompileTime)
77  : derived().nestedExpression().outerStride() *
78  sizeof(typename traits<MatrixType>::Scalar) / sizeof(Scalar);
79  }
80 
81  protected:
82  EIGEN_DEFAULT_EMPTY_CONSTRUCTOR_AND_DESTRUCTOR(CwiseUnaryViewImpl)
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 
89  EIGEN_DEVICE_FUNC inline const Scalar& coeffRef(Index row, Index col) const {
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:
98  typedef CwiseUnaryViewImpl<ViewOp, MatrixType, StrideType, Dense, false> Base;
99  typedef CwiseUnaryView<ViewOp, MatrixType, StrideType> Derived;
100  EIGEN_DENSE_PUBLIC_INTERFACE(Derived)
101  EIGEN_INHERIT_ASSIGNMENT_OPERATORS(CwiseUnaryViewImpl)
102 
103  using Base::data;
104  EIGEN_DEVICE_FUNC inline Scalar* data() { return &(this->coeffRef(0)); }
105 
106  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef(Index row, Index col) {
107  return internal::evaluator<Derived>(derived()).coeffRef(row, col);
108  }
109 
110  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef(Index index) {
111  return internal::evaluator<Derived>(derived()).coeffRef(index);
112  }
113 
114  protected:
115  EIGEN_DEFAULT_EMPTY_CONSTRUCTOR_AND_DESTRUCTOR(CwiseUnaryViewImpl)
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,
138  typename internal::traits<MatrixType>::StorageKind>::Base Base;
139  EIGEN_GENERIC_PUBLIC_INTERFACE(CwiseUnaryView)
140  typedef typename internal::ref_selector<MatrixType>::non_const_type MatrixTypeNested;
141  typedef internal::remove_all_t<MatrixType> NestedExpression;
142 
143  explicit EIGEN_DEVICE_FUNC inline CwiseUnaryView(MatrixType& mat, const ViewOp& func = ViewOp())
144  : m_matrix(mat), m_functor(func) {}
145 
146  EIGEN_INHERIT_ASSIGNMENT_OPERATORS(CwiseUnaryView)
147 
148  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr Index rows() const noexcept { return m_matrix.rows(); }
149  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr Index cols() const noexcept { return m_matrix.cols(); }
150 
152  EIGEN_DEVICE_FUNC const ViewOp& functor() const { return m_functor; }
153 
155  EIGEN_DEVICE_FUNC const internal::remove_all_t<MatrixTypeNested>& nestedExpression() const { return m_matrix; }
156 
158  EIGEN_DEVICE_FUNC std::remove_reference_t<MatrixTypeNested>& nestedExpression() { return m_matrix; }
159 
160  protected:
161  MatrixTypeNested m_matrix;
162  ViewOp m_functor;
163 };
164 
165 } // namespace Eigen
166 
167 #endif // EIGEN_CWISE_UNARY_VIEW_H
const unsigned int DirectAccessBit
Definition: Constants.h:159
const unsigned int LvalueBit
Definition: Constants.h:148
const ViewOp & functor() const
Definition: CwiseUnaryView.h:152
Namespace containing all symbols from the Eigen library.
Definition: B01_Experimental.dox:1
const internal::remove_all_t< MatrixTypeNested > & nestedExpression() const
Definition: CwiseUnaryView.h:155
const unsigned int RowMajorBit
Definition: Constants.h:70
Generic lvalue expression of a coefficient-wise unary operator of a matrix or a vector.
Definition: CwiseUnaryView.h:134
std::remove_reference_t< MatrixTypeNested > & nestedExpression()
Definition: CwiseUnaryView.h:158
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82
const int Dynamic
Definition: Constants.h:25