SelfCwiseBinaryOp.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2009-2010 Gael Guennebaud <gael.guennebaud@inria.fr>
5 //
6 // Eigen is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 3 of the License, or (at your option) any later version.
10 //
11 // Alternatively, you can redistribute it and/or
12 // modify it under the terms of the GNU General Public License as
13 // published by the Free Software Foundation; either version 2 of
14 // the License, or (at your option) any later version.
15 //
16 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
17 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU Lesser General Public
22 // License and a copy of the GNU General Public License along with
23 // Eigen. If not, see <http://www.gnu.org/licenses/>.
24 
25 #ifndef EIGEN_SELFCWISEBINARYOP_H
26 #define EIGEN_SELFCWISEBINARYOP_H
27 
28 namespace Eigen {
29 
45 namespace internal {
46 template<typename BinaryOp, typename Lhs, typename Rhs>
47 struct traits<SelfCwiseBinaryOp<BinaryOp,Lhs,Rhs> >
48  : traits<CwiseBinaryOp<BinaryOp,Lhs,Rhs> >
49 {
50  enum {
51  // Note that it is still a good idea to preserve the DirectAccessBit
52  // so that assign can correctly align the data.
53  Flags = traits<CwiseBinaryOp<BinaryOp,Lhs,Rhs> >::Flags | (Lhs::Flags&DirectAccessBit) | (Lhs::Flags&LvalueBit),
54  OuterStrideAtCompileTime = Lhs::OuterStrideAtCompileTime,
55  InnerStrideAtCompileTime = Lhs::InnerStrideAtCompileTime
56  };
57 };
58 }
59 
60 template<typename BinaryOp, typename Lhs, typename Rhs> class SelfCwiseBinaryOp
61  : public internal::dense_xpr_base< SelfCwiseBinaryOp<BinaryOp, Lhs, Rhs> >::type
62 {
63  public:
64 
65  typedef typename internal::dense_xpr_base<SelfCwiseBinaryOp>::type Base;
67 
68  typedef typename internal::packet_traits<Scalar>::type Packet;
69 
70  inline SelfCwiseBinaryOp(Lhs& xpr, const BinaryOp& func = BinaryOp()) : m_matrix(xpr), m_functor(func) {}
71 
72  inline Index rows() const { return m_matrix.rows(); }
73  inline Index cols() const { return m_matrix.cols(); }
74  inline Index outerStride() const { return m_matrix.outerStride(); }
75  inline Index innerStride() const { return m_matrix.innerStride(); }
76  inline const Scalar* data() const { return m_matrix.data(); }
77 
78  // note that this function is needed by assign to correctly align loads/stores
79  // TODO make Assign use .data()
80  inline Scalar& coeffRef(Index row, Index col)
81  {
83  return m_matrix.const_cast_derived().coeffRef(row, col);
84  }
85  inline const Scalar& coeffRef(Index row, Index col) const
86  {
87  return m_matrix.coeffRef(row, col);
88  }
89 
90  // note that this function is needed by assign to correctly align loads/stores
91  // TODO make Assign use .data()
92  inline Scalar& coeffRef(Index index)
93  {
95  return m_matrix.const_cast_derived().coeffRef(index);
96  }
97  inline const Scalar& coeffRef(Index index) const
98  {
99  return m_matrix.const_cast_derived().coeffRef(index);
100  }
101 
102  template<typename OtherDerived>
103  void copyCoeff(Index row, Index col, const DenseBase<OtherDerived>& other)
104  {
105  OtherDerived& _other = other.const_cast_derived();
106  eigen_internal_assert(row >= 0 && row < rows()
107  && col >= 0 && col < cols());
108  Scalar& tmp = m_matrix.coeffRef(row,col);
109  tmp = m_functor(tmp, _other.coeff(row,col));
110  }
111 
112  template<typename OtherDerived>
113  void copyCoeff(Index index, const DenseBase<OtherDerived>& other)
114  {
115  OtherDerived& _other = other.const_cast_derived();
116  eigen_internal_assert(index >= 0 && index < m_matrix.size());
117  Scalar& tmp = m_matrix.coeffRef(index);
118  tmp = m_functor(tmp, _other.coeff(index));
119  }
120 
121  template<typename OtherDerived, int StoreMode, int LoadMode>
122  void copyPacket(Index row, Index col, const DenseBase<OtherDerived>& other)
123  {
124  OtherDerived& _other = other.const_cast_derived();
125  eigen_internal_assert(row >= 0 && row < rows()
126  && col >= 0 && col < cols());
127  m_matrix.template writePacket<StoreMode>(row, col,
128  m_functor.packetOp(m_matrix.template packet<StoreMode>(row, col),_other.template packet<LoadMode>(row, col)) );
129  }
130 
131  template<typename OtherDerived, int StoreMode, int LoadMode>
132  void copyPacket(Index index, const DenseBase<OtherDerived>& other)
133  {
134  OtherDerived& _other = other.const_cast_derived();
135  eigen_internal_assert(index >= 0 && index < m_matrix.size());
136  m_matrix.template writePacket<StoreMode>(index,
137  m_functor.packetOp(m_matrix.template packet<StoreMode>(index),_other.template packet<LoadMode>(index)) );
138  }
139 
140  // reimplement lazyAssign to handle complex *= real
141  // see CwiseBinaryOp ctor for details
142  template<typename RhsDerived>
144  {
146  EIGEN_CHECK_BINARY_COMPATIBILIY(BinaryOp,typename Lhs::Scalar,typename RhsDerived::Scalar);
147 
148  #ifdef EIGEN_DEBUG_ASSIGN
149  internal::assign_traits<SelfCwiseBinaryOp, RhsDerived>::debug();
150  #endif
151  eigen_assert(rows() == rhs.rows() && cols() == rhs.cols());
152  internal::assign_impl<SelfCwiseBinaryOp, RhsDerived>::run(*this,rhs.derived());
153  #ifndef EIGEN_NO_DEBUG
154  this->checkTransposeAliasing(rhs.derived());
155  #endif
156  return *this;
157  }
158 
159  // overloaded to honor evaluation of special matrices
160  // maybe another solution would be to not use SelfCwiseBinaryOp
161  // at first...
162  SelfCwiseBinaryOp& operator=(const Rhs& _rhs)
163  {
164  typename internal::nested<Rhs>::type rhs(_rhs);
165  return Base::operator=(rhs);
166  }
167 
168  Lhs& expression() const
169  {
170  return m_matrix;
171  }
172 
173  const BinaryOp& functor() const
174  {
175  return m_functor;
176  }
177 
178  protected:
179  Lhs& m_matrix;
180  const BinaryOp& m_functor;
181 
182  private:
184 };
185 
186 template<typename Derived>
187 inline Derived& DenseBase<Derived>::operator*=(const Scalar& other)
188 {
189  typedef typename Derived::PlainObject PlainObject;
190  SelfCwiseBinaryOp<internal::scalar_product_op<Scalar>, Derived, typename PlainObject::ConstantReturnType> tmp(derived());
191  tmp = PlainObject::Constant(rows(),cols(),other);
192  return derived();
193 }
194 
195 template<typename Derived>
196 inline Derived& DenseBase<Derived>::operator/=(const Scalar& other)
197 {
198  typedef typename internal::conditional<NumTraits<Scalar>::IsInteger,
199  internal::scalar_quotient_op<Scalar>,
200  internal::scalar_product_op<Scalar> >::type BinOp;
201  typedef typename Derived::PlainObject PlainObject;
203  tmp = PlainObject::Constant(rows(),cols(), NumTraits<Scalar>::IsInteger ? other : Scalar(1)/other);
204  return derived();
205 }
206 
207 } // end namespace Eigen
208 
209 #endif // EIGEN_SELFCWISEBINARYOP_H