Main MRPT website > C++ reference
MRPT logo

NoAlias.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 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_NOALIAS_H
00026 #define EIGEN_NOALIAS_H
00027 
00028 /** \class NoAlias
00029   * \ingroup Core_Module
00030   *
00031   * \brief Pseudo expression providing an operator = assuming no aliasing
00032   *
00033   * \param ExpressionType the type of the object on which to do the lazy assignment
00034   *
00035   * This class represents an expression with special assignment operators
00036   * assuming no aliasing between the target expression and the source expression.
00037   * More precisely it alloas to bypass the EvalBeforeAssignBit flag of the source expression.
00038   * It is the return type of MatrixBase::noalias()
00039   * and most of the time this is the only way it is used.
00040   *
00041   * \sa MatrixBase::noalias()
00042   */
00043 template<typename ExpressionType, template <typename> class StorageBase>
00044 class NoAlias
00045 {
00046     typedef typename ExpressionType::Scalar Scalar;
00047   public:
00048     NoAlias(ExpressionType& expression) : m_expression(expression) {}
00049 
00050     /** Behaves like MatrixBase::lazyAssign(other)
00051       * \sa MatrixBase::lazyAssign() */
00052     template<typename OtherDerived>
00053     EIGEN_STRONG_INLINE ExpressionType& operator=(const StorageBase<OtherDerived>& other)
00054     { return internal::assign_selector<ExpressionType,OtherDerived,false>::run(m_expression,other.derived()); }
00055 
00056     /** \sa MatrixBase::operator+= */
00057     template<typename OtherDerived>
00058     EIGEN_STRONG_INLINE ExpressionType& operator+=(const StorageBase<OtherDerived>& other)
00059     {
00060       typedef SelfCwiseBinaryOp<internal::scalar_sum_op<Scalar>, ExpressionType, OtherDerived> SelfAdder;
00061       SelfAdder tmp(m_expression);
00062       typedef typename internal::nested<OtherDerived>::type OtherDerivedNested;
00063       typedef typename internal::remove_all<OtherDerivedNested>::type _OtherDerivedNested;
00064       internal::assign_selector<SelfAdder,_OtherDerivedNested,false>::run(tmp,OtherDerivedNested(other.derived()));
00065       return m_expression;
00066     }
00067 
00068     /** \sa MatrixBase::operator-= */
00069     template<typename OtherDerived>
00070     EIGEN_STRONG_INLINE ExpressionType& operator-=(const StorageBase<OtherDerived>& other)
00071     {
00072       typedef SelfCwiseBinaryOp<internal::scalar_difference_op<Scalar>, ExpressionType, OtherDerived> SelfAdder;
00073       SelfAdder tmp(m_expression);
00074       typedef typename internal::nested<OtherDerived>::type OtherDerivedNested;
00075       typedef typename internal::remove_all<OtherDerivedNested>::type _OtherDerivedNested;
00076       internal::assign_selector<SelfAdder,_OtherDerivedNested,false>::run(tmp,OtherDerivedNested(other.derived()));
00077       return m_expression;
00078     }
00079 
00080 #ifndef EIGEN_PARSED_BY_DOXYGEN
00081     template<typename ProductDerived, typename Lhs, typename Rhs>
00082     EIGEN_STRONG_INLINE ExpressionType& operator+=(const ProductBase<ProductDerived, Lhs,Rhs>& other)
00083     { other.derived().addTo(m_expression); return m_expression; }
00084 
00085     template<typename ProductDerived, typename Lhs, typename Rhs>
00086     EIGEN_STRONG_INLINE ExpressionType& operator-=(const ProductBase<ProductDerived, Lhs,Rhs>& other)
00087     { other.derived().subTo(m_expression); return m_expression; }
00088 
00089     template<typename Lhs, typename Rhs, int NestingFlags>
00090     EIGEN_STRONG_INLINE ExpressionType& operator+=(const CoeffBasedProduct<Lhs,Rhs,NestingFlags>& other)
00091     { return m_expression.derived() += CoeffBasedProduct<Lhs,Rhs,NestByRefBit>(other.lhs(), other.rhs()); }
00092 
00093     template<typename Lhs, typename Rhs, int NestingFlags>
00094     EIGEN_STRONG_INLINE ExpressionType& operator-=(const CoeffBasedProduct<Lhs,Rhs,NestingFlags>& other)
00095     { return m_expression.derived() -= CoeffBasedProduct<Lhs,Rhs,NestByRefBit>(other.lhs(), other.rhs()); }
00096 #endif
00097 
00098   protected:
00099     ExpressionType& m_expression;
00100 };
00101 
00102 /** \returns a pseudo expression of \c *this with an operator= assuming
00103   * no aliasing between \c *this and the source expression.
00104   *
00105   * More precisely, noalias() allows to bypass the EvalBeforeAssignBit flag.
00106   * Currently, even though several expressions may alias, only product
00107   * expressions have this flag. Therefore, noalias() is only usefull when
00108   * the source expression contains a matrix product.
00109   *
00110   * Here are some examples where noalias is usefull:
00111   * \code
00112   * D.noalias()  = A * B;
00113   * D.noalias() += A.transpose() * B;
00114   * D.noalias() -= 2 * A * B.adjoint();
00115   * \endcode
00116   *
00117   * On the other hand the following example will lead to a \b wrong result:
00118   * \code
00119   * A.noalias() = A * B;
00120   * \endcode
00121   * because the result matrix A is also an operand of the matrix product. Therefore,
00122   * there is no alternative than evaluating A * B in a temporary, that is the default
00123   * behavior when you write:
00124   * \code
00125   * A = A * B;
00126   * \endcode
00127   *
00128   * \sa class NoAlias
00129   */
00130 template<typename Derived>
00131 NoAlias<Derived,MatrixBase> MatrixBase<Derived>::noalias()
00132 {
00133   return derived();
00134 }
00135 
00136 #endif // EIGEN_NOALIAS_H



Page generated by Doxygen 1.7.3 for MRPT 0.9.4 SVN: at Sat Mar 26 06:40:17 UTC 2011