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


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:32:28