13 #if !defined(KRATOS_EIGEN_DENSE_COLUMN_PIVOTING_HOUSEHOLDER_QR_H_INCLUDED)
14 #define KRATOS_EIGEN_DENSE_COLUMN_PIVOTING_HOUSEHOLDER_QR_H_INCLUDED
45 template<
class TDenseSpace>
56 typedef typename TDenseSpace::DataType
DataType;
80 return "dense_column_pivoting_householder_qr_decomposition";
86 const std::size_t
m = rInputMatrix.size1();
87 const std::size_t
n = rInputMatrix.size2();
88 KRATOS_ERROR_IF(
m <
n) <<
"Householder QR decomposition requires m >= n. Input matrix size is (" <<
m <<
"," <<
n <<
")." << std::endl;
92 Eigen::Map<EigenMatrix> eigen_input_matrix_map(rInputMatrix.data().begin(),
m,
n);
93 mpColPivHouseholderQR = Kratos::make_unique<Eigen::ColPivHouseholderQR<Eigen::Ref<EigenMatrix>>>(eigen_input_matrix_map);
111 KRATOS_ERROR_IF(!mpColPivHouseholderQR) <<
"QR decomposition not computed yet. Please call 'Compute' before 'Solve'." << std::endl;
114 std::size_t
n = rB.size2();
115 const std::size_t rank =
Rank();
116 if (rX.size1() != rank || rX.size2() !=
n) {
117 rX.resize(rank,
n,
false);
121 Eigen::Map<EigenMatrix> eigen_x_map(rX.data().begin(), rank,
n);
122 Eigen::Map<EigenMatrix> eigen_rhs_map(rB.data().begin(), rB.size1(),
n);
123 eigen_x_map = mpColPivHouseholderQR->solve(eigen_rhs_map);
131 KRATOS_ERROR_IF(!mpColPivHouseholderQR) <<
"QR decomposition not computed yet. Please call 'Compute' before 'Solve'." << std::endl;
134 const std::size_t rank =
Rank();
135 if (rX.size() != rank) {
136 rX.resize(rank,
false);
140 Eigen::Map<EigenMatrix> eigen_x_map(rX.data().begin(), rank, 1);
141 Eigen::Map<EigenMatrix> eigen_rhs_map(
const_cast<VectorType&
>(rB).
data().begin(), rB.size(), 1);
142 eigen_x_map = mpColPivHouseholderQR->solve(eigen_rhs_map);
148 KRATOS_ERROR_IF(!mpColPivHouseholderQR) <<
"QR decomposition not computed yet. Please call 'Compute' before 'MatrixQ'." << std::endl;
151 const std::size_t Q_rows = mpColPivHouseholderQR->householderQ().rows();
152 const std::size_t rank =
Rank();
153 if (rMatrixQ.size1() != Q_rows || rMatrixQ.size2() != rank) {
154 rMatrixQ.resize(Q_rows,rank,
false);
159 Eigen::Map<EigenMatrix> thin_Q(rMatrixQ.data().begin(), Q_rows, rank);
160 thin_Q = EigenMatrix::Identity(Q_rows,rank);
161 thin_Q = mpColPivHouseholderQR->householderQ() * thin_Q;
167 KRATOS_ERROR_IF(!mpColPivHouseholderQR) <<
"QR decomposition not computed yet. Please call 'Compute' before 'MatrixR'." << std::endl;
170 const std::size_t rank =
Rank();
171 if (rMatrixR.size1() != rank || rMatrixR.size2() != rank) {
172 rMatrixR.resize(rank,rank,
false);
177 Eigen::Map<EigenMatrix> matrix_R_map(rMatrixR.data().begin(), rank, rank);
178 matrix_R_map = mpColPivHouseholderQR->matrixR().topLeftCorner(rank, rank).template triangularView<Eigen::Upper>();
184 KRATOS_ERROR_IF(!mpColPivHouseholderQR) <<
"QR decomposition not computed yet. Please call 'Compute' before 'MatrixP'." << std::endl;
187 const auto& r_P = mpColPivHouseholderQR->colsPermutation();
188 const std::size_t
m = r_P.rows();
189 const std::size_t
n = r_P.cols();
192 if (rMatrixP.size1() !=
m || rMatrixP.size2() !=
n) {
193 rMatrixP.resize(
m,
n,
false);
195 Eigen::Map<EigenMatrix> matrix_P_map(rMatrixP.data().begin(),
m,
n);
199 std::size_t
Rank()
const override
202 KRATOS_ERROR_IF(!mpColPivHouseholderQR) <<
"QR decomposition not computed yet. Please call 'Compute' before 'Rank'." << std::endl;
204 return mpColPivHouseholderQR->rank();
209 rOStream <<
"EigenDecomposition <" <<
Name() <<
"> finished.";
242 std::unique_ptr<Eigen::ColPivHouseholderQR<Eigen::Ref<EigenMatrix>>> mpColPivHouseholderQR = std::unique_ptr<Eigen::ColPivHouseholderQR<Eigen::Ref<EigenMatrix>>>(
nullptr);
Definition: dense_qr_decomposition.h:44
Definition: eigen_dense_column_pivoting_householder_qr_decomposition.h:47
void MatrixP(MatrixType &rMatrixP) const override
Definition: eigen_dense_column_pivoting_householder_qr_decomposition.h:181
TDenseSpace::DataType DataType
Definition: eigen_dense_column_pivoting_householder_qr_decomposition.h:56
std::size_t Rank() const override
Rank of the provided array Calculates and returns the rank of the array decomposed with the QR.
Definition: eigen_dense_column_pivoting_householder_qr_decomposition.h:199
static std::string Name()
Definition: eigen_dense_column_pivoting_householder_qr_decomposition.h:78
void Solve(const VectorType &rB, VectorType &rX) const override
Definition: eigen_dense_column_pivoting_householder_qr_decomposition.h:126
TDenseSpace::MatrixType MatrixType
Definition: eigen_dense_column_pivoting_householder_qr_decomposition.h:58
void MatrixQ(MatrixType &rMatrixQ) const override
Definition: eigen_dense_column_pivoting_householder_qr_decomposition.h:145
Kratos::EigenDynamicMatrix< DataType > EigenMatrix
Definition: eigen_dense_column_pivoting_householder_qr_decomposition.h:61
void MatrixR(MatrixType &rMatrixR) const override
Definition: eigen_dense_column_pivoting_householder_qr_decomposition.h:164
Kratos::EigenDynamicVector< DataType > EigenVector
Definition: eigen_dense_column_pivoting_householder_qr_decomposition.h:60
void Compute(MatrixType &rInputMatrix, MatrixType &rMatrixQ, MatrixType &rMatrixR) override
Definition: eigen_dense_column_pivoting_householder_qr_decomposition.h:96
TDenseSpace::VectorType VectorType
Definition: eigen_dense_column_pivoting_householder_qr_decomposition.h:57
void Compute(MatrixType &rInputMatrix) override
Definition: eigen_dense_column_pivoting_householder_qr_decomposition.h:83
EigenDenseColumnPivotingHouseholderQRDecomposition()=default
void Solve(MatrixType &rB, MatrixType &rX) const override
Definition: eigen_dense_column_pivoting_householder_qr_decomposition.h:106
void PrintInfo(std::ostream &rOStream) const override
QR information Outputs the QR class information.
Definition: eigen_dense_column_pivoting_householder_qr_decomposition.h:207
KRATOS_CLASS_POINTER_DEFINITION(EigenDenseColumnPivotingHouseholderQRDecomposition)
Definition of the shared pointer of the class.
#define KRATOS_ERROR_IF(conditional)
Definition: exception.h:162
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
Eigen::Matrix< _Scalar, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > EigenDynamicMatrix
Definition: linear_solvers_define.h:32
Eigen::Matrix< _Scalar, Eigen::Dynamic, 1 > EigenDynamicVector
Definition: linear_solvers_define.h:33
data
Definition: mesh_to_mdpa_converter.py:59
int n
manufactured solution and derivatives (u=0 at z=0 dudz=0 at z=domain_height)
Definition: ode_solve.py:402
int m
Definition: run_marine_rain_substepping.py:8