1 // This file is part of Eigen, a lightweight C++ template library 2 // for linear algebra. 3 // 4 // Copyright (C) 2009-2010 Gael Guennebaud <gael.guennebaud (at) inria.fr> 5 // 6 // This Source Code Form is subject to the terms of the Mozilla 7 // Public License v. 2.0. If a copy of the MPL was not distributed 8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 9 10 #ifndef EIGEN_PRODUCTBASE_H 11 #define EIGEN_PRODUCTBASE_H 12 13 namespace Eigen { 14 15 /** \class ProductBase 16 * \ingroup Core_Module 17 * 18 */ 19 20 namespace internal { 21 template<typename Derived, typename _Lhs, typename _Rhs> 22 struct traits<ProductBase<Derived,_Lhs,_Rhs> > 23 { 24 typedef MatrixXpr XprKind; 25 typedef typename remove_all<_Lhs>::type Lhs; 26 typedef typename remove_all<_Rhs>::type Rhs; 27 typedef typename scalar_product_traits<typename Lhs::Scalar, typename Rhs::Scalar>::ReturnType Scalar; 28 typedef typename promote_storage_type<typename traits<Lhs>::StorageKind, 29 typename traits<Rhs>::StorageKind>::ret StorageKind; 30 typedef typename promote_index_type<typename traits<Lhs>::Index, 31 typename traits<Rhs>::Index>::type Index; 32 enum { 33 RowsAtCompileTime = traits<Lhs>::RowsAtCompileTime, 34 ColsAtCompileTime = traits<Rhs>::ColsAtCompileTime, 35 MaxRowsAtCompileTime = traits<Lhs>::MaxRowsAtCompileTime, 36 MaxColsAtCompileTime = traits<Rhs>::MaxColsAtCompileTime, 37 Flags = (MaxRowsAtCompileTime==1 ? RowMajorBit : 0) 38 | EvalBeforeNestingBit | EvalBeforeAssigningBit | NestByRefBit, 39 // Note that EvalBeforeNestingBit and NestByRefBit 40 // are not used in practice because nested is overloaded for products 41 CoeffReadCost = 0 // FIXME why is it needed ? 42 }; 43 }; 44 } 45 46 #define EIGEN_PRODUCT_PUBLIC_INTERFACE(Derived) \ 47 typedef ProductBase<Derived, Lhs, Rhs > Base; \ 48 EIGEN_DENSE_PUBLIC_INTERFACE(Derived) \ 49 typedef typename Base::LhsNested LhsNested; \ 50 typedef typename Base::_LhsNested _LhsNested; \ 51 typedef typename Base::LhsBlasTraits LhsBlasTraits; \ 52 typedef typename Base::ActualLhsType ActualLhsType; \ 53 typedef typename Base::_ActualLhsType _ActualLhsType; \ 54 typedef typename Base::RhsNested RhsNested; \ 55 typedef typename Base::_RhsNested _RhsNested; \ 56 typedef typename Base::RhsBlasTraits RhsBlasTraits; \ 57 typedef typename Base::ActualRhsType ActualRhsType; \ 58 typedef typename Base::_ActualRhsType _ActualRhsType; \ 59 using Base::m_lhs; \ 60 using Base::m_rhs; 61 62 template<typename Derived, typename Lhs, typename Rhs> 63 class ProductBase : public MatrixBase<Derived> 64 { 65 public: 66 typedef MatrixBase<Derived> Base; 67 EIGEN_DENSE_PUBLIC_INTERFACE(ProductBase) 68 69 typedef typename Lhs::Nested LhsNested; 70 typedef typename internal::remove_all<LhsNested>::type _LhsNested; 71 typedef internal::blas_traits<_LhsNested> LhsBlasTraits; 72 typedef typename LhsBlasTraits::DirectLinearAccessType ActualLhsType; 73 typedef typename internal::remove_all<ActualLhsType>::type _ActualLhsType; 74 typedef typename internal::traits<Lhs>::Scalar LhsScalar; 75 76 typedef typename Rhs::Nested RhsNested; 77 typedef typename internal::remove_all<RhsNested>::type _RhsNested; 78 typedef internal::blas_traits<_RhsNested> RhsBlasTraits; 79 typedef typename RhsBlasTraits::DirectLinearAccessType ActualRhsType; 80 typedef typename internal::remove_all<ActualRhsType>::type _ActualRhsType; 81 typedef typename internal::traits<Rhs>::Scalar RhsScalar; 82 83 // Diagonal of a product: no need to evaluate the arguments because they are going to be evaluated only once 84 typedef CoeffBasedProduct<LhsNested, RhsNested, 0> FullyLazyCoeffBaseProductType; 85 86 public: 87 88 typedef typename Base::PlainObject PlainObject; 89 90 ProductBase(const Lhs& lhs, const Rhs& rhs) 91 : m_lhs(lhs), m_rhs(rhs) 92 { 93 eigen_assert(lhs.cols() == rhs.rows() 94 && "invalid matrix product" 95 && "if you wanted a coeff-wise or a dot product use the respective explicit functions"); 96 } 97 98 inline Index rows() const { return m_lhs.rows(); } 99 inline Index cols() const { return m_rhs.cols(); } 100 101 template<typename Dest> 102 inline void evalTo(Dest& dst) const { dst.setZero(); scaleAndAddTo(dst,Scalar(1)); } 103 104 template<typename Dest> 105 inline void addTo(Dest& dst) const { scaleAndAddTo(dst,Scalar(1)); } 106 107 template<typename Dest> 108 inline void subTo(Dest& dst) const { scaleAndAddTo(dst,Scalar(-1)); } 109 110 template<typename Dest> 111 inline void scaleAndAddTo(Dest& dst,Scalar alpha) const { derived().scaleAndAddTo(dst,alpha); } 112 113 const _LhsNested& lhs() const { return m_lhs; } 114 const _RhsNested& rhs() const { return m_rhs; } 115 116 // Implicit conversion to the nested type (trigger the evaluation of the product) 117 operator const PlainObject& () const 118 { 119 m_result.resize(m_lhs.rows(), m_rhs.cols()); 120 derived().evalTo(m_result); 121 return m_result; 122 } 123 124 const Diagonal<const FullyLazyCoeffBaseProductType,0> diagonal() const 125 { return FullyLazyCoeffBaseProductType(m_lhs, m_rhs); } 126 127 template<int Index> 128 const Diagonal<FullyLazyCoeffBaseProductType,Index> diagonal() const 129 { return FullyLazyCoeffBaseProductType(m_lhs, m_rhs); } 130 131 const Diagonal<FullyLazyCoeffBaseProductType,Dynamic> diagonal(Index index) const 132 { return FullyLazyCoeffBaseProductType(m_lhs, m_rhs).diagonal(index); } 133 134 // restrict coeff accessors to 1x1 expressions. No need to care about mutators here since this isnt a Lvalue expression 135 typename Base::CoeffReturnType coeff(Index row, Index col) const 136 { 137 #ifdef EIGEN2_SUPPORT 138 return lhs().row(row).cwiseProduct(rhs().col(col).transpose()).sum(); 139 #else 140 EIGEN_STATIC_ASSERT_SIZE_1x1(Derived) 141 eigen_assert(this->rows() == 1 && this->cols() == 1); 142 Matrix<Scalar,1,1> result = *this; 143 return result.coeff(row,col); 144 #endif 145 } 146 147 typename Base::CoeffReturnType coeff(Index i) const 148 { 149 EIGEN_STATIC_ASSERT_SIZE_1x1(Derived) 150 eigen_assert(this->rows() == 1 && this->cols() == 1); 151 Matrix<Scalar,1,1> result = *this; 152 return result.coeff(i); 153 } 154 155 const Scalar& coeffRef(Index row, Index col) const 156 { 157 EIGEN_STATIC_ASSERT_SIZE_1x1(Derived) 158 eigen_assert(this->rows() == 1 && this->cols() == 1); 159 return derived().coeffRef(row,col); 160 } 161 162 const Scalar& coeffRef(Index i) const 163 { 164 EIGEN_STATIC_ASSERT_SIZE_1x1(Derived) 165 eigen_assert(this->rows() == 1 && this->cols() == 1); 166 return derived().coeffRef(i); 167 } 168 169 protected: 170 171 LhsNested m_lhs; 172 RhsNested m_rhs; 173 174 mutable PlainObject m_result; 175 }; 176 177 // here we need to overload the nested rule for products 178 // such that the nested type is a const reference to a plain matrix 179 namespace internal { 180 template<typename Lhs, typename Rhs, int Mode, int N, typename PlainObject> 181 struct nested<GeneralProduct<Lhs,Rhs,Mode>, N, PlainObject> 182 { 183 typedef PlainObject const& type; 184 }; 185 } 186 187 template<typename NestedProduct> 188 class ScaledProduct; 189 190 // Note that these two operator* functions are not defined as member 191 // functions of ProductBase, because, otherwise we would have to 192 // define all overloads defined in MatrixBase. Furthermore, Using 193 // "using Base::operator*" would not work with MSVC. 194 // 195 // Also note that here we accept any compatible scalar types 196 template<typename Derived,typename Lhs,typename Rhs> 197 const ScaledProduct<Derived> 198 operator*(const ProductBase<Derived,Lhs,Rhs>& prod, typename Derived::Scalar x) 199 { return ScaledProduct<Derived>(prod.derived(), x); } 200 201 template<typename Derived,typename Lhs,typename Rhs> 202 typename internal::enable_if<!internal::is_same<typename Derived::Scalar,typename Derived::RealScalar>::value, 203 const ScaledProduct<Derived> >::type 204 operator*(const ProductBase<Derived,Lhs,Rhs>& prod, typename Derived::RealScalar x) 205 { return ScaledProduct<Derived>(prod.derived(), x); } 206 207 208 template<typename Derived,typename Lhs,typename Rhs> 209 const ScaledProduct<Derived> 210 operator*(typename Derived::Scalar x,const ProductBase<Derived,Lhs,Rhs>& prod) 211 { return ScaledProduct<Derived>(prod.derived(), x); } 212 213 template<typename Derived,typename Lhs,typename Rhs> 214 typename internal::enable_if<!internal::is_same<typename Derived::Scalar,typename Derived::RealScalar>::value, 215 const ScaledProduct<Derived> >::type 216 operator*(typename Derived::RealScalar x,const ProductBase<Derived,Lhs,Rhs>& prod) 217 { return ScaledProduct<Derived>(prod.derived(), x); } 218 219 namespace internal { 220 template<typename NestedProduct> 221 struct traits<ScaledProduct<NestedProduct> > 222 : traits<ProductBase<ScaledProduct<NestedProduct>, 223 typename NestedProduct::_LhsNested, 224 typename NestedProduct::_RhsNested> > 225 { 226 typedef typename traits<NestedProduct>::StorageKind StorageKind; 227 }; 228 } 229 230 template<typename NestedProduct> 231 class ScaledProduct 232 : public ProductBase<ScaledProduct<NestedProduct>, 233 typename NestedProduct::_LhsNested, 234 typename NestedProduct::_RhsNested> 235 { 236 public: 237 typedef ProductBase<ScaledProduct<NestedProduct>, 238 typename NestedProduct::_LhsNested, 239 typename NestedProduct::_RhsNested> Base; 240 typedef typename Base::Scalar Scalar; 241 typedef typename Base::PlainObject PlainObject; 242 // EIGEN_PRODUCT_PUBLIC_INTERFACE(ScaledProduct) 243 244 ScaledProduct(const NestedProduct& prod, Scalar x) 245 : Base(prod.lhs(),prod.rhs()), m_prod(prod), m_alpha(x) {} 246 247 template<typename Dest> 248 inline void evalTo(Dest& dst) const { dst.setZero(); scaleAndAddTo(dst, Scalar(1)); } 249 250 template<typename Dest> 251 inline void addTo(Dest& dst) const { scaleAndAddTo(dst, Scalar(1)); } 252 253 template<typename Dest> 254 inline void subTo(Dest& dst) const { scaleAndAddTo(dst, Scalar(-1)); } 255 256 template<typename Dest> 257 inline void scaleAndAddTo(Dest& dst,Scalar alpha) const { m_prod.derived().scaleAndAddTo(dst,alpha * m_alpha); } 258 259 const Scalar& alpha() const { return m_alpha; } 260 261 protected: 262 const NestedProduct& m_prod; 263 Scalar m_alpha; 264 }; 265 266 /** \internal 267 * Overloaded to perform an efficient C = (A*B).lazy() */ 268 template<typename Derived> 269 template<typename ProductDerived, typename Lhs, typename Rhs> 270 Derived& MatrixBase<Derived>::lazyAssign(const ProductBase<ProductDerived, Lhs,Rhs>& other) 271 { 272 other.derived().evalTo(derived()); 273 return derived(); 274 } 275 276 } // end namespace Eigen 277 278 #endif // EIGEN_PRODUCTBASE_H 279