source: pacpussensors/trunk/Vislab/lib3dv/eigen/Eigen/src/Core/CwiseBinaryOp.h@ 136

Last change on this file since 136 was 136, checked in by ldecherf, 7 years ago

Doc

File size: 9.6 KB
Line 
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2008-2009 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
14namespace Eigen {
15
16/** \class CwiseBinaryOp
17 * \ingroup Core_Module
18 *
19 * \brief Generic expression where a coefficient-wise binary operator is applied to two expressions
20 *
21 * \param BinaryOp template functor implementing the operator
22 * \param Lhs the type of the left-hand side
23 * \param Rhs the type of the right-hand side
24 *
25 * This class represents an expression where a coefficient-wise binary operator is applied to two expressions.
26 * It is the return type of binary operators, by which we mean only those binary operators where
27 * both the left-hand side and the right-hand side are Eigen expressions.
28 * For example, the return type of matrix1+matrix2 is a CwiseBinaryOp.
29 *
30 * Most of the time, this is the only way that it is used, so you typically don't have to name
31 * CwiseBinaryOp types explicitly.
32 *
33 * \sa MatrixBase::binaryExpr(const MatrixBase<OtherDerived> &,const CustomBinaryOp &) const, class CwiseUnaryOp, class CwiseNullaryOp
34 */
35
36namespace internal {
37template<typename BinaryOp, typename Lhs, typename Rhs>
38struct traits<CwiseBinaryOp<BinaryOp, Lhs, Rhs> >
39{
40 // we must not inherit from traits<Lhs> since it has
41 // the potential to cause problems with MSVC
42 typedef typename remove_all<Lhs>::type Ancestor;
43 typedef typename traits<Ancestor>::XprKind XprKind;
44 enum {
45 RowsAtCompileTime = traits<Ancestor>::RowsAtCompileTime,
46 ColsAtCompileTime = traits<Ancestor>::ColsAtCompileTime,
47 MaxRowsAtCompileTime = traits<Ancestor>::MaxRowsAtCompileTime,
48 MaxColsAtCompileTime = traits<Ancestor>::MaxColsAtCompileTime
49 };
50
51 // even though we require Lhs and Rhs to have the same scalar type (see CwiseBinaryOp constructor),
52 // we still want to handle the case when the result type is different.
53 typedef typename result_of<
54 BinaryOp(
55 typename Lhs::Scalar,
56 typename Rhs::Scalar
57 )
58 >::type Scalar;
59 typedef typename promote_storage_type<typename traits<Lhs>::StorageKind,
60 typename traits<Rhs>::StorageKind>::ret StorageKind;
61 typedef typename promote_index_type<typename traits<Lhs>::Index,
62 typename traits<Rhs>::Index>::type Index;
63 typedef typename Lhs::Nested LhsNested;
64 typedef typename Rhs::Nested RhsNested;
65 typedef typename remove_reference<LhsNested>::type _LhsNested;
66 typedef typename remove_reference<RhsNested>::type _RhsNested;
67 enum {
68 LhsCoeffReadCost = _LhsNested::CoeffReadCost,
69 RhsCoeffReadCost = _RhsNested::CoeffReadCost,
70 LhsFlags = _LhsNested::Flags,
71 RhsFlags = _RhsNested::Flags,
72 SameType = is_same<typename _LhsNested::Scalar,typename _RhsNested::Scalar>::value,
73 StorageOrdersAgree = (int(Lhs::Flags)&RowMajorBit)==(int(Rhs::Flags)&RowMajorBit),
74 Flags0 = (int(LhsFlags) | int(RhsFlags)) & (
75 HereditaryBits
76 | (int(LhsFlags) & int(RhsFlags) &
77 ( AlignedBit
78 | (StorageOrdersAgree ? LinearAccessBit : 0)
79 | (functor_traits<BinaryOp>::PacketAccess && StorageOrdersAgree && SameType ? PacketAccessBit : 0)
80 )
81 )
82 ),
83 Flags = (Flags0 & ~RowMajorBit) | (LhsFlags & RowMajorBit),
84 Cost0 = EIGEN_ADD_COST(LhsCoeffReadCost,RhsCoeffReadCost),
85 CoeffReadCost = EIGEN_ADD_COST(Cost0,functor_traits<BinaryOp>::Cost)
86 };
87};
88} // end namespace internal
89
90// we require Lhs and Rhs to have the same scalar type. Currently there is no example of a binary functor
91// that would take two operands of different types. If there were such an example, then this check should be
92// moved to the BinaryOp functors, on a per-case basis. This would however require a change in the BinaryOp functors, as
93// currently they take only one typename Scalar template parameter.
94// It is tempting to always allow mixing different types but remember that this is often impossible in the vectorized paths.
95// So allowing mixing different types gives very unexpected errors when enabling vectorization, when the user tries to
96// add together a float matrix and a double matrix.
97#define EIGEN_CHECK_BINARY_COMPATIBILIY(BINOP,LHS,RHS) \
98 EIGEN_STATIC_ASSERT((internal::functor_is_product_like<BINOP>::ret \
99 ? int(internal::scalar_product_traits<LHS, RHS>::Defined) \
100 : int(internal::is_same<LHS, RHS>::value)), \
101 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
102
103template<typename BinaryOp, typename Lhs, typename Rhs, typename StorageKind>
104class CwiseBinaryOpImpl;
105
106template<typename BinaryOp, typename Lhs, typename Rhs>
107class CwiseBinaryOp : internal::no_assignment_operator,
108 public CwiseBinaryOpImpl<
109 BinaryOp, Lhs, Rhs,
110 typename internal::promote_storage_type<typename internal::traits<Lhs>::StorageKind,
111 typename internal::traits<Rhs>::StorageKind>::ret>
112{
113 public:
114
115 typedef typename CwiseBinaryOpImpl<
116 BinaryOp, Lhs, Rhs,
117 typename internal::promote_storage_type<typename internal::traits<Lhs>::StorageKind,
118 typename internal::traits<Rhs>::StorageKind>::ret>::Base Base;
119 EIGEN_GENERIC_PUBLIC_INTERFACE(CwiseBinaryOp)
120
121 typedef typename internal::nested<Lhs>::type LhsNested;
122 typedef typename internal::nested<Rhs>::type RhsNested;
123 typedef typename internal::remove_reference<LhsNested>::type _LhsNested;
124 typedef typename internal::remove_reference<RhsNested>::type _RhsNested;
125
126 EIGEN_STRONG_INLINE CwiseBinaryOp(const Lhs& aLhs, const Rhs& aRhs, const BinaryOp& func = BinaryOp())
127 : m_lhs(aLhs), m_rhs(aRhs), m_functor(func)
128 {
129 EIGEN_CHECK_BINARY_COMPATIBILIY(BinaryOp,typename Lhs::Scalar,typename Rhs::Scalar);
130 // require the sizes to match
131 EIGEN_STATIC_ASSERT_SAME_MATRIX_SIZE(Lhs, Rhs)
132 eigen_assert(aLhs.rows() == aRhs.rows() && aLhs.cols() == aRhs.cols());
133 }
134
135 EIGEN_STRONG_INLINE Index rows() const {
136 // return the fixed size type if available to enable compile time optimizations
137 if (internal::traits<typename internal::remove_all<LhsNested>::type>::RowsAtCompileTime==Dynamic)
138 return m_rhs.rows();
139 else
140 return m_lhs.rows();
141 }
142 EIGEN_STRONG_INLINE Index cols() const {
143 // return the fixed size type if available to enable compile time optimizations
144 if (internal::traits<typename internal::remove_all<LhsNested>::type>::ColsAtCompileTime==Dynamic)
145 return m_rhs.cols();
146 else
147 return m_lhs.cols();
148 }
149
150 /** \returns the left hand side nested expression */
151 const _LhsNested& lhs() const { return m_lhs; }
152 /** \returns the right hand side nested expression */
153 const _RhsNested& rhs() const { return m_rhs; }
154 /** \returns the functor representing the binary operation */
155 const BinaryOp& functor() const { return m_functor; }
156
157 protected:
158 LhsNested m_lhs;
159 RhsNested m_rhs;
160 const BinaryOp m_functor;
161};
162
163template<typename BinaryOp, typename Lhs, typename Rhs>
164class CwiseBinaryOpImpl<BinaryOp, Lhs, Rhs, Dense>
165 : public internal::dense_xpr_base<CwiseBinaryOp<BinaryOp, Lhs, Rhs> >::type
166{
167 typedef CwiseBinaryOp<BinaryOp, Lhs, Rhs> Derived;
168 public:
169
170 typedef typename internal::dense_xpr_base<CwiseBinaryOp<BinaryOp, Lhs, Rhs> >::type Base;
171 EIGEN_DENSE_PUBLIC_INTERFACE( Derived )
172
173 EIGEN_STRONG_INLINE const Scalar coeff(Index rowId, Index colId) const
174 {
175 return derived().functor()(derived().lhs().coeff(rowId, colId),
176 derived().rhs().coeff(rowId, colId));
177 }
178
179 template<int LoadMode>
180 EIGEN_STRONG_INLINE PacketScalar packet(Index rowId, Index colId) const
181 {
182 return derived().functor().packetOp(derived().lhs().template packet<LoadMode>(rowId, colId),
183 derived().rhs().template packet<LoadMode>(rowId, colId));
184 }
185
186 EIGEN_STRONG_INLINE const Scalar coeff(Index index) const
187 {
188 return derived().functor()(derived().lhs().coeff(index),
189 derived().rhs().coeff(index));
190 }
191
192 template<int LoadMode>
193 EIGEN_STRONG_INLINE PacketScalar packet(Index index) const
194 {
195 return derived().functor().packetOp(derived().lhs().template packet<LoadMode>(index),
196 derived().rhs().template packet<LoadMode>(index));
197 }
198};
199
200/** replaces \c *this by \c *this - \a other.
201 *
202 * \returns a reference to \c *this
203 */
204template<typename Derived>
205template<typename OtherDerived>
206EIGEN_STRONG_INLINE Derived &
207MatrixBase<Derived>::operator-=(const MatrixBase<OtherDerived> &other)
208{
209 SelfCwiseBinaryOp<internal::scalar_difference_op<Scalar>, Derived, OtherDerived> tmp(derived());
210 tmp = other.derived();
211 return derived();
212}
213
214/** replaces \c *this by \c *this + \a other.
215 *
216 * \returns a reference to \c *this
217 */
218template<typename Derived>
219template<typename OtherDerived>
220EIGEN_STRONG_INLINE Derived &
221MatrixBase<Derived>::operator+=(const MatrixBase<OtherDerived>& other)
222{
223 SelfCwiseBinaryOp<internal::scalar_sum_op<Scalar>, Derived, OtherDerived> tmp(derived());
224 tmp = other.derived();
225 return derived();
226}
227
228} // end namespace Eigen
229
230#endif // EIGEN_CWISE_BINARY_OP_H
Note: See TracBrowser for help on using the repository browser.