13#ifndef EIGEN_PRODUCTEVALUATORS_H
14#define EIGEN_PRODUCTEVALUATORS_H
17#include "./InternalHeaderCheck.h"
23#pragma warning(disable : 4804)
38template <
typename Lhs,
typename Rhs,
int Options>
39struct evaluator<Product<Lhs, Rhs, Options>> :
public product_evaluator<Product<Lhs, Rhs, Options>> {
40 using XprType = Product<Lhs, Rhs, Options>;
41 using Base = product_evaluator<XprType>;
43 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
explicit evaluator(
const XprType& xpr) : Base(xpr) {}
47template <typename Lhs, typename Shape = typename evaluator_traits<Lhs>::Shape>
48struct product_can_fold_scalar
49 : bool_constant<std::is_same<Shape, DenseShape>::value || std::is_same<Shape, SparseShape>::value ||
50 std::is_same<Shape, DiagonalShape>::value || std::is_same<Shape, SelfAdjointShape>::value> {};
52template <
typename Lhs>
53struct product_can_fold_scalar<Lhs, TriangularShape> : bool_constant<(int(Lhs::Mode) & int(UnitDiag)) == 0> {};
55template <typename Lhs, typename Rhs, typename LhsShape = typename evaluator_traits<Lhs>::Shape,
56 typename RhsShape =
typename evaluator_traits<Rhs>::Shape>
57struct product_can_scale_unit_triangle : std::false_type {};
60template <
typename Lhs,
typename Rhs>
61struct product_can_scale_unit_triangle<Lhs, Rhs, TriangularShape, DenseShape>
63 (int(Lhs::Mode) & int(UnitDiag)) != 0 && std::is_same<typename Lhs::Scalar, typename Rhs::Scalar>::value &&
64 (std::is_floating_point<typename Lhs::Scalar>::value ||
65 (std::is_same<typename Lhs::Scalar, std::complex<typename NumTraits<typename Lhs::Scalar>::Real>>::value &&
66 std::is_floating_point<typename NumTraits<typename Lhs::Scalar>::Real>::value)) &&
67 !blas_traits<typename Lhs::MatrixType>::HasScalarFactor && !blas_traits<Rhs>::HasScalarFactor> {};
69template <
typename Lhs,
typename Rhs>
70struct product_can_scale_unit_triangle<Lhs, Rhs, TriangularShape, DiagonalShape>
71 : product_can_scale_unit_triangle<Lhs, Rhs, TriangularShape, DenseShape> {};
74template <typename Factor, typename Lhs, typename Shape = typename evaluator_traits<Lhs>::Shape>
75struct product_can_fold_scalar_factor : product_can_fold_scalar<Lhs, Shape> {};
77template <
typename Factor,
typename Lhs>
78struct product_can_fold_scalar_factor<Factor, Lhs, TriangularShape>
79 : bool_constant<product_can_fold_scalar<Lhs>::value && std::is_convertible<Factor, typename Lhs::Scalar>::value> {};
81template <
typename Factor,
typename Lhs>
82struct product_can_fold_scalar_factor<Factor, Lhs, SelfAdjointShape>
83 : bool_constant<product_can_fold_scalar<Lhs>::value && std::is_convertible<Factor, typename Lhs::Scalar>::value> {};
86template <
typename Factor,
typename Lhs,
typename Rhs>
87struct product_evaluator_can_fold_scalar
88 : bool_constant<product_can_fold_scalar_factor<Factor, Lhs>::value &&
89 !(std::is_same<typename evaluator_traits<Lhs>::Shape, SelfAdjointShape>::value &&
90 std::is_same<typename evaluator_traits<Rhs>::Shape, DiagonalShape>::value)> {};
92template <
typename Xpr,
bool Fold = product_evaluator_can_fold_scalar<
typename Xpr::Lhs::Scalar,
typename Xpr::Rhs::Lhs,
93 typename Xpr::Rhs::Rhs>::value>
94struct scaled_product_evaluator_type {
97 std::conditional_t<product_can_fold_scalar_factor<typename Xpr::Lhs::Scalar, typename Xpr::Rhs::Lhs>::value,
98 evaluator<EvalToTemp<Xpr>>, binary_evaluator<Xpr>>;
101template <
typename Xpr>
102struct scaled_product_evaluator_type<Xpr, true> {
104 evaluator<remove_all_t<decltype((std::declval<Xpr>().lhs().functor().m_other * std::declval<Xpr>().rhs().lhs()) *
105 std::declval<Xpr>().rhs().rhs())>>;
110template <
typename Lhs,
typename Rhs,
typename Scalar1,
typename Scalar2,
typename Plain1>
111struct evaluator_assume_aliasing<CwiseBinaryOp<internal::scalar_product_op<Scalar1, Scalar2>,
112 const CwiseNullaryOp<internal::scalar_constant_op<Scalar1>, Plain1>,
113 const Product<Lhs, Rhs, DefaultProduct>>> : std::true_type {};
114template <
typename Lhs,
typename Rhs,
typename Scalar1,
typename Scalar2,
typename Plain1>
115struct evaluator<CwiseBinaryOp<internal::scalar_product_op<Scalar1, Scalar2>,
116 const CwiseNullaryOp<internal::scalar_constant_op<Scalar1>, Plain1>,
117 const Product<Lhs, Rhs, DefaultProduct>>>
118 : scaled_product_evaluator_type<CwiseBinaryOp<internal::scalar_product_op<Scalar1, Scalar2>,
119 const CwiseNullaryOp<internal::scalar_constant_op<Scalar1>, Plain1>,
120 const Product<Lhs, Rhs, DefaultProduct>>>::type {
121 using XprType = CwiseBinaryOp<internal::scalar_product_op<Scalar1, Scalar2>,
122 const CwiseNullaryOp<internal::scalar_constant_op<Scalar1>, Plain1>,
123 const Product<Lhs, Rhs, DefaultProduct>>;
124 using Base =
typename scaled_product_evaluator_type<XprType>::type;
126 template <bool Fold = product_evaluator_can_fold_scalar<Scalar1, Lhs, Rhs>::value, std::enable_if_t<Fold, int> = 0>
127 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
explicit evaluator(
const XprType& xpr)
128 : Base(xpr.lhs().functor().m_other * xpr.rhs().lhs() * xpr.rhs().rhs()) {}
130 template <bool Fold = product_evaluator_can_fold_scalar<Scalar1, Lhs, Rhs>::value, std::enable_if_t<!Fold, int> = 0>
131 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
explicit evaluator(
const XprType& xpr) : Base(xpr) {}
134template <
typename Lhs,
typename Rhs,
int DiagIndex>
135struct evaluator<Diagonal<const Product<Lhs, Rhs, DefaultProduct>, DiagIndex>>
136 :
public evaluator<Diagonal<const Product<Lhs, Rhs, LazyProduct>, DiagIndex>> {
137 using XprType = Diagonal<const Product<Lhs, Rhs, DefaultProduct>, DiagIndex>;
138 using Base = evaluator<Diagonal<const Product<Lhs, Rhs, LazyProduct>, DiagIndex>>;
140 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
explicit evaluator(
const XprType& xpr)
141 : Base(Diagonal<const Product<Lhs, Rhs, LazyProduct>, DiagIndex>(
142 Product<Lhs, Rhs, LazyProduct>(xpr.nestedExpression().lhs(), xpr.nestedExpression().rhs()), xpr.index())) {}
148template <typename Lhs, typename Rhs, typename LhsShape = typename evaluator_traits<Lhs>::Shape,
149 typename RhsShape =
typename evaluator_traits<Rhs>::Shape,
150 int ProductType = internal::product_type<Lhs, Rhs>::value>
151struct generic_product_impl;
153template <
typename Lhs,
typename Rhs>
154struct evaluator_assume_aliasing<Product<Lhs, Rhs, DefaultProduct>> : std::true_type {};
158template <
typename Lhs,
typename Rhs,
int Options,
int ProductTag,
typename LhsShape,
typename RhsShape>
159struct product_evaluator<Product<Lhs, Rhs, Options>, ProductTag, LhsShape, RhsShape>
160 :
public evaluator<typename Product<Lhs, Rhs, Options>::PlainObject> {
161 using XprType = Product<Lhs, Rhs, Options>;
162 using PlainObject =
typename XprType::PlainObject;
163 using Base = evaluator<PlainObject>;
166 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
explicit product_evaluator(
const XprType& xpr)
167 : m_result(xpr.rows(), xpr.cols()) {
168 internal::construct_at<Base>(
this, m_result);
183 generic_product_impl<Lhs, Rhs, LhsShape, RhsShape, ProductTag>::evalTo(m_result, xpr.lhs(), xpr.rhs());
187 PlainObject m_result;
194template <
typename DstXprType,
typename Lhs,
typename Rhs,
int Options,
typename Scalar>
195struct Assignment<DstXprType, Product<Lhs, Rhs, Options>, internal::assign_op<Scalar, Scalar>, Dense2Dense,
196 std::enable_if_t<(Options == DefaultProduct || Options == AliasFreeProduct)>> {
197 using SrcXprType = Product<Lhs, Rhs, Options>;
198 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void run(DstXprType& dst,
const SrcXprType& src,
199 const internal::assign_op<Scalar, Scalar>& func) {
200 resize_if_allowed(dst, src, func);
202 generic_product_impl<Lhs, Rhs>::evalTo(dst, src.lhs(), src.rhs());
207template <
typename DstXprType,
typename Lhs,
typename Rhs,
int Options,
typename Scalar>
208struct Assignment<DstXprType, Product<Lhs, Rhs, Options>, internal::add_assign_op<Scalar, Scalar>, Dense2Dense,
209 std::enable_if_t<(Options == DefaultProduct || Options == AliasFreeProduct)>> {
210 using SrcXprType = Product<Lhs, Rhs, Options>;
211 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void run(DstXprType& dst,
const SrcXprType& src,
212 const internal::add_assign_op<Scalar, Scalar>&) {
213 EIGEN_INTERNAL_ASSERT_SAME_SHAPE(dst, src);
215 generic_product_impl<Lhs, Rhs>::addTo(dst, src.lhs(), src.rhs());
220template <
typename DstXprType,
typename Lhs,
typename Rhs,
int Options,
typename Scalar>
221struct Assignment<DstXprType, Product<Lhs, Rhs, Options>, internal::sub_assign_op<Scalar, Scalar>, Dense2Dense,
222 std::enable_if_t<(Options == DefaultProduct || Options == AliasFreeProduct)>> {
223 using SrcXprType = Product<Lhs, Rhs, Options>;
224 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void run(DstXprType& dst,
const SrcXprType& src,
225 const internal::sub_assign_op<Scalar, Scalar>&) {
226 EIGEN_INTERNAL_ASSERT_SAME_SHAPE(dst, src);
228 generic_product_impl<Lhs, Rhs>::subTo(dst, src.lhs(), src.rhs());
235template <
typename DstXprType,
typename Lhs,
typename Rhs,
typename AssignFunc,
typename Scalar,
typename ScalarBis,
237struct Assignment<DstXprType,
238 CwiseBinaryOp<internal::scalar_product_op<ScalarBis, Scalar>,
239 const CwiseNullaryOp<internal::scalar_constant_op<ScalarBis>, Plain>,
240 const Product<Lhs, Rhs, DefaultProduct>>,
241 AssignFunc, Dense2Dense, std::enable_if_t<product_can_fold_scalar_factor<ScalarBis, Lhs>::value>> {
242 using SrcXprType = CwiseBinaryOp<internal::scalar_product_op<ScalarBis, Scalar>,
243 const CwiseNullaryOp<internal::scalar_constant_op<ScalarBis>, Plain>,
244 const Product<Lhs, Rhs, DefaultProduct>>;
245 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void run(DstXprType& dst,
const SrcXprType& src,
246 const AssignFunc& func) {
247 call_assignment_no_alias(dst, (src.lhs().functor().m_other * src.rhs().lhs()) * src.rhs().rhs(), func);
252template <
typename Dst,
typename Lhs,
typename Rhs,
typename Func,
typename Scalar,
typename Scale,
typename Plain>
255 CwiseBinaryOp<scalar_product_op<Scale, Scalar>, const CwiseNullaryOp<scalar_constant_op<Scale>, Plain>,
256 const Product<Lhs, Rhs, DefaultProduct>>,
259 product_can_scale_unit_triangle<Lhs, Rhs>::value && std::is_same<Scale, Scalar>::value &&
260 std::is_same<typename Lhs::Scalar, Scalar>::value && std::is_same<typename Dst::Scalar, Scalar>::value &&
261 std::is_same<typename traits<Dst>::XprKind, MatrixXpr>::value && ((int(Dst::Flags) & DirectAccessBit) != 0) &&
262 (Rhs::ColsAtCompileTime != 1 || Dst::IsVectorAtCompileTime) &&
263 (std::is_same<Func, assign_op<Scalar, Scalar>>::value ||
264 std::is_same<Func, add_assign_op<Scalar, Scalar>>::value ||
265 std::is_same<Func, sub_assign_op<Scalar, Scalar>>::value)>> {
266 using Src = CwiseBinaryOp<scalar_product_op<Scale, Scalar>,
const CwiseNullaryOp<scalar_constant_op<Scale>, Plain>,
267 const Product<Lhs, Rhs, DefaultProduct>>;
268 static EIGEN_STRONG_INLINE
void run(Dst& dst,
const Src& src,
const Func& func) {
269 Scalar alpha = src.lhs().functor().m_other;
270 resize_if_allowed(dst, src, func);
271 EIGEN_IF_CONSTEXPR ((std::is_same<Func, assign_op<Scalar, Scalar>>::value)) dst.setZero();
272 if (numext::is_exactly_zero(alpha))
return;
273 EIGEN_IF_CONSTEXPR ((std::is_same<Func, sub_assign_op<Scalar, Scalar>>::value)) alpha = -alpha;
274 generic_product_impl<Lhs, Rhs>::scaleAndAddTo(dst, src.rhs().lhs(), src.rhs().rhs(), alpha);
282template <
typename OtherXpr,
typename Lhs,
typename Rhs>
283struct evaluator_assume_aliasing<
285 internal::scalar_sum_op<typename OtherXpr::Scalar, typename Product<Lhs, Rhs, DefaultProduct>::Scalar>,
286 const OtherXpr, const Product<Lhs, Rhs, DefaultProduct>>,
287 DenseShape> : std::true_type {};
289template <
typename OtherXpr,
typename Lhs,
typename Rhs>
290struct evaluator_assume_aliasing<
292 internal::scalar_difference_op<typename OtherXpr::Scalar, typename Product<Lhs, Rhs, DefaultProduct>::Scalar>,
293 const OtherXpr, const Product<Lhs, Rhs, DefaultProduct>>,
294 DenseShape> : std::true_type {};
296template <
typename DstXprType,
typename OtherXpr,
typename ProductType,
typename Func1,
typename Func2>
297struct assignment_from_xpr_op_product {
298 template <
typename SrcXprType,
typename InitialFunc>
299 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void run(DstXprType& dst,
const SrcXprType& src,
300 const InitialFunc& ) {
301 call_assignment_no_alias(dst, src.lhs(), Func1());
302 call_assignment_no_alias(dst, src.rhs(), Func2());
306#define EIGEN_CATCH_ASSIGN_XPR_OP_PRODUCT(ASSIGN_OP, BINOP, ASSIGN_OP2) \
307 template <typename DstXprType, typename OtherXpr, typename Lhs, typename Rhs, typename DstScalar, \
308 typename SrcScalar, typename OtherScalar, typename ProdScalar> \
309 struct Assignment<DstXprType, \
310 CwiseBinaryOp<internal::BINOP<OtherScalar, ProdScalar>, const OtherXpr, \
311 const Product<Lhs, Rhs, DefaultProduct>>, \
312 internal::ASSIGN_OP<DstScalar, SrcScalar>, Dense2Dense> \
313 : assignment_from_xpr_op_product<DstXprType, OtherXpr, Product<Lhs, Rhs, DefaultProduct>, \
314 internal::ASSIGN_OP<DstScalar, OtherScalar>, \
315 internal::ASSIGN_OP2<DstScalar, ProdScalar>> {}
317EIGEN_CATCH_ASSIGN_XPR_OP_PRODUCT(assign_op, scalar_sum_op, add_assign_op);
318EIGEN_CATCH_ASSIGN_XPR_OP_PRODUCT(add_assign_op, scalar_sum_op, add_assign_op);
319EIGEN_CATCH_ASSIGN_XPR_OP_PRODUCT(sub_assign_op, scalar_sum_op, sub_assign_op);
321EIGEN_CATCH_ASSIGN_XPR_OP_PRODUCT(assign_op, scalar_difference_op, sub_assign_op);
322EIGEN_CATCH_ASSIGN_XPR_OP_PRODUCT(add_assign_op, scalar_difference_op, sub_assign_op);
323EIGEN_CATCH_ASSIGN_XPR_OP_PRODUCT(sub_assign_op, scalar_difference_op, add_assign_op);
327template <
typename Lhs,
typename Rhs>
328struct generic_product_impl<Lhs, Rhs, DenseShape, DenseShape, InnerProduct> {
329 using impl = inner_product_dispatch<Lhs, Rhs, false>;
330 template <
typename Dst>
331 static EIGEN_DEVICE_FUNC
constexpr EIGEN_STRONG_INLINE
void evalTo(Dst& dst,
const Lhs& lhs,
const Rhs& rhs) {
332 dst.coeffRef(0, 0) = impl::run(lhs, rhs);
335 template <
typename Dst>
336 static EIGEN_DEVICE_FUNC
constexpr EIGEN_STRONG_INLINE
void addTo(Dst& dst,
const Lhs& lhs,
const Rhs& rhs) {
337 dst.coeffRef(0, 0) += impl::run(lhs, rhs);
340 template <
typename Dst>
341 static EIGEN_DEVICE_FUNC
constexpr EIGEN_STRONG_INLINE
void subTo(Dst& dst,
const Lhs& lhs,
const Rhs& rhs) {
342 dst.coeffRef(0, 0) -= impl::run(lhs, rhs);
351template <
typename Dst,
typename Lhs,
typename Rhs,
typename Func>
352void EIGEN_DEVICE_FUNC outer_product_selector_run(Dst& dst,
const Lhs& lhs,
const Rhs& rhs,
const Func& func,
353 const std::false_type&) {
354 evaluator<Rhs> rhsEval(rhs);
355 ei_declare_local_nested_eval(Lhs, lhs, Rhs::SizeAtCompileTime, actual_lhs);
358 const Index cols = dst.cols();
359 for (Index j = 0; j < cols; ++j) func(dst.col(j), actual_lhs * rhsEval.coeff(Index(0), j));
363template <
typename Dst,
typename Lhs,
typename Rhs,
typename Func>
364void EIGEN_DEVICE_FUNC outer_product_selector_run(Dst& dst,
const Lhs& lhs,
const Rhs& rhs,
const Func& func,
365 const std::true_type&) {
366 evaluator<Lhs> lhsEval(lhs);
367 ei_declare_local_nested_eval(Rhs, rhs, Lhs::SizeAtCompileTime, actual_rhs);
370 const Index rows = dst.rows();
371 for (Index i = 0; i < rows; ++i) func(dst.row(i), lhsEval.coeff(i, Index(0)) * actual_rhs);
374template <
typename Dst>
375EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
bool outer_product_use_small_assignment(
const Dst& dst) {
376 return dst.rows() <= 16 && dst.cols() <= 16;
379template <
bool ApplyScale = true,
typename Dst,
typename Lhs,
typename Rhs,
typename Func,
typename Scalar>
380void EIGEN_DEVICE_FUNC outer_product_selector_run_small(Dst& dst,
const Lhs& lhs,
const Rhs& rhs,
const Func& func,
381 const Scalar& alpha,
const std::false_type&) {
382 evaluator<Rhs> rhsEval(rhs);
383 ei_declare_local_nested_eval(Lhs, lhs, Rhs::SizeAtCompileTime, actual_lhs);
384 const Index rows = dst.rows();
385 const Index cols = dst.cols();
386 for (Index j = 0; j < cols; ++j) {
387 const typename Rhs::Scalar rhs_j = rhsEval.coeff(Index(0), j);
388 for (Index i = 0; i < rows; ++i) {
389 EIGEN_IF_CONSTEXPR (ApplyScale) {
390 func.assignCoeff(dst.coeffRef(i, j), internal::mul(alpha, internal::mul(actual_lhs.coeff(i, Index(0)), rhs_j)));
392 func.assignCoeff(dst.coeffRef(i, j), internal::mul(actual_lhs.coeff(i, Index(0)), rhs_j));
398template <
bool ApplyScale = true,
typename Dst,
typename Lhs,
typename Rhs,
typename Func,
typename Scalar>
399void EIGEN_DEVICE_FUNC outer_product_selector_run_small(Dst& dst,
const Lhs& lhs,
const Rhs& rhs,
const Func& func,
400 const Scalar& alpha,
const std::true_type&) {
401 evaluator<Lhs> lhsEval(lhs);
402 ei_declare_local_nested_eval(Rhs, rhs, Lhs::SizeAtCompileTime, actual_rhs);
403 const Index rows = dst.rows();
404 const Index cols = dst.cols();
405 for (Index i = 0; i < rows; ++i) {
406 const typename Lhs::Scalar lhs_i = lhsEval.coeff(i, Index(0));
407 for (Index j = 0; j < cols; ++j) {
408 EIGEN_IF_CONSTEXPR (ApplyScale) {
409 func.assignCoeff(dst.coeffRef(i, j), internal::mul(alpha, internal::mul(lhs_i, actual_rhs.coeff(Index(0), j))));
411 func.assignCoeff(dst.coeffRef(i, j), internal::mul(lhs_i, actual_rhs.coeff(Index(0), j)));
417template <
typename Lhs,
typename Rhs>
418struct generic_product_impl<Lhs, Rhs, DenseShape, DenseShape, OuterProduct> {
419 template <
typename T>
420 struct is_row_major : bool_constant<(int(T::Flags) & RowMajorBit)> {};
421 using Scalar =
typename Product<Lhs, Rhs>::Scalar;
425 template <
typename Dst,
typename Src>
426 EIGEN_DEVICE_FUNC
void operator()(
const Dst& dst,
const Src& src)
const {
427 dst.const_cast_derived() = src;
432 template <
typename Dst,
typename Src>
433 EIGEN_DEVICE_FUNC
void operator()(
const Dst& dst,
const Src& src)
const {
434 dst.const_cast_derived() += src;
438 template <
typename Dst,
typename Src>
439 EIGEN_DEVICE_FUNC
void operator()(
const Dst& dst,
const Src& src)
const {
440 dst.const_cast_derived() -= src;
447 EIGEN_DEVICE_FUNC
explicit adds(
const Scalar& s) : m_scale(s) {}
449 template <
typename Dst,
typename Src>
450 void EIGEN_DEVICE_FUNC
operator()(
const Dst& dst,
const Src& src)
const {
451 dst.const_cast_derived() += m_scale * src;
455 template <
typename Dst>
456 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void evalTo(Dst& dst,
const Lhs& lhs,
const Rhs& rhs) {
457 if (internal::outer_product_use_small_assignment(dst)) {
458 internal::outer_product_selector_run_small<false>(
459 dst, lhs, rhs, internal::assign_op<typename Dst::Scalar, Scalar>(), Scalar(1), is_row_major<Dst>());
461 internal::outer_product_selector_run(dst, lhs, rhs, set(), is_row_major<Dst>());
465 template <
typename Dst>
466 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void addTo(Dst& dst,
const Lhs& lhs,
const Rhs& rhs) {
467 if (internal::outer_product_use_small_assignment(dst)) {
468 internal::outer_product_selector_run_small<false>(
469 dst, lhs, rhs, internal::add_assign_op<typename Dst::Scalar, Scalar>(), Scalar(1), is_row_major<Dst>());
471 internal::outer_product_selector_run(dst, lhs, rhs, add(), is_row_major<Dst>());
475 template <
typename Dst>
476 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void subTo(Dst& dst,
const Lhs& lhs,
const Rhs& rhs) {
477 if (internal::outer_product_use_small_assignment(dst)) {
478 internal::outer_product_selector_run_small<false>(
479 dst, lhs, rhs, internal::sub_assign_op<typename Dst::Scalar, Scalar>(), Scalar(1), is_row_major<Dst>());
481 internal::outer_product_selector_run(dst, lhs, rhs, sub(), is_row_major<Dst>());
485 template <
typename Dst>
486 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void scaleAndAddTo(Dst& dst,
const Lhs& lhs,
const Rhs& rhs,
487 const Scalar& alpha) {
488 if (internal::outer_product_use_small_assignment(dst)) {
489 internal::outer_product_selector_run_small(dst, lhs, rhs, internal::add_assign_op<typename Dst::Scalar, Scalar>(),
490 alpha, is_row_major<Dst>());
492 internal::outer_product_selector_run(dst, lhs, rhs, adds(alpha), is_row_major<Dst>());
498template <
typename Lhs,
typename Rhs,
typename Derived>
499struct generic_product_impl_base {
500 using Scalar =
typename Product<Lhs, Rhs>::Scalar;
502 template <
typename Dst>
503 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void evalTo(Dst& dst,
const Lhs& lhs,
const Rhs& rhs) {
505 scaleAndAddTo(dst, lhs, rhs, Scalar(1));
508 template <
typename Dst>
509 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void addTo(Dst& dst,
const Lhs& lhs,
const Rhs& rhs) {
510 scaleAndAddTo(dst, lhs, rhs, Scalar(1));
513 template <
typename Dst>
514 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void subTo(Dst& dst,
const Lhs& lhs,
const Rhs& rhs) {
515 scaleAndAddTo(dst, lhs, rhs, Scalar(-1));
518 template <
typename Dst>
519 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void scaleAndAddTo(Dst& dst,
const Lhs& lhs,
const Rhs& rhs,
520 const Scalar& alpha) {
521 Derived::scaleAndAddTo(dst, lhs, rhs, alpha);
525template <
typename Lhs,
typename Rhs,
typename Scalar>
526struct gemv_leading_constant : std::false_type {};
528template <
typename Plain,
typename Nested,
typename Rhs,
typename Scalar>
529struct gemv_leading_constant<
530 CwiseBinaryOp<scalar_product_op<Scalar>, const CwiseNullaryOp<scalar_constant_op<Scalar>, Plain>, Nested>, Rhs,
531 Scalar> : std::is_same<typename Rhs::Scalar, Scalar> {
532 using Inner = remove_all_t<Nested>;
537template <
typename Scalar,
int Count>
538struct gemv_ordered_accumulators {
539 Scalar values[Count];
540 template <
typename Term,
int... Indices>
541 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
explicit gemv_ordered_accumulators(
const Term& term, Index offset,
542 std::integer_sequence<int, Indices...>)
543 : values{term(offset + Indices, 0)...} {}
548template <
typename Plain,
typename Dest,
typename Scalar,
typename Accumulate>
549EIGEN_DEVICE_FUNC EIGEN_DONT_INLINE
void gemv_add_scaled_product(Dest& dst,
const Scalar& alpha,
550 const Accumulate& accumulate) {
551 ei_declare_aligned_stack_constructed_variable(Scalar, buffer, dst.size(),
nullptr);
552 Map<Plain> product(buffer, dst.rows(), dst.cols());
555 for (Index j = 0; j < dst.cols(); ++j)
556 for (Index i = 0; i < dst.rows(); ++i) dst.coeffRef(i, j) += alpha * product.coeff(i, j);
559template <
typename Lhs,
typename Rhs>
560struct generic_product_impl<Lhs, Rhs, DenseShape, DenseShape, GemvProduct>
561 : generic_product_impl_base<Lhs, Rhs, generic_product_impl<Lhs, Rhs, DenseShape, DenseShape, GemvProduct>> {
562 using LhsNested =
typename nested_eval<Lhs, 1>::type;
563 using RhsNested =
typename nested_eval<Rhs, 1>::type;
564 using Scalar =
typename Product<Lhs, Rhs>::Scalar;
568 static constexpr bool OnRight = int(Side) == int(
OnTheRight);
569 using MatrixType = internal::remove_all_t<std::conditional_t<int(Side) ==
OnTheRight, LhsNested, RhsNested>>;
571 template <
typename Dest>
572 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void scaleAndAddTo(Dest& dst,
const Lhs& lhs,
const Rhs& rhs,
573 const Scalar& alpha) {
574 run(dst, lhs, rhs, alpha, blas_product_is_commutative<typename Lhs::Scalar, typename Rhs::Scalar>());
578 template <
typename Dest>
579 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void run(Dest& dst,
const Lhs& lhs,
const Rhs& rhs,
const Scalar& alpha,
581 if (lhs.cols() == 0 || numext::is_exactly_zero(alpha) || blas_traits<Lhs>::hasZeroScalarFactor(lhs) ||
582 blas_traits<Rhs>::hasZeroScalarFactor(rhs))
584 run_ordered(dst, lhs, rhs, alpha, gemv_leading_constant<Lhs, Rhs, Scalar>());
587 template <
typename Dest>
588 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void run_ordered(Dest& dst,
const Lhs& lhs,
const Rhs& rhs,
589 const Scalar& alpha, std::true_type) {
590 const Scalar scale = lhs.lhs().functor().m_other;
592 if (numext::is_exactly_zero(scale))
return run_ordered(dst, lhs, rhs, alpha, std::false_type());
593 using Inner =
typename gemv_leading_constant<Lhs, Rhs, Scalar>::Inner;
594 using Impl = generic_product_impl<Inner, Rhs, DenseShape, DenseShape, GemvProduct>;
595 if (numext::is_exactly_one(alpha))
return Impl::scaleAndAddTo(dst, lhs.rhs(), rhs, scale);
597 gemv_add_scaled_product<typename Product<Lhs, Rhs>::PlainObject>(
598 dst, alpha, [&](
auto& product) { Impl::scaleAndAddTo(product, lhs.rhs(), rhs, scale); });
601 template <
typename Dest>
602 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void run_ordered(Dest& dst,
const Lhs& lhs,
const Rhs& rhs,
603 const Scalar& alpha, std::false_type) {
605 LhsNested actual_lhs(lhs);
606 RhsNested actual_rhs(rhs);
607 const bool unit_alpha = numext::is_exactly_one(alpha);
608 const Index size = OnRight ? lhs.rows() : rhs.cols();
609 const Index depth = lhs.cols();
610 const auto out = [&](Index i) ->
decltype(
auto) {
return OnRight ? dst.coeffRef(i, 0) : dst.coeffRef(0, i); };
611 EIGEN_IF_CONSTEXPR (OnRight == !(MatrixType::Flags &
RowMajorBit)) {
612 const auto accumulate = [&](
const auto& at) {
613 for (Index k = 0; k < depth; ++k) {
614 EIGEN_IF_CONSTEXPR (OnRight) {
615 const typename Rhs::Scalar value = actual_rhs.coeff(k, 0);
616 for (Index i = 0; i < size; ++i) at(i) += actual_lhs.coeff(i, k) * value;
618 const typename Lhs::Scalar value = actual_lhs.coeff(0, k);
619 for (Index i = 0; i < size; ++i) at(i) += value * actual_rhs.coeff(k, i);
626 gemv_add_scaled_product<typename Product<Lhs, Rhs>::PlainObject>(
627 dst, alpha, [&](
auto& product) { accumulate([&](Index i) -> Scalar& {
return product.coeffRef(i); }); });
631 const auto term = [&](Index i, Index k) -> Scalar {
632 return OnRight ? Scalar(actual_lhs.coeff(i, k) * actual_rhs.coeff(k, 0))
633 : Scalar(actual_lhs.coeff(0, k) * actual_rhs.coeff(k, i));
636 constexpr int Block =
sizeof(Scalar) <= 16 ? 8 : 4;
638 for (; size - i >= Block; i += Block) {
639 gemv_ordered_accumulators<Scalar, Block> sums(term, i, std::make_integer_sequence<int, Block>{});
640 for (Index k = 1; k < depth; ++k) {
641 EIGEN_IF_CONSTEXPR (OnRight) {
642 const typename Rhs::Scalar value = actual_rhs.coeff(k, 0);
644 for (
int r = 0; r < Block; ++r) sums.values[r] += actual_lhs.coeff(i + r, k) * value;
646 const typename Lhs::Scalar value = actual_lhs.coeff(0, k);
648 for (
int r = 0; r < Block; ++r) sums.values[r] += value * actual_rhs.coeff(k, i + r);
652 for (
int r = 0; r < Block; ++r) out(i + r) += unit_alpha ? sums.values[r] : alpha * sums.values[r];
654 constexpr int OutputSize = OnRight ? int(traits<Lhs>::RowsAtCompileTime) : int(traits<Rhs>::ColsAtCompileTime);
655 EIGEN_IF_CONSTEXPR (OutputSize == Dynamic || OutputSize % Block != 0) {
656 for (; i < size; ++i) {
657 Scalar sum = term(i, 0);
658 for (Index k = 1; k < depth; ++k) sum += term(i, k);
659 out(i) += unit_alpha ? sum : alpha * sum;
664 template <
typename Dest>
665 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void run(Dest& dst,
const Lhs& lhs,
const Rhs& rhs,
const Scalar& alpha,
668 if (lhs.rows() == 1 && rhs.cols() == 1) {
670 if (numext::is_exactly_zero(alpha) || blas_traits<Lhs>::hasZeroScalarFactor(lhs) ||
671 blas_traits<Rhs>::hasZeroScalarFactor(rhs))
673 dst.coeffRef(0, 0) += alpha * lhs.row(0).conjugate().dot(rhs.col(0));
676 LhsNested actual_lhs(lhs);
677 RhsNested actual_rhs(rhs);
679 bool(internal::blas_traits<MatrixType>::HasUsableDirectAccess)>::run(actual_lhs,
702template <
typename Packet,
bool ColMajor,
703 bool Terminal = std::is_same<typename unpacket_traits<Packet>::half, Packet>::value>
704struct product_packet_cascade {
705 template <
typename Func,
typename Dst,
typename ProdEval>
706 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void run(
const Func& func, Dst& dst,
const ProdEval& prod, Index outer,
707 Index begin, Index end) {
708 constexpr int PacketSize = unpacket_traits<Packet>::size;
709 Index pktEnd = begin + numext::round_down(end - begin, PacketSize);
711 EIGEN_IF_CONSTEXPR (ProdEval::MixedRealComplex) {
712 if (pktEnd - begin == PacketSize && pktEnd < end) {
715 const Packet result = prod.template packet<Unaligned, Packet>(r, c);
716 product_packet_cascade<typename unpacket_traits<Packet>::half,
ColMajor>::run(func, dst, prod, outer, pktEnd,
718 func.template assignPacket<Unaligned, Packet>(&dst.coeffRef(r, c), result);
722 for (; i < pktEnd; i += PacketSize) {
724 func.template assignPacket<Unaligned, Packet>(&dst.coeffRef(r, c), prod.template packet<Unaligned, Packet>(r, c));
727 product_packet_cascade<typename unpacket_traits<Packet>::half,
ColMajor>::run(func, dst, prod, outer, i, end);
732template <
typename Packet,
bool ColMajor>
733struct product_packet_cascade<Packet,
ColMajor, true> {
734 template <
typename Func,
typename Dst,
typename ProdEval>
735 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void run(
const Func& func, Dst& dst,
const ProdEval& prod, Index outer,
736 Index begin, Index end) {
737 constexpr int PacketSize = unpacket_traits<Packet>::size;
738 Index pktEnd = begin + numext::round_down(end - begin, PacketSize);
740 for (; i < pktEnd; i += PacketSize) {
742 func.template assignPacket<Unaligned, Packet>(&dst.coeffRef(r, c), prod.template packet<Unaligned, Packet>(r, c));
744 EIGEN_IF_CONSTEXPR (ProdEval::MixedRealComplex &&
745 (
ColMajor ?
bool(NumTraits<typename ProdEval::LhsScalar>::IsComplex)
746 :
bool(NumTraits<typename ProdEval::RhsScalar>::IsComplex))) {
747 if (i > begin && i < end) {
750 const Index start = end - PacketSize;
752 const Packet result = prod.template packet<Unaligned, Packet>(r, c);
753 func.template assignPacketSegment<Unaligned, Packet>(&dst.coeffRef(r, c), result, i - start, end - i);
757 for (; i < end; ++i) {
759 func.assignCoeff(dst.coeffRef(r, c), prod.coeff(r, c));
770template <
typename Packet,
int Size,
771 bool Terminal = std::is_same<typename unpacket_traits<Packet>::half, Packet>::value>
772struct largest_exact_divisor_size {
773 static constexpr int s = unpacket_traits<Packet>::size;
774 static constexpr int value =
775 (Size % s == 0) ? s : largest_exact_divisor_size<typename unpacket_traits<Packet>::half, Size>::value;
777template <
typename Packet,
int Size>
778struct largest_exact_divisor_size<Packet, Size, true> {
779 static constexpr int s = unpacket_traits<Packet>::size;
780 static constexpr int value = (Size % s == 0) ? s : 1;
800template <
typename ProdEval,
typename Dst,
typename Func>
801struct product_packet_cascade_traits {
802 using Scalar =
typename ProdEval::Scalar;
803 using DstEval = evaluator<Dst>;
804 static constexpr int Rows = ProdEval::RowsAtCompileTime;
805 static constexpr int Cols = ProdEval::ColsAtCompileTime;
806 static constexpr bool DstRowMajor = bool(Dst::IsRowMajor);
807 static constexpr bool DstContiguous =
808 (int(DstEval::Flags) &
DirectAccessBit) != 0 &&
int(Dst::InnerStrideAtCompileTime) == 1;
812 static constexpr bool ProdRowMajor = (int(ProdEval::Flags) &
RowMajorBit) != 0;
813 static constexpr bool VecLhs =
814 bool(ProdEval::Vectorize) && !ProdRowMajor && !DstRowMajor && Rows != Dynamic;
815 static constexpr bool VecRhs =
816 bool(ProdEval::Vectorize) && ProdRowMajor && DstRowMajor && Cols != Dynamic;
817 static constexpr int Extent = VecLhs ? Rows : VecRhs ? Cols : 1;
818 using Packet =
typename find_largest_packet<Scalar, Extent>::type;
819 static constexpr int LargestSize = unpacket_traits<Packet>::size;
820 static constexpr int GenericSize = largest_exact_divisor_size<typename packet_traits<Scalar>::type, Extent>::value;
821 static constexpr bool MightVectorize =
822 bool(EIGEN_UNALIGNED_VECTORIZE) &&
824 bool(functor_traits<Func>::PacketAccess);
825 static constexpr bool Enable = (ProdEval::SameType || ProdEval::MixedRealComplex) && (VecLhs || VecRhs) &&
826 DstContiguous && MightVectorize && (LargestSize <= Extent) &&
827 (LargestSize > GenericSize);
833template <
typename Traits,
typename Func,
typename Dst,
typename ProdEval>
834EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void product_run_packet_cascade(
const Func& func, Dst& dst,
835 const ProdEval& prod) {
836 using Packet =
typename Traits::Packet;
837 Index rows = dst.rows(), cols = dst.cols();
838 Index outerCount = Traits::VecLhs ? cols : rows;
839 Index innerEnd = Traits::VecLhs ? rows : cols;
840 for (Index outer = 0; outer < outerCount; ++outer)
841 product_packet_cascade<Packet, Traits::VecLhs>::run(func, dst, prod, outer, 0, innerEnd);
849template <
typename Lhs,
typename Rhs,
bool EvalToRowMajor>
850struct coeff_based_product_evaluator;
853template <
typename Lhs,
typename Rhs,
typename Dst>
854using dst_ordered_product_evaluator = coeff_based_product_evaluator<Lhs, Rhs, bool(Dst::IsRowMajor)>;
856template <
typename Func,
typename Dst,
typename Lhs,
typename Rhs>
857EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void product_packet_assign(std::true_type,
const Func& func, Dst& dst,
858 const Lhs& lhs,
const Rhs& rhs) {
859 using ProdEval = dst_ordered_product_evaluator<Lhs, Rhs, Dst>;
860 using Traits = product_packet_cascade_traits<ProdEval, Dst, Func>;
861 const ProdEval prodEval(lhs, rhs);
862 product_run_packet_cascade<Traits>(func, dst, prodEval);
864template <
typename Func,
typename Dst,
typename Lhs,
typename Rhs>
865EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void product_packet_assign(std::false_type,
const Func& func, Dst& dst,
866 const Lhs& lhs,
const Rhs& rhs) {
867 call_assignment_no_alias(dst, lhs.lazyProduct(rhs), func);
870template <
typename Lhs,
typename Rhs>
871struct generic_product_impl<Lhs, Rhs, DenseShape, DenseShape, CoeffBasedProductMode> {
872 using Scalar =
typename Product<Lhs, Rhs>::Scalar;
874 template <
typename Dst>
875 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void evalTo(Dst& dst,
const Lhs& lhs,
const Rhs& rhs) {
884 using ProdEval = dst_ordered_product_evaluator<Lhs, Rhs, Dst>;
885 using Func = internal::assign_op<typename Dst::Scalar, Scalar>;
886 using Traits = product_packet_cascade_traits<ProdEval, Dst, Func>;
887 product_packet_assign(bool_constant<(ProdEval::MixedRealComplex && Traits::Enable)>(), Func(), dst, lhs, rhs);
890 template <
typename Dst>
891 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void addTo(Dst& dst,
const Lhs& lhs,
const Rhs& rhs) {
893 using ProdEval = dst_ordered_product_evaluator<Lhs, Rhs, Dst>;
894 using Func = internal::add_assign_op<typename Dst::Scalar, Scalar>;
895 using Traits = product_packet_cascade_traits<ProdEval, Dst, Func>;
896 product_packet_assign(bool_constant<Traits::Enable>(), Func(), dst, lhs, rhs);
899 template <
typename Dst>
900 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void subTo(Dst& dst,
const Lhs& lhs,
const Rhs& rhs) {
902 using ProdEval = dst_ordered_product_evaluator<Lhs, Rhs, Dst>;
903 using Func = internal::sub_assign_op<typename Dst::Scalar, Scalar>;
904 using Traits = product_packet_cascade_traits<ProdEval, Dst, Func>;
905 product_packet_assign(bool_constant<Traits::Enable>(), Func(), dst, lhs, rhs);
922 template <
typename Dst,
typename Func>
923 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void eval_dynamic(Dst& dst,
const Lhs& lhs,
const Rhs& rhs,
926 HasScalarFactor = blas_traits<Lhs>::HasScalarFactor || blas_traits<Rhs>::HasScalarFactor,
927 ConjLhs = blas_traits<Lhs>::NeedToConjugate,
928 ConjRhs = blas_traits<Rhs>::NeedToConjugate
932 Scalar actualAlpha = combine_scalar_factors<Scalar>(lhs, rhs);
934 eval_dynamic_impl(dst, blas_traits<Lhs>::extract(lhs).
template conjugateIf<ConjLhs>(),
935 blas_traits<Rhs>::extract(rhs).
template conjugateIf<ConjRhs>(), func, actualAlpha,
936 bool_constant<HasScalarFactor>());
940 template <
typename Dst,
typename LhsT,
typename RhsT,
typename Func,
typename Scalar>
941 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void eval_dynamic_impl(Dst& dst,
const LhsT& lhs,
const RhsT& rhs,
942 const Func& func,
const Scalar& s ,
944 EIGEN_UNUSED_VARIABLE(s);
945 eigen_internal_assert(numext::is_exactly_one(s));
946 call_restricted_packet_assignment_no_alias(dst, lhs.lazyProduct(rhs), func);
949 template <
typename Dst,
typename LhsT,
typename RhsT,
typename Func,
typename Scalar>
950 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void eval_dynamic_impl(Dst& dst,
const LhsT& lhs,
const RhsT& rhs,
951 const Func& func,
const Scalar& s,
953 call_restricted_packet_assignment_no_alias(dst, s * lhs.lazyProduct(rhs), func);
958template <
typename Lhs,
typename Rhs>
959struct generic_product_impl<Lhs, Rhs, DenseShape, DenseShape, LazyCoeffBasedProductMode>
960 : generic_product_impl<Lhs, Rhs, DenseShape, DenseShape, CoeffBasedProductMode> {};
962template <typename Packet, typename Scalar, typename ResultScalar = typename unpacket_traits<Packet>::type,
963 bool Real = std::is_same<Scalar, float>::value || std::is_same<Scalar, double>::value>
964struct product_operand_packet {
969template <
typename Packet,
typename Real>
970struct product_operand_packet<Packet, Real, std::complex<Real>, true> {
971 using type =
typename unpacket_traits<Packet>::as_real;
974template <
typename Packet,
typename Scalar>
975using product_operand_packet_t =
typename product_operand_packet<Packet, Scalar>::type;
978template <
typename Xpr>
979struct product_real_packet_access {
980 using CoeffReturnType =
decltype(std::declval<const evaluator<Xpr>&>().coeff(Index(0), Index(0)));
981 static constexpr bool value =
982 (evaluator<Xpr>::Flags &
DirectAccessBit) != 0 && Xpr::InnerStrideAtCompileTime == 1 &&
983 std::is_same<std::remove_const_t<std::remove_reference_t<CoeffReturnType>>,
typename Xpr::Scalar>::value &&
984 std::is_lvalue_reference<CoeffReturnType>::value;
987template <
typename Xpr>
988struct product_real_packet_evaluator : evaluator<Xpr> {
989 using Base = evaluator<Xpr>;
990 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
explicit product_real_packet_evaluator(
const Xpr& xpr) : Base(xpr) {}
992 template <
int LoadMode,
typename Packet>
993 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
typename unpacket_traits<Packet>::as_real packet(Index row, Index col)
const {
994 return ploaddup<typename unpacket_traits<Packet>::as_real>(&this->coeff(row, col));
997 template <
int LoadMode,
typename Packet>
998 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
typename unpacket_traits<Packet>::as_real packetSegment(Index row, Index col,
1000 Index count)
const {
1002 EIGEN_UNUSED_VARIABLE(begin);
1003 EIGEN_UNUSED_VARIABLE(count);
1004 eigen_internal_assert(begin == 0 && count == unpacket_traits<Packet>::size);
1005 return packet<LoadMode, Packet>(row, col);
1009template <
int StorageOrder,
int UnrollingIndex,
typename Lhs,
typename Rhs,
typename Packet,
int LoadMode>
1010struct etor_product_packet_impl;
1012struct product_empty_packet_evaluator {
1013 template <
typename Xpr>
1014 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
explicit product_empty_packet_evaluator(
const Xpr&) {}
1018template <
typename Lhs,
typename Rhs,
bool EvalToRowMajor_>
1019struct coeff_based_product_evaluator : evaluator_base<Product<Lhs, Rhs, LazyProduct>> {
1020 using XprType = Product<Lhs, Rhs, LazyProduct>;
1021 using Scalar =
typename XprType::Scalar;
1022 using CoeffReturnType =
typename XprType::CoeffReturnType;
1024 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
explicit coeff_based_product_evaluator(
const XprType& xpr)
1025 : coeff_based_product_evaluator(xpr.lhs(), xpr.rhs()) {}
1029 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE coeff_based_product_evaluator(
const Lhs& lhs,
const Rhs& rhs)
1034 m_innerDim(lhs.cols()) {
1035 EIGEN_INTERNAL_CHECK_COST_VALUE(NumTraits<Scalar>::MulCost);
1036 EIGEN_INTERNAL_CHECK_COST_VALUE(NumTraits<Scalar>::AddCost);
1037 EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
1042 using LhsNested =
typename internal::nested_eval<Lhs, Rhs::ColsAtCompileTime>::type;
1043 using RhsNested =
typename internal::nested_eval<Rhs, Lhs::RowsAtCompileTime>::type;
1045 using LhsNestedCleaned = internal::remove_all_t<LhsNested>;
1046 using RhsNestedCleaned = internal::remove_all_t<RhsNested>;
1048 using LhsScalar =
typename LhsNestedCleaned::Scalar;
1049 using RhsScalar =
typename RhsNestedCleaned::Scalar;
1051 using LhsEtorType = evaluator<LhsNestedCleaned>;
1052 using RhsEtorType = evaluator<RhsNestedCleaned>;
1055 RowsAtCompileTime = LhsNestedCleaned::RowsAtCompileTime,
1056 ColsAtCompileTime = RhsNestedCleaned::ColsAtCompileTime,
1057 InnerSize = min_size_prefer_fixed(LhsNestedCleaned::ColsAtCompileTime, RhsNestedCleaned::RowsAtCompileTime),
1058 MaxRowsAtCompileTime = LhsNestedCleaned::MaxRowsAtCompileTime,
1059 MaxColsAtCompileTime = RhsNestedCleaned::MaxColsAtCompileTime
1062 using LhsVecPacketType =
typename find_best_packet<Scalar, RowsAtCompileTime>::type;
1063 using RhsVecPacketType =
typename find_best_packet<Scalar, ColsAtCompileTime>::type;
1065 static constexpr int LhsCoeffReadCost = LhsEtorType::CoeffReadCost;
1066 static constexpr int RhsCoeffReadCost = RhsEtorType::CoeffReadCost;
1067 static constexpr int CoeffReadCost =
1068 InnerSize == 0 ? NumTraits<Scalar>::ReadCost
1069 : InnerSize == Dynamic
1071 : InnerSize * (NumTraits<Scalar>::MulCost + int(LhsCoeffReadCost) + int(RhsCoeffReadCost)) +
1072 (InnerSize - 1) * NumTraits<Scalar>::AddCost;
1074 static constexpr bool Unroll = CoeffReadCost <= EIGEN_UNROLLING_LIMIT;
1076 static constexpr int LhsFlags = LhsEtorType::Flags;
1077 static constexpr int RhsFlags = RhsEtorType::Flags;
1079 static constexpr int LhsRowMajor = LhsFlags &
RowMajorBit;
1080 static constexpr int RhsRowMajor = RhsFlags &
RowMajorBit;
1082 static constexpr int LhsVecPacketSize = unpacket_traits<LhsVecPacketType>::size;
1083 static constexpr int RhsVecPacketSize = unpacket_traits<RhsVecPacketType>::size;
1086 static constexpr int LhsAlignment =
1087 plain_enum_min(LhsEtorType::Alignment, LhsVecPacketSize*
int(
sizeof(
typename LhsNestedCleaned::Scalar)));
1088 static constexpr int RhsAlignment =
1089 plain_enum_min(RhsEtorType::Alignment, RhsVecPacketSize*
int(
sizeof(
typename RhsNestedCleaned::Scalar)));
1091 static constexpr bool SameType =
1092 std::is_same<typename LhsNestedCleaned::Scalar, typename RhsNestedCleaned::Scalar>::value;
1094 static constexpr bool CanVectorizeRhs = bool(RhsRowMajor) && (RhsFlags &
PacketAccessBit) && (ColsAtCompileTime != 1);
1095 static constexpr bool CanVectorizeLhs = (!LhsRowMajor) && (LhsFlags &
PacketAccessBit) && (RowsAtCompileTime != 1);
1098 static constexpr bool EvalToRowMajor = EvalToRowMajor_;
1099 using RealScalar =
typename NumTraits<Scalar>::Real;
1100 static constexpr bool LhsIsReal = std::is_same<LhsScalar, RealScalar>::value;
1101 static constexpr bool RhsIsReal = std::is_same<RhsScalar, RealScalar>::value;
1102 static constexpr bool MixedScalarTypes =
1103 std::is_same<Scalar, std::complex<RealScalar>>::value &&
1104 (std::is_same<RealScalar, float>::value || std::is_same<RealScalar, double>::value) &&
1105 ((LhsIsReal && std::is_same<RhsScalar, Scalar>::value) || (RhsIsReal && std::is_same<LhsScalar, Scalar>::value));
1106 static constexpr bool RealPacketLhs = LhsIsReal && product_real_packet_access<LhsNestedCleaned>::value;
1107 static constexpr bool RealPacketRhs = RhsIsReal && product_real_packet_access<RhsNestedCleaned>::value;
1108 static constexpr bool PacketOperandVectorizable = EvalToRowMajor ? CanVectorizeRhs : CanVectorizeLhs;
1110 static constexpr bool MixedRealComplex =
1111 MixedScalarTypes && packet_traits<Scalar>::Vectorizable && PacketOperandVectorizable &&
1112 (EvalToRowMajor ? (!RhsIsReal || RealPacketRhs) : (!LhsIsReal || RealPacketLhs)) &&
1113 (
bool(
int(traits<XprType>::Flags) &
RowMajorBit) ==
bool(EvalToRowMajor));
1115 static constexpr bool Vectorize = (SameType && PacketOperandVectorizable) || MixedRealComplex;
1116 static constexpr int Flags = ((int(LhsFlags) | int(RhsFlags)) & HereditaryBits & ~RowMajorBit) |
1119 using LhsPacketEtorType =
1121 std::conditional_t<MixedRealComplex && RealPacketLhs && !EvalToRowMajor,
1122 product_real_packet_evaluator<LhsNestedCleaned>, LhsEtorType>,
1123 product_empty_packet_evaluator>;
1124 using RhsPacketEtorType =
1126 std::conditional_t<MixedRealComplex && RealPacketRhs && EvalToRowMajor,
1127 product_real_packet_evaluator<RhsNestedCleaned>, RhsEtorType>,
1128 product_empty_packet_evaluator>;
1130 static constexpr bool SingleTermCoeff = InnerSize == 1 && Vectorize;
1132 static constexpr int LhsOuterStrideBytes =
1133 int(LhsNestedCleaned::OuterStrideAtCompileTime) * int(
sizeof(
typename LhsNestedCleaned::Scalar));
1134 static constexpr int RhsOuterStrideBytes =
1135 int(RhsNestedCleaned::OuterStrideAtCompileTime) * int(
sizeof(
typename RhsNestedCleaned::Scalar));
1138 static constexpr int VecOuterStrideBytes = EvalToRowMajor ? RhsOuterStrideBytes : LhsOuterStrideBytes;
1139 static constexpr int VecAlignment = EvalToRowMajor ? RhsAlignment : LhsAlignment;
1140 static constexpr int Alignment =
1141 (Vectorize && VecOuterStrideBytes > 0 && VecOuterStrideBytes % plain_enum_max(1, VecAlignment) == 0)
1145 EIGEN_DEVICE_FUNC
constexpr EIGEN_STRONG_INLINE
const CoeffReturnType coeff(Index row, Index col)
const {
1146 return coeff_impl(row, col, bool_constant<SingleTermCoeff>());
1149 EIGEN_DEVICE_FUNC
constexpr EIGEN_STRONG_INLINE CoeffReturnType coeff_impl(Index row, Index col,
1150 std::true_type)
const {
1151 return fast_mult_op<LhsScalar, RhsScalar>()(m_lhsImpl.coeff(row, Index(0)), m_rhsImpl.coeff(Index(0), col));
1154 EIGEN_DEVICE_FUNC
constexpr EIGEN_STRONG_INLINE CoeffReturnType coeff_impl(Index row, Index col,
1155 std::false_type)
const {
1156 return coeff_reduced(row, col, bool_constant<MixedRealComplex>());
1159 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff_reduced(Index row, Index col, std::true_type)
const {
1160 using SinglePacket =
typename find_best_packet<Scalar, 1>::type;
1161 EIGEN_IF_CONSTEXPR (unpacket_traits<SinglePacket>::size == 1) {
1162 return pfirst(packetSegment<Unaligned, SinglePacket>(row, col, 0, 1));
1164 return coeff_reduced(row, col, std::false_type());
1167 EIGEN_DEVICE_FUNC
constexpr EIGEN_STRONG_INLINE CoeffReturnType coeff_reduced(Index row, Index col,
1168 std::false_type)
const {
1172 return m_lhs.row(row).transpose().binaryExpr(m_rhs.col(col), fast_mult_op<LhsScalar, RhsScalar>()).sum();
1179 EIGEN_DEVICE_FUNC
constexpr EIGEN_STRONG_INLINE
const CoeffReturnType coeff(Index index)
const {
1180 const Index row = (RowsAtCompileTime == 1 || MaxRowsAtCompileTime == 1) ? 0 : index;
1181 const Index col = (RowsAtCompileTime == 1 || MaxRowsAtCompileTime == 1) ? index : 0;
1182 return coeff(row, col);
1185 template <
int LoadMode,
typename PacketType>
1186 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const PacketType packet(Index row, Index col)
const {
1189 Unroll ? int(InnerSize) : Dynamic, LhsPacketEtorType, RhsPacketEtorType,
1190 PacketType, LoadMode>;
1191 PacketImpl::run(row, col, m_lhsImpl, m_rhsImpl, m_innerDim, res);
1195 template <
int LoadMode,
typename PacketType>
1196 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const PacketType packet(Index index)
const {
1197 const Index row = (RowsAtCompileTime == 1 || MaxRowsAtCompileTime == 1) ? 0 : index;
1198 const Index col = (RowsAtCompileTime == 1 || MaxRowsAtCompileTime == 1) ? index : 0;
1199 return packet<LoadMode, PacketType>(row, col);
1202 template <
int LoadMode,
typename PacketType>
1203 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const PacketType packetSegment(Index row, Index col, Index begin,
1204 Index count)
const {
1205 EIGEN_IF_CONSTEXPR (MixedRealComplex) {
1206 if (count == 0)
return pset1<PacketType>(Scalar(0));
1208 EIGEN_IF_CONSTEXPR (MixedRealComplex && (EvalToRowMajor ? RealPacketRhs : RealPacketLhs) &&
1209 unpacket_traits<PacketType>::size > 1) {
1210 if (count != unpacket_traits<PacketType>::size) {
1212 Scalar values[unpacket_traits<PacketType>::size] = {};
1213 for (Index i = begin; i < begin + count; ++i) {
1214 values[i] = coeff_reduced(row + (EvalToRowMajor ? 0 : i), col + (EvalToRowMajor ? i : 0), std::false_type());
1216 return ploadu<PacketType>(values);
1221 Unroll ? int(InnerSize) : Dynamic, LhsPacketEtorType, RhsPacketEtorType,
1222 PacketType, LoadMode>;
1223 PacketImpl::run_segment(row, col, m_lhsImpl, m_rhsImpl, m_innerDim, res, begin, count);
1227 template <
int LoadMode,
typename PacketType>
1228 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const PacketType packetSegment(Index index, Index begin, Index count)
const {
1229 const Index row = (RowsAtCompileTime == 1 || MaxRowsAtCompileTime == 1) ? 0 : index;
1230 const Index col = (RowsAtCompileTime == 1 || MaxRowsAtCompileTime == 1) ? index : 0;
1231 return packetSegment<LoadMode, PacketType>(row, col, begin, count);
1235 add_const_on_value_type_t<LhsNested> m_lhs;
1236 add_const_on_value_type_t<RhsNested> m_rhs;
1238 LhsPacketEtorType m_lhsImpl;
1239 RhsPacketEtorType m_rhsImpl;
1241 variable_if_dynamic<Index, InnerSize> m_innerDim;
1246template <
typename Lhs,
typename Rhs,
int ProductTag>
1247struct product_evaluator<Product<Lhs, Rhs, LazyProduct>, ProductTag, DenseShape, DenseShape>
1248 : coeff_based_product_evaluator<Lhs, Rhs, (int(traits<Product<Lhs, Rhs, LazyProduct>>::Flags) & RowMajorBit) != 0> {
1249 using XprType = Product<Lhs, Rhs, LazyProduct>;
1251 coeff_based_product_evaluator<Lhs, Rhs, (int(traits<Product<Lhs, Rhs, LazyProduct>>::Flags) &
RowMajorBit) != 0>;
1253 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
explicit product_evaluator(
const XprType& xpr) : Base(xpr) {}
1254 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE product_evaluator(
const Lhs& lhs,
const Rhs& rhs) : Base(lhs, rhs) {}
1260template <
typename DstEvaluatorType,
typename Lhs,
typename Rhs>
1261struct dense_assignment_source_evaluator<
1262 DstEvaluatorType, Product<Lhs, Rhs, LazyProduct>,
1263 std::enable_if_t<std::is_same<typename evaluator_traits<Lhs>::Shape, DenseShape>::value &&
1264 std::is_same<typename evaluator_traits<Rhs>::Shape, DenseShape>::value>> {
1265 using DstOrdered = coeff_based_product_evaluator<Lhs, Rhs, (int(DstEvaluatorType::Flags) &
RowMajorBit) != 0>;
1266 using type = std::conditional_t<DstOrdered::Vectorize, DstOrdered, evaluator<Product<Lhs, Rhs, LazyProduct>>>;
1269template <
typename Lhs,
typename Rhs>
1270struct product_evaluator<Product<Lhs, Rhs, DefaultProduct>, LazyCoeffBasedProductMode, DenseShape, DenseShape>
1271 : product_evaluator<Product<Lhs, Rhs, LazyProduct>, CoeffBasedProductMode, DenseShape, DenseShape> {
1272 using XprType = Product<Lhs, Rhs, DefaultProduct>;
1273 using BaseProduct = Product<Lhs, Rhs, LazyProduct>;
1274 using Base = product_evaluator<BaseProduct, CoeffBasedProductMode, DenseShape, DenseShape>;
1276 "the evaluator must use the storage order its expression reports");
1278 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
explicit product_evaluator(
const XprType& xpr)
1279 : Base(BaseProduct(xpr.lhs(), xpr.rhs())) {}
1286template <
int UnrollingIndex,
typename Lhs,
typename Rhs,
typename Packet,
int LoadMode>
1287struct etor_product_packet_impl<
RowMajor, UnrollingIndex, Lhs, Rhs, Packet, LoadMode> {
1288 using FactorPacket = product_operand_packet_t<Packet, remove_all_t<decltype(std::declval<const Lhs&>().coeff(0, 0))>>;
1289 using ValuePacket = product_operand_packet_t<Packet, remove_all_t<decltype(std::declval<const Rhs&>().coeff(0, 0))>>;
1290 using Op = conj_helper<FactorPacket, ValuePacket, false, false>;
1291 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void run(Index row, Index col,
const Lhs& lhs,
const Rhs& rhs,
1292 Index innerDim, Packet& res) {
1293 etor_product_packet_impl<RowMajor, UnrollingIndex - 1, Lhs, Rhs, Packet, LoadMode>::run(row, col, lhs, rhs,
1295 res = Op().pmadd(pset1<FactorPacket>(lhs.coeff(row, Index(UnrollingIndex - 1))),
1296 rhs.template packet<LoadMode, Packet>(Index(UnrollingIndex - 1), col), res);
1298 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void run_segment(Index row, Index col,
const Lhs& lhs,
const Rhs& rhs,
1299 Index innerDim, Packet& res, Index begin, Index count) {
1300 etor_product_packet_impl<RowMajor, UnrollingIndex - 1, Lhs, Rhs, Packet, LoadMode>::run_segment(
1301 row, col, lhs, rhs, innerDim, res, begin, count);
1302 res = Op().pmadd(pset1<FactorPacket>(lhs.coeff(row, Index(UnrollingIndex - 1))),
1303 rhs.template packetSegment<LoadMode, Packet>(Index(UnrollingIndex - 1), col, begin, count), res);
1307template <
int UnrollingIndex,
typename Lhs,
typename Rhs,
typename Packet,
int LoadMode>
1308struct etor_product_packet_impl<
ColMajor, UnrollingIndex, Lhs, Rhs, Packet, LoadMode> {
1309 using FactorPacket = product_operand_packet_t<Packet, remove_all_t<decltype(std::declval<const Rhs&>().coeff(0, 0))>>;
1310 using ValuePacket = product_operand_packet_t<Packet, remove_all_t<decltype(std::declval<const Lhs&>().coeff(0, 0))>>;
1311 using Op = conj_helper<ValuePacket, FactorPacket, false, false>;
1312 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void run(Index row, Index col,
const Lhs& lhs,
const Rhs& rhs,
1313 Index innerDim, Packet& res) {
1314 etor_product_packet_impl<ColMajor, UnrollingIndex - 1, Lhs, Rhs, Packet, LoadMode>::run(row, col, lhs, rhs,
1316 res = Op().pmadd(lhs.template packet<LoadMode, Packet>(row, Index(UnrollingIndex - 1)),
1317 pset1<FactorPacket>(rhs.coeff(Index(UnrollingIndex - 1), col)), res);
1319 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void run_segment(Index row, Index col,
const Lhs& lhs,
const Rhs& rhs,
1320 Index innerDim, Packet& res, Index begin, Index count) {
1321 etor_product_packet_impl<ColMajor, UnrollingIndex - 1, Lhs, Rhs, Packet, LoadMode>::run_segment(
1322 row, col, lhs, rhs, innerDim, res, begin, count);
1323 res = Op().pmadd(lhs.template packetSegment<LoadMode, Packet>(row, Index(UnrollingIndex - 1), begin, count),
1324 pset1<FactorPacket>(rhs.coeff(Index(UnrollingIndex - 1), col)), res);
1328template <
typename Lhs,
typename Rhs,
typename Packet,
int LoadMode>
1329struct etor_product_packet_impl<
RowMajor, 1, Lhs, Rhs, Packet, LoadMode> {
1330 using FactorPacket = product_operand_packet_t<Packet, remove_all_t<decltype(std::declval<const Lhs&>().coeff(0, 0))>>;
1331 using ValuePacket = product_operand_packet_t<Packet, remove_all_t<decltype(std::declval<const Rhs&>().coeff(0, 0))>>;
1332 using Op = conj_helper<FactorPacket, ValuePacket, false, false>;
1333 static EIGEN_DEVICE_FUNC
constexpr EIGEN_STRONG_INLINE
void run(Index row, Index col,
const Lhs& lhs,
const Rhs& rhs,
1334 Index , Packet& res) {
1336 Op().pmul(pset1<FactorPacket>(lhs.coeff(row, Index(0))), rhs.template packet<LoadMode, Packet>(Index(0), col));
1338 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void run_segment(Index row, Index col,
const Lhs& lhs,
const Rhs& rhs,
1339 Index , Packet& res, Index begin,
1341 res = Op().pmul(pset1<FactorPacket>(lhs.coeff(row, Index(0))),
1342 rhs.template packetSegment<LoadMode, Packet>(Index(0), col, begin, count));
1346template <
typename Lhs,
typename Rhs,
typename Packet,
int LoadMode>
1347struct etor_product_packet_impl<
ColMajor, 1, Lhs, Rhs, Packet, LoadMode> {
1348 using FactorPacket = product_operand_packet_t<Packet, remove_all_t<decltype(std::declval<const Rhs&>().coeff(0, 0))>>;
1349 using ValuePacket = product_operand_packet_t<Packet, remove_all_t<decltype(std::declval<const Lhs&>().coeff(0, 0))>>;
1350 using Op = conj_helper<ValuePacket, FactorPacket, false, false>;
1351 static EIGEN_DEVICE_FUNC
constexpr EIGEN_STRONG_INLINE
void run(Index row, Index col,
const Lhs& lhs,
const Rhs& rhs,
1352 Index , Packet& res) {
1354 Op().pmul(lhs.template packet<LoadMode, Packet>(row, Index(0)), pset1<FactorPacket>(rhs.coeff(Index(0), col)));
1356 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void run_segment(Index row, Index col,
const Lhs& lhs,
const Rhs& rhs,
1357 Index , Packet& res, Index begin,
1359 res = Op().pmul(lhs.template packetSegment<LoadMode, Packet>(row, Index(0), begin, count),
1360 pset1<FactorPacket>(rhs.coeff(Index(0), col)));
1364template <
typename Lhs,
typename Rhs,
typename Packet,
int LoadMode>
1365struct etor_product_packet_impl<
RowMajor, 0, Lhs, Rhs, Packet, LoadMode> {
1366 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void run(Index , Index ,
const Lhs& ,
1367 const Rhs& , Index , Packet& res) {
1368 res = pset1<Packet>(
typename unpacket_traits<Packet>::type(0));
1370 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void run_segment(Index , Index ,
const Lhs& ,
1371 const Rhs& , Index , Packet& res,
1373 res = pset1<Packet>(
typename unpacket_traits<Packet>::type(0));
1377template <
typename Lhs,
typename Rhs,
typename Packet,
int LoadMode>
1378struct etor_product_packet_impl<
ColMajor, 0, Lhs, Rhs, Packet, LoadMode> {
1379 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void run(Index , Index ,
const Lhs& ,
1380 const Rhs& , Index , Packet& res) {
1381 res = pset1<Packet>(
typename unpacket_traits<Packet>::type(0));
1383 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void run_segment(Index , Index ,
const Lhs& ,
1384 const Rhs& , Index , Packet& res,
1386 res = pset1<Packet>(
typename unpacket_traits<Packet>::type(0));
1390template <
typename Packet,
typename ReadLhs,
typename ReadRhs>
1391EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet product_packet_accumulate(Index innerDim,
const ReadLhs& lhs,
1392 const ReadRhs& rhs) {
1393 using LhsPacket =
decltype(lhs(Index(0)));
1394 using RhsPacket =
decltype(rhs(Index(0)));
1395 const conj_helper<LhsPacket, RhsPacket, false, false> op;
1396 Packet res = pset1<Packet>(
typename unpacket_traits<Packet>::type(0));
1399 EIGEN_IF_CONSTEXPR ((!std::is_same<LhsPacket, RhsPacket>::value)) {
1401 res = op.pmul(lhs(0), rhs(0));
1405 for (; i < innerDim; ++i) res = op.pmadd(lhs(i), rhs(i), res);
1409template <
typename Lhs,
typename Rhs,
typename Packet,
int LoadMode>
1410struct etor_product_packet_impl<
RowMajor, Dynamic, Lhs, Rhs, Packet, LoadMode> {
1411 using FactorPacket = product_operand_packet_t<Packet, remove_all_t<decltype(std::declval<const Lhs&>().coeff(0, 0))>>;
1412 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void run(Index row, Index col,
const Lhs& lhs,
const Rhs& rhs,
1413 Index innerDim, Packet& res) {
1414 res = product_packet_accumulate<Packet>(
1415 innerDim, [&](Index i) {
return pset1<FactorPacket>(lhs.coeff(row, i)); },
1416 [&](Index i) {
return rhs.template packet<LoadMode, Packet>(i, col); });
1418 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void run_segment(Index row, Index col,
const Lhs& lhs,
const Rhs& rhs,
1419 Index innerDim, Packet& res, Index begin, Index count) {
1420 res = product_packet_accumulate<Packet>(
1421 innerDim, [&](Index i) {
return pset1<FactorPacket>(lhs.coeff(row, i)); },
1422 [&](Index i) {
return rhs.template packetSegment<LoadMode, Packet>(i, col, begin, count); });
1426template <
typename Lhs,
typename Rhs,
typename Packet,
int LoadMode>
1427struct etor_product_packet_impl<
ColMajor, Dynamic, Lhs, Rhs, Packet, LoadMode> {
1428 using FactorPacket = product_operand_packet_t<Packet, remove_all_t<decltype(std::declval<const Rhs&>().coeff(0, 0))>>;
1429 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void run(Index row, Index col,
const Lhs& lhs,
const Rhs& rhs,
1430 Index innerDim, Packet& res) {
1431 res = product_packet_accumulate<Packet>(
1432 innerDim, [&](Index i) {
return lhs.template packet<LoadMode, Packet>(row, i); },
1433 [&](Index i) {
return pset1<FactorPacket>(rhs.coeff(i, col)); });
1435 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void run_segment(Index row, Index col,
const Lhs& lhs,
const Rhs& rhs,
1436 Index innerDim, Packet& res, Index begin, Index count) {
1437 res = product_packet_accumulate<Packet>(
1438 innerDim, [&](Index i) {
return lhs.template packetSegment<LoadMode, Packet>(row, i, begin, count); },
1439 [&](Index i) {
return pset1<FactorPacket>(rhs.coeff(i, col)); });
1446template <
int Mode,
bool LhsIsTriangular,
typename Lhs,
bool LhsIsVector,
typename Rhs,
bool RhsIsVector>
1447struct triangular_product_impl;
1449template <
int ProductOrder>
1450struct diagonal_product_segment_impl;
1453struct diagonal_product_segment_impl<
OnTheLeft> {
1454 template <
typename DstSegment,
typename Coeffs,
typename DiagonalType,
typename Alpha>
1455 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void run(DstSegment& dst,
const Coeffs& coeffs,
1456 const DiagonalType& diagonal, Index begin, Index ,
1457 const Alpha& alpha) {
1458 dst += alpha * diagonal.segment(begin, dst.size()).cwiseProduct(coeffs);
1460 template <
typename DstSegment,
typename Coeffs,
typename DiagonalType>
1461 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void runOverwrite(DstSegment& dst,
const Coeffs& coeffs,
1462 const DiagonalType& diagonal, Index begin,
1464 dst = diagonal.segment(begin, dst.size()).cwiseProduct(coeffs);
1469struct diagonal_product_segment_impl<
OnTheRight> {
1470 template <
typename DstSegment,
typename Coeffs,
typename DiagonalType,
typename Alpha>
1471 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void run(DstSegment& dst,
const Coeffs& coeffs,
1472 const DiagonalType& diagonal, Index , Index col,
1473 const Alpha& alpha) {
1474 dst += alpha * (coeffs * diagonal.coeff(col));
1476 template <
typename DstSegment,
typename Coeffs,
typename DiagonalType>
1477 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void runOverwrite(DstSegment& dst,
const Coeffs& coeffs,
1478 const DiagonalType& diagonal, Index ,
1480 dst = coeffs * diagonal.coeff(col);
1484template <
int Mode,
int ProductOrder,
typename MatrixType,
typename DiagonalType>
1485struct triangular_diagonal_product_impl {
1486 using MatrixScalar =
typename MatrixType::Scalar;
1488 template <
typename Dest,
typename Alpha>
1489 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void run(Dest& dst,
const MatrixType& matrix,
1490 const DiagonalType& diagonal,
const Alpha& alpha) {
1491 EIGEN_INTERNAL_ASSERT_SIZE_MATCHES_AXIS_MSG(diagonal, matrix, ProductOrder ==
OnTheLeft,
"invalid matrix product");
1493 const Index rows = matrix.rows();
1494 const Index cols = matrix.cols();
1495 for (Index col = 0; col < cols; ++col) {
1496 EIGEN_IF_CONSTEXPR ((Mode &
Upper) ==
Upper) {
1497 const Index end = (std::min)(rows, ((Mode & (
UnitDiag |
ZeroDiag)) ? col : col + 1));
1498 addStoredSegment(dst, matrix, diagonal, 0, end, col, alpha);
1501 addStoredSegment(dst, matrix, diagonal, begin, rows - begin, col, alpha);
1505 if (col < rows) addUnitCoeff(dst, diagonal, col, alpha);
1511 template <
typename Dest,
typename Alpha>
1512 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void addStoredSegment(Dest& dst,
const MatrixType& matrix,
1513 const DiagonalType& diagonal, Index begin,
1514 Index size, Index col,
const Alpha& alpha) {
1515 if (size <= 0)
return;
1516 auto dstSegment = dst.col(col).segment(begin, size);
1517 diagonal_product_segment_impl<ProductOrder>::run(dstSegment, matrix.col(col).segment(begin, size), diagonal, begin,
1521 template <
typename Dest,
typename Alpha>
1522 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void addUnitCoeff(Dest& dst,
const DiagonalType& diagonal, Index index,
1523 const Alpha& alpha) {
1524 EIGEN_IF_CONSTEXPR (ProductOrder ==
OnTheLeft) {
1525 dst.coeffRef(index, index) += alpha * (diagonal.coeff(index) * MatrixScalar(1));
1527 dst.coeffRef(index, index) += alpha * (MatrixScalar(1) * diagonal.coeff(index));
1532template <
typename Lhs,
typename Rhs,
int ProductTag>
1533struct generic_product_impl<Lhs, Rhs, TriangularShape, DenseShape, ProductTag>
1534 : generic_product_impl_base<Lhs, Rhs, generic_product_impl<Lhs, Rhs, TriangularShape, DenseShape, ProductTag>> {
1535 using Scalar =
typename Product<Lhs, Rhs>::Scalar;
1537 template <
typename Dest>
1538 static void scaleAndAddTo(Dest& dst,
const Lhs& lhs,
const Rhs& rhs,
const Scalar& alpha) {
1539 triangular_product_impl<Lhs::Mode, true, typename Lhs::MatrixType, false, Rhs, Rhs::ColsAtCompileTime == 1>::run(
1540 dst, lhs.nestedExpression(), rhs, alpha);
1544template <
typename Lhs,
typename Rhs,
int ProductTag>
1545struct generic_product_impl<Lhs, Rhs, DenseShape, TriangularShape, ProductTag>
1546 : generic_product_impl_base<Lhs, Rhs, generic_product_impl<Lhs, Rhs, DenseShape, TriangularShape, ProductTag>> {
1547 using Scalar =
typename Product<Lhs, Rhs>::Scalar;
1549 template <
typename Dest>
1550 static void scaleAndAddTo(Dest& dst,
const Lhs& lhs,
const Rhs& rhs,
const Scalar& alpha) {
1551 triangular_product_impl<Rhs::Mode, false, Lhs, Lhs::RowsAtCompileTime == 1, typename Rhs::MatrixType, false>::run(
1552 dst, lhs, rhs.nestedExpression(), alpha);
1556template <
typename Lhs,
typename Rhs,
int ProductTag>
1557struct generic_product_impl<Lhs, Rhs, TriangularShape, DiagonalShape, ProductTag>
1558 : generic_product_impl_base<Lhs, Rhs, generic_product_impl<Lhs, Rhs, TriangularShape, DiagonalShape, ProductTag>> {
1559 using Scalar =
typename Product<Lhs, Rhs>::Scalar;
1561 template <
typename Dest>
1562 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void scaleAndAddTo(Dest& dst,
const Lhs& lhs,
const Rhs& rhs,
1563 const Scalar& alpha) {
1564 triangular_diagonal_product_impl<Lhs::Mode,
OnTheRight,
typename Lhs::MatrixType,
1565 typename Rhs::DiagonalVectorType>::run(dst, lhs.nestedExpression(), rhs.diagonal(),
1570template <
typename Lhs,
typename Rhs,
int ProductTag>
1571struct generic_product_impl<Lhs, Rhs, DiagonalShape, TriangularShape, ProductTag>
1572 : generic_product_impl_base<Lhs, Rhs, generic_product_impl<Lhs, Rhs, DiagonalShape, TriangularShape, ProductTag>> {
1573 using Scalar =
typename Product<Lhs, Rhs>::Scalar;
1575 template <
typename Dest>
1576 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void scaleAndAddTo(Dest& dst,
const Lhs& lhs,
const Rhs& rhs,
1577 const Scalar& alpha) {
1578 triangular_diagonal_product_impl<Rhs::Mode,
OnTheLeft,
typename Rhs::MatrixType,
1579 typename Lhs::DiagonalVectorType>::run(dst, rhs.nestedExpression(), lhs.diagonal(),
1587template <
typename Lhs,
int LhsMode,
bool LhsIsVector,
typename Rhs,
int RhsMode,
bool RhsIsVector>
1588struct selfadjoint_product_impl;
1590template <
int Mode,
int ProductOrder,
typename MatrixType,
typename DiagonalType>
1591struct selfadjoint_diagonal_product_impl {
1594 template <
typename Dest,
typename Alpha>
1595 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void run(Dest& dst,
const MatrixType& matrix,
1596 const DiagonalType& diagonal,
const Alpha& alpha) {
1597 runImpl<true>(dst, matrix, diagonal, alpha);
1603 template <
typename Dest>
1604 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void runOverwrite(Dest& dst,
const MatrixType& matrix,
1605 const DiagonalType& diagonal) {
1606 using Scalar =
typename traits<MatrixType>::Scalar;
1607 runImpl<false>(dst, matrix, diagonal, Scalar(1));
1617 static constexpr Index BlockSize = 32;
1626 template <
bool Accumulate,
typename Dest,
typename Alpha>
1627 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void runImpl(Dest& dst,
const MatrixType& matrix,
1628 const DiagonalType& diagonal,
const Alpha& alpha) {
1629 EIGEN_INTERNAL_ASSERT_SQUARE_MSG(matrix,
"SelfAdjointView is only for squared matrices");
1630 EIGEN_INTERNAL_ASSERT_SIZE_MATCHES_ROWS_MSG(diagonal, matrix,
"invalid matrix product");
1632 const Index size = matrix.rows();
1635 for (Index col = 0; col < size; ++col) {
1636 EIGEN_IF_CONSTEXPR ((Mode &
Upper) ==
Upper) {
1637 storedSegment<Accumulate>(dst, matrix, diagonal, 0, col + 1, col, alpha);
1639 storedSegment<Accumulate>(dst, matrix, diagonal, col, size - col, col, alpha);
1644 for (Index ib = 0; ib < size; ib += BlockSize) {
1645 const Index ib_end = numext::mini(size, ib + BlockSize);
1646 const Index br = ib_end - ib;
1647 EIGEN_IF_CONSTEXPR ((Mode &
Upper) ==
Upper) {
1649 for (Index jb = 0; jb < ib; jb += BlockSize) {
1650 const Index bc = numext::mini(jb + BlockSize, ib) - jb;
1651 mirrorBlock<Accumulate>(dst, matrix, diagonal, ib, jb, br, bc, alpha);
1654 for (Index col = ib; col < ib_end; ++col)
1655 conjugateSegment<Accumulate>(dst, matrix, diagonal, col + 1, ib_end - col - 1, col, alpha);
1658 for (Index jb = ib_end; jb < size; jb += BlockSize) {
1659 const Index bc = numext::mini(size, jb + BlockSize) - jb;
1660 mirrorBlock<Accumulate>(dst, matrix, diagonal, ib, jb, br, bc, alpha);
1663 for (Index col = ib; col < ib_end; ++col)
1664 conjugateSegment<Accumulate>(dst, matrix, diagonal, ib, col - ib, col, alpha);
1669 template <
bool Accumulate,
typename Dest,
typename Alpha>
1670 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void storedSegment(Dest& dst,
const MatrixType& matrix,
1671 const DiagonalType& diagonal, Index begin, Index size,
1672 Index col,
const Alpha& alpha) {
1673 if (size <= 0)
return;
1674 auto dstSegment = dst.col(col).segment(begin, size);
1675 auto srcSegment = matrix.col(col).segment(begin, size);
1676 EIGEN_IF_CONSTEXPR (Accumulate) {
1677 diagonal_product_segment_impl<ProductOrder>::run(dstSegment, srcSegment, diagonal, begin, col, alpha);
1679 diagonal_product_segment_impl<ProductOrder>::runOverwrite(dstSegment, srcSegment, diagonal, begin, col);
1683 template <
bool Accumulate,
typename Dest,
typename Alpha>
1684 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void conjugateSegment(Dest& dst,
const MatrixType& matrix,
1685 const DiagonalType& diagonal, Index begin,
1686 Index size, Index col,
const Alpha& alpha) {
1687 if (size <= 0)
return;
1688 auto dstSegment = dst.col(col).segment(begin, size);
1689 auto srcSegment = matrix.row(col).segment(begin, size).conjugate().transpose();
1690 EIGEN_IF_CONSTEXPR (Accumulate) {
1691 diagonal_product_segment_impl<ProductOrder>::run(dstSegment, srcSegment, diagonal, begin, col, alpha);
1693 diagonal_product_segment_impl<ProductOrder>::runOverwrite(dstSegment, srcSegment, diagonal, begin, col);
1700 template <
bool Accumulate,
typename Dest,
typename Alpha>
1701 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void mirrorBlock(Dest& dst,
const MatrixType& matrix,
1702 const DiagonalType& diagonal, Index ib, Index jb,
1703 Index br, Index bc,
const Alpha& alpha) {
1704 auto dstBlock = dst.block(ib, jb, br, bc);
1705 auto srcAdjoint = matrix.block(jb, ib, bc, br).adjoint();
1706 EIGEN_IF_CONSTEXPR (ProductOrder ==
OnTheRight) {
1707 auto scaled = srcAdjoint * diagonal.segment(jb, bc).asDiagonal();
1708 EIGEN_IF_CONSTEXPR (Accumulate) {
1709 dstBlock += alpha * scaled;
1714 auto scaled = diagonal.segment(ib, br).asDiagonal() * srcAdjoint;
1715 EIGEN_IF_CONSTEXPR (Accumulate) {
1716 dstBlock += alpha * scaled;
1724template <
typename Lhs,
typename Rhs,
int ProductTag>
1725struct generic_product_impl<Lhs, Rhs, SelfAdjointShape, DenseShape, ProductTag>
1726 : generic_product_impl_base<Lhs, Rhs, generic_product_impl<Lhs, Rhs, SelfAdjointShape, DenseShape, ProductTag>> {
1727 using Scalar =
typename Product<Lhs, Rhs>::Scalar;
1729 template <
typename Dest>
1730 static EIGEN_DEVICE_FUNC
void scaleAndAddTo(Dest& dst,
const Lhs& lhs,
const Rhs& rhs,
const Scalar& alpha) {
1731 selfadjoint_product_impl<typename Lhs::MatrixType, Lhs::Mode, false, Rhs, 0, Rhs::ColsAtCompileTime == 1>::run(
1732 dst, lhs.nestedExpression(), rhs, alpha);
1736template <
typename Lhs,
typename Rhs,
int ProductTag>
1737struct generic_product_impl<Lhs, Rhs, DenseShape, SelfAdjointShape, ProductTag>
1738 : generic_product_impl_base<Lhs, Rhs, generic_product_impl<Lhs, Rhs, DenseShape, SelfAdjointShape, ProductTag>> {
1739 using Scalar =
typename Product<Lhs, Rhs>::Scalar;
1741 template <
typename Dest>
1742 static void scaleAndAddTo(Dest& dst,
const Lhs& lhs,
const Rhs& rhs,
const Scalar& alpha) {
1743 selfadjoint_product_impl<Lhs, 0, Lhs::RowsAtCompileTime == 1, typename Rhs::MatrixType, Rhs::Mode, false>::run(
1744 dst, lhs, rhs.nestedExpression(), alpha);
1748template <
typename Lhs,
typename Rhs,
int ProductTag>
1749struct generic_product_impl<Lhs, Rhs, SelfAdjointShape, DiagonalShape, ProductTag>
1750 : generic_product_impl_base<Lhs, Rhs, generic_product_impl<Lhs, Rhs, SelfAdjointShape, DiagonalShape, ProductTag>> {
1751 using Scalar =
typename Product<Lhs, Rhs>::Scalar;
1758 using LhsBlasTraits = blas_traits<typename Lhs::MatrixType>;
1760 static constexpr bool ConjLhs = LhsBlasTraits::NeedToConjugate;
1762 using ActualLhsMatrixXpr =
1763 decltype(LhsBlasTraits::extract(std::declval<const typename Lhs::MatrixType&>()).template conjugateIf<ConjLhs>());
1764 using ActualLhsMatrix = std::conditional_t<std::is_reference<typename LhsBlasTraits::ExtractType>::value,
1765 ActualLhsMatrixXpr, remove_all_t<ActualLhsMatrixXpr>>;
1766 using ActualLhsMatrixType = remove_all_t<ActualLhsMatrix>;
1768 selfadjoint_diagonal_product_impl<Lhs::Mode, OnTheRight, ActualLhsMatrixType, typename Rhs::DiagonalVectorType>;
1770 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE ActualLhsMatrix actualLhsMatrix(
const typename Lhs::MatrixType& matrix) {
1771 return LhsBlasTraits::extract(matrix).template conjugateIf<ConjLhs>();
1774 template <
typename Dest>
1775 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void evalTo(Dest& dst,
const Lhs& lhs,
const Rhs& rhs) {
1776 EIGEN_IF_CONSTEXPR (LhsBlasTraits::HasScalarFactor) {
1778 Scalar factor = LhsBlasTraits::extractScalarFactor(lhs.nestedExpression());
1780 Kernel::run(dst, actualLhsMatrix(lhs.nestedExpression()), rhs.diagonal(), factor);
1783 Kernel::runOverwrite(dst, actualLhsMatrix(lhs.nestedExpression()), rhs.diagonal());
1787 template <
typename Dest>
1788 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void scaleAndAddTo(Dest& dst,
const Lhs& lhs,
const Rhs& rhs,
1789 const Scalar& alpha) {
1790 Scalar combinedAlpha = alpha * LhsBlasTraits::extractScalarFactor(lhs.nestedExpression());
1791 Kernel::run(dst, actualLhsMatrix(lhs.nestedExpression()), rhs.diagonal(), combinedAlpha);
1795template <
typename Lhs,
typename Rhs,
int ProductTag>
1796struct generic_product_impl<Lhs, Rhs, DiagonalShape, SelfAdjointShape, ProductTag>
1797 : generic_product_impl_base<Lhs, Rhs, generic_product_impl<Lhs, Rhs, DiagonalShape, SelfAdjointShape, ProductTag>> {
1798 using Scalar =
typename Product<Lhs, Rhs>::Scalar;
1801 using RhsBlasTraits = blas_traits<typename Rhs::MatrixType>;
1802 static constexpr bool ConjRhs = RhsBlasTraits::NeedToConjugate;
1804 using ActualRhsMatrixXpr =
1805 decltype(RhsBlasTraits::extract(std::declval<const typename Rhs::MatrixType&>()).template conjugateIf<ConjRhs>());
1806 using ActualRhsMatrix = std::conditional_t<std::is_reference<typename RhsBlasTraits::ExtractType>::value,
1807 ActualRhsMatrixXpr, remove_all_t<ActualRhsMatrixXpr>>;
1808 using ActualRhsMatrixType = remove_all_t<ActualRhsMatrix>;
1810 selfadjoint_diagonal_product_impl<Rhs::Mode, OnTheLeft, ActualRhsMatrixType, typename Lhs::DiagonalVectorType>;
1812 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE ActualRhsMatrix actualRhsMatrix(
const typename Rhs::MatrixType& matrix) {
1813 return RhsBlasTraits::extract(matrix).template conjugateIf<ConjRhs>();
1816 template <
typename Dest>
1817 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void evalTo(Dest& dst,
const Lhs& lhs,
const Rhs& rhs) {
1818 EIGEN_IF_CONSTEXPR (RhsBlasTraits::HasScalarFactor) {
1819 Scalar factor = RhsBlasTraits::extractScalarFactor(rhs.nestedExpression());
1821 Kernel::run(dst, actualRhsMatrix(rhs.nestedExpression()), lhs.diagonal(), factor);
1823 Kernel::runOverwrite(dst, actualRhsMatrix(rhs.nestedExpression()), lhs.diagonal());
1827 template <
typename Dest>
1828 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void scaleAndAddTo(Dest& dst,
const Lhs& lhs,
const Rhs& rhs,
1829 const Scalar& alpha) {
1830 Scalar combinedAlpha = alpha * RhsBlasTraits::extractScalarFactor(rhs.nestedExpression());
1831 Kernel::run(dst, actualRhsMatrix(rhs.nestedExpression()), lhs.diagonal(), combinedAlpha);
1845template <typename View, typename Shape = typename evaluator_traits<View>::Shape>
1846struct densified_view_operand {
1847 using Scalar =
typename View::Scalar;
1848 template <
typename Dense>
1849 static Scalar run(Dense& dense,
const View& view) {
1855template <
typename View>
1856struct densified_view_operand<View, SelfAdjointShape> {
1857 using Scalar =
typename View::Scalar;
1858 using BlasTraits = blas_traits<typename View::MatrixType>;
1859 static constexpr unsigned int UpLo = unsigned(
int(View::Mode) &
int(
Upper |
Lower));
1860 template <
typename Dense>
1861 static Scalar run(Dense& dense,
const View& view) {
1862 dense = BlasTraits::extract(view.nestedExpression())
1863 .template conjugateIf<bool(BlasTraits::NeedToConjugate)>()
1864 .template selfadjointView<UpLo>();
1865 return BlasTraits::extractScalarFactor(view.nestedExpression());
1876template <
typename Lhs,
typename Rhs,
typename LhsShape,
typename RhsShape,
int ProductTag,
bool DensifyRhs>
1877struct structured_view_product_impl;
1879template <
typename Lhs,
typename Rhs,
typename LhsShape,
typename RhsShape,
int ProductTag>
1880struct structured_view_product_impl<Lhs, Rhs, LhsShape, RhsShape, ProductTag, true>
1881 : generic_product_impl_base<Lhs, Rhs,
1882 structured_view_product_impl<Lhs, Rhs, LhsShape, RhsShape, ProductTag, true>> {
1883 using Scalar =
typename Product<Lhs, Rhs>::Scalar;
1884 using RhsDenseType =
typename Rhs::DenseMatrixType;
1886 template <
typename Dest>
1887 static void scaleAndAddTo(Dest& dst,
const Lhs& lhs,
const Rhs& rhs,
const Scalar& alpha) {
1888 RhsDenseType rhsDense(rhs.rows(), rhs.cols());
1889 const Scalar factor = densified_view_operand<Rhs>::run(rhsDense, rhs);
1890 generic_product_impl<Lhs, RhsDenseType, LhsShape, DenseShape, ProductTag>::scaleAndAddTo(dst, lhs, rhsDense,
1895template <
typename Lhs,
typename Rhs,
typename LhsShape,
typename RhsShape,
int ProductTag>
1896struct structured_view_product_impl<Lhs, Rhs, LhsShape, RhsShape, ProductTag, false>
1897 : generic_product_impl_base<Lhs, Rhs,
1898 structured_view_product_impl<Lhs, Rhs, LhsShape, RhsShape, ProductTag, false>> {
1899 using Scalar =
typename Product<Lhs, Rhs>::Scalar;
1900 using LhsDenseType =
typename Lhs::DenseMatrixType;
1902 template <
typename Dest>
1903 static void scaleAndAddTo(Dest& dst,
const Lhs& lhs,
const Rhs& rhs,
const Scalar& alpha) {
1904 LhsDenseType lhsDense(lhs.rows(), lhs.cols());
1905 const Scalar factor = densified_view_operand<Lhs>::run(lhsDense, lhs);
1906 generic_product_impl<LhsDenseType, Rhs, DenseShape, RhsShape, ProductTag>::scaleAndAddTo(dst, lhsDense, rhs,
1911template <
typename Lhs,
typename Rhs,
int ProductTag>
1912struct generic_product_impl<Lhs, Rhs, TriangularShape, TriangularShape, ProductTag>
1913 : structured_view_product_impl<Lhs, Rhs, TriangularShape, TriangularShape, ProductTag, true> {};
1915template <
typename Lhs,
typename Rhs,
int ProductTag>
1916struct generic_product_impl<Lhs, Rhs, TriangularShape, SelfAdjointShape, ProductTag>
1917 : structured_view_product_impl<Lhs, Rhs, TriangularShape, SelfAdjointShape, ProductTag, true> {};
1919template <
typename Lhs,
typename Rhs,
int ProductTag>
1920struct generic_product_impl<Lhs, Rhs, SelfAdjointShape, TriangularShape, ProductTag>
1921 : structured_view_product_impl<Lhs, Rhs, SelfAdjointShape, TriangularShape, ProductTag, false> {};
1923template <
typename Lhs,
typename Rhs,
int ProductTag>
1924struct generic_product_impl<Lhs, Rhs, SelfAdjointShape, SelfAdjointShape, ProductTag>
1925 : structured_view_product_impl<Lhs, Rhs, SelfAdjointShape, SelfAdjointShape, ProductTag, true> {};
1931template <
typename MatrixType,
typename DiagonalType,
typename Derived,
int ProductOrder>
1932struct diagonal_product_evaluator_base : evaluator_base<Derived> {
1933 using Scalar =
typename Derived::Scalar;
1937 CoeffReadCost = int(NumTraits<Scalar>::MulCost) + int(evaluator<MatrixType>::CoeffReadCost) +
1938 int(evaluator<DiagonalType>::CoeffReadCost),
1940 MatrixFlags = evaluator<MatrixType>::Flags,
1941 DiagFlags = evaluator<DiagonalType>::Flags,
1943 StorageOrder_ = (Derived::MaxRowsAtCompileTime == 1 && Derived::MaxColsAtCompileTime != 1) ?
RowMajor
1944 : (Derived::MaxColsAtCompileTime == 1 && Derived::MaxRowsAtCompileTime != 1) ?
ColMajor
1949 ScalarAccessOnDiag_ = !((int(StorageOrder_) ==
ColMajor && int(ProductOrder) ==
OnTheLeft) ||
1951 SameTypes_ = std::is_same<typename MatrixType::Scalar, typename DiagonalType::Scalar>::value,
1955 Vectorizable_ = bool(int(MatrixFlags) &
PacketAccessBit) && SameTypes_ &&
1959 (MatrixType::RowsAtCompileTime == 1 || MatrixType::ColsAtCompileTime == 1) ?
LinearAccessBit : 0,
1960 Flags = ((HereditaryBits | LinearAccessMask_) & static_cast<unsigned int>(MatrixFlags)) |
1962 Alignment = evaluator<MatrixType>::Alignment,
1965 (DiagonalType::SizeAtCompileTime == 1) ||
1966 (DiagonalType::SizeAtCompileTime == Dynamic && MatrixType::RowsAtCompileTime == 1 &&
1968 (DiagonalType::SizeAtCompileTime == Dynamic && MatrixType::ColsAtCompileTime == 1 && ProductOrder ==
OnTheRight)
1971 EIGEN_DEVICE_FUNC diagonal_product_evaluator_base(
const MatrixType& mat,
const DiagonalType& diag)
1972 : m_diagImpl(diag), m_matImpl(mat) {
1973 EIGEN_INTERNAL_CHECK_COST_VALUE(NumTraits<Scalar>::MulCost);
1974 EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
1977 EIGEN_DEVICE_FUNC
constexpr EIGEN_STRONG_INLINE
const Scalar coeff(Index idx)
const {
1978 using DiagonalFirst = bool_constant<
1980 functor_is_commutative<scalar_product_op<typename MatrixType::Scalar, typename DiagonalType::Scalar>>::value>;
1981 return coeff_impl(idx, DiagonalFirst());
1985 EIGEN_DEVICE_FUNC
constexpr EIGEN_STRONG_INLINE Scalar coeff_impl(Index idx, std::true_type)
const {
1986 EIGEN_IF_CONSTEXPR (AsScalarProduct)
1987 return m_diagImpl.coeff(0) * m_matImpl.coeff(idx);
1989 return m_diagImpl.coeff(idx) * m_matImpl.coeff(idx);
1992 EIGEN_DEVICE_FUNC
constexpr EIGEN_STRONG_INLINE Scalar coeff_impl(Index idx, std::false_type)
const {
1993 EIGEN_IF_CONSTEXPR (AsScalarProduct)
1994 return m_matImpl.coeff(idx) * m_diagImpl.coeff(0);
1996 return m_matImpl.coeff(idx) * m_diagImpl.coeff(idx);
1999 template <
int LoadMode,
typename PacketType>
2000 EIGEN_STRONG_INLINE PacketType packet_impl(Index row, Index col, Index
id, std::true_type)
const {
2001 return internal::pmul(m_matImpl.template packet<LoadMode, PacketType>(row, col),
2002 internal::pset1<PacketType>(m_diagImpl.coeff(
id)));
2005 template <
int LoadMode,
typename PacketType>
2006 EIGEN_STRONG_INLINE PacketType packet_impl(Index row, Index col, Index
id, std::false_type)
const {
2007 enum { DiagonalPacketLoadMode = plain_enum_min(LoadMode,
int(evaluator<DiagonalType>::Alignment)) };
2008 return internal::pmul(m_matImpl.template packet<LoadMode, PacketType>(row, col),
2009 m_diagImpl.template packet<DiagonalPacketLoadMode, PacketType>(
id));
2012 template <
int LoadMode,
typename PacketType>
2013 EIGEN_STRONG_INLINE PacketType packet_segment_impl(Index row, Index col, Index
id, Index begin, Index count,
2014 std::true_type)
const {
2015 return internal::pmul(m_matImpl.template packetSegment<LoadMode, PacketType>(row, col, begin, count),
2016 internal::pset1<PacketType>(m_diagImpl.coeff(
id)));
2019 template <
int LoadMode,
typename PacketType>
2020 EIGEN_STRONG_INLINE PacketType packet_segment_impl(Index row, Index col, Index
id, Index begin, Index count,
2021 std::false_type)
const {
2022 enum { DiagonalPacketLoadMode = plain_enum_min(LoadMode,
int(evaluator<DiagonalType>::Alignment)) };
2023 return internal::pmul(m_matImpl.template packetSegment<LoadMode, PacketType>(row, col, begin, count),
2024 m_diagImpl.template packetSegment<DiagonalPacketLoadMode, PacketType>(
id, begin, count));
2027 evaluator<DiagonalType> m_diagImpl;
2028 evaluator<MatrixType> m_matImpl;
2032template <
typename Lhs,
typename Rhs,
int ProductKind,
int ProductTag>
2033struct product_evaluator<Product<Lhs, Rhs, ProductKind>, ProductTag, DiagonalShape, DenseShape>
2034 : diagonal_product_evaluator_base<Rhs, typename Lhs::DiagonalVectorType, Product<Lhs, Rhs, LazyProduct>,
2037 diagonal_product_evaluator_base<Rhs, typename Lhs::DiagonalVectorType, Product<Lhs, Rhs, LazyProduct>,
OnTheLeft>;
2039 using Base::m_diagImpl;
2040 using Base::m_matImpl;
2041 using Scalar =
typename Base::Scalar;
2043 using XprType = Product<Lhs, Rhs, ProductKind>;
2044 using PlainObject =
typename XprType::PlainObject;
2046 static constexpr int StorageOrder = Base::StorageOrder_;
2047 using IsRowMajor_t = bool_constant<StorageOrder == RowMajor>;
2049 EIGEN_DEVICE_FUNC
constexpr explicit product_evaluator(
const XprType& xpr) : Base(xpr.rhs(), xpr.lhs().diagonal()) {}
2051 EIGEN_DEVICE_FUNC
constexpr EIGEN_STRONG_INLINE
const Scalar coeff(Index row, Index col)
const {
2052 return m_diagImpl.coeff(row) * m_matImpl.coeff(row, col);
2056 template <
int LoadMode,
typename PacketType>
2057 EIGEN_STRONG_INLINE PacketType packet(Index row, Index col)
const {
2060 return this->
template packet_impl<LoadMode, PacketType>(row, col, row, IsRowMajor_t());
2063 template <
int LoadMode,
typename PacketType>
2064 EIGEN_STRONG_INLINE PacketType packet(Index idx)
const {
2065 return packet<LoadMode, PacketType>(
int(StorageOrder) == ColMajor ? idx : 0,
2066 int(StorageOrder) == ColMajor ? 0 : idx);
2069 template <
int LoadMode,
typename PacketType>
2070 EIGEN_STRONG_INLINE PacketType packetSegment(Index row, Index col, Index begin, Index count)
const {
2073 return this->
template packet_segment_impl<LoadMode, PacketType>(row, col, row, begin, count, IsRowMajor_t());
2076 template <
int LoadMode,
typename PacketType>
2077 EIGEN_STRONG_INLINE PacketType packetSegment(Index idx, Index begin, Index count)
const {
2078 return packetSegment<LoadMode, PacketType>(StorageOrder == ColMajor ? idx : 0, StorageOrder == ColMajor ? 0 : idx,
2085template <
typename Lhs,
typename Rhs,
int ProductKind,
int ProductTag>
2086struct product_evaluator<Product<Lhs, Rhs, ProductKind>, ProductTag, DenseShape, DiagonalShape>
2087 : diagonal_product_evaluator_base<Lhs, typename Rhs::DiagonalVectorType, Product<Lhs, Rhs, LazyProduct>,
2089 using Base = diagonal_product_evaluator_base<Lhs, typename Rhs::DiagonalVectorType, Product<Lhs, Rhs, LazyProduct>,
2092 using Base::m_diagImpl;
2093 using Base::m_matImpl;
2094 using Scalar =
typename Base::Scalar;
2096 using XprType = Product<Lhs, Rhs, ProductKind>;
2097 using PlainObject =
typename XprType::PlainObject;
2099 static constexpr int StorageOrder = Base::StorageOrder_;
2100 using IsColMajor_t = bool_constant<StorageOrder == ColMajor>;
2102 EIGEN_DEVICE_FUNC
constexpr explicit product_evaluator(
const XprType& xpr) : Base(xpr.lhs(), xpr.rhs().diagonal()) {}
2104 EIGEN_DEVICE_FUNC
constexpr EIGEN_STRONG_INLINE
const Scalar coeff(Index row, Index col)
const {
2105 return m_matImpl.coeff(row, col) * m_diagImpl.coeff(col);
2109 template <
int LoadMode,
typename PacketType>
2110 EIGEN_STRONG_INLINE PacketType packet(Index row, Index col)
const {
2111 return this->
template packet_impl<LoadMode, PacketType>(row, col, col, IsColMajor_t());
2114 template <
int LoadMode,
typename PacketType>
2115 EIGEN_STRONG_INLINE PacketType packet(Index idx)
const {
2116 return packet<LoadMode, PacketType>(StorageOrder ==
ColMajor ? idx : 0, StorageOrder ==
ColMajor ? 0 : idx);
2119 template <
int LoadMode,
typename PacketType>
2120 EIGEN_STRONG_INLINE PacketType packetSegment(Index row, Index col, Index begin, Index count)
const {
2121 return this->
template packet_segment_impl<LoadMode, PacketType>(row, col, col, begin, count, IsColMajor_t());
2124 template <
int LoadMode,
typename PacketType>
2125 EIGEN_STRONG_INLINE PacketType packetSegment(Index idx, Index begin, Index count)
const {
2126 return packetSegment<LoadMode, PacketType>(StorageOrder ==
ColMajor ? idx : 0, StorageOrder ==
ColMajor ? 0 : idx,
2132template <
typename MatrixScalar,
typename DiagonalScalar>
2133EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
auto diagonal_product_coeff(
const MatrixScalar& matrix,
2134 const DiagonalScalar& diagonal, true_type)
2135 ->
decltype(diagonal * matrix) {
2136 return diagonal * matrix;
2139template <
typename MatrixScalar,
typename DiagonalScalar>
2140EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
auto diagonal_product_coeff(
const MatrixScalar& matrix,
2141 const DiagonalScalar& diagonal, false_type)
2142 ->
decltype(matrix * diagonal) {
2143 return matrix * diagonal;
2157template <
int Mode,
int ProductOrder,
typename MatrixType,
typename DiagonalType,
typename ProductXpr>
2158struct triangular_diagonal_product_lazy_evaluator_base : evaluator_base<ProductXpr> {
2159 using Scalar =
typename ProductXpr::Scalar;
2160 using DiagonalOnLeft = bool_constant<ProductOrder == OnTheLeft>;
2161 using MatrixScalar =
typename MatrixType::Scalar;
2164 CoeffReadCost = int(NumTraits<Scalar>::MulCost) + int(evaluator<MatrixType>::CoeffReadCost) +
2165 int(evaluator<DiagonalType>::CoeffReadCost),
2166 Flags = HereditaryBits &
static_cast<unsigned int>(evaluator<MatrixType>::Flags),
2170 EIGEN_DEVICE_FUNC triangular_diagonal_product_lazy_evaluator_base(
const MatrixType& mat,
const DiagonalType& diag)
2171 : m_diagImpl(diag), m_matImpl(mat) {}
2173 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar coeff(Index row, Index col)
const {
2174 const bool inActive = ((Mode &
Upper) ==
Upper) ? (row <= col) : (row >= col);
2175 if (!inActive)
return Scalar(0);
2178 return Scalar(diagonal_product_coeff(MatrixScalar(1), m_diagImpl.coeff(row), DiagonalOnLeft()));
2182 return Scalar(diagonal_product_coeff(m_matImpl.coeff(row, col),
2183 m_diagImpl.coeff(ProductOrder ==
OnTheLeft ? row : col), DiagonalOnLeft()));
2187 evaluator<DiagonalType> m_diagImpl;
2188 evaluator<MatrixType> m_matImpl;
2192template <
typename Lhs,
typename Rhs,
int ProductKind,
int ProductTag>
2193struct product_evaluator<Product<Lhs, Rhs, ProductKind>, ProductTag, TriangularShape, DiagonalShape>
2194 : triangular_diagonal_product_lazy_evaluator_base<Lhs::Mode, OnTheRight, typename Lhs::MatrixType,
2195 typename Rhs::DiagonalVectorType,
2196 Product<Lhs, Rhs, ProductKind>> {
2197 using XprType = Product<Lhs, Rhs, ProductKind>;
2198 using Base = triangular_diagonal_product_lazy_evaluator_base<Lhs::Mode,
OnTheRight,
typename Lhs::MatrixType,
2199 typename Rhs::DiagonalVectorType, XprType>;
2201 EIGEN_DEVICE_FUNC
explicit product_evaluator(
const XprType& xpr)
2202 : Base(xpr.lhs().nestedExpression(), xpr.rhs().diagonal()) {}
2206template <
typename Lhs,
typename Rhs,
int ProductKind,
int ProductTag>
2207struct product_evaluator<Product<Lhs, Rhs, ProductKind>, ProductTag, DiagonalShape, TriangularShape>
2208 : triangular_diagonal_product_lazy_evaluator_base<Rhs::Mode, OnTheLeft, typename Rhs::MatrixType,
2209 typename Lhs::DiagonalVectorType,
2210 Product<Lhs, Rhs, ProductKind>> {
2211 using XprType = Product<Lhs, Rhs, ProductKind>;
2212 using Base = triangular_diagonal_product_lazy_evaluator_base<Rhs::Mode,
OnTheLeft,
typename Rhs::MatrixType,
2213 typename Lhs::DiagonalVectorType, XprType>;
2215 EIGEN_DEVICE_FUNC
explicit product_evaluator(
const XprType& xpr)
2216 : Base(xpr.rhs().nestedExpression(), xpr.lhs().diagonal()) {}
2221template <
int Mode,
int ProductOrder,
typename MatrixType,
typename DiagonalType,
typename ProductXpr,
2223 NumTraits<typename MatrixType::Scalar>::IsComplex && blas_traits<MatrixType>::HasScalarFactor>
2224struct selfadjoint_diagonal_product_lazy_evaluator_base : evaluator_base<ProductXpr> {
2225 using Scalar =
typename ProductXpr::Scalar;
2226 using MatrixScalar =
typename MatrixType::Scalar;
2227 using DiagonalOnLeft = bool_constant<ProductOrder == OnTheLeft>;
2230 CoeffReadCost = int(NumTraits<Scalar>::MulCost) + int(evaluator<MatrixType>::CoeffReadCost) +
2231 int(evaluator<DiagonalType>::CoeffReadCost),
2232 Flags = HereditaryBits &
static_cast<unsigned int>(evaluator<MatrixType>::Flags),
2236 EIGEN_DEVICE_FUNC selfadjoint_diagonal_product_lazy_evaluator_base(
const ProductXpr&,
const MatrixType& mat,
2237 const DiagonalType& diag)
2238 : m_diagImpl(diag), m_matImpl(mat) {}
2240 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar coeff(Index row, Index col)
const {
2241 const bool storedHere = ((Mode &
Upper) ==
Upper) ? (row <= col) : (row >= col);
2242 const MatrixScalar matCoeff =
2243 storedHere ? MatrixScalar(m_matImpl.coeff(row, col)) : MatrixScalar(numext::conj(m_matImpl.coeff(col, row)));
2245 diagonal_product_coeff(matCoeff, m_diagImpl.coeff(ProductOrder == OnTheLeft ? row : col), DiagonalOnLeft()));
2249 evaluator<DiagonalType> m_diagImpl;
2250 evaluator<MatrixType> m_matImpl;
2255template <
int Mode,
int ProductOrder,
typename MatrixType,
typename DiagonalType,
typename ProductXpr>
2256struct selfadjoint_diagonal_product_lazy_evaluator_base<Mode, ProductOrder, MatrixType, DiagonalType, ProductXpr, true>
2257 : evaluator<EvalToTemp<Product<typename ProductXpr::Lhs, typename ProductXpr::Rhs, DefaultProduct>>> {
2258 using DefaultProductType = Product<typename ProductXpr::Lhs, typename ProductXpr::Rhs, DefaultProduct>;
2259 using Base = evaluator<EvalToTemp<DefaultProductType>>;
2260 EIGEN_DEVICE_FUNC selfadjoint_diagonal_product_lazy_evaluator_base(
const ProductXpr& xpr,
const MatrixType&,
2261 const DiagonalType&)
2262 : Base(DefaultProductType(xpr.lhs(), xpr.rhs())) {}
2266template <
typename Lhs,
typename Rhs,
int ProductKind,
int ProductTag>
2267struct product_evaluator<Product<Lhs, Rhs, ProductKind>, ProductTag, SelfAdjointShape, DiagonalShape>
2268 : selfadjoint_diagonal_product_lazy_evaluator_base<Lhs::Mode, OnTheRight, typename Lhs::MatrixType,
2269 typename Rhs::DiagonalVectorType,
2270 Product<Lhs, Rhs, ProductKind>> {
2271 using XprType = Product<Lhs, Rhs, ProductKind>;
2272 using Base = selfadjoint_diagonal_product_lazy_evaluator_base<Lhs::Mode,
OnTheRight,
typename Lhs::MatrixType,
2273 typename Rhs::DiagonalVectorType, XprType>;
2275 EIGEN_DEVICE_FUNC
explicit product_evaluator(
const XprType& xpr)
2276 : Base(xpr, xpr.lhs().nestedExpression(), xpr.rhs().diagonal()) {}
2280template <
typename Lhs,
typename Rhs,
int ProductKind,
int ProductTag>
2281struct product_evaluator<Product<Lhs, Rhs, ProductKind>, ProductTag, DiagonalShape, SelfAdjointShape>
2282 : selfadjoint_diagonal_product_lazy_evaluator_base<Rhs::Mode, OnTheLeft, typename Rhs::MatrixType,
2283 typename Lhs::DiagonalVectorType,
2284 Product<Lhs, Rhs, ProductKind>> {
2285 using XprType = Product<Lhs, Rhs, ProductKind>;
2286 using Base = selfadjoint_diagonal_product_lazy_evaluator_base<Rhs::Mode,
OnTheLeft,
typename Rhs::MatrixType,
2287 typename Lhs::DiagonalVectorType, XprType>;
2289 EIGEN_DEVICE_FUNC
explicit product_evaluator(
const XprType& xpr)
2290 : Base(xpr, xpr.rhs().nestedExpression(), xpr.lhs().diagonal()) {}
2294template <
typename Lhs,
typename Rhs,
typename Derived>
2295struct product_accumulate_through_temporary {
2296 template <
typename Dest>
2297 static EIGEN_DEVICE_FUNC
void addTo(Dest& dst,
const Lhs& lhs,
const Rhs& rhs) {
2298 typename Product<Lhs, Rhs>::PlainObject tmp(lhs.rows(), rhs.cols());
2299 Derived::evalTo(tmp, lhs, rhs);
2303 template <
typename Dest>
2304 static EIGEN_DEVICE_FUNC
void subTo(Dest& dst,
const Lhs& lhs,
const Rhs& rhs) {
2305 typename Product<Lhs, Rhs>::PlainObject tmp(lhs.rows(), rhs.cols());
2306 Derived::evalTo(tmp, lhs, rhs);
2311template <
typename Lhs,
typename Rhs,
typename Derived,
typename MatrixShape>
2312struct indexed_product_accumulation : product_accumulate_through_temporary<Lhs, Rhs, Derived> {};
2314template <
typename Lhs,
typename Rhs,
typename Derived>
2315struct indexed_product_accumulation<Lhs, Rhs, Derived, DenseShape> {
2316 template <
typename Dest>
2317 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void addTo(Dest& dst,
const Lhs& lhs,
const Rhs& rhs) {
2318 Derived::accumulate(dst, lhs, rhs, add_assign_op<
typename Dest::Scalar,
typename Product<Lhs, Rhs>::Scalar>());
2321 template <
typename Dest>
2322 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void subTo(Dest& dst,
const Lhs& lhs,
const Rhs& rhs) {
2323 Derived::accumulate(dst, lhs, rhs, sub_assign_op<
typename Dest::Scalar,
typename Product<Lhs, Rhs>::Scalar>());
2327template <
typename Lhs,
typename Rhs,
typename Derived>
2328struct indexed_product_accumulation<Lhs, Rhs, Derived, TriangularShape>
2329 : indexed_product_accumulation<Lhs, Rhs, Derived, DenseShape> {};
2331template <
typename Lhs,
typename Rhs,
typename Derived>
2332struct indexed_product_accumulation<Lhs, Rhs, Derived, SelfAdjointShape>
2333 : indexed_product_accumulation<Lhs, Rhs, Derived, DenseShape> {};
2335template <
int S
ide,
bool Transposed,
typename Dest,
typename Indices,
typename Expression,
typename Func>
2336EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void indexed_matrix_update(Dest& dst,
const Indices& indices,
2337 const Expression& xpr,
const Func& func) {
2338 using MatrixType =
typename nested_eval<Expression, 1>::type;
2339 using MatrixTypeCleaned = remove_all_t<MatrixType>;
2340 MatrixType mat(xpr);
2342 typename nested_eval<Indices, Dynamic>::type actualIndices(indices);
2343 const Index n = Side ==
OnTheLeft ? mat.rows() : mat.cols();
2344 EIGEN_IF_CONSTEXPR (
bool(Dest::IsRowMajor) ==
bool(MatrixTypeCleaned::IsRowMajor) &&
2345 bool(Dest::IsRowMajor) == (Side ==
OnTheRight)) {
2346 for (Index j = 0; j < (Side ==
OnTheLeft ? mat.cols() : mat.rows()); ++j) {
2347 auto target = dst.template subVector < Side == OnTheLeft ? Vertical : Horizontal > (j);
2348 const auto source = mat.template subVector < Side == OnTheLeft ? Vertical : Horizontal > (j);
2349 for (Index i = 0; i < n; ++i) {
2350 const Index p = actualIndices.coeff(i);
2351 func.assignCoeff(target.coeffRef(((Side ==
OnTheLeft) ^ Transposed) ? p : i),
2352 source.coeff(((Side ==
OnTheRight) ^ Transposed) ? p : i));
2356 for (Index i = 0; i < n; ++i) {
2357 const Index p = actualIndices.coeff(i);
2358 auto target = dst.template subVector < Side ==
OnTheLeft
2361 const auto source = mat.template subVector < Side ==
OnTheLeft
2364 call_assignment_no_alias(target, source, func);
2381template <
typename ExpressionType,
int S
ide,
bool Transposed,
typename ExpressionShape>
2382struct permutation_matrix_product {
2383 template <
typename Dest,
typename PermutationType>
2384 static EIGEN_DEVICE_FUNC
void run(Dest&,
const PermutationType&,
const ExpressionType&) {
2385 static_assert(std::is_same<ExpressionShape, DenseShape>::value,
2386 "PERMUTATION_PRODUCTS_ARE_NOT_IMPLEMENTED_FOR_THIS_OPERAND_SHAPE");
2390template <
typename ExpressionType,
int S
ide,
bool Transposed>
2391struct permutation_matrix_product<ExpressionType, Side, Transposed, DenseShape> {
2392 using MatrixType =
typename nested_eval<ExpressionType, 1>::type;
2393 using MatrixTypeCleaned = remove_all_t<MatrixType>;
2395 template <
typename Dest,
typename Indices,
typename Func>
2396 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void accumulate(Dest& dst,
const Indices& indices,
2397 const ExpressionType& xpr,
const Func& func) {
2398 indexed_matrix_update<Side, Transposed>(dst, indices, xpr, func);
2401 template <
typename Dest,
typename PermutationType>
2402 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void run(Dest& dst,
const PermutationType& perm,
2403 const ExpressionType& xpr) {
2404 MatrixType mat(xpr);
2405 const Index n = Side ==
OnTheLeft ? mat.rows() : mat.cols();
2409 if (is_same_dense(dst, mat)) {
2411 Matrix<bool, PermutationType::RowsAtCompileTime, 1, 0, PermutationType::MaxRowsAtCompileTime> mask(perm.size());
2414 while (r < perm.size()) {
2416 while (r < perm.size() && mask[r]) r++;
2417 if (r >= perm.size())
break;
2421 mask.coeffRef(k0) =
true;
2422 for (Index k = perm.indices().coeff(k0); k != k0; k = perm.indices().coeff(k)) {
2423 Block<Dest, Side ==
OnTheLeft ? 1 : Dest::RowsAtCompileTime,
2424 Side ==
OnTheRight ? 1 : Dest::ColsAtCompileTime>(dst, k)
2425 .swap(Block < Dest, Side == OnTheLeft ? 1 : Dest::RowsAtCompileTime,
2428 : Dest::ColsAtCompileTime > (dst, ((Side == OnTheLeft) ^ Transposed) ? k0 : kPrev));
2430 mask.coeffRef(k) =
true;
2435 for (Index i = 0; i < n; ++i) {
2436 Block<Dest, Side == OnTheLeft ? 1 : Dest::RowsAtCompileTime, Side == OnTheRight ? 1 : Dest::ColsAtCompileTime>(
2437 dst, ((Side == OnTheLeft) ^ Transposed) ? perm.indices().coeff(i) : i)
2441 Block <
const MatrixTypeCleaned,
2442 Side ==
OnTheLeft ? 1 : MatrixTypeCleaned::RowsAtCompileTime,
2444 : MatrixTypeCleaned::ColsAtCompileTime >
2445 (mat, ((Side ==
OnTheRight) ^ Transposed) ? perm.indices().coeff(i) : i);
2456template <
typename ExpressionType,
int S
ide,
bool Transposed>
2457struct permutation_densified_matrix_product {
2458 using DenseType =
typename ExpressionType::DenseMatrixType;
2460 template <
typename Dest,
typename Indices,
typename Func>
2461 static EIGEN_DEVICE_FUNC
void accumulate(Dest& dst,
const Indices& indices,
const ExpressionType& xpr,
2463 const DenseType dense(xpr);
2464 indexed_matrix_update<Side, Transposed>(dst, indices, dense, func);
2467 template <
typename Dest,
typename PermutationType>
2468 static EIGEN_DEVICE_FUNC
void run(Dest& dst,
const PermutationType& perm,
const ExpressionType& xpr) {
2469 const DenseType dense(xpr);
2470 permutation_matrix_product<DenseType, Side, Transposed, DenseShape>::run(dst, perm, dense);
2474template <
typename ExpressionType,
int S
ide,
bool Transposed>
2475struct permutation_matrix_product<ExpressionType, Side, Transposed, TriangularShape>
2476 : permutation_densified_matrix_product<ExpressionType, Side, Transposed> {};
2480template <
typename ExpressionType,
int S
ide,
bool Transposed>
2481struct permutation_matrix_product<ExpressionType, Side, Transposed, SelfAdjointShape> {
2482 using DenseType =
typename ExpressionType::DenseMatrixType;
2484 template <
typename Dest,
typename Indices,
typename Func>
2485 static void accumulate(Dest& dst,
const Indices& indices,
const ExpressionType& xpr,
const Func& func) {
2486 DenseType dense(xpr.rows(), xpr.cols());
2487 const typename ExpressionType::Scalar factor = densified_view_operand<ExpressionType>::run(dense, xpr);
2488 EIGEN_IF_CONSTEXPR (blas_traits<typename ExpressionType::MatrixType>::HasScalarFactor) dense *= factor;
2489 indexed_matrix_update<Side, Transposed>(dst, indices, dense, func);
2492 template <
typename Dest,
typename PermutationType>
2493 static void run(Dest& dst,
const PermutationType& perm,
const ExpressionType& xpr) {
2494 DenseType dense(xpr.rows(), xpr.cols());
2495 const typename ExpressionType::Scalar factor = densified_view_operand<ExpressionType>::run(dense, xpr);
2496 EIGEN_IF_CONSTEXPR (blas_traits<typename ExpressionType::MatrixType>::HasScalarFactor) dense *= factor;
2497 permutation_matrix_product<DenseType, Side, Transposed, DenseShape>::run(dst, perm, dense);
2501template <
typename Lhs,
typename Rhs,
int ProductTag,
typename MatrixShape>
2502struct generic_product_impl<Lhs, Rhs, PermutationShape, MatrixShape, ProductTag>
2503 : indexed_product_accumulation<Lhs, Rhs, generic_product_impl<Lhs, Rhs, PermutationShape, MatrixShape, ProductTag>,
2505 template <
typename Dest,
typename Func>
2506 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void accumulate(Dest& dst,
const Lhs& lhs,
const Rhs& rhs,
2508 permutation_matrix_product<Rhs, OnTheLeft, false, MatrixShape>::accumulate(dst, lhs.indices(), rhs, func);
2510 template <
typename Dest>
2511 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void evalTo(Dest& dst,
const Lhs& lhs,
const Rhs& rhs) {
2512 permutation_matrix_product<Rhs, OnTheLeft, false, MatrixShape>::run(dst, lhs, rhs);
2516template <
typename Lhs,
typename Rhs,
int ProductTag,
typename MatrixShape>
2517struct generic_product_impl<Lhs, Rhs, MatrixShape, PermutationShape, ProductTag>
2518 : indexed_product_accumulation<Lhs, Rhs, generic_product_impl<Lhs, Rhs, MatrixShape, PermutationShape, ProductTag>,
2520 template <
typename Dest,
typename Func>
2521 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void accumulate(Dest& dst,
const Lhs& lhs,
const Rhs& rhs,
2523 permutation_matrix_product<Lhs, OnTheRight, false, MatrixShape>::accumulate(dst, rhs.indices(), lhs, func);
2525 template <
typename Dest>
2526 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void evalTo(Dest& dst,
const Lhs& lhs,
const Rhs& rhs) {
2527 permutation_matrix_product<Lhs, OnTheRight, false, MatrixShape>::run(dst, rhs, lhs);
2531template <
typename Lhs,
typename Rhs,
int ProductTag,
typename MatrixShape>
2532struct generic_product_impl<Inverse<Lhs>, Rhs, PermutationShape, MatrixShape, ProductTag>
2533 : indexed_product_accumulation<Inverse<Lhs>, Rhs,
2534 generic_product_impl<Inverse<Lhs>, Rhs, PermutationShape, MatrixShape, ProductTag>,
2536 template <
typename Dest,
typename Func>
2537 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void accumulate(Dest& dst,
const Inverse<Lhs>& lhs,
const Rhs& rhs,
2539 permutation_matrix_product<Rhs, OnTheLeft, true, MatrixShape>::accumulate(dst, lhs.nestedExpression().indices(),
2542 template <
typename Dest>
2543 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void evalTo(Dest& dst,
const Inverse<Lhs>& lhs,
const Rhs& rhs) {
2544 permutation_matrix_product<Rhs, OnTheLeft, true, MatrixShape>::run(dst, lhs.nestedExpression(), rhs);
2548template <
typename Lhs,
typename Rhs,
int ProductTag,
typename MatrixShape>
2549struct generic_product_impl<Lhs, Inverse<Rhs>, MatrixShape, PermutationShape, ProductTag>
2550 : indexed_product_accumulation<Lhs, Inverse<Rhs>,
2551 generic_product_impl<Lhs, Inverse<Rhs>, MatrixShape, PermutationShape, ProductTag>,
2553 template <
typename Dest,
typename Func>
2554 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void accumulate(Dest& dst,
const Lhs& lhs,
const Inverse<Rhs>& rhs,
2556 permutation_matrix_product<Lhs, OnTheRight, true, MatrixShape>::accumulate(dst, rhs.nestedExpression().indices(),
2559 template <
typename Dest>
2560 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void evalTo(Dest& dst,
const Lhs& lhs,
const Inverse<Rhs>& rhs) {
2561 permutation_matrix_product<Lhs, OnTheRight, true, MatrixShape>::run(dst, rhs.nestedExpression(), lhs);
2570template <
typename Lhs,
typename Rhs,
typename DstScalar,
typename SrcScalar>
2571struct assignment_assume_aliasing<Product<Lhs, Rhs, AliasFreeProduct>, add_assign_op<DstScalar, SrcScalar>>
2572 : bool_constant<evaluator_assume_aliasing<Product<Lhs, Rhs, AliasFreeProduct>>::value ||
2573 std::is_same<typename evaluator_traits<Lhs>::Shape, TranspositionsShape>::value ||
2574 std::is_same<typename evaluator_traits<Rhs>::Shape, TranspositionsShape>::value> {};
2576template <
typename Lhs,
typename Rhs,
typename DstScalar,
typename SrcScalar>
2577struct assignment_assume_aliasing<Product<Lhs, Rhs, AliasFreeProduct>, sub_assign_op<DstScalar, SrcScalar>>
2578 : assignment_assume_aliasing<Product<Lhs, Rhs, AliasFreeProduct>, add_assign_op<DstScalar, SrcScalar>> {};
2586template <
typename ExpressionType,
int S
ide,
bool Transposed,
typename ExpressionShape>
2587struct transposition_matrix_product {
2588 using MatrixType =
typename nested_eval<ExpressionType, 1>::type;
2589 using MatrixTypeCleaned = remove_all_t<MatrixType>;
2591 template <
typename Dest,
typename TranspositionType,
typename Func>
2592 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void accumulate(Dest& dst,
const TranspositionType& tr,
2593 const ExpressionType& xpr,
const Func& func) {
2594 using SourceIndices =
typename TranspositionType::IndicesType;
2596 constexpr bool FitsStack = SourceIndices::MaxSizeAtCompileTime != Dynamic &&
2597 (EIGEN_STACK_ALLOCATION_LIMIT == 0 || std::size_t(SourceIndices::MaxSizeAtCompileTime) <=
2598 EIGEN_STACK_ALLOCATION_LIMIT /
sizeof(Index));
2599 using Indices = Matrix<Index, FitsStack ? SourceIndices::SizeAtCompileTime : Dynamic, 1, 0,
2600 FitsStack ? SourceIndices::MaxSizeAtCompileTime : Dynamic, 1>;
2601 Indices indices(tr.size());
2602 for (Index i = 0; i < tr.size(); ++i) indices.coeffRef(i) = i;
2603 for (Index k = tr.size(); k > 0; --k) numext::swap(indices.coeffRef(k - 1), indices.coeffRef(tr.coeff(k - 1)));
2605 indexed_matrix_update<Side, Transposed ^ (Side == OnTheRight)>(dst, indices, xpr, func);
2608 template <
typename Dest,
typename TranspositionType>
2609 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void run(Dest& dst,
const TranspositionType& tr,
2610 const ExpressionType& xpr) {
2612 static_assert(std::is_same<ExpressionShape, DenseShape>::value,
2613 "TRANSPOSITIONS_PRODUCTS_ARE_ONLY_IMPLEMENTED_FOR_DENSE_OPERANDS");
2614 MatrixType mat(xpr);
2615 using StorageIndex =
typename TranspositionType::StorageIndex;
2616 const Index size = tr.size();
2618 if (!is_same_dense(dst, mat)) dst = mat;
2620 for (Index k = (Transposed ? size - 1 : 0); Transposed ? k >= 0 : k < size; Transposed ? --k : ++k) {
2621 const StorageIndex j = tr.coeff(k);
2622 if (Index(j) != k) {
2623 EIGEN_IF_CONSTEXPR (Side == OnTheLeft) {
2624 dst.row(k).swap(dst.row(j));
2625 }
else EIGEN_IF_CONSTEXPR (Side == OnTheRight) {
2626 dst.col(k).swap(dst.col(j));
2633template <
typename Lhs,
typename Rhs,
int ProductTag,
typename MatrixShape>
2634struct generic_product_impl<Lhs, Rhs, TranspositionsShape, MatrixShape, ProductTag>
2635 : indexed_product_accumulation<
2636 Lhs, Rhs, generic_product_impl<Lhs, Rhs, TranspositionsShape, MatrixShape, ProductTag>, MatrixShape> {
2637 template <
typename Dest,
typename Func>
2638 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void accumulate(Dest& dst,
const Lhs& lhs,
const Rhs& rhs,
2640 transposition_matrix_product<Rhs, OnTheLeft, false, MatrixShape>::accumulate(dst, lhs, rhs, func);
2642 template <
typename Dest>
2643 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void evalTo(Dest& dst,
const Lhs& lhs,
const Rhs& rhs) {
2644 transposition_matrix_product<Rhs, OnTheLeft, false, MatrixShape>::run(dst, lhs, rhs);
2648template <
typename Lhs,
typename Rhs,
int ProductTag,
typename MatrixShape>
2649struct generic_product_impl<Lhs, Rhs, MatrixShape, TranspositionsShape, ProductTag>
2650 : indexed_product_accumulation<
2651 Lhs, Rhs, generic_product_impl<Lhs, Rhs, MatrixShape, TranspositionsShape, ProductTag>, MatrixShape> {
2652 template <
typename Dest,
typename Func>
2653 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void accumulate(Dest& dst,
const Lhs& lhs,
const Rhs& rhs,
2655 transposition_matrix_product<Lhs, OnTheRight, false, MatrixShape>::accumulate(dst, rhs, lhs, func);
2657 template <
typename Dest>
2658 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void evalTo(Dest& dst,
const Lhs& lhs,
const Rhs& rhs) {
2659 transposition_matrix_product<Lhs, OnTheRight, false, MatrixShape>::run(dst, rhs, lhs);
2663template <
typename Lhs,
typename Rhs,
int ProductTag,
typename MatrixShape>
2664struct generic_product_impl<Transpose<Lhs>, Rhs, TranspositionsShape, MatrixShape, ProductTag>
2665 : indexed_product_accumulation<
2666 Transpose<Lhs>, Rhs, generic_product_impl<Transpose<Lhs>, Rhs, TranspositionsShape, MatrixShape, ProductTag>,
2668 template <
typename Dest,
typename Func>
2669 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void accumulate(Dest& dst,
const Transpose<Lhs>& lhs,
const Rhs& rhs,
2671 transposition_matrix_product<Rhs, OnTheLeft, true, MatrixShape>::accumulate(dst, lhs.nestedExpression(), rhs, func);
2673 template <
typename Dest>
2674 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void evalTo(Dest& dst,
const Transpose<Lhs>& lhs,
const Rhs& rhs) {
2675 transposition_matrix_product<Rhs, OnTheLeft, true, MatrixShape>::run(dst, lhs.nestedExpression(), rhs);
2679template <
typename Lhs,
typename Rhs,
int ProductTag,
typename MatrixShape>
2680struct generic_product_impl<Lhs, Transpose<Rhs>, MatrixShape, TranspositionsShape, ProductTag>
2681 : indexed_product_accumulation<
2682 Lhs, Transpose<Rhs>, generic_product_impl<Lhs, Transpose<Rhs>, MatrixShape, TranspositionsShape, ProductTag>,
2684 template <
typename Dest,
typename Func>
2685 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void accumulate(Dest& dst,
const Lhs& lhs,
const Transpose<Rhs>& rhs,
2687 transposition_matrix_product<Lhs, OnTheRight, true, MatrixShape>::accumulate(dst, rhs.nestedExpression(), lhs,
2690 template <
typename Dest>
2691 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void evalTo(Dest& dst,
const Lhs& lhs,
const Transpose<Rhs>& rhs) {
2692 transposition_matrix_product<Lhs, OnTheRight, true, MatrixShape>::run(dst, rhs.nestedExpression(), lhs);
2702template <
typename Lhs,
typename Rhs,
int ProductTag,
typename MatrixShape>
2703struct generic_product_impl<Lhs, Rhs, SkewSymmetricShape, MatrixShape, ProductTag> {
2704 template <
typename Dest>
2705 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void evalTo(Dest& dst,
const Lhs& lhs,
const Rhs& rhs) {
2706 call_assignment_no_alias(dst,
typename Lhs::DenseMatrixType(lhs) * rhs);
2710template <
typename Lhs,
typename Rhs,
int ProductTag,
typename MatrixShape>
2711struct generic_product_impl<Lhs, Rhs, MatrixShape, SkewSymmetricShape, ProductTag> {
2712 template <
typename Dest>
2713 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void evalTo(Dest& dst,
const Lhs& lhs,
const Rhs& rhs) {
2714 call_assignment_no_alias(dst, lhs *
typename Rhs::DenseMatrixType(rhs));
2718template <
typename Lhs,
typename Rhs,
int ProductTag>
2719struct generic_product_impl<Lhs, Rhs, SkewSymmetricShape, SkewSymmetricShape, ProductTag> {
2720 template <
typename Dest>
2721 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void evalTo(Dest& dst,
const Lhs& lhs,
const Rhs& rhs) {
2722 generic_product_impl<
typename Lhs::DenseMatrixType,
typename Rhs::DenseMatrixType, DenseShape, DenseShape,
2723 ProductTag>::evalTo(dst, lhs, rhs);
2727template <
typename Lhs,
typename Rhs,
int ProductTag,
typename MatrixShape>
2728struct generic_product_impl<Lhs, Rhs, MatrixShape, HomogeneousShape, ProductTag>
2729 : generic_product_impl<Lhs, typename Rhs::PlainObject, MatrixShape, DenseShape, ProductTag> {};
2731template <
typename Lhs,
typename Rhs,
int ProductTag,
typename MatrixShape>
2732struct generic_product_impl<Lhs, Rhs, HomogeneousShape, MatrixShape, ProductTag>
2733 : generic_product_impl<typename Lhs::PlainObject, Rhs, DenseShape, MatrixShape, ProductTag> {};
2735template <
typename Lhs,
typename Rhs,
int ProductTag>
2736struct generic_product_impl<Lhs, Rhs, PermutationShape, HomogeneousShape, ProductTag>
2737 : generic_product_impl<Lhs, Rhs, PermutationShape, DenseShape, ProductTag> {};
2739template <
typename Lhs,
typename Rhs,
int ProductTag>
2740struct generic_product_impl<Lhs, Rhs, HomogeneousShape, PermutationShape, ProductTag>
2741 : generic_product_impl<Lhs, Rhs, DenseShape, PermutationShape, ProductTag> {};
@ UnitDiag
Definition Constants.h:216
@ ZeroDiag
Definition Constants.h:218
@ Lower
Definition Constants.h:212
@ Upper
Definition Constants.h:214
@ ColMajor
Definition Constants.h:319
@ RowMajor
Definition Constants.h:321
@ OnTheLeft
Definition Constants.h:332
@ OnTheRight
Definition Constants.h:334
@ Horizontal
Definition Constants.h:270
@ Vertical
Definition Constants.h:267
constexpr unsigned int ActualPacketAccessBit
Definition Constants.h:109
constexpr unsigned int PacketAccessBit
Definition Constants.h:98
constexpr unsigned int DirectAccessBit
Definition Constants.h:160
constexpr unsigned int EvalBeforeNestingBit
Definition Constants.h:75
constexpr unsigned int LinearAccessBit
Definition Constants.h:134
constexpr unsigned int RowMajorBit
Definition Constants.h:71
adds(const Scalar &s)
Definition ProductEvaluators.h:447
void operator()(const Dst &dst, const Src &src) const
Definition ProductEvaluators.h:450