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 
34 #ifndef EIGEN_COLPIVOTINGHOUSEHOLDERQR_LAPACKE_H
35 #define EIGEN_COLPIVOTINGHOUSEHOLDERQR_LAPACKE_H
36 
37 namespace Eigen {
38 
39 /** \internal Specialization for the data types supported by LAPACKe */
40 
41 #define EIGEN_LAPACKE_QR_COLPIV(EIGTYPE, LAPACKE_TYPE, LAPACKE_PREFIX, EIGCOLROW, LAPACKE_COLROW) \
42 template<> template<typename InputType> inline \
43 ColPivHouseholderQR<Matrix<EIGTYPE, Dynamic, Dynamic, EIGCOLROW, Dynamic, Dynamic> >& \
44 ColPivHouseholderQR<Matrix<EIGTYPE, Dynamic, Dynamic, EIGCOLROW, Dynamic, Dynamic> >::compute( \
45               const EigenBase<InputType>& matrix) \
46 \
47 { \
48   using std::abs; \
49   typedef Matrix<EIGTYPE, Dynamic, Dynamic, EIGCOLROW, Dynamic, Dynamic> MatrixType; \
50   typedef MatrixType::RealScalar RealScalar; \
51   Index rows = matrix.rows();\
52   Index cols = matrix.cols();\
53 \
54   m_qr = matrix;\
55   Index size = m_qr.diagonalSize();\
56   m_hCoeffs.resize(size);\
57 \
58   m_colsTranspositions.resize(cols);\
59   /*Index number_of_transpositions = 0;*/ \
60 \
61   m_nonzero_pivots = 0; \
62   m_maxpivot = RealScalar(0);\
63   m_colsPermutation.resize(cols); \
64   m_colsPermutation.indices().setZero(); \
65 \
66   lapack_int lda = internal::convert_index<lapack_int,Index>(m_qr.outerStride()); \
67   lapack_int matrix_order = LAPACKE_COLROW; \
68   LAPACKE_##LAPACKE_PREFIX##geqp3( matrix_order, internal::convert_index<lapack_int,Index>(rows), internal::convert_index<lapack_int,Index>(cols), \
69                               (LAPACKE_TYPE*)m_qr.data(), lda, (lapack_int*)m_colsPermutation.indices().data(), (LAPACKE_TYPE*)m_hCoeffs.data()); \
70   m_isInitialized = true; \
71   m_maxpivot=m_qr.diagonal().cwiseAbs().maxCoeff(); \
72   m_hCoeffs.adjointInPlace(); \
73   RealScalar premultiplied_threshold = abs(m_maxpivot) * threshold(); \
74   lapack_int *perm = m_colsPermutation.indices().data(); \
75   for(Index i=0;i<size;i++) { \
76     m_nonzero_pivots += (abs(m_qr.coeff(i,i)) > premultiplied_threshold);\
77   } \
78   for(Index i=0;i<cols;i++) perm[i]--;\
79 \
80   /*m_det_pq = (number_of_transpositions%2) ? -1 : 1;  // TODO: It's not needed now; fix upon availability in Eigen */ \
81 \
82   return *this; \
83 }
84 
85 EIGEN_LAPACKE_QR_COLPIV(double,   double,        d, ColMajor, LAPACK_COL_MAJOR)
86 EIGEN_LAPACKE_QR_COLPIV(float,    float,         s, ColMajor, LAPACK_COL_MAJOR)
87 EIGEN_LAPACKE_QR_COLPIV(dcomplex, lapack_complex_double, z, ColMajor, LAPACK_COL_MAJOR)
88 EIGEN_LAPACKE_QR_COLPIV(scomplex, lapack_complex_float,  c, ColMajor, LAPACK_COL_MAJOR)
89 
90 EIGEN_LAPACKE_QR_COLPIV(double,   double,        d, RowMajor, LAPACK_ROW_MAJOR)
91 EIGEN_LAPACKE_QR_COLPIV(float,    float,         s, RowMajor, LAPACK_ROW_MAJOR)
92 EIGEN_LAPACKE_QR_COLPIV(dcomplex, lapack_complex_double, z, RowMajor, LAPACK_ROW_MAJOR)
93 EIGEN_LAPACKE_QR_COLPIV(scomplex, lapack_complex_float,  c, RowMajor, LAPACK_ROW_MAJOR)
94 
95 } // end namespace Eigen
96 
97 #endif // EIGEN_COLPIVOTINGHOUSEHOLDERQR_LAPACKE_H
98