SelfCwiseBinaryOp.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_SELFCWISEBINARYOP_H
00026 #define EIGEN_SELFCWISEBINARYOP_H
00027 
00028 namespace Eigen { 
00029 
00045 namespace internal {
00046 template<typename BinaryOp, typename Lhs, typename Rhs>
00047 struct traits<SelfCwiseBinaryOp<BinaryOp,Lhs,Rhs> >
00048   : traits<CwiseBinaryOp<BinaryOp,Lhs,Rhs> >
00049 {
00050   enum {
00051     // Note that it is still a good idea to preserve the DirectAccessBit
00052     // so that assign can correctly align the data.
00053     Flags = traits<CwiseBinaryOp<BinaryOp,Lhs,Rhs> >::Flags | (Lhs::Flags&DirectAccessBit) | (Lhs::Flags&LvalueBit),
00054     OuterStrideAtCompileTime = Lhs::OuterStrideAtCompileTime,
00055     InnerStrideAtCompileTime = Lhs::InnerStrideAtCompileTime
00056   };
00057 };
00058 }
00059 
00060 template<typename BinaryOp, typename Lhs, typename Rhs> class SelfCwiseBinaryOp
00061   : public internal::dense_xpr_base< SelfCwiseBinaryOp<BinaryOp, Lhs, Rhs> >::type
00062 {
00063   public:
00064 
00065     typedef typename internal::dense_xpr_base<SelfCwiseBinaryOp>::type Base;
00066     EIGEN_DENSE_PUBLIC_INTERFACE(SelfCwiseBinaryOp)
00067 
00068     typedef typename internal::packet_traits<Scalar>::type Packet;
00069 
00070     inline SelfCwiseBinaryOp(Lhs& xpr, const BinaryOp& func = BinaryOp()) : m_matrix(xpr), m_functor(func) {}
00071 
00072     inline Index rows() const { return m_matrix.rows(); }
00073     inline Index cols() const { return m_matrix.cols(); }
00074     inline Index outerStride() const { return m_matrix.outerStride(); }
00075     inline Index innerStride() const { return m_matrix.innerStride(); }
00076     inline const Scalar* data() const { return m_matrix.data(); }
00077 
00078     // note that this function is needed by assign to correctly align loads/stores
00079     // TODO make Assign use .data()
00080     inline Scalar& coeffRef(Index row, Index col)
00081     {
00082       EIGEN_STATIC_ASSERT_LVALUE(Lhs)
00083       return m_matrix.const_cast_derived().coeffRef(row, col);
00084     }
00085     inline const Scalar& coeffRef(Index row, Index col) const
00086     {
00087       return m_matrix.coeffRef(row, col);
00088     }
00089 
00090     // note that this function is needed by assign to correctly align loads/stores
00091     // TODO make Assign use .data()
00092     inline Scalar& coeffRef(Index index)
00093     {
00094       EIGEN_STATIC_ASSERT_LVALUE(Lhs)
00095       return m_matrix.const_cast_derived().coeffRef(index);
00096     }
00097     inline const Scalar& coeffRef(Index index) const
00098     {
00099       return m_matrix.const_cast_derived().coeffRef(index);
00100     }
00101 
00102     template<typename OtherDerived>
00103     void copyCoeff(Index row, Index col, const DenseBase<OtherDerived>& other)
00104     {
00105       OtherDerived& _other = other.const_cast_derived();
00106       eigen_internal_assert(row >= 0 && row < rows()
00107                          && col >= 0 && col < cols());
00108       Scalar& tmp = m_matrix.coeffRef(row,col);
00109       tmp = m_functor(tmp, _other.coeff(row,col));
00110     }
00111 
00112     template<typename OtherDerived>
00113     void copyCoeff(Index index, const DenseBase<OtherDerived>& other)
00114     {
00115       OtherDerived& _other = other.const_cast_derived();
00116       eigen_internal_assert(index >= 0 && index < m_matrix.size());
00117       Scalar& tmp = m_matrix.coeffRef(index);
00118       tmp = m_functor(tmp, _other.coeff(index));
00119     }
00120 
00121     template<typename OtherDerived, int StoreMode, int LoadMode>
00122     void copyPacket(Index row, Index col, const DenseBase<OtherDerived>& other)
00123     {
00124       OtherDerived& _other = other.const_cast_derived();
00125       eigen_internal_assert(row >= 0 && row < rows()
00126                         && col >= 0 && col < cols());
00127       m_matrix.template writePacket<StoreMode>(row, col,
00128         m_functor.packetOp(m_matrix.template packet<StoreMode>(row, col),_other.template packet<LoadMode>(row, col)) );
00129     }
00130 
00131     template<typename OtherDerived, int StoreMode, int LoadMode>
00132     void copyPacket(Index index, const DenseBase<OtherDerived>& other)
00133     {
00134       OtherDerived& _other = other.const_cast_derived();
00135       eigen_internal_assert(index >= 0 && index < m_matrix.size());
00136       m_matrix.template writePacket<StoreMode>(index,
00137         m_functor.packetOp(m_matrix.template packet<StoreMode>(index),_other.template packet<LoadMode>(index)) );
00138     }
00139 
00140     // reimplement lazyAssign to handle complex *= real
00141     // see CwiseBinaryOp ctor for details
00142     template<typename RhsDerived>
00143     EIGEN_STRONG_INLINE SelfCwiseBinaryOp& lazyAssign(const DenseBase<RhsDerived>& rhs)
00144     {
00145       EIGEN_STATIC_ASSERT_SAME_MATRIX_SIZE(Lhs,RhsDerived)
00146       EIGEN_CHECK_BINARY_COMPATIBILIY(BinaryOp,typename Lhs::Scalar,typename RhsDerived::Scalar);
00147       
00148     #ifdef EIGEN_DEBUG_ASSIGN
00149       internal::assign_traits<SelfCwiseBinaryOp, RhsDerived>::debug();
00150     #endif
00151       eigen_assert(rows() == rhs.rows() && cols() == rhs.cols());
00152       internal::assign_impl<SelfCwiseBinaryOp, RhsDerived>::run(*this,rhs.derived());
00153     #ifndef EIGEN_NO_DEBUG
00154       this->checkTransposeAliasing(rhs.derived());
00155     #endif
00156       return *this;
00157     }
00158     
00159     // overloaded to honor evaluation of special matrices
00160     // maybe another solution would be to not use SelfCwiseBinaryOp
00161     // at first...
00162     SelfCwiseBinaryOp& operator=(const Rhs& _rhs)
00163     {
00164       typename internal::nested<Rhs>::type rhs(_rhs);
00165       return Base::operator=(rhs);
00166     }
00167 
00168     Lhs& expression() const 
00169     { 
00170       return m_matrix;
00171     }
00172 
00173     const BinaryOp& functor() const 
00174     { 
00175       return m_functor;
00176     }
00177 
00178   protected:
00179     Lhs& m_matrix;
00180     const BinaryOp& m_functor;
00181 
00182   private:
00183     SelfCwiseBinaryOp& operator=(const SelfCwiseBinaryOp&);
00184 };
00185 
00186 template<typename Derived>
00187 inline Derived& DenseBase<Derived>::operator*=(const Scalar& other)
00188 {
00189   typedef typename Derived::PlainObject PlainObject;
00190   SelfCwiseBinaryOp<internal::scalar_product_op<Scalar>, Derived, typename PlainObject::ConstantReturnType> tmp(derived());
00191   tmp = PlainObject::Constant(rows(),cols(),other);
00192   return derived();
00193 }
00194 
00195 template<typename Derived>
00196 inline Derived& DenseBase<Derived>::operator/=(const Scalar& other)
00197 {
00198   typedef typename internal::conditional<NumTraits<Scalar>::IsInteger,
00199                                         internal::scalar_quotient_op<Scalar>,
00200                                         internal::scalar_product_op<Scalar> >::type BinOp;
00201   typedef typename Derived::PlainObject PlainObject;
00202   SelfCwiseBinaryOp<BinOp, Derived, typename PlainObject::ConstantReturnType> tmp(derived());
00203   tmp = PlainObject::Constant(rows(),cols(), NumTraits<Scalar>::IsInteger ? other : Scalar(1)/other);
00204   return derived();
00205 }
00206 
00207 } // end namespace Eigen
00208 
00209 #endif // EIGEN_SELFCWISEBINARYOP_H