Tridiagonalization.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
  5. // Copyright (C) 2010 Jitse Niesen <jitse@maths.leeds.ac.uk>
  6. //
  7. // This Source Code Form is subject to the terms of the Mozilla
  8. // Public License v. 2.0. If a copy of the MPL was not distributed
  9. // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
  10. #ifndef EIGEN_TRIDIAGONALIZATION_H
  11. #define EIGEN_TRIDIAGONALIZATION_H
  12. namespace Eigen {
  13. namespace internal {
  14. template<typename MatrixType> struct TridiagonalizationMatrixTReturnType;
  15. template<typename MatrixType>
  16. struct traits<TridiagonalizationMatrixTReturnType<MatrixType> >
  17. : public traits<typename MatrixType::PlainObject>
  18. {
  19. typedef typename MatrixType::PlainObject ReturnType; // FIXME shall it be a BandMatrix?
  20. enum { Flags = 0 };
  21. };
  22. template<typename MatrixType, typename CoeffVectorType>
  23. EIGEN_DEVICE_FUNC
  24. void tridiagonalization_inplace(MatrixType& matA, CoeffVectorType& hCoeffs);
  25. }
  26. /** \eigenvalues_module \ingroup Eigenvalues_Module
  27. *
  28. *
  29. * \class Tridiagonalization
  30. *
  31. * \brief Tridiagonal decomposition of a selfadjoint matrix
  32. *
  33. * \tparam _MatrixType the type of the matrix of which we are computing the
  34. * tridiagonal decomposition; this is expected to be an instantiation of the
  35. * Matrix class template.
  36. *
  37. * This class performs a tridiagonal decomposition of a selfadjoint matrix \f$ A \f$ such that:
  38. * \f$ A = Q T Q^* \f$ where \f$ Q \f$ is unitary and \f$ T \f$ a real symmetric tridiagonal matrix.
  39. *
  40. * A tridiagonal matrix is a matrix which has nonzero elements only on the
  41. * main diagonal and the first diagonal below and above it. The Hessenberg
  42. * decomposition of a selfadjoint matrix is in fact a tridiagonal
  43. * decomposition. This class is used in SelfAdjointEigenSolver to compute the
  44. * eigenvalues and eigenvectors of a selfadjoint matrix.
  45. *
  46. * Call the function compute() to compute the tridiagonal decomposition of a
  47. * given matrix. Alternatively, you can use the Tridiagonalization(const MatrixType&)
  48. * constructor which computes the tridiagonal Schur decomposition at
  49. * construction time. Once the decomposition is computed, you can use the
  50. * matrixQ() and matrixT() functions to retrieve the matrices Q and T in the
  51. * decomposition.
  52. *
  53. * The documentation of Tridiagonalization(const MatrixType&) contains an
  54. * example of the typical use of this class.
  55. *
  56. * \sa class HessenbergDecomposition, class SelfAdjointEigenSolver
  57. */
  58. template<typename _MatrixType> class Tridiagonalization
  59. {
  60. public:
  61. /** \brief Synonym for the template parameter \p _MatrixType. */
  62. typedef _MatrixType MatrixType;
  63. typedef typename MatrixType::Scalar Scalar;
  64. typedef typename NumTraits<Scalar>::Real RealScalar;
  65. typedef Eigen::Index Index; ///< \deprecated since Eigen 3.3
  66. enum {
  67. Size = MatrixType::RowsAtCompileTime,
  68. SizeMinusOne = Size == Dynamic ? Dynamic : (Size > 1 ? Size - 1 : 1),
  69. Options = MatrixType::Options,
  70. MaxSize = MatrixType::MaxRowsAtCompileTime,
  71. MaxSizeMinusOne = MaxSize == Dynamic ? Dynamic : (MaxSize > 1 ? MaxSize - 1 : 1)
  72. };
  73. typedef Matrix<Scalar, SizeMinusOne, 1, Options & ~RowMajor, MaxSizeMinusOne, 1> CoeffVectorType;
  74. typedef typename internal::plain_col_type<MatrixType, RealScalar>::type DiagonalType;
  75. typedef Matrix<RealScalar, SizeMinusOne, 1, Options & ~RowMajor, MaxSizeMinusOne, 1> SubDiagonalType;
  76. typedef typename internal::remove_all<typename MatrixType::RealReturnType>::type MatrixTypeRealView;
  77. typedef internal::TridiagonalizationMatrixTReturnType<MatrixTypeRealView> MatrixTReturnType;
  78. typedef typename internal::conditional<NumTraits<Scalar>::IsComplex,
  79. typename internal::add_const_on_value_type<typename Diagonal<const MatrixType>::RealReturnType>::type,
  80. const Diagonal<const MatrixType>
  81. >::type DiagonalReturnType;
  82. typedef typename internal::conditional<NumTraits<Scalar>::IsComplex,
  83. typename internal::add_const_on_value_type<typename Diagonal<const MatrixType, -1>::RealReturnType>::type,
  84. const Diagonal<const MatrixType, -1>
  85. >::type SubDiagonalReturnType;
  86. /** \brief Return type of matrixQ() */
  87. typedef HouseholderSequence<MatrixType,typename internal::remove_all<typename CoeffVectorType::ConjugateReturnType>::type> HouseholderSequenceType;
  88. /** \brief Default constructor.
  89. *
  90. * \param [in] size Positive integer, size of the matrix whose tridiagonal
  91. * decomposition will be computed.
  92. *
  93. * The default constructor is useful in cases in which the user intends to
  94. * perform decompositions via compute(). The \p size parameter is only
  95. * used as a hint. It is not an error to give a wrong \p size, but it may
  96. * impair performance.
  97. *
  98. * \sa compute() for an example.
  99. */
  100. explicit Tridiagonalization(Index size = Size==Dynamic ? 2 : Size)
  101. : m_matrix(size,size),
  102. m_hCoeffs(size > 1 ? size-1 : 1),
  103. m_isInitialized(false)
  104. {}
  105. /** \brief Constructor; computes tridiagonal decomposition of given matrix.
  106. *
  107. * \param[in] matrix Selfadjoint matrix whose tridiagonal decomposition
  108. * is to be computed.
  109. *
  110. * This constructor calls compute() to compute the tridiagonal decomposition.
  111. *
  112. * Example: \include Tridiagonalization_Tridiagonalization_MatrixType.cpp
  113. * Output: \verbinclude Tridiagonalization_Tridiagonalization_MatrixType.out
  114. */
  115. template<typename InputType>
  116. explicit Tridiagonalization(const EigenBase<InputType>& matrix)
  117. : m_matrix(matrix.derived()),
  118. m_hCoeffs(matrix.cols() > 1 ? matrix.cols()-1 : 1),
  119. m_isInitialized(false)
  120. {
  121. internal::tridiagonalization_inplace(m_matrix, m_hCoeffs);
  122. m_isInitialized = true;
  123. }
  124. /** \brief Computes tridiagonal decomposition of given matrix.
  125. *
  126. * \param[in] matrix Selfadjoint matrix whose tridiagonal decomposition
  127. * is to be computed.
  128. * \returns Reference to \c *this
  129. *
  130. * The tridiagonal decomposition is computed by bringing the columns of
  131. * the matrix successively in the required form using Householder
  132. * reflections. The cost is \f$ 4n^3/3 \f$ flops, where \f$ n \f$ denotes
  133. * the size of the given matrix.
  134. *
  135. * This method reuses of the allocated data in the Tridiagonalization
  136. * object, if the size of the matrix does not change.
  137. *
  138. * Example: \include Tridiagonalization_compute.cpp
  139. * Output: \verbinclude Tridiagonalization_compute.out
  140. */
  141. template<typename InputType>
  142. Tridiagonalization& compute(const EigenBase<InputType>& matrix)
  143. {
  144. m_matrix = matrix.derived();
  145. m_hCoeffs.resize(matrix.rows()-1, 1);
  146. internal::tridiagonalization_inplace(m_matrix, m_hCoeffs);
  147. m_isInitialized = true;
  148. return *this;
  149. }
  150. /** \brief Returns the Householder coefficients.
  151. *
  152. * \returns a const reference to the vector of Householder coefficients
  153. *
  154. * \pre Either the constructor Tridiagonalization(const MatrixType&) or
  155. * the member function compute(const MatrixType&) has been called before
  156. * to compute the tridiagonal decomposition of a matrix.
  157. *
  158. * The Householder coefficients allow the reconstruction of the matrix
  159. * \f$ Q \f$ in the tridiagonal decomposition from the packed data.
  160. *
  161. * Example: \include Tridiagonalization_householderCoefficients.cpp
  162. * Output: \verbinclude Tridiagonalization_householderCoefficients.out
  163. *
  164. * \sa packedMatrix(), \ref Householder_Module "Householder module"
  165. */
  166. inline CoeffVectorType householderCoefficients() const
  167. {
  168. eigen_assert(m_isInitialized && "Tridiagonalization is not initialized.");
  169. return m_hCoeffs;
  170. }
  171. /** \brief Returns the internal representation of the decomposition
  172. *
  173. * \returns a const reference to a matrix with the internal representation
  174. * of the decomposition.
  175. *
  176. * \pre Either the constructor Tridiagonalization(const MatrixType&) or
  177. * the member function compute(const MatrixType&) has been called before
  178. * to compute the tridiagonal decomposition of a matrix.
  179. *
  180. * The returned matrix contains the following information:
  181. * - the strict upper triangular part is equal to the input matrix A.
  182. * - the diagonal and lower sub-diagonal represent the real tridiagonal
  183. * symmetric matrix T.
  184. * - the rest of the lower part contains the Householder vectors that,
  185. * combined with Householder coefficients returned by
  186. * householderCoefficients(), allows to reconstruct the matrix Q as
  187. * \f$ Q = H_{N-1} \ldots H_1 H_0 \f$.
  188. * Here, the matrices \f$ H_i \f$ are the Householder transformations
  189. * \f$ H_i = (I - h_i v_i v_i^T) \f$
  190. * where \f$ h_i \f$ is the \f$ i \f$th Householder coefficient and
  191. * \f$ v_i \f$ is the Householder vector defined by
  192. * \f$ v_i = [ 0, \ldots, 0, 1, M(i+2,i), \ldots, M(N-1,i) ]^T \f$
  193. * with M the matrix returned by this function.
  194. *
  195. * See LAPACK for further details on this packed storage.
  196. *
  197. * Example: \include Tridiagonalization_packedMatrix.cpp
  198. * Output: \verbinclude Tridiagonalization_packedMatrix.out
  199. *
  200. * \sa householderCoefficients()
  201. */
  202. inline const MatrixType& packedMatrix() const
  203. {
  204. eigen_assert(m_isInitialized && "Tridiagonalization is not initialized.");
  205. return m_matrix;
  206. }
  207. /** \brief Returns the unitary matrix Q in the decomposition
  208. *
  209. * \returns object representing the matrix Q
  210. *
  211. * \pre Either the constructor Tridiagonalization(const MatrixType&) or
  212. * the member function compute(const MatrixType&) has been called before
  213. * to compute the tridiagonal decomposition of a matrix.
  214. *
  215. * This function returns a light-weight object of template class
  216. * HouseholderSequence. You can either apply it directly to a matrix or
  217. * you can convert it to a matrix of type #MatrixType.
  218. *
  219. * \sa Tridiagonalization(const MatrixType&) for an example,
  220. * matrixT(), class HouseholderSequence
  221. */
  222. HouseholderSequenceType matrixQ() const
  223. {
  224. eigen_assert(m_isInitialized && "Tridiagonalization is not initialized.");
  225. return HouseholderSequenceType(m_matrix, m_hCoeffs.conjugate())
  226. .setLength(m_matrix.rows() - 1)
  227. .setShift(1);
  228. }
  229. /** \brief Returns an expression of the tridiagonal matrix T in the decomposition
  230. *
  231. * \returns expression object representing the matrix T
  232. *
  233. * \pre Either the constructor Tridiagonalization(const MatrixType&) or
  234. * the member function compute(const MatrixType&) has been called before
  235. * to compute the tridiagonal decomposition of a matrix.
  236. *
  237. * Currently, this function can be used to extract the matrix T from internal
  238. * data and copy it to a dense matrix object. In most cases, it may be
  239. * sufficient to directly use the packed matrix or the vector expressions
  240. * returned by diagonal() and subDiagonal() instead of creating a new
  241. * dense copy matrix with this function.
  242. *
  243. * \sa Tridiagonalization(const MatrixType&) for an example,
  244. * matrixQ(), packedMatrix(), diagonal(), subDiagonal()
  245. */
  246. MatrixTReturnType matrixT() const
  247. {
  248. eigen_assert(m_isInitialized && "Tridiagonalization is not initialized.");
  249. return MatrixTReturnType(m_matrix.real());
  250. }
  251. /** \brief Returns the diagonal of the tridiagonal matrix T in the decomposition.
  252. *
  253. * \returns expression representing the diagonal of T
  254. *
  255. * \pre Either the constructor Tridiagonalization(const MatrixType&) or
  256. * the member function compute(const MatrixType&) has been called before
  257. * to compute the tridiagonal decomposition of a matrix.
  258. *
  259. * Example: \include Tridiagonalization_diagonal.cpp
  260. * Output: \verbinclude Tridiagonalization_diagonal.out
  261. *
  262. * \sa matrixT(), subDiagonal()
  263. */
  264. DiagonalReturnType diagonal() const;
  265. /** \brief Returns the subdiagonal of the tridiagonal matrix T in the decomposition.
  266. *
  267. * \returns expression representing the subdiagonal of T
  268. *
  269. * \pre Either the constructor Tridiagonalization(const MatrixType&) or
  270. * the member function compute(const MatrixType&) has been called before
  271. * to compute the tridiagonal decomposition of a matrix.
  272. *
  273. * \sa diagonal() for an example, matrixT()
  274. */
  275. SubDiagonalReturnType subDiagonal() const;
  276. protected:
  277. MatrixType m_matrix;
  278. CoeffVectorType m_hCoeffs;
  279. bool m_isInitialized;
  280. };
  281. template<typename MatrixType>
  282. typename Tridiagonalization<MatrixType>::DiagonalReturnType
  283. Tridiagonalization<MatrixType>::diagonal() const
  284. {
  285. eigen_assert(m_isInitialized && "Tridiagonalization is not initialized.");
  286. return m_matrix.diagonal().real();
  287. }
  288. template<typename MatrixType>
  289. typename Tridiagonalization<MatrixType>::SubDiagonalReturnType
  290. Tridiagonalization<MatrixType>::subDiagonal() const
  291. {
  292. eigen_assert(m_isInitialized && "Tridiagonalization is not initialized.");
  293. return m_matrix.template diagonal<-1>().real();
  294. }
  295. namespace internal {
  296. /** \internal
  297. * Performs a tridiagonal decomposition of the selfadjoint matrix \a matA in-place.
  298. *
  299. * \param[in,out] matA On input the selfadjoint matrix. Only the \b lower triangular part is referenced.
  300. * On output, the strict upper part is left unchanged, and the lower triangular part
  301. * represents the T and Q matrices in packed format has detailed below.
  302. * \param[out] hCoeffs returned Householder coefficients (see below)
  303. *
  304. * On output, the tridiagonal selfadjoint matrix T is stored in the diagonal
  305. * and lower sub-diagonal of the matrix \a matA.
  306. * The unitary matrix Q is represented in a compact way as a product of
  307. * Householder reflectors \f$ H_i \f$ such that:
  308. * \f$ Q = H_{N-1} \ldots H_1 H_0 \f$.
  309. * The Householder reflectors are defined as
  310. * \f$ H_i = (I - h_i v_i v_i^T) \f$
  311. * where \f$ h_i = hCoeffs[i]\f$ is the \f$ i \f$th Householder coefficient and
  312. * \f$ v_i \f$ is the Householder vector defined by
  313. * \f$ v_i = [ 0, \ldots, 0, 1, matA(i+2,i), \ldots, matA(N-1,i) ]^T \f$.
  314. *
  315. * Implemented from Golub's "Matrix Computations", algorithm 8.3.1.
  316. *
  317. * \sa Tridiagonalization::packedMatrix()
  318. */
  319. template<typename MatrixType, typename CoeffVectorType>
  320. EIGEN_DEVICE_FUNC
  321. void tridiagonalization_inplace(MatrixType& matA, CoeffVectorType& hCoeffs)
  322. {
  323. using numext::conj;
  324. typedef typename MatrixType::Scalar Scalar;
  325. typedef typename MatrixType::RealScalar RealScalar;
  326. Index n = matA.rows();
  327. eigen_assert(n==matA.cols());
  328. eigen_assert(n==hCoeffs.size()+1 || n==1);
  329. for (Index i = 0; i<n-1; ++i)
  330. {
  331. Index remainingSize = n-i-1;
  332. RealScalar beta;
  333. Scalar h;
  334. matA.col(i).tail(remainingSize).makeHouseholderInPlace(h, beta);
  335. // Apply similarity transformation to remaining columns,
  336. // i.e., A = H A H' where H = I - h v v' and v = matA.col(i).tail(n-i-1)
  337. matA.col(i).coeffRef(i+1) = 1;
  338. hCoeffs.tail(n-i-1).noalias() = (matA.bottomRightCorner(remainingSize,remainingSize).template selfadjointView<Lower>()
  339. * (conj(h) * matA.col(i).tail(remainingSize)));
  340. 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);
  341. matA.bottomRightCorner(remainingSize, remainingSize).template selfadjointView<Lower>()
  342. .rankUpdate(matA.col(i).tail(remainingSize), hCoeffs.tail(remainingSize), Scalar(-1));
  343. matA.col(i).coeffRef(i+1) = beta;
  344. hCoeffs.coeffRef(i) = h;
  345. }
  346. }
  347. // forward declaration, implementation at the end of this file
  348. template<typename MatrixType,
  349. int Size=MatrixType::ColsAtCompileTime,
  350. bool IsComplex=NumTraits<typename MatrixType::Scalar>::IsComplex>
  351. struct tridiagonalization_inplace_selector;
  352. /** \brief Performs a full tridiagonalization in place
  353. *
  354. * \param[in,out] mat On input, the selfadjoint matrix whose tridiagonal
  355. * decomposition is to be computed. Only the lower triangular part referenced.
  356. * The rest is left unchanged. On output, the orthogonal matrix Q
  357. * in the decomposition if \p extractQ is true.
  358. * \param[out] diag The diagonal of the tridiagonal matrix T in the
  359. * decomposition.
  360. * \param[out] subdiag The subdiagonal of the tridiagonal matrix T in
  361. * the decomposition.
  362. * \param[in] extractQ If true, the orthogonal matrix Q in the
  363. * decomposition is computed and stored in \p mat.
  364. *
  365. * Computes the tridiagonal decomposition of the selfadjoint matrix \p mat in place
  366. * such that \f$ mat = Q T Q^* \f$ where \f$ Q \f$ is unitary and \f$ T \f$ a real
  367. * symmetric tridiagonal matrix.
  368. *
  369. * The tridiagonal matrix T is passed to the output parameters \p diag and \p subdiag. If
  370. * \p extractQ is true, then the orthogonal matrix Q is passed to \p mat. Otherwise the lower
  371. * part of the matrix \p mat is destroyed.
  372. *
  373. * The vectors \p diag and \p subdiag are not resized. The function
  374. * assumes that they are already of the correct size. The length of the
  375. * vector \p diag should equal the number of rows in \p mat, and the
  376. * length of the vector \p subdiag should be one left.
  377. *
  378. * This implementation contains an optimized path for 3-by-3 matrices
  379. * which is especially useful for plane fitting.
  380. *
  381. * \note Currently, it requires two temporary vectors to hold the intermediate
  382. * Householder coefficients, and to reconstruct the matrix Q from the Householder
  383. * reflectors.
  384. *
  385. * Example (this uses the same matrix as the example in
  386. * Tridiagonalization::Tridiagonalization(const MatrixType&)):
  387. * \include Tridiagonalization_decomposeInPlace.cpp
  388. * Output: \verbinclude Tridiagonalization_decomposeInPlace.out
  389. *
  390. * \sa class Tridiagonalization
  391. */
  392. template<typename MatrixType, typename DiagonalType, typename SubDiagonalType, typename CoeffVectorType>
  393. EIGEN_DEVICE_FUNC
  394. void tridiagonalization_inplace(MatrixType& mat, DiagonalType& diag, SubDiagonalType& subdiag,
  395. CoeffVectorType& hcoeffs, bool extractQ)
  396. {
  397. eigen_assert(mat.cols()==mat.rows() && diag.size()==mat.rows() && subdiag.size()==mat.rows()-1);
  398. tridiagonalization_inplace_selector<MatrixType>::run(mat, diag, subdiag, hcoeffs, extractQ);
  399. }
  400. /** \internal
  401. * General full tridiagonalization
  402. */
  403. template<typename MatrixType, int Size, bool IsComplex>
  404. struct tridiagonalization_inplace_selector
  405. {
  406. typedef typename Tridiagonalization<MatrixType>::CoeffVectorType CoeffVectorType;
  407. typedef typename Tridiagonalization<MatrixType>::HouseholderSequenceType HouseholderSequenceType;
  408. template<typename DiagonalType, typename SubDiagonalType>
  409. static EIGEN_DEVICE_FUNC
  410. void run(MatrixType& mat, DiagonalType& diag, SubDiagonalType& subdiag, CoeffVectorType& hCoeffs, bool extractQ)
  411. {
  412. tridiagonalization_inplace(mat, hCoeffs);
  413. diag = mat.diagonal().real();
  414. subdiag = mat.template diagonal<-1>().real();
  415. if(extractQ)
  416. mat = HouseholderSequenceType(mat, hCoeffs.conjugate())
  417. .setLength(mat.rows() - 1)
  418. .setShift(1);
  419. }
  420. };
  421. /** \internal
  422. * Specialization for 3x3 real matrices.
  423. * Especially useful for plane fitting.
  424. */
  425. template<typename MatrixType>
  426. struct tridiagonalization_inplace_selector<MatrixType,3,false>
  427. {
  428. typedef typename MatrixType::Scalar Scalar;
  429. typedef typename MatrixType::RealScalar RealScalar;
  430. template<typename DiagonalType, typename SubDiagonalType, typename CoeffVectorType>
  431. static void run(MatrixType& mat, DiagonalType& diag, SubDiagonalType& subdiag, CoeffVectorType&, bool extractQ)
  432. {
  433. using std::sqrt;
  434. const RealScalar tol = (std::numeric_limits<RealScalar>::min)();
  435. diag[0] = mat(0,0);
  436. RealScalar v1norm2 = numext::abs2(mat(2,0));
  437. if(v1norm2 <= tol)
  438. {
  439. diag[1] = mat(1,1);
  440. diag[2] = mat(2,2);
  441. subdiag[0] = mat(1,0);
  442. subdiag[1] = mat(2,1);
  443. if (extractQ)
  444. mat.setIdentity();
  445. }
  446. else
  447. {
  448. RealScalar beta = sqrt(numext::abs2(mat(1,0)) + v1norm2);
  449. RealScalar invBeta = RealScalar(1)/beta;
  450. Scalar m01 = mat(1,0) * invBeta;
  451. Scalar m02 = mat(2,0) * invBeta;
  452. Scalar q = RealScalar(2)*m01*mat(2,1) + m02*(mat(2,2) - mat(1,1));
  453. diag[1] = mat(1,1) + m02*q;
  454. diag[2] = mat(2,2) - m02*q;
  455. subdiag[0] = beta;
  456. subdiag[1] = mat(2,1) - m01 * q;
  457. if (extractQ)
  458. {
  459. mat << 1, 0, 0,
  460. 0, m01, m02,
  461. 0, m02, -m01;
  462. }
  463. }
  464. }
  465. };
  466. /** \internal
  467. * Trivial specialization for 1x1 matrices
  468. */
  469. template<typename MatrixType, bool IsComplex>
  470. struct tridiagonalization_inplace_selector<MatrixType,1,IsComplex>
  471. {
  472. typedef typename MatrixType::Scalar Scalar;
  473. template<typename DiagonalType, typename SubDiagonalType, typename CoeffVectorType>
  474. static EIGEN_DEVICE_FUNC
  475. void run(MatrixType& mat, DiagonalType& diag, SubDiagonalType&, CoeffVectorType&, bool extractQ)
  476. {
  477. diag(0,0) = numext::real(mat(0,0));
  478. if(extractQ)
  479. mat(0,0) = Scalar(1);
  480. }
  481. };
  482. /** \internal
  483. * \eigenvalues_module \ingroup Eigenvalues_Module
  484. *
  485. * \brief Expression type for return value of Tridiagonalization::matrixT()
  486. *
  487. * \tparam MatrixType type of underlying dense matrix
  488. */
  489. template<typename MatrixType> struct TridiagonalizationMatrixTReturnType
  490. : public ReturnByValue<TridiagonalizationMatrixTReturnType<MatrixType> >
  491. {
  492. public:
  493. /** \brief Constructor.
  494. *
  495. * \param[in] mat The underlying dense matrix
  496. */
  497. TridiagonalizationMatrixTReturnType(const MatrixType& mat) : m_matrix(mat) { }
  498. template <typename ResultType>
  499. inline void evalTo(ResultType& result) const
  500. {
  501. result.setZero();
  502. result.template diagonal<1>() = m_matrix.template diagonal<-1>().conjugate();
  503. result.diagonal() = m_matrix.diagonal();
  504. result.template diagonal<-1>() = m_matrix.template diagonal<-1>();
  505. }
  506. EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT { return m_matrix.rows(); }
  507. EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT { return m_matrix.cols(); }
  508. protected:
  509. typename MatrixType::Nested m_matrix;
  510. };
  511. } // end namespace internal
  512. } // end namespace Eigen
  513. #endif // EIGEN_TRIDIAGONALIZATION_H