Eigen  3.3.90 (mercurial changeset 94875feeeeb9)
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 namespace Eigen {
15 
16 namespace internal {
17 template<typename BinaryOp, typename Lhs, typename Rhs>
18 struct traits<CwiseBinaryOp<BinaryOp, Lhs, Rhs> >
19 {
20  // we must not inherit from traits<Lhs> since it has
21  // the potential to cause problems with MSVC
22  typedef typename remove_all<Lhs>::type Ancestor;
23  typedef typename traits<Ancestor>::XprKind XprKind;
24  enum {
25  RowsAtCompileTime = traits<Ancestor>::RowsAtCompileTime,
26  ColsAtCompileTime = traits<Ancestor>::ColsAtCompileTime,
27  MaxRowsAtCompileTime = traits<Ancestor>::MaxRowsAtCompileTime,
28  MaxColsAtCompileTime = traits<Ancestor>::MaxColsAtCompileTime
29  };
30 
31  // even though we require Lhs and Rhs to have the same scalar type (see CwiseBinaryOp constructor),
32  // we still want to handle the case when the result type is different.
33  typedef typename result_of<
34  BinaryOp(
35  const typename Lhs::Scalar&,
36  const typename Rhs::Scalar&
37  )
38  >::type Scalar;
39  typedef typename cwise_promote_storage_type<typename traits<Lhs>::StorageKind,
40  typename traits<Rhs>::StorageKind,
41  BinaryOp>::ret StorageKind;
42  typedef typename promote_index_type<typename traits<Lhs>::StorageIndex,
43  typename traits<Rhs>::StorageIndex>::type StorageIndex;
44  typedef typename Lhs::Nested LhsNested;
45  typedef typename Rhs::Nested RhsNested;
46  typedef typename remove_reference<LhsNested>::type _LhsNested;
47  typedef typename remove_reference<RhsNested>::type _RhsNested;
48  enum {
49  Flags = cwise_promote_storage_order<typename traits<Lhs>::StorageKind,typename traits<Rhs>::StorageKind,_LhsNested::Flags & RowMajorBit,_RhsNested::Flags & RowMajorBit>::value
50  };
51 };
52 } // end namespace internal
53 
54 template<typename BinaryOp, typename Lhs, typename Rhs, typename StorageKind>
55 class CwiseBinaryOpImpl;
56 
76 template<typename BinaryOp, typename LhsType, typename RhsType>
77 class CwiseBinaryOp :
78  public CwiseBinaryOpImpl<
79  BinaryOp, LhsType, RhsType,
80  typename internal::cwise_promote_storage_type<typename internal::traits<LhsType>::StorageKind,
81  typename internal::traits<RhsType>::StorageKind,
82  BinaryOp>::ret>,
83  internal::no_assignment_operator
84 {
85  public:
86 
87  typedef typename internal::remove_all<BinaryOp>::type Functor;
88  typedef typename internal::remove_all<LhsType>::type Lhs;
89  typedef typename internal::remove_all<RhsType>::type Rhs;
90 
91  typedef typename CwiseBinaryOpImpl<
92  BinaryOp, LhsType, RhsType,
93  typename internal::cwise_promote_storage_type<typename internal::traits<LhsType>::StorageKind,
94  typename internal::traits<Rhs>::StorageKind,
95  BinaryOp>::ret>::Base Base;
96  EIGEN_GENERIC_PUBLIC_INTERFACE(CwiseBinaryOp)
97 
98  typedef typename internal::ref_selector<LhsType>::type LhsNested;
99  typedef typename internal::ref_selector<RhsType>::type RhsNested;
100  typedef typename internal::remove_reference<LhsNested>::type _LhsNested;
101  typedef typename internal::remove_reference<RhsNested>::type _RhsNested;
102 
103 #if EIGEN_COMP_MSVC && EIGEN_HAS_CXX11
104  //Required for Visual Studio or the Copy constructor will probably not get inlined!
105  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
107 #endif
108 
109  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
110  CwiseBinaryOp(const Lhs& aLhs, const Rhs& aRhs, const BinaryOp& func = BinaryOp())
111  : m_lhs(aLhs), m_rhs(aRhs), m_functor(func)
112  {
113  EIGEN_CHECK_BINARY_COMPATIBILIY(BinaryOp,typename Lhs::Scalar,typename Rhs::Scalar);
114  // require the sizes to match
115  EIGEN_STATIC_ASSERT_SAME_MATRIX_SIZE(Lhs, Rhs)
116  eigen_assert(aLhs.rows() == aRhs.rows() && aLhs.cols() == aRhs.cols());
117  }
118 
119  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
120  Index rows() const {
121  // return the fixed size type if available to enable compile time optimizations
122  if (internal::traits<typename internal::remove_all<LhsNested>::type>::RowsAtCompileTime==Dynamic)
123  return m_rhs.rows();
124  else
125  return m_lhs.rows();
126  }
127  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
128  Index cols() const {
129  // return the fixed size type if available to enable compile time optimizations
130  if (internal::traits<typename internal::remove_all<LhsNested>::type>::ColsAtCompileTime==Dynamic)
131  return m_rhs.cols();
132  else
133  return m_lhs.cols();
134  }
135 
137  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
138  const _LhsNested& lhs() const { return m_lhs; }
140  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
141  const _RhsNested& rhs() const { return m_rhs; }
143  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
144  const BinaryOp& functor() const { return m_functor; }
145 
146  protected:
147  LhsNested m_lhs;
148  RhsNested m_rhs;
149  const BinaryOp m_functor;
150 };
151 
152 // Generic API dispatcher
153 template<typename BinaryOp, typename Lhs, typename Rhs, typename StorageKind>
154 class CwiseBinaryOpImpl
155  : public internal::generic_xpr_base<CwiseBinaryOp<BinaryOp, Lhs, Rhs> >::type
156 {
157 public:
158  typedef typename internal::generic_xpr_base<CwiseBinaryOp<BinaryOp, Lhs, Rhs> >::type Base;
159 };
160 
165 template<typename Derived>
166 template<typename OtherDerived>
167 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived &
169 {
170  call_assignment(derived(), other.derived(), internal::sub_assign_op<Scalar,typename OtherDerived::Scalar>());
171  return derived();
172 }
173 
178 template<typename Derived>
179 template<typename OtherDerived>
180 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived &
182 {
183  call_assignment(derived(), other.derived(), internal::add_assign_op<Scalar,typename OtherDerived::Scalar>());
184  return derived();
185 }
186 
187 } // end namespace Eigen
188 
189 #endif // EIGEN_CWISE_BINARY_OP_H
const _LhsNested & lhs() const
Definition: CwiseBinaryOp.h:138
Namespace containing all symbols from the Eigen library.
Definition: Core:129
const unsigned int RowMajorBit
Definition: Constants.h:65
Derived & operator+=(const MatrixBase< OtherDerived > &other)
Definition: CwiseBinaryOp.h:181
Generic expression where a coefficient-wise binary operator is applied to two expressions.
Definition: CwiseBinaryOp.h:77
Derived & derived()
Definition: EigenBase.h:46
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:42
Definition: Eigen_Colamd.h:50
Derived & operator-=(const MatrixBase< OtherDerived > &other)
Definition: CwiseBinaryOp.h:168
const int Dynamic
Definition: Constants.h:21
const _RhsNested & rhs() const
Definition: CwiseBinaryOp.h:141
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
const BinaryOp & functor() const
Definition: CwiseBinaryOp.h:144