Eigen  5.0.1
 
Loading...
Searching...
No Matches
ColPivHouseholderQR_LAPACKE.h
1/*
2 Copyright (c) 2011, Intel Corporation. All rights reserved.
3
4 Redistribution and use in source and binary forms, with or without modification,
5 are permitted provided that the following conditions are met:
6
7 * Redistributions of source code must retain the above copyright notice, this
8 list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright notice,
10 this list of conditions and the following disclaimer in the documentation
11 and/or other materials provided with the distribution.
12 * Neither the name of Intel Corporation nor the names of its contributors may
13 be used to endorse or promote products derived from this software without
14 specific prior written permission.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
23 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27 ********************************************************************************
28 * Content : Eigen bindings to LAPACKe
29 * Householder QR decomposition of a matrix with column pivoting based on
30 * LAPACKE_?geqp3 function.
31 ********************************************************************************
32*/
33// SPDX-License-Identifier: BSD-3-Clause
34
35#ifndef EIGEN_COLPIVOTINGHOUSEHOLDERQR_LAPACKE_H
36#define EIGEN_COLPIVOTINGHOUSEHOLDERQR_LAPACKE_H
37
38// IWYU pragma: private
39#include "./InternalHeaderCheck.h"
40
41namespace Eigen {
42
43#if defined(EIGEN_USE_LAPACKE)
44
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,
47 Scalar* tau);
48template <>
49inline lapack_int call_geqp3(int matrix_layout, lapack_int m, lapack_int n, float* a, lapack_int lda, lapack_int* jpvt,
50 float* tau) {
51 return LAPACKE_sgeqp3(matrix_layout, m, n, a, lda, jpvt, tau);
52}
53template <>
54inline lapack_int call_geqp3(int matrix_layout, lapack_int m, lapack_int n, double* a, lapack_int lda, lapack_int* jpvt,
55 double* tau) {
56 return LAPACKE_dgeqp3(matrix_layout, m, n, a, lda, jpvt, tau);
57}
58template <>
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);
62}
63template <>
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);
67}
68
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;
75
76 typedef typename internal::plain_diag_type<MatrixType>::type HCoeffsType;
77 typedef PermutationMatrix<Dynamic, Dynamic, lapack_int> PermutationType;
78
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());
84 nonzero_pivots = 0;
85 maxpivot = RealScalar(0);
86 colsPermutation.resize(qr.cols());
87 colsPermutation.indices().setZero();
88
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());
95
96 lapack_int info = call_geqp3(LapackeStorage, rows, cols, qr_data, lda, perm_data, hCoeffs_data);
97 if (info != 0) return;
98
99 maxpivot = qr.diagonal().cwiseAbs().maxCoeff();
100 hCoeffs.adjointInPlace();
101 // Higham's backward error bound (Theorem 19.4): ||ΔA||₂ ≤ c·min(m,n)·u·||A||₂.
102 // The factor of 4 covers the constant c (typically 3–6 worst-case).
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;
110 }
111
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;
119 }
120};
121
122#define COLPIVQR_LAPACKE_COMPUTEINPLACE(EIGTYPE) \
123 template <> \
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); \
128 }
129
130#define COLPIVQR_LAPACKE_INIT(EIGTYPE) \
131 template <> \
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); \
135 }
136
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>)
142
145typedef Matrix<std::complex<float>, Dynamic, Dynamic, ColMajor> MatrixXcfC;
146typedef Matrix<std::complex<double>, Dynamic, Dynamic, ColMajor> MatrixXcdC;
149typedef Matrix<std::complex<float>, Dynamic, Dynamic, RowMajor> MatrixXcfR;
150typedef Matrix<std::complex<double>, Dynamic, Dynamic, RowMajor> MatrixXcdR;
151
152COLPIVQR_LAPACKE(MatrixXfC)
153COLPIVQR_LAPACKE(MatrixXdC)
154COLPIVQR_LAPACKE(MatrixXcfC)
155COLPIVQR_LAPACKE(MatrixXcdC)
156COLPIVQR_LAPACKE(MatrixXfR)
157COLPIVQR_LAPACKE(MatrixXdR)
158COLPIVQR_LAPACKE(MatrixXcfR)
159COLPIVQR_LAPACKE(MatrixXcdR)
160
161#undef COLPIVQR_LAPACKE
162#undef COLPIVQR_LAPACKE_INIT
163#undef COLPIVQR_LAPACKE_COMPUTEINPLACE
164
165#endif
166} // end namespace Eigen
167
168#endif // EIGEN_COLPIVOTINGHOUSEHOLDERQR_LAPACKE_H
The matrix class, also used for vectors and row-vectors.
Definition Matrix.h:188
@ ColMajor
Definition Constants.h:319
@ RowMajor
Definition Constants.h:321