Tridiagonalization.h 22 KB

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