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 internal::scaled_product<Scalar> det = internal::scaled_prod(scales());
173 det.multiply(Scalar(RealScalar(permutation().determinant())));
174 return det.value();
175 }
176
179 template <typename OtherDerived>
181 const MatrixBase<OtherDerived>& other) const {
183 }
184
187 template <typename OtherDerived>
189 const MatrixBase<OtherDerived>& other, const ScaledPermutationBase& scaled) {
190 return Product<OtherDerived, Derived, DefaultProduct>(other.derived(), scaled.derived());
191 }
192
194 template <typename OtherDerived>
199
201 template <typename OtherDerived>
203 const TriangularBase<OtherDerived>& other, const ScaledPermutationBase& scaled) {
205 }
206
207 // Closed-form products, all O(n). With S e_c = v_c e_{tau(c)} the rules are:
208 // S D: scale v_c d_c D S: scale d_{tau(c)} v_c
209 // S P: (tau(sigma(c)), v_{sigma(c)}) P S: (sigma(tau(c)), v_c)
210 // S T: (tau(tau2(c)), v_{tau2(c)} v2_c)
211
213 template <typename OtherDerived>
214 typename internal::scaled_permutation_product_result<Derived, OtherDerived, StorageIndex>::type operator*(
215 const DiagonalBase<OtherDerived>& other) const {
216 using Result = typename internal::scaled_permutation_product_result<Derived, OtherDerived, StorageIndex>::type;
217 EIGEN_INTERNAL_ASSERT_SAME_ROWS(*this, other);
218 return Result(indices(), scales().cwiseProduct(other.diagonal()));
219 }
220
222 template <typename OtherDerived>
223 friend typename internal::scaled_permutation_product_result<OtherDerived, Derived, StorageIndex>::type operator*(
224 const DiagonalBase<OtherDerived>& other, const ScaledPermutationBase& scaled) {
225 using Result = typename internal::scaled_permutation_product_result<OtherDerived, Derived, StorageIndex>::type;
226 EIGEN_INTERNAL_ASSERT_SAME_ROWS(other, scaled);
227 Result result(scaled.rows());
228 result.indices() = scaled.indices();
229 for (Index k = 0; k < scaled.rows(); ++k)
230 result.scales().coeffRef(k) = other.diagonal().coeff(scaled.indices().coeff(k)) * scaled.scales().coeff(k);
231 return result;
232 }
233
235 template <typename OtherDerived>
236 PlainObject operator*(const PermutationBase<OtherDerived>& other) const {
237 EIGEN_INTERNAL_ASSERT_SAME_ROWS(*this, other);
238 PlainObject result(rows());
239 for (Index k = 0; k < rows(); ++k) {
240 const Index source = other.indices().coeff(k);
241 result.indices().coeffRef(k) = indices().coeff(source);
242 result.scales().coeffRef(k) = scales().coeff(source);
243 }
244 return result;
245 }
246
248 template <typename OtherDerived>
249 friend PlainObject operator*(const PermutationBase<OtherDerived>& other, const ScaledPermutationBase& scaled) {
250 EIGEN_INTERNAL_ASSERT_SAME_ROWS(other, scaled);
251 PlainObject result(scaled.rows());
252 for (Index k = 0; k < scaled.rows(); ++k)
253 result.indices().coeffRef(k) = StorageIndex(other.indices().coeff(scaled.indices().coeff(k)));
254 result.scales() = scaled.scales();
255 return result;
256 }
257
259 template <typename OtherDerived>
260 PlainObject operator*(const InverseImpl<OtherDerived, PermutationStorage>& other) const {
261 EIGEN_INTERNAL_ASSERT_SAME_ROWS(*this, other);
262 IndicesType inverse(rows());
263 internal::invert_permutation_indices(other.derived().nestedExpression().indices(), inverse);
264 PlainObject result(rows());
265 for (Index k = 0; k < rows(); ++k) {
266 const Index source = inverse.coeff(k);
267 result.indices().coeffRef(k) = indices().coeff(source);
268 result.scales().coeffRef(k) = scales().coeff(source);
269 }
270 return result;
271 }
272
274 template <typename OtherDerived>
275 friend PlainObject operator*(const InverseImpl<OtherDerived, PermutationStorage>& other,
276 const ScaledPermutationBase& scaled) {
277 EIGEN_INTERNAL_ASSERT_SAME_ROWS(other, scaled);
278 IndicesType inverse(scaled.rows());
279 internal::invert_permutation_indices(other.derived().nestedExpression().indices(), inverse);
280 PlainObject result(scaled.rows());
281 for (Index k = 0; k < scaled.rows(); ++k) result.indices().coeffRef(k) = inverse.coeff(scaled.indices().coeff(k));
282 result.scales() = scaled.scales();
283 return result;
284 }
285
287 template <typename OtherDerived>
288 typename internal::scaled_permutation_product_result<Derived, OtherDerived, StorageIndex>::type operator*(
289 const ScaledPermutationBase<OtherDerived>& other) const {
290 using Result = typename internal::scaled_permutation_product_result<Derived, OtherDerived, StorageIndex>::type;
291 EIGEN_INTERNAL_ASSERT_SAME_ROWS(*this, other);
292 Result result(rows());
293 for (Index k = 0; k < rows(); ++k) {
294 const Index source = other.indices().coeff(k);
295 result.indices().coeffRef(k) = indices().coeff(source);
296 result.scales().coeffRef(k) = scales().coeff(source) * other.scales().coeff(k);
297 }
298 return result;
299 }
300
302 PlainObject operator*(const Scalar& alpha) const { return PlainObject(indices(), scales() * alpha); }
303
305 friend PlainObject operator*(const Scalar& alpha, const ScaledPermutationBase& scaled) {
306 return PlainObject(scaled.indices(), alpha * scaled.scales());
307 }
308
310 PlainObject operator-() const { return PlainObject(indices(), -scales()); }
311
312 // Sums with a dense matrix are lazy: the scaled permutation is read through its index-based evaluator.
313
315 template <typename OtherDerived>
316 EIGEN_DEVICE_FUNC friend const EIGEN_CWISE_BINARY_RETURN_TYPE(OtherDerived, Derived, internal::scalar_sum_op)
317 operator+(const MatrixBase<OtherDerived>& lhs, const ScaledPermutationBase & rhs) {
318 return EIGEN_CWISE_BINARY_RETURN_TYPE(OtherDerived, Derived, internal::scalar_sum_op)(lhs.derived(), rhs.derived());
319 }
320
322 template <typename OtherDerived>
323 EIGEN_DEVICE_FUNC friend const EIGEN_CWISE_BINARY_RETURN_TYPE(Derived, OtherDerived, internal::scalar_sum_op)
324 operator+(const ScaledPermutationBase & lhs, const MatrixBase<OtherDerived>& rhs) {
325 return EIGEN_CWISE_BINARY_RETURN_TYPE(Derived, OtherDerived, internal::scalar_sum_op)(lhs.derived(), rhs.derived());
326 }
327
329 template <typename OtherDerived>
330 EIGEN_DEVICE_FUNC friend const EIGEN_CWISE_BINARY_RETURN_TYPE(OtherDerived, Derived, internal::scalar_difference_op)
331 operator-(const MatrixBase<OtherDerived>& lhs, const ScaledPermutationBase & rhs) {
332 return EIGEN_CWISE_BINARY_RETURN_TYPE(OtherDerived, Derived, internal::scalar_difference_op)(lhs.derived(),
333 rhs.derived());
334 }
335
337 template <typename OtherDerived>
338 EIGEN_DEVICE_FUNC friend const EIGEN_CWISE_BINARY_RETURN_TYPE(Derived, OtherDerived, internal::scalar_difference_op)
339 operator-(const ScaledPermutationBase & lhs, const MatrixBase<OtherDerived>& rhs) {
340 return EIGEN_CWISE_BINARY_RETURN_TYPE(Derived, OtherDerived, internal::scalar_difference_op)(lhs.derived(),
341 rhs.derived());
342 }
343};
344
360template <typename Scalar_, int SizeAtCompileTime, int MaxSizeAtCompileTime, typename StorageIndex_>
362 : public ScaledPermutationBase<
363 ScaledPermutationMatrix<Scalar_, SizeAtCompileTime, MaxSizeAtCompileTime, StorageIndex_>> {
365 using Traits = internal::traits<ScaledPermutationMatrix>;
366
367 public:
368#ifndef EIGEN_PARSED_BY_DOXYGEN
369 using Scalar = Scalar_;
370 using StorageIndex = StorageIndex_;
371 using PermutationType = typename Traits::PermutationType;
372 using ScalesType = typename Traits::ScalesType;
373 using IndicesType = typename PermutationType::IndicesType;
374#endif
375
377 EIGEN_DEVICE_FUNC ScaledPermutationMatrix() = default;
378
380 EIGEN_DEVICE_FUNC explicit ScaledPermutationMatrix(Index size) : m_permutation(size), m_scales(size) {}
381
383 template <typename PermutationDerived, typename DiagonalDerived>
385 const DiagonalBase<DiagonalDerived>& diagonal)
386 : m_permutation(permutation), m_scales(diagonal.diagonal()) {
387 EIGEN_INTERNAL_ASSERT_SAME_ROWS(permutation, diagonal);
388 }
389
391 template <typename IndicesDerived, typename ScalesDerived>
394 : m_permutation(indices), m_scales(scales) {
395 EIGEN_INTERNAL_ASSERT_SAME_SIZE(indices, scales);
396 }
397
399 template <typename OtherDerived>
401 : m_permutation(permutation), m_scales(ScalesType::Ones(permutation.rows())) {}
402
404 template <typename OtherDerived>
405 EIGEN_DEVICE_FUNC explicit ScaledPermutationMatrix(const DiagonalBase<OtherDerived>& diagonal)
406 : m_permutation(diagonal.rows()), m_scales(diagonal.diagonal()) {
407 m_permutation.setIdentity();
408 }
409
411 template <typename OtherDerived>
413 : m_permutation(other.permutation()), m_scales(other.scales()) {}
414
416 template <typename OtherDerived>
418 m_permutation = other.permutation();
419 m_scales = other.scales();
420 return *this;
421 }
422
424 EIGEN_DEVICE_FUNC const PermutationType& permutation() const { return m_permutation; }
426 EIGEN_DEVICE_FUNC PermutationType& permutation() { return m_permutation; }
428 EIGEN_DEVICE_FUNC constexpr const IndicesType& indices() const { return m_permutation.indices(); }
430 EIGEN_DEVICE_FUNC constexpr IndicesType& indices() { return m_permutation.indices(); }
432 EIGEN_DEVICE_FUNC const ScalesType& scales() const { return m_scales; }
434 EIGEN_DEVICE_FUNC ScalesType& scales() { return m_scales; }
435
437 EIGEN_DEVICE_FUNC void resize(Index size) {
438 m_permutation.resize(size);
439 m_scales.resize(size);
440 }
441
443 EIGEN_DEVICE_FUNC void setIdentity() {
444 m_permutation.setIdentity();
445 m_scales.setOnes();
446 }
447
449 EIGEN_DEVICE_FUNC void setIdentity(Index size) {
450 resize(size);
451 setIdentity();
452 }
453
454 protected:
455 PermutationType m_permutation;
456 ScalesType m_scales;
457};
458
459// The products of a permutation (or its inverse) and a diagonal matrix. With P e_c = e_{sigma(c)}:
460// P D: (sigma(c), d_c) D P: (sigma(c), d_{sigma(c)})
461// P^-1 D: (sigma^-1(c), d_c) D P^-1: (sigma^-1(c), d_{sigma^-1(c)})
462
465template <typename PermutationDerived, typename DiagonalDerived>
466typename internal::scaled_permutation_result<typename DiagonalDerived::Scalar, PermutationDerived, DiagonalDerived,
467 typename PermutationDerived::StorageIndex>::type
469 using Result =
470 typename internal::scaled_permutation_result<typename DiagonalDerived::Scalar, PermutationDerived,
471 DiagonalDerived, typename PermutationDerived::StorageIndex>::type;
472 EIGEN_INTERNAL_ASSERT_SAME_ROWS(permutation, diagonal);
473 return Result(permutation.indices(), diagonal.diagonal());
474}
475
478template <typename DiagonalDerived, typename PermutationDerived>
479typename internal::scaled_permutation_result<typename DiagonalDerived::Scalar, PermutationDerived, DiagonalDerived,
480 typename PermutationDerived::StorageIndex>::type
482 using Result =
483 typename internal::scaled_permutation_result<typename DiagonalDerived::Scalar, PermutationDerived,
484 DiagonalDerived, typename PermutationDerived::StorageIndex>::type;
485 EIGEN_INTERNAL_ASSERT_SAME_ROWS(permutation, diagonal);
486 Result result(permutation.rows());
487 result.indices() = permutation.indices();
488 for (Index k = 0; k < permutation.rows(); ++k)
489 result.scales().coeffRef(k) = diagonal.diagonal().coeff(permutation.indices().coeff(k));
490 return result;
491}
492
495template <typename PermutationType, typename DiagonalDerived>
496typename internal::scaled_permutation_result<typename DiagonalDerived::Scalar, PermutationType, DiagonalDerived,
497 typename PermutationType::StorageIndex>::type
498operator*(const InverseImpl<PermutationType, PermutationStorage>& inverse,
499 const DiagonalBase<DiagonalDerived>& diagonal) {
500 using Result =
501 typename internal::scaled_permutation_result<typename DiagonalDerived::Scalar, PermutationType, DiagonalDerived,
502 typename PermutationType::StorageIndex>::type;
503 EIGEN_INTERNAL_ASSERT_SAME_ROWS(inverse, diagonal);
504 Result result(inverse.rows());
505 internal::invert_permutation_indices(inverse.derived().nestedExpression().indices(), result.indices());
506 result.scales() = diagonal.diagonal();
507 return result;
508}
509
512template <typename DiagonalDerived, typename PermutationType>
513typename internal::scaled_permutation_result<typename DiagonalDerived::Scalar, PermutationType, DiagonalDerived,
514 typename PermutationType::StorageIndex>::type
516 const InverseImpl<PermutationType, PermutationStorage>& inverse) {
517 using Result =
518 typename internal::scaled_permutation_result<typename DiagonalDerived::Scalar, PermutationType, DiagonalDerived,
519 typename PermutationType::StorageIndex>::type;
520 EIGEN_INTERNAL_ASSERT_SAME_ROWS(inverse, diagonal);
521 Result result(inverse.rows());
522 internal::invert_permutation_indices(inverse.derived().nestedExpression().indices(), result.indices());
523 for (Index k = 0; k < inverse.rows(); ++k)
524 result.scales().coeffRef(k) = diagonal.diagonal().coeff(result.indices().coeff(k));
525 return result;
526}
527
528namespace internal {
529
530template <typename Scalar_, int SizeAtCompileTime, int MaxSizeAtCompileTime, typename StorageIndex_>
531struct evaluator_traits<ScaledPermutationMatrix<Scalar_, SizeAtCompileTime, MaxSizeAtCompileTime, StorageIndex_>> {
532 using Kind = IndexBased;
533 using Shape = ScaledPermutationShape;
534};
535
538template <typename Scalar_, int SizeAtCompileTime, int MaxSizeAtCompileTime, typename StorageIndex_>
539struct evaluator<ScaledPermutationMatrix<Scalar_, SizeAtCompileTime, MaxSizeAtCompileTime, StorageIndex_>>
540 : evaluator_base<ScaledPermutationMatrix<Scalar_, SizeAtCompileTime, MaxSizeAtCompileTime, StorageIndex_>> {
541 using XprType = ScaledPermutationMatrix<Scalar_, SizeAtCompileTime, MaxSizeAtCompileTime, StorageIndex_>;
542 using Scalar = Scalar_;
543 using CoeffReturnType = Scalar;
544
545 static constexpr int CoeffReadCost =
546 int(NumTraits<StorageIndex_>::ReadCost) + int(NumTraits<Scalar>::ReadCost) + int(NumTraits<Scalar>::AddCost);
547 static constexpr unsigned int Flags = 0;
548 static constexpr int Alignment = 0;
549
550 EIGEN_DEVICE_FUNC explicit evaluator(const XprType& xpr) : m_indices(xpr.indices()), m_scales(xpr.scales()) {}
551
552 EIGEN_DEVICE_FUNC Scalar coeff(Index row, Index col) const {
553 return Index(m_indices.coeff(col)) == row ? m_scales.coeff(col) : Scalar(0);
554 }
555
556 // Linear access is requested only for vector-shaped operands (inner products), i.e. a 1x1 matrix.
557 EIGEN_DEVICE_FUNC Scalar coeff(Index index) const {
558 eigen_assert(index == 0);
559 return m_scales.coeff(index);
560 }
561
562 protected:
563 evaluator<typename XprType::IndicesType> m_indices;
564 evaluator<typename XprType::ScalesType> m_scales;
565};
566
567struct ScaledPermutation2Dense {};
568
569template <>
570struct AssignmentKind<DenseShape, ScaledPermutationShape> {
571 using Kind = ScaledPermutation2Dense;
572};
573
574// Scaled permutation to dense assignment: zero fill plus one scatter of n coefficients.
575template <typename DstXprType, typename SrcXprType, typename Functor>
576struct Assignment<DstXprType, SrcXprType, Functor, ScaledPermutation2Dense> {
577 static EIGEN_DEVICE_FUNC void run(
578 DstXprType& dst, const SrcXprType& src,
579 const internal::assign_op<typename DstXprType::Scalar, typename SrcXprType::Scalar>&) {
580 if (dst.rows() != src.rows() || dst.cols() != src.cols()) dst.resize(src.rows(), src.cols());
581 dst.setZero();
582 for (Index k = 0; k < src.rows(); ++k) dst.coeffRef(src.indices().coeff(k), k) = src.scales().coeff(k);
583 }
584
585 static EIGEN_DEVICE_FUNC void run(
586 DstXprType& dst, const SrcXprType& src,
587 const internal::add_assign_op<typename DstXprType::Scalar, typename SrcXprType::Scalar>&) {
588 EIGEN_INTERNAL_ASSERT_SAME_SHAPE(dst, src);
589 for (Index k = 0; k < src.rows(); ++k) dst.coeffRef(src.indices().coeff(k), k) += src.scales().coeff(k);
590 }
591
592 static EIGEN_DEVICE_FUNC void run(
593 DstXprType& dst, const SrcXprType& src,
594 const internal::sub_assign_op<typename DstXprType::Scalar, typename SrcXprType::Scalar>&) {
595 EIGEN_INTERNAL_ASSERT_SAME_SHAPE(dst, src);
596 for (Index k = 0; k < src.rows(); ++k) dst.coeffRef(src.indices().coeff(k), k) -= src.scales().coeff(k);
597 }
598};
599
600/***************************************************************************
601 * Products with a dense matrix: rows (S * X) or columns (X * S) are scaled and permuted, O(n m).
602 * evalTo reuses the dense permutation kernel, which handles X aliasing dst, and scales in place afterwards;
603 * the accumulating form works on the nested (evaluated once) operand.
604 * scaleAndAddTo evaluates dst += alpha * (lhs * rhs) with alpha a left factor of the product. Scalar
605 * multiplication need not commute, so both kernels keep that operand order: S * X regroups it as
606 * (alpha * v_c) * X(c, j) by associativity; X * S keeps alpha * (X(i, tau(c)) * v_c), as folding alpha into
607 * v_c would move it past the dense coefficient.
608 ***************************************************************************/
609
610template <typename Lhs, typename Rhs, int ProductTag>
611struct generic_product_impl<Lhs, Rhs, ScaledPermutationShape, DenseShape, ProductTag>
612 : generic_product_impl_base<Lhs, Rhs,
613 generic_product_impl<Lhs, Rhs, ScaledPermutationShape, DenseShape, ProductTag>> {
614 using Scalar = typename Product<Lhs, Rhs>::Scalar;
615
616 template <typename Dest>
617 static EIGEN_DEVICE_FUNC void evalTo(Dest& dst, const Lhs& lhs, const Rhs& rhs) {
618 permutation_matrix_product<Rhs, OnTheLeft, false, DenseShape>::run(dst, lhs.permutation(), rhs);
619 for (Index k = 0; k < lhs.rows(); ++k) {
620 auto row = dst.row(lhs.indices().coeff(k));
621 row = lhs.scales().coeff(k) * row;
622 }
623 }
624
625 template <typename Dest>
626 static EIGEN_DEVICE_FUNC void scaleAndAddTo(Dest& dst, const Lhs& lhs, const Rhs& rhs, const Scalar& alpha) {
627 typename nested_eval<Rhs, 1>::type rhsNested(rhs);
628 for (Index k = 0; k < lhs.rows(); ++k)
629 dst.row(lhs.indices().coeff(k)) += (alpha * lhs.scales().coeff(k)) * rhsNested.row(k);
630 }
631};
632
633template <typename Lhs, typename Rhs, int ProductTag>
634struct generic_product_impl<Lhs, Rhs, DenseShape, ScaledPermutationShape, ProductTag>
635 : generic_product_impl_base<Lhs, Rhs,
636 generic_product_impl<Lhs, Rhs, DenseShape, ScaledPermutationShape, ProductTag>> {
637 using Scalar = typename Product<Lhs, Rhs>::Scalar;
638
639 template <typename Dest>
640 static EIGEN_DEVICE_FUNC void evalTo(Dest& dst, const Lhs& lhs, const Rhs& rhs) {
641 permutation_matrix_product<Lhs, OnTheRight, false, DenseShape>::run(dst, rhs.permutation(), lhs);
642 for (Index k = 0; k < rhs.rows(); ++k) dst.col(k) *= rhs.scales().coeff(k);
643 }
644
645 template <typename Dest>
646 static EIGEN_DEVICE_FUNC void scaleAndAddTo(Dest& dst, const Lhs& lhs, const Rhs& rhs, const Scalar& alpha) {
647 typename nested_eval<Lhs, 1>::type lhsNested(lhs);
648 for (Index k = 0; k < rhs.rows(); ++k)
649 dst.col(k) += alpha * (lhsNested.col(rhs.indices().coeff(k)) * rhs.scales().coeff(k));
650 }
651};
652
653// A triangular or self-adjoint operand is evaluated into a plain matrix first, as toDenseMatrix() does.
654template <typename Lhs, typename Rhs, int ProductTag, bool ViewOnTheRight>
655struct scaled_permutation_view_product_impl;
656
657template <typename Lhs, typename Rhs, int ProductTag>
658struct scaled_permutation_view_product_impl<Lhs, Rhs, ProductTag, true>
659 : generic_product_impl_base<Lhs, Rhs, scaled_permutation_view_product_impl<Lhs, Rhs, ProductTag, true>> {
660 using Scalar = typename Product<Lhs, Rhs>::Scalar;
661 using DenseType = typename Rhs::DenseMatrixType;
662 using DenseImpl = generic_product_impl<Lhs, DenseType, ScaledPermutationShape, DenseShape, ProductTag>;
663
664 template <typename Dest>
665 static EIGEN_DEVICE_FUNC void evalTo(Dest& dst, const Lhs& lhs, const Rhs& rhs) {
666 const DenseType dense(rhs);
667 DenseImpl::evalTo(dst, lhs, dense);
668 }
669 template <typename Dest>
670 static EIGEN_DEVICE_FUNC void scaleAndAddTo(Dest& dst, const Lhs& lhs, const Rhs& rhs, const Scalar& alpha) {
671 const DenseType dense(rhs);
672 DenseImpl::scaleAndAddTo(dst, lhs, dense, alpha);
673 }
674};
675
676template <typename Lhs, typename Rhs, int ProductTag>
677struct scaled_permutation_view_product_impl<Lhs, Rhs, ProductTag, false>
678 : generic_product_impl_base<Lhs, Rhs, scaled_permutation_view_product_impl<Lhs, Rhs, ProductTag, false>> {
679 using Scalar = typename Product<Lhs, Rhs>::Scalar;
680 using DenseType = typename Lhs::DenseMatrixType;
681 using DenseImpl = generic_product_impl<DenseType, Rhs, DenseShape, ScaledPermutationShape, ProductTag>;
682
683 template <typename Dest>
684 static EIGEN_DEVICE_FUNC void evalTo(Dest& dst, const Lhs& lhs, const Rhs& rhs) {
685 const DenseType dense(lhs);
686 DenseImpl::evalTo(dst, dense, rhs);
687 }
688 template <typename Dest>
689 static EIGEN_DEVICE_FUNC void scaleAndAddTo(Dest& dst, const Lhs& lhs, const Rhs& rhs, const Scalar& alpha) {
690 const DenseType dense(lhs);
691 DenseImpl::scaleAndAddTo(dst, dense, rhs, alpha);
692 }
693};
694
695template <typename Lhs, typename Rhs, int ProductTag>
696struct generic_product_impl<Lhs, Rhs, ScaledPermutationShape, TriangularShape, ProductTag>
697 : scaled_permutation_view_product_impl<Lhs, Rhs, ProductTag, true> {};
698template <typename Lhs, typename Rhs, int ProductTag>
699struct generic_product_impl<Lhs, Rhs, ScaledPermutationShape, SelfAdjointShape, ProductTag>
700 : scaled_permutation_view_product_impl<Lhs, Rhs, ProductTag, true> {};
701template <typename Lhs, typename Rhs, int ProductTag>
702struct generic_product_impl<Lhs, Rhs, TriangularShape, ScaledPermutationShape, ProductTag>
703 : scaled_permutation_view_product_impl<Lhs, Rhs, ProductTag, false> {};
704template <typename Lhs, typename Rhs, int ProductTag>
705struct generic_product_impl<Lhs, Rhs, SelfAdjointShape, ScaledPermutationShape, ProductTag>
706 : scaled_permutation_view_product_impl<Lhs, Rhs, ProductTag, false> {};
707
708} // namespace internal
709
710} // namespace Eigen
711
712#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:225
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:214
PlainObject adjoint() const
Definition ScaledPermutationMatrix.h:164
PlainObject operator*(const Scalar &alpha) const
Definition ScaledPermutationMatrix.h:302
friend PlainObject operator*(const InverseImpl< OtherDerived, PermutationStorage > &other, const ScaledPermutationBase &scaled)
Definition ScaledPermutationMatrix.h:275
const Product< Derived, OtherDerived, DefaultProduct > operator*(const TriangularBase< OtherDerived > &other) const
Definition ScaledPermutationMatrix.h:195
Scalar coeff(Index row, Index col) const
Definition ScaledPermutationMatrix.h:124
friend PlainObject operator*(const Scalar &alpha, const ScaledPermutationBase &scaled)
Definition ScaledPermutationMatrix.h:305
const ScalesType & scales() const
Definition ScaledPermutationMatrix.h:121
friend PlainObject operator*(const PermutationBase< OtherDerived > &other, const ScaledPermutationBase &scaled)
Definition ScaledPermutationMatrix.h:249
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:236
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:223
friend const Product< OtherDerived, Derived, DefaultProduct > operator*(const TriangularBase< OtherDerived > &other, const ScaledPermutationBase &scaled)
Definition ScaledPermutationMatrix.h:202
const Product< Derived, OtherDerived, DefaultProduct > operator*(const MatrixBase< OtherDerived > &other) const
Definition ScaledPermutationMatrix.h:180
friend const Product< OtherDerived, Derived, DefaultProduct > operator*(const MatrixBase< OtherDerived > &other, const ScaledPermutationBase &scaled)
Definition ScaledPermutationMatrix.h:188
internal::scaled_permutation_product_result< Derived, OtherDerived, StorageIndex >::type operator*(const ScaledPermutationBase< OtherDerived > &other) const
Definition ScaledPermutationMatrix.h:288
constexpr Index cols() const
Definition ScaledPermutationMatrix.h:114
PlainObject operator*(const InverseImpl< OtherDerived, PermutationStorage > &other) const
Definition ScaledPermutationMatrix.h:260
PlainObject operator-() const
Definition ScaledPermutationMatrix.h:310
DenseMatrixType toDenseMatrix() const
Definition ScaledPermutationMatrix.h:130
Scaled permutation matrix with its storage.
Definition ScaledPermutationMatrix.h:363
void resize(Index size)
Definition ScaledPermutationMatrix.h:437
ScaledPermutationMatrix(const DiagonalBase< OtherDerived > &diagonal)
Definition ScaledPermutationMatrix.h:405
internal::scaled_permutation_result< typenameDiagonalDerived::Scalar, PermutationDerived, DiagonalDerived, typenamePermutationDerived::StorageIndex >::type operator*(const PermutationBase< PermutationDerived > &permutation, const DiagonalBase< DiagonalDerived > &diagonal)
Definition ScaledPermutationMatrix.h:468
const PermutationType & permutation() const
Definition ScaledPermutationMatrix.h:424
ScaledPermutationMatrix(const ScaledPermutationBase< OtherDerived > &other)
Definition ScaledPermutationMatrix.h:412
const ScalesType & scales() const
Definition ScaledPermutationMatrix.h:432
constexpr const IndicesType & indices() const
Definition ScaledPermutationMatrix.h:428
ScaledPermutationMatrix(const PermutationBase< OtherDerived > &permutation)
Definition ScaledPermutationMatrix.h:400
ScaledPermutationMatrix(Index size)
Definition ScaledPermutationMatrix.h:380
constexpr IndicesType & indices()
Definition ScaledPermutationMatrix.h:430
internal::scaled_permutation_result< typenameDiagonalDerived::Scalar, PermutationDerived, DiagonalDerived, typenamePermutationDerived::StorageIndex >::type operator*(const DiagonalBase< DiagonalDerived > &diagonal, const PermutationBase< PermutationDerived > &permutation)
Definition ScaledPermutationMatrix.h:481
ScaledPermutationMatrix & operator=(const ScaledPermutationBase< OtherDerived > &other)
Definition ScaledPermutationMatrix.h:417
ScaledPermutationMatrix(const PermutationBase< PermutationDerived > &permutation, const DiagonalBase< DiagonalDerived > &diagonal)
Definition ScaledPermutationMatrix.h:384
void setIdentity()
Definition ScaledPermutationMatrix.h:443
ScalesType & scales()
Definition ScaledPermutationMatrix.h:434
internal::scaled_permutation_result< typenameDiagonalDerived::Scalar, PermutationType, DiagonalDerived, typenamePermutationType::StorageIndex >::type operator*(const InverseImpl< PermutationType, PermutationStorage > &inverse, const DiagonalBase< DiagonalDerived > &diagonal)
Definition ScaledPermutationMatrix.h:498
ScaledPermutationMatrix(const MatrixBase< IndicesDerived > &indices, const MatrixBase< ScalesDerived > &scales)
Definition ScaledPermutationMatrix.h:392
PermutationType & permutation()
Definition ScaledPermutationMatrix.h:426
internal::scaled_permutation_result< typenameDiagonalDerived::Scalar, PermutationType, DiagonalDerived, typenamePermutationType::StorageIndex >::type operator*(const DiagonalBase< DiagonalDerived > &diagonal, const InverseImpl< PermutationType, PermutationStorage > &inverse)
Definition ScaledPermutationMatrix.h:515
void setIdentity(Index size)
Definition ScaledPermutationMatrix.h:449
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