Eigen  5.0.1-dev+master
 
Loading...
Searching...
No Matches
TriangularMatrix.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2008 Benoit Jacob <jacob.benoit.1@gmail.com>
5// Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
6//
7// This Source Code Form is subject to the terms of the Mozilla
8// Public License v. 2.0. If a copy of the MPL was not distributed
9// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10// SPDX-License-Identifier: MPL-2.0
11
12#ifndef EIGEN_TRIANGULARMATRIX_H
13#define EIGEN_TRIANGULARMATRIX_H
14
15// IWYU pragma: private
16#include "./InternalHeaderCheck.h"
17
18namespace Eigen {
19
20namespace internal {
21
22template <int Side, typename TriangularType, typename Rhs>
23struct triangular_solve_retval;
24
25template <typename MatrixType, unsigned int Mode, bool IsSelfAdjoint = (int(Mode) & int(SelfAdjoint)) != 0>
26struct triangular_base_return_types {
27 using MatrixConjugateReturnType = internal::remove_all_t<typename MatrixType::ConjugateReturnType>;
28 enum {
29 TransposeMode = (int(Mode) & int(Upper) ? Lower : 0) | (int(Mode) & int(Lower) ? Upper : 0) |
30 (int(Mode) & int(UnitDiag)) | (int(Mode) & int(ZeroDiag))
31 };
32 using ConstView = TriangularView<std::add_const_t<MatrixType>, Mode>;
33 using ConjugateReturnType = TriangularView<const MatrixConjugateReturnType, Mode>;
34 using AdjointReturnType = TriangularView<const typename MatrixType::AdjointReturnType, TransposeMode>;
35 using TransposeReturnType = TriangularView<typename MatrixType::TransposeReturnType, TransposeMode>;
36 using ConstTransposeReturnType = TriangularView<const typename MatrixType::ConstTransposeReturnType, TransposeMode>;
37};
38
39template <typename MatrixType, unsigned int Mode>
40struct triangular_base_return_types<MatrixType, Mode, true> {
41 using MatrixConjugateReturnType = internal::remove_all_t<typename MatrixType::ConjugateReturnType>;
42 enum {
43 TriangularPart = int(Mode) & int(Upper | Lower),
44 TransposeMode = (int(Mode) & int(Upper) ? Lower : 0) | (int(Mode) & int(Lower) ? Upper : 0)
45 };
46 using ConstView = SelfAdjointView<std::add_const_t<MatrixType>, TriangularPart>;
47 using ConjugateReturnType = SelfAdjointView<const MatrixConjugateReturnType, TriangularPart>;
48 using AdjointReturnType = SelfAdjointView<const typename MatrixType::AdjointReturnType, TransposeMode>;
49 using TransposeReturnType = SelfAdjointView<typename MatrixType::TransposeReturnType, TransposeMode>;
50 using ConstTransposeReturnType = SelfAdjointView<const typename MatrixType::ConstTransposeReturnType, TransposeMode>;
51};
52
53template <unsigned int Mode, typename Expression>
54EIGEN_DEVICE_FUNC inline typename triangular_base_return_types<Expression, Mode>::ConstView
55make_triangular_base_cwise_view(const Expression& expression) {
56 using ReturnType = typename triangular_base_return_types<Expression, Mode>::ConstView;
57 return ReturnType(expression);
58}
59
60} // namespace internal
61
67template <typename Derived>
68class TriangularBase : public EigenBase<Derived> {
69 public:
70 enum {
71 Mode = internal::traits<Derived>::Mode,
72 TransposeMode = (int(Mode) & int(Upper) ? Lower : 0) | (int(Mode) & int(Lower) ? Upper : 0) |
73 (int(Mode) & int(UnitDiag)) | (int(Mode) & int(ZeroDiag)),
74 RowsAtCompileTime = internal::traits<Derived>::RowsAtCompileTime,
75 ColsAtCompileTime = internal::traits<Derived>::ColsAtCompileTime,
76 MaxRowsAtCompileTime = internal::traits<Derived>::MaxRowsAtCompileTime,
77 MaxColsAtCompileTime = internal::traits<Derived>::MaxColsAtCompileTime,
78
79 SizeAtCompileTime = (internal::size_of_xpr_at_compile_time<Derived>::value),
83
84 MaxSizeAtCompileTime = internal::size_at_compile_time(internal::traits<Derived>::MaxRowsAtCompileTime,
85 internal::traits<Derived>::MaxColsAtCompileTime)
86
87 };
88 using Scalar = typename internal::traits<Derived>::Scalar;
89 using StorageKind = typename internal::traits<Derived>::StorageKind;
90 using StorageIndex = typename internal::traits<Derived>::StorageIndex;
91 using DenseMatrixType = typename internal::traits<Derived>::FullMatrixType;
92 using ExpressionType = typename internal::traits<Derived>::ExpressionType;
93 using DenseType = DenseMatrixType;
94 using Nested = const Derived&;
95 using ReturnTypes = internal::triangular_base_return_types<ExpressionType, Mode>;
96 using ConstView = typename ReturnTypes::ConstView;
97 using ConjugateReturnType = typename ReturnTypes::ConjugateReturnType;
98 using AdjointReturnType = typename ReturnTypes::AdjointReturnType;
99 using TransposeReturnType = typename ReturnTypes::TransposeReturnType;
100 using ConstTransposeReturnType = typename ReturnTypes::ConstTransposeReturnType;
101
102 EIGEN_DEVICE_FUNC inline TriangularBase() {
103 eigen_assert(!((int(Mode) & int(UnitDiag)) && (int(Mode) & int(ZeroDiag))));
104 }
105
106 EIGEN_DEVICE_FUNC constexpr Index rows() const noexcept { return derived().nestedExpression().rows(); }
107 EIGEN_DEVICE_FUNC constexpr Index cols() const noexcept { return derived().nestedExpression().cols(); }
108 EIGEN_DEVICE_FUNC constexpr Index size() const noexcept { return rows() * cols(); }
109 EIGEN_DEVICE_FUNC constexpr Index outerStride() const noexcept { return derived().nestedExpression().outerStride(); }
110 EIGEN_DEVICE_FUNC constexpr Index innerStride() const noexcept { return derived().nestedExpression().innerStride(); }
111
112 // dummy resize function
113 EIGEN_DEVICE_FUNC void resize(Index rows, Index cols) {
114 EIGEN_UNUSED_VARIABLE(rows);
115 EIGEN_UNUSED_VARIABLE(cols);
116 eigen_assert(rows == this->rows() && cols == this->cols());
117 }
118
120 EIGEN_DEVICE_FUNC void fill(const Scalar& value) { setConstant(value); }
121
123 EIGEN_DEVICE_FUNC Derived& setConstant(const Scalar& value) {
124 EIGEN_STATIC_ASSERT_LVALUE(Derived);
125 return derived() = DenseMatrixType::Constant(rows(), cols(), value);
126 }
127
129 EIGEN_DEVICE_FUNC Derived& setZero() { return setConstant(Scalar(0)); }
130
132 EIGEN_DEVICE_FUNC Derived& setOnes() { return setConstant(Scalar(1)); }
133
135 EIGEN_DEVICE_FUNC Derived& setRandom() {
136 EIGEN_STATIC_ASSERT_LVALUE(Derived);
137 return derived() = DenseMatrixType::Random(rows(), cols());
138 }
139
141 EIGEN_DEVICE_FUNC Derived& setIdentity() {
142 EIGEN_STATIC_ASSERT_LVALUE(Derived);
143 return derived() = DenseMatrixType::Identity(rows(), cols());
144 }
145
154 EIGEN_DEVICE_FUNC inline Scalar coeff(Index row, Index col) const {
155 check_coordinates_internal(row, col);
156 return derived().nestedExpression().coeff(row, col);
157 }
158
163 EIGEN_DEVICE_FUNC inline Scalar& coeffRef(Index row, Index col) {
164 EIGEN_STATIC_ASSERT_LVALUE(Derived);
165 check_coordinates_internal(row, col);
166 return derived().nestedExpression().coeffRef(row, col);
167 }
168
171 template <typename Other>
172 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void copyCoeff(Index row, Index col, Other& other) {
173 coeffRef(row, col) = other.coeff(row, col);
174 }
175
179 EIGEN_DEVICE_FUNC inline Scalar operator()(Index row, Index col) const {
180 check_coordinates(row, col);
181 return coeff(row, col);
182 }
183
185 EIGEN_DEVICE_FUNC inline Scalar& operator()(Index row, Index col) {
186 check_coordinates(row, col);
187 return coeffRef(row, col);
188 }
189
190#ifdef EIGEN_MULTIDIMENSIONAL_SUBSCRIPT
191 EIGEN_DEVICE_FUNC inline Scalar operator[](Index row, Index col) const { return operator()(row, col); }
192 EIGEN_DEVICE_FUNC inline Scalar& operator[](Index row, Index col) { return operator()(row, col); }
193#endif
194
195#ifndef EIGEN_PARSED_BY_DOXYGEN
196 EIGEN_DEVICE_FUNC constexpr inline const Derived& derived() const noexcept {
197 return *static_cast<const Derived*>(this);
198 }
199 EIGEN_DEVICE_FUNC constexpr inline Derived& derived() noexcept { return *static_cast<Derived*>(this); }
200#endif // not EIGEN_PARSED_BY_DOXYGEN
201
202 template <typename DenseDerived>
203 EIGEN_DEVICE_FUNC void evalTo(MatrixBase<DenseDerived>& other) const;
204 template <typename DenseDerived>
205 EIGEN_DEVICE_FUNC void evalToLazy(MatrixBase<DenseDerived>& other) const;
206
207 template <typename OtherDerived>
208 EIGEN_DEVICE_FUNC const Product<Derived, OtherDerived> operator*(const MatrixBase<OtherDerived>& rhs) const {
209 return Product<Derived, OtherDerived>(derived(), rhs.derived());
210 }
211
212 template <typename OtherDerived>
213 EIGEN_DEVICE_FUNC const Product<Derived, OtherDerived> operator*(const DiagonalBase<OtherDerived>& rhs) const {
215 }
216
223 template <typename OtherDerived>
224 EIGEN_DEVICE_FUNC const Product<Derived, OtherDerived> operator*(const TriangularBase<OtherDerived>& rhs) const {
226 }
227
228 template <typename OtherDerived,
229 std::enable_if_t<int(Mode) == int(OtherDerived::Mode) && (int(Mode) & int(UnitDiag)) == 0, int> = 0>
230 EIGEN_DEVICE_FUNC inline auto operator+(const TriangularBase<OtherDerived>& other) const {
231 return internal::make_triangular_base_cwise_view<Mode>(derived().nestedExpression() +
232 other.derived().nestedExpression());
233 }
234
235 template <typename OtherDerived,
236 std::enable_if_t<int(Mode) == int(OtherDerived::Mode) && (int(Mode) & int(UnitDiag)) == 0, int> = 0>
237 EIGEN_DEVICE_FUNC inline auto operator-(const TriangularBase<OtherDerived>& other) const {
238 return internal::make_triangular_base_cwise_view<Mode>(derived().nestedExpression() -
239 other.derived().nestedExpression());
240 }
241
242 // Sums with a diagonal matrix keep the view's structure: the result is the same view of the lazy dense sum.
243 // A unit or zero diagonal is implicit in the view and would swallow the diagonal term, so those modes have
244 // no such operator; a self-adjoint view needs a real diagonal to stay self-adjoint.
245
246 // The Mode_ = Mode parameter makes the condition depend on the operator's own template parameters, so an
247 // excluded mode removes the overload instead of failing the class instantiation.
248
250 template <typename OtherDerived, unsigned int Mode_ = Mode,
251 std::enable_if_t<(int(Mode_) & (int(UnitDiag) | int(ZeroDiag))) == 0, int> = 0>
252 EIGEN_DEVICE_FUNC inline auto operator+(const DiagonalBase<OtherDerived>& other) const {
253 return internal::make_triangular_base_cwise_view<Mode>(derived().nestedExpression() + other.derived());
254 }
255
258 template <typename OtherDerived, unsigned int Mode_ = Mode,
259 std::enable_if_t<(int(Mode_) & (int(UnitDiag) | int(ZeroDiag))) == 0, int> = 0>
260 EIGEN_DEVICE_FUNC inline auto operator-(const DiagonalBase<OtherDerived>& other) const {
261 return internal::make_triangular_base_cwise_view<Mode>(derived().nestedExpression() - other.derived());
262 }
263
266 template <typename OtherDerived, unsigned int Mode_ = Mode,
267 std::enable_if_t<(int(Mode_) & (int(UnitDiag) | int(ZeroDiag))) == 0, int> = 0>
268 friend EIGEN_DEVICE_FUNC inline auto operator+(const DiagonalBase<OtherDerived>& lhs, const Derived& rhs) {
269 return internal::make_triangular_base_cwise_view<Mode>(lhs.derived() + rhs.nestedExpression());
270 }
271
274 template <typename OtherDerived, unsigned int Mode_ = Mode,
275 std::enable_if_t<(int(Mode_) & (int(UnitDiag) | int(ZeroDiag))) == 0, int> = 0>
276 friend EIGEN_DEVICE_FUNC inline auto operator-(const DiagonalBase<OtherDerived>& lhs, const Derived& rhs) {
277 return internal::make_triangular_base_cwise_view<Mode>(lhs.derived() - rhs.nestedExpression());
278 }
279
280 template <typename OtherDerived>
281 friend EIGEN_DEVICE_FUNC const Product<OtherDerived, Derived> operator*(const MatrixBase<OtherDerived>& lhs,
282 const Derived& rhs) {
283 return Product<OtherDerived, Derived>(lhs.derived(), rhs);
284 }
285
286 template <typename OtherDerived>
287 friend EIGEN_DEVICE_FUNC const Product<OtherDerived, Derived> operator*(const DiagonalBase<OtherDerived>& lhs,
288 const Derived& rhs) {
289 return Product<OtherDerived, Derived>(lhs.derived(), rhs);
290 }
291
292 // Products with permutations (and their inverses) evaluate to a dense matrix: the permuted view has no
293 // triangular or self-adjoint structure Eigen could represent. They live here rather than next to the
294 // MatrixBase permutation operators so that every operator* a TriangularBase takes part in is in one place.
295
297 template <typename OtherDerived>
300 }
301
303 template <typename OtherDerived>
305 const InverseImpl<OtherDerived, PermutationStorage>& rhs) const {
306 return Product<Derived, Inverse<OtherDerived>>(derived(), rhs.derived());
307 }
308
310 template <typename OtherDerived>
312 const Derived& rhs) {
313 return Product<OtherDerived, Derived>(lhs.derived(), rhs);
314 }
315
317 template <typename OtherDerived>
318 friend EIGEN_DEVICE_FUNC const Product<Inverse<OtherDerived>, Derived> operator*(
319 const InverseImpl<OtherDerived, PermutationStorage>& lhs, const Derived& rhs) {
320 return Product<Inverse<OtherDerived>, Derived>(lhs.derived(), rhs);
321 }
322
324 EIGEN_DEVICE_FUNC inline const ConjugateReturnType conjugate() const {
325 return ConjugateReturnType(derived().nestedExpression().conjugate());
326 }
327
331 template <bool Cond>
332 EIGEN_DEVICE_FUNC inline std::conditional_t<Cond, ConjugateReturnType, ConstView> conjugateIf() const {
333 using ReturnType = std::conditional_t<Cond, ConjugateReturnType, ConstView>;
334 return ReturnType(derived().nestedExpression().template conjugateIf<Cond>());
335 }
336
338 EIGEN_DEVICE_FUNC inline const AdjointReturnType adjoint() const {
339 return AdjointReturnType(derived().nestedExpression().adjoint());
340 }
341
343 template <class Dummy = int, std::enable_if_t<Eigen::internal::is_lvalue<ExpressionType>::value, Dummy*> = nullptr>
344 EIGEN_DEVICE_FUNC inline TransposeReturnType transpose() {
345 typename ExpressionType::TransposeReturnType tmp(derived().nestedExpression());
346 return TransposeReturnType(tmp);
347 }
348
349 template <class Dummy = int>
350 EIGEN_DEPRECATED_WITH_REASON("Omit the implementation-only argument.")
351 EIGEN_DEVICE_FUNC inline TransposeReturnType
352 transpose(std::enable_if_t<Eigen::internal::is_lvalue<ExpressionType>::value, Dummy*>) {
353 return transpose<Dummy>();
354 }
355
357 EIGEN_DEVICE_FUNC inline const ConstTransposeReturnType transpose() const {
358 return ConstTransposeReturnType(derived().nestedExpression().transpose());
359 }
360
361 EIGEN_DEVICE_FUNC DenseMatrixType toDenseMatrix() const {
362 DenseMatrixType res(rows(), cols());
363 evalToLazy(res);
364 return res;
365 }
366
367 protected:
368 void check_coordinates(Index row, Index col) const {
369 EIGEN_ONLY_USED_FOR_DEBUG(row);
370 EIGEN_ONLY_USED_FOR_DEBUG(col);
371 eigen_assert(col >= 0 && col < cols() && row >= 0 && row < rows());
372 const int mode = int(Mode) & ~SelfAdjoint;
373 EIGEN_ONLY_USED_FOR_DEBUG(mode);
374 eigen_assert((mode == Upper && col >= row) || (mode == Lower && col <= row) ||
375 ((mode == StrictlyUpper || mode == UnitUpper) && col > row) ||
376 ((mode == StrictlyLower || mode == UnitLower) && col < row));
377 }
378
379#ifdef EIGEN_INTERNAL_DEBUGGING
380 void check_coordinates_internal(Index row, Index col) const { check_coordinates(row, col); }
381#else
382 void check_coordinates_internal(Index, Index) const {}
383#endif
384};
385
404namespace internal {
405template <typename MatrixType, unsigned int Mode_>
406struct traits<TriangularView<MatrixType, Mode_>> : traits<MatrixType> {
407 using MatrixTypeNested = typename ref_selector<MatrixType>::non_const_type;
408 using MatrixTypeNestedNonRef = std::remove_reference_t<MatrixTypeNested>;
409 using MatrixTypeNestedCleaned = remove_all_t<MatrixTypeNested>;
410 using FullMatrixType = typename MatrixType::PlainObject;
411 using ExpressionType = MatrixType;
412 enum {
413 Mode = Mode_,
414 FlagsLvalueBit = is_lvalue<MatrixType>::value ? LvalueBit : 0,
415 Flags = (MatrixTypeNestedCleaned::Flags & (HereditaryBits | FlagsLvalueBit) &
417 };
418};
419} // namespace internal
420
421template <typename MatrixType_, unsigned int Mode_, typename StorageKind>
422class TriangularViewImpl;
423
424template <typename MatrixType_, unsigned int Mode_>
425class TriangularView
426 : public TriangularViewImpl<MatrixType_, Mode_, typename internal::traits<MatrixType_>::StorageKind> {
427 public:
428 using Base = TriangularViewImpl<MatrixType_, Mode_, typename internal::traits<MatrixType_>::StorageKind>;
429 using Scalar = typename internal::traits<TriangularView>::Scalar;
430 using MatrixType = MatrixType_;
431
432 protected:
433 using MatrixTypeNested = typename internal::traits<TriangularView>::MatrixTypeNested;
434 using MatrixTypeNestedNonRef = typename internal::traits<TriangularView>::MatrixTypeNestedNonRef;
435
436 public:
437 using StorageKind = typename internal::traits<TriangularView>::StorageKind;
438 using NestedExpression = typename internal::traits<TriangularView>::MatrixTypeNestedCleaned;
439
440 enum {
441 Mode = Mode_,
442 Flags = internal::traits<TriangularView>::Flags,
443 TransposeMode = (int(Mode) & int(Upper) ? Lower : 0) | (int(Mode) & int(Lower) ? Upper : 0) |
444 (int(Mode) & int(UnitDiag)) | (int(Mode) & int(ZeroDiag)),
445 IsVectorAtCompileTime = false
446 };
447
448 EIGEN_DEVICE_FUNC explicit inline TriangularView(MatrixType& matrix) : m_matrix(matrix) {}
449
450 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(TriangularView)
451
452
453 EIGEN_DEVICE_FUNC constexpr const NestedExpression& nestedExpression() const noexcept { return m_matrix; }
454
456 EIGEN_DEVICE_FUNC constexpr MatrixTypeNestedNonRef& nestedExpression() noexcept { return m_matrix; }
457
458 template <typename Other>
459 EIGEN_DEVICE_FUNC inline Solve<TriangularView, Other> solve(const MatrixBase<Other>& other) const {
460 return Solve<TriangularView, Other>(*this, other.derived());
461 }
462
463 using Base::solve;
464
470 EIGEN_STATIC_ASSERT((Mode & (UnitDiag | ZeroDiag)) == 0, PROGRAMMING_ERROR);
472 }
473
476 EIGEN_STATIC_ASSERT((Mode & (UnitDiag | ZeroDiag)) == 0, PROGRAMMING_ERROR);
478 }
479
482 EIGEN_DEVICE_FUNC Scalar determinant() const {
483 EIGEN_IF_CONSTEXPR (int(Mode) & int(UnitDiag)) {
484 return Scalar(1);
485 } else EIGEN_IF_CONSTEXPR (int(Mode) & int(ZeroDiag)) {
486 // A zero diagonal makes the product vanish unless it is empty.
487 return Scalar(m_matrix.rows() == 0 ? 1 : 0);
488 } else {
489 return internal::scaled_prod(m_matrix.diagonal()).value();
490 }
491 }
492
493 protected:
494 MatrixTypeNested m_matrix;
495};
496
506template <typename MatrixType_, unsigned int Mode_>
507class TriangularViewImpl<MatrixType_, Mode_, Dense> : public TriangularBase<TriangularView<MatrixType_, Mode_>> {
508 public:
509 using TriangularViewType = TriangularView<MatrixType_, Mode_>;
510
511 using Base = TriangularBase<TriangularViewType>;
512 using Scalar = typename internal::traits<TriangularViewType>::Scalar;
513
514 using MatrixType = MatrixType_;
515 using DenseMatrixType = typename MatrixType::PlainObject;
516 using PlainObject = DenseMatrixType;
517
518 public:
519 using Base::derived;
520 using Base::evalToLazy;
521 using Base::operator*;
522
523 using StorageKind = typename internal::traits<TriangularViewType>::StorageKind;
524
525 enum { Mode = Mode_, Flags = internal::traits<TriangularViewType>::Flags };
526
528 template <typename Other>
529 EIGEN_DEVICE_FUNC TriangularViewType& operator+=(const DenseBase<Other>& other) {
530 internal::call_assignment_no_alias(derived(), other.derived(),
531 internal::add_assign_op<Scalar, typename Other::Scalar>());
532 return derived();
533 }
534
535 template <typename Other>
536 EIGEN_DEVICE_FUNC TriangularViewType& operator-=(const DenseBase<Other>& other) {
537 internal::call_assignment_no_alias(derived(), other.derived(),
538 internal::sub_assign_op<Scalar, typename Other::Scalar>());
539 return derived();
540 }
541
543 EIGEN_DEVICE_FUNC TriangularViewType& operator*=(const typename internal::traits<MatrixType>::Scalar& other) {
544 return *this = derived().nestedExpression() * other;
545 }
546
547 EIGEN_DEVICE_FUNC TriangularViewType& operator/=(const typename internal::traits<MatrixType>::Scalar& other) {
548 return *this = derived().nestedExpression() / other;
549 }
550
552 template <typename OtherDerived>
553 EIGEN_DEVICE_FUNC TriangularViewType& operator=(const TriangularBase<OtherDerived>& other);
554
556 template <typename OtherDerived>
557 EIGEN_DEVICE_FUNC TriangularViewType& operator=(const MatrixBase<OtherDerived>& other);
558
559#ifndef EIGEN_PARSED_BY_DOXYGEN
560 EIGEN_DEVICE_FUNC TriangularViewType& operator=(const TriangularViewImpl& other) {
561 return *this = other.derived().nestedExpression();
562 }
563
564 template <typename OtherDerived>
566 EIGEN_DEPRECATED EIGEN_DEVICE_FUNC void lazyAssign(const TriangularBase<OtherDerived>& other);
567
568 template <typename OtherDerived>
570 EIGEN_DEPRECATED EIGEN_DEVICE_FUNC void lazyAssign(const MatrixBase<OtherDerived>& other);
571#endif
572
573 // Scaling a unit triangular view would break its implicit unit diagonal, so only non-unit modes participate.
574 template <unsigned int M = Mode, std::enable_if_t<(M & UnitDiag) == 0, int> = 0>
575 EIGEN_DEVICE_FUNC const TriangularView<
576 const EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(MatrixType, Scalar, internal::scalar_product_op), Mode>
577 operator*(const Scalar& s) const {
578 return (derived().nestedExpression() * s).template triangularView<Mode>();
579 }
580
581 template <unsigned int M = Mode, std::enable_if_t<(M & UnitDiag) == 0, int> = 0>
582 friend EIGEN_DEVICE_FUNC const TriangularView<
583 const EIGEN_SCALAR_BINARYOP_EXPR_RETURN_TYPE(Scalar, MatrixType, internal::scalar_product_op), Mode>
584 operator*(const Scalar& s, const TriangularViewImpl& mat) {
585 return (s * mat.derived().nestedExpression()).template triangularView<Mode>();
586 }
587
611 template <int Side, typename Other>
612 inline const internal::triangular_solve_retval<Side, TriangularViewType, Other> solve(
613 const MatrixBase<Other>& other) const;
614
624 template <int Side, typename OtherDerived>
625 EIGEN_DEVICE_FUNC void solveInPlace(const MatrixBase<OtherDerived>& other) const;
626
627 template <typename OtherDerived>
628 EIGEN_DEVICE_FUNC void solveInPlace(const MatrixBase<OtherDerived>& other) const {
629 return solveInPlace<OnTheLeft>(other);
630 }
631
646 EIGEN_DEVICE_FUNC void inverseInPlace();
647
649 template <typename OtherDerived>
650 EIGEN_DEVICE_FUNC
651#ifdef EIGEN_PARSED_BY_DOXYGEN
652 void
653 swap(TriangularBase<OtherDerived>& other)
654#else
655 void
656 swap(TriangularBase<OtherDerived> const& other)
657#endif
658 {
659 EIGEN_STATIC_ASSERT_LVALUE(OtherDerived);
660 call_assignment(derived(), other.const_cast_derived(), internal::swap_assign_op<Scalar>());
661 }
662
664 template <typename OtherDerived>
666 EIGEN_DEPRECATED EIGEN_DEVICE_FUNC void swap(MatrixBase<OtherDerived> const& other) {
667 EIGEN_STATIC_ASSERT_LVALUE(OtherDerived);
668 call_assignment(derived(), other.const_cast_derived(), internal::swap_assign_op<Scalar>());
669 }
670
671 template <typename RhsType, typename DstType>
672 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void _solve_impl(const RhsType& rhs, DstType& dst) const {
673 if (!internal::is_same_dense(dst, rhs)) dst = rhs;
674 this->solveInPlace(dst);
675 }
676
677 template <typename ProductType>
678 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TriangularViewType& _assignProduct(const ProductType& prod, const Scalar& alpha,
679 bool beta);
680
681 protected:
682 EIGEN_DEFAULT_COPY_CONSTRUCTOR(TriangularViewImpl)
683 EIGEN_DEFAULT_EMPTY_CONSTRUCTOR_AND_DESTRUCTOR(TriangularViewImpl)
684};
685
686/***************************************************************************
687 * Implementation of triangular evaluation/assignment
688 ***************************************************************************/
689
690#ifndef EIGEN_PARSED_BY_DOXYGEN
691// FIXME should we keep that possibility
692template <typename MatrixType, unsigned int Mode>
693template <typename OtherDerived>
694EIGEN_DEVICE_FUNC inline TriangularView<MatrixType, Mode>& TriangularViewImpl<MatrixType, Mode, Dense>::operator=(
695 const MatrixBase<OtherDerived>& other) {
696 internal::call_assignment_no_alias(derived(), other.derived(),
697 internal::assign_op<Scalar, typename OtherDerived::Scalar>());
698 return derived();
699}
700
701// FIXME should we keep that possibility
702template <typename MatrixType, unsigned int Mode>
703template <typename OtherDerived>
704EIGEN_DEVICE_FUNC void TriangularViewImpl<MatrixType, Mode, Dense>::lazyAssign(const MatrixBase<OtherDerived>& other) {
705 internal::call_assignment_no_alias(derived(), other.template triangularView<Mode>());
706}
707
708template <typename MatrixType, unsigned int Mode>
709template <typename OtherDerived>
710EIGEN_DEVICE_FUNC inline TriangularView<MatrixType, Mode>& TriangularViewImpl<MatrixType, Mode, Dense>::operator=(
711 const TriangularBase<OtherDerived>& other) {
712 eigen_assert(Mode == int(OtherDerived::Mode));
713 internal::call_assignment(derived(), other.derived());
714 return derived();
715}
716
717template <typename MatrixType, unsigned int Mode>
718template <typename OtherDerived>
719EIGEN_DEVICE_FUNC void TriangularViewImpl<MatrixType, Mode, Dense>::lazyAssign(
720 const TriangularBase<OtherDerived>& other) {
721 eigen_assert(Mode == int(OtherDerived::Mode));
722 internal::call_assignment_no_alias(derived(), other.derived());
723}
724#endif
725
726/***************************************************************************
727 * Implementation of TriangularBase methods
728 ***************************************************************************/
729
732template <typename Derived>
733template <typename DenseDerived>
734EIGEN_DEVICE_FUNC void TriangularBase<Derived>::evalTo(MatrixBase<DenseDerived>& other) const {
735 evalToLazy(other.derived());
736}
737
738/***************************************************************************
739 * Implementation of TriangularView methods
740 ***************************************************************************/
741
742/***************************************************************************
743 * Implementation of MatrixBase methods
744 ***************************************************************************/
745
757template <typename Derived>
758template <unsigned int Mode>
759EIGEN_DEVICE_FUNC constexpr typename MatrixBase<Derived>::template TriangularViewReturnType<Mode>::Type
760MatrixBase<Derived>::triangularView() {
761 return typename TriangularViewReturnType<Mode>::Type(derived());
762}
763
765template <typename Derived>
766template <unsigned int Mode>
767EIGEN_DEVICE_FUNC constexpr typename MatrixBase<Derived>::template ConstTriangularViewReturnType<Mode>::Type
768MatrixBase<Derived>::triangularView() const {
769 return typename ConstTriangularViewReturnType<Mode>::Type(derived());
770}
771
777template <typename Derived>
778bool MatrixBase<Derived>::isUpperTriangular(const RealScalar& prec) const {
779 RealScalar maxAbsOnUpperPart = static_cast<RealScalar>(-1);
780 for (Index j = 0; j < cols(); ++j) {
781 Index maxi = numext::mini(j, rows() - 1);
782 for (Index i = 0; i <= maxi; ++i) {
783 RealScalar absValue = numext::abs(coeff(i, j));
784 if (absValue > maxAbsOnUpperPart) maxAbsOnUpperPart = absValue;
785 }
786 }
787 RealScalar threshold = maxAbsOnUpperPart * prec;
788 for (Index j = 0; j < cols(); ++j)
789 for (Index i = j + 1; i < rows(); ++i)
790 if (numext::abs(coeff(i, j)) > threshold) return false;
791 return true;
792}
793
799template <typename Derived>
800bool MatrixBase<Derived>::isLowerTriangular(const RealScalar& prec) const {
801 RealScalar maxAbsOnLowerPart = static_cast<RealScalar>(-1);
802 for (Index j = 0; j < cols(); ++j)
803 for (Index i = j; i < rows(); ++i) {
804 RealScalar absValue = numext::abs(coeff(i, j));
805 if (absValue > maxAbsOnLowerPart) maxAbsOnLowerPart = absValue;
806 }
807 RealScalar threshold = maxAbsOnLowerPart * prec;
808 for (Index j = 1; j < cols(); ++j) {
809 // Rows [0, min(j, rows())) of column j lie strictly above the diagonal. For a column past the diagonal block of a
810 // wide matrix that is the whole column, so the bound is rows(), not rows() - 1.
811 Index maxi = numext::mini(j, rows());
812 for (Index i = 0; i < maxi; ++i)
813 if (numext::abs(coeff(i, j)) > threshold) return false;
814 }
815 return true;
816}
817
818/***************************************************************************
819****************************************************************************
820* Evaluators and Assignment of triangular expressions
821***************************************************************************
822***************************************************************************/
823
824namespace internal {
825
826// TODO: currently a triangular expression has the form TriangularView<.,.>
827// in the future triangular-ness should be defined by the expression traits
828// such that Transpose<TriangularView<.,.> > is valid. (currently TriangularBase::transpose() is overloaded to make
829// it work)
830template <typename MatrixType, unsigned int Mode>
831struct evaluator_traits<TriangularView<MatrixType, Mode>> {
832 using Kind = typename storage_kind_to_evaluator_kind<typename MatrixType::StorageKind>::Kind;
833 using Shape = typename glue_shapes<typename evaluator_traits<MatrixType>::Shape, TriangularShape>::type;
834};
835
836template <typename MatrixType, unsigned int Mode>
837struct unary_evaluator<TriangularView<MatrixType, Mode>, IndexBased> : evaluator<internal::remove_all_t<MatrixType>> {
838 using XprType = TriangularView<MatrixType, Mode>;
839 using Base = evaluator<internal::remove_all_t<MatrixType>>;
840 EIGEN_DEVICE_FUNC unary_evaluator(const XprType& xpr) : Base(xpr.nestedExpression()) {}
841};
842
843// Additional assignment kinds:
844struct Triangular2Triangular {};
845struct Triangular2Dense {};
846struct Dense2Triangular {};
847
848template <typename Kernel, unsigned int Mode, int UnrollCount, bool SetOpposite>
849struct triangular_assignment_loop;
850
851// Whether call_triangular_assignment_loop fully unrolls the assignment.
852template <typename DstEvaluatorType, typename SrcEvaluatorType>
853struct triangular_assignment_unrolls {
854 static constexpr int Size = DstEvaluatorType::XprType::SizeAtCompileTime;
855 static constexpr bool value =
856 Size != Dynamic && SrcEvaluatorType::CoeffReadCost < HugeCost &&
857 Size * (int(DstEvaluatorType::CoeffReadCost) + int(SrcEvaluatorType::CoeffReadCost)) / 2 <= EIGEN_UNROLLING_LIMIT;
858};
859
865template <int UpLo, int Mode, int SetOpposite, typename DstEvaluatorTypeT, typename SrcEvaluatorTypeT, typename Functor,
866 int Version = Specialized>
867class triangular_dense_assignment_kernel
868 : public generic_dense_assignment_kernel<DstEvaluatorTypeT, SrcEvaluatorTypeT, Functor, Version> {
869 protected:
870 using Base = generic_dense_assignment_kernel<DstEvaluatorTypeT, SrcEvaluatorTypeT, Functor, Version>;
871 using DstXprType = typename Base::DstXprType;
872 using SrcXprType = typename Base::SrcXprType;
873 using Base::m_dst;
874 using Base::m_functor;
875 using Base::m_src;
876
877 public:
878 using DstEvaluatorType = typename Base::DstEvaluatorType;
879 using SrcEvaluatorType = typename Base::SrcEvaluatorType;
880 using Scalar = typename Base::Scalar;
881 using AssignmentTraits = typename Base::AssignmentTraits;
882
883 EIGEN_DEVICE_FUNC triangular_dense_assignment_kernel(DstEvaluatorType& dst, const SrcEvaluatorType& src,
884 const Functor& func, DstXprType& dstExpr)
885 : Base(dst, src, func, dstExpr) {}
886
887#ifdef EIGEN_INTERNAL_DEBUGGING
888 EIGEN_DEVICE_FUNC void assignCoeff(Index row, Index col) {
889 eigen_internal_assert(row != col);
890 Base::assignCoeff(row, col);
891 }
892#else
893 using Base::assignCoeff;
894#endif
895
896 EIGEN_DEVICE_FUNC void assignDiagonalCoeff(Index id) {
897 EIGEN_IF_CONSTEXPR (Mode == UnitDiag && SetOpposite) {
898 m_functor.assignCoeff(m_dst.coeffRef(id, id), Scalar(1));
899 } else EIGEN_IF_CONSTEXPR (Mode == ZeroDiag && SetOpposite) {
900 m_functor.assignCoeff(m_dst.coeffRef(id, id), Scalar(0));
901 } else EIGEN_IF_CONSTEXPR (Mode == 0) {
902 Base::assignCoeff(id, id);
903 }
904 }
905
906 EIGEN_DEVICE_FUNC void assignOppositeCoeff(Index row, Index col) {
907 eigen_internal_assert(row != col);
908 EIGEN_IF_CONSTEXPR (SetOpposite) {
909 m_functor.assignCoeff(m_dst.coeffRef(row, col), Scalar(0));
910 }
911 }
912};
913
914template <int Mode, bool SetOpposite, typename DstXprType, typename SrcXprType, typename Functor>
915EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void call_triangular_assignment_loop(DstXprType& dst, const SrcXprType& src,
916 const Functor& func) {
917 using DstEvaluatorType = evaluator<DstXprType>;
918 using SrcEvaluatorType = evaluator<SrcXprType>;
919
920 SrcEvaluatorType srcEvaluator(src);
921
922 Index dstRows = src.rows();
923 Index dstCols = src.cols();
924 if ((dst.rows() != dstRows) || (dst.cols() != dstCols)) dst.resize(dstRows, dstCols);
925 DstEvaluatorType dstEvaluator(dst);
926
927 using Kernel = triangular_dense_assignment_kernel<Mode&(Lower | Upper), Mode&(UnitDiag | ZeroDiag | SelfAdjoint),
928 SetOpposite, DstEvaluatorType, SrcEvaluatorType, Functor>;
929 Kernel kernel(dstEvaluator, srcEvaluator, func, dst.const_cast_derived());
930
931 constexpr bool unroll = triangular_assignment_unrolls<DstEvaluatorType, SrcEvaluatorType>::value;
932 triangular_assignment_loop<Kernel, Mode, unroll ? int(DstXprType::SizeAtCompileTime) : Dynamic, SetOpposite>::run(
933 kernel);
934}
935
936template <int Mode, bool SetOpposite, typename DstXprType, typename SrcXprType>
937EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void call_triangular_assignment_loop(DstXprType& dst, const SrcXprType& src) {
938 call_triangular_assignment_loop<Mode, SetOpposite>(
939 dst, src, internal::assign_op<typename DstXprType::Scalar, typename SrcXprType::Scalar>());
940}
941
942template <>
943struct AssignmentKind<TriangularShape, TriangularShape> {
944 using Kind = Triangular2Triangular;
945};
946template <>
947struct AssignmentKind<DenseShape, TriangularShape> {
948 using Kind = Triangular2Dense;
949};
950template <>
951struct AssignmentKind<TriangularShape, DenseShape> {
952 using Kind = Dense2Triangular;
953};
954
955template <typename Shape>
956struct is_dense_structured_shape
957 : bool_constant<std::is_same<Shape, TriangularShape>::value || std::is_same<Shape, SelfAdjointShape>::value> {};
958
959template <typename Lhs, typename Rhs>
960struct is_dense_structured_diagonal_product
961 : bool_constant<(is_dense_structured_shape<typename evaluator_traits<Lhs>::Shape>::value &&
962 std::is_same<typename evaluator_traits<Rhs>::Shape, DiagonalShape>::value) ||
963 (std::is_same<typename evaluator_traits<Lhs>::Shape, DiagonalShape>::value &&
964 is_dense_structured_shape<typename evaluator_traits<Rhs>::Shape>::value)> {};
965
966template <typename DstXprType, typename SrcXprType, typename Functor>
967struct Assignment<DstXprType, SrcXprType, Functor, Triangular2Triangular> {
968 EIGEN_DEVICE_FUNC static void run(DstXprType& dst, const SrcXprType& src, const Functor& func) {
969 eigen_assert(int(DstXprType::Mode) == int(SrcXprType::Mode));
970
971 call_triangular_assignment_loop<DstXprType::Mode, false>(dst, src, func);
972 }
973};
974
975template <typename DstXprType, typename SrcXprType, typename Functor>
976struct Assignment<DstXprType, SrcXprType, Functor, Triangular2Dense> {
977 EIGEN_DEVICE_FUNC static void run(DstXprType& dst, const SrcXprType& src, const Functor& func) {
978 call_triangular_assignment_loop<SrcXprType::Mode, (int(SrcXprType::Mode) & int(SelfAdjoint)) == 0>(dst, src, func);
979 }
980};
981
982template <typename DstXprType, typename SrcXprType, typename Functor>
983struct Assignment<DstXprType, SrcXprType, Functor, Dense2Triangular> {
984 EIGEN_DEVICE_FUNC static void run(DstXprType& dst, const SrcXprType& src, const Functor& func) {
985 call_triangular_assignment_loop<DstXprType::Mode, false>(dst, src, func);
986 }
987};
988
989template <typename Kernel, unsigned int Mode, int UnrollCount, bool SetOpposite>
990struct triangular_assignment_loop {
991 // FIXME: this is not very clean, perhaps this information should be provided by the kernel?
992 using DstEvaluatorType = typename Kernel::DstEvaluatorType;
993 using DstXprType = typename DstEvaluatorType::XprType;
994
995 enum {
996 col = (UnrollCount - 1) / DstXprType::RowsAtCompileTime,
997 row = (UnrollCount - 1) % DstXprType::RowsAtCompileTime
998 };
999
1000 using Scalar = typename Kernel::Scalar;
1001
1002 EIGEN_DEVICE_FUNC static inline void run(Kernel& kernel) {
1003 triangular_assignment_loop<Kernel, Mode, UnrollCount - 1, SetOpposite>::run(kernel);
1004
1005 if (row == col)
1006 kernel.assignDiagonalCoeff(row);
1007 else if (((Mode & Lower) && row > col) || ((Mode & Upper) && row < col))
1008 kernel.assignCoeff(row, col);
1009 else EIGEN_IF_CONSTEXPR (SetOpposite) {
1010 kernel.assignOppositeCoeff(row, col);
1011 }
1012 }
1013};
1014
1015// prevent buggy user code from causing an infinite recursion
1016template <typename Kernel, unsigned int Mode, bool SetOpposite>
1017struct triangular_assignment_loop<Kernel, Mode, 0, SetOpposite> {
1018 EIGEN_DEVICE_FUNC static inline void run(Kernel&) {}
1019};
1020
1021// TODO: experiment with a recursive assignment procedure splitting the current
1022// triangular part into one rectangular and two triangular parts.
1023
1024template <typename Kernel, unsigned int Mode, bool SetOpposite>
1025struct triangular_assignment_loop<Kernel, Mode, Dynamic, SetOpposite> {
1026 using Scalar = typename Kernel::Scalar;
1027 using DstEvaluatorType = typename Kernel::DstEvaluatorType;
1028 using AssignmentTraits = typename Kernel::AssignmentTraits;
1029
1030 enum {
1031 IsRowMajor = (int(DstEvaluatorType::Flags) & RowMajorBit) != 0,
1032 // In col-major: inner=row, outer=col. Upper means row<col i.e. inner<outer -> active before diagonal.
1033 // In row-major: inner=col, outer=row. Upper means row<col i.e. inner>outer -> active after diagonal.
1034 // So ActiveBeforeDiag = (Upper XOR IsRowMajor).
1035 ActiveBeforeDiag = (bool(Mode & Upper) != bool(IsRowMajor))
1036 };
1037
1038 // Compile-time outer/inner to row/col mapping. These constant-fold away entirely:
1039 // ColMajor: row(outer,i) -> i, col(outer,i) -> outer
1040 // RowMajor: row(outer,i) -> outer, col(outer,i) -> i
1041 static constexpr Index row(Index outer, Index inner) { return IsRowMajor ? outer : inner; }
1042 static constexpr Index col(Index outer, Index inner) { return IsRowMajor ? inner : outer; }
1043
1044 // Iterates in outer/inner order matching the storage layout for cache friendliness.
1045 // Unlike the old code (which always iterated outer=col, inner=row), this gives
1046 // contiguous memory access for both ColMajor and RowMajor storage.
1047 // Simple scalar loops allow GCC to recognize memcpy/memset idioms and Clang to auto-vectorize.
1048 // Uses a single running index 'i' per column (not separate loop variables) so the compiler
1049 // can track the continuous progression and optimize register allocation.
1050 EIGEN_DEVICE_FUNC static inline void run(Kernel& kernel) {
1051 const Index outerSize = IsRowMajor ? kernel.rows() : kernel.cols();
1052 const Index innerSize = IsRowMajor ? kernel.cols() : kernel.rows();
1053
1054 for (Index outer = 0; outer < outerSize; ++outer) {
1055 const Index maxi = numext::mini(outer, innerSize);
1056 Index i = 0;
1057
1058 EIGEN_IF_CONSTEXPR (ActiveBeforeDiag) {
1059 for (; i < maxi; ++i) kernel.assignCoeff(row(outer, i), col(outer, i));
1060 } else EIGEN_IF_CONSTEXPR (SetOpposite) {
1061 for (; i < maxi; ++i) kernel.assignOppositeCoeff(row(outer, i), col(outer, i));
1062 } else {
1063 i = maxi;
1064 }
1065
1066 if (i < innerSize) kernel.assignDiagonalCoeff(i++);
1067
1068 EIGEN_IF_CONSTEXPR (!ActiveBeforeDiag) {
1069 for (; i < innerSize; ++i) kernel.assignCoeff(row(outer, i), col(outer, i));
1070 } else EIGEN_IF_CONSTEXPR (SetOpposite) {
1071 for (; i < innerSize; ++i) kernel.assignOppositeCoeff(row(outer, i), col(outer, i));
1072 }
1073 }
1074 }
1075};
1076
1077} // end namespace internal
1078
1081template <typename Derived>
1082template <typename DenseDerived>
1084 other.derived().resize(this->rows(), this->cols());
1085 internal::call_triangular_assignment_loop<Derived::Mode,
1086 (int(Derived::Mode) & int(SelfAdjoint)) == 0 /* SetOpposite */>(
1087 other.derived(), derived().nestedExpression());
1088}
1089
1090namespace internal {
1091
1092template <bool UseTriangularAssignmentLoop>
1093struct triangular_product_assignment_dispatcher {
1094 template <typename DstXprType, typename SrcXprType, typename Functor, typename Scalar>
1095 static void run(DstXprType& dst, const SrcXprType& src, const Functor&, const Scalar& alpha, bool beta) {
1096 if (!beta) {
1097 Index dstRows = src.rows();
1098 Index dstCols = src.cols();
1099 if ((dst.rows() != dstRows) || (dst.cols() != dstCols)) dst.resize(dstRows, dstCols);
1100 }
1101
1102 dst._assignProduct(src, alpha, beta);
1103 }
1104};
1105
1106// Underlying-storage data pointer for the diagonal operand of a structured x diagonal
1107// product, or nullptr for non-diagonal operands. The structured (triangular/selfadjoint)
1108// operand can safely share storage with dst because the kernel reads each (row, col) cell
1109// before writing it; only diagonal/dst overlap can corrupt later reads via the diagonal
1110// entries that have already been written.
1111template <typename Op>
1112EIGEN_DEVICE_FUNC inline const void* diagonal_operand_data(const Op& op, DiagonalShape) {
1113 return extract_data(op.diagonal());
1114}
1115template <typename Op, typename Shape>
1116EIGEN_DEVICE_FUNC inline const void* diagonal_operand_data(const Op& /*op*/, Shape) {
1117 return nullptr;
1118}
1119
1120template <typename DstXprType, typename SrcXprType>
1121EIGEN_DEVICE_FUNC inline bool structured_diagonal_product_aliases(const DstXprType& dst, const SrcXprType& src) {
1122 const void* dst_data = dst.nestedExpression().data();
1123 if (dst_data == nullptr) return false;
1124 const void* lhs_diag_data =
1125 diagonal_operand_data(src.lhs(), typename evaluator_traits<typename SrcXprType::Lhs>::Shape{});
1126 const void* rhs_diag_data =
1127 diagonal_operand_data(src.rhs(), typename evaluator_traits<typename SrcXprType::Rhs>::Shape{});
1128 return (lhs_diag_data != nullptr && lhs_diag_data == dst_data) ||
1129 (rhs_diag_data != nullptr && rhs_diag_data == dst_data);
1130}
1131
1132template <>
1133struct triangular_product_assignment_dispatcher<true> {
1134 template <typename DstXprType, typename SrcXprType, typename Functor, typename Scalar>
1135 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run(DstXprType& dst, const SrcXprType& src, const Functor& func,
1136 const Scalar& alpha, bool beta) {
1137 EIGEN_UNUSED_VARIABLE(alpha);
1138 EIGEN_UNUSED_VARIABLE(beta);
1139 EIGEN_STATIC_ASSERT((int(DstXprType::Mode) & int(UnitDiag)) == 0,
1140 WRITING_TO_TRIANGULAR_PART_WITH_UNIT_DIAGONAL_IS_NOT_SUPPORTED);
1141 // The triangular assignment loop reads src.coeff(row, col) lazily while writing
1142 // dst.coeffRef(row, col). When the diagonal operand of the product shares storage with
1143 // dst (e.g.
1144 // A.triangularView<Upper>() = A.diagonal().asDiagonal() * A.triangularView<Upper>())
1145 // the diagonal entries already written in earlier columns would feed back as modified
1146 // values, corrupting later reads. Materialize the source into a temporary first when
1147 // overlap is detected at run time. The structured (triangular/selfadjoint) operand may
1148 // safely alias dst because the kernel reads each cell before writing it.
1149 if (structured_diagonal_product_aliases(dst, src)) {
1150 typename SrcXprType::PlainObject tmp(src);
1151 call_triangular_assignment_loop<DstXprType::Mode, false>(dst, tmp, func);
1152 } else {
1153 call_triangular_assignment_loop<DstXprType::Mode, false>(dst, src, func);
1154 }
1155 }
1156};
1157
1158// Triangular = Product
1159template <typename DstXprType, typename Lhs, typename Rhs, typename Scalar>
1160struct Assignment<DstXprType, Product<Lhs, Rhs, DefaultProduct>,
1161 internal::assign_op<Scalar, typename Product<Lhs, Rhs, DefaultProduct>::Scalar>, Dense2Triangular> {
1162 using SrcXprType = Product<Lhs, Rhs, DefaultProduct>;
1163 static void run(DstXprType& dst, const SrcXprType& src,
1164 const internal::assign_op<Scalar, typename SrcXprType::Scalar>& func) {
1165 enum { UseTriangularAssignmentLoop = is_dense_structured_diagonal_product<Lhs, Rhs>::value };
1166 triangular_product_assignment_dispatcher<UseTriangularAssignmentLoop>::run(dst, src, func, Scalar(1), false);
1167 }
1168};
1169
1170// Triangular += Product
1171template <typename DstXprType, typename Lhs, typename Rhs, typename Scalar>
1172struct Assignment<DstXprType, Product<Lhs, Rhs, DefaultProduct>,
1173 internal::add_assign_op<Scalar, typename Product<Lhs, Rhs, DefaultProduct>::Scalar>,
1174 Dense2Triangular> {
1175 using SrcXprType = Product<Lhs, Rhs, DefaultProduct>;
1176 static void run(DstXprType& dst, const SrcXprType& src,
1177 const internal::add_assign_op<Scalar, typename SrcXprType::Scalar>& func) {
1178 enum { UseTriangularAssignmentLoop = is_dense_structured_diagonal_product<Lhs, Rhs>::value };
1179 triangular_product_assignment_dispatcher<UseTriangularAssignmentLoop>::run(dst, src, func, Scalar(1), true);
1180 }
1181};
1182
1183// Triangular -= Product
1184template <typename DstXprType, typename Lhs, typename Rhs, typename Scalar>
1185struct Assignment<DstXprType, Product<Lhs, Rhs, DefaultProduct>,
1186 internal::sub_assign_op<Scalar, typename Product<Lhs, Rhs, DefaultProduct>::Scalar>,
1187 Dense2Triangular> {
1188 using SrcXprType = Product<Lhs, Rhs, DefaultProduct>;
1189 static void run(DstXprType& dst, const SrcXprType& src,
1190 const internal::sub_assign_op<Scalar, typename SrcXprType::Scalar>& func) {
1191 enum { UseTriangularAssignmentLoop = is_dense_structured_diagonal_product<Lhs, Rhs>::value };
1192 triangular_product_assignment_dispatcher<UseTriangularAssignmentLoop>::run(dst, src, func, Scalar(-1), true);
1193 }
1194};
1195
1196} // end namespace internal
1197
1198} // end namespace Eigen
1199
1200#endif // EIGEN_TRIANGULARMATRIX_H
Base class for all dense matrices, vectors, and arrays.
Definition DenseBase.h:45
void resize(Index newSize)
Definition DenseBase.h:228
Base class for diagonal matrices and expressions.
Definition DiagonalMatrix.h:34
const Derived & derived() const
Definition DiagonalMatrix.h:60
Base class for all dense matrices, vectors, and expressions.
Definition MatrixBase.h:53
bool isLowerTriangular(const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition TriangularMatrix.h:800
bool isUpperTriangular(const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition TriangularMatrix.h:778
const CwiseBinaryOp< internal::scalar_sum_op< Scalar, typename OtherDerived::Scalar >, const MatrixWrapper< ExpressionType >, const OtherDerived > operator+(const Eigen::MatrixBase< OtherDerived > &other) const
Base class for permutations.
Definition PermutationMatrix.h:92
Expression of the product of two arbitrary matrices or vectors.
Definition Product.h:211
Expression of a selfadjoint matrix from a triangular part of a dense matrix.
Definition SelfAdjointView.h:90
Pseudo expression representing a solving operation.
Definition Solve.h:63
Base class for triangular part in a matrix.
Definition TriangularMatrix.h:68
const Product< Derived, OtherDerived > operator*(const PermutationBase< OtherDerived > &rhs) const
Definition TriangularMatrix.h:298
const Product< Derived, OtherDerived > operator*(const TriangularBase< OtherDerived > &rhs) const
Definition TriangularMatrix.h:224
void copyCoeff(Index row, Index col, Other &other)
Definition TriangularMatrix.h:172
Derived & setConstant(const Scalar &value)
Definition TriangularMatrix.h:123
Scalar & coeffRef(Index row, Index col)
Definition TriangularMatrix.h:163
const AdjointReturnType adjoint() const
Definition TriangularMatrix.h:338
Derived & setOnes()
Definition TriangularMatrix.h:132
@ SizeAtCompileTime
Definition TriangularMatrix.h:79
const Product< Derived, Inverse< OtherDerived > > operator*(const InverseImpl< OtherDerived, PermutationStorage > &rhs) const
Definition TriangularMatrix.h:304
void evalTo(MatrixBase< DenseDerived > &other) const
Definition TriangularMatrix.h:734
Derived & setRandom()
Definition TriangularMatrix.h:135
const ConstTransposeReturnType transpose() const
Definition TriangularMatrix.h:357
auto operator-(const DiagonalBase< OtherDerived > &other) const
Definition TriangularMatrix.h:260
Scalar & operator()(Index row, Index col)
Definition TriangularMatrix.h:185
Derived & setIdentity()
Definition TriangularMatrix.h:141
friend const Product< OtherDerived, Derived > operator*(const PermutationBase< OtherDerived > &lhs, const Derived &rhs)
Definition TriangularMatrix.h:311
Scalar operator()(Index row, Index col) const
Definition TriangularMatrix.h:179
friend auto operator-(const DiagonalBase< OtherDerived > &lhs, const Derived &rhs)
Definition TriangularMatrix.h:276
const ConjugateReturnType conjugate() const
Definition TriangularMatrix.h:324
void evalToLazy(MatrixBase< DenseDerived > &other) const
Definition TriangularMatrix.h:1083
Derived & setZero()
Definition TriangularMatrix.h:129
friend const Product< Inverse< OtherDerived >, Derived > operator*(const InverseImpl< OtherDerived, PermutationStorage > &lhs, const Derived &rhs)
Definition TriangularMatrix.h:318
TransposeReturnType transpose()
Definition TriangularMatrix.h:344
void fill(const Scalar &value)
Definition TriangularMatrix.h:120
std::conditional_t< Cond, ConjugateReturnType, ConstView > conjugateIf() const
Definition TriangularMatrix.h:332
Scalar coeff(Index row, Index col) const
Definition TriangularMatrix.h:154
auto operator+(const DiagonalBase< OtherDerived > &other) const
Definition TriangularMatrix.h:252
TriangularViewType & operator*=(const typename internal::traits< MatrixType >::Scalar &other)
Definition TriangularMatrix.h:543
TriangularViewType & operator+=(const DenseBase< Other > &other)
Definition TriangularMatrix.h:529
TriangularViewType & operator/=(const typename internal::traits< MatrixType >::Scalar &other)
Definition TriangularMatrix.h:547
TriangularViewType & operator-=(const DenseBase< Other > &other)
Definition TriangularMatrix.h:536
TriangularViewType & operator=(const TriangularBase< OtherDerived > &other)
EIGEN_DEPRECATED void swap(MatrixBase< OtherDerived > const &other)
Definition TriangularMatrix.h:666
TriangularViewType & operator=(const MatrixBase< OtherDerived > &other)
const internal::triangular_solve_retval< Side, TriangularViewType, Other > solve(const MatrixBase< Other > &other) const
void solveInPlace(const MatrixBase< OtherDerived > &other) const
void swap(TriangularBase< OtherDerived > &other)
Definition TriangularMatrix.h:653
Expression of a triangular part in a matrix.
Definition TriangularMatrix.h:426
Scalar determinant() const
Definition TriangularMatrix.h:482
constexpr MatrixTypeNestedNonRef & nestedExpression() noexcept
Definition TriangularMatrix.h:456
constexpr const NestedExpression & nestedExpression() const noexcept
Definition TriangularMatrix.h:453
const SelfAdjointView< MatrixTypeNestedNonRef, Mode > selfadjointView() const
Definition TriangularMatrix.h:475
SelfAdjointView< MatrixTypeNestedNonRef, Mode > selfadjointView()
Definition TriangularMatrix.h:469
@ StrictlyLower
Definition Constants.h:224
@ UnitDiag
Definition Constants.h:216
@ StrictlyUpper
Definition Constants.h:226
@ UnitLower
Definition Constants.h:220
@ ZeroDiag
Definition Constants.h:218
@ SelfAdjoint
Definition Constants.h:228
@ UnitUpper
Definition Constants.h:222
@ Lower
Definition Constants.h:212
@ Upper
Definition Constants.h:214
constexpr unsigned int PacketAccessBit
Definition Constants.h:98
constexpr unsigned int DirectAccessBit
Definition Constants.h:160
constexpr unsigned int LinearAccessBit
Definition Constants.h:134
constexpr unsigned int LvalueBit
Definition Constants.h:149
constexpr unsigned int RowMajorBit
Definition Constants.h:71
Definition Constants.h:542
Definition EigenBase.h:34
constexpr Derived & derived()
Definition EigenBase.h:50
Eigen::Index Index
The interface type of indices.
Definition EigenBase.h:44