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