13 #if !defined(KRATOS_EIGEN_DENSE_HOUSEHOLDER_QR_H_INCLUDED)
14 #define KRATOS_EIGEN_DENSE_HOUSEHOLDER_QR_H_INCLUDED
45 template<
class TDenseSpace>
56 typedef typename TDenseSpace::DataType
DataType;
80 return "dense_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 mpHouseholderQR = Kratos::make_unique<Eigen::HouseholderQR<Eigen::Ref<EigenMatrix>>>(eigen_input_matrix_map);
101 KRATOS_ERROR <<
"Householder QR decomposition does not implement the R matrix return. Call the \'Compute()\' and the \'MatrixQ\' methods sequentially." << std::endl;
109 KRATOS_ERROR_IF(!mpHouseholderQR) <<
"QR decomposition not computed yet. Please call 'Compute' before 'Solve'." << std::endl;
112 std::size_t
n = rB.size2();
113 const std::size_t
k = mpHouseholderQR->matrixQR().cols();
114 if (rX.size1() !=
k || rX.size2() !=
n) {
115 rX.resize(
k,
n,
false);
119 Eigen::Map<EigenMatrix> eigen_x_map(rX.data().begin(),
k,
n);
120 Eigen::Map<EigenMatrix> eigen_rhs_map(rB.data().begin(), rB.size1(),
n);
121 eigen_x_map = mpHouseholderQR->solve(eigen_rhs_map);
129 KRATOS_ERROR_IF(!mpHouseholderQR) <<
"QR decomposition not computed yet. Please call 'Compute' before 'Solve'." << std::endl;
132 const std::size_t
k = mpHouseholderQR->matrixQR().cols();
133 if (rX.size() !=
k) {
138 Eigen::Map<EigenMatrix> eigen_x_map(rX.data().begin(),
k, 1);
139 Eigen::Map<EigenMatrix> eigen_rhs_map(
const_cast<VectorType&
>(rB).
data().begin(), rB.size(), 1);
140 eigen_x_map = mpHouseholderQR->solve(eigen_rhs_map);
146 KRATOS_ERROR_IF(!mpHouseholderQR) <<
"QR decomposition not computed yet. Please call 'Compute' before 'MatrixQ'." << std::endl;
149 const std::size_t Q_rows = mpHouseholderQR->householderQ().rows();
150 const std::size_t
n = mpHouseholderQR->matrixQR().cols();
151 if (rMatrixQ.size1() != Q_rows || rMatrixQ.size2() !=
n) {
152 rMatrixQ.resize(Q_rows,
n,
false);
157 Eigen::Map<EigenMatrix> thin_Q(rMatrixQ.data().begin(), Q_rows,
n);
158 thin_Q = EigenMatrix::Identity(Q_rows,
n);
159 thin_Q = mpHouseholderQR->householderQ() * thin_Q;
165 KRATOS_ERROR_IF(!mpHouseholderQR) <<
"QR decomposition not computed yet. Please call 'Compute' before 'MatrixR'." << std::endl;
168 const std::size_t
n = mpHouseholderQR->matrixQR().cols();
169 if (rMatrixR.size1() !=
n || rMatrixR.size2() !=
n) {
170 rMatrixR.resize(
n,
n,
false);
175 Eigen::Map<EigenMatrix> matrix_R_map(rMatrixR.data().begin(),
n,
n);
176 matrix_R_map = mpHouseholderQR->matrixQR().topLeftCorner(
n,
n).template triangularView<Eigen::Upper>();
181 KRATOS_ERROR <<
"Householder QR decomposition does not implement the P matrix return" << std::endl;
184 std::size_t
Rank()
const override
186 KRATOS_ERROR <<
"Householder QR decomposition is not rank revealing." << std::endl;
191 rOStream <<
"EigenDecomposition <" <<
Name() <<
"> finished.";
224 std::unique_ptr<Eigen::HouseholderQR<Eigen::Ref<EigenMatrix>>> mpHouseholderQR = std::unique_ptr<Eigen::HouseholderQR<Eigen::Ref<EigenMatrix>>>(
nullptr);
Definition: dense_qr_decomposition.h:44
Definition: eigen_dense_householder_qr_decomposition.h:47
void Compute(MatrixType &rInputMatrix) override
Definition: eigen_dense_householder_qr_decomposition.h:83
TDenseSpace::VectorType VectorType
Definition: eigen_dense_householder_qr_decomposition.h:57
void MatrixR(MatrixType &rMatrixR) const override
Definition: eigen_dense_householder_qr_decomposition.h:162
void Compute(MatrixType &rInputMatrix, MatrixType &rMatrixQ, MatrixType &rMatrixR) override
Definition: eigen_dense_householder_qr_decomposition.h:96
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_householder_qr_decomposition.h:184
void MatrixP(MatrixType &rMatrixP) const override
Definition: eigen_dense_householder_qr_decomposition.h:179
static std::string Name()
Definition: eigen_dense_householder_qr_decomposition.h:78
KRATOS_CLASS_POINTER_DEFINITION(EigenDenseHouseholderQRDecomposition)
Definition of the shared pointer of the class.
EigenDenseHouseholderQRDecomposition()=default
TDenseSpace::DataType DataType
Definition: eigen_dense_householder_qr_decomposition.h:56
void Solve(const VectorType &rB, VectorType &rX) const override
Definition: eigen_dense_householder_qr_decomposition.h:124
Kratos::EigenDynamicVector< DataType > EigenVector
Definition: eigen_dense_householder_qr_decomposition.h:60
void Solve(MatrixType &rB, MatrixType &rX) const override
Definition: eigen_dense_householder_qr_decomposition.h:104
void PrintInfo(std::ostream &rOStream) const override
QR information Outputs the QR class information.
Definition: eigen_dense_householder_qr_decomposition.h:189
Kratos::EigenDynamicMatrix< DataType > EigenMatrix
Definition: eigen_dense_householder_qr_decomposition.h:61
TDenseSpace::MatrixType MatrixType
Definition: eigen_dense_householder_qr_decomposition.h:58
void MatrixQ(MatrixType &rMatrixQ) const override
Definition: eigen_dense_householder_qr_decomposition.h:143
#define KRATOS_ERROR
Definition: exception.h:161
#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 k
Definition: quadrature.py:595
int m
Definition: run_marine_rain_substepping.py:8