Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/eigen3/Eigen/src/Eigenvalues/Tridiagonalization.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // This file is part of Eigen, a lightweight C++ template library
0002 // for linear algebra.
0003 //
0004 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
0005 // Copyright (C) 2010 Jitse Niesen <jitse@maths.leeds.ac.uk>
0006 //
0007 // This Source Code Form is subject to the terms of the Mozilla
0008 // Public License v. 2.0. If a copy of the MPL was not distributed
0009 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
0010 
0011 #ifndef EIGEN_TRIDIAGONALIZATION_H
0012 #define EIGEN_TRIDIAGONALIZATION_H
0013 
0014 namespace Eigen {
0015 
0016 namespace internal {
0017 
0018 template<typename MatrixType> struct TridiagonalizationMatrixTReturnType;
0019 template<typename MatrixType>
0020 struct traits<TridiagonalizationMatrixTReturnType<MatrixType> >
0021   : public traits<typename MatrixType::PlainObject>
0022 {
0023   typedef typename MatrixType::PlainObject ReturnType; // FIXME shall it be a BandMatrix?
0024   enum { Flags = 0 };
0025 };
0026 
0027 template<typename MatrixType, typename CoeffVectorType>
0028 EIGEN_DEVICE_FUNC
0029 void tridiagonalization_inplace(MatrixType& matA, CoeffVectorType& hCoeffs);
0030 }
0031 
0032 /** \eigenvalues_module \ingroup Eigenvalues_Module
0033   *
0034   *
0035   * \class Tridiagonalization
0036   *
0037   * \brief Tridiagonal decomposition of a selfadjoint matrix
0038   *
0039   * \tparam _MatrixType the type of the matrix of which we are computing the
0040   * tridiagonal decomposition; this is expected to be an instantiation of the
0041   * Matrix class template.
0042   *
0043   * This class performs a tridiagonal decomposition of a selfadjoint matrix \f$ A \f$ such that:
0044   * \f$ A = Q T Q^* \f$ where \f$ Q \f$ is unitary and \f$ T \f$ a real symmetric tridiagonal matrix.
0045   *
0046   * A tridiagonal matrix is a matrix which has nonzero elements only on the
0047   * main diagonal and the first diagonal below and above it. The Hessenberg
0048   * decomposition of a selfadjoint matrix is in fact a tridiagonal
0049   * decomposition. This class is used in SelfAdjointEigenSolver to compute the
0050   * eigenvalues and eigenvectors of a selfadjoint matrix.
0051   *
0052   * Call the function compute() to compute the tridiagonal decomposition of a
0053   * given matrix. Alternatively, you can use the Tridiagonalization(const MatrixType&)
0054   * constructor which computes the tridiagonal Schur decomposition at
0055   * construction time. Once the decomposition is computed, you can use the
0056   * matrixQ() and matrixT() functions to retrieve the matrices Q and T in the
0057   * decomposition.
0058   *
0059   * The documentation of Tridiagonalization(const MatrixType&) contains an
0060   * example of the typical use of this class.
0061   *
0062   * \sa class HessenbergDecomposition, class SelfAdjointEigenSolver
0063   */
0064 template<typename _MatrixType> class Tridiagonalization
0065 {
0066   public:
0067 
0068     /** \brief Synonym for the template parameter \p _MatrixType. */
0069     typedef _MatrixType MatrixType;
0070 
0071     typedef typename MatrixType::Scalar Scalar;
0072     typedef typename NumTraits<Scalar>::Real RealScalar;
0073     typedef Eigen::Index Index; ///< \deprecated since Eigen 3.3
0074 
0075     enum {
0076       Size = MatrixType::RowsAtCompileTime,
0077       SizeMinusOne = Size == Dynamic ? Dynamic : (Size > 1 ? Size - 1 : 1),
0078       Options = MatrixType::Options,
0079       MaxSize = MatrixType::MaxRowsAtCompileTime,
0080       MaxSizeMinusOne = MaxSize == Dynamic ? Dynamic : (MaxSize > 1 ? MaxSize - 1 : 1)
0081     };
0082 
0083     typedef Matrix<Scalar, SizeMinusOne, 1, Options & ~RowMajor, MaxSizeMinusOne, 1> CoeffVectorType;
0084     typedef typename internal::plain_col_type<MatrixType, RealScalar>::type DiagonalType;
0085     typedef Matrix<RealScalar, SizeMinusOne, 1, Options & ~RowMajor, MaxSizeMinusOne, 1> SubDiagonalType;
0086     typedef typename internal::remove_all<typename MatrixType::RealReturnType>::type MatrixTypeRealView;
0087     typedef internal::TridiagonalizationMatrixTReturnType<MatrixTypeRealView> MatrixTReturnType;
0088 
0089     typedef typename internal::conditional<NumTraits<Scalar>::IsComplex,
0090               typename internal::add_const_on_value_type<typename Diagonal<const MatrixType>::RealReturnType>::type,
0091               const Diagonal<const MatrixType>
0092             >::type DiagonalReturnType;
0093 
0094     typedef typename internal::conditional<NumTraits<Scalar>::IsComplex,
0095               typename internal::add_const_on_value_type<typename Diagonal<const MatrixType, -1>::RealReturnType>::type,
0096               const Diagonal<const MatrixType, -1>
0097             >::type SubDiagonalReturnType;
0098 
0099     /** \brief Return type of matrixQ() */
0100     typedef HouseholderSequence<MatrixType,typename internal::remove_all<typename CoeffVectorType::ConjugateReturnType>::type> HouseholderSequenceType;
0101 
0102     /** \brief Default constructor.
0103       *
0104       * \param [in]  size  Positive integer, size of the matrix whose tridiagonal
0105       * decomposition will be computed.
0106       *
0107       * The default constructor is useful in cases in which the user intends to
0108       * perform decompositions via compute().  The \p size parameter is only
0109       * used as a hint. It is not an error to give a wrong \p size, but it may
0110       * impair performance.
0111       *
0112       * \sa compute() for an example.
0113       */
0114     explicit Tridiagonalization(Index size = Size==Dynamic ? 2 : Size)
0115       : m_matrix(size,size),
0116         m_hCoeffs(size > 1 ? size-1 : 1),
0117         m_isInitialized(false)
0118     {}
0119 
0120     /** \brief Constructor; computes tridiagonal decomposition of given matrix.
0121       *
0122       * \param[in]  matrix  Selfadjoint matrix whose tridiagonal decomposition
0123       * is to be computed.
0124       *
0125       * This constructor calls compute() to compute the tridiagonal decomposition.
0126       *
0127       * Example: \include Tridiagonalization_Tridiagonalization_MatrixType.cpp
0128       * Output: \verbinclude Tridiagonalization_Tridiagonalization_MatrixType.out
0129       */
0130     template<typename InputType>
0131     explicit Tridiagonalization(const EigenBase<InputType>& matrix)
0132       : m_matrix(matrix.derived()),
0133         m_hCoeffs(matrix.cols() > 1 ? matrix.cols()-1 : 1),
0134         m_isInitialized(false)
0135     {
0136       internal::tridiagonalization_inplace(m_matrix, m_hCoeffs);
0137       m_isInitialized = true;
0138     }
0139 
0140     /** \brief Computes tridiagonal decomposition of given matrix.
0141       *
0142       * \param[in]  matrix  Selfadjoint matrix whose tridiagonal decomposition
0143       * is to be computed.
0144       * \returns    Reference to \c *this
0145       *
0146       * The tridiagonal decomposition is computed by bringing the columns of
0147       * the matrix successively in the required form using Householder
0148       * reflections. The cost is \f$ 4n^3/3 \f$ flops, where \f$ n \f$ denotes
0149       * the size of the given matrix.
0150       *
0151       * This method reuses of the allocated data in the Tridiagonalization
0152       * object, if the size of the matrix does not change.
0153       *
0154       * Example: \include Tridiagonalization_compute.cpp
0155       * Output: \verbinclude Tridiagonalization_compute.out
0156       */
0157     template<typename InputType>
0158     Tridiagonalization& compute(const EigenBase<InputType>& matrix)
0159     {
0160       m_matrix = matrix.derived();
0161       m_hCoeffs.resize(matrix.rows()-1, 1);
0162       internal::tridiagonalization_inplace(m_matrix, m_hCoeffs);
0163       m_isInitialized = true;
0164       return *this;
0165     }
0166 
0167     /** \brief Returns the Householder coefficients.
0168       *
0169       * \returns a const reference to the vector of Householder coefficients
0170       *
0171       * \pre Either the constructor Tridiagonalization(const MatrixType&) or
0172       * the member function compute(const MatrixType&) has been called before
0173       * to compute the tridiagonal decomposition of a matrix.
0174       *
0175       * The Householder coefficients allow the reconstruction of the matrix
0176       * \f$ Q \f$ in the tridiagonal decomposition from the packed data.
0177       *
0178       * Example: \include Tridiagonalization_householderCoefficients.cpp
0179       * Output: \verbinclude Tridiagonalization_householderCoefficients.out
0180       *
0181       * \sa packedMatrix(), \ref Householder_Module "Householder module"
0182       */
0183     inline CoeffVectorType householderCoefficients() const
0184     {
0185       eigen_assert(m_isInitialized && "Tridiagonalization is not initialized.");
0186       return m_hCoeffs;
0187     }
0188 
0189     /** \brief Returns the internal representation of the decomposition
0190       *
0191       * \returns a const reference to a matrix with the internal representation
0192       *          of the decomposition.
0193       *
0194       * \pre Either the constructor Tridiagonalization(const MatrixType&) or
0195       * the member function compute(const MatrixType&) has been called before
0196       * to compute the tridiagonal decomposition of a matrix.
0197       *
0198       * The returned matrix contains the following information:
0199       *  - the strict upper triangular part is equal to the input matrix A.
0200       *  - the diagonal and lower sub-diagonal represent the real tridiagonal
0201       *    symmetric matrix T.
0202       *  - the rest of the lower part contains the Householder vectors that,
0203       *    combined with Householder coefficients returned by
0204       *    householderCoefficients(), allows to reconstruct the matrix Q as
0205       *       \f$ Q = H_{N-1} \ldots H_1 H_0 \f$.
0206       *    Here, the matrices \f$ H_i \f$ are the Householder transformations
0207       *       \f$ H_i = (I - h_i v_i v_i^T) \f$
0208       *    where \f$ h_i \f$ is the \f$ i \f$th Householder coefficient and
0209       *    \f$ v_i \f$ is the Householder vector defined by
0210       *       \f$ v_i = [ 0, \ldots, 0, 1, M(i+2,i), \ldots, M(N-1,i) ]^T \f$
0211       *    with M the matrix returned by this function.
0212       *
0213       * See LAPACK for further details on this packed storage.
0214       *
0215       * Example: \include Tridiagonalization_packedMatrix.cpp
0216       * Output: \verbinclude Tridiagonalization_packedMatrix.out
0217       *
0218       * \sa householderCoefficients()
0219       */
0220     inline const MatrixType& packedMatrix() const
0221     {
0222       eigen_assert(m_isInitialized && "Tridiagonalization is not initialized.");
0223       return m_matrix;
0224     }
0225 
0226     /** \brief Returns the unitary matrix Q in the decomposition
0227       *
0228       * \returns object representing the matrix Q
0229       *
0230       * \pre Either the constructor Tridiagonalization(const MatrixType&) or
0231       * the member function compute(const MatrixType&) has been called before
0232       * to compute the tridiagonal decomposition of a matrix.
0233       *
0234       * This function returns a light-weight object of template class
0235       * HouseholderSequence. You can either apply it directly to a matrix or
0236       * you can convert it to a matrix of type #MatrixType.
0237       *
0238       * \sa Tridiagonalization(const MatrixType&) for an example,
0239       *     matrixT(), class HouseholderSequence
0240       */
0241     HouseholderSequenceType matrixQ() const
0242     {
0243       eigen_assert(m_isInitialized && "Tridiagonalization is not initialized.");
0244       return HouseholderSequenceType(m_matrix, m_hCoeffs.conjugate())
0245              .setLength(m_matrix.rows() - 1)
0246              .setShift(1);
0247     }
0248 
0249     /** \brief Returns an expression of the tridiagonal matrix T in the decomposition
0250       *
0251       * \returns expression object representing the matrix T
0252       *
0253       * \pre Either the constructor Tridiagonalization(const MatrixType&) or
0254       * the member function compute(const MatrixType&) has been called before
0255       * to compute the tridiagonal decomposition of a matrix.
0256       *
0257       * Currently, this function can be used to extract the matrix T from internal
0258       * data and copy it to a dense matrix object. In most cases, it may be
0259       * sufficient to directly use the packed matrix or the vector expressions
0260       * returned by diagonal() and subDiagonal() instead of creating a new
0261       * dense copy matrix with this function.
0262       *
0263       * \sa Tridiagonalization(const MatrixType&) for an example,
0264       * matrixQ(), packedMatrix(), diagonal(), subDiagonal()
0265       */
0266     MatrixTReturnType matrixT() const
0267     {
0268       eigen_assert(m_isInitialized && "Tridiagonalization is not initialized.");
0269       return MatrixTReturnType(m_matrix.real());
0270     }
0271 
0272     /** \brief Returns the diagonal of the tridiagonal matrix T in the decomposition.
0273       *
0274       * \returns expression representing the diagonal of T
0275       *
0276       * \pre Either the constructor Tridiagonalization(const MatrixType&) or
0277       * the member function compute(const MatrixType&) has been called before
0278       * to compute the tridiagonal decomposition of a matrix.
0279       *
0280       * Example: \include Tridiagonalization_diagonal.cpp
0281       * Output: \verbinclude Tridiagonalization_diagonal.out
0282       *
0283       * \sa matrixT(), subDiagonal()
0284       */
0285     DiagonalReturnType diagonal() const;
0286 
0287     /** \brief Returns the subdiagonal of the tridiagonal matrix T in the decomposition.
0288       *
0289       * \returns expression representing the subdiagonal of T
0290       *
0291       * \pre Either the constructor Tridiagonalization(const MatrixType&) or
0292       * the member function compute(const MatrixType&) has been called before
0293       * to compute the tridiagonal decomposition of a matrix.
0294       *
0295       * \sa diagonal() for an example, matrixT()
0296       */
0297     SubDiagonalReturnType subDiagonal() const;
0298 
0299   protected:
0300 
0301     MatrixType m_matrix;
0302     CoeffVectorType m_hCoeffs;
0303     bool m_isInitialized;
0304 };
0305 
0306 template<typename MatrixType>
0307 typename Tridiagonalization<MatrixType>::DiagonalReturnType
0308 Tridiagonalization<MatrixType>::diagonal() const
0309 {
0310   eigen_assert(m_isInitialized && "Tridiagonalization is not initialized.");
0311   return m_matrix.diagonal().real();
0312 }
0313 
0314 template<typename MatrixType>
0315 typename Tridiagonalization<MatrixType>::SubDiagonalReturnType
0316 Tridiagonalization<MatrixType>::subDiagonal() const
0317 {
0318   eigen_assert(m_isInitialized && "Tridiagonalization is not initialized.");
0319   return m_matrix.template diagonal<-1>().real();
0320 }
0321 
0322 namespace internal {
0323 
0324 /** \internal
0325   * Performs a tridiagonal decomposition of the selfadjoint matrix \a matA in-place.
0326   *
0327   * \param[in,out] matA On input the selfadjoint matrix. Only the \b lower triangular part is referenced.
0328   *                     On output, the strict upper part is left unchanged, and the lower triangular part
0329   *                     represents the T and Q matrices in packed format has detailed below.
0330   * \param[out]    hCoeffs returned Householder coefficients (see below)
0331   *
0332   * On output, the tridiagonal selfadjoint matrix T is stored in the diagonal
0333   * and lower sub-diagonal of the matrix \a matA.
0334   * The unitary matrix Q is represented in a compact way as a product of
0335   * Householder reflectors \f$ H_i \f$ such that:
0336   *       \f$ Q = H_{N-1} \ldots H_1 H_0 \f$.
0337   * The Householder reflectors are defined as
0338   *       \f$ H_i = (I - h_i v_i v_i^T) \f$
0339   * where \f$ h_i = hCoeffs[i]\f$ is the \f$ i \f$th Householder coefficient and
0340   * \f$ v_i \f$ is the Householder vector defined by
0341   *       \f$ v_i = [ 0, \ldots, 0, 1, matA(i+2,i), \ldots, matA(N-1,i) ]^T \f$.
0342   *
0343   * Implemented from Golub's "Matrix Computations", algorithm 8.3.1.
0344   *
0345   * \sa Tridiagonalization::packedMatrix()
0346   */
0347 template<typename MatrixType, typename CoeffVectorType>
0348 EIGEN_DEVICE_FUNC
0349 void tridiagonalization_inplace(MatrixType& matA, CoeffVectorType& hCoeffs)
0350 {
0351   using numext::conj;
0352   typedef typename MatrixType::Scalar Scalar;
0353   typedef typename MatrixType::RealScalar RealScalar;
0354   Index n = matA.rows();
0355   eigen_assert(n==matA.cols());
0356   eigen_assert(n==hCoeffs.size()+1 || n==1);
0357 
0358   for (Index i = 0; i<n-1; ++i)
0359   {
0360     Index remainingSize = n-i-1;
0361     RealScalar beta;
0362     Scalar h;
0363     matA.col(i).tail(remainingSize).makeHouseholderInPlace(h, beta);
0364 
0365     // Apply similarity transformation to remaining columns,
0366     // i.e., A = H A H' where H = I - h v v' and v = matA.col(i).tail(n-i-1)
0367     matA.col(i).coeffRef(i+1) = 1;
0368 
0369     hCoeffs.tail(n-i-1).noalias() = (matA.bottomRightCorner(remainingSize,remainingSize).template selfadjointView<Lower>()
0370                                   * (conj(h) * matA.col(i).tail(remainingSize)));
0371 
0372     hCoeffs.tail(n-i-1) += (conj(h)*RealScalar(-0.5)*(hCoeffs.tail(remainingSize).dot(matA.col(i).tail(remainingSize)))) * matA.col(i).tail(n-i-1);
0373 
0374     matA.bottomRightCorner(remainingSize, remainingSize).template selfadjointView<Lower>()
0375       .rankUpdate(matA.col(i).tail(remainingSize), hCoeffs.tail(remainingSize), Scalar(-1));
0376 
0377     matA.col(i).coeffRef(i+1) = beta;
0378     hCoeffs.coeffRef(i) = h;
0379   }
0380 }
0381 
0382 // forward declaration, implementation at the end of this file
0383 template<typename MatrixType,
0384          int Size=MatrixType::ColsAtCompileTime,
0385          bool IsComplex=NumTraits<typename MatrixType::Scalar>::IsComplex>
0386 struct tridiagonalization_inplace_selector;
0387 
0388 /** \brief Performs a full tridiagonalization in place
0389   *
0390   * \param[in,out]  mat  On input, the selfadjoint matrix whose tridiagonal
0391   *    decomposition is to be computed. Only the lower triangular part referenced.
0392   *    The rest is left unchanged. On output, the orthogonal matrix Q
0393   *    in the decomposition if \p extractQ is true.
0394   * \param[out]  diag  The diagonal of the tridiagonal matrix T in the
0395   *    decomposition.
0396   * \param[out]  subdiag  The subdiagonal of the tridiagonal matrix T in
0397   *    the decomposition.
0398   * \param[in]  extractQ  If true, the orthogonal matrix Q in the
0399   *    decomposition is computed and stored in \p mat.
0400   *
0401   * Computes the tridiagonal decomposition of the selfadjoint matrix \p mat in place
0402   * such that \f$ mat = Q T Q^* \f$ where \f$ Q \f$ is unitary and \f$ T \f$ a real
0403   * symmetric tridiagonal matrix.
0404   *
0405   * The tridiagonal matrix T is passed to the output parameters \p diag and \p subdiag. If
0406   * \p extractQ is true, then the orthogonal matrix Q is passed to \p mat. Otherwise the lower
0407   * part of the matrix \p mat is destroyed.
0408   *
0409   * The vectors \p diag and \p subdiag are not resized. The function
0410   * assumes that they are already of the correct size. The length of the
0411   * vector \p diag should equal the number of rows in \p mat, and the
0412   * length of the vector \p subdiag should be one left.
0413   *
0414   * This implementation contains an optimized path for 3-by-3 matrices
0415   * which is especially useful for plane fitting.
0416   *
0417   * \note Currently, it requires two temporary vectors to hold the intermediate
0418   * Householder coefficients, and to reconstruct the matrix Q from the Householder
0419   * reflectors.
0420   *
0421   * Example (this uses the same matrix as the example in
0422   *    Tridiagonalization::Tridiagonalization(const MatrixType&)):
0423   *    \include Tridiagonalization_decomposeInPlace.cpp
0424   * Output: \verbinclude Tridiagonalization_decomposeInPlace.out
0425   *
0426   * \sa class Tridiagonalization
0427   */
0428 template<typename MatrixType, typename DiagonalType, typename SubDiagonalType, typename CoeffVectorType>
0429 EIGEN_DEVICE_FUNC
0430 void tridiagonalization_inplace(MatrixType& mat, DiagonalType& diag, SubDiagonalType& subdiag,
0431                                 CoeffVectorType& hcoeffs, bool extractQ)
0432 {
0433   eigen_assert(mat.cols()==mat.rows() && diag.size()==mat.rows() && subdiag.size()==mat.rows()-1);
0434   tridiagonalization_inplace_selector<MatrixType>::run(mat, diag, subdiag, hcoeffs, extractQ);
0435 }
0436 
0437 /** \internal
0438   * General full tridiagonalization
0439   */
0440 template<typename MatrixType, int Size, bool IsComplex>
0441 struct tridiagonalization_inplace_selector
0442 {
0443   typedef typename Tridiagonalization<MatrixType>::CoeffVectorType CoeffVectorType;
0444   typedef typename Tridiagonalization<MatrixType>::HouseholderSequenceType HouseholderSequenceType;
0445   template<typename DiagonalType, typename SubDiagonalType>
0446   static EIGEN_DEVICE_FUNC
0447       void run(MatrixType& mat, DiagonalType& diag, SubDiagonalType& subdiag, CoeffVectorType& hCoeffs, bool extractQ)
0448   {
0449     tridiagonalization_inplace(mat, hCoeffs);
0450     diag = mat.diagonal().real();
0451     subdiag = mat.template diagonal<-1>().real();
0452     if(extractQ)
0453       mat = HouseholderSequenceType(mat, hCoeffs.conjugate())
0454             .setLength(mat.rows() - 1)
0455             .setShift(1);
0456   }
0457 };
0458 
0459 /** \internal
0460   * Specialization for 3x3 real matrices.
0461   * Especially useful for plane fitting.
0462   */
0463 template<typename MatrixType>
0464 struct tridiagonalization_inplace_selector<MatrixType,3,false>
0465 {
0466   typedef typename MatrixType::Scalar Scalar;
0467   typedef typename MatrixType::RealScalar RealScalar;
0468 
0469   template<typename DiagonalType, typename SubDiagonalType, typename CoeffVectorType>
0470   static void run(MatrixType& mat, DiagonalType& diag, SubDiagonalType& subdiag, CoeffVectorType&, bool extractQ)
0471   {
0472     using std::sqrt;
0473     const RealScalar tol = (std::numeric_limits<RealScalar>::min)();
0474     diag[0] = mat(0,0);
0475     RealScalar v1norm2 = numext::abs2(mat(2,0));
0476     if(v1norm2 <= tol)
0477     {
0478       diag[1] = mat(1,1);
0479       diag[2] = mat(2,2);
0480       subdiag[0] = mat(1,0);
0481       subdiag[1] = mat(2,1);
0482       if (extractQ)
0483         mat.setIdentity();
0484     }
0485     else
0486     {
0487       RealScalar beta = sqrt(numext::abs2(mat(1,0)) + v1norm2);
0488       RealScalar invBeta = RealScalar(1)/beta;
0489       Scalar m01 = mat(1,0) * invBeta;
0490       Scalar m02 = mat(2,0) * invBeta;
0491       Scalar q = RealScalar(2)*m01*mat(2,1) + m02*(mat(2,2) - mat(1,1));
0492       diag[1] = mat(1,1) + m02*q;
0493       diag[2] = mat(2,2) - m02*q;
0494       subdiag[0] = beta;
0495       subdiag[1] = mat(2,1) - m01 * q;
0496       if (extractQ)
0497       {
0498         mat << 1,   0,    0,
0499                0, m01,  m02,
0500                0, m02, -m01;
0501       }
0502     }
0503   }
0504 };
0505 
0506 /** \internal
0507   * Trivial specialization for 1x1 matrices
0508   */
0509 template<typename MatrixType, bool IsComplex>
0510 struct tridiagonalization_inplace_selector<MatrixType,1,IsComplex>
0511 {
0512   typedef typename MatrixType::Scalar Scalar;
0513 
0514   template<typename DiagonalType, typename SubDiagonalType, typename CoeffVectorType>
0515   static EIGEN_DEVICE_FUNC
0516   void run(MatrixType& mat, DiagonalType& diag, SubDiagonalType&, CoeffVectorType&, bool extractQ)
0517   {
0518     diag(0,0) = numext::real(mat(0,0));
0519     if(extractQ)
0520       mat(0,0) = Scalar(1);
0521   }
0522 };
0523 
0524 /** \internal
0525   * \eigenvalues_module \ingroup Eigenvalues_Module
0526   *
0527   * \brief Expression type for return value of Tridiagonalization::matrixT()
0528   *
0529   * \tparam MatrixType type of underlying dense matrix
0530   */
0531 template<typename MatrixType> struct TridiagonalizationMatrixTReturnType
0532 : public ReturnByValue<TridiagonalizationMatrixTReturnType<MatrixType> >
0533 {
0534   public:
0535     /** \brief Constructor.
0536       *
0537       * \param[in] mat The underlying dense matrix
0538       */
0539     TridiagonalizationMatrixTReturnType(const MatrixType& mat) : m_matrix(mat) { }
0540 
0541     template <typename ResultType>
0542     inline void evalTo(ResultType& result) const
0543     {
0544       result.setZero();
0545       result.template diagonal<1>() = m_matrix.template diagonal<-1>().conjugate();
0546       result.diagonal() = m_matrix.diagonal();
0547       result.template diagonal<-1>() = m_matrix.template diagonal<-1>();
0548     }
0549 
0550     EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT { return m_matrix.rows(); }
0551     EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT { return m_matrix.cols(); }
0552 
0553   protected:
0554     typename MatrixType::Nested m_matrix;
0555 };
0556 
0557 } // end namespace internal
0558 
0559 } // end namespace Eigen
0560 
0561 #endif // EIGEN_TRIDIAGONALIZATION_H