Eigen  5.0.1-dev+master
 
Loading...
Searching...
No Matches
ScaledPermutationMatrix.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// This Source Code Form is subject to the terms of the Mozilla
5// Public License v. 2.0. If a copy of the MPL was not distributed
6// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
7// SPDX-FileCopyrightText: The Eigen Authors
8// SPDX-License-Identifier: MPL-2.0
9
10#ifndef EIGEN_SCALEDPERMUTATIONMATRIX_H
11#define EIGEN_SCALEDPERMUTATIONMATRIX_H
12
13// IWYU pragma: private
14#include "./InternalHeaderCheck.h"
15
16namespace Eigen {
17
18namespace internal {
19
20template <typename Scalar_, int SizeAtCompileTime, int MaxSizeAtCompileTime, typename StorageIndex_>
21struct traits<ScaledPermutationMatrix<Scalar_, SizeAtCompileTime, MaxSizeAtCompileTime, StorageIndex_>> {
22 using Scalar = Scalar_;
23 using StorageIndex = StorageIndex_;
24 // Dense, so that products and sums with dense operands promote with the existing rules; the O(n) kernels are
25 // selected by the shape (ScaledPermutationShape), as for TriangularView.
26 using StorageKind = Dense;
27 using XprKind = MatrixXpr;
28 using PermutationType = PermutationMatrix<SizeAtCompileTime, MaxSizeAtCompileTime, StorageIndex_>;
29 using ScalesType = Matrix<Scalar_, SizeAtCompileTime, 1, 0, MaxSizeAtCompileTime, 1>;
30 static constexpr int RowsAtCompileTime = SizeAtCompileTime;
31 static constexpr int ColsAtCompileTime = SizeAtCompileTime;
32 static constexpr int MaxRowsAtCompileTime = MaxSizeAtCompileTime;
33 static constexpr int MaxColsAtCompileTime = MaxSizeAtCompileTime;
34 static constexpr unsigned int Flags = NestByRefBit;
35};
36
38template <typename Scalar, typename Lhs, typename Rhs, typename StorageIndex>
39struct scaled_permutation_result {
40 static constexpr int Size = size_prefer_fixed(traits<Lhs>::RowsAtCompileTime, traits<Rhs>::RowsAtCompileTime);
41 static constexpr int MaxSize =
42 min_size_prefer_fixed(traits<Lhs>::MaxRowsAtCompileTime, traits<Rhs>::MaxRowsAtCompileTime);
43 using type = ScaledPermutationMatrix<Scalar, Size, MaxSize, StorageIndex>;
44};
45
46template <typename Lhs, typename Rhs, typename StorageIndex>
47struct scaled_permutation_product_result {
48 using LhsScalar = typename traits<Lhs>::Scalar;
49 using RhsScalar = typename traits<Rhs>::Scalar;
50 using Scalar =
51 typename ScalarBinaryOpTraits<LhsScalar, RhsScalar, scalar_product_op<LhsScalar, RhsScalar>>::ReturnType;
52 using type = typename scaled_permutation_result<Scalar, Lhs, Rhs, StorageIndex>::type;
53};
54
56template <typename IndicesType, typename Result>
57EIGEN_DEVICE_FUNC void invert_permutation_indices(const IndicesType& indices, Result& result) {
58 using StorageIndex = typename Result::Scalar;
59 for (Index k = 0; k < indices.size(); ++k) result.coeffRef(indices.coeff(k)) = StorageIndex(k);
60}
61
62} // namespace internal
63
81template <typename Derived>
82class ScaledPermutationBase : public EigenBase<Derived> {
83 using Traits = internal::traits<Derived>;
84 using Base = EigenBase<Derived>;
85
86 public:
87#ifndef EIGEN_PARSED_BY_DOXYGEN
88 using Scalar = typename Traits::Scalar;
89 using RealScalar = typename NumTraits<Scalar>::Real;
90 using StorageIndex = typename Traits::StorageIndex;
91 using StorageKind = typename Traits::StorageKind;
92 using PermutationType = typename Traits::PermutationType;
93 using ScalesType = typename Traits::ScalesType;
94 using IndicesType = typename PermutationType::IndicesType;
95 static constexpr int RowsAtCompileTime = Traits::RowsAtCompileTime;
96 static constexpr int ColsAtCompileTime = Traits::ColsAtCompileTime;
97 static constexpr int MaxRowsAtCompileTime = Traits::MaxRowsAtCompileTime;
98 static constexpr int MaxColsAtCompileTime = Traits::MaxColsAtCompileTime;
99 static constexpr int SizeAtCompileTime = internal::size_at_compile_time(RowsAtCompileTime, ColsAtCompileTime);
100 static constexpr int MaxSizeAtCompileTime =
101 internal::size_at_compile_time(MaxRowsAtCompileTime, MaxColsAtCompileTime);
102 static constexpr bool IsVectorAtCompileTime = false;
103 static constexpr unsigned int Flags = Traits::Flags;
104 using DenseMatrixType =
107 using Nested = const Derived&;
108 using Base::derived;
109#endif
110
112 EIGEN_DEVICE_FUNC constexpr Index rows() const { return indices().size(); }
114 EIGEN_DEVICE_FUNC constexpr Index cols() const { return indices().size(); }
115
117 EIGEN_DEVICE_FUNC const PermutationType& permutation() const { return derived().permutation(); }
119 EIGEN_DEVICE_FUNC constexpr const IndicesType& indices() const { return derived().indices(); }
121 EIGEN_DEVICE_FUNC const ScalesType& scales() const { return derived().scales(); }
122
124 EIGEN_DEVICE_FUNC Scalar coeff(Index row, Index col) const {
125 eigen_assert(row >= 0 && col >= 0 && row < rows() && col < cols());
126 return Index(indices().coeff(col)) == row ? scales().coeff(col) : Scalar(0);
127 }
128
130 EIGEN_DEVICE_FUNC DenseMatrixType toDenseMatrix() const { return derived(); }
131
132#ifndef EIGEN_PARSED_BY_DOXYGEN
133 template <typename DenseDerived>
134 EIGEN_DEVICE_FUNC void evalTo(MatrixBase<DenseDerived>& other) const {
135 other.setZero();
136 for (Index k = 0; k < rows(); ++k) other.coeffRef(indices().coeff(k), k) = scales().coeff(k);
137 }
138#endif
139
142 PlainObject inverse() const {
143 PlainObject result(rows());
144 for (Index k = 0; k < rows(); ++k) {
145 const Index image = indices().coeff(k);
146 result.indices().coeffRef(image) = StorageIndex(k);
147 result.scales().coeffRef(image) = Scalar(1) / scales().coeff(k);
148 }
149 return result;
150 }
151
153 PlainObject transpose() const {
154 PlainObject result(rows());
155 for (Index k = 0; k < rows(); ++k) {
156 const Index image = indices().coeff(k);
157 result.indices().coeffRef(image) = StorageIndex(k);
158 result.scales().coeffRef(image) = scales().coeff(k);
159 }
160 return result;
161 }
162
164 PlainObject adjoint() const {
165 PlainObject result = transpose();
166 result.scales() = result.scales().conjugate();
167 return result;
168 }
169
171 Scalar determinant() const {
172 return Scalar(RealScalar(permutation().determinant())) * internal::scaled_prod(scales()).value();
173 }
174
177 template <typename OtherDerived>
179 const MatrixBase<OtherDerived>& other) const {
181 }
182
185 template <typename OtherDerived>
187 const MatrixBase<OtherDerived>& other, const ScaledPermutationBase& scaled) {
188 return Product<OtherDerived, Derived, DefaultProduct>(other.derived(), scaled.derived());
189 }
190
192 template <typename OtherDerived>
197
199 template <typename OtherDerived>
201 const TriangularBase<OtherDerived>& other, const ScaledPermutationBase& scaled) {
203 }
204
205 // Closed-form products, all O(n). With S e_c = v_c e_{tau(c)} the rules are:
206 // S D: scale v_c d_c D S: scale d_{tau(c)} v_c
207 // S P: (tau(sigma(c)), v_{sigma(c)}) P S: (sigma(tau(c)), v_c)
208 // S T: (tau(tau2(c)), v_{tau2(c)} v2_c)
209
211 template <typename OtherDerived>
212 typename internal::scaled_permutation_product_result<Derived, OtherDerived, StorageIndex>::type operator*(
213 const DiagonalBase<OtherDerived>& other) const {
214 using Result = typename internal::scaled_permutation_product_result<Derived, OtherDerived, StorageIndex>::type;
215 EIGEN_INTERNAL_ASSERT_SAME_ROWS(*this, other);
216 return Result(indices(), scales().cwiseProduct(other.diagonal()));
217 }
218
220 template <typename OtherDerived>
221 friend typename internal::scaled_permutation_product_result<OtherDerived, Derived, StorageIndex>::type operator*(
222 const DiagonalBase<OtherDerived>& other, const ScaledPermutationBase& scaled) {
223 using Result = typename internal::scaled_permutation_product_result<OtherDerived, Derived, StorageIndex>::type;
224 EIGEN_INTERNAL_ASSERT_SAME_ROWS(other, scaled);
225 Result result(scaled.rows());
226 result.indices() = scaled.indices();
227 for (Index k = 0; k < scaled.rows(); ++k)
228 result.scales().coeffRef(k) = other.diagonal().coeff(scaled.indices().coeff(k)) * scaled.scales().coeff(k);
229 return result;
230 }
231
233 template <typename OtherDerived>
234 PlainObject operator*(const PermutationBase<OtherDerived>& other) const {
235 EIGEN_INTERNAL_ASSERT_SAME_ROWS(*this, other);
236 PlainObject result(rows());
237 for (Index k = 0; k < rows(); ++k) {
238 const Index source = other.indices().coeff(k);
239 result.indices().coeffRef(k) = indices().coeff(source);
240 result.scales().coeffRef(k) = scales().coeff(source);
241 }
242 return result;
243 }
244
246 template <typename OtherDerived>
247 friend PlainObject operator*(const PermutationBase<OtherDerived>& other, const ScaledPermutationBase& scaled) {
248 EIGEN_INTERNAL_ASSERT_SAME_ROWS(other, scaled);
249 PlainObject result(scaled.rows());
250 for (Index k = 0; k < scaled.rows(); ++k)
251 result.indices().coeffRef(k) = StorageIndex(other.indices().coeff(scaled.indices().coeff(k)));
252 result.scales() = scaled.scales();
253 return result;
254 }
255
257 template <typename OtherDerived>
258 PlainObject operator*(const InverseImpl<OtherDerived, PermutationStorage>& other) const {
259 EIGEN_INTERNAL_ASSERT_SAME_ROWS(*this, other);
260 IndicesType inverse(rows());
261 internal::invert_permutation_indices(other.derived().nestedExpression().indices(), inverse);
262 PlainObject result(rows());
263 for (Index k = 0; k < rows(); ++k) {
264 const Index source = inverse.coeff(k);
265 result.indices().coeffRef(k) = indices().coeff(source);
266 result.scales().coeffRef(k) = scales().coeff(source);
267 }
268 return result;
269 }
270
272 template <typename OtherDerived>
273 friend PlainObject operator*(const InverseImpl<OtherDerived, PermutationStorage>& other,
274 const ScaledPermutationBase& scaled) {
275 EIGEN_INTERNAL_ASSERT_SAME_ROWS(other, scaled);
276 IndicesType inverse(scaled.rows());
277 internal::invert_permutation_indices(other.derived().nestedExpression().indices(), inverse);
278 PlainObject result(scaled.rows());
279 for (Index k = 0; k < scaled.rows(); ++k) result.indices().coeffRef(k) = inverse.coeff(scaled.indices().coeff(k));
280 result.scales() = scaled.scales();
281 return result;
282 }
283
285 template <typename OtherDerived>
286 typename internal::scaled_permutation_product_result<Derived, OtherDerived, StorageIndex>::type operator*(
287 const ScaledPermutationBase<OtherDerived>& other) const {
288 using Result = typename internal::scaled_permutation_product_result<Derived, OtherDerived, StorageIndex>::type;
289 EIGEN_INTERNAL_ASSERT_SAME_ROWS(*this, other);
290 Result result(rows());
291 for (Index k = 0; k < rows(); ++k) {
292 const Index source = other.indices().coeff(k);
293 result.indices().coeffRef(k) = indices().coeff(source);
294 result.scales().coeffRef(k) = scales().coeff(source) * other.scales().coeff(k);
295 }
296 return result;
297 }
298
300 PlainObject operator*(const Scalar& alpha) const { return PlainObject(indices(), scales() * alpha); }
301
303 friend PlainObject operator*(const Scalar& alpha, const ScaledPermutationBase& scaled) {
304 return PlainObject(scaled.indices(), alpha * scaled.scales());
305 }
306
308 PlainObject operator-() const { return PlainObject(indices(), -scales()); }
309
310 // Sums with a dense matrix are lazy: the scaled permutation is read through its index-based evaluator.
311
313 template <typename OtherDerived>
314 EIGEN_DEVICE_FUNC friend const EIGEN_CWISE_BINARY_RETURN_TYPE(OtherDerived, Derived, internal::scalar_sum_op)
315 operator+(const MatrixBase<OtherDerived>& lhs, const ScaledPermutationBase & rhs) {
316 return EIGEN_CWISE_BINARY_RETURN_TYPE(OtherDerived, Derived, internal::scalar_sum_op)(lhs.derived(), rhs.derived());
317 }
318
320 template <typename OtherDerived>
321 EIGEN_DEVICE_FUNC friend const EIGEN_CWISE_BINARY_RETURN_TYPE(Derived, OtherDerived, internal::scalar_sum_op)
322 operator+(const ScaledPermutationBase & lhs, const MatrixBase<OtherDerived>& rhs) {
323 return EIGEN_CWISE_BINARY_RETURN_TYPE(Derived, OtherDerived, internal::scalar_sum_op)(lhs.derived(), rhs.derived());
324 }
325
327 template <typename OtherDerived>
328 EIGEN_DEVICE_FUNC friend const EIGEN_CWISE_BINARY_RETURN_TYPE(OtherDerived, Derived, internal::scalar_difference_op)
329 operator-(const MatrixBase<OtherDerived>& lhs, const ScaledPermutationBase & rhs) {
330 return EIGEN_CWISE_BINARY_RETURN_TYPE(OtherDerived, Derived, internal::scalar_difference_op)(lhs.derived(),
331 rhs.derived());
332 }
333
335 template <typename OtherDerived>
336 EIGEN_DEVICE_FUNC friend const EIGEN_CWISE_BINARY_RETURN_TYPE(Derived, OtherDerived, internal::scalar_difference_op)
337 operator-(const ScaledPermutationBase & lhs, const MatrixBase<OtherDerived>& rhs) {
338 return EIGEN_CWISE_BINARY_RETURN_TYPE(Derived, OtherDerived, internal::scalar_difference_op)(lhs.derived(),
339 rhs.derived());
340 }
341};
342
358template <typename Scalar_, int SizeAtCompileTime, int MaxSizeAtCompileTime, typename StorageIndex_>
360 : public ScaledPermutationBase<
361 ScaledPermutationMatrix<Scalar_, SizeAtCompileTime, MaxSizeAtCompileTime, StorageIndex_>> {
363 using Traits = internal::traits<ScaledPermutationMatrix>;
364
365 public:
366#ifndef EIGEN_PARSED_BY_DOXYGEN
367 using Scalar = Scalar_;
368 using StorageIndex = StorageIndex_;
369 using PermutationType = typename Traits::PermutationType;
370 using ScalesType = typename Traits::ScalesType;
371 using IndicesType = typename PermutationType::IndicesType;
372#endif
373
375 EIGEN_DEVICE_FUNC ScaledPermutationMatrix() = default;
376
378 EIGEN_DEVICE_FUNC explicit ScaledPermutationMatrix(Index size) : m_permutation(size), m_scales(size) {}
379
381 template <typename PermutationDerived, typename DiagonalDerived>
383 const DiagonalBase<DiagonalDerived>& diagonal)
384 : m_permutation(permutation), m_scales(diagonal.diagonal()) {
385 EIGEN_INTERNAL_ASSERT_SAME_ROWS(permutation, diagonal);
386 }
387
389 template <typename IndicesDerived, typename ScalesDerived>
392 : m_permutation(indices), m_scales(scales) {
393 EIGEN_INTERNAL_ASSERT_SAME_SIZE(indices, scales);
394 }
395
397 template <typename OtherDerived>
399 : m_permutation(permutation), m_scales(ScalesType::Ones(permutation.rows())) {}
400
402 template <typename OtherDerived>
403 EIGEN_DEVICE_FUNC explicit ScaledPermutationMatrix(const DiagonalBase<OtherDerived>& diagonal)
404 : m_permutation(diagonal.rows()), m_scales(diagonal.diagonal()) {
405 m_permutation.setIdentity();
406 }
407
409 template <typename OtherDerived>
411 : m_permutation(other.permutation()), m_scales(other.scales()) {}
412
414 template <typename OtherDerived>
416 m_permutation = other.permutation();
417 m_scales = other.scales();
418 return *this;
419 }
420
422 EIGEN_DEVICE_FUNC const PermutationType& permutation() const { return m_permutation; }
424 EIGEN_DEVICE_FUNC PermutationType& permutation() { return m_permutation; }
426 EIGEN_DEVICE_FUNC constexpr const IndicesType& indices() const { return m_permutation.indices(); }
428 EIGEN_DEVICE_FUNC constexpr IndicesType& indices() { return m_permutation.indices(); }
430 EIGEN_DEVICE_FUNC const ScalesType& scales() const { return m_scales; }
432 EIGEN_DEVICE_FUNC ScalesType& scales() { return m_scales; }
433
435 EIGEN_DEVICE_FUNC void resize(Index size) {
436 m_permutation.resize(size);
437 m_scales.resize(size);
438 }
439
441 EIGEN_DEVICE_FUNC void setIdentity() {
442 m_permutation.setIdentity();
443 m_scales.setOnes();
444 }
445
447 EIGEN_DEVICE_FUNC void setIdentity(Index size) {
448 resize(size);
449 setIdentity();
450 }
451
452 protected:
453 PermutationType m_permutation;
454 ScalesType m_scales;
455};
456
457// The products of a permutation (or its inverse) and a diagonal matrix. With P e_c = e_{sigma(c)}:
458// P D: (sigma(c), d_c) D P: (sigma(c), d_{sigma(c)})
459// P^-1 D: (sigma^-1(c), d_c) D P^-1: (sigma^-1(c), d_{sigma^-1(c)})
460
463template <typename PermutationDerived, typename DiagonalDerived>
464typename internal::scaled_permutation_result<typename DiagonalDerived::Scalar, PermutationDerived, DiagonalDerived,
465 typename PermutationDerived::StorageIndex>::type
467 using Result =
468 typename internal::scaled_permutation_result<typename DiagonalDerived::Scalar, PermutationDerived,
469 DiagonalDerived, typename PermutationDerived::StorageIndex>::type;
470 EIGEN_INTERNAL_ASSERT_SAME_ROWS(permutation, diagonal);
471 return Result(permutation.indices(), diagonal.diagonal());
472}
473
476template <typename DiagonalDerived, typename PermutationDerived>
477typename internal::scaled_permutation_result<typename DiagonalDerived::Scalar, PermutationDerived, DiagonalDerived,
478 typename PermutationDerived::StorageIndex>::type
480 using Result =
481 typename internal::scaled_permutation_result<typename DiagonalDerived::Scalar, PermutationDerived,
482 DiagonalDerived, typename PermutationDerived::StorageIndex>::type;
483 EIGEN_INTERNAL_ASSERT_SAME_ROWS(permutation, diagonal);
484 Result result(permutation.rows());
485 result.indices() = permutation.indices();
486 for (Index k = 0; k < permutation.rows(); ++k)
487 result.scales().coeffRef(k) = diagonal.diagonal().coeff(permutation.indices().coeff(k));
488 return result;
489}
490
493template <typename PermutationType, typename DiagonalDerived>
494typename internal::scaled_permutation_result<typename DiagonalDerived::Scalar, PermutationType, DiagonalDerived,
495 typename PermutationType::StorageIndex>::type
496operator*(const InverseImpl<PermutationType, PermutationStorage>& inverse,
497 const DiagonalBase<DiagonalDerived>& diagonal) {
498 using Result =
499 typename internal::scaled_permutation_result<typename DiagonalDerived::Scalar, PermutationType, DiagonalDerived,
500 typename PermutationType::StorageIndex>::type;
501 EIGEN_INTERNAL_ASSERT_SAME_ROWS(inverse, diagonal);
502 Result result(inverse.rows());
503 internal::invert_permutation_indices(inverse.derived().nestedExpression().indices(), result.indices());
504 result.scales() = diagonal.diagonal();
505 return result;
506}
507
510template <typename DiagonalDerived, typename PermutationType>
511typename internal::scaled_permutation_result<typename DiagonalDerived::Scalar, PermutationType, DiagonalDerived,
512 typename PermutationType::StorageIndex>::type
514 const InverseImpl<PermutationType, PermutationStorage>& inverse) {
515 using Result =
516 typename internal::scaled_permutation_result<typename DiagonalDerived::Scalar, PermutationType, DiagonalDerived,
517 typename PermutationType::StorageIndex>::type;
518 EIGEN_INTERNAL_ASSERT_SAME_ROWS(inverse, diagonal);
519 Result result(inverse.rows());
520 internal::invert_permutation_indices(inverse.derived().nestedExpression().indices(), result.indices());
521 for (Index k = 0; k < inverse.rows(); ++k)
522 result.scales().coeffRef(k) = diagonal.diagonal().coeff(result.indices().coeff(k));
523 return result;
524}
525
526namespace internal {
527
528template <typename Scalar_, int SizeAtCompileTime, int MaxSizeAtCompileTime, typename StorageIndex_>
529struct evaluator_traits<ScaledPermutationMatrix<Scalar_, SizeAtCompileTime, MaxSizeAtCompileTime, StorageIndex_>> {
530 using Kind = IndexBased;
531 using Shape = ScaledPermutationShape;
532};
533
536template <typename Scalar_, int SizeAtCompileTime, int MaxSizeAtCompileTime, typename StorageIndex_>
537struct evaluator<ScaledPermutationMatrix<Scalar_, SizeAtCompileTime, MaxSizeAtCompileTime, StorageIndex_>>
538 : evaluator_base<ScaledPermutationMatrix<Scalar_, SizeAtCompileTime, MaxSizeAtCompileTime, StorageIndex_>> {
539 using XprType = ScaledPermutationMatrix<Scalar_, SizeAtCompileTime, MaxSizeAtCompileTime, StorageIndex_>;
540 using Scalar = Scalar_;
541 using CoeffReturnType = Scalar;
542
543 static constexpr int CoeffReadCost =
544 int(NumTraits<StorageIndex_>::ReadCost) + int(NumTraits<Scalar>::ReadCost) + int(NumTraits<Scalar>::AddCost);
545 static constexpr unsigned int Flags = 0;
546 static constexpr int Alignment = 0;
547
548 EIGEN_DEVICE_FUNC explicit evaluator(const XprType& xpr) : m_indices(xpr.indices()), m_scales(xpr.scales()) {}
549
550 EIGEN_DEVICE_FUNC Scalar coeff(Index row, Index col) const {
551 return Index(m_indices.coeff(col)) == row ? m_scales.coeff(col) : Scalar(0);
552 }
553
554 // Linear access is requested only for vector-shaped operands (inner products), i.e. a 1x1 matrix.
555 EIGEN_DEVICE_FUNC Scalar coeff(Index index) const {
556 eigen_assert(index == 0);
557 return m_scales.coeff(index);
558 }
559
560 protected:
561 evaluator<typename XprType::IndicesType> m_indices;
562 evaluator<typename XprType::ScalesType> m_scales;
563};
564
565struct ScaledPermutation2Dense {};
566
567template <>
568struct AssignmentKind<DenseShape, ScaledPermutationShape> {
569 using Kind = ScaledPermutation2Dense;
570};
571
572// Scaled permutation to dense assignment: zero fill plus one scatter of n coefficients.
573template <typename DstXprType, typename SrcXprType, typename Functor>
574struct Assignment<DstXprType, SrcXprType, Functor, ScaledPermutation2Dense> {
575 static EIGEN_DEVICE_FUNC void run(
576 DstXprType& dst, const SrcXprType& src,
577 const internal::assign_op<typename DstXprType::Scalar, typename SrcXprType::Scalar>&) {
578 if (dst.rows() != src.rows() || dst.cols() != src.cols()) dst.resize(src.rows(), src.cols());
579 dst.setZero();
580 for (Index k = 0; k < src.rows(); ++k) dst.coeffRef(src.indices().coeff(k), k) = src.scales().coeff(k);
581 }
582
583 static EIGEN_DEVICE_FUNC void run(
584 DstXprType& dst, const SrcXprType& src,
585 const internal::add_assign_op<typename DstXprType::Scalar, typename SrcXprType::Scalar>&) {
586 EIGEN_INTERNAL_ASSERT_SAME_SHAPE(dst, src);
587 for (Index k = 0; k < src.rows(); ++k) dst.coeffRef(src.indices().coeff(k), k) += src.scales().coeff(k);
588 }
589
590 static EIGEN_DEVICE_FUNC void run(
591 DstXprType& dst, const SrcXprType& src,
592 const internal::sub_assign_op<typename DstXprType::Scalar, typename SrcXprType::Scalar>&) {
593 EIGEN_INTERNAL_ASSERT_SAME_SHAPE(dst, src);
594 for (Index k = 0; k < src.rows(); ++k) dst.coeffRef(src.indices().coeff(k), k) -= src.scales().coeff(k);
595 }
596};
597
598/***************************************************************************
599 * Products with a dense matrix: rows (S * X) or columns (X * S) are scaled and permuted, O(n m).
600 * evalTo reuses the dense permutation kernel, which handles X aliasing dst, and scales in place afterwards;
601 * the accumulating form works on the nested (evaluated once) operand.
602 * scaleAndAddTo evaluates dst += alpha * (lhs * rhs) with alpha a left factor of the product. Scalar
603 * multiplication need not commute, so both kernels keep that operand order: S * X regroups it as
604 * (alpha * v_c) * X(c, j) by associativity; X * S keeps alpha * (X(i, tau(c)) * v_c), as folding alpha into
605 * v_c would move it past the dense coefficient.
606 ***************************************************************************/
607
608template <typename Lhs, typename Rhs, int ProductTag>
609struct generic_product_impl<Lhs, Rhs, ScaledPermutationShape, DenseShape, ProductTag>
610 : generic_product_impl_base<Lhs, Rhs,
611 generic_product_impl<Lhs, Rhs, ScaledPermutationShape, DenseShape, ProductTag>> {
612 using Scalar = typename Product<Lhs, Rhs>::Scalar;
613
614 template <typename Dest>
615 static EIGEN_DEVICE_FUNC void evalTo(Dest& dst, const Lhs& lhs, const Rhs& rhs) {
616 permutation_matrix_product<Rhs, OnTheLeft, false, DenseShape>::run(dst, lhs.permutation(), rhs);
617 for (Index k = 0; k < lhs.rows(); ++k) {
618 auto row = dst.row(lhs.indices().coeff(k));
619 row = lhs.scales().coeff(k) * row;
620 }
621 }
622
623 template <typename Dest>
624 static EIGEN_DEVICE_FUNC void scaleAndAddTo(Dest& dst, const Lhs& lhs, const Rhs& rhs, const Scalar& alpha) {
625 typename nested_eval<Rhs, 1>::type rhsNested(rhs);
626 for (Index k = 0; k < lhs.rows(); ++k)
627 dst.row(lhs.indices().coeff(k)) += (alpha * lhs.scales().coeff(k)) * rhsNested.row(k);
628 }
629};
630
631template <typename Lhs, typename Rhs, int ProductTag>
632struct generic_product_impl<Lhs, Rhs, DenseShape, ScaledPermutationShape, ProductTag>
633 : generic_product_impl_base<Lhs, Rhs,
634 generic_product_impl<Lhs, Rhs, DenseShape, ScaledPermutationShape, ProductTag>> {
635 using Scalar = typename Product<Lhs, Rhs>::Scalar;
636
637 template <typename Dest>
638 static EIGEN_DEVICE_FUNC void evalTo(Dest& dst, const Lhs& lhs, const Rhs& rhs) {
639 permutation_matrix_product<Lhs, OnTheRight, false, DenseShape>::run(dst, rhs.permutation(), lhs);
640 for (Index k = 0; k < rhs.rows(); ++k) dst.col(k) *= rhs.scales().coeff(k);
641 }
642
643 template <typename Dest>
644 static EIGEN_DEVICE_FUNC void scaleAndAddTo(Dest& dst, const Lhs& lhs, const Rhs& rhs, const Scalar& alpha) {
645 typename nested_eval<Lhs, 1>::type lhsNested(lhs);
646 for (Index k = 0; k < rhs.rows(); ++k)
647 dst.col(k) += alpha * (lhsNested.col(rhs.indices().coeff(k)) * rhs.scales().coeff(k));
648 }
649};
650
651// A triangular or self-adjoint operand is evaluated into a plain matrix first, as toDenseMatrix() does.
652template <typename Lhs, typename Rhs, int ProductTag, bool ViewOnTheRight>
653struct scaled_permutation_view_product_impl;
654
655template <typename Lhs, typename Rhs, int ProductTag>
656struct scaled_permutation_view_product_impl<Lhs, Rhs, ProductTag, true>
657 : generic_product_impl_base<Lhs, Rhs, scaled_permutation_view_product_impl<Lhs, Rhs, ProductTag, true>> {
658 using Scalar = typename Product<Lhs, Rhs>::Scalar;
659 using DenseType = typename Rhs::DenseMatrixType;
660 using DenseImpl = generic_product_impl<Lhs, DenseType, ScaledPermutationShape, DenseShape, ProductTag>;
661
662 template <typename Dest>
663 static EIGEN_DEVICE_FUNC void evalTo(Dest& dst, const Lhs& lhs, const Rhs& rhs) {
664 const DenseType dense(rhs);
665 DenseImpl::evalTo(dst, lhs, dense);
666 }
667 template <typename Dest>
668 static EIGEN_DEVICE_FUNC void scaleAndAddTo(Dest& dst, const Lhs& lhs, const Rhs& rhs, const Scalar& alpha) {
669 const DenseType dense(rhs);
670 DenseImpl::scaleAndAddTo(dst, lhs, dense, alpha);
671 }
672};
673
674template <typename Lhs, typename Rhs, int ProductTag>
675struct scaled_permutation_view_product_impl<Lhs, Rhs, ProductTag, false>
676 : generic_product_impl_base<Lhs, Rhs, scaled_permutation_view_product_impl<Lhs, Rhs, ProductTag, false>> {
677 using Scalar = typename Product<Lhs, Rhs>::Scalar;
678 using DenseType = typename Lhs::DenseMatrixType;
679 using DenseImpl = generic_product_impl<DenseType, Rhs, DenseShape, ScaledPermutationShape, ProductTag>;
680
681 template <typename Dest>
682 static EIGEN_DEVICE_FUNC void evalTo(Dest& dst, const Lhs& lhs, const Rhs& rhs) {
683 const DenseType dense(lhs);
684 DenseImpl::evalTo(dst, dense, rhs);
685 }
686 template <typename Dest>
687 static EIGEN_DEVICE_FUNC void scaleAndAddTo(Dest& dst, const Lhs& lhs, const Rhs& rhs, const Scalar& alpha) {
688 const DenseType dense(lhs);
689 DenseImpl::scaleAndAddTo(dst, dense, rhs, alpha);
690 }
691};
692
693template <typename Lhs, typename Rhs, int ProductTag>
694struct generic_product_impl<Lhs, Rhs, ScaledPermutationShape, TriangularShape, ProductTag>
695 : scaled_permutation_view_product_impl<Lhs, Rhs, ProductTag, true> {};
696template <typename Lhs, typename Rhs, int ProductTag>
697struct generic_product_impl<Lhs, Rhs, ScaledPermutationShape, SelfAdjointShape, ProductTag>
698 : scaled_permutation_view_product_impl<Lhs, Rhs, ProductTag, true> {};
699template <typename Lhs, typename Rhs, int ProductTag>
700struct generic_product_impl<Lhs, Rhs, TriangularShape, ScaledPermutationShape, ProductTag>
701 : scaled_permutation_view_product_impl<Lhs, Rhs, ProductTag, false> {};
702template <typename Lhs, typename Rhs, int ProductTag>
703struct generic_product_impl<Lhs, Rhs, SelfAdjointShape, ScaledPermutationShape, ProductTag>
704 : scaled_permutation_view_product_impl<Lhs, Rhs, ProductTag, false> {};
705
706} // namespace internal
707
708} // namespace Eigen
709
710#endif // EIGEN_SCALEDPERMUTATIONMATRIX_H
Derived & setZero()
Definition CwiseNullaryOp.h:521
Base class for diagonal matrices and expressions.
Definition DiagonalMatrix.h:34
const DiagonalVectorType & diagonal() const
Definition DiagonalMatrix.h:71
Base class for all dense matrices, vectors, and expressions.
Definition MatrixBase.h:53
The matrix class, also used for vectors and row-vectors.
Definition Matrix.h:188
Base class for permutations.
Definition PermutationMatrix.h:92
constexpr const IndicesType & indices() const
Definition PermutationMatrix.h:160
Expression of the product of two arbitrary matrices or vectors.
Definition Product.h:211
Base class for scaled permutation matrices.
Definition ScaledPermutationMatrix.h:82
const PermutationType & permutation() const
Definition ScaledPermutationMatrix.h:117
PlainObject transpose() const
Definition ScaledPermutationMatrix.h:153
internal::scaled_permutation_product_result< Derived, OtherDerived, StorageIndex >::type operator*(const DiagonalBase< OtherDerived > &other) const
Definition ScaledPermutationMatrix.h:212
PlainObject adjoint() const
Definition ScaledPermutationMatrix.h:164
PlainObject operator*(const Scalar &alpha) const
Definition ScaledPermutationMatrix.h:300
friend PlainObject operator*(const InverseImpl< OtherDerived, PermutationStorage > &other, const ScaledPermutationBase &scaled)
Definition ScaledPermutationMatrix.h:273
const Product< Derived, OtherDerived, DefaultProduct > operator*(const TriangularBase< OtherDerived > &other) const
Definition ScaledPermutationMatrix.h:193
Scalar coeff(Index row, Index col) const
Definition ScaledPermutationMatrix.h:124
friend PlainObject operator*(const Scalar &alpha, const ScaledPermutationBase &scaled)
Definition ScaledPermutationMatrix.h:303
const ScalesType & scales() const
Definition ScaledPermutationMatrix.h:121
friend PlainObject operator*(const PermutationBase< OtherDerived > &other, const ScaledPermutationBase &scaled)
Definition ScaledPermutationMatrix.h:247
PlainObject inverse() const
Definition ScaledPermutationMatrix.h:142
constexpr Index rows() const
Definition ScaledPermutationMatrix.h:112
PlainObject operator*(const PermutationBase< OtherDerived > &other) const
Definition ScaledPermutationMatrix.h:234
Scalar determinant() const
Definition ScaledPermutationMatrix.h:171
constexpr const IndicesType & indices() const
Definition ScaledPermutationMatrix.h:119
friend internal::scaled_permutation_product_result< OtherDerived, Derived, StorageIndex >::type operator*(const DiagonalBase< OtherDerived > &other, const ScaledPermutationBase &scaled)
Definition ScaledPermutationMatrix.h:221
friend const Product< OtherDerived, Derived, DefaultProduct > operator*(const TriangularBase< OtherDerived > &other, const ScaledPermutationBase &scaled)
Definition ScaledPermutationMatrix.h:200
const Product< Derived, OtherDerived, DefaultProduct > operator*(const MatrixBase< OtherDerived > &other) const
Definition ScaledPermutationMatrix.h:178
friend const Product< OtherDerived, Derived, DefaultProduct > operator*(const MatrixBase< OtherDerived > &other, const ScaledPermutationBase &scaled)
Definition ScaledPermutationMatrix.h:186
internal::scaled_permutation_product_result< Derived, OtherDerived, StorageIndex >::type operator*(const ScaledPermutationBase< OtherDerived > &other) const
Definition ScaledPermutationMatrix.h:286
constexpr Index cols() const
Definition ScaledPermutationMatrix.h:114
PlainObject operator*(const InverseImpl< OtherDerived, PermutationStorage > &other) const
Definition ScaledPermutationMatrix.h:258
PlainObject operator-() const
Definition ScaledPermutationMatrix.h:308
DenseMatrixType toDenseMatrix() const
Definition ScaledPermutationMatrix.h:130
Scaled permutation matrix with its storage.
Definition ScaledPermutationMatrix.h:361
void resize(Index size)
Definition ScaledPermutationMatrix.h:435
ScaledPermutationMatrix(const DiagonalBase< OtherDerived > &diagonal)
Definition ScaledPermutationMatrix.h:403
internal::scaled_permutation_result< typenameDiagonalDerived::Scalar, PermutationDerived, DiagonalDerived, typenamePermutationDerived::StorageIndex >::type operator*(const PermutationBase< PermutationDerived > &permutation, const DiagonalBase< DiagonalDerived > &diagonal)
Definition ScaledPermutationMatrix.h:466
const PermutationType & permutation() const
Definition ScaledPermutationMatrix.h:422
ScaledPermutationMatrix(const ScaledPermutationBase< OtherDerived > &other)
Definition ScaledPermutationMatrix.h:410
const ScalesType & scales() const
Definition ScaledPermutationMatrix.h:430
constexpr const IndicesType & indices() const
Definition ScaledPermutationMatrix.h:426
ScaledPermutationMatrix(const PermutationBase< OtherDerived > &permutation)
Definition ScaledPermutationMatrix.h:398
ScaledPermutationMatrix(Index size)
Definition ScaledPermutationMatrix.h:378
constexpr IndicesType & indices()
Definition ScaledPermutationMatrix.h:428
internal::scaled_permutation_result< typenameDiagonalDerived::Scalar, PermutationDerived, DiagonalDerived, typenamePermutationDerived::StorageIndex >::type operator*(const DiagonalBase< DiagonalDerived > &diagonal, const PermutationBase< PermutationDerived > &permutation)
Definition ScaledPermutationMatrix.h:479
ScaledPermutationMatrix & operator=(const ScaledPermutationBase< OtherDerived > &other)
Definition ScaledPermutationMatrix.h:415
ScaledPermutationMatrix(const PermutationBase< PermutationDerived > &permutation, const DiagonalBase< DiagonalDerived > &diagonal)
Definition ScaledPermutationMatrix.h:382
void setIdentity()
Definition ScaledPermutationMatrix.h:441
ScalesType & scales()
Definition ScaledPermutationMatrix.h:432
internal::scaled_permutation_result< typenameDiagonalDerived::Scalar, PermutationType, DiagonalDerived, typenamePermutationType::StorageIndex >::type operator*(const InverseImpl< PermutationType, PermutationStorage > &inverse, const DiagonalBase< DiagonalDerived > &diagonal)
Definition ScaledPermutationMatrix.h:496
ScaledPermutationMatrix(const MatrixBase< IndicesDerived > &indices, const MatrixBase< ScalesDerived > &scales)
Definition ScaledPermutationMatrix.h:390
PermutationType & permutation()
Definition ScaledPermutationMatrix.h:424
internal::scaled_permutation_result< typenameDiagonalDerived::Scalar, PermutationType, DiagonalDerived, typenamePermutationType::StorageIndex >::type operator*(const DiagonalBase< DiagonalDerived > &diagonal, const InverseImpl< PermutationType, PermutationStorage > &inverse)
Definition ScaledPermutationMatrix.h:513
void setIdentity(Index size)
Definition ScaledPermutationMatrix.h:447
Base class for triangular part in a matrix.
Definition TriangularMatrix.h:68
Definition EigenBase.h:34
constexpr Derived & derived()
Definition EigenBase.h:50
constexpr Index size() const noexcept
Definition EigenBase.h:65
Eigen::Index Index
The interface type of indices.
Definition EigenBase.h:44