CwiseBinaryOp.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) 2008-2014 Gael Guennebaud <gael.guennebaud@inria.fr>
5 // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
6 //
7 // This Source Code Form is subject to the terms of the Mozilla
8 // Public License v. 2.0. If a copy of the MPL was not distributed
9 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 
11 #ifndef EIGEN_CWISE_BINARY_OP_H
12 #define EIGEN_CWISE_BINARY_OP_H
13 
14 #include "./InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 
18 namespace internal {
19 template<typename BinaryOp, typename Lhs, typename Rhs>
20 struct traits<CwiseBinaryOp<BinaryOp, Lhs, Rhs> >
21 {
22  // we must not inherit from traits<Lhs> since it has
23  // the potential to cause problems with MSVC
24  typedef remove_all_t<Lhs> Ancestor;
25  typedef typename traits<Ancestor>::XprKind XprKind;
26  enum {
27  RowsAtCompileTime = traits<Ancestor>::RowsAtCompileTime,
28  ColsAtCompileTime = traits<Ancestor>::ColsAtCompileTime,
29  MaxRowsAtCompileTime = traits<Ancestor>::MaxRowsAtCompileTime,
30  MaxColsAtCompileTime = traits<Ancestor>::MaxColsAtCompileTime
31  };
32 
33  // even though we require Lhs and Rhs to have the same scalar type (see CwiseBinaryOp constructor),
34  // we still want to handle the case when the result type is different.
35  typedef typename result_of<
36  BinaryOp(
37  const typename Lhs::Scalar&,
38  const typename Rhs::Scalar&
39  )
40  >::type Scalar;
41  typedef typename cwise_promote_storage_type<typename traits<Lhs>::StorageKind,
42  typename traits<Rhs>::StorageKind,
43  BinaryOp>::ret StorageKind;
44  typedef typename promote_index_type<typename traits<Lhs>::StorageIndex,
45  typename traits<Rhs>::StorageIndex>::type StorageIndex;
46  typedef typename Lhs::Nested LhsNested;
47  typedef typename Rhs::Nested RhsNested;
48  typedef std::remove_reference_t<LhsNested> LhsNested_;
49  typedef std::remove_reference_t<RhsNested> RhsNested_;
50  enum {
51  Flags = cwise_promote_storage_order<typename traits<Lhs>::StorageKind,typename traits<Rhs>::StorageKind,LhsNested_::Flags & RowMajorBit,RhsNested_::Flags & RowMajorBit>::value
52  };
53 };
54 } // end namespace internal
55 
56 template<typename BinaryOp, typename Lhs, typename Rhs, typename StorageKind>
57 class CwiseBinaryOpImpl;
58 
78 template<typename BinaryOp, typename LhsType, typename RhsType>
80  public CwiseBinaryOpImpl<
81  BinaryOp, LhsType, RhsType,
82  typename internal::cwise_promote_storage_type<typename internal::traits<LhsType>::StorageKind,
83  typename internal::traits<RhsType>::StorageKind,
84  BinaryOp>::ret>,
85  internal::no_assignment_operator
86 {
87  public:
88 
92 
93  typedef typename CwiseBinaryOpImpl<
94  BinaryOp, LhsType, RhsType,
95  typename internal::cwise_promote_storage_type<typename internal::traits<LhsType>::StorageKind,
96  typename internal::traits<Rhs>::StorageKind,
99 
100  EIGEN_CHECK_BINARY_COMPATIBILIY(BinaryOp,typename Lhs::Scalar,typename Rhs::Scalar)
102 
103  typedef typename internal::ref_selector<LhsType>::type LhsNested;
104  typedef typename internal::ref_selector<RhsType>::type RhsNested;
105  typedef std::remove_reference_t<LhsNested> LhsNested_;
106  typedef std::remove_reference_t<RhsNested> RhsNested_;
107 
108 #if EIGEN_COMP_MSVC
109  //Required for Visual Studio or the Copy constructor will probably not get inlined!
110  EIGEN_STRONG_INLINE
112 #endif
113 
114  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
115  CwiseBinaryOp(const Lhs& aLhs, const Rhs& aRhs, const BinaryOp& func = BinaryOp())
116  : m_lhs(aLhs), m_rhs(aRhs), m_functor(func)
117  {
118  eigen_assert(aLhs.rows() == aRhs.rows() && aLhs.cols() == aRhs.cols());
119  }
120 
121  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR
123  // return the fixed size type if available to enable compile time optimizations
124  return internal::traits<internal::remove_all_t<LhsNested>>::RowsAtCompileTime==Dynamic ? m_rhs.rows() : m_lhs.rows();
125  }
126  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR
128  // return the fixed size type if available to enable compile time optimizations
129  return internal::traits<internal::remove_all_t<LhsNested>>::ColsAtCompileTime==Dynamic ? m_rhs.cols() : m_lhs.cols();
130  }
131 
133  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
134  const LhsNested_& lhs() const { return m_lhs; }
136  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
137  const RhsNested_& rhs() const { return m_rhs; }
139  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
140  const BinaryOp& functor() const { return m_functor; }
141 
142  protected:
146 };
147 
148 // Generic API dispatcher
149 template<typename BinaryOp, typename Lhs, typename Rhs, typename StorageKind>
151  : public internal::generic_xpr_base<CwiseBinaryOp<BinaryOp, Lhs, Rhs> >::type
152 {
153 public:
154  typedef typename internal::generic_xpr_base<CwiseBinaryOp<BinaryOp, Lhs, Rhs> >::type Base;
155 };
156 
161 template<typename Derived>
162 template<typename OtherDerived>
163 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived &
165 {
166  call_assignment(derived(), other.derived(), internal::sub_assign_op<Scalar,typename OtherDerived::Scalar>());
167  return derived();
168 }
169 
174 template<typename Derived>
175 template<typename OtherDerived>
176 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived &
178 {
179  call_assignment(derived(), other.derived(), internal::add_assign_op<Scalar,typename OtherDerived::Scalar>());
180  return derived();
181 }
182 
183 } // end namespace Eigen
184 
185 #endif // EIGEN_CWISE_BINARY_OP_H
#define EIGEN_GENERIC_PUBLIC_INTERFACE(Derived)
Definition: Macros.h:1149
#define EIGEN_NOEXCEPT
Definition: Macros.h:1260
#define EIGEN_CONSTEXPR
Definition: Macros.h:747
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:883
#define eigen_assert(x)
Definition: Macros.h:902
#define EIGEN_STATIC_ASSERT_SAME_MATRIX_SIZE(TYPE0, TYPE1)
Definition: StaticAssert.h:86
#define EIGEN_CHECK_BINARY_COMPATIBILIY(BINOP, LHS, RHS)
Definition: XprHelper.h:900
internal::generic_xpr_base< CwiseBinaryOp< BinaryOp, Lhs, Rhs > >::type Base
Generic expression where a coefficient-wise binary operator is applied to two expressions.
Definition: CwiseBinaryOp.h:86
const BinaryOp m_functor
std::remove_reference_t< LhsNested > LhsNested_
std::remove_reference_t< RhsNested > RhsNested_
internal::ref_selector< LhsType >::type LhsNested
internal::remove_all_t< LhsType > Lhs
Definition: CwiseBinaryOp.h:90
internal::ref_selector< RhsType >::type RhsNested
internal::remove_all_t< RhsType > Rhs
Definition: CwiseBinaryOp.h:91
EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
const LhsNested_ & lhs() const
const RhsNested_ & rhs() const
const BinaryOp & functor() const
CwiseBinaryOpImpl< BinaryOp, LhsType, RhsType, typename internal::cwise_promote_storage_type< typename internal::traits< LhsType >::StorageKind, typename internal::traits< Rhs >::StorageKind, BinaryOp >::ret >::Base Base
Definition: CwiseBinaryOp.h:97
internal::remove_all_t< BinaryOp > Functor
Definition: CwiseBinaryOp.h:89
CwiseBinaryOp(const Lhs &aLhs, const Rhs &aRhs, const BinaryOp &func=BinaryOp())
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:52
Derived & operator-=(const MatrixBase< OtherDerived > &other)
Derived & operator+=(const MatrixBase< OtherDerived > &other)
const unsigned int RowMajorBit
Definition: Constants.h:68
void call_assignment(Dst &dst, const Src &src)
typename remove_all< T >::type remove_all_t
Definition: Meta.h:119
: InteropHeaders
Definition: Core:139
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82
const int Dynamic
Definition: Constants.h:24
Definition: BFloat16.h:222