Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 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 | |
Rasmus Munk Larsen | 66c640f | 2023-08-22 14:16:58 -0700 | [diff] [blame] | 13 | // IWYU pragma: private |
C. Antonio Sanchez | dcfd770 | 2021-10-26 11:41:24 -0700 | [diff] [blame] | 14 | #include "./InternalHeaderCheck.h" |
| 15 | |
Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 16 | namespace Eigen { |
| 17 | |
| 18 | namespace internal { |
Rasmus Munk Larsen | e783d93 | 2022-01-20 14:51:11 -0800 | [diff] [blame] | 19 | template<typename ViewOp, typename MatrixType, typename StrideType> |
| 20 | struct traits<CwiseUnaryView<ViewOp, MatrixType, StrideType> > |
Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 21 | : traits<MatrixType> |
| 22 | { |
| 23 | typedef typename result_of< |
| 24 | ViewOp(const typename traits<MatrixType>::Scalar&) |
| 25 | >::type Scalar; |
| 26 | typedef typename MatrixType::Nested MatrixTypeNested; |
C. Antonio Sanchez | 8d2cb1a | 2022-04-28 08:58:14 -0700 | [diff] [blame] | 27 | typedef remove_all_t<MatrixTypeNested> MatrixTypeNested_; |
Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 28 | enum { |
| 29 | FlagsLvalueBit = is_lvalue<MatrixType>::value ? LvalueBit : 0, |
Rasmus Munk Larsen | e783d93 | 2022-01-20 14:51:11 -0800 | [diff] [blame] | 30 | Flags = traits<MatrixTypeNested_>::Flags & (RowMajorBit | FlagsLvalueBit | DirectAccessBit), // FIXME DirectAccessBit should not be handled by expressions |
Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 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 |
Rasmus Munk Larsen | e783d93 | 2022-01-20 14:51:11 -0800 | [diff] [blame] | 34 | InnerStrideAtCompileTime = StrideType::InnerStrideAtCompileTime == 0 |
| 35 | ? (MatrixTypeInnerStride == Dynamic |
| 36 | ? int(Dynamic) |
| 37 | : int(MatrixTypeInnerStride) * int(sizeof(typename traits<MatrixType>::Scalar) / sizeof(Scalar))) |
| 38 | : int(StrideType::InnerStrideAtCompileTime), |
| 39 | |
| 40 | OuterStrideAtCompileTime = StrideType::OuterStrideAtCompileTime == 0 |
| 41 | ? (outer_stride_at_compile_time<MatrixType>::ret == Dynamic |
| 42 | ? int(Dynamic) |
| 43 | : outer_stride_at_compile_time<MatrixType>::ret * int(sizeof(typename traits<MatrixType>::Scalar) / sizeof(Scalar))) |
| 44 | : int(StrideType::OuterStrideAtCompileTime) |
Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 45 | }; |
| 46 | }; |
| 47 | } |
| 48 | |
Rasmus Munk Larsen | e783d93 | 2022-01-20 14:51:11 -0800 | [diff] [blame] | 49 | template<typename ViewOp, typename MatrixType, typename StrideType, typename StorageKind> |
Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 50 | class CwiseUnaryViewImpl; |
| 51 | |
| 52 | /** \class CwiseUnaryView |
| 53 | * \ingroup Core_Module |
| 54 | * |
| 55 | * \brief Generic lvalue expression of a coefficient-wise unary operator of a matrix or a vector |
| 56 | * |
| 57 | * \tparam ViewOp template functor implementing the view |
| 58 | * \tparam MatrixType the type of the matrix we are applying the unary operator |
| 59 | * |
| 60 | * This class represents a lvalue expression of a generic unary view operator of a matrix or a vector. |
| 61 | * It is the return type of real() and imag(), and most of the time this is the only way it is used. |
| 62 | * |
| 63 | * \sa MatrixBase::unaryViewExpr(const CustomUnaryOp &) const, class CwiseUnaryOp |
| 64 | */ |
Rasmus Munk Larsen | e783d93 | 2022-01-20 14:51:11 -0800 | [diff] [blame] | 65 | template<typename ViewOp, typename MatrixType, typename StrideType> |
| 66 | class CwiseUnaryView : public CwiseUnaryViewImpl<ViewOp, MatrixType, StrideType, typename internal::traits<MatrixType>::StorageKind> |
Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 67 | { |
| 68 | public: |
| 69 | |
Rasmus Munk Larsen | e783d93 | 2022-01-20 14:51:11 -0800 | [diff] [blame] | 70 | typedef typename CwiseUnaryViewImpl<ViewOp, MatrixType, StrideType, typename internal::traits<MatrixType>::StorageKind>::Base Base; |
Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 71 | EIGEN_GENERIC_PUBLIC_INTERFACE(CwiseUnaryView) |
| 72 | typedef typename internal::ref_selector<MatrixType>::non_const_type MatrixTypeNested; |
C. Antonio Sanchez | 8d2cb1a | 2022-04-28 08:58:14 -0700 | [diff] [blame] | 73 | typedef internal::remove_all_t<MatrixType> NestedExpression; |
Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 74 | |
cantonios | ff5a7ab | 2021-01-13 12:52:22 -0800 | [diff] [blame] | 75 | explicit EIGEN_DEVICE_FUNC inline CwiseUnaryView(MatrixType& mat, const ViewOp& func = ViewOp()) |
Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 76 | : m_matrix(mat), m_functor(func) {} |
| 77 | |
| 78 | EIGEN_INHERIT_ASSIGNMENT_OPERATORS(CwiseUnaryView) |
| 79 | |
cantonios | 26133d6 | 2021-04-13 14:14:06 -0700 | [diff] [blame] | 80 | EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR |
| 81 | Index rows() const EIGEN_NOEXCEPT { return m_matrix.rows(); } |
| 82 | EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR |
| 83 | Index cols() const EIGEN_NOEXCEPT { return m_matrix.cols(); } |
Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 84 | |
| 85 | /** \returns the functor representing unary operation */ |
cantonios | ff5a7ab | 2021-01-13 12:52:22 -0800 | [diff] [blame] | 86 | EIGEN_DEVICE_FUNC const ViewOp& functor() const { return m_functor; } |
Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 87 | |
| 88 | /** \returns the nested expression */ |
C. Antonio Sanchez | 8d2cb1a | 2022-04-28 08:58:14 -0700 | [diff] [blame] | 89 | EIGEN_DEVICE_FUNC const internal::remove_all_t<MatrixTypeNested>& |
Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 90 | nestedExpression() const { return m_matrix; } |
| 91 | |
| 92 | /** \returns the nested expression */ |
C. Antonio Sanchez | 8d2cb1a | 2022-04-28 08:58:14 -0700 | [diff] [blame] | 93 | EIGEN_DEVICE_FUNC std::remove_reference_t<MatrixTypeNested>& |
Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 94 | nestedExpression() { return m_matrix; } |
| 95 | |
| 96 | protected: |
| 97 | MatrixTypeNested m_matrix; |
| 98 | ViewOp m_functor; |
| 99 | }; |
| 100 | |
| 101 | // Generic API dispatcher |
Rasmus Munk Larsen | e783d93 | 2022-01-20 14:51:11 -0800 | [diff] [blame] | 102 | template<typename ViewOp, typename XprType, typename StrideType, typename StorageKind> |
Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 103 | class CwiseUnaryViewImpl |
Rasmus Munk Larsen | e783d93 | 2022-01-20 14:51:11 -0800 | [diff] [blame] | 104 | : public internal::generic_xpr_base<CwiseUnaryView<ViewOp, XprType, StrideType> >::type |
Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 105 | { |
| 106 | public: |
Rasmus Munk Larsen | e783d93 | 2022-01-20 14:51:11 -0800 | [diff] [blame] | 107 | typedef typename internal::generic_xpr_base<CwiseUnaryView<ViewOp, XprType, StrideType> >::type Base; |
Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 108 | }; |
| 109 | |
Rasmus Munk Larsen | e783d93 | 2022-01-20 14:51:11 -0800 | [diff] [blame] | 110 | template<typename ViewOp, typename MatrixType, typename StrideType> |
| 111 | class CwiseUnaryViewImpl<ViewOp,MatrixType,StrideType,Dense> |
| 112 | : public internal::dense_xpr_base< CwiseUnaryView<ViewOp, MatrixType, StrideType> >::type |
Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 113 | { |
| 114 | public: |
| 115 | |
Rasmus Munk Larsen | e783d93 | 2022-01-20 14:51:11 -0800 | [diff] [blame] | 116 | typedef CwiseUnaryView<ViewOp, MatrixType,StrideType> Derived; |
| 117 | typedef typename internal::dense_xpr_base< CwiseUnaryView<ViewOp, MatrixType,StrideType> >::type Base; |
Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 118 | |
| 119 | EIGEN_DENSE_PUBLIC_INTERFACE(Derived) |
| 120 | EIGEN_INHERIT_ASSIGNMENT_OPERATORS(CwiseUnaryViewImpl) |
cantonios | 26133d6 | 2021-04-13 14:14:06 -0700 | [diff] [blame] | 121 | |
Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 122 | EIGEN_DEVICE_FUNC inline Scalar* data() { return &(this->coeffRef(0)); } |
| 123 | EIGEN_DEVICE_FUNC inline const Scalar* data() const { return &(this->coeff(0)); } |
| 124 | |
cantonios | 26133d6 | 2021-04-13 14:14:06 -0700 | [diff] [blame] | 125 | EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR inline Index innerStride() const |
Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 126 | { |
Rasmus Munk Larsen | e783d93 | 2022-01-20 14:51:11 -0800 | [diff] [blame] | 127 | return StrideType::InnerStrideAtCompileTime != 0 |
| 128 | ? int(StrideType::InnerStrideAtCompileTime) |
| 129 | : derived().nestedExpression().innerStride() * sizeof(typename internal::traits<MatrixType>::Scalar) / sizeof(Scalar); |
Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 130 | } |
| 131 | |
cantonios | 26133d6 | 2021-04-13 14:14:06 -0700 | [diff] [blame] | 132 | EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR inline Index outerStride() const |
Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 133 | { |
Rasmus Munk Larsen | e783d93 | 2022-01-20 14:51:11 -0800 | [diff] [blame] | 134 | return StrideType::OuterStrideAtCompileTime != 0 |
| 135 | ? int(StrideType::OuterStrideAtCompileTime) |
| 136 | : derived().nestedExpression().outerStride() * sizeof(typename internal::traits<MatrixType>::Scalar) / sizeof(Scalar); |
Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 137 | } |
Rasmus Munk Larsen | 1e3554a | 2020-01-08 16:22:35 -0800 | [diff] [blame] | 138 | protected: |
| 139 | EIGEN_DEFAULT_EMPTY_CONSTRUCTOR_AND_DESTRUCTOR(CwiseUnaryViewImpl) |
Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | } // end namespace Eigen |
| 143 | |
| 144 | #endif // EIGEN_CWISE_UNARY_VIEW_H |