Eigen  3.4.90 (git rev 67eeba6e720c5745abc77ae6c92ce0a44aa7b7ae)
CwiseBinaryOp.h
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 
89  typedef internal::remove_all_t<BinaryOp> Functor;
90  typedef internal::remove_all_t<LhsType> Lhs;
91  typedef internal::remove_all_t<RhsType> Rhs;
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,
97  BinaryOp>::ret>::Base Base;
98  EIGEN_GENERIC_PUBLIC_INTERFACE(CwiseBinaryOp)
99 
100  EIGEN_CHECK_BINARY_COMPATIBILIY(BinaryOp,typename Lhs::Scalar,typename Rhs::Scalar)
101  EIGEN_STATIC_ASSERT_SAME_MATRIX_SIZE(Lhs, Rhs)
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
122  Index rows() const EIGEN_NOEXCEPT {
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
127  Index cols() const EIGEN_NOEXCEPT {
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:
143  LhsNested m_lhs;
144  RhsNested m_rhs;
145  const BinaryOp m_functor;
146 };
147 
148 // Generic API dispatcher
149 template<typename BinaryOp, typename Lhs, typename Rhs, typename StorageKind>
150 class CwiseBinaryOpImpl
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
Generic expression where a coefficient-wise binary operator is applied to two expressions.
Definition: CwiseBinaryOp.h:86
const LhsNested_ & lhs() const
Definition: CwiseBinaryOp.h:134
const RhsNested_ & rhs() const
Definition: CwiseBinaryOp.h:137
const BinaryOp & functor() const
Definition: CwiseBinaryOp.h:140
Derived & derived()
Definition: EigenBase.h:48
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:52
Derived & operator-=(const MatrixBase< OtherDerived > &other)
Definition: CwiseBinaryOp.h:164
Derived & operator+=(const MatrixBase< OtherDerived > &other)
Definition: CwiseBinaryOp.h:177
const unsigned int RowMajorBit
Definition: Constants.h:68
Namespace containing all symbols from the Eigen library.
Definition: Core:139
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:59
const int Dynamic
Definition: Constants.h:24