Please, help us to better know about our user community by answering the following short survey: https://forms.gle/wpyrxWi18ox9Z5ae9
Eigen  3.3.9
ProductEvaluators.h
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-2010 Gael Guennebaud <gael.guennebaud@inria.fr>
6 // Copyright (C) 2011 Jitse Niesen <jitse@maths.leeds.ac.uk>
7 //
8 // This Source Code Form is subject to the terms of the Mozilla
9 // Public License v. 2.0. If a copy of the MPL was not distributed
10 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
11 
12 
13 #ifndef EIGEN_PRODUCTEVALUATORS_H
14 #define EIGEN_PRODUCTEVALUATORS_H
15 
16 namespace Eigen {
17 
18 namespace internal {
19 
28 template<typename Lhs, typename Rhs, int Options>
29 struct evaluator<Product<Lhs, Rhs, Options> >
30  : public product_evaluator<Product<Lhs, Rhs, Options> >
31 {
32  typedef Product<Lhs, Rhs, Options> XprType;
33  typedef product_evaluator<XprType> Base;
34 
35  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE explicit evaluator(const XprType& xpr) : Base(xpr) {}
36 };
37 
38 // Catch "scalar * ( A * B )" and transform it to "(A*scalar) * B"
39 // TODO we should apply that rule only if that's really helpful
40 template<typename Lhs, typename Rhs, typename Scalar1, typename Scalar2, typename Plain1>
41 struct evaluator_assume_aliasing<CwiseBinaryOp<internal::scalar_product_op<Scalar1,Scalar2>,
42  const CwiseNullaryOp<internal::scalar_constant_op<Scalar1>, Plain1>,
43  const Product<Lhs, Rhs, DefaultProduct> > >
44 {
45  static const bool value = true;
46 };
47 template<typename Lhs, typename Rhs, typename Scalar1, typename Scalar2, typename Plain1>
48 struct evaluator<CwiseBinaryOp<internal::scalar_product_op<Scalar1,Scalar2>,
49  const CwiseNullaryOp<internal::scalar_constant_op<Scalar1>, Plain1>,
50  const Product<Lhs, Rhs, DefaultProduct> > >
51  : public evaluator<Product<EIGEN_SCALAR_BINARYOP_EXPR_RETURN_TYPE(Scalar1,Lhs,product), Rhs, DefaultProduct> >
52 {
53  typedef CwiseBinaryOp<internal::scalar_product_op<Scalar1,Scalar2>,
54  const CwiseNullaryOp<internal::scalar_constant_op<Scalar1>, Plain1>,
55  const Product<Lhs, Rhs, DefaultProduct> > XprType;
56  typedef evaluator<Product<EIGEN_SCALAR_BINARYOP_EXPR_RETURN_TYPE(Scalar1,Lhs,product), Rhs, DefaultProduct> > Base;
57 
58  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE explicit evaluator(const XprType& xpr)
59  : Base(xpr.lhs().functor().m_other * xpr.rhs().lhs() * xpr.rhs().rhs())
60  {}
61 };
62 
63 
64 template<typename Lhs, typename Rhs, int DiagIndex>
65 struct evaluator<Diagonal<const Product<Lhs, Rhs, DefaultProduct>, DiagIndex> >
66  : public evaluator<Diagonal<const Product<Lhs, Rhs, LazyProduct>, DiagIndex> >
67 {
68  typedef Diagonal<const Product<Lhs, Rhs, DefaultProduct>, DiagIndex> XprType;
69  typedef evaluator<Diagonal<const Product<Lhs, Rhs, LazyProduct>, DiagIndex> > Base;
70 
71  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE explicit evaluator(const XprType& xpr)
72  : Base(Diagonal<const Product<Lhs, Rhs, LazyProduct>, DiagIndex>(
73  Product<Lhs, Rhs, LazyProduct>(xpr.nestedExpression().lhs(), xpr.nestedExpression().rhs()),
74  xpr.index() ))
75  {}
76 };
77 
78 
79 // Helper class to perform a matrix product with the destination at hand.
80 // Depending on the sizes of the factors, there are different evaluation strategies
81 // as controlled by internal::product_type.
82 template< typename Lhs, typename Rhs,
83  typename LhsShape = typename evaluator_traits<Lhs>::Shape,
84  typename RhsShape = typename evaluator_traits<Rhs>::Shape,
85  int ProductType = internal::product_type<Lhs,Rhs>::value>
86 struct generic_product_impl;
87 
88 template<typename Lhs, typename Rhs>
89 struct evaluator_assume_aliasing<Product<Lhs, Rhs, DefaultProduct> > {
90  static const bool value = true;
91 };
92 
93 // This is the default evaluator implementation for products:
94 // It creates a temporary and call generic_product_impl
95 template<typename Lhs, typename Rhs, int Options, int ProductTag, typename LhsShape, typename RhsShape>
96 struct product_evaluator<Product<Lhs, Rhs, Options>, ProductTag, LhsShape, RhsShape>
97  : public evaluator<typename Product<Lhs, Rhs, Options>::PlainObject>
98 {
99  typedef Product<Lhs, Rhs, Options> XprType;
100  typedef typename XprType::PlainObject PlainObject;
101  typedef evaluator<PlainObject> Base;
102  enum {
103  Flags = Base::Flags | EvalBeforeNestingBit
104  };
105 
106  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
107  explicit product_evaluator(const XprType& xpr)
108  : m_result(xpr.rows(), xpr.cols())
109  {
110  ::new (static_cast<Base*>(this)) Base(m_result);
111 
112 // FIXME shall we handle nested_eval here?,
113 // if so, then we must take care at removing the call to nested_eval in the specializations (e.g., in permutation_matrix_product, transposition_matrix_product, etc.)
114 // typedef typename internal::nested_eval<Lhs,Rhs::ColsAtCompileTime>::type LhsNested;
115 // typedef typename internal::nested_eval<Rhs,Lhs::RowsAtCompileTime>::type RhsNested;
116 // typedef typename internal::remove_all<LhsNested>::type LhsNestedCleaned;
117 // typedef typename internal::remove_all<RhsNested>::type RhsNestedCleaned;
118 //
119 // const LhsNested lhs(xpr.lhs());
120 // const RhsNested rhs(xpr.rhs());
121 //
122 // generic_product_impl<LhsNestedCleaned, RhsNestedCleaned>::evalTo(m_result, lhs, rhs);
123 
124  generic_product_impl<Lhs, Rhs, LhsShape, RhsShape, ProductTag>::evalTo(m_result, xpr.lhs(), xpr.rhs());
125  }
126 
127 protected:
128  PlainObject m_result;
129 };
130 
131 // The following three shortcuts are enabled only if the scalar types match excatly.
132 // TODO: we could enable them for different scalar types when the product is not vectorized.
133 
134 // Dense = Product
135 template< typename DstXprType, typename Lhs, typename Rhs, int Options, typename Scalar>
136 struct Assignment<DstXprType, Product<Lhs,Rhs,Options>, internal::assign_op<Scalar,Scalar>, Dense2Dense,
137  typename enable_if<(Options==DefaultProduct || Options==AliasFreeProduct)>::type>
138 {
139  typedef Product<Lhs,Rhs,Options> SrcXprType;
140  static EIGEN_STRONG_INLINE
141  void run(DstXprType &dst, const SrcXprType &src, const internal::assign_op<Scalar,Scalar> &)
142  {
143  Index dstRows = src.rows();
144  Index dstCols = src.cols();
145  if((dst.rows()!=dstRows) || (dst.cols()!=dstCols))
146  dst.resize(dstRows, dstCols);
147  // FIXME shall we handle nested_eval here?
148  generic_product_impl<Lhs, Rhs>::evalTo(dst, src.lhs(), src.rhs());
149  }
150 };
151 
152 // Dense += Product
153 template< typename DstXprType, typename Lhs, typename Rhs, int Options, typename Scalar>
154 struct Assignment<DstXprType, Product<Lhs,Rhs,Options>, internal::add_assign_op<Scalar,Scalar>, Dense2Dense,
155  typename enable_if<(Options==DefaultProduct || Options==AliasFreeProduct)>::type>
156 {
157  typedef Product<Lhs,Rhs,Options> SrcXprType;
158  static EIGEN_STRONG_INLINE
159  void run(DstXprType &dst, const SrcXprType &src, const internal::add_assign_op<Scalar,Scalar> &)
160  {
161  eigen_assert(dst.rows() == src.rows() && dst.cols() == src.cols());
162  // FIXME shall we handle nested_eval here?
163  generic_product_impl<Lhs, Rhs>::addTo(dst, src.lhs(), src.rhs());
164  }
165 };
166 
167 // Dense -= Product
168 template< typename DstXprType, typename Lhs, typename Rhs, int Options, typename Scalar>
169 struct Assignment<DstXprType, Product<Lhs,Rhs,Options>, internal::sub_assign_op<Scalar,Scalar>, Dense2Dense,
170  typename enable_if<(Options==DefaultProduct || Options==AliasFreeProduct)>::type>
171 {
172  typedef Product<Lhs,Rhs,Options> SrcXprType;
173  static EIGEN_STRONG_INLINE
174  void run(DstXprType &dst, const SrcXprType &src, const internal::sub_assign_op<Scalar,Scalar> &)
175  {
176  eigen_assert(dst.rows() == src.rows() && dst.cols() == src.cols());
177  // FIXME shall we handle nested_eval here?
178  generic_product_impl<Lhs, Rhs>::subTo(dst, src.lhs(), src.rhs());
179  }
180 };
181 
182 
183 // Dense ?= scalar * Product
184 // TODO we should apply that rule if that's really helpful
185 // for instance, this is not good for inner products
186 template< typename DstXprType, typename Lhs, typename Rhs, typename AssignFunc, typename Scalar, typename ScalarBis, typename Plain>
187 struct Assignment<DstXprType, CwiseBinaryOp<internal::scalar_product_op<ScalarBis,Scalar>, const CwiseNullaryOp<internal::scalar_constant_op<ScalarBis>,Plain>,
188  const Product<Lhs,Rhs,DefaultProduct> >, AssignFunc, Dense2Dense>
189 {
190  typedef CwiseBinaryOp<internal::scalar_product_op<ScalarBis,Scalar>,
191  const CwiseNullaryOp<internal::scalar_constant_op<ScalarBis>,Plain>,
192  const Product<Lhs,Rhs,DefaultProduct> > SrcXprType;
193  static EIGEN_STRONG_INLINE
194  void run(DstXprType &dst, const SrcXprType &src, const AssignFunc& func)
195  {
196  call_assignment_no_alias(dst, (src.lhs().functor().m_other * src.rhs().lhs())*src.rhs().rhs(), func);
197  }
198 };
199 
200 //----------------------------------------
201 // Catch "Dense ?= xpr + Product<>" expression to save one temporary
202 // FIXME we could probably enable these rules for any product, i.e., not only Dense and DefaultProduct
203 
204 template<typename OtherXpr, typename Lhs, typename Rhs>
205 struct evaluator_assume_aliasing<CwiseBinaryOp<internal::scalar_sum_op<typename OtherXpr::Scalar,typename Product<Lhs,Rhs,DefaultProduct>::Scalar>, const OtherXpr,
206  const Product<Lhs,Rhs,DefaultProduct> >, DenseShape > {
207  static const bool value = true;
208 };
209 
210 template<typename OtherXpr, typename Lhs, typename Rhs>
211 struct evaluator_assume_aliasing<CwiseBinaryOp<internal::scalar_difference_op<typename OtherXpr::Scalar,typename Product<Lhs,Rhs,DefaultProduct>::Scalar>, const OtherXpr,
212  const Product<Lhs,Rhs,DefaultProduct> >, DenseShape > {
213  static const bool value = true;
214 };
215 
216 template<typename DstXprType, typename OtherXpr, typename ProductType, typename Func1, typename Func2>
217 struct assignment_from_xpr_op_product
218 {
219  template<typename SrcXprType, typename InitialFunc>
220  static EIGEN_STRONG_INLINE
221  void run(DstXprType &dst, const SrcXprType &src, const InitialFunc& /*func*/)
222  {
223  call_assignment_no_alias(dst, src.lhs(), Func1());
224  call_assignment_no_alias(dst, src.rhs(), Func2());
225  }
226 };
227 
228 #define EIGEN_CATCH_ASSIGN_XPR_OP_PRODUCT(ASSIGN_OP,BINOP,ASSIGN_OP2) \
229  template< typename DstXprType, typename OtherXpr, typename Lhs, typename Rhs, typename DstScalar, typename SrcScalar, typename OtherScalar,typename ProdScalar> \
230  struct Assignment<DstXprType, CwiseBinaryOp<internal::BINOP<OtherScalar,ProdScalar>, const OtherXpr, \
231  const Product<Lhs,Rhs,DefaultProduct> >, internal::ASSIGN_OP<DstScalar,SrcScalar>, Dense2Dense> \
232  : assignment_from_xpr_op_product<DstXprType, OtherXpr, Product<Lhs,Rhs,DefaultProduct>, internal::ASSIGN_OP<DstScalar,OtherScalar>, internal::ASSIGN_OP2<DstScalar,ProdScalar> > \
233  {}
234 
235 EIGEN_CATCH_ASSIGN_XPR_OP_PRODUCT(assign_op, scalar_sum_op,add_assign_op);
236 EIGEN_CATCH_ASSIGN_XPR_OP_PRODUCT(add_assign_op,scalar_sum_op,add_assign_op);
237 EIGEN_CATCH_ASSIGN_XPR_OP_PRODUCT(sub_assign_op,scalar_sum_op,sub_assign_op);
238 
239 EIGEN_CATCH_ASSIGN_XPR_OP_PRODUCT(assign_op, scalar_difference_op,sub_assign_op);
240 EIGEN_CATCH_ASSIGN_XPR_OP_PRODUCT(add_assign_op,scalar_difference_op,sub_assign_op);
241 EIGEN_CATCH_ASSIGN_XPR_OP_PRODUCT(sub_assign_op,scalar_difference_op,add_assign_op);
242 
243 //----------------------------------------
244 
245 template<typename Lhs, typename Rhs>
246 struct generic_product_impl<Lhs,Rhs,DenseShape,DenseShape,InnerProduct>
247 {
248  template<typename Dst>
249  static EIGEN_STRONG_INLINE void evalTo(Dst& dst, const Lhs& lhs, const Rhs& rhs)
250  {
251  dst.coeffRef(0,0) = (lhs.transpose().cwiseProduct(rhs)).sum();
252  }
253 
254  template<typename Dst>
255  static EIGEN_STRONG_INLINE void addTo(Dst& dst, const Lhs& lhs, const Rhs& rhs)
256  {
257  dst.coeffRef(0,0) += (lhs.transpose().cwiseProduct(rhs)).sum();
258  }
259 
260  template<typename Dst>
261  static EIGEN_STRONG_INLINE void subTo(Dst& dst, const Lhs& lhs, const Rhs& rhs)
262  { dst.coeffRef(0,0) -= (lhs.transpose().cwiseProduct(rhs)).sum(); }
263 };
264 
265 
266 /***********************************************************************
267 * Implementation of outer dense * dense vector product
268 ***********************************************************************/
269 
270 // Column major result
271 template<typename Dst, typename Lhs, typename Rhs, typename Func>
272 void outer_product_selector_run(Dst& dst, const Lhs &lhs, const Rhs &rhs, const Func& func, const false_type&)
273 {
274  evaluator<Rhs> rhsEval(rhs);
275  typename nested_eval<Lhs,Rhs::SizeAtCompileTime>::type actual_lhs(lhs);
276  // FIXME if cols is large enough, then it might be useful to make sure that lhs is sequentially stored
277  // FIXME not very good if rhs is real and lhs complex while alpha is real too
278  const Index cols = dst.cols();
279  for (Index j=0; j<cols; ++j)
280  func(dst.col(j), rhsEval.coeff(Index(0),j) * actual_lhs);
281 }
282 
283 // Row major result
284 template<typename Dst, typename Lhs, typename Rhs, typename Func>
285 void outer_product_selector_run(Dst& dst, const Lhs &lhs, const Rhs &rhs, const Func& func, const true_type&)
286 {
287  evaluator<Lhs> lhsEval(lhs);
288  typename nested_eval<Rhs,Lhs::SizeAtCompileTime>::type actual_rhs(rhs);
289  // FIXME if rows is large enough, then it might be useful to make sure that rhs is sequentially stored
290  // FIXME not very good if lhs is real and rhs complex while alpha is real too
291  const Index rows = dst.rows();
292  for (Index i=0; i<rows; ++i)
293  func(dst.row(i), lhsEval.coeff(i,Index(0)) * actual_rhs);
294 }
295 
296 template<typename Lhs, typename Rhs>
297 struct generic_product_impl<Lhs,Rhs,DenseShape,DenseShape,OuterProduct>
298 {
299  template<typename T> struct is_row_major : internal::conditional<(int(T::Flags)&RowMajorBit), internal::true_type, internal::false_type>::type {};
300  typedef typename Product<Lhs,Rhs>::Scalar Scalar;
301 
302  // TODO it would be nice to be able to exploit our *_assign_op functors for that purpose
303  struct set { template<typename Dst, typename Src> void operator()(const Dst& dst, const Src& src) const { dst.const_cast_derived() = src; } };
304  struct add { template<typename Dst, typename Src> void operator()(const Dst& dst, const Src& src) const { dst.const_cast_derived() += src; } };
305  struct sub { template<typename Dst, typename Src> void operator()(const Dst& dst, const Src& src) const { dst.const_cast_derived() -= src; } };
306  struct adds {
307  Scalar m_scale;
308  explicit adds(const Scalar& s) : m_scale(s) {}
309  template<typename Dst, typename Src> void operator()(const Dst& dst, const Src& src) const {
310  dst.const_cast_derived() += m_scale * src;
311  }
312  };
313 
314  template<typename Dst>
315  static EIGEN_STRONG_INLINE void evalTo(Dst& dst, const Lhs& lhs, const Rhs& rhs)
316  {
317  internal::outer_product_selector_run(dst, lhs, rhs, set(), is_row_major<Dst>());
318  }
319 
320  template<typename Dst>
321  static EIGEN_STRONG_INLINE void addTo(Dst& dst, const Lhs& lhs, const Rhs& rhs)
322  {
323  internal::outer_product_selector_run(dst, lhs, rhs, add(), is_row_major<Dst>());
324  }
325 
326  template<typename Dst>
327  static EIGEN_STRONG_INLINE void subTo(Dst& dst, const Lhs& lhs, const Rhs& rhs)
328  {
329  internal::outer_product_selector_run(dst, lhs, rhs, sub(), is_row_major<Dst>());
330  }
331 
332  template<typename Dst>
333  static EIGEN_STRONG_INLINE void scaleAndAddTo(Dst& dst, const Lhs& lhs, const Rhs& rhs, const Scalar& alpha)
334  {
335  internal::outer_product_selector_run(dst, lhs, rhs, adds(alpha), is_row_major<Dst>());
336  }
337 
338 };
339 
340 
341 // This base class provides default implementations for evalTo, addTo, subTo, in terms of scaleAndAddTo
342 template<typename Lhs, typename Rhs, typename Derived>
343 struct generic_product_impl_base
344 {
345  typedef typename Product<Lhs,Rhs>::Scalar Scalar;
346 
347  template<typename Dst>
348  static EIGEN_STRONG_INLINE void evalTo(Dst& dst, const Lhs& lhs, const Rhs& rhs)
349  { dst.setZero(); scaleAndAddTo(dst, lhs, rhs, Scalar(1)); }
350 
351  template<typename Dst>
352  static EIGEN_STRONG_INLINE void addTo(Dst& dst, const Lhs& lhs, const Rhs& rhs)
353  { scaleAndAddTo(dst,lhs, rhs, Scalar(1)); }
354 
355  template<typename Dst>
356  static EIGEN_STRONG_INLINE void subTo(Dst& dst, const Lhs& lhs, const Rhs& rhs)
357  { scaleAndAddTo(dst, lhs, rhs, Scalar(-1)); }
358 
359  template<typename Dst>
360  static EIGEN_STRONG_INLINE void scaleAndAddTo(Dst& dst, const Lhs& lhs, const Rhs& rhs, const Scalar& alpha)
361  { Derived::scaleAndAddTo(dst,lhs,rhs,alpha); }
362 
363 };
364 
365 template<typename Lhs, typename Rhs>
366 struct generic_product_impl<Lhs,Rhs,DenseShape,DenseShape,GemvProduct>
367  : generic_product_impl_base<Lhs,Rhs,generic_product_impl<Lhs,Rhs,DenseShape,DenseShape,GemvProduct> >
368 {
369  typedef typename nested_eval<Lhs,1>::type LhsNested;
370  typedef typename nested_eval<Rhs,1>::type RhsNested;
371  typedef typename Product<Lhs,Rhs>::Scalar Scalar;
372  enum { Side = Lhs::IsVectorAtCompileTime ? OnTheLeft : OnTheRight };
373  typedef typename internal::remove_all<typename internal::conditional<int(Side)==OnTheRight,LhsNested,RhsNested>::type>::type MatrixType;
374 
375  template<typename Dest>
376  static EIGEN_STRONG_INLINE void scaleAndAddTo(Dest& dst, const Lhs& lhs, const Rhs& rhs, const Scalar& alpha)
377  {
378  LhsNested actual_lhs(lhs);
379  RhsNested actual_rhs(rhs);
380  internal::gemv_dense_selector<Side,
381  (int(MatrixType::Flags)&RowMajorBit) ? RowMajor : ColMajor,
382  bool(internal::blas_traits<MatrixType>::HasUsableDirectAccess)
383  >::run(actual_lhs, actual_rhs, dst, alpha);
384  }
385 };
386 
387 template<typename Lhs, typename Rhs>
388 struct generic_product_impl<Lhs,Rhs,DenseShape,DenseShape,CoeffBasedProductMode>
389 {
390  typedef typename Product<Lhs,Rhs>::Scalar Scalar;
391 
392  template<typename Dst>
393  static EIGEN_STRONG_INLINE void evalTo(Dst& dst, const Lhs& lhs, const Rhs& rhs)
394  {
395  // Same as: dst.noalias() = lhs.lazyProduct(rhs);
396  // but easier on the compiler side
397  call_assignment_no_alias(dst, lhs.lazyProduct(rhs), internal::assign_op<typename Dst::Scalar,Scalar>());
398  }
399 
400  template<typename Dst>
401  static EIGEN_STRONG_INLINE void addTo(Dst& dst, const Lhs& lhs, const Rhs& rhs)
402  {
403  // dst.noalias() += lhs.lazyProduct(rhs);
404  call_assignment_no_alias(dst, lhs.lazyProduct(rhs), internal::add_assign_op<typename Dst::Scalar,Scalar>());
405  }
406 
407  template<typename Dst>
408  static EIGEN_STRONG_INLINE void subTo(Dst& dst, const Lhs& lhs, const Rhs& rhs)
409  {
410  // dst.noalias() -= lhs.lazyProduct(rhs);
411  call_assignment_no_alias(dst, lhs.lazyProduct(rhs), internal::sub_assign_op<typename Dst::Scalar,Scalar>());
412  }
413 
414  // Catch "dst {,+,-}= (s*A)*B" and evaluate it lazily by moving out the scalar factor:
415  // dst {,+,-}= s * (A.lazyProduct(B))
416  // This is a huge benefit for heap-allocated matrix types as it save one costly allocation.
417  // For them, this strategy is also faster than simply by-passing the heap allocation through
418  // stack allocation.
419  // For fixed sizes matrices, this is less obvious, it is sometimes x2 faster, but sometimes x3 slower,
420  // and the behavior depends also a lot on the compiler... so let's be conservative and enable them for dynamic-size only,
421  // that is when coming from generic_product_impl<...,GemmProduct> in file GeneralMatrixMatrix.h
422  template<typename Dst, typename Scalar1, typename Scalar2, typename Plain1, typename Xpr2, typename Func>
423  static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
424  void eval_dynamic(Dst& dst, const CwiseBinaryOp<internal::scalar_product_op<Scalar1,Scalar2>,
425  const CwiseNullaryOp<internal::scalar_constant_op<Scalar1>, Plain1>, Xpr2>& lhs, const Rhs& rhs, const Func &func)
426  {
427  call_assignment_no_alias(dst, lhs.lhs().functor().m_other * lhs.rhs().lazyProduct(rhs), func);
428  }
429 
430  // Here, we we always have LhsT==Lhs, but we need to make it a template type to make the above
431  // overload more specialized.
432  template<typename Dst, typename LhsT, typename Func>
433  static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
434  void eval_dynamic(Dst& dst, const LhsT& lhs, const Rhs& rhs, const Func &func)
435  {
436  call_assignment_no_alias(dst, lhs.lazyProduct(rhs), func);
437  }
438 
439 
440 // template<typename Dst>
441 // static inline void scaleAndAddTo(Dst& dst, const Lhs& lhs, const Rhs& rhs, const Scalar& alpha)
442 // { dst.noalias() += alpha * lhs.lazyProduct(rhs); }
443 };
444 
445 // This specialization enforces the use of a coefficient-based evaluation strategy
446 template<typename Lhs, typename Rhs>
447 struct generic_product_impl<Lhs,Rhs,DenseShape,DenseShape,LazyCoeffBasedProductMode>
448  : generic_product_impl<Lhs,Rhs,DenseShape,DenseShape,CoeffBasedProductMode> {};
449 
450 // Case 2: Evaluate coeff by coeff
451 //
452 // This is mostly taken from CoeffBasedProduct.h
453 // The main difference is that we add an extra argument to the etor_product_*_impl::run() function
454 // for the inner dimension of the product, because evaluator object do not know their size.
455 
456 template<int Traversal, int UnrollingIndex, typename Lhs, typename Rhs, typename RetScalar>
457 struct etor_product_coeff_impl;
458 
459 template<int StorageOrder, int UnrollingIndex, typename Lhs, typename Rhs, typename Packet, int LoadMode>
460 struct etor_product_packet_impl;
461 
462 template<typename Lhs, typename Rhs, int ProductTag>
463 struct product_evaluator<Product<Lhs, Rhs, LazyProduct>, ProductTag, DenseShape, DenseShape>
464  : evaluator_base<Product<Lhs, Rhs, LazyProduct> >
465 {
466  typedef Product<Lhs, Rhs, LazyProduct> XprType;
467  typedef typename XprType::Scalar Scalar;
468  typedef typename XprType::CoeffReturnType CoeffReturnType;
469 
470  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
471  explicit product_evaluator(const XprType& xpr)
472  : m_lhs(xpr.lhs()),
473  m_rhs(xpr.rhs()),
474  m_lhsImpl(m_lhs), // FIXME the creation of the evaluator objects should result in a no-op, but check that!
475  m_rhsImpl(m_rhs), // Moreover, they are only useful for the packet path, so we could completely disable them when not needed,
476  // or perhaps declare them on the fly on the packet method... We have experiment to check what's best.
477  m_innerDim(xpr.lhs().cols())
478  {
479  EIGEN_INTERNAL_CHECK_COST_VALUE(NumTraits<Scalar>::MulCost);
480  EIGEN_INTERNAL_CHECK_COST_VALUE(NumTraits<Scalar>::AddCost);
481  EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
482 #if 0
483  std::cerr << "LhsOuterStrideBytes= " << LhsOuterStrideBytes << "\n";
484  std::cerr << "RhsOuterStrideBytes= " << RhsOuterStrideBytes << "\n";
485  std::cerr << "LhsAlignment= " << LhsAlignment << "\n";
486  std::cerr << "RhsAlignment= " << RhsAlignment << "\n";
487  std::cerr << "CanVectorizeLhs= " << CanVectorizeLhs << "\n";
488  std::cerr << "CanVectorizeRhs= " << CanVectorizeRhs << "\n";
489  std::cerr << "CanVectorizeInner= " << CanVectorizeInner << "\n";
490  std::cerr << "EvalToRowMajor= " << EvalToRowMajor << "\n";
491  std::cerr << "Alignment= " << Alignment << "\n";
492  std::cerr << "Flags= " << Flags << "\n";
493 #endif
494  }
495 
496  // Everything below here is taken from CoeffBasedProduct.h
497 
498  typedef typename internal::nested_eval<Lhs,Rhs::ColsAtCompileTime>::type LhsNested;
499  typedef typename internal::nested_eval<Rhs,Lhs::RowsAtCompileTime>::type RhsNested;
500 
501  typedef typename internal::remove_all<LhsNested>::type LhsNestedCleaned;
502  typedef typename internal::remove_all<RhsNested>::type RhsNestedCleaned;
503 
504  typedef evaluator<LhsNestedCleaned> LhsEtorType;
505  typedef evaluator<RhsNestedCleaned> RhsEtorType;
506 
507  enum {
508  RowsAtCompileTime = LhsNestedCleaned::RowsAtCompileTime,
509  ColsAtCompileTime = RhsNestedCleaned::ColsAtCompileTime,
510  InnerSize = EIGEN_SIZE_MIN_PREFER_FIXED(LhsNestedCleaned::ColsAtCompileTime, RhsNestedCleaned::RowsAtCompileTime),
511  MaxRowsAtCompileTime = LhsNestedCleaned::MaxRowsAtCompileTime,
512  MaxColsAtCompileTime = RhsNestedCleaned::MaxColsAtCompileTime
513  };
514 
515  typedef typename find_best_packet<Scalar,RowsAtCompileTime>::type LhsVecPacketType;
516  typedef typename find_best_packet<Scalar,ColsAtCompileTime>::type RhsVecPacketType;
517 
518  enum {
519 
520  LhsCoeffReadCost = LhsEtorType::CoeffReadCost,
521  RhsCoeffReadCost = RhsEtorType::CoeffReadCost,
522  CoeffReadCost = InnerSize==0 ? NumTraits<Scalar>::ReadCost
523  : InnerSize == Dynamic ? HugeCost
524  : InnerSize * (NumTraits<Scalar>::MulCost + LhsCoeffReadCost + RhsCoeffReadCost)
525  + (InnerSize - 1) * NumTraits<Scalar>::AddCost,
526 
527  Unroll = CoeffReadCost <= EIGEN_UNROLLING_LIMIT,
528 
529  LhsFlags = LhsEtorType::Flags,
530  RhsFlags = RhsEtorType::Flags,
531 
532  LhsRowMajor = LhsFlags & RowMajorBit,
533  RhsRowMajor = RhsFlags & RowMajorBit,
534 
535  LhsVecPacketSize = unpacket_traits<LhsVecPacketType>::size,
536  RhsVecPacketSize = unpacket_traits<RhsVecPacketType>::size,
537 
538  // Here, we don't care about alignment larger than the usable packet size.
539  LhsAlignment = EIGEN_PLAIN_ENUM_MIN(LhsEtorType::Alignment,LhsVecPacketSize*int(sizeof(typename LhsNestedCleaned::Scalar))),
540  RhsAlignment = EIGEN_PLAIN_ENUM_MIN(RhsEtorType::Alignment,RhsVecPacketSize*int(sizeof(typename RhsNestedCleaned::Scalar))),
541 
542  SameType = is_same<typename LhsNestedCleaned::Scalar,typename RhsNestedCleaned::Scalar>::value,
543 
544  CanVectorizeRhs = bool(RhsRowMajor) && (RhsFlags & PacketAccessBit) && (ColsAtCompileTime!=1),
545  CanVectorizeLhs = (!LhsRowMajor) && (LhsFlags & PacketAccessBit) && (RowsAtCompileTime!=1),
546 
547  EvalToRowMajor = (MaxRowsAtCompileTime==1&&MaxColsAtCompileTime!=1) ? 1
548  : (MaxColsAtCompileTime==1&&MaxRowsAtCompileTime!=1) ? 0
549  : (bool(RhsRowMajor) && !CanVectorizeLhs),
550 
551  Flags = ((unsigned int)(LhsFlags | RhsFlags) & HereditaryBits & ~RowMajorBit)
552  | (EvalToRowMajor ? RowMajorBit : 0)
553  // TODO enable vectorization for mixed types
554  | (SameType && (CanVectorizeLhs || CanVectorizeRhs) ? PacketAccessBit : 0)
555  | (XprType::IsVectorAtCompileTime ? LinearAccessBit : 0),
556 
557  LhsOuterStrideBytes = int(LhsNestedCleaned::OuterStrideAtCompileTime) * int(sizeof(typename LhsNestedCleaned::Scalar)),
558  RhsOuterStrideBytes = int(RhsNestedCleaned::OuterStrideAtCompileTime) * int(sizeof(typename RhsNestedCleaned::Scalar)),
559 
560  Alignment = bool(CanVectorizeLhs) ? (LhsOuterStrideBytes<=0 || (int(LhsOuterStrideBytes) % EIGEN_PLAIN_ENUM_MAX(1,LhsAlignment))!=0 ? 0 : LhsAlignment)
561  : bool(CanVectorizeRhs) ? (RhsOuterStrideBytes<=0 || (int(RhsOuterStrideBytes) % EIGEN_PLAIN_ENUM_MAX(1,RhsAlignment))!=0 ? 0 : RhsAlignment)
562  : 0,
563 
564  /* CanVectorizeInner deserves special explanation. It does not affect the product flags. It is not used outside
565  * of Product. If the Product itself is not a packet-access expression, there is still a chance that the inner
566  * loop of the product might be vectorized. This is the meaning of CanVectorizeInner. Since it doesn't affect
567  * the Flags, it is safe to make this value depend on ActualPacketAccessBit, that doesn't affect the ABI.
568  */
569  CanVectorizeInner = SameType
570  && LhsRowMajor
571  && (!RhsRowMajor)
572  && (LhsFlags & RhsFlags & ActualPacketAccessBit)
573  && (InnerSize % packet_traits<Scalar>::size == 0)
574  };
575 
576  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const CoeffReturnType coeff(Index row, Index col) const
577  {
578  return (m_lhs.row(row).transpose().cwiseProduct( m_rhs.col(col) )).sum();
579  }
580 
581  /* Allow index-based non-packet access. It is impossible though to allow index-based packed access,
582  * which is why we don't set the LinearAccessBit.
583  * TODO: this seems possible when the result is a vector
584  */
585  EIGEN_DEVICE_FUNC const CoeffReturnType coeff(Index index) const
586  {
587  const Index row = (RowsAtCompileTime == 1 || MaxRowsAtCompileTime==1) ? 0 : index;
588  const Index col = (RowsAtCompileTime == 1 || MaxRowsAtCompileTime==1) ? index : 0;
589  return (m_lhs.row(row).transpose().cwiseProduct( m_rhs.col(col) )).sum();
590  }
591 
592  template<int LoadMode, typename PacketType>
593  const PacketType packet(Index row, Index col) const
594  {
595  PacketType res;
596  typedef etor_product_packet_impl<bool(int(Flags)&RowMajorBit) ? RowMajor : ColMajor,
597  Unroll ? int(InnerSize) : Dynamic,
598  LhsEtorType, RhsEtorType, PacketType, LoadMode> PacketImpl;
599  PacketImpl::run(row, col, m_lhsImpl, m_rhsImpl, m_innerDim, res);
600  return res;
601  }
602 
603  template<int LoadMode, typename PacketType>
604  const PacketType packet(Index index) const
605  {
606  const Index row = (RowsAtCompileTime == 1 || MaxRowsAtCompileTime==1) ? 0 : index;
607  const Index col = (RowsAtCompileTime == 1 || MaxRowsAtCompileTime==1) ? index : 0;
608  return packet<LoadMode,PacketType>(row,col);
609  }
610 
611 protected:
612  typename internal::add_const_on_value_type<LhsNested>::type m_lhs;
613  typename internal::add_const_on_value_type<RhsNested>::type m_rhs;
614 
615  LhsEtorType m_lhsImpl;
616  RhsEtorType m_rhsImpl;
617 
618  // TODO: Get rid of m_innerDim if known at compile time
619  Index m_innerDim;
620 };
621 
622 template<typename Lhs, typename Rhs>
623 struct product_evaluator<Product<Lhs, Rhs, DefaultProduct>, LazyCoeffBasedProductMode, DenseShape, DenseShape>
624  : product_evaluator<Product<Lhs, Rhs, LazyProduct>, CoeffBasedProductMode, DenseShape, DenseShape>
625 {
626  typedef Product<Lhs, Rhs, DefaultProduct> XprType;
627  typedef Product<Lhs, Rhs, LazyProduct> BaseProduct;
628  typedef product_evaluator<BaseProduct, CoeffBasedProductMode, DenseShape, DenseShape> Base;
629  enum {
630  Flags = Base::Flags | EvalBeforeNestingBit
631  };
632  EIGEN_DEVICE_FUNC explicit product_evaluator(const XprType& xpr)
633  : Base(BaseProduct(xpr.lhs(),xpr.rhs()))
634  {}
635 };
636 
637 /****************************************
638 *** Coeff based product, Packet path ***
639 ****************************************/
640 
641 template<int UnrollingIndex, typename Lhs, typename Rhs, typename Packet, int LoadMode>
642 struct etor_product_packet_impl<RowMajor, UnrollingIndex, Lhs, Rhs, Packet, LoadMode>
643 {
644  static EIGEN_STRONG_INLINE void run(Index row, Index col, const Lhs& lhs, const Rhs& rhs, Index innerDim, Packet &res)
645  {
646  etor_product_packet_impl<RowMajor, UnrollingIndex-1, Lhs, Rhs, Packet, LoadMode>::run(row, col, lhs, rhs, innerDim, res);
647  res = pmadd(pset1<Packet>(lhs.coeff(row, Index(UnrollingIndex-1))), rhs.template packet<LoadMode,Packet>(Index(UnrollingIndex-1), col), res);
648  }
649 };
650 
651 template<int UnrollingIndex, typename Lhs, typename Rhs, typename Packet, int LoadMode>
652 struct etor_product_packet_impl<ColMajor, UnrollingIndex, Lhs, Rhs, Packet, LoadMode>
653 {
654  static EIGEN_STRONG_INLINE void run(Index row, Index col, const Lhs& lhs, const Rhs& rhs, Index innerDim, Packet &res)
655  {
656  etor_product_packet_impl<ColMajor, UnrollingIndex-1, Lhs, Rhs, Packet, LoadMode>::run(row, col, lhs, rhs, innerDim, res);
657  res = pmadd(lhs.template packet<LoadMode,Packet>(row, Index(UnrollingIndex-1)), pset1<Packet>(rhs.coeff(Index(UnrollingIndex-1), col)), res);
658  }
659 };
660 
661 template<typename Lhs, typename Rhs, typename Packet, int LoadMode>
662 struct etor_product_packet_impl<RowMajor, 1, Lhs, Rhs, Packet, LoadMode>
663 {
664  static EIGEN_STRONG_INLINE void run(Index row, Index col, const Lhs& lhs, const Rhs& rhs, Index /*innerDim*/, Packet &res)
665  {
666  res = pmul(pset1<Packet>(lhs.coeff(row, Index(0))),rhs.template packet<LoadMode,Packet>(Index(0), col));
667  }
668 };
669 
670 template<typename Lhs, typename Rhs, typename Packet, int LoadMode>
671 struct etor_product_packet_impl<ColMajor, 1, Lhs, Rhs, Packet, LoadMode>
672 {
673  static EIGEN_STRONG_INLINE void run(Index row, Index col, const Lhs& lhs, const Rhs& rhs, Index /*innerDim*/, Packet &res)
674  {
675  res = pmul(lhs.template packet<LoadMode,Packet>(row, Index(0)), pset1<Packet>(rhs.coeff(Index(0), col)));
676  }
677 };
678 
679 template<typename Lhs, typename Rhs, typename Packet, int LoadMode>
680 struct etor_product_packet_impl<RowMajor, 0, Lhs, Rhs, Packet, LoadMode>
681 {
682  static EIGEN_STRONG_INLINE void run(Index /*row*/, Index /*col*/, const Lhs& /*lhs*/, const Rhs& /*rhs*/, Index /*innerDim*/, Packet &res)
683  {
684  res = pset1<Packet>(typename unpacket_traits<Packet>::type(0));
685  }
686 };
687 
688 template<typename Lhs, typename Rhs, typename Packet, int LoadMode>
689 struct etor_product_packet_impl<ColMajor, 0, Lhs, Rhs, Packet, LoadMode>
690 {
691  static EIGEN_STRONG_INLINE void run(Index /*row*/, Index /*col*/, const Lhs& /*lhs*/, const Rhs& /*rhs*/, Index /*innerDim*/, Packet &res)
692  {
693  res = pset1<Packet>(typename unpacket_traits<Packet>::type(0));
694  }
695 };
696 
697 template<typename Lhs, typename Rhs, typename Packet, int LoadMode>
698 struct etor_product_packet_impl<RowMajor, Dynamic, Lhs, Rhs, Packet, LoadMode>
699 {
700  static EIGEN_STRONG_INLINE void run(Index row, Index col, const Lhs& lhs, const Rhs& rhs, Index innerDim, Packet& res)
701  {
702  res = pset1<Packet>(typename unpacket_traits<Packet>::type(0));
703  for(Index i = 0; i < innerDim; ++i)
704  res = pmadd(pset1<Packet>(lhs.coeff(row, i)), rhs.template packet<LoadMode,Packet>(i, col), res);
705  }
706 };
707 
708 template<typename Lhs, typename Rhs, typename Packet, int LoadMode>
709 struct etor_product_packet_impl<ColMajor, Dynamic, Lhs, Rhs, Packet, LoadMode>
710 {
711  static EIGEN_STRONG_INLINE void run(Index row, Index col, const Lhs& lhs, const Rhs& rhs, Index innerDim, Packet& res)
712  {
713  res = pset1<Packet>(typename unpacket_traits<Packet>::type(0));
714  for(Index i = 0; i < innerDim; ++i)
715  res = pmadd(lhs.template packet<LoadMode,Packet>(row, i), pset1<Packet>(rhs.coeff(i, col)), res);
716  }
717 };
718 
719 
720 /***************************************************************************
721 * Triangular products
722 ***************************************************************************/
723 template<int Mode, bool LhsIsTriangular,
724  typename Lhs, bool LhsIsVector,
725  typename Rhs, bool RhsIsVector>
726 struct triangular_product_impl;
727 
728 template<typename Lhs, typename Rhs, int ProductTag>
729 struct generic_product_impl<Lhs,Rhs,TriangularShape,DenseShape,ProductTag>
730  : generic_product_impl_base<Lhs,Rhs,generic_product_impl<Lhs,Rhs,TriangularShape,DenseShape,ProductTag> >
731 {
732  typedef typename Product<Lhs,Rhs>::Scalar Scalar;
733 
734  template<typename Dest>
735  static void scaleAndAddTo(Dest& dst, const Lhs& lhs, const Rhs& rhs, const Scalar& alpha)
736  {
737  triangular_product_impl<Lhs::Mode,true,typename Lhs::MatrixType,false,Rhs, Rhs::ColsAtCompileTime==1>
738  ::run(dst, lhs.nestedExpression(), rhs, alpha);
739  }
740 };
741 
742 template<typename Lhs, typename Rhs, int ProductTag>
743 struct generic_product_impl<Lhs,Rhs,DenseShape,TriangularShape,ProductTag>
744 : generic_product_impl_base<Lhs,Rhs,generic_product_impl<Lhs,Rhs,DenseShape,TriangularShape,ProductTag> >
745 {
746  typedef typename Product<Lhs,Rhs>::Scalar Scalar;
747 
748  template<typename Dest>
749  static void scaleAndAddTo(Dest& dst, const Lhs& lhs, const Rhs& rhs, const Scalar& alpha)
750  {
751  triangular_product_impl<Rhs::Mode,false,Lhs,Lhs::RowsAtCompileTime==1, typename Rhs::MatrixType, false>::run(dst, lhs, rhs.nestedExpression(), alpha);
752  }
753 };
754 
755 
756 /***************************************************************************
757 * SelfAdjoint products
758 ***************************************************************************/
759 template <typename Lhs, int LhsMode, bool LhsIsVector,
760  typename Rhs, int RhsMode, bool RhsIsVector>
761 struct selfadjoint_product_impl;
762 
763 template<typename Lhs, typename Rhs, int ProductTag>
764 struct generic_product_impl<Lhs,Rhs,SelfAdjointShape,DenseShape,ProductTag>
765  : generic_product_impl_base<Lhs,Rhs,generic_product_impl<Lhs,Rhs,SelfAdjointShape,DenseShape,ProductTag> >
766 {
767  typedef typename Product<Lhs,Rhs>::Scalar Scalar;
768 
769  template<typename Dest>
770  static void scaleAndAddTo(Dest& dst, const Lhs& lhs, const Rhs& rhs, const Scalar& alpha)
771  {
772  selfadjoint_product_impl<typename Lhs::MatrixType,Lhs::Mode,false,Rhs,0,Rhs::IsVectorAtCompileTime>::run(dst, lhs.nestedExpression(), rhs, alpha);
773  }
774 };
775 
776 template<typename Lhs, typename Rhs, int ProductTag>
777 struct generic_product_impl<Lhs,Rhs,DenseShape,SelfAdjointShape,ProductTag>
778 : generic_product_impl_base<Lhs,Rhs,generic_product_impl<Lhs,Rhs,DenseShape,SelfAdjointShape,ProductTag> >
779 {
780  typedef typename Product<Lhs,Rhs>::Scalar Scalar;
781 
782  template<typename Dest>
783  static void scaleAndAddTo(Dest& dst, const Lhs& lhs, const Rhs& rhs, const Scalar& alpha)
784  {
785  selfadjoint_product_impl<Lhs,0,Lhs::IsVectorAtCompileTime,typename Rhs::MatrixType,Rhs::Mode,false>::run(dst, lhs, rhs.nestedExpression(), alpha);
786  }
787 };
788 
789 
790 /***************************************************************************
791 * Diagonal products
792 ***************************************************************************/
793 
794 template<typename MatrixType, typename DiagonalType, typename Derived, int ProductOrder>
795 struct diagonal_product_evaluator_base
796  : evaluator_base<Derived>
797 {
798  typedef typename ScalarBinaryOpTraits<typename MatrixType::Scalar, typename DiagonalType::Scalar>::ReturnType Scalar;
799 public:
800  enum {
801  CoeffReadCost = NumTraits<Scalar>::MulCost + evaluator<MatrixType>::CoeffReadCost + evaluator<DiagonalType>::CoeffReadCost,
802 
803  MatrixFlags = evaluator<MatrixType>::Flags,
804  DiagFlags = evaluator<DiagonalType>::Flags,
805  _StorageOrder = MatrixFlags & RowMajorBit ? RowMajor : ColMajor,
806  _ScalarAccessOnDiag = !((int(_StorageOrder) == ColMajor && int(ProductOrder) == OnTheLeft)
807  ||(int(_StorageOrder) == RowMajor && int(ProductOrder) == OnTheRight)),
808  _SameTypes = is_same<typename MatrixType::Scalar, typename DiagonalType::Scalar>::value,
809  // FIXME currently we need same types, but in the future the next rule should be the one
810  //_Vectorizable = bool(int(MatrixFlags)&PacketAccessBit) && ((!_PacketOnDiag) || (_SameTypes && bool(int(DiagFlags)&PacketAccessBit))),
811  _Vectorizable = bool(int(MatrixFlags)&PacketAccessBit) && _SameTypes && (_ScalarAccessOnDiag || (bool(int(DiagFlags)&PacketAccessBit))),
812  _LinearAccessMask = (MatrixType::RowsAtCompileTime==1 || MatrixType::ColsAtCompileTime==1) ? LinearAccessBit : 0,
813  Flags = ((HereditaryBits|_LinearAccessMask) & (unsigned int)(MatrixFlags)) | (_Vectorizable ? PacketAccessBit : 0),
814  Alignment = evaluator<MatrixType>::Alignment,
815 
816  AsScalarProduct = (DiagonalType::SizeAtCompileTime==1)
817  || (DiagonalType::SizeAtCompileTime==Dynamic && MatrixType::RowsAtCompileTime==1 && ProductOrder==OnTheLeft)
818  || (DiagonalType::SizeAtCompileTime==Dynamic && MatrixType::ColsAtCompileTime==1 && ProductOrder==OnTheRight)
819  };
820 
821  diagonal_product_evaluator_base(const MatrixType &mat, const DiagonalType &diag)
822  : m_diagImpl(diag), m_matImpl(mat)
823  {
824  EIGEN_INTERNAL_CHECK_COST_VALUE(NumTraits<Scalar>::MulCost);
825  EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
826  }
827 
828  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar coeff(Index idx) const
829  {
830  if(AsScalarProduct)
831  return m_diagImpl.coeff(0) * m_matImpl.coeff(idx);
832  else
833  return m_diagImpl.coeff(idx) * m_matImpl.coeff(idx);
834  }
835 
836 protected:
837  template<int LoadMode,typename PacketType>
838  EIGEN_STRONG_INLINE PacketType packet_impl(Index row, Index col, Index id, internal::true_type) const
839  {
840  return internal::pmul(m_matImpl.template packet<LoadMode,PacketType>(row, col),
841  internal::pset1<PacketType>(m_diagImpl.coeff(id)));
842  }
843 
844  template<int LoadMode,typename PacketType>
845  EIGEN_STRONG_INLINE PacketType packet_impl(Index row, Index col, Index id, internal::false_type) const
846  {
847  enum {
848  InnerSize = (MatrixType::Flags & RowMajorBit) ? MatrixType::ColsAtCompileTime : MatrixType::RowsAtCompileTime,
849  DiagonalPacketLoadMode = EIGEN_PLAIN_ENUM_MIN(LoadMode,((InnerSize%16) == 0) ? int(Aligned16) : int(evaluator<DiagonalType>::Alignment)) // FIXME hardcoded 16!!
850  };
851  return internal::pmul(m_matImpl.template packet<LoadMode,PacketType>(row, col),
852  m_diagImpl.template packet<DiagonalPacketLoadMode,PacketType>(id));
853  }
854 
855  evaluator<DiagonalType> m_diagImpl;
856  evaluator<MatrixType> m_matImpl;
857 };
858 
859 // diagonal * dense
860 template<typename Lhs, typename Rhs, int ProductKind, int ProductTag>
861 struct product_evaluator<Product<Lhs, Rhs, ProductKind>, ProductTag, DiagonalShape, DenseShape>
862  : diagonal_product_evaluator_base<Rhs, typename Lhs::DiagonalVectorType, Product<Lhs, Rhs, LazyProduct>, OnTheLeft>
863 {
864  typedef diagonal_product_evaluator_base<Rhs, typename Lhs::DiagonalVectorType, Product<Lhs, Rhs, LazyProduct>, OnTheLeft> Base;
865  using Base::m_diagImpl;
866  using Base::m_matImpl;
867  using Base::coeff;
868  typedef typename Base::Scalar Scalar;
869 
870  typedef Product<Lhs, Rhs, ProductKind> XprType;
871  typedef typename XprType::PlainObject PlainObject;
872 
873  enum {
874  StorageOrder = int(Rhs::Flags) & RowMajorBit ? RowMajor : ColMajor
875  };
876 
877  EIGEN_DEVICE_FUNC explicit product_evaluator(const XprType& xpr)
878  : Base(xpr.rhs(), xpr.lhs().diagonal())
879  {
880  }
881 
882  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar coeff(Index row, Index col) const
883  {
884  return m_diagImpl.coeff(row) * m_matImpl.coeff(row, col);
885  }
886 
887 #ifndef __CUDACC__
888  template<int LoadMode,typename PacketType>
889  EIGEN_STRONG_INLINE PacketType packet(Index row, Index col) const
890  {
891  // FIXME: NVCC used to complain about the template keyword, but we have to check whether this is still the case.
892  // See also similar calls below.
893  return this->template packet_impl<LoadMode,PacketType>(row,col, row,
894  typename internal::conditional<int(StorageOrder)==RowMajor, internal::true_type, internal::false_type>::type());
895  }
896 
897  template<int LoadMode,typename PacketType>
898  EIGEN_STRONG_INLINE PacketType packet(Index idx) const
899  {
900  return packet<LoadMode,PacketType>(int(StorageOrder)==ColMajor?idx:0,int(StorageOrder)==ColMajor?0:idx);
901  }
902 #endif
903 };
904 
905 // dense * diagonal
906 template<typename Lhs, typename Rhs, int ProductKind, int ProductTag>
907 struct product_evaluator<Product<Lhs, Rhs, ProductKind>, ProductTag, DenseShape, DiagonalShape>
908  : diagonal_product_evaluator_base<Lhs, typename Rhs::DiagonalVectorType, Product<Lhs, Rhs, LazyProduct>, OnTheRight>
909 {
910  typedef diagonal_product_evaluator_base<Lhs, typename Rhs::DiagonalVectorType, Product<Lhs, Rhs, LazyProduct>, OnTheRight> Base;
911  using Base::m_diagImpl;
912  using Base::m_matImpl;
913  using Base::coeff;
914  typedef typename Base::Scalar Scalar;
915 
916  typedef Product<Lhs, Rhs, ProductKind> XprType;
917  typedef typename XprType::PlainObject PlainObject;
918 
919  enum { StorageOrder = int(Lhs::Flags) & RowMajorBit ? RowMajor : ColMajor };
920 
921  EIGEN_DEVICE_FUNC explicit product_evaluator(const XprType& xpr)
922  : Base(xpr.lhs(), xpr.rhs().diagonal())
923  {
924  }
925 
926  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar coeff(Index row, Index col) const
927  {
928  return m_matImpl.coeff(row, col) * m_diagImpl.coeff(col);
929  }
930 
931 #ifndef __CUDACC__
932  template<int LoadMode,typename PacketType>
933  EIGEN_STRONG_INLINE PacketType packet(Index row, Index col) const
934  {
935  return this->template packet_impl<LoadMode,PacketType>(row,col, col,
936  typename internal::conditional<int(StorageOrder)==ColMajor, internal::true_type, internal::false_type>::type());
937  }
938 
939  template<int LoadMode,typename PacketType>
940  EIGEN_STRONG_INLINE PacketType packet(Index idx) const
941  {
942  return packet<LoadMode,PacketType>(int(StorageOrder)==ColMajor?idx:0,int(StorageOrder)==ColMajor?0:idx);
943  }
944 #endif
945 };
946 
947 /***************************************************************************
948 * Products with permutation matrices
949 ***************************************************************************/
950 
956 template<typename ExpressionType, int Side, bool Transposed, typename ExpressionShape>
957 struct permutation_matrix_product;
958 
959 template<typename ExpressionType, int Side, bool Transposed>
960 struct permutation_matrix_product<ExpressionType, Side, Transposed, DenseShape>
961 {
962  typedef typename nested_eval<ExpressionType, 1>::type MatrixType;
963  typedef typename remove_all<MatrixType>::type MatrixTypeCleaned;
964 
965  template<typename Dest, typename PermutationType>
966  static inline void run(Dest& dst, const PermutationType& perm, const ExpressionType& xpr)
967  {
968  MatrixType mat(xpr);
969  const Index n = Side==OnTheLeft ? mat.rows() : mat.cols();
970  // FIXME we need an is_same for expression that is not sensitive to constness. For instance
971  // is_same_xpr<Block<const Matrix>, Block<Matrix> >::value should be true.
972  //if(is_same<MatrixTypeCleaned,Dest>::value && extract_data(dst) == extract_data(mat))
973  if(is_same_dense(dst, mat))
974  {
975  // apply the permutation inplace
976  Matrix<bool,PermutationType::RowsAtCompileTime,1,0,PermutationType::MaxRowsAtCompileTime> mask(perm.size());
977  mask.fill(false);
978  Index r = 0;
979  while(r < perm.size())
980  {
981  // search for the next seed
982  while(r<perm.size() && mask[r]) r++;
983  if(r>=perm.size())
984  break;
985  // we got one, let's follow it until we are back to the seed
986  Index k0 = r++;
987  Index kPrev = k0;
988  mask.coeffRef(k0) = true;
989  for(Index k=perm.indices().coeff(k0); k!=k0; k=perm.indices().coeff(k))
990  {
991  Block<Dest, Side==OnTheLeft ? 1 : Dest::RowsAtCompileTime, Side==OnTheRight ? 1 : Dest::ColsAtCompileTime>(dst, k)
992  .swap(Block<Dest, Side==OnTheLeft ? 1 : Dest::RowsAtCompileTime, Side==OnTheRight ? 1 : Dest::ColsAtCompileTime>
993  (dst,((Side==OnTheLeft) ^ Transposed) ? k0 : kPrev));
994 
995  mask.coeffRef(k) = true;
996  kPrev = k;
997  }
998  }
999  }
1000  else
1001  {
1002  for(Index i = 0; i < n; ++i)
1003  {
1004  Block<Dest, Side==OnTheLeft ? 1 : Dest::RowsAtCompileTime, Side==OnTheRight ? 1 : Dest::ColsAtCompileTime>
1005  (dst, ((Side==OnTheLeft) ^ Transposed) ? perm.indices().coeff(i) : i)
1006 
1007  =
1008 
1009  Block<const MatrixTypeCleaned,Side==OnTheLeft ? 1 : MatrixTypeCleaned::RowsAtCompileTime,Side==OnTheRight ? 1 : MatrixTypeCleaned::ColsAtCompileTime>
1010  (mat, ((Side==OnTheRight) ^ Transposed) ? perm.indices().coeff(i) : i);
1011  }
1012  }
1013  }
1014 };
1015 
1016 template<typename Lhs, typename Rhs, int ProductTag, typename MatrixShape>
1017 struct generic_product_impl<Lhs, Rhs, PermutationShape, MatrixShape, ProductTag>
1018 {
1019  template<typename Dest>
1020  static void evalTo(Dest& dst, const Lhs& lhs, const Rhs& rhs)
1021  {
1022  permutation_matrix_product<Rhs, OnTheLeft, false, MatrixShape>::run(dst, lhs, rhs);
1023  }
1024 };
1025 
1026 template<typename Lhs, typename Rhs, int ProductTag, typename MatrixShape>
1027 struct generic_product_impl<Lhs, Rhs, MatrixShape, PermutationShape, ProductTag>
1028 {
1029  template<typename Dest>
1030  static void evalTo(Dest& dst, const Lhs& lhs, const Rhs& rhs)
1031  {
1032  permutation_matrix_product<Lhs, OnTheRight, false, MatrixShape>::run(dst, rhs, lhs);
1033  }
1034 };
1035 
1036 template<typename Lhs, typename Rhs, int ProductTag, typename MatrixShape>
1037 struct generic_product_impl<Inverse<Lhs>, Rhs, PermutationShape, MatrixShape, ProductTag>
1038 {
1039  template<typename Dest>
1040  static void evalTo(Dest& dst, const Inverse<Lhs>& lhs, const Rhs& rhs)
1041  {
1042  permutation_matrix_product<Rhs, OnTheLeft, true, MatrixShape>::run(dst, lhs.nestedExpression(), rhs);
1043  }
1044 };
1045 
1046 template<typename Lhs, typename Rhs, int ProductTag, typename MatrixShape>
1047 struct generic_product_impl<Lhs, Inverse<Rhs>, MatrixShape, PermutationShape, ProductTag>
1048 {
1049  template<typename Dest>
1050  static void evalTo(Dest& dst, const Lhs& lhs, const Inverse<Rhs>& rhs)
1051  {
1052  permutation_matrix_product<Lhs, OnTheRight, true, MatrixShape>::run(dst, rhs.nestedExpression(), lhs);
1053  }
1054 };
1055 
1056 
1057 /***************************************************************************
1058 * Products with transpositions matrices
1059 ***************************************************************************/
1060 
1061 // FIXME could we unify Transpositions and Permutation into a single "shape"??
1062 
1067 template<typename ExpressionType, int Side, bool Transposed, typename ExpressionShape>
1068 struct transposition_matrix_product
1069 {
1070  typedef typename nested_eval<ExpressionType, 1>::type MatrixType;
1071  typedef typename remove_all<MatrixType>::type MatrixTypeCleaned;
1072 
1073  template<typename Dest, typename TranspositionType>
1074  static inline void run(Dest& dst, const TranspositionType& tr, const ExpressionType& xpr)
1075  {
1076  MatrixType mat(xpr);
1077  typedef typename TranspositionType::StorageIndex StorageIndex;
1078  const Index size = tr.size();
1079  StorageIndex j = 0;
1080 
1081  if(!is_same_dense(dst,mat))
1082  dst = mat;
1083 
1084  for(Index k=(Transposed?size-1:0) ; Transposed?k>=0:k<size ; Transposed?--k:++k)
1085  if(Index(j=tr.coeff(k))!=k)
1086  {
1087  if(Side==OnTheLeft) dst.row(k).swap(dst.row(j));
1088  else if(Side==OnTheRight) dst.col(k).swap(dst.col(j));
1089  }
1090  }
1091 };
1092 
1093 template<typename Lhs, typename Rhs, int ProductTag, typename MatrixShape>
1094 struct generic_product_impl<Lhs, Rhs, TranspositionsShape, MatrixShape, ProductTag>
1095 {
1096  template<typename Dest>
1097  static void evalTo(Dest& dst, const Lhs& lhs, const Rhs& rhs)
1098  {
1099  transposition_matrix_product<Rhs, OnTheLeft, false, MatrixShape>::run(dst, lhs, rhs);
1100  }
1101 };
1102 
1103 template<typename Lhs, typename Rhs, int ProductTag, typename MatrixShape>
1104 struct generic_product_impl<Lhs, Rhs, MatrixShape, TranspositionsShape, ProductTag>
1105 {
1106  template<typename Dest>
1107  static void evalTo(Dest& dst, const Lhs& lhs, const Rhs& rhs)
1108  {
1109  transposition_matrix_product<Lhs, OnTheRight, false, MatrixShape>::run(dst, rhs, lhs);
1110  }
1111 };
1112 
1113 
1114 template<typename Lhs, typename Rhs, int ProductTag, typename MatrixShape>
1115 struct generic_product_impl<Transpose<Lhs>, Rhs, TranspositionsShape, MatrixShape, ProductTag>
1116 {
1117  template<typename Dest>
1118  static void evalTo(Dest& dst, const Transpose<Lhs>& lhs, const Rhs& rhs)
1119  {
1120  transposition_matrix_product<Rhs, OnTheLeft, true, MatrixShape>::run(dst, lhs.nestedExpression(), rhs);
1121  }
1122 };
1123 
1124 template<typename Lhs, typename Rhs, int ProductTag, typename MatrixShape>
1125 struct generic_product_impl<Lhs, Transpose<Rhs>, MatrixShape, TranspositionsShape, ProductTag>
1126 {
1127  template<typename Dest>
1128  static void evalTo(Dest& dst, const Lhs& lhs, const Transpose<Rhs>& rhs)
1129  {
1130  transposition_matrix_product<Lhs, OnTheRight, true, MatrixShape>::run(dst, rhs.nestedExpression(), lhs);
1131  }
1132 };
1133 
1134 } // end namespace internal
1135 
1136 } // end namespace Eigen
1137 
1138 #endif // EIGEN_PRODUCT_EVALUATORS_H
Definition: Constants.h:320
const int HugeCost
Definition: Constants.h:39
Definition: Constants.h:335
Definition: Constants.h:230
Namespace containing all symbols from the Eigen library.
Definition: Core:309
const unsigned int RowMajorBit
Definition: Constants.h:61
const unsigned int PacketAccessBit
Definition: Constants.h:89
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:33
Definition: Constants.h:333
Definition: Eigen_Colamd.h:50
Definition: Constants.h:322
const int Dynamic
Definition: Constants.h:21
const unsigned int EvalBeforeNestingBit
Definition: Constants.h:65
const unsigned int ActualPacketAccessBit
Definition: Constants.h:100
const unsigned int LinearAccessBit
Definition: Constants.h:125