GeneralProduct.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) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
5 // Copyright (C) 2008-2011 Gael Guennebaud <gael.guennebaud@inria.fr>
6 //
7 // Eigen is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Lesser General Public
9 // License as published by the Free Software Foundation; either
10 // version 3 of the License, or (at your option) any later version.
11 //
12 // Alternatively, you can redistribute it and/or
13 // modify it under the terms of the GNU General Public License as
14 // published by the Free Software Foundation; either version 2 of
15 // the License, or (at your option) any later version.
16 //
17 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
20 // GNU General Public License for more details.
21 //
22 // You should have received a copy of the GNU Lesser General Public
23 // License and a copy of the GNU General Public License along with
24 // Eigen. If not, see <http://www.gnu.org/licenses/>.
25 
26 #ifndef EIGEN_GENERAL_PRODUCT_H
27 #define EIGEN_GENERAL_PRODUCT_H
28 
29 namespace Eigen {
30 
50 template<typename Lhs, typename Rhs, int ProductType = internal::product_type<Lhs,Rhs>::value>
51 class GeneralProduct;
52 
53 enum {
54  Large = 2,
55  Small = 3
56 };
57 
58 namespace internal {
59 
60 template<int Rows, int Cols, int Depth> struct product_type_selector;
61 
62 template<int Size, int MaxSize> struct product_size_category
63 {
64  enum { is_large = MaxSize == Dynamic ||
66  value = is_large ? Large
67  : Size == 1 ? 1
68  : Small
69  };
70 };
71 
72 template<typename Lhs, typename Rhs> struct product_type
73 {
74  typedef typename remove_all<Lhs>::type _Lhs;
75  typedef typename remove_all<Rhs>::type _Rhs;
76  enum {
77  MaxRows = _Lhs::MaxRowsAtCompileTime,
78  Rows = _Lhs::RowsAtCompileTime,
79  MaxCols = _Rhs::MaxColsAtCompileTime,
80  Cols = _Rhs::ColsAtCompileTime,
81  MaxDepth = EIGEN_SIZE_MIN_PREFER_FIXED(_Lhs::MaxColsAtCompileTime,
82  _Rhs::MaxRowsAtCompileTime),
83  Depth = EIGEN_SIZE_MIN_PREFER_FIXED(_Lhs::ColsAtCompileTime,
84  _Rhs::RowsAtCompileTime),
86  };
87 
88  // the splitting into different lines of code here, introducing the _select enums and the typedef below,
89  // is to work around an internal compiler error with gcc 4.1 and 4.2.
90 private:
91  enum {
92  rows_select = product_size_category<Rows,MaxRows>::value,
93  cols_select = product_size_category<Cols,MaxCols>::value,
94  depth_select = product_size_category<Depth,MaxDepth>::value
95  };
96  typedef product_type_selector<rows_select, cols_select, depth_select> selector;
97 
98 public:
99  enum {
100  value = selector::ret
101  };
102 #ifdef EIGEN_DEBUG_PRODUCT
103  static void debug()
104  {
105  EIGEN_DEBUG_VAR(Rows);
106  EIGEN_DEBUG_VAR(Cols);
107  EIGEN_DEBUG_VAR(Depth);
108  EIGEN_DEBUG_VAR(rows_select);
109  EIGEN_DEBUG_VAR(cols_select);
110  EIGEN_DEBUG_VAR(depth_select);
111  EIGEN_DEBUG_VAR(value);
112  }
113 #endif
114 };
115 
116 
117 /* The following allows to select the kind of product at compile time
118  * based on the three dimensions of the product.
119  * This is a compile time mapping from {1,Small,Large}^3 -> {product types} */
120 // FIXME I'm not sure the current mapping is the ideal one.
121 template<int M, int N> struct product_type_selector<M,N,1> { enum { ret = OuterProduct }; };
122 template<int Depth> struct product_type_selector<1, 1, Depth> { enum { ret = InnerProduct }; };
123 template<> struct product_type_selector<1, 1, 1> { enum { ret = InnerProduct }; };
124 template<> struct product_type_selector<Small,1, Small> { enum { ret = CoeffBasedProductMode }; };
125 template<> struct product_type_selector<1, Small,Small> { enum { ret = CoeffBasedProductMode }; };
126 template<> struct product_type_selector<Small,Small,Small> { enum { ret = CoeffBasedProductMode }; };
127 template<> struct product_type_selector<Small, Small, 1> { enum { ret = LazyCoeffBasedProductMode }; };
128 template<> struct product_type_selector<Small, Large, 1> { enum { ret = LazyCoeffBasedProductMode }; };
129 template<> struct product_type_selector<Large, Small, 1> { enum { ret = LazyCoeffBasedProductMode }; };
130 template<> struct product_type_selector<1, Large,Small> { enum { ret = CoeffBasedProductMode }; };
131 template<> struct product_type_selector<1, Large,Large> { enum { ret = GemvProduct }; };
132 template<> struct product_type_selector<1, Small,Large> { enum { ret = CoeffBasedProductMode }; };
133 template<> struct product_type_selector<Large,1, Small> { enum { ret = CoeffBasedProductMode }; };
134 template<> struct product_type_selector<Large,1, Large> { enum { ret = GemvProduct }; };
135 template<> struct product_type_selector<Small,1, Large> { enum { ret = CoeffBasedProductMode }; };
136 template<> struct product_type_selector<Small,Small,Large> { enum { ret = GemmProduct }; };
137 template<> struct product_type_selector<Large,Small,Large> { enum { ret = GemmProduct }; };
138 template<> struct product_type_selector<Small,Large,Large> { enum { ret = GemmProduct }; };
139 template<> struct product_type_selector<Large,Large,Large> { enum { ret = GemmProduct }; };
140 template<> struct product_type_selector<Large,Small,Small> { enum { ret = GemmProduct }; };
141 template<> struct product_type_selector<Small,Large,Small> { enum { ret = GemmProduct }; };
142 template<> struct product_type_selector<Large,Large,Small> { enum { ret = GemmProduct }; };
143 
144 } // end namespace internal
145 
163 template<typename Lhs, typename Rhs, int ProductType>
165 {
166  // TODO use the nested type to reduce instanciations ????
167 // typedef typename internal::nested<Lhs,Rhs::ColsAtCompileTime>::type LhsNested;
168 // typedef typename internal::nested<Rhs,Lhs::RowsAtCompileTime>::type RhsNested;
169 
170  typedef GeneralProduct<Lhs/*Nested*/, Rhs/*Nested*/, ProductType> Type;
171 };
172 
173 template<typename Lhs, typename Rhs>
175 {
176  typedef typename internal::nested<Lhs, Rhs::ColsAtCompileTime, typename internal::plain_matrix_type<Lhs>::type >::type LhsNested;
177  typedef typename internal::nested<Rhs, Lhs::RowsAtCompileTime, typename internal::plain_matrix_type<Rhs>::type >::type RhsNested;
179 };
180 
181 template<typename Lhs, typename Rhs>
183 {
184  typedef typename internal::nested<Lhs, Rhs::ColsAtCompileTime, typename internal::plain_matrix_type<Lhs>::type >::type LhsNested;
185  typedef typename internal::nested<Rhs, Lhs::RowsAtCompileTime, typename internal::plain_matrix_type<Rhs>::type >::type RhsNested;
187 };
188 
189 // this is a workaround for sun CC
190 template<typename Lhs, typename Rhs>
191 struct LazyProductReturnType : public ProductReturnType<Lhs,Rhs,LazyCoeffBasedProductMode>
192 {};
193 
194 /***********************************************************************
195 * Implementation of Inner Vector Vector Product
196 ***********************************************************************/
197 
198 // FIXME : maybe the "inner product" could return a Scalar
199 // instead of a 1x1 matrix ??
200 // Pro: more natural for the user
201 // Cons: this could be a problem if in a meta unrolled algorithm a matrix-matrix
202 // product ends up to a row-vector times col-vector product... To tackle this use
203 // case, we could have a specialization for Block<MatrixType,1,1> with: operator=(Scalar x);
204 
205 namespace internal {
206 
207 template<typename Lhs, typename Rhs>
208 struct traits<GeneralProduct<Lhs,Rhs,InnerProduct> >
209  : traits<Matrix<typename scalar_product_traits<typename Lhs::Scalar, typename Rhs::Scalar>::ReturnType,1,1> >
210 {};
211 
212 }
213 
214 template<typename Lhs, typename Rhs>
215 class GeneralProduct<Lhs, Rhs, InnerProduct>
216  : internal::no_assignment_operator,
217  public Matrix<typename internal::scalar_product_traits<typename Lhs::Scalar, typename Rhs::Scalar>::ReturnType,1,1>
218 {
220  public:
221  GeneralProduct(const Lhs& lhs, const Rhs& rhs)
222  {
223  EIGEN_STATIC_ASSERT((internal::is_same<typename Lhs::RealScalar, typename Rhs::RealScalar>::value),
224  YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
225 
226  Base::coeffRef(0,0) = (lhs.transpose().cwiseProduct(rhs)).sum();
227  }
228 
230  operator const typename Base::Scalar() const {
231  return Base::coeff(0,0);
232  }
233 };
234 
235 /***********************************************************************
236 * Implementation of Outer Vector Vector Product
237 ***********************************************************************/
238 
239 namespace internal {
240 template<int StorageOrder> struct outer_product_selector;
241 
242 template<typename Lhs, typename Rhs>
243 struct traits<GeneralProduct<Lhs,Rhs,OuterProduct> >
244  : traits<ProductBase<GeneralProduct<Lhs,Rhs,OuterProduct>, Lhs, Rhs> >
245 {};
246 
247 }
248 
249 template<typename Lhs, typename Rhs>
250 class GeneralProduct<Lhs, Rhs, OuterProduct>
251  : public ProductBase<GeneralProduct<Lhs,Rhs,OuterProduct>, Lhs, Rhs>
252 {
253  public:
255 
256  GeneralProduct(const Lhs& lhs, const Rhs& rhs) : Base(lhs,rhs)
257  {
258  EIGEN_STATIC_ASSERT((internal::is_same<typename Lhs::RealScalar, typename Rhs::RealScalar>::value),
259  YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
260  }
261 
262  template<typename Dest> void scaleAndAddTo(Dest& dest, Scalar alpha) const
263  {
264  internal::outer_product_selector<(int(Dest::Flags)&RowMajorBit) ? RowMajor : ColMajor>::run(*this, dest, alpha);
265  }
266 };
267 
268 namespace internal {
269 
270 template<> struct outer_product_selector<ColMajor> {
271  template<typename ProductType, typename Dest>
272  static EIGEN_DONT_INLINE void run(const ProductType& prod, Dest& dest, typename ProductType::Scalar alpha) {
273  typedef typename Dest::Index Index;
274  // FIXME make sure lhs is sequentially stored
275  // FIXME not very good if rhs is real and lhs complex while alpha is real too
276  const Index cols = dest.cols();
277  for (Index j=0; j<cols; ++j)
278  dest.col(j) += (alpha * prod.rhs().coeff(j)) * prod.lhs();
279  }
280 };
281 
282 template<> struct outer_product_selector<RowMajor> {
283  template<typename ProductType, typename Dest>
284  static EIGEN_DONT_INLINE void run(const ProductType& prod, Dest& dest, typename ProductType::Scalar alpha) {
285  typedef typename Dest::Index Index;
286  // FIXME make sure rhs is sequentially stored
287  // FIXME not very good if lhs is real and rhs complex while alpha is real too
288  const Index rows = dest.rows();
289  for (Index i=0; i<rows; ++i)
290  dest.row(i) += (alpha * prod.lhs().coeff(i)) * prod.rhs();
291  }
292 };
293 
294 } // end namespace internal
295 
296 /***********************************************************************
297 * Implementation of General Matrix Vector Product
298 ***********************************************************************/
299 
300 /* According to the shape/flags of the matrix we have to distinghish 3 different cases:
301  * 1 - the matrix is col-major, BLAS compatible and M is large => call fast BLAS-like colmajor routine
302  * 2 - the matrix is row-major, BLAS compatible and N is large => call fast BLAS-like rowmajor routine
303  * 3 - all other cases are handled using a simple loop along the outer-storage direction.
304  * Therefore we need a lower level meta selector.
305  * Furthermore, if the matrix is the rhs, then the product has to be transposed.
306  */
307 namespace internal {
308 
309 template<typename Lhs, typename Rhs>
310 struct traits<GeneralProduct<Lhs,Rhs,GemvProduct> >
311  : traits<ProductBase<GeneralProduct<Lhs,Rhs,GemvProduct>, Lhs, Rhs> >
312 {};
313 
314 template<int Side, int StorageOrder, bool BlasCompatible>
315 struct gemv_selector;
316 
317 } // end namespace internal
318 
319 template<typename Lhs, typename Rhs>
320 class GeneralProduct<Lhs, Rhs, GemvProduct>
321  : public ProductBase<GeneralProduct<Lhs,Rhs,GemvProduct>, Lhs, Rhs>
322 {
323  public:
325 
326  typedef typename Lhs::Scalar LhsScalar;
327  typedef typename Rhs::Scalar RhsScalar;
328 
329  GeneralProduct(const Lhs& lhs, const Rhs& rhs) : Base(lhs,rhs)
330  {
331 // EIGEN_STATIC_ASSERT((internal::is_same<typename Lhs::Scalar, typename Rhs::Scalar>::value),
332 // YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
333  }
334 
335  enum { Side = Lhs::IsVectorAtCompileTime ? OnTheLeft : OnTheRight };
336  typedef typename internal::conditional<int(Side)==OnTheRight,_LhsNested,_RhsNested>::type MatrixType;
337 
338  template<typename Dest> void scaleAndAddTo(Dest& dst, Scalar alpha) const
339  {
340  eigen_assert(m_lhs.rows() == dst.rows() && m_rhs.cols() == dst.cols());
341  internal::gemv_selector<Side,(int(MatrixType::Flags)&RowMajorBit) ? RowMajor : ColMajor,
342  bool(internal::blas_traits<MatrixType>::HasUsableDirectAccess)>::run(*this, dst, alpha);
343  }
344 };
345 
346 namespace internal {
347 
348 // The vector is on the left => transposition
349 template<int StorageOrder, bool BlasCompatible>
350 struct gemv_selector<OnTheLeft,StorageOrder,BlasCompatible>
351 {
352  template<typename ProductType, typename Dest>
353  static void run(const ProductType& prod, Dest& dest, typename ProductType::Scalar alpha)
354  {
355  Transpose<Dest> destT(dest);
356  enum { OtherStorageOrder = StorageOrder == RowMajor ? ColMajor : RowMajor };
357  gemv_selector<OnTheRight,OtherStorageOrder,BlasCompatible>
358  ::run(GeneralProduct<Transpose<const typename ProductType::_RhsNested>,Transpose<const typename ProductType::_LhsNested>, GemvProduct>
359  (prod.rhs().transpose(), prod.lhs().transpose()), destT, alpha);
360  }
361 };
362 
363 template<typename Scalar,int Size,int MaxSize,bool Cond> struct gemv_static_vector_if;
364 
365 template<typename Scalar,int Size,int MaxSize>
366 struct gemv_static_vector_if<Scalar,Size,MaxSize,false>
367 {
368  EIGEN_STRONG_INLINE Scalar* data() { eigen_internal_assert(false && "should never be called"); return 0; }
369 };
370 
371 template<typename Scalar,int Size>
372 struct gemv_static_vector_if<Scalar,Size,Dynamic,true>
373 {
374  EIGEN_STRONG_INLINE Scalar* data() { return 0; }
375 };
376 
377 template<typename Scalar,int Size,int MaxSize>
378 struct gemv_static_vector_if<Scalar,Size,MaxSize,true>
379 {
380  #if EIGEN_ALIGN_STATICALLY
381  internal::plain_array<Scalar,EIGEN_SIZE_MIN_PREFER_FIXED(Size,MaxSize),0> m_data;
382  EIGEN_STRONG_INLINE Scalar* data() { return m_data.array; }
383  #else
384  // Some architectures cannot align on the stack,
385  // => let's manually enforce alignment by allocating more data and return the address of the first aligned element.
386  enum {
387  ForceAlignment = internal::packet_traits<Scalar>::Vectorizable,
388  PacketSize = internal::packet_traits<Scalar>::size
389  };
390  internal::plain_array<Scalar,EIGEN_SIZE_MIN_PREFER_FIXED(Size,MaxSize)+(ForceAlignment?PacketSize:0),0> m_data;
391  EIGEN_STRONG_INLINE Scalar* data() {
392  return ForceAlignment
393  ? reinterpret_cast<Scalar*>((reinterpret_cast<size_t>(m_data.array) & ~(size_t(15))) + 16)
394  : m_data.array;
395  }
396  #endif
397 };
398 
399 template<> struct gemv_selector<OnTheRight,ColMajor,true>
400 {
401  template<typename ProductType, typename Dest>
402  static inline void run(const ProductType& prod, Dest& dest, typename ProductType::Scalar alpha)
403  {
404  typedef typename ProductType::Index Index;
405  typedef typename ProductType::LhsScalar LhsScalar;
406  typedef typename ProductType::RhsScalar RhsScalar;
407  typedef typename ProductType::Scalar ResScalar;
408  typedef typename ProductType::RealScalar RealScalar;
409  typedef typename ProductType::ActualLhsType ActualLhsType;
410  typedef typename ProductType::ActualRhsType ActualRhsType;
411  typedef typename ProductType::LhsBlasTraits LhsBlasTraits;
412  typedef typename ProductType::RhsBlasTraits RhsBlasTraits;
413  typedef Map<Matrix<ResScalar,Dynamic,1>, Aligned> MappedDest;
414 
415  ActualLhsType actualLhs = LhsBlasTraits::extract(prod.lhs());
416  ActualRhsType actualRhs = RhsBlasTraits::extract(prod.rhs());
417 
418  ResScalar actualAlpha = alpha * LhsBlasTraits::extractScalarFactor(prod.lhs())
419  * RhsBlasTraits::extractScalarFactor(prod.rhs());
420 
421  enum {
422  // FIXME find a way to allow an inner stride on the result if packet_traits<Scalar>::size==1
423  // on, the other hand it is good for the cache to pack the vector anyways...
424  EvalToDestAtCompileTime = Dest::InnerStrideAtCompileTime==1,
426  MightCannotUseDest = (Dest::InnerStrideAtCompileTime!=1) || ComplexByReal
427  };
428 
429  gemv_static_vector_if<ResScalar,Dest::SizeAtCompileTime,Dest::MaxSizeAtCompileTime,MightCannotUseDest> static_dest;
430 
431  bool alphaIsCompatible = (!ComplexByReal) || (imag(actualAlpha)==RealScalar(0));
432  bool evalToDest = EvalToDestAtCompileTime && alphaIsCompatible;
433 
434  RhsScalar compatibleAlpha = get_factor<ResScalar,RhsScalar>::run(actualAlpha);
435 
436  ei_declare_aligned_stack_constructed_variable(ResScalar,actualDestPtr,dest.size(),
437  evalToDest ? dest.data() : static_dest.data());
438 
439  if(!evalToDest)
440  {
441  #ifdef EIGEN_DENSE_STORAGE_CTOR_PLUGIN
442  int size = dest.size();
443  EIGEN_DENSE_STORAGE_CTOR_PLUGIN
444  #endif
445  if(!alphaIsCompatible)
446  {
447  MappedDest(actualDestPtr, dest.size()).setZero();
448  compatibleAlpha = RhsScalar(1);
449  }
450  else
451  MappedDest(actualDestPtr, dest.size()) = dest;
452  }
453 
455  <Index,LhsScalar,ColMajor,LhsBlasTraits::NeedToConjugate,RhsScalar,RhsBlasTraits::NeedToConjugate>::run(
456  actualLhs.rows(), actualLhs.cols(),
457  actualLhs.data(), actualLhs.outerStride(),
458  actualRhs.data(), actualRhs.innerStride(),
459  actualDestPtr, 1,
460  compatibleAlpha);
461 
462  if (!evalToDest)
463  {
464  if(!alphaIsCompatible)
465  dest += actualAlpha * MappedDest(actualDestPtr, dest.size());
466  else
467  dest = MappedDest(actualDestPtr, dest.size());
468  }
469  }
470 };
471 
472 template<> struct gemv_selector<OnTheRight,RowMajor,true>
473 {
474  template<typename ProductType, typename Dest>
475  static void run(const ProductType& prod, Dest& dest, typename ProductType::Scalar alpha)
476  {
477  typedef typename ProductType::LhsScalar LhsScalar;
478  typedef typename ProductType::RhsScalar RhsScalar;
479  typedef typename ProductType::Scalar ResScalar;
480  typedef typename ProductType::Index Index;
481  typedef typename ProductType::ActualLhsType ActualLhsType;
482  typedef typename ProductType::ActualRhsType ActualRhsType;
483  typedef typename ProductType::_ActualRhsType _ActualRhsType;
484  typedef typename ProductType::LhsBlasTraits LhsBlasTraits;
485  typedef typename ProductType::RhsBlasTraits RhsBlasTraits;
486 
487  typename add_const<ActualLhsType>::type actualLhs = LhsBlasTraits::extract(prod.lhs());
488  typename add_const<ActualRhsType>::type actualRhs = RhsBlasTraits::extract(prod.rhs());
489 
490  ResScalar actualAlpha = alpha * LhsBlasTraits::extractScalarFactor(prod.lhs())
491  * RhsBlasTraits::extractScalarFactor(prod.rhs());
492 
493  enum {
494  // FIXME find a way to allow an inner stride on the result if packet_traits<Scalar>::size==1
495  // on, the other hand it is good for the cache to pack the vector anyways...
496  DirectlyUseRhs = _ActualRhsType::InnerStrideAtCompileTime==1
497  };
498 
499  gemv_static_vector_if<RhsScalar,_ActualRhsType::SizeAtCompileTime,_ActualRhsType::MaxSizeAtCompileTime,!DirectlyUseRhs> static_rhs;
500 
501  ei_declare_aligned_stack_constructed_variable(RhsScalar,actualRhsPtr,actualRhs.size(),
502  DirectlyUseRhs ? const_cast<RhsScalar*>(actualRhs.data()) : static_rhs.data());
503 
504  if(!DirectlyUseRhs)
505  {
506  #ifdef EIGEN_DENSE_STORAGE_CTOR_PLUGIN
507  int size = actualRhs.size();
508  EIGEN_DENSE_STORAGE_CTOR_PLUGIN
509  #endif
510  Map<typename _ActualRhsType::PlainObject>(actualRhsPtr, actualRhs.size()) = actualRhs;
511  }
512 
514  <Index,LhsScalar,RowMajor,LhsBlasTraits::NeedToConjugate,RhsScalar,RhsBlasTraits::NeedToConjugate>::run(
515  actualLhs.rows(), actualLhs.cols(),
516  actualLhs.data(), actualLhs.outerStride(),
517  actualRhsPtr, 1,
518  dest.data(), dest.innerStride(),
519  actualAlpha);
520  }
521 };
522 
523 template<> struct gemv_selector<OnTheRight,ColMajor,false>
524 {
525  template<typename ProductType, typename Dest>
526  static void run(const ProductType& prod, Dest& dest, typename ProductType::Scalar alpha)
527  {
528  typedef typename Dest::Index Index;
529  // TODO makes sure dest is sequentially stored in memory, otherwise use a temp
530  const Index size = prod.rhs().rows();
531  for(Index k=0; k<size; ++k)
532  dest += (alpha*prod.rhs().coeff(k)) * prod.lhs().col(k);
533  }
534 };
535 
536 template<> struct gemv_selector<OnTheRight,RowMajor,false>
537 {
538  template<typename ProductType, typename Dest>
539  static void run(const ProductType& prod, Dest& dest, typename ProductType::Scalar alpha)
540  {
541  typedef typename Dest::Index Index;
542  // TODO makes sure rhs is sequentially stored in memory, otherwise use a temp
543  const Index rows = prod.rows();
544  for(Index i=0; i<rows; ++i)
545  dest.coeffRef(i) += alpha * (prod.lhs().row(i).cwiseProduct(prod.rhs().transpose())).sum();
546  }
547 };
548 
549 } // end namespace internal
550 
551 /***************************************************************************
552 * Implementation of matrix base methods
553 ***************************************************************************/
554 
561 template<typename Derived>
562 template<typename OtherDerived>
565 {
566  // A note regarding the function declaration: In MSVC, this function will sometimes
567  // not be inlined since DenseStorage is an unwindable object for dynamic
568  // matrices and product types are holding a member to store the result.
569  // Thus it does not help tagging this function with EIGEN_STRONG_INLINE.
570  enum {
571  ProductIsValid = Derived::ColsAtCompileTime==Dynamic
572  || OtherDerived::RowsAtCompileTime==Dynamic
573  || int(Derived::ColsAtCompileTime)==int(OtherDerived::RowsAtCompileTime),
574  AreVectors = Derived::IsVectorAtCompileTime && OtherDerived::IsVectorAtCompileTime,
575  SameSizes = EIGEN_PREDICATE_SAME_MATRIX_SIZE(Derived,OtherDerived)
576  };
577  // note to the lost user:
578  // * for a dot product use: v1.dot(v2)
579  // * for a coeff-wise product use: v1.cwiseProduct(v2)
580  EIGEN_STATIC_ASSERT(ProductIsValid || !(AreVectors && SameSizes),
581  INVALID_VECTOR_VECTOR_PRODUCT__IF_YOU_WANTED_A_DOT_OR_COEFF_WISE_PRODUCT_YOU_MUST_USE_THE_EXPLICIT_FUNCTIONS)
582  EIGEN_STATIC_ASSERT(ProductIsValid || !(SameSizes && !AreVectors),
583  INVALID_MATRIX_PRODUCT__IF_YOU_WANTED_A_COEFF_WISE_PRODUCT_YOU_MUST_USE_THE_EXPLICIT_FUNCTION)
584  EIGEN_STATIC_ASSERT(ProductIsValid || SameSizes, INVALID_MATRIX_PRODUCT)
585 #ifdef EIGEN_DEBUG_PRODUCT
586  internal::product_type<Derived,OtherDerived>::debug();
587 #endif
588  return typename ProductReturnType<Derived,OtherDerived>::Type(derived(), other.derived());
589 }
590 
602 template<typename Derived>
603 template<typename OtherDerived>
606 {
607  enum {
608  ProductIsValid = Derived::ColsAtCompileTime==Dynamic
609  || OtherDerived::RowsAtCompileTime==Dynamic
610  || int(Derived::ColsAtCompileTime)==int(OtherDerived::RowsAtCompileTime),
611  AreVectors = Derived::IsVectorAtCompileTime && OtherDerived::IsVectorAtCompileTime,
612  SameSizes = EIGEN_PREDICATE_SAME_MATRIX_SIZE(Derived,OtherDerived)
613  };
614  // note to the lost user:
615  // * for a dot product use: v1.dot(v2)
616  // * for a coeff-wise product use: v1.cwiseProduct(v2)
617  EIGEN_STATIC_ASSERT(ProductIsValid || !(AreVectors && SameSizes),
618  INVALID_VECTOR_VECTOR_PRODUCT__IF_YOU_WANTED_A_DOT_OR_COEFF_WISE_PRODUCT_YOU_MUST_USE_THE_EXPLICIT_FUNCTIONS)
619  EIGEN_STATIC_ASSERT(ProductIsValid || !(SameSizes && !AreVectors),
620  INVALID_MATRIX_PRODUCT__IF_YOU_WANTED_A_COEFF_WISE_PRODUCT_YOU_MUST_USE_THE_EXPLICIT_FUNCTION)
621  EIGEN_STATIC_ASSERT(ProductIsValid || SameSizes, INVALID_MATRIX_PRODUCT)
622 
623  return typename LazyProductReturnType<Derived,OtherDerived>::Type(derived(), other.derived());
624 }
625 
626 } // end namespace Eigen
627 
628 #endif // EIGEN_PRODUCT_H