35#ifndef EIGEN_COLPIVOTINGHOUSEHOLDERQR_LAPACKE_H
36#define EIGEN_COLPIVOTINGHOUSEHOLDERQR_LAPACKE_H
39#include "./InternalHeaderCheck.h"
43#if defined(EIGEN_USE_LAPACKE)
45template <
typename Scalar>
46inline lapack_int call_geqp3(
int matrix_layout, lapack_int m, lapack_int n, Scalar* a, lapack_int lda, lapack_int* jpvt,
49inline lapack_int call_geqp3(
int matrix_layout, lapack_int m, lapack_int n,
float* a, lapack_int lda, lapack_int* jpvt,
51 return LAPACKE_sgeqp3(matrix_layout, m, n, a, lda, jpvt, tau);
54inline lapack_int call_geqp3(
int matrix_layout, lapack_int m, lapack_int n,
double* a, lapack_int lda, lapack_int* jpvt,
56 return LAPACKE_dgeqp3(matrix_layout, m, n, a, lda, jpvt, tau);
59inline lapack_int call_geqp3(
int matrix_layout, lapack_int m, lapack_int n, lapack_complex_float* a, lapack_int lda,
60 lapack_int* jpvt, lapack_complex_float* tau) {
61 return LAPACKE_cgeqp3(matrix_layout, m, n, a, lda, jpvt, tau);
64inline lapack_int call_geqp3(
int matrix_layout, lapack_int m, lapack_int n, lapack_complex_double* a, lapack_int lda,
65 lapack_int* jpvt, lapack_complex_double* tau) {
66 return LAPACKE_zgeqp3(matrix_layout, m, n, a, lda, jpvt, tau);
69template <
typename MatrixType>
70struct ColPivHouseholderQR_LAPACKE_impl {
71 typedef typename MatrixType::Scalar Scalar;
72 typedef typename MatrixType::RealScalar RealScalar;
73 typedef typename internal::lapacke_helpers::translate_type_imp<Scalar>::type LapackeType;
74 static constexpr int LapackeStorage = MatrixType::IsRowMajor ? LAPACK_ROW_MAJOR : LAPACK_COL_MAJOR;
76 typedef typename internal::plain_diag_type<MatrixType>::type HCoeffsType;
77 typedef PermutationMatrix<Dynamic, Dynamic, lapack_int> PermutationType;
79 static void run(MatrixType& qr, HCoeffsType& hCoeffs, PermutationType& colsPermutation, Index& nonzero_pivots,
80 RealScalar& maxpivot,
bool usePrescribedThreshold, RealScalar prescribedThreshold, Index& det_p,
81 bool& isInitialized) {
82 isInitialized =
false;
83 hCoeffs.resize(qr.diagonalSize());
85 maxpivot = RealScalar(0);
86 colsPermutation.resize(qr.cols());
87 colsPermutation.indices().setZero();
89 lapack_int rows = internal::lapacke_helpers::to_lapack(qr.rows());
90 lapack_int cols = internal::lapacke_helpers::to_lapack(qr.cols());
91 LapackeType* qr_data = (LapackeType*)(qr.data());
92 lapack_int lda = internal::lapacke_helpers::to_lapack(qr.outerStride());
93 lapack_int* perm_data = colsPermutation.indices().data();
94 LapackeType* hCoeffs_data = (LapackeType*)(hCoeffs.data());
96 lapack_int info = call_geqp3(LapackeStorage, rows, cols, qr_data, lda, perm_data, hCoeffs_data);
97 if (info != 0)
return;
99 maxpivot = qr.diagonal().cwiseAbs().maxCoeff();
100 hCoeffs.adjointInPlace();
103 RealScalar defaultThreshold = NumTraits<RealScalar>::epsilon() * RealScalar(4 * qr.diagonalSize());
104 RealScalar threshold = usePrescribedThreshold ? prescribedThreshold : defaultThreshold;
105 RealScalar premultiplied_threshold = maxpivot * threshold;
106 nonzero_pivots = (qr.diagonal().cwiseAbs().array() > premultiplied_threshold).count();
107 colsPermutation.indices().array() -= 1;
108 det_p = colsPermutation.determinant();
109 isInitialized =
true;
112 static void init(Index rows, Index cols, HCoeffsType& hCoeffs, PermutationType& colsPermutation,
113 bool& usePrescribedThreshold,
bool& isInitialized) {
114 Index diag = numext::mini(rows, cols);
115 hCoeffs.resize(diag);
116 colsPermutation.resize(cols);
117 usePrescribedThreshold =
false;
118 isInitialized =
false;
122#define COLPIVQR_LAPACKE_COMPUTEINPLACE(EIGTYPE) \
124 inline void ColPivHouseholderQR<EIGTYPE, lapack_int>::computeInPlace() { \
125 ColPivHouseholderQR_LAPACKE_impl<MatrixType>::run(m_qr, m_hCoeffs, m_colsPermutation, m_nonzero_pivots, \
126 m_maxpivot, m_usePrescribedThreshold, m_prescribedThreshold, \
127 m_det_p, m_isInitialized); \
130#define COLPIVQR_LAPACKE_INIT(EIGTYPE) \
132 inline void ColPivHouseholderQR<EIGTYPE, lapack_int>::init(Index rows, Index cols) { \
133 ColPivHouseholderQR_LAPACKE_impl<MatrixType>::init(rows, cols, m_hCoeffs, m_colsPermutation, m_isInitialized, \
134 m_usePrescribedThreshold); \
137#define COLPIVQR_LAPACKE(EIGTYPE) \
138 COLPIVQR_LAPACKE_COMPUTEINPLACE(EIGTYPE) \
139 COLPIVQR_LAPACKE_INIT(EIGTYPE) \
140 COLPIVQR_LAPACKE_COMPUTEINPLACE(Ref<EIGTYPE>) \
141 COLPIVQR_LAPACKE_INIT(Ref<EIGTYPE>)
152COLPIVQR_LAPACKE(MatrixXfC)
153COLPIVQR_LAPACKE(MatrixXdC)
154COLPIVQR_LAPACKE(MatrixXcfC)
155COLPIVQR_LAPACKE(MatrixXcdC)
156COLPIVQR_LAPACKE(MatrixXfR)
157COLPIVQR_LAPACKE(MatrixXdR)
158COLPIVQR_LAPACKE(MatrixXcfR)
159COLPIVQR_LAPACKE(MatrixXcdR)
161#undef COLPIVQR_LAPACKE
162#undef COLPIVQR_LAPACKE_INIT
163#undef COLPIVQR_LAPACKE_COMPUTEINPLACE
The matrix class, also used for vectors and row-vectors.
Definition Matrix.h:188
@ ColMajor
Definition Constants.h:319
@ RowMajor
Definition Constants.h:321