MOAB
4.9.3pre
|
00001 // This file is part of Eigen, a lightweight C++ template library 00002 // for linear algebra. 00003 // 00004 // Copyright (C) 2008-2014 Gael Guennebaud <[email protected]> 00005 // Copyright (C) 2006-2008 Benoit Jacob <[email protected]> 00006 // 00007 // This Source Code Form is subject to the terms of the Mozilla 00008 // Public License v. 2.0. If a copy of the MPL was not distributed 00009 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 00010 00011 #ifndef EIGEN_CWISE_BINARY_OP_H 00012 #define EIGEN_CWISE_BINARY_OP_H 00013 00014 namespace Eigen { 00015 00016 namespace internal { 00017 template<typename BinaryOp, typename Lhs, typename Rhs> 00018 struct traits<CwiseBinaryOp<BinaryOp, Lhs, Rhs> > 00019 { 00020 // we must not inherit from traits<Lhs> since it has 00021 // the potential to cause problems with MSVC 00022 typedef typename remove_all<Lhs>::type Ancestor; 00023 typedef typename traits<Ancestor>::XprKind XprKind; 00024 enum { 00025 RowsAtCompileTime = traits<Ancestor>::RowsAtCompileTime, 00026 ColsAtCompileTime = traits<Ancestor>::ColsAtCompileTime, 00027 MaxRowsAtCompileTime = traits<Ancestor>::MaxRowsAtCompileTime, 00028 MaxColsAtCompileTime = traits<Ancestor>::MaxColsAtCompileTime 00029 }; 00030 00031 // even though we require Lhs and Rhs to have the same scalar type (see CwiseBinaryOp constructor), 00032 // we still want to handle the case when the result type is different. 00033 typedef typename result_of< 00034 BinaryOp( 00035 const typename Lhs::Scalar&, 00036 const typename Rhs::Scalar& 00037 ) 00038 >::type Scalar; 00039 typedef typename cwise_promote_storage_type<typename traits<Lhs>::StorageKind, 00040 typename traits<Rhs>::StorageKind, 00041 BinaryOp>::ret StorageKind; 00042 typedef typename promote_index_type<typename traits<Lhs>::StorageIndex, 00043 typename traits<Rhs>::StorageIndex>::type StorageIndex; 00044 typedef typename Lhs::Nested LhsNested; 00045 typedef typename Rhs::Nested RhsNested; 00046 typedef typename remove_reference<LhsNested>::type _LhsNested; 00047 typedef typename remove_reference<RhsNested>::type _RhsNested; 00048 enum { 00049 Flags = _LhsNested::Flags & RowMajorBit 00050 }; 00051 }; 00052 } // end namespace internal 00053 00054 template<typename BinaryOp, typename Lhs, typename Rhs, typename StorageKind> 00055 class CwiseBinaryOpImpl; 00056 00076 template<typename BinaryOp, typename LhsType, typename RhsType> 00077 class CwiseBinaryOp : 00078 public CwiseBinaryOpImpl< 00079 BinaryOp, LhsType, RhsType, 00080 typename internal::cwise_promote_storage_type<typename internal::traits<LhsType>::StorageKind, 00081 typename internal::traits<RhsType>::StorageKind, 00082 BinaryOp>::ret>, 00083 internal::no_assignment_operator 00084 { 00085 public: 00086 00087 typedef typename internal::remove_all<LhsType>::type Lhs; 00088 typedef typename internal::remove_all<RhsType>::type Rhs; 00089 00090 typedef typename CwiseBinaryOpImpl< 00091 BinaryOp, LhsType, RhsType, 00092 typename internal::cwise_promote_storage_type<typename internal::traits<LhsType>::StorageKind, 00093 typename internal::traits<Rhs>::StorageKind, 00094 BinaryOp>::ret>::Base Base; 00095 EIGEN_GENERIC_PUBLIC_INTERFACE(CwiseBinaryOp) 00096 00097 typedef typename internal::ref_selector<LhsType>::type LhsNested; 00098 typedef typename internal::ref_selector<RhsType>::type RhsNested; 00099 typedef typename internal::remove_reference<LhsNested>::type _LhsNested; 00100 typedef typename internal::remove_reference<RhsNested>::type _RhsNested; 00101 00102 EIGEN_DEVICE_FUNC 00103 EIGEN_STRONG_INLINE CwiseBinaryOp(const Lhs& aLhs, const Rhs& aRhs, const BinaryOp& func = BinaryOp()) 00104 : m_lhs(aLhs), m_rhs(aRhs), m_functor(func) 00105 { 00106 EIGEN_CHECK_BINARY_COMPATIBILIY(BinaryOp,typename Lhs::Scalar,typename Rhs::Scalar); 00107 // require the sizes to match 00108 EIGEN_STATIC_ASSERT_SAME_MATRIX_SIZE(Lhs, Rhs) 00109 eigen_assert(aLhs.rows() == aRhs.rows() && aLhs.cols() == aRhs.cols()); 00110 } 00111 00112 EIGEN_DEVICE_FUNC 00113 EIGEN_STRONG_INLINE Index rows() const { 00114 // return the fixed size type if available to enable compile time optimizations 00115 if (internal::traits<typename internal::remove_all<LhsNested>::type>::RowsAtCompileTime==Dynamic) 00116 return m_rhs.rows(); 00117 else 00118 return m_lhs.rows(); 00119 } 00120 EIGEN_DEVICE_FUNC 00121 EIGEN_STRONG_INLINE Index cols() const { 00122 // return the fixed size type if available to enable compile time optimizations 00123 if (internal::traits<typename internal::remove_all<LhsNested>::type>::ColsAtCompileTime==Dynamic) 00124 return m_rhs.cols(); 00125 else 00126 return m_lhs.cols(); 00127 } 00128 00130 EIGEN_DEVICE_FUNC 00131 const _LhsNested& lhs() const { return m_lhs; } 00133 EIGEN_DEVICE_FUNC 00134 const _RhsNested& rhs() const { return m_rhs; } 00136 EIGEN_DEVICE_FUNC 00137 const BinaryOp& functor() const { return m_functor; } 00138 00139 protected: 00140 LhsNested m_lhs; 00141 RhsNested m_rhs; 00142 const BinaryOp m_functor; 00143 }; 00144 00145 // Generic API dispatcher 00146 template<typename BinaryOp, typename Lhs, typename Rhs, typename StorageKind> 00147 class CwiseBinaryOpImpl 00148 : public internal::generic_xpr_base<CwiseBinaryOp<BinaryOp, Lhs, Rhs> >::type 00149 { 00150 public: 00151 typedef typename internal::generic_xpr_base<CwiseBinaryOp<BinaryOp, Lhs, Rhs> >::type Base; 00152 }; 00153 00158 template<typename Derived> 00159 template<typename OtherDerived> 00160 EIGEN_STRONG_INLINE Derived & 00161 MatrixBase<Derived>::operator-=(const MatrixBase<OtherDerived> &other) 00162 { 00163 call_assignment(derived(), other.derived(), internal::sub_assign_op<Scalar>()); 00164 return derived(); 00165 } 00166 00171 template<typename Derived> 00172 template<typename OtherDerived> 00173 EIGEN_STRONG_INLINE Derived & 00174 MatrixBase<Derived>::operator+=(const MatrixBase<OtherDerived>& other) 00175 { 00176 call_assignment(derived(), other.derived(), internal::add_assign_op<Scalar>()); 00177 return derived(); 00178 } 00179 00180 } // end namespace Eigen 00181 00182 #endif // EIGEN_CWISE_BINARY_OP_H 00183