$darkmode
Eigen  5.0.1-dev
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 // IWYU pragma: private
15 #include "./InternalHeaderCheck.h"
16 
17 namespace Eigen {
18 
19 namespace internal {
20 template <typename BinaryOp, typename Lhs, typename Rhs>
21 struct traits<CwiseBinaryOp<BinaryOp, Lhs, Rhs>> {
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<BinaryOp(const typename Lhs::Scalar&, const typename Rhs::Scalar&)>::type Scalar;
36  typedef typename cwise_promote_storage_type<typename traits<Lhs>::StorageKind, typename traits<Rhs>::StorageKind,
37  BinaryOp>::ret StorageKind;
38  typedef typename promote_index_type<typename traits<Lhs>::StorageIndex, typename traits<Rhs>::StorageIndex>::type
39  StorageIndex;
40  typedef typename Lhs::Nested LhsNested;
41  typedef typename Rhs::Nested RhsNested;
42  typedef std::remove_reference_t<LhsNested> LhsNested_;
43  typedef std::remove_reference_t<RhsNested> RhsNested_;
44  enum {
45  Flags = cwise_promote_storage_order<typename traits<Lhs>::StorageKind, typename traits<Rhs>::StorageKind,
46  LhsNested_::Flags & RowMajorBit, RhsNested_::Flags & RowMajorBit>::value
47  };
48 };
49 } // end namespace internal
50 
51 template <typename BinaryOp, typename Lhs, typename Rhs, typename StorageKind>
52 class CwiseBinaryOpImpl;
53 
74 template <typename BinaryOp, typename LhsType, typename RhsType>
75 class CwiseBinaryOp : public CwiseBinaryOpImpl<BinaryOp, LhsType, RhsType,
76  typename internal::cwise_promote_storage_type<
77  typename internal::traits<LhsType>::StorageKind,
78  typename internal::traits<RhsType>::StorageKind, BinaryOp>::ret>,
79  internal::no_assignment_operator {
80  public:
81  typedef internal::remove_all_t<BinaryOp> Functor;
82  typedef internal::remove_all_t<LhsType> Lhs;
83  typedef internal::remove_all_t<RhsType> Rhs;
84 
85  typedef typename CwiseBinaryOpImpl<
86  BinaryOp, LhsType, RhsType,
87  typename internal::cwise_promote_storage_type<typename internal::traits<LhsType>::StorageKind,
88  typename internal::traits<Rhs>::StorageKind, BinaryOp>::ret>::Base
89  Base;
90  EIGEN_GENERIC_PUBLIC_INTERFACE(CwiseBinaryOp)
91 
92  EIGEN_CHECK_BINARY_COMPATIBILIY(BinaryOp, typename Lhs::Scalar, typename Rhs::Scalar)
93  EIGEN_STATIC_ASSERT_SAME_MATRIX_SIZE(Lhs, Rhs)
94 
95  typedef typename internal::ref_selector<LhsType>::type LhsNested;
96  typedef typename internal::ref_selector<RhsType>::type RhsNested;
97  typedef std::remove_reference_t<LhsNested> LhsNested_;
98  typedef std::remove_reference_t<RhsNested> RhsNested_;
99 
100 #if EIGEN_COMP_MSVC
101  // Required for Visual Studio or the Copy constructor will probably not get inlined!
102  EIGEN_STRONG_INLINE CwiseBinaryOp(const CwiseBinaryOp<BinaryOp, LhsType, RhsType>&) = default;
103 #endif
104 
105  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CwiseBinaryOp(const Lhs& aLhs, const Rhs& aRhs,
106  const BinaryOp& func = BinaryOp())
107  : m_lhs(aLhs), m_rhs(aRhs), m_functor(func) {
108  eigen_assert(aLhs.rows() == aRhs.rows() && aLhs.cols() == aRhs.cols());
109  }
110 
111  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr Index rows() const noexcept {
112  // return the fixed size type if available to enable compile time optimizations
113  return internal::traits<internal::remove_all_t<LhsNested>>::RowsAtCompileTime == Dynamic ? m_rhs.rows()
114  : m_lhs.rows();
115  }
116  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr Index cols() const noexcept {
117  // return the fixed size type if available to enable compile time optimizations
118  return internal::traits<internal::remove_all_t<LhsNested>>::ColsAtCompileTime == Dynamic ? m_rhs.cols()
119  : m_lhs.cols();
120  }
121 
123  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const LhsNested_& lhs() const { return m_lhs; }
125  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const RhsNested_& rhs() const { return m_rhs; }
127  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const BinaryOp& functor() const { return m_functor; }
128 
129  protected:
130  LhsNested m_lhs;
131  RhsNested m_rhs;
132  const BinaryOp m_functor;
133 };
134 
135 // Generic API dispatcher
136 template <typename BinaryOp, typename Lhs, typename Rhs, typename StorageKind>
137 class CwiseBinaryOpImpl : public internal::generic_xpr_base<CwiseBinaryOp<BinaryOp, Lhs, Rhs>>::type {
138  public:
139  typedef typename internal::generic_xpr_base<CwiseBinaryOp<BinaryOp, Lhs, Rhs>>::type Base;
140 };
141 
146 template <typename Derived>
147 template <typename OtherDerived>
148 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::operator-=(const MatrixBase<OtherDerived>& other) {
149  call_assignment(derived(), other.derived(), internal::sub_assign_op<Scalar, typename OtherDerived::Scalar>());
150  return derived();
151 }
152 
157 template <typename Derived>
158 template <typename OtherDerived>
159 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::operator+=(const MatrixBase<OtherDerived>& other) {
160  call_assignment(derived(), other.derived(), internal::add_assign_op<Scalar, typename OtherDerived::Scalar>());
161  return derived();
162 }
163 
164 } // end namespace Eigen
165 
166 #endif // EIGEN_CWISE_BINARY_OP_H
const RhsNested_ & rhs() const
Definition: CwiseBinaryOp.h:125
constexpr Derived & derived()
Definition: EigenBase.h:49
Namespace containing all symbols from the Eigen library.
Definition: B01_Experimental.dox:1
const unsigned int RowMajorBit
Definition: Constants.h:70
Derived & operator+=(const MatrixBase< OtherDerived > &other)
Definition: CwiseBinaryOp.h:159
Generic expression where a coefficient-wise binary operator is applied to two expressions.
Definition: CwiseBinaryOp.h:75
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82
Derived & operator-=(const MatrixBase< OtherDerived > &other)
Definition: CwiseBinaryOp.h:148
const LhsNested_ & lhs() const
Definition: CwiseBinaryOp.h:123
const int Dynamic
Definition: Constants.h:25
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:52
const BinaryOp & functor() const
Definition: CwiseBinaryOp.h:127