blob: ef1c208cb7512beff8eb3844e338e44bf050011a [file] [log] [blame]
Googler45874d82019-08-21 12:06:47 -07001// 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 Larsen66c640f2023-08-22 14:16:58 -070013// IWYU pragma: private
C. Antonio Sanchezdcfd7702021-10-26 11:41:24 -070014#include "./InternalHeaderCheck.h"
15
Googler45874d82019-08-21 12:06:47 -070016namespace Eigen {
17
18namespace internal {
Rasmus Munk Larsene783d932022-01-20 14:51:11 -080019template<typename ViewOp, typename MatrixType, typename StrideType>
20struct traits<CwiseUnaryView<ViewOp, MatrixType, StrideType> >
Googler45874d82019-08-21 12:06:47 -070021 : 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 Sanchez8d2cb1a2022-04-28 08:58:14 -070027 typedef remove_all_t<MatrixTypeNested> MatrixTypeNested_;
Googler45874d82019-08-21 12:06:47 -070028 enum {
29 FlagsLvalueBit = is_lvalue<MatrixType>::value ? LvalueBit : 0,
Rasmus Munk Larsene783d932022-01-20 14:51:11 -080030 Flags = traits<MatrixTypeNested_>::Flags & (RowMajorBit | FlagsLvalueBit | DirectAccessBit), // FIXME DirectAccessBit should not be handled by expressions
Googler45874d82019-08-21 12:06:47 -070031 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 Larsene783d932022-01-20 14:51:11 -080034 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)
Googler45874d82019-08-21 12:06:47 -070045 };
46};
47}
48
Rasmus Munk Larsene783d932022-01-20 14:51:11 -080049template<typename ViewOp, typename MatrixType, typename StrideType, typename StorageKind>
Googler45874d82019-08-21 12:06:47 -070050class 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 Larsene783d932022-01-20 14:51:11 -080065template<typename ViewOp, typename MatrixType, typename StrideType>
66class CwiseUnaryView : public CwiseUnaryViewImpl<ViewOp, MatrixType, StrideType, typename internal::traits<MatrixType>::StorageKind>
Googler45874d82019-08-21 12:06:47 -070067{
68 public:
69
Rasmus Munk Larsene783d932022-01-20 14:51:11 -080070 typedef typename CwiseUnaryViewImpl<ViewOp, MatrixType, StrideType, typename internal::traits<MatrixType>::StorageKind>::Base Base;
Googler45874d82019-08-21 12:06:47 -070071 EIGEN_GENERIC_PUBLIC_INTERFACE(CwiseUnaryView)
72 typedef typename internal::ref_selector<MatrixType>::non_const_type MatrixTypeNested;
C. Antonio Sanchez8d2cb1a2022-04-28 08:58:14 -070073 typedef internal::remove_all_t<MatrixType> NestedExpression;
Googler45874d82019-08-21 12:06:47 -070074
cantoniosff5a7ab2021-01-13 12:52:22 -080075 explicit EIGEN_DEVICE_FUNC inline CwiseUnaryView(MatrixType& mat, const ViewOp& func = ViewOp())
Googler45874d82019-08-21 12:06:47 -070076 : m_matrix(mat), m_functor(func) {}
77
78 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(CwiseUnaryView)
79
cantonios26133d62021-04-13 14:14:06 -070080 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(); }
Googler45874d82019-08-21 12:06:47 -070084
85 /** \returns the functor representing unary operation */
cantoniosff5a7ab2021-01-13 12:52:22 -080086 EIGEN_DEVICE_FUNC const ViewOp& functor() const { return m_functor; }
Googler45874d82019-08-21 12:06:47 -070087
88 /** \returns the nested expression */
C. Antonio Sanchez8d2cb1a2022-04-28 08:58:14 -070089 EIGEN_DEVICE_FUNC const internal::remove_all_t<MatrixTypeNested>&
Googler45874d82019-08-21 12:06:47 -070090 nestedExpression() const { return m_matrix; }
91
92 /** \returns the nested expression */
C. Antonio Sanchez8d2cb1a2022-04-28 08:58:14 -070093 EIGEN_DEVICE_FUNC std::remove_reference_t<MatrixTypeNested>&
Googler45874d82019-08-21 12:06:47 -070094 nestedExpression() { return m_matrix; }
95
96 protected:
97 MatrixTypeNested m_matrix;
98 ViewOp m_functor;
99};
100
101// Generic API dispatcher
Rasmus Munk Larsene783d932022-01-20 14:51:11 -0800102template<typename ViewOp, typename XprType, typename StrideType, typename StorageKind>
Googler45874d82019-08-21 12:06:47 -0700103class CwiseUnaryViewImpl
Rasmus Munk Larsene783d932022-01-20 14:51:11 -0800104 : public internal::generic_xpr_base<CwiseUnaryView<ViewOp, XprType, StrideType> >::type
Googler45874d82019-08-21 12:06:47 -0700105{
106public:
Rasmus Munk Larsene783d932022-01-20 14:51:11 -0800107 typedef typename internal::generic_xpr_base<CwiseUnaryView<ViewOp, XprType, StrideType> >::type Base;
Googler45874d82019-08-21 12:06:47 -0700108};
109
Rasmus Munk Larsene783d932022-01-20 14:51:11 -0800110template<typename ViewOp, typename MatrixType, typename StrideType>
111class CwiseUnaryViewImpl<ViewOp,MatrixType,StrideType,Dense>
112 : public internal::dense_xpr_base< CwiseUnaryView<ViewOp, MatrixType, StrideType> >::type
Googler45874d82019-08-21 12:06:47 -0700113{
114 public:
115
Rasmus Munk Larsene783d932022-01-20 14:51:11 -0800116 typedef CwiseUnaryView<ViewOp, MatrixType,StrideType> Derived;
117 typedef typename internal::dense_xpr_base< CwiseUnaryView<ViewOp, MatrixType,StrideType> >::type Base;
Googler45874d82019-08-21 12:06:47 -0700118
119 EIGEN_DENSE_PUBLIC_INTERFACE(Derived)
120 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(CwiseUnaryViewImpl)
cantonios26133d62021-04-13 14:14:06 -0700121
Googler45874d82019-08-21 12:06:47 -0700122 EIGEN_DEVICE_FUNC inline Scalar* data() { return &(this->coeffRef(0)); }
123 EIGEN_DEVICE_FUNC inline const Scalar* data() const { return &(this->coeff(0)); }
124
cantonios26133d62021-04-13 14:14:06 -0700125 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR inline Index innerStride() const
Googler45874d82019-08-21 12:06:47 -0700126 {
Rasmus Munk Larsene783d932022-01-20 14:51:11 -0800127 return StrideType::InnerStrideAtCompileTime != 0
128 ? int(StrideType::InnerStrideAtCompileTime)
129 : derived().nestedExpression().innerStride() * sizeof(typename internal::traits<MatrixType>::Scalar) / sizeof(Scalar);
Googler45874d82019-08-21 12:06:47 -0700130 }
131
cantonios26133d62021-04-13 14:14:06 -0700132 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR inline Index outerStride() const
Googler45874d82019-08-21 12:06:47 -0700133 {
Rasmus Munk Larsene783d932022-01-20 14:51:11 -0800134 return StrideType::OuterStrideAtCompileTime != 0
135 ? int(StrideType::OuterStrideAtCompileTime)
136 : derived().nestedExpression().outerStride() * sizeof(typename internal::traits<MatrixType>::Scalar) / sizeof(Scalar);
Googler45874d82019-08-21 12:06:47 -0700137 }
Rasmus Munk Larsen1e3554a2020-01-08 16:22:35 -0800138 protected:
139 EIGEN_DEFAULT_EMPTY_CONSTRUCTOR_AND_DESTRUCTOR(CwiseUnaryViewImpl)
Googler45874d82019-08-21 12:06:47 -0700140};
141
142} // end namespace Eigen
143
144#endif // EIGEN_CWISE_UNARY_VIEW_H