Eigen  5.0.1-dev+master
 
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
72 ? int(StrideType::InnerStrideAtCompileTime)
73 : derived().nestedExpression().innerStride() * Index(sizeof(typename traits<MatrixType>::Scalar)) /
74 Index(sizeof(Scalar));
75 }
76
77 EIGEN_DEVICE_FUNC constexpr Index outerStride() const {
78 return StrideType::OuterStrideAtCompileTime != 0
79 ? int(StrideType::OuterStrideAtCompileTime)
80 : derived().nestedExpression().outerStride() * Index(sizeof(typename traits<MatrixType>::Scalar)) /
81 Index(sizeof(Scalar));
82 }
83
84 protected:
85 EIGEN_DEFAULT_EMPTY_CONSTRUCTOR_AND_DESTRUCTOR(CwiseUnaryViewImpl)
86
87 // Allow const access to coeffRef for the case of direct access being enabled.
88 EIGEN_DEVICE_FUNC inline const Scalar& coeffRef(Index index) const {
89 return internal::evaluator<Derived>(derived()).coeffRef(index);
90 }
91
92 EIGEN_DEVICE_FUNC inline const Scalar& coeffRef(Index row, Index col) const {
93 return internal::evaluator<Derived>(derived()).coeffRef(row, col);
94 }
95};
96
97template <typename ViewOp, typename MatrixType, typename StrideType>
98class CwiseUnaryViewImpl<ViewOp, MatrixType, StrideType, Dense, true>
99 : public CwiseUnaryViewImpl<ViewOp, MatrixType, StrideType, Dense, false> {
100 public:
101 using Base = CwiseUnaryViewImpl<ViewOp, MatrixType, StrideType, Dense, false>;
102 using Derived = CwiseUnaryView<ViewOp, MatrixType, StrideType>;
103 EIGEN_DENSE_PUBLIC_INTERFACE(Derived)
104 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(CwiseUnaryViewImpl)
105
106 using Base::coeffRef;
107 using Base::data;
108 EIGEN_DEVICE_FUNC inline Scalar* data() { return &(this->coeffRef(0)); }
109
110 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef(Index row, Index col) {
111 return internal::evaluator<Derived>(derived()).coeffRef(row, col);
112 }
113
114 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef(Index index) {
115 return internal::evaluator<Derived>(derived()).coeffRef(index);
116 }
117
118 protected:
119 EIGEN_DEFAULT_EMPTY_CONSTRUCTOR_AND_DESTRUCTOR(CwiseUnaryViewImpl)
120};
121
122} // namespace internal
123
137template <typename ViewOp, typename MatrixType, typename StrideType>
138class CwiseUnaryView : public internal::CwiseUnaryViewImpl<ViewOp, MatrixType, StrideType,
139 typename internal::traits<MatrixType>::StorageKind> {
140 public:
141 using Base = typename internal::CwiseUnaryViewImpl<ViewOp, MatrixType, StrideType,
142 typename internal::traits<MatrixType>::StorageKind>::Base;
143 EIGEN_GENERIC_PUBLIC_INTERFACE(CwiseUnaryView)
144 using MatrixTypeNested = typename internal::ref_selector<MatrixType>::non_const_type;
145 using NestedExpression = internal::remove_all_t<MatrixType>;
146
147 explicit EIGEN_DEVICE_FUNC constexpr inline CwiseUnaryView(MatrixType& mat, const ViewOp& func = ViewOp())
148 : m_matrix(mat), m_functor(func) {}
149
150 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(CwiseUnaryView)
151
152 EIGEN_DEVICE_FUNC constexpr Index rows() const noexcept { return m_matrix.rows(); }
153 EIGEN_DEVICE_FUNC constexpr Index cols() const noexcept { return m_matrix.cols(); }
154
156 EIGEN_DEVICE_FUNC constexpr const ViewOp& functor() const { return m_functor; }
157
159 EIGEN_DEVICE_FUNC constexpr const internal::remove_all_t<MatrixTypeNested>& nestedExpression() const {
160 return m_matrix;
161 }
162
164 EIGEN_DEVICE_FUNC constexpr std::remove_reference_t<MatrixTypeNested>& nestedExpression() { return m_matrix; }
165
166 protected:
167 MatrixTypeNested m_matrix;
168 ViewOp m_functor;
169};
170
171} // namespace Eigen
172
173#endif // EIGEN_CWISE_UNARY_VIEW_H
constexpr std::remove_reference_t< MatrixTypeNested > & nestedExpression()
Definition CwiseUnaryView.h:164
constexpr const internal::remove_all_t< MatrixTypeNested > & nestedExpression() const
Definition CwiseUnaryView.h:159
constexpr const ViewOp & functor() const
Definition CwiseUnaryView.h:156
constexpr unsigned int DirectAccessBit
Definition Constants.h:160
constexpr unsigned int LvalueBit
Definition Constants.h:149
constexpr unsigned int RowMajorBit
Definition Constants.h:71