ProductBase.h
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2009-2010 Gael Guennebaud <gael.guennebaud@inria.fr>
00005 //
00006 // Eigen is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 3 of the License, or (at your option) any later version.
00010 //
00011 // Alternatively, you can redistribute it and/or
00012 // modify it under the terms of the GNU General Public License as
00013 // published by the Free Software Foundation; either version 2 of
00014 // the License, or (at your option) any later version.
00015 //
00016 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00017 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00018 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00019 // GNU General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License and a copy of the GNU General Public License along with
00023 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00024 
00025 #ifndef EIGEN_PRODUCTBASE_H
00026 #define EIGEN_PRODUCTBASE_H
00027 
00028 namespace Eigen { 
00029 
00035 namespace internal {
00036 template<typename Derived, typename _Lhs, typename _Rhs>
00037 struct traits<ProductBase<Derived,_Lhs,_Rhs> >
00038 {
00039   typedef MatrixXpr XprKind;
00040   typedef typename remove_all<_Lhs>::type Lhs;
00041   typedef typename remove_all<_Rhs>::type Rhs;
00042   typedef typename scalar_product_traits<typename Lhs::Scalar, typename Rhs::Scalar>::ReturnType Scalar;
00043   typedef typename promote_storage_type<typename traits<Lhs>::StorageKind,
00044                                            typename traits<Rhs>::StorageKind>::ret StorageKind;
00045   typedef typename promote_index_type<typename traits<Lhs>::Index,
00046                                          typename traits<Rhs>::Index>::type Index;
00047   enum {
00048     RowsAtCompileTime = traits<Lhs>::RowsAtCompileTime,
00049     ColsAtCompileTime = traits<Rhs>::ColsAtCompileTime,
00050     MaxRowsAtCompileTime = traits<Lhs>::MaxRowsAtCompileTime,
00051     MaxColsAtCompileTime = traits<Rhs>::MaxColsAtCompileTime,
00052     Flags = (MaxRowsAtCompileTime==1 ? RowMajorBit : 0)
00053           | EvalBeforeNestingBit | EvalBeforeAssigningBit | NestByRefBit,
00054                   // Note that EvalBeforeNestingBit and NestByRefBit
00055                   // are not used in practice because nested is overloaded for products
00056     CoeffReadCost = 0 // FIXME why is it needed ?
00057   };
00058 };
00059 }
00060 
00061 #define EIGEN_PRODUCT_PUBLIC_INTERFACE(Derived) \
00062   typedef ProductBase<Derived, Lhs, Rhs > Base; \
00063   EIGEN_DENSE_PUBLIC_INTERFACE(Derived) \
00064   typedef typename Base::LhsNested LhsNested; \
00065   typedef typename Base::_LhsNested _LhsNested; \
00066   typedef typename Base::LhsBlasTraits LhsBlasTraits; \
00067   typedef typename Base::ActualLhsType ActualLhsType; \
00068   typedef typename Base::_ActualLhsType _ActualLhsType; \
00069   typedef typename Base::RhsNested RhsNested; \
00070   typedef typename Base::_RhsNested _RhsNested; \
00071   typedef typename Base::RhsBlasTraits RhsBlasTraits; \
00072   typedef typename Base::ActualRhsType ActualRhsType; \
00073   typedef typename Base::_ActualRhsType _ActualRhsType; \
00074   using Base::m_lhs; \
00075   using Base::m_rhs;
00076 
00077 template<typename Derived, typename Lhs, typename Rhs>
00078 class ProductBase : public MatrixBase<Derived>
00079 {
00080   public:
00081     typedef MatrixBase<Derived> Base;
00082     EIGEN_DENSE_PUBLIC_INTERFACE(ProductBase)
00083     
00084     typedef typename Lhs::Nested LhsNested;
00085     typedef typename internal::remove_all<LhsNested>::type _LhsNested;
00086     typedef internal::blas_traits<_LhsNested> LhsBlasTraits;
00087     typedef typename LhsBlasTraits::DirectLinearAccessType ActualLhsType;
00088     typedef typename internal::remove_all<ActualLhsType>::type _ActualLhsType;
00089     typedef typename internal::traits<Lhs>::Scalar LhsScalar;
00090 
00091     typedef typename Rhs::Nested RhsNested;
00092     typedef typename internal::remove_all<RhsNested>::type _RhsNested;
00093     typedef internal::blas_traits<_RhsNested> RhsBlasTraits;
00094     typedef typename RhsBlasTraits::DirectLinearAccessType ActualRhsType;
00095     typedef typename internal::remove_all<ActualRhsType>::type _ActualRhsType;
00096     typedef typename internal::traits<Rhs>::Scalar RhsScalar;
00097 
00098     // Diagonal of a product: no need to evaluate the arguments because they are going to be evaluated only once
00099     typedef CoeffBasedProduct<LhsNested, RhsNested, 0> FullyLazyCoeffBaseProductType;
00100 
00101   public:
00102 
00103     typedef typename Base::PlainObject PlainObject;
00104 
00105     ProductBase(const Lhs& lhs, const Rhs& rhs)
00106       : m_lhs(lhs), m_rhs(rhs)
00107     {
00108       eigen_assert(lhs.cols() == rhs.rows()
00109         && "invalid matrix product"
00110         && "if you wanted a coeff-wise or a dot product use the respective explicit functions");
00111     }
00112 
00113     inline Index rows() const { return m_lhs.rows(); }
00114     inline Index cols() const { return m_rhs.cols(); }
00115 
00116     template<typename Dest>
00117     inline void evalTo(Dest& dst) const { dst.setZero(); scaleAndAddTo(dst,Scalar(1)); }
00118 
00119     template<typename Dest>
00120     inline void addTo(Dest& dst) const { scaleAndAddTo(dst,Scalar(1)); }
00121 
00122     template<typename Dest>
00123     inline void subTo(Dest& dst) const { scaleAndAddTo(dst,Scalar(-1)); }
00124 
00125     template<typename Dest>
00126     inline void scaleAndAddTo(Dest& dst,Scalar alpha) const { derived().scaleAndAddTo(dst,alpha); }
00127 
00128     const _LhsNested& lhs() const { return m_lhs; }
00129     const _RhsNested& rhs() const { return m_rhs; }
00130 
00131     // Implicit conversion to the nested type (trigger the evaluation of the product)
00132     operator const PlainObject& () const
00133     {
00134       m_result.resize(m_lhs.rows(), m_rhs.cols());
00135       derived().evalTo(m_result);
00136       return m_result;
00137     }
00138 
00139     const Diagonal<const FullyLazyCoeffBaseProductType,0> diagonal() const
00140     { return FullyLazyCoeffBaseProductType(m_lhs, m_rhs); }
00141 
00142     template<int Index>
00143     const Diagonal<FullyLazyCoeffBaseProductType,Index> diagonal() const
00144     { return FullyLazyCoeffBaseProductType(m_lhs, m_rhs); }
00145 
00146     const Diagonal<FullyLazyCoeffBaseProductType,Dynamic> diagonal(Index index) const
00147     { return FullyLazyCoeffBaseProductType(m_lhs, m_rhs).diagonal(index); }
00148 
00149     // restrict coeff accessors to 1x1 expressions. No need to care about mutators here since this isnt a Lvalue expression
00150     typename Base::CoeffReturnType coeff(Index row, Index col) const
00151     {
00152 #ifdef EIGEN2_SUPPORT
00153       return lhs().row(row).cwiseProduct(rhs().col(col).transpose()).sum();
00154 #else
00155       EIGEN_STATIC_ASSERT_SIZE_1x1(Derived)
00156       eigen_assert(this->rows() == 1 && this->cols() == 1);
00157       Matrix<Scalar,1,1> result = *this;
00158       return result.coeff(row,col);
00159 #endif
00160     }
00161 
00162     typename Base::CoeffReturnType coeff(Index i) const
00163     {
00164       EIGEN_STATIC_ASSERT_SIZE_1x1(Derived)
00165       eigen_assert(this->rows() == 1 && this->cols() == 1);
00166       Matrix<Scalar,1,1> result = *this;
00167       return result.coeff(i);
00168     }
00169 
00170     const Scalar& coeffRef(Index row, Index col) const
00171     {
00172       EIGEN_STATIC_ASSERT_SIZE_1x1(Derived)
00173       eigen_assert(this->rows() == 1 && this->cols() == 1);
00174       return derived().coeffRef(row,col);
00175     }
00176 
00177     const Scalar& coeffRef(Index i) const
00178     {
00179       EIGEN_STATIC_ASSERT_SIZE_1x1(Derived)
00180       eigen_assert(this->rows() == 1 && this->cols() == 1);
00181       return derived().coeffRef(i);
00182     }
00183 
00184   protected:
00185 
00186     LhsNested m_lhs;
00187     RhsNested m_rhs;
00188 
00189     mutable PlainObject m_result;
00190 };
00191 
00192 // here we need to overload the nested rule for products
00193 // such that the nested type is a const reference to a plain matrix
00194 namespace internal {
00195 template<typename Lhs, typename Rhs, int Mode, int N, typename PlainObject>
00196 struct nested<GeneralProduct<Lhs,Rhs,Mode>, N, PlainObject>
00197 {
00198   typedef PlainObject const& type;
00199 };
00200 }
00201 
00202 template<typename NestedProduct>
00203 class ScaledProduct;
00204 
00205 // Note that these two operator* functions are not defined as member
00206 // functions of ProductBase, because, otherwise we would have to
00207 // define all overloads defined in MatrixBase. Furthermore, Using
00208 // "using Base::operator*" would not work with MSVC.
00209 //
00210 // Also note that here we accept any compatible scalar types
00211 template<typename Derived,typename Lhs,typename Rhs>
00212 const ScaledProduct<Derived>
00213 operator*(const ProductBase<Derived,Lhs,Rhs>& prod, typename Derived::Scalar x)
00214 { return ScaledProduct<Derived>(prod.derived(), x); }
00215 
00216 template<typename Derived,typename Lhs,typename Rhs>
00217 typename internal::enable_if<!internal::is_same<typename Derived::Scalar,typename Derived::RealScalar>::value,
00218                       const ScaledProduct<Derived> >::type
00219 operator*(const ProductBase<Derived,Lhs,Rhs>& prod, typename Derived::RealScalar x)
00220 { return ScaledProduct<Derived>(prod.derived(), x); }
00221 
00222 
00223 template<typename Derived,typename Lhs,typename Rhs>
00224 const ScaledProduct<Derived>
00225 operator*(typename Derived::Scalar x,const ProductBase<Derived,Lhs,Rhs>& prod)
00226 { return ScaledProduct<Derived>(prod.derived(), x); }
00227 
00228 template<typename Derived,typename Lhs,typename Rhs>
00229 typename internal::enable_if<!internal::is_same<typename Derived::Scalar,typename Derived::RealScalar>::value,
00230                       const ScaledProduct<Derived> >::type
00231 operator*(typename Derived::RealScalar x,const ProductBase<Derived,Lhs,Rhs>& prod)
00232 { return ScaledProduct<Derived>(prod.derived(), x); }
00233 
00234 namespace internal {
00235 template<typename NestedProduct>
00236 struct traits<ScaledProduct<NestedProduct> >
00237  : traits<ProductBase<ScaledProduct<NestedProduct>,
00238                          typename NestedProduct::_LhsNested,
00239                          typename NestedProduct::_RhsNested> >
00240 {
00241   typedef typename traits<NestedProduct>::StorageKind StorageKind;
00242 };
00243 }
00244 
00245 template<typename NestedProduct>
00246 class ScaledProduct
00247   : public ProductBase<ScaledProduct<NestedProduct>,
00248                        typename NestedProduct::_LhsNested,
00249                        typename NestedProduct::_RhsNested>
00250 {
00251   public:
00252     typedef ProductBase<ScaledProduct<NestedProduct>,
00253                        typename NestedProduct::_LhsNested,
00254                        typename NestedProduct::_RhsNested> Base;
00255     typedef typename Base::Scalar Scalar;
00256     typedef typename Base::PlainObject PlainObject;
00257 //     EIGEN_PRODUCT_PUBLIC_INTERFACE(ScaledProduct)
00258 
00259     ScaledProduct(const NestedProduct& prod, Scalar x)
00260     : Base(prod.lhs(),prod.rhs()), m_prod(prod), m_alpha(x) {}
00261 
00262     template<typename Dest>
00263     inline void evalTo(Dest& dst) const { dst.setZero(); scaleAndAddTo(dst, Scalar(1)); }
00264 
00265     template<typename Dest>
00266     inline void addTo(Dest& dst) const { scaleAndAddTo(dst, Scalar(1)); }
00267 
00268     template<typename Dest>
00269     inline void subTo(Dest& dst) const { scaleAndAddTo(dst, Scalar(-1)); }
00270 
00271     template<typename Dest>
00272     inline void scaleAndAddTo(Dest& dst,Scalar alpha) const { m_prod.derived().scaleAndAddTo(dst,alpha * m_alpha); }
00273 
00274     const Scalar& alpha() const { return m_alpha; }
00275     
00276   protected:
00277     const NestedProduct& m_prod;
00278     Scalar m_alpha;
00279 };
00280 
00283 template<typename Derived>
00284 template<typename ProductDerived, typename Lhs, typename Rhs>
00285 Derived& MatrixBase<Derived>::lazyAssign(const ProductBase<ProductDerived, Lhs,Rhs>& other)
00286 {
00287   other.derived().evalTo(derived());
00288   return derived();
00289 }
00290 
00291 } // end namespace Eigen
00292 
00293 #endif // EIGEN_PRODUCTBASE_H