TensorExpr.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2014 Benoit Steiner <benoit.steiner.goog@gmail.com>
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_CXX11_TENSOR_TENSOR_EXPR_H
11 #define EIGEN_CXX11_TENSOR_TENSOR_EXPR_H
12 
13 // IWYU pragma: private
14 #include "./InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 
33 namespace internal {
34 template <typename NullaryOp, typename XprType>
35 struct traits<TensorCwiseNullaryOp<NullaryOp, XprType> > : traits<XprType> {
37  typedef typename XprType::Scalar Scalar;
38  typedef typename XprType::Nested XprTypeNested;
39  typedef std::remove_reference_t<XprTypeNested> XprTypeNested_;
40  static constexpr int NumDimensions = XprTraits::NumDimensions;
41  static constexpr int Layout = XprTraits::Layout;
42  typedef typename XprTraits::PointerType PointerType;
43  enum { Flags = 0 };
44 };
45 
46 } // end namespace internal
47 
48 template <typename NullaryOp, typename XprType>
49 class TensorCwiseNullaryOp : public TensorBase<TensorCwiseNullaryOp<NullaryOp, XprType>, ReadOnlyAccessors> {
50  public:
53  typedef typename XprType::CoeffReturnType CoeffReturnType;
57 
58  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorCwiseNullaryOp(const XprType& xpr, const NullaryOp& func = NullaryOp())
59  : m_xpr(xpr), m_functor(func) {}
60 
62 
63  EIGEN_DEVICE_FUNC const NullaryOp& functor() const { return m_functor; }
64 
65  protected:
66  typename XprType::Nested m_xpr;
67  const NullaryOp m_functor;
68 };
69 
70 namespace internal {
71 template <typename UnaryOp, typename XprType>
72 struct traits<TensorCwiseUnaryOp<UnaryOp, XprType> > : traits<XprType> {
73  // TODO(phli): Add InputScalar, InputPacket. Check references to
74  // current Scalar/Packet to see if the intent is Input or Output.
75  typedef typename result_of<UnaryOp(typename XprType::Scalar)>::type Scalar;
77  typedef typename XprType::Nested XprTypeNested;
78  typedef std::remove_reference_t<XprTypeNested> XprTypeNested_;
79  static constexpr int NumDimensions = XprTraits::NumDimensions;
80  static constexpr int Layout = XprTraits::Layout;
82 };
83 
84 template <typename UnaryOp, typename XprType>
85 struct eval<TensorCwiseUnaryOp<UnaryOp, XprType>, Eigen::Dense> {
87 };
88 
89 template <typename UnaryOp, typename XprType>
90 struct nested<TensorCwiseUnaryOp<UnaryOp, XprType>, 1, typename eval<TensorCwiseUnaryOp<UnaryOp, XprType> >::type> {
92 };
93 
94 } // end namespace internal
95 
96 template <typename UnaryOp, typename XprType>
97 class TensorCwiseUnaryOp : public TensorBase<TensorCwiseUnaryOp<UnaryOp, XprType>, ReadOnlyAccessors> {
98  public:
99  // TODO(phli): Add InputScalar, InputPacket. Check references to
100  // current Scalar/Packet to see if the intent is Input or Output.
107 
108  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorCwiseUnaryOp(const XprType& xpr, const UnaryOp& func = UnaryOp())
109  : m_xpr(xpr), m_functor(func) {}
110 
111  EIGEN_DEVICE_FUNC const UnaryOp& functor() const { return m_functor; }
112 
115 
116  protected:
117  typename XprType::Nested m_xpr;
118  const UnaryOp m_functor;
119 };
120 
121 namespace internal {
122 template <typename BinaryOp, typename LhsXprType, typename RhsXprType>
123 struct traits<TensorCwiseBinaryOp<BinaryOp, LhsXprType, RhsXprType> > {
124  // Type promotion to handle the case where the types of the lhs and the rhs
125  // are different.
126  // TODO(phli): Add Lhs/RhsScalar, Lhs/RhsPacket. Check references to
127  // current Scalar/Packet to see if the intent is Inputs or Output.
128  typedef typename result_of<BinaryOp(typename LhsXprType::Scalar, typename RhsXprType::Scalar)>::type Scalar;
132  typedef
134  typedef typename LhsXprType::Nested LhsNested;
135  typedef typename RhsXprType::Nested RhsNested;
136  typedef std::remove_reference_t<LhsNested> LhsNested_;
137  typedef std::remove_reference_t<RhsNested> RhsNested_;
138  static constexpr int NumDimensions = XprTraits::NumDimensions;
139  static constexpr int Layout = XprTraits::Layout;
140  typedef typename TypeConversion<Scalar,
144  enum { Flags = 0 };
145 };
146 
147 template <typename BinaryOp, typename LhsXprType, typename RhsXprType>
148 struct eval<TensorCwiseBinaryOp<BinaryOp, LhsXprType, RhsXprType>, Eigen::Dense> {
150 };
151 
152 template <typename BinaryOp, typename LhsXprType, typename RhsXprType>
153 struct nested<TensorCwiseBinaryOp<BinaryOp, LhsXprType, RhsXprType>, 1,
154  typename eval<TensorCwiseBinaryOp<BinaryOp, LhsXprType, RhsXprType> >::type> {
156 };
157 
158 } // end namespace internal
159 
160 template <typename BinaryOp, typename LhsXprType, typename RhsXprType>
162  : public TensorBase<TensorCwiseBinaryOp<BinaryOp, LhsXprType, RhsXprType>, ReadOnlyAccessors> {
163  public:
164  // TODO(phli): Add Lhs/RhsScalar, Lhs/RhsPacket. Check references to
165  // current Scalar/Packet to see if the intent is Inputs or Output.
172 
173  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorCwiseBinaryOp(const LhsXprType& lhs, const RhsXprType& rhs,
174  const BinaryOp& func = BinaryOp())
175  : m_lhs_xpr(lhs), m_rhs_xpr(rhs), m_functor(func) {}
176 
177  EIGEN_DEVICE_FUNC const BinaryOp& functor() const { return m_functor; }
178 
181  return m_lhs_xpr;
182  }
183 
185  return m_rhs_xpr;
186  }
187 
188  protected:
189  typename LhsXprType::Nested m_lhs_xpr;
190  typename RhsXprType::Nested m_rhs_xpr;
191  const BinaryOp m_functor;
192 };
193 
194 namespace internal {
195 template <typename TernaryOp, typename Arg1XprType, typename Arg2XprType, typename Arg3XprType>
196 struct traits<TensorCwiseTernaryOp<TernaryOp, Arg1XprType, Arg2XprType, Arg3XprType> > {
197  // Type promotion to handle the case where the types of the args are different.
198  typedef typename result_of<TernaryOp(typename Arg1XprType::Scalar, typename Arg2XprType::Scalar,
203  typedef typename Arg1XprType::Nested Arg1Nested;
204  typedef typename Arg2XprType::Nested Arg2Nested;
205  typedef typename Arg3XprType::Nested Arg3Nested;
206  typedef std::remove_reference_t<Arg1Nested> Arg1Nested_;
207  typedef std::remove_reference_t<Arg2Nested> Arg2Nested_;
208  typedef std::remove_reference_t<Arg3Nested> Arg3Nested_;
209  static constexpr int NumDimensions = XprTraits::NumDimensions;
210  static constexpr int Layout = XprTraits::Layout;
211  typedef typename TypeConversion<Scalar,
215  enum { Flags = 0 };
216 };
217 
218 template <typename TernaryOp, typename Arg1XprType, typename Arg2XprType, typename Arg3XprType>
219 struct eval<TensorCwiseTernaryOp<TernaryOp, Arg1XprType, Arg2XprType, Arg3XprType>, Eigen::Dense> {
221 };
222 
223 template <typename TernaryOp, typename Arg1XprType, typename Arg2XprType, typename Arg3XprType>
224 struct nested<TensorCwiseTernaryOp<TernaryOp, Arg1XprType, Arg2XprType, Arg3XprType>, 1,
225  typename eval<TensorCwiseTernaryOp<TernaryOp, Arg1XprType, Arg2XprType, Arg3XprType> >::type> {
227 };
228 
229 } // end namespace internal
230 
231 template <typename TernaryOp, typename Arg1XprType, typename Arg2XprType, typename Arg3XprType>
233  : public TensorBase<TensorCwiseTernaryOp<TernaryOp, Arg1XprType, Arg2XprType, Arg3XprType>, ReadOnlyAccessors> {
234  public:
241 
242  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorCwiseTernaryOp(const Arg1XprType& arg1, const Arg2XprType& arg2,
243  const Arg3XprType& arg3,
244  const TernaryOp& func = TernaryOp())
245  : m_arg1_xpr(arg1), m_arg2_xpr(arg2), m_arg3_xpr(arg3), m_functor(func) {}
246 
247  EIGEN_DEVICE_FUNC const TernaryOp& functor() const { return m_functor; }
248 
251  return m_arg1_xpr;
252  }
253 
255  return m_arg2_xpr;
256  }
257 
259  return m_arg3_xpr;
260  }
261 
262  protected:
263  typename Arg1XprType::Nested m_arg1_xpr;
264  typename Arg2XprType::Nested m_arg2_xpr;
265  typename Arg3XprType::Nested m_arg3_xpr;
266  const TernaryOp m_functor;
267 };
268 
269 namespace internal {
270 template <typename IfXprType, typename ThenXprType, typename ElseXprType>
271 struct traits<TensorSelectOp<IfXprType, ThenXprType, ElseXprType> > : traits<ThenXprType> {
276  typedef
278  typedef typename IfXprType::Nested IfNested;
279  typedef typename ThenXprType::Nested ThenNested;
280  typedef typename ElseXprType::Nested ElseNested;
281  static constexpr int NumDimensions = XprTraits::NumDimensions;
282  static constexpr int Layout = XprTraits::Layout;
286 };
287 
288 template <typename IfXprType, typename ThenXprType, typename ElseXprType>
289 struct eval<TensorSelectOp<IfXprType, ThenXprType, ElseXprType>, Eigen::Dense> {
291 };
292 
293 template <typename IfXprType, typename ThenXprType, typename ElseXprType>
294 struct nested<TensorSelectOp<IfXprType, ThenXprType, ElseXprType>, 1,
295  typename eval<TensorSelectOp<IfXprType, ThenXprType, ElseXprType> >::type> {
297 };
298 
299 } // end namespace internal
300 
301 template <typename IfXprType, typename ThenXprType, typename ElseXprType>
302 class TensorSelectOp : public TensorBase<TensorSelectOp<IfXprType, ThenXprType, ElseXprType>, ReadOnlyAccessors> {
303  public:
306  typedef typename internal::promote_storage_type<typename ThenXprType::CoeffReturnType,
307  typename ElseXprType::CoeffReturnType>::ret CoeffReturnType;
311 
312  EIGEN_DEVICE_FUNC TensorSelectOp(const IfXprType& a_condition, const ThenXprType& a_then, const ElseXprType& a_else)
313  : m_condition(a_condition), m_then(a_then), m_else(a_else) {}
314 
315  EIGEN_DEVICE_FUNC const IfXprType& ifExpression() const { return m_condition; }
316 
317  EIGEN_DEVICE_FUNC const ThenXprType& thenExpression() const { return m_then; }
318 
319  EIGEN_DEVICE_FUNC const ElseXprType& elseExpression() const { return m_else; }
320 
321  protected:
322  typename IfXprType::Nested m_condition;
323  typename ThenXprType::Nested m_then;
324  typename ElseXprType::Nested m_else;
325 };
326 
327 } // end namespace Eigen
328 
329 #endif // EIGEN_CXX11_TENSOR_TENSOR_EXPR_H
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:892
#define EIGEN_STRONG_INLINE
Definition: Macros.h:834
SCALAR Scalar
Definition: bench_gemm.cpp:45
Generic expression where a coefficient-wise binary operator is applied to two expressions.
Definition: CwiseBinaryOp.h:79
The tensor base class.
Definition: TensorBase.h:1026
Definition: TensorExpr.h:162
Eigen::internal::traits< TensorCwiseBinaryOp >::Index Index
Definition: TensorExpr.h:171
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorCwiseBinaryOp(const LhsXprType &lhs, const RhsXprType &rhs, const BinaryOp &func=BinaryOp())
Definition: TensorExpr.h:173
EIGEN_DEVICE_FUNC const internal::remove_all_t< typename LhsXprType::Nested > & lhsExpression() const
Definition: TensorExpr.h:180
RhsXprType::Nested m_rhs_xpr
Definition: TensorExpr.h:190
const BinaryOp m_functor
Definition: TensorExpr.h:191
Scalar CoeffReturnType
Definition: TensorExpr.h:168
EIGEN_DEVICE_FUNC const internal::remove_all_t< typename RhsXprType::Nested > & rhsExpression() const
Definition: TensorExpr.h:184
Eigen::internal::nested< TensorCwiseBinaryOp >::type Nested
Definition: TensorExpr.h:169
Eigen::internal::traits< TensorCwiseBinaryOp >::Scalar Scalar
Definition: TensorExpr.h:166
Eigen::internal::traits< TensorCwiseBinaryOp >::StorageKind StorageKind
Definition: TensorExpr.h:170
LhsXprType::Nested m_lhs_xpr
Definition: TensorExpr.h:189
EIGEN_DEVICE_FUNC const BinaryOp & functor() const
Definition: TensorExpr.h:177
Eigen::NumTraits< Scalar >::Real RealScalar
Definition: TensorExpr.h:167
Definition: TensorExpr.h:49
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorCwiseNullaryOp(const XprType &xpr, const NullaryOp &func=NullaryOp())
Definition: TensorExpr.h:58
TensorCwiseNullaryOp< NullaryOp, XprType > Nested
Definition: TensorExpr.h:54
EIGEN_DEVICE_FUNC const internal::remove_all_t< typename XprType::Nested > & nestedExpression() const
Definition: TensorExpr.h:61
Eigen::internal::traits< TensorCwiseNullaryOp >::Scalar Scalar
Definition: TensorExpr.h:51
Eigen::NumTraits< Scalar >::Real RealScalar
Definition: TensorExpr.h:52
Eigen::internal::traits< TensorCwiseNullaryOp >::StorageKind StorageKind
Definition: TensorExpr.h:55
XprType::CoeffReturnType CoeffReturnType
Definition: TensorExpr.h:53
const NullaryOp m_functor
Definition: TensorExpr.h:67
Eigen::internal::traits< TensorCwiseNullaryOp >::Index Index
Definition: TensorExpr.h:56
EIGEN_DEVICE_FUNC const NullaryOp & functor() const
Definition: TensorExpr.h:63
XprType::Nested m_xpr
Definition: TensorExpr.h:66
Definition: TensorExpr.h:233
EIGEN_DEVICE_FUNC const internal::remove_all_t< typename Arg2XprType::Nested > & arg2Expression() const
Definition: TensorExpr.h:254
Eigen::internal::traits< TensorCwiseTernaryOp >::StorageKind StorageKind
Definition: TensorExpr.h:239
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorCwiseTernaryOp(const Arg1XprType &arg1, const Arg2XprType &arg2, const Arg3XprType &arg3, const TernaryOp &func=TernaryOp())
Definition: TensorExpr.h:242
EIGEN_DEVICE_FUNC const internal::remove_all_t< typename Arg3XprType::Nested > & arg3Expression() const
Definition: TensorExpr.h:258
Arg3XprType::Nested m_arg3_xpr
Definition: TensorExpr.h:265
EIGEN_DEVICE_FUNC const internal::remove_all_t< typename Arg1XprType::Nested > & arg1Expression() const
Definition: TensorExpr.h:250
Scalar CoeffReturnType
Definition: TensorExpr.h:237
Eigen::NumTraits< Scalar >::Real RealScalar
Definition: TensorExpr.h:236
EIGEN_DEVICE_FUNC const TernaryOp & functor() const
Definition: TensorExpr.h:247
Arg2XprType::Nested m_arg2_xpr
Definition: TensorExpr.h:264
Eigen::internal::traits< TensorCwiseTernaryOp >::Index Index
Definition: TensorExpr.h:240
Eigen::internal::traits< TensorCwiseTernaryOp >::Scalar Scalar
Definition: TensorExpr.h:235
const TernaryOp m_functor
Definition: TensorExpr.h:266
Arg1XprType::Nested m_arg1_xpr
Definition: TensorExpr.h:263
Eigen::internal::nested< TensorCwiseTernaryOp >::type Nested
Definition: TensorExpr.h:238
Definition: TensorExpr.h:97
Eigen::internal::traits< TensorCwiseUnaryOp >::StorageKind StorageKind
Definition: TensorExpr.h:105
XprType::Nested m_xpr
Definition: TensorExpr.h:117
const UnaryOp m_functor
Definition: TensorExpr.h:118
EIGEN_DEVICE_FUNC const internal::remove_all_t< typename XprType::Nested > & nestedExpression() const
Definition: TensorExpr.h:114
Eigen::internal::traits< TensorCwiseUnaryOp >::Scalar Scalar
Definition: TensorExpr.h:101
Scalar CoeffReturnType
Definition: TensorExpr.h:103
EIGEN_DEVICE_FUNC const UnaryOp & functor() const
Definition: TensorExpr.h:111
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorCwiseUnaryOp(const XprType &xpr, const UnaryOp &func=UnaryOp())
Definition: TensorExpr.h:108
Eigen::internal::traits< TensorCwiseUnaryOp >::Index Index
Definition: TensorExpr.h:106
Eigen::internal::nested< TensorCwiseUnaryOp >::type Nested
Definition: TensorExpr.h:104
Eigen::NumTraits< Scalar >::Real RealScalar
Definition: TensorExpr.h:102
Definition: TensorExpr.h:302
ThenXprType::Nested m_then
Definition: TensorExpr.h:323
Eigen::internal::traits< TensorSelectOp >::Index Index
Definition: TensorExpr.h:310
Eigen::NumTraits< Scalar >::Real RealScalar
Definition: TensorExpr.h:305
Eigen::internal::nested< TensorSelectOp >::type Nested
Definition: TensorExpr.h:308
EIGEN_DEVICE_FUNC TensorSelectOp(const IfXprType &a_condition, const ThenXprType &a_then, const ElseXprType &a_else)
Definition: TensorExpr.h:312
Eigen::internal::traits< TensorSelectOp >::StorageKind StorageKind
Definition: TensorExpr.h:309
Eigen::internal::traits< TensorSelectOp >::Scalar Scalar
Definition: TensorExpr.h:304
EIGEN_DEVICE_FUNC const IfXprType & ifExpression() const
Definition: TensorExpr.h:315
EIGEN_DEVICE_FUNC const ThenXprType & thenExpression() const
Definition: TensorExpr.h:317
IfXprType::Nested m_condition
Definition: TensorExpr.h:322
ElseXprType::Nested m_else
Definition: TensorExpr.h:324
internal::promote_storage_type< typename ThenXprType::CoeffReturnType, typename ElseXprType::CoeffReturnType >::ret CoeffReturnType
Definition: TensorExpr.h:307
EIGEN_DEVICE_FUNC const ElseXprType & elseExpression() const
Definition: TensorExpr.h:319
Eigen::DenseIndex ret
Definition: level1_cplx_impl.h:43
typename remove_all< T >::type remove_all_t
Definition: Meta.h:142
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
Extend namespace for flags.
Definition: fsi_chan_precond_driver.cc:56
val
Definition: calibrate.py:119
type
Definition: compute_granudrum_aor.py:141
Definition: Eigen_Colamd.h:49
Definition: Constants.h:519
T Real
Definition: NumTraits.h:183
Definition: TensorForwardDeclarations.h:54
A * type
Definition: TensorForwardDeclarations.h:55
const TensorCwiseBinaryOp< BinaryOp, LhsXprType, RhsXprType > & type
Definition: TensorExpr.h:149
const TensorCwiseTernaryOp< TernaryOp, Arg1XprType, Arg2XprType, Arg3XprType > & type
Definition: TensorExpr.h:220
const TensorCwiseUnaryOp< UnaryOp, XprType > & type
Definition: TensorExpr.h:86
const TensorSelectOp< IfXprType, ThenXprType, ElseXprType > & type
Definition: TensorExpr.h:290
Definition: XprHelper.h:427
Definition: TensorTraits.h:152
ref_selector< T >::type type
Definition: TensorTraits.h:153
Definition: XprHelper.h:145
Definition: XprHelper.h:591
Definition: Meta.h:388
std::remove_reference_t< RhsNested > RhsNested_
Definition: TensorExpr.h:137
promote_index_type< typename traits< LhsXprType >::Index, typename traits< RhsXprType >::Index >::type Index
Definition: TensorExpr.h:133
result_of< BinaryOp(typename LhsXprType::Scalar, typename RhsXprType::Scalar)>::type Scalar
Definition: TensorExpr.h:128
promote_storage_type< typename traits< LhsXprType >::StorageKind, typename traits< RhsXprType >::StorageKind >::ret StorageKind
Definition: TensorExpr.h:131
std::remove_reference_t< LhsNested > LhsNested_
Definition: TensorExpr.h:136
TypeConversion< Scalar, std::conditional_t< Pointer_type_promotion< typename LhsXprType::Scalar, Scalar >::val, typename traits< LhsXprType >::PointerType, typename traits< RhsXprType >::PointerType > >::type PointerType
Definition: TensorExpr.h:143
XprType::Nested XprTypeNested
Definition: TensorExpr.h:38
XprTraits::PointerType PointerType
Definition: TensorExpr.h:42
std::remove_reference_t< XprTypeNested > XprTypeNested_
Definition: TensorExpr.h:39
traits< XprType > XprTraits
Definition: TensorExpr.h:36
TypeConversion< Scalar, std::conditional_t< Pointer_type_promotion< typename Arg2XprType::Scalar, Scalar >::val, typename traits< Arg2XprType >::PointerType, typename traits< Arg3XprType >::PointerType > >::type PointerType
Definition: TensorExpr.h:214
std::remove_reference_t< Arg1Nested > Arg1Nested_
Definition: TensorExpr.h:206
std::remove_reference_t< Arg2Nested > Arg2Nested_
Definition: TensorExpr.h:207
result_of< TernaryOp(typename Arg1XprType::Scalar, typename Arg2XprType::Scalar, typename Arg3XprType::Scalar)>::type Scalar
Definition: TensorExpr.h:199
std::remove_reference_t< Arg3Nested > Arg3Nested_
Definition: TensorExpr.h:208
XprType::Nested XprTypeNested
Definition: TensorExpr.h:77
result_of< UnaryOp(typename XprType::Scalar)>::type Scalar
Definition: TensorExpr.h:75
std::remove_reference_t< XprTypeNested > XprTypeNested_
Definition: TensorExpr.h:78
TypeConversion< Scalar, typename XprTraits::PointerType >::type PointerType
Definition: TensorExpr.h:81
traits< XprType > XprTraits
Definition: TensorExpr.h:76
promote_storage_type< typename traits< ThenXprType >::StorageKind, typename traits< ElseXprType >::StorageKind >::ret StorageKind
Definition: TensorExpr.h:275
traits< ThenXprType >::Scalar Scalar
Definition: TensorExpr.h:272
std::conditional_t< Pointer_type_promotion< typename ThenXprType::Scalar, Scalar >::val, typename traits< ThenXprType >::PointerType, typename traits< ElseXprType >::PointerType > PointerType
Definition: TensorExpr.h:285
promote_index_type< typename traits< ElseXprType >::Index, typename traits< ThenXprType >::Index >::type Index
Definition: TensorExpr.h:277
Definition: ForwardDeclarations.h:21
Definition: benchGeometry.cpp:21