Eigen  5.0.1
 
Loading...
Searching...
No Matches
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// SPDX-License-Identifier: MPL-2.0
10
11#ifndef EIGEN_CWISE_UNARY_VIEW_H
12#define EIGEN_CWISE_UNARY_VIEW_H
13
14// IWYU pragma: private
15#include "./InternalHeaderCheck.h"
16
17namespace Eigen {
18
19namespace internal {
20template <typename ViewOp, typename MatrixType, typename StrideType>
21struct traits<CwiseUnaryView<ViewOp, MatrixType, StrideType> > : traits<MatrixType> {
22 using ScalarRef = typename result_of<ViewOp(typename traits<MatrixType>::Scalar&)>::type1;
23 static_assert(std::is_reference<ScalarRef>::value, "Views must return a reference type.");
24 using Scalar = remove_cvref_t<ScalarRef>;
25 using MatrixTypeNested = typename MatrixType::Nested;
26 using MatrixTypeNested_ = remove_all_t<MatrixTypeNested>;
27 enum {
28 FlagsLvalueBit = is_lvalue<MatrixType>::value ? LvalueBit : 0,
29 Flags =
30 traits<MatrixTypeNested_>::Flags &
31 (RowMajorBit | FlagsLvalueBit | DirectAccessBit), // FIXME DirectAccessBit should not be handled by expressions
32 MatrixTypeInnerStride = inner_stride_at_compile_time<MatrixType>::value,
33 // need to cast the sizeof's from size_t to int explicitly, otherwise:
34 // "error: no integral type can represent all of the enumerator values
35 InnerStrideAtCompileTime =
36 StrideType::InnerStrideAtCompileTime == 0
37 ? (MatrixTypeInnerStride == Dynamic
38 ? int(Dynamic)
39 : int(MatrixTypeInnerStride) * int(sizeof(typename traits<MatrixType>::Scalar) / sizeof(Scalar)))
40 : int(StrideType::InnerStrideAtCompileTime),
41
42 OuterStrideAtCompileTime = StrideType::OuterStrideAtCompileTime == 0
43 ? (outer_stride_at_compile_time<MatrixType>::value == Dynamic
44 ? int(Dynamic)
45 : outer_stride_at_compile_time<MatrixType>::value *
46 int(sizeof(typename traits<MatrixType>::Scalar) / sizeof(Scalar)))
47 : int(StrideType::OuterStrideAtCompileTime)
48 };
49};
50
51// Generic API dispatcher
52template <typename ViewOp, typename XprType, typename StrideType, typename StorageKind,
53 bool Mutable = !std::is_const<XprType>::value>
54class CwiseUnaryViewImpl : public generic_xpr_base<CwiseUnaryView<ViewOp, XprType, StrideType> >::type {
55 public:
56 using Base = typename generic_xpr_base<CwiseUnaryView<ViewOp, XprType, StrideType>>::type;
57};
58
59template <typename ViewOp, typename MatrixType, typename StrideType>
60class CwiseUnaryViewImpl<ViewOp, MatrixType, StrideType, Dense, false>
61 : public dense_xpr_base<CwiseUnaryView<ViewOp, MatrixType, StrideType> >::type {
62 public:
63 using Derived = CwiseUnaryView<ViewOp, MatrixType, StrideType>;
64 using Base = typename dense_xpr_base<CwiseUnaryView<ViewOp, MatrixType, StrideType>>::type;
65 EIGEN_DENSE_PUBLIC_INTERFACE(Derived)
66 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(CwiseUnaryViewImpl)
67
68 EIGEN_DEVICE_FUNC inline const Scalar* data() const { return &(this->coeffRef(0)); }
69
70 EIGEN_DEVICE_FUNC constexpr Index innerStride() const {
71 return StrideType::InnerStrideAtCompileTime != 0 ? int(StrideType::InnerStrideAtCompileTime)
72 : derived().nestedExpression().innerStride() *
73 sizeof(typename traits<MatrixType>::Scalar) / sizeof(Scalar);
74 }
75
76 EIGEN_DEVICE_FUNC constexpr Index outerStride() const {
77 return StrideType::OuterStrideAtCompileTime != 0 ? int(StrideType::OuterStrideAtCompileTime)
78 : derived().nestedExpression().outerStride() *
79 sizeof(typename traits<MatrixType>::Scalar) / sizeof(Scalar);
80 }
81
82 protected:
83 EIGEN_DEFAULT_EMPTY_CONSTRUCTOR_AND_DESTRUCTOR(CwiseUnaryViewImpl)
84
85 // Allow const access to coeffRef for the case of direct access being enabled.
86 EIGEN_DEVICE_FUNC inline const Scalar& coeffRef(Index index) const {
87 return internal::evaluator<Derived>(derived()).coeffRef(index);
88 }
89
90 EIGEN_DEVICE_FUNC inline const Scalar& coeffRef(Index row, Index col) const {
91 return internal::evaluator<Derived>(derived()).coeffRef(row, col);
92 }
93};
94
95template <typename ViewOp, typename MatrixType, typename StrideType>
96class CwiseUnaryViewImpl<ViewOp, MatrixType, StrideType, Dense, true>
97 : public CwiseUnaryViewImpl<ViewOp, MatrixType, StrideType, Dense, false> {
98 public:
99 using Base = CwiseUnaryViewImpl<ViewOp, MatrixType, StrideType, Dense, false>;
100 using Derived = CwiseUnaryView<ViewOp, MatrixType, StrideType>;
101 EIGEN_DENSE_PUBLIC_INTERFACE(Derived)
102 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(CwiseUnaryViewImpl)
103
104 using Base::coeffRef;
105 using Base::data;
106 EIGEN_DEVICE_FUNC inline Scalar* data() { return &(this->coeffRef(0)); }
107
108 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef(Index row, Index col) {
109 return internal::evaluator<Derived>(derived()).coeffRef(row, col);
110 }
111
112 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef(Index index) {
113 return internal::evaluator<Derived>(derived()).coeffRef(index);
114 }
115
116 protected:
117 EIGEN_DEFAULT_EMPTY_CONSTRUCTOR_AND_DESTRUCTOR(CwiseUnaryViewImpl)
118};
119
120} // namespace internal
121
135template <typename ViewOp, typename MatrixType, typename StrideType>
136class CwiseUnaryView : public internal::CwiseUnaryViewImpl<ViewOp, MatrixType, StrideType,
137 typename internal::traits<MatrixType>::StorageKind> {
138 public:
139 using Base = typename internal::CwiseUnaryViewImpl<ViewOp, MatrixType, StrideType,
140 typename internal::traits<MatrixType>::StorageKind>::Base;
141 EIGEN_GENERIC_PUBLIC_INTERFACE(CwiseUnaryView)
142 using MatrixTypeNested = typename internal::ref_selector<MatrixType>::non_const_type;
143 using NestedExpression = internal::remove_all_t<MatrixType>;
144
145 explicit EIGEN_DEVICE_FUNC constexpr inline CwiseUnaryView(MatrixType& mat, const ViewOp& func = ViewOp())
146 : m_matrix(mat), m_functor(func) {}
147
148 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(CwiseUnaryView)
149
150 EIGEN_DEVICE_FUNC constexpr Index rows() const noexcept { return m_matrix.rows(); }
151 EIGEN_DEVICE_FUNC constexpr Index cols() const noexcept { return m_matrix.cols(); }
152
154 EIGEN_DEVICE_FUNC constexpr const ViewOp& functor() const { return m_functor; }
155
157 EIGEN_DEVICE_FUNC constexpr const internal::remove_all_t<MatrixTypeNested>& nestedExpression() const {
158 return m_matrix;
159 }
160
162 EIGEN_DEVICE_FUNC constexpr std::remove_reference_t<MatrixTypeNested>& nestedExpression() { return m_matrix; }
163
164 protected:
165 MatrixTypeNested m_matrix;
166 ViewOp m_functor;
167};
168
169} // namespace Eigen
170
171#endif // EIGEN_CWISE_UNARY_VIEW_H
constexpr std::remove_reference_t< MatrixTypeNested > & nestedExpression()
Definition CwiseUnaryView.h:162
constexpr const internal::remove_all_t< MatrixTypeNested > & nestedExpression() const
Definition CwiseUnaryView.h:157
constexpr const ViewOp & functor() const
Definition CwiseUnaryView.h:154
constexpr unsigned int DirectAccessBit
Definition Constants.h:160
constexpr unsigned int LvalueBit
Definition Constants.h:149
constexpr unsigned int RowMajorBit
Definition Constants.h:71