WPILibC++ 2023.4.3
TriangularSolverVector.h
Go to the documentation of this file.
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2008-2010 Gael Guennebaud <gael.guennebaud@inria.fr>
5//
6// This Source Code Form is subject to the terms of the Mozilla
7// Public License v. 2.0. If a copy of the MPL was not distributed
8// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9
10#ifndef EIGEN_TRIANGULAR_SOLVER_VECTOR_H
11#define EIGEN_TRIANGULAR_SOLVER_VECTOR_H
12
13namespace Eigen {
14
15namespace internal {
16
17template<typename LhsScalar, typename RhsScalar, typename Index, int Mode, bool Conjugate, int StorageOrder>
18struct triangular_solve_vector<LhsScalar, RhsScalar, Index, OnTheRight, Mode, Conjugate, StorageOrder>
19{
20 static void run(Index size, const LhsScalar* _lhs, Index lhsStride, RhsScalar* rhs)
21 {
22 triangular_solve_vector<LhsScalar,RhsScalar,Index,OnTheLeft,
23 ((Mode&Upper)==Upper ? Lower : Upper) | (Mode&UnitDiag),
25 >::run(size, _lhs, lhsStride, rhs);
26 }
27};
28
29// forward and backward substitution, row-major, rhs is a vector
30template<typename LhsScalar, typename RhsScalar, typename Index, int Mode, bool Conjugate>
31struct triangular_solve_vector<LhsScalar, RhsScalar, Index, OnTheLeft, Mode, Conjugate, RowMajor>
32{
33 enum {
34 IsLower = ((Mode&Lower)==Lower)
35 };
36 static void run(Index size, const LhsScalar* _lhs, Index lhsStride, RhsScalar* rhs)
37 {
39 const LhsMap lhs(_lhs,size,size,OuterStride<>(lhsStride));
40
43
44 typename internal::conditional<
47 const LhsMap&>
48 ::type cjLhs(lhs);
49 static const Index PanelWidth = EIGEN_TUNE_TRIANGULAR_PANEL_WIDTH;
50 for(Index pi=IsLower ? 0 : size;
51 IsLower ? pi<size : pi>0;
52 IsLower ? pi+=PanelWidth : pi-=PanelWidth)
53 {
54 Index actualPanelWidth = (std::min)(IsLower ? size - pi : pi, PanelWidth);
55
56 Index r = IsLower ? pi : size - pi; // remaining size
57 if (r > 0)
58 {
59 // let's directly call the low level product function because:
60 // 1 - it is faster to compile
61 // 2 - it is slightly faster at runtime
62 Index startRow = IsLower ? pi : pi-actualPanelWidth;
63 Index startCol = IsLower ? 0 : pi;
64
66 actualPanelWidth, r,
67 LhsMapper(&lhs.coeffRef(startRow,startCol), lhsStride),
68 RhsMapper(rhs + startCol, 1),
69 rhs + startRow, 1,
70 RhsScalar(-1));
71 }
72
73 for(Index k=0; k<actualPanelWidth; ++k)
74 {
75 Index i = IsLower ? pi+k : pi-k-1;
76 Index s = IsLower ? pi : i+1;
77 if (k>0)
78 rhs[i] -= (cjLhs.row(i).segment(s,k).transpose().cwiseProduct(Map<const Matrix<RhsScalar,Dynamic,1> >(rhs+s,k))).sum();
79
80 if((!(Mode & UnitDiag)) && numext::not_equal_strict(rhs[i],RhsScalar(0)))
81 rhs[i] /= cjLhs(i,i);
82 }
83 }
84 }
85};
86
87// forward and backward substitution, column-major, rhs is a vector
88template<typename LhsScalar, typename RhsScalar, typename Index, int Mode, bool Conjugate>
89struct triangular_solve_vector<LhsScalar, RhsScalar, Index, OnTheLeft, Mode, Conjugate, ColMajor>
90{
91 enum {
92 IsLower = ((Mode&Lower)==Lower)
93 };
94 static void run(Index size, const LhsScalar* _lhs, Index lhsStride, RhsScalar* rhs)
95 {
97 const LhsMap lhs(_lhs,size,size,OuterStride<>(lhsStride));
102 const LhsMap&
103 >::type cjLhs(lhs);
104 static const Index PanelWidth = EIGEN_TUNE_TRIANGULAR_PANEL_WIDTH;
105
106 for(Index pi=IsLower ? 0 : size;
107 IsLower ? pi<size : pi>0;
108 IsLower ? pi+=PanelWidth : pi-=PanelWidth)
109 {
110 Index actualPanelWidth = (std::min)(IsLower ? size - pi : pi, PanelWidth);
111 Index startBlock = IsLower ? pi : pi-actualPanelWidth;
112 Index endBlock = IsLower ? pi + actualPanelWidth : 0;
113
114 for(Index k=0; k<actualPanelWidth; ++k)
115 {
116 Index i = IsLower ? pi+k : pi-k-1;
117 if(numext::not_equal_strict(rhs[i],RhsScalar(0)))
118 {
119 if(!(Mode & UnitDiag))
120 rhs[i] /= cjLhs.coeff(i,i);
121
122 Index r = actualPanelWidth - k - 1; // remaining size
123 Index s = IsLower ? i+1 : i-r;
124 if (r>0)
125 Map<Matrix<RhsScalar,Dynamic,1> >(rhs+s,r) -= rhs[i] * cjLhs.col(i).segment(s,r);
126 }
127 }
128 Index r = IsLower ? size - endBlock : startBlock; // remaining size
129 if (r > 0)
130 {
131 // let's directly call the low level product function because:
132 // 1 - it is faster to compile
133 // 2 - it is slightly faster at runtime
135 r, actualPanelWidth,
136 LhsMapper(&lhs.coeffRef(endBlock,startBlock), lhsStride),
137 RhsMapper(rhs+startBlock, 1),
138 rhs+endBlock, 1, RhsScalar(-1));
139 }
140 }
141 }
142};
143
144} // end namespace internal
145
146} // end namespace Eigen
147
148#endif // EIGEN_TRIANGULAR_SOLVER_VECTOR_H
#define EIGEN_TUNE_TRIANGULAR_PANEL_WIDTH
Defines the maximal width of the blocks used in the triangular product and solver for vectors (level ...
Definition: Settings.h:38
Definition: ForwardDeclarations.h:87
Generic expression where a coefficient-wise unary operator is applied to an expression.
Definition: CwiseUnaryOp.h:56
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:96
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:180
Convenience specialization of Stride to specify only an outer stride See class Map for some examples.
Definition: Stride.h:107
Definition: BlasUtil.h:389
type
Definition: core.h:575
@ UnitDiag
Matrix has ones on the diagonal; to be used in combination with Lower or Upper.
Definition: Constants.h:213
@ Lower
View matrix as a lower triangular matrix.
Definition: Constants.h:209
@ Upper
View matrix as an upper triangular matrix.
Definition: Constants.h:211
@ ColMajor
Storage order is column major (see TopicStorageOrders).
Definition: Constants.h:319
@ RowMajor
Storage order is row major (see TopicStorageOrders).
Definition: Constants.h:321
@ OnTheLeft
Apply transformation on the left.
Definition: Constants.h:332
@ OnTheRight
Apply transformation on the right.
Definition: Constants.h:334
constexpr common_t< T1, T2 > min(const T1 x, const T2 y) noexcept
Compile-time pairwise minimum function.
Definition: min.hpp:35
EIGEN_CONSTEXPR Index size(const T &x)
Definition: Meta.h:479
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool not_equal_strict(const X &x, const Y &y)
Definition: Meta.h:798
Namespace containing all symbols from the Eigen library.
Definition: MatrixExponential.h:16
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74
Definition: Eigen_Colamd.h:50
static constexpr const unit_t< PI > pi(1)
Ratio of a circle's circumference to its diameter.
Definition: Meta.h:109
static void run(Index size, const LhsScalar *_lhs, Index lhsStride, RhsScalar *rhs)
Definition: TriangularSolverVector.h:36
static void run(Index size, const LhsScalar *_lhs, Index lhsStride, RhsScalar *rhs)
Definition: TriangularSolverVector.h:94
static void run(Index size, const LhsScalar *_lhs, Index lhsStride, RhsScalar *rhs)
Definition: TriangularSolverVector.h:20
Definition: SolveTriangular.h:20