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 using Scalar =
typename MatrixType::Scalar;
72 using RealScalar =
typename MatrixType::RealScalar;
73 using LapackeType =
typename internal::lapacke_helpers::translate_type_imp<Scalar>::type;
74 static constexpr int LapackeStorage = MatrixType::IsRowMajor ? LAPACK_ROW_MAJOR : LAPACK_COL_MAJOR;
76 using HCoeffsType =
typename internal::plain_diag_type<MatrixType>::type;
77 using PermutationType = PermutationMatrix<Dynamic, Dynamic, lapack_int>;
81 static bool run(MatrixType& qr, HCoeffsType& hCoeffs, PermutationType& colsPermutation, Index& nonzero_pivots,
82 RealScalar& maxpivot,
bool usePrescribedThreshold, RealScalar prescribedThreshold, Index& det_p,
83 bool& isInitialized) {
84 isInitialized =
false;
85 hCoeffs.resize(qr.diagonalSize());
87 maxpivot = RealScalar(0);
88 colsPermutation.resize(qr.cols());
89 colsPermutation.indices().setZero();
91 lapack_int rows = internal::lapacke_helpers::to_lapack(qr.rows());
92 lapack_int cols = internal::lapacke_helpers::to_lapack(qr.cols());
93 LapackeType* qr_data = (LapackeType*)(qr.data());
94 lapack_int lda = internal::lapacke_helpers::to_lapack(qr.outerStride());
95 lapack_int* perm_data = colsPermutation.indices().data();
96 LapackeType* hCoeffs_data = (LapackeType*)(hCoeffs.data());
98 lapack_int info = call_geqp3(LapackeStorage, rows, cols, qr_data, lda, perm_data, hCoeffs_data);
99 if (info == -4)
return false;
100 if (info != 0)
return true;
102 maxpivot = qr.diagonal().cwiseAbs().maxCoeff();
103 hCoeffs.adjointInPlace();
106 RealScalar defaultThreshold = NumTraits<RealScalar>::epsilon() * RealScalar(4 * qr.diagonalSize());
107 RealScalar threshold = usePrescribedThreshold ? prescribedThreshold : defaultThreshold;
108 RealScalar premultiplied_threshold = maxpivot * threshold;
109 nonzero_pivots = (qr.diagonal().cwiseAbs().array() > premultiplied_threshold).count();
110 colsPermutation.indices().array() -= 1;
111 det_p = colsPermutation.determinant();
112 isInitialized =
true;
116 static void init(Index rows, Index cols, HCoeffsType& hCoeffs, PermutationType& colsPermutation,
117 bool& usePrescribedThreshold,
bool& isInitialized) {
118 Index diag = numext::mini(rows, cols);
119 hCoeffs.resize(diag);
120 colsPermutation.resize(cols);
121 usePrescribedThreshold =
false;
122 isInitialized =
false;
126#define COLPIVQR_LAPACKE_COMPUTEINPLACE(EIGTYPE) \
128 inline void ColPivHouseholderQR<EIGTYPE, lapack_int>::computeInPlace() { \
129 if (!ColPivHouseholderQR_LAPACKE_impl<MatrixType>::run(m_qr, m_hCoeffs, m_colsPermutation, m_nonzero_pivots, \
130 m_maxpivot, m_usePrescribedThreshold, \
131 m_prescribedThreshold, m_det_p, m_isInitialized)) \
132 computeInPlaceNative(); \
135#define COLPIVQR_LAPACKE_INIT(EIGTYPE) \
137 inline void ColPivHouseholderQR<EIGTYPE, lapack_int>::init(Index rows, Index cols) { \
138 ColPivHouseholderQR_LAPACKE_impl<MatrixType>::init(rows, cols, m_hCoeffs, m_colsPermutation, m_isInitialized, \
139 m_usePrescribedThreshold); \
142#define COLPIVQR_LAPACKE(EIGTYPE) \
143 COLPIVQR_LAPACKE_COMPUTEINPLACE(EIGTYPE) \
144 COLPIVQR_LAPACKE_INIT(EIGTYPE) \
145 COLPIVQR_LAPACKE_COMPUTEINPLACE(Ref<EIGTYPE>) \
146 COLPIVQR_LAPACKE_INIT(Ref<EIGTYPE>)
157COLPIVQR_LAPACKE(MatrixXfC)
158COLPIVQR_LAPACKE(MatrixXdC)
159COLPIVQR_LAPACKE(MatrixXcfC)
160COLPIVQR_LAPACKE(MatrixXcdC)
161COLPIVQR_LAPACKE(MatrixXfR)
162COLPIVQR_LAPACKE(MatrixXdR)
163COLPIVQR_LAPACKE(MatrixXcfR)
164COLPIVQR_LAPACKE(MatrixXcdR)
166#undef COLPIVQR_LAPACKE
167#undef COLPIVQR_LAPACKE_INIT
168#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