Main MRPT website > C++ reference
MRPT logo

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 /** \class ProductBase
00029   * \ingroup Core_Module
00030   *
00031   */
00032 
00033 namespace internal {
00034 template<typename Derived, typename _Lhs, typename _Rhs>
00035 struct traits<ProductBase<Derived,_Lhs,_Rhs> >
00036 {
00037   typedef MatrixXpr XprKind;
00038   typedef typename remove_all<_Lhs>::type Lhs;
00039   typedef typename remove_all<_Rhs>::type Rhs;
00040   typedef typename scalar_product_traits<typename Lhs::Scalar, typename Rhs::Scalar>::ReturnType Scalar;
00041   typedef typename promote_storage_type<typename traits<Lhs>::StorageKind,
00042                                            typename traits<Rhs>::StorageKind>::ret StorageKind;
00043   typedef typename promote_index_type<typename traits<Lhs>::Index,
00044                                          typename traits<Rhs>::Index>::type Index;
00045   enum {
00046     RowsAtCompileTime = traits<Lhs>::RowsAtCompileTime,
00047     ColsAtCompileTime = traits<Rhs>::ColsAtCompileTime,
00048     MaxRowsAtCompileTime = traits<Lhs>::MaxRowsAtCompileTime,
00049     MaxColsAtCompileTime = traits<Rhs>::MaxColsAtCompileTime,
00050     Flags = (MaxRowsAtCompileTime==1 ? RowMajorBit : 0)
00051           | EvalBeforeNestingBit | EvalBeforeAssigningBit | NestByRefBit,
00052                   // Note that EvalBeforeNestingBit and NestByRefBit
00053                   // are not used in practice because nested is overloaded for products
00054     CoeffReadCost = 0 // FIXME why is it needed ?
00055   };
00056 };
00057 }
00058 
00059 #define EIGEN_PRODUCT_PUBLIC_INTERFACE(Derived) \
00060   typedef ProductBase<Derived, Lhs, Rhs > Base; \
00061   EIGEN_DENSE_PUBLIC_INTERFACE(Derived) \
00062   typedef typename Base::LhsNested LhsNested; \
00063   typedef typename Base::_LhsNested _LhsNested; \
00064   typedef typename Base::LhsBlasTraits LhsBlasTraits; \
00065   typedef typename Base::ActualLhsType ActualLhsType; \
00066   typedef typename Base::_ActualLhsType _ActualLhsType; \
00067   typedef typename Base::RhsNested RhsNested; \
00068   typedef typename Base::_RhsNested _RhsNested; \
00069   typedef typename Base::RhsBlasTraits RhsBlasTraits; \
00070   typedef typename Base::ActualRhsType ActualRhsType; \
00071   typedef typename Base::_ActualRhsType _ActualRhsType; \
00072   using Base::m_lhs; \
00073   using Base::m_rhs;
00074 
00075 template<typename Derived, typename Lhs, typename Rhs>
00076 class ProductBase : public MatrixBase<Derived>
00077 {
00078   public:
00079     typedef MatrixBase<Derived> Base;
00080     EIGEN_DENSE_PUBLIC_INTERFACE(ProductBase)
00081     
00082     typedef typename Lhs::Nested LhsNested;
00083     typedef typename internal::remove_all<LhsNested>::type _LhsNested;
00084     typedef internal::blas_traits<_LhsNested> LhsBlasTraits;
00085     typedef typename LhsBlasTraits::DirectLinearAccessType ActualLhsType;
00086     typedef typename internal::remove_all<ActualLhsType>::type _ActualLhsType;
00087 
00088     typedef typename Rhs::Nested RhsNested;
00089     typedef typename internal::remove_all<RhsNested>::type _RhsNested;
00090     typedef internal::blas_traits<_RhsNested> RhsBlasTraits;
00091     typedef typename RhsBlasTraits::DirectLinearAccessType ActualRhsType;
00092     typedef typename internal::remove_all<ActualRhsType>::type _ActualRhsType;
00093 
00094     // Diagonal of a product: no need to evaluate the arguments because they are going to be evaluated only once
00095     typedef CoeffBasedProduct<LhsNested, RhsNested, 0> FullyLazyCoeffBaseProductType;
00096 
00097   public:
00098 
00099     typedef typename Base::PlainObject PlainObject;
00100 
00101     ProductBase(const Lhs& lhs, const Rhs& rhs)
00102       : m_lhs(lhs), m_rhs(rhs)
00103     {
00104       eigen_assert(lhs.cols() == rhs.rows()
00105         && "invalid matrix product"
00106         && "if you wanted a coeff-wise or a dot product use the respective explicit functions");
00107     }
00108 
00109     inline Index rows() const { return m_lhs.rows(); }
00110     inline Index cols() const { return m_rhs.cols(); }
00111 
00112     template<typename Dest>
00113     inline void evalTo(Dest& dst) const { dst.setZero(); scaleAndAddTo(dst,Scalar(1)); }
00114 
00115     template<typename Dest>
00116     inline void addTo(Dest& dst) const { scaleAndAddTo(dst,1); }
00117 
00118     template<typename Dest>
00119     inline void subTo(Dest& dst) const { scaleAndAddTo(dst,-1); }
00120 
00121     template<typename Dest>
00122     inline void scaleAndAddTo(Dest& dst,Scalar alpha) const { derived().scaleAndAddTo(dst,alpha); }
00123 
00124     const _LhsNested& lhs() const { return m_lhs; }
00125     const _RhsNested& rhs() const { return m_rhs; }
00126 
00127     // Implicit conversion to the nested type (trigger the evaluation of the product)
00128     operator const PlainObject& () const
00129     {
00130       m_result.resize(m_lhs.rows(), m_rhs.cols());
00131       derived().evalTo(m_result);
00132       return m_result;
00133     }
00134 
00135     const Diagonal<FullyLazyCoeffBaseProductType,0> diagonal() const
00136     { return FullyLazyCoeffBaseProductType(m_lhs, m_rhs); }
00137 
00138     template<int Index>
00139     const Diagonal<FullyLazyCoeffBaseProductType,Index> diagonal() const
00140     { return FullyLazyCoeffBaseProductType(m_lhs, m_rhs); }
00141 
00142     const Diagonal<FullyLazyCoeffBaseProductType,Dynamic> diagonal(Index index) const
00143     { return FullyLazyCoeffBaseProductType(m_lhs, m_rhs).diagonal(index); }
00144 
00145     // restrict coeff accessors to 1x1 expressions. No need to care about mutators here since this isnt a Lvalue expression
00146     typename Base::CoeffReturnType coeff(Index row, Index col) const
00147     {
00148       EIGEN_STATIC_ASSERT_SIZE_1x1(Derived)
00149       eigen_assert(this->rows() == 1 && this->cols() == 1);
00150       return derived().coeff(row,col);
00151     }
00152 
00153     typename Base::CoeffReturnType coeff(Index i) const
00154     {
00155       EIGEN_STATIC_ASSERT_SIZE_1x1(Derived)
00156       eigen_assert(this->rows() == 1 && this->cols() == 1);
00157       return derived().coeff(i);
00158     }
00159 
00160     const Scalar& coeffRef(Index row, Index col) const
00161     {
00162       EIGEN_STATIC_ASSERT_SIZE_1x1(Derived)
00163       eigen_assert(this->rows() == 1 && this->cols() == 1);
00164       return derived().coeffRef(row,col);
00165     }
00166 
00167     const Scalar& coeffRef(Index i) const
00168     {
00169       EIGEN_STATIC_ASSERT_SIZE_1x1(Derived)
00170       eigen_assert(this->rows() == 1 && this->cols() == 1);
00171       return derived().coeffRef(i);
00172     }
00173 
00174   protected:
00175 
00176     const LhsNested m_lhs;
00177     const RhsNested m_rhs;
00178 
00179     mutable PlainObject m_result;
00180 };
00181 
00182 // here we need to overload the nested rule for products
00183 // such that the nested type is a const reference to a plain matrix
00184 namespace internal {
00185 template<typename Lhs, typename Rhs, int Mode, int N, typename PlainObject>
00186 struct nested<GeneralProduct<Lhs,Rhs,Mode>, N, PlainObject>
00187 {
00188   typedef PlainObject const& type;
00189 };
00190 }
00191 
00192 template<typename NestedProduct>
00193 class ScaledProduct;
00194 
00195 // Note that these two operator* functions are not defined as member
00196 // functions of ProductBase, because, otherwise we would have to
00197 // define all overloads defined in MatrixBase. Furthermore, Using
00198 // "using Base::operator*" would not work with MSVC.
00199 //
00200 // Also note that here we accept any compatible scalar types
00201 template<typename Derived,typename Lhs,typename Rhs>
00202 const ScaledProduct<Derived>
00203 operator*(const ProductBase<Derived,Lhs,Rhs>& prod, typename Derived::Scalar x)
00204 { return ScaledProduct<Derived>(prod.derived(), x); }
00205 
00206 template<typename Derived,typename Lhs,typename Rhs>
00207 typename internal::enable_if<!internal::is_same<typename Derived::Scalar,typename Derived::RealScalar>::value,
00208                       const ScaledProduct<Derived> >::type
00209 operator*(const ProductBase<Derived,Lhs,Rhs>& prod, typename Derived::RealScalar x)
00210 { return ScaledProduct<Derived>(prod.derived(), x); }
00211 
00212 
00213 template<typename Derived,typename Lhs,typename Rhs>
00214 const ScaledProduct<Derived>
00215 operator*(typename Derived::Scalar x,const ProductBase<Derived,Lhs,Rhs>& prod)
00216 { return ScaledProduct<Derived>(prod.derived(), x); }
00217 
00218 template<typename Derived,typename Lhs,typename Rhs>
00219 typename internal::enable_if<!internal::is_same<typename Derived::Scalar,typename Derived::RealScalar>::value,
00220                       const ScaledProduct<Derived> >::type
00221 operator*(typename Derived::RealScalar x,const ProductBase<Derived,Lhs,Rhs>& prod)
00222 { return ScaledProduct<Derived>(prod.derived(), x); }
00223 
00224 namespace internal {
00225 template<typename NestedProduct>
00226 struct traits<ScaledProduct<NestedProduct> >
00227  : traits<ProductBase<ScaledProduct<NestedProduct>,
00228                          typename NestedProduct::_LhsNested,
00229                          typename NestedProduct::_RhsNested> >
00230 {
00231   typedef typename traits<NestedProduct>::StorageKind StorageKind;
00232 };
00233 }
00234 
00235 template<typename NestedProduct>
00236 class ScaledProduct
00237   : public ProductBase<ScaledProduct<NestedProduct>,
00238                        typename NestedProduct::_LhsNested,
00239                        typename NestedProduct::_RhsNested>
00240 {
00241   public:
00242     typedef ProductBase<ScaledProduct<NestedProduct>,
00243                        typename NestedProduct::_LhsNested,
00244                        typename NestedProduct::_RhsNested> Base;
00245     typedef typename Base::Scalar Scalar;
00246     typedef typename Base::PlainObject PlainObject;
00247 //     EIGEN_PRODUCT_PUBLIC_INTERFACE(ScaledProduct)
00248 
00249     ScaledProduct(const NestedProduct& prod, Scalar x)
00250     : Base(prod.lhs(),prod.rhs()), m_prod(prod), m_alpha(x) {}
00251 
00252     template<typename Dest>
00253     inline void evalTo(Dest& dst) const { dst.setZero(); scaleAndAddTo(dst,m_alpha); }
00254 
00255     template<typename Dest>
00256     inline void addTo(Dest& dst) const { scaleAndAddTo(dst,m_alpha); }
00257 
00258     template<typename Dest>
00259     inline void subTo(Dest& dst) const { scaleAndAddTo(dst,-m_alpha); }
00260 
00261     template<typename Dest>
00262     inline void scaleAndAddTo(Dest& dst,Scalar alpha) const { m_prod.derived().scaleAndAddTo(dst,alpha); }
00263 
00264     const Scalar& alpha() const { return m_alpha; }
00265     
00266   protected:
00267     const NestedProduct& m_prod;
00268     Scalar m_alpha;
00269 };
00270 
00271 /** \internal
00272   * Overloaded to perform an efficient C = (A*B).lazy() */
00273 template<typename Derived>
00274 template<typename ProductDerived, typename Lhs, typename Rhs>
00275 Derived& MatrixBase<Derived>::lazyAssign(const ProductBase<ProductDerived, Lhs,Rhs>& other)
00276 {
00277   other.derived().evalTo(derived());
00278   return derived();
00279 }
00280 
00281 
00282 #endif // EIGEN_PRODUCTBASE_H



Page generated by Doxygen 1.7.3 for MRPT 0.9.4 SVN:exported at Tue Jan 25 21:56:31 UTC 2011