Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:57:05

0001 // This file is part of Eigen, a lightweight C++ template library
0002 // for linear algebra.
0003 //
0004 // Copyright (C) 2011, 2013 Jitse Niesen <jitse@maths.leeds.ac.uk>
0005 //
0006 // This Source Code Form is subject to the terms of the Mozilla
0007 // Public License v. 2.0. If a copy of the MPL was not distributed
0008 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
0009 
0010 #ifndef EIGEN_MATRIX_SQUARE_ROOT
0011 #define EIGEN_MATRIX_SQUARE_ROOT
0012 
0013 namespace Eigen { 
0014 
0015 namespace internal {
0016 
0017 // pre:  T.block(i,i,2,2) has complex conjugate eigenvalues
0018 // post: sqrtT.block(i,i,2,2) is square root of T.block(i,i,2,2)
0019 template <typename MatrixType, typename ResultType>
0020 void matrix_sqrt_quasi_triangular_2x2_diagonal_block(const MatrixType& T, Index i, ResultType& sqrtT)
0021 {
0022   // TODO: This case (2-by-2 blocks with complex conjugate eigenvalues) is probably hidden somewhere
0023   //       in EigenSolver. If we expose it, we could call it directly from here.
0024   typedef typename traits<MatrixType>::Scalar Scalar;
0025   Matrix<Scalar,2,2> block = T.template block<2,2>(i,i);
0026   EigenSolver<Matrix<Scalar,2,2> > es(block);
0027   sqrtT.template block<2,2>(i,i)
0028     = (es.eigenvectors() * es.eigenvalues().cwiseSqrt().asDiagonal() * es.eigenvectors().inverse()).real();
0029 }
0030 
0031 // pre:  block structure of T is such that (i,j) is a 1x1 block,
0032 //       all blocks of sqrtT to left of and below (i,j) are correct
0033 // post: sqrtT(i,j) has the correct value
0034 template <typename MatrixType, typename ResultType>
0035 void matrix_sqrt_quasi_triangular_1x1_off_diagonal_block(const MatrixType& T, Index i, Index j, ResultType& sqrtT)
0036 {
0037   typedef typename traits<MatrixType>::Scalar Scalar;
0038   Scalar tmp = (sqrtT.row(i).segment(i+1,j-i-1) * sqrtT.col(j).segment(i+1,j-i-1)).value();
0039   sqrtT.coeffRef(i,j) = (T.coeff(i,j) - tmp) / (sqrtT.coeff(i,i) + sqrtT.coeff(j,j));
0040 }
0041 
0042 // similar to compute1x1offDiagonalBlock()
0043 template <typename MatrixType, typename ResultType>
0044 void matrix_sqrt_quasi_triangular_1x2_off_diagonal_block(const MatrixType& T, Index i, Index j, ResultType& sqrtT)
0045 {
0046   typedef typename traits<MatrixType>::Scalar Scalar;
0047   Matrix<Scalar,1,2> rhs = T.template block<1,2>(i,j);
0048   if (j-i > 1)
0049     rhs -= sqrtT.block(i, i+1, 1, j-i-1) * sqrtT.block(i+1, j, j-i-1, 2);
0050   Matrix<Scalar,2,2> A = sqrtT.coeff(i,i) * Matrix<Scalar,2,2>::Identity();
0051   A += sqrtT.template block<2,2>(j,j).transpose();
0052   sqrtT.template block<1,2>(i,j).transpose() = A.fullPivLu().solve(rhs.transpose());
0053 }
0054 
0055 // similar to compute1x1offDiagonalBlock()
0056 template <typename MatrixType, typename ResultType>
0057 void matrix_sqrt_quasi_triangular_2x1_off_diagonal_block(const MatrixType& T, Index i, Index j, ResultType& sqrtT)
0058 {
0059   typedef typename traits<MatrixType>::Scalar Scalar;
0060   Matrix<Scalar,2,1> rhs = T.template block<2,1>(i,j);
0061   if (j-i > 2)
0062     rhs -= sqrtT.block(i, i+2, 2, j-i-2) * sqrtT.block(i+2, j, j-i-2, 1);
0063   Matrix<Scalar,2,2> A = sqrtT.coeff(j,j) * Matrix<Scalar,2,2>::Identity();
0064   A += sqrtT.template block<2,2>(i,i);
0065   sqrtT.template block<2,1>(i,j) = A.fullPivLu().solve(rhs);
0066 }
0067 
0068 // solves the equation A X + X B = C where all matrices are 2-by-2
0069 template <typename MatrixType>
0070 void matrix_sqrt_quasi_triangular_solve_auxiliary_equation(MatrixType& X, const MatrixType& A, const MatrixType& B, const MatrixType& C)
0071 {
0072   typedef typename traits<MatrixType>::Scalar Scalar;
0073   Matrix<Scalar,4,4> coeffMatrix = Matrix<Scalar,4,4>::Zero();
0074   coeffMatrix.coeffRef(0,0) = A.coeff(0,0) + B.coeff(0,0);
0075   coeffMatrix.coeffRef(1,1) = A.coeff(0,0) + B.coeff(1,1);
0076   coeffMatrix.coeffRef(2,2) = A.coeff(1,1) + B.coeff(0,0);
0077   coeffMatrix.coeffRef(3,3) = A.coeff(1,1) + B.coeff(1,1);
0078   coeffMatrix.coeffRef(0,1) = B.coeff(1,0);
0079   coeffMatrix.coeffRef(0,2) = A.coeff(0,1);
0080   coeffMatrix.coeffRef(1,0) = B.coeff(0,1);
0081   coeffMatrix.coeffRef(1,3) = A.coeff(0,1);
0082   coeffMatrix.coeffRef(2,0) = A.coeff(1,0);
0083   coeffMatrix.coeffRef(2,3) = B.coeff(1,0);
0084   coeffMatrix.coeffRef(3,1) = A.coeff(1,0);
0085   coeffMatrix.coeffRef(3,2) = B.coeff(0,1);
0086 
0087   Matrix<Scalar,4,1> rhs;
0088   rhs.coeffRef(0) = C.coeff(0,0);
0089   rhs.coeffRef(1) = C.coeff(0,1);
0090   rhs.coeffRef(2) = C.coeff(1,0);
0091   rhs.coeffRef(3) = C.coeff(1,1);
0092 
0093   Matrix<Scalar,4,1> result;
0094   result = coeffMatrix.fullPivLu().solve(rhs);
0095 
0096   X.coeffRef(0,0) = result.coeff(0);
0097   X.coeffRef(0,1) = result.coeff(1);
0098   X.coeffRef(1,0) = result.coeff(2);
0099   X.coeffRef(1,1) = result.coeff(3);
0100 }
0101 
0102 // similar to compute1x1offDiagonalBlock()
0103 template <typename MatrixType, typename ResultType>
0104 void matrix_sqrt_quasi_triangular_2x2_off_diagonal_block(const MatrixType& T, Index i, Index j, ResultType& sqrtT)
0105 {
0106   typedef typename traits<MatrixType>::Scalar Scalar;
0107   Matrix<Scalar,2,2> A = sqrtT.template block<2,2>(i,i);
0108   Matrix<Scalar,2,2> B = sqrtT.template block<2,2>(j,j);
0109   Matrix<Scalar,2,2> C = T.template block<2,2>(i,j);
0110   if (j-i > 2)
0111     C -= sqrtT.block(i, i+2, 2, j-i-2) * sqrtT.block(i+2, j, j-i-2, 2);
0112   Matrix<Scalar,2,2> X;
0113   matrix_sqrt_quasi_triangular_solve_auxiliary_equation(X, A, B, C);
0114   sqrtT.template block<2,2>(i,j) = X;
0115 }
0116 
0117 // pre:  T is quasi-upper-triangular and sqrtT is a zero matrix of the same size
0118 // post: the diagonal blocks of sqrtT are the square roots of the diagonal blocks of T
0119 template <typename MatrixType, typename ResultType>
0120 void matrix_sqrt_quasi_triangular_diagonal(const MatrixType& T, ResultType& sqrtT)
0121 {
0122   using std::sqrt;
0123   const Index size = T.rows();
0124   for (Index i = 0; i < size; i++) {
0125     if (i == size - 1 || T.coeff(i+1, i) == 0) {
0126       eigen_assert(T(i,i) >= 0);
0127       sqrtT.coeffRef(i,i) = sqrt(T.coeff(i,i));
0128     }
0129     else {
0130       matrix_sqrt_quasi_triangular_2x2_diagonal_block(T, i, sqrtT);
0131       ++i;
0132     }
0133   }
0134 }
0135 
0136 // pre:  T is quasi-upper-triangular and diagonal blocks of sqrtT are square root of diagonal blocks of T.
0137 // post: sqrtT is the square root of T.
0138 template <typename MatrixType, typename ResultType>
0139 void matrix_sqrt_quasi_triangular_off_diagonal(const MatrixType& T, ResultType& sqrtT)
0140 {
0141   const Index size = T.rows();
0142   for (Index j = 1; j < size; j++) {
0143       if (T.coeff(j, j-1) != 0)  // if T(j-1:j, j-1:j) is a 2-by-2 block
0144     continue;
0145     for (Index i = j-1; i >= 0; i--) {
0146       if (i > 0 && T.coeff(i, i-1) != 0)  // if T(i-1:i, i-1:i) is a 2-by-2 block
0147     continue;
0148       bool iBlockIs2x2 = (i < size - 1) && (T.coeff(i+1, i) != 0);
0149       bool jBlockIs2x2 = (j < size - 1) && (T.coeff(j+1, j) != 0);
0150       if (iBlockIs2x2 && jBlockIs2x2) 
0151         matrix_sqrt_quasi_triangular_2x2_off_diagonal_block(T, i, j, sqrtT);
0152       else if (iBlockIs2x2 && !jBlockIs2x2) 
0153         matrix_sqrt_quasi_triangular_2x1_off_diagonal_block(T, i, j, sqrtT);
0154       else if (!iBlockIs2x2 && jBlockIs2x2) 
0155         matrix_sqrt_quasi_triangular_1x2_off_diagonal_block(T, i, j, sqrtT);
0156       else if (!iBlockIs2x2 && !jBlockIs2x2) 
0157         matrix_sqrt_quasi_triangular_1x1_off_diagonal_block(T, i, j, sqrtT);
0158     }
0159   }
0160 }
0161 
0162 } // end of namespace internal
0163 
0164 /** \ingroup MatrixFunctions_Module
0165   * \brief Compute matrix square root of quasi-triangular matrix.
0166   *
0167   * \tparam  MatrixType  type of \p arg, the argument of matrix square root,
0168   *                      expected to be an instantiation of the Matrix class template.
0169   * \tparam  ResultType  type of \p result, where result is to be stored.
0170   * \param[in]  arg      argument of matrix square root.
0171   * \param[out] result   matrix square root of upper Hessenberg part of \p arg.
0172   *
0173   * This function computes the square root of the upper quasi-triangular matrix stored in the upper
0174   * Hessenberg part of \p arg.  Only the upper Hessenberg part of \p result is updated, the rest is
0175   * not touched.  See MatrixBase::sqrt() for details on how this computation is implemented.
0176   *
0177   * \sa MatrixSquareRoot, MatrixSquareRootQuasiTriangular
0178   */
0179 template <typename MatrixType, typename ResultType> 
0180 void matrix_sqrt_quasi_triangular(const MatrixType &arg, ResultType &result)
0181 {
0182   eigen_assert(arg.rows() == arg.cols());
0183   result.resize(arg.rows(), arg.cols());
0184   internal::matrix_sqrt_quasi_triangular_diagonal(arg, result);
0185   internal::matrix_sqrt_quasi_triangular_off_diagonal(arg, result);
0186 }
0187 
0188 
0189 /** \ingroup MatrixFunctions_Module
0190   * \brief Compute matrix square root of triangular matrix.
0191   *
0192   * \tparam  MatrixType  type of \p arg, the argument of matrix square root,
0193   *                      expected to be an instantiation of the Matrix class template.
0194   * \tparam  ResultType  type of \p result, where result is to be stored.
0195   * \param[in]  arg      argument of matrix square root.
0196   * \param[out] result   matrix square root of upper triangular part of \p arg.
0197   *
0198   * Only the upper triangular part (including the diagonal) of \p result is updated, the rest is not
0199   * touched.  See MatrixBase::sqrt() for details on how this computation is implemented.
0200   *
0201   * \sa MatrixSquareRoot, MatrixSquareRootQuasiTriangular
0202   */
0203 template <typename MatrixType, typename ResultType> 
0204 void matrix_sqrt_triangular(const MatrixType &arg, ResultType &result)
0205 {
0206   using std::sqrt;
0207   typedef typename MatrixType::Scalar Scalar;
0208 
0209   eigen_assert(arg.rows() == arg.cols());
0210 
0211   // Compute square root of arg and store it in upper triangular part of result
0212   // This uses that the square root of triangular matrices can be computed directly.
0213   result.resize(arg.rows(), arg.cols());
0214   for (Index i = 0; i < arg.rows(); i++) {
0215     result.coeffRef(i,i) = sqrt(arg.coeff(i,i));
0216   }
0217   for (Index j = 1; j < arg.cols(); j++) {
0218     for (Index i = j-1; i >= 0; i--) {
0219       // if i = j-1, then segment has length 0 so tmp = 0
0220       Scalar tmp = (result.row(i).segment(i+1,j-i-1) * result.col(j).segment(i+1,j-i-1)).value();
0221       // denominator may be zero if original matrix is singular
0222       result.coeffRef(i,j) = (arg.coeff(i,j) - tmp) / (result.coeff(i,i) + result.coeff(j,j));
0223     }
0224   }
0225 }
0226 
0227 
0228 namespace internal {
0229 
0230 /** \ingroup MatrixFunctions_Module
0231   * \brief Helper struct for computing matrix square roots of general matrices.
0232   * \tparam  MatrixType  type of the argument of the matrix square root,
0233   *                      expected to be an instantiation of the Matrix class template.
0234   *
0235   * \sa MatrixSquareRootTriangular, MatrixSquareRootQuasiTriangular, MatrixBase::sqrt()
0236   */
0237 template <typename MatrixType, int IsComplex = NumTraits<typename internal::traits<MatrixType>::Scalar>::IsComplex>
0238 struct matrix_sqrt_compute
0239 {
0240   /** \brief Compute the matrix square root
0241     *
0242     * \param[in]  arg     matrix whose square root is to be computed.
0243     * \param[out] result  square root of \p arg.
0244     *
0245     * See MatrixBase::sqrt() for details on how this computation is implemented.
0246     */
0247   template <typename ResultType> static void run(const MatrixType &arg, ResultType &result);    
0248 };
0249 
0250 
0251 // ********** Partial specialization for real matrices **********
0252 
0253 template <typename MatrixType>
0254 struct matrix_sqrt_compute<MatrixType, 0>
0255 {
0256   typedef typename MatrixType::PlainObject PlainType;
0257   template <typename ResultType>
0258   static void run(const MatrixType &arg, ResultType &result)
0259   {
0260     eigen_assert(arg.rows() == arg.cols());
0261 
0262     // Compute Schur decomposition of arg
0263     const RealSchur<PlainType> schurOfA(arg);
0264     const PlainType& T = schurOfA.matrixT();
0265     const PlainType& U = schurOfA.matrixU();
0266     
0267     // Compute square root of T
0268     PlainType sqrtT = PlainType::Zero(arg.rows(), arg.cols());
0269     matrix_sqrt_quasi_triangular(T, sqrtT);
0270     
0271     // Compute square root of arg
0272     result = U * sqrtT * U.adjoint();
0273   }
0274 };
0275 
0276 
0277 // ********** Partial specialization for complex matrices **********
0278 
0279 template <typename MatrixType>
0280 struct matrix_sqrt_compute<MatrixType, 1>
0281 {
0282   typedef typename MatrixType::PlainObject PlainType;
0283   template <typename ResultType>
0284   static void run(const MatrixType &arg, ResultType &result)
0285   {
0286     eigen_assert(arg.rows() == arg.cols());
0287 
0288     // Compute Schur decomposition of arg
0289     const ComplexSchur<PlainType> schurOfA(arg);
0290     const PlainType& T = schurOfA.matrixT();
0291     const PlainType& U = schurOfA.matrixU();
0292     
0293     // Compute square root of T
0294     PlainType sqrtT;
0295     matrix_sqrt_triangular(T, sqrtT);
0296     
0297     // Compute square root of arg
0298     result = U * (sqrtT.template triangularView<Upper>() * U.adjoint());
0299   }
0300 };
0301 
0302 } // end namespace internal
0303 
0304 /** \ingroup MatrixFunctions_Module
0305   *
0306   * \brief Proxy for the matrix square root of some matrix (expression).
0307   *
0308   * \tparam Derived  Type of the argument to the matrix square root.
0309   *
0310   * This class holds the argument to the matrix square root until it
0311   * is assigned or evaluated for some other reason (so the argument
0312   * should not be changed in the meantime). It is the return type of
0313   * MatrixBase::sqrt() and most of the time this is the only way it is
0314   * used.
0315   */
0316 template<typename Derived> class MatrixSquareRootReturnValue
0317 : public ReturnByValue<MatrixSquareRootReturnValue<Derived> >
0318 {
0319   protected:
0320     typedef typename internal::ref_selector<Derived>::type DerivedNested;
0321 
0322   public:
0323     /** \brief Constructor.
0324       *
0325       * \param[in]  src  %Matrix (expression) forming the argument of the
0326       * matrix square root.
0327       */
0328     explicit MatrixSquareRootReturnValue(const Derived& src) : m_src(src) { }
0329 
0330     /** \brief Compute the matrix square root.
0331       *
0332       * \param[out]  result  the matrix square root of \p src in the
0333       * constructor.
0334       */
0335     template <typename ResultType>
0336     inline void evalTo(ResultType& result) const
0337     {
0338       typedef typename internal::nested_eval<Derived, 10>::type DerivedEvalType;
0339       typedef typename internal::remove_all<DerivedEvalType>::type DerivedEvalTypeClean;
0340       DerivedEvalType tmp(m_src);
0341       internal::matrix_sqrt_compute<DerivedEvalTypeClean>::run(tmp, result);
0342     }
0343 
0344     Index rows() const { return m_src.rows(); }
0345     Index cols() const { return m_src.cols(); }
0346 
0347   protected:
0348     const DerivedNested m_src;
0349 };
0350 
0351 namespace internal {
0352 template<typename Derived>
0353 struct traits<MatrixSquareRootReturnValue<Derived> >
0354 {
0355   typedef typename Derived::PlainObject ReturnType;
0356 };
0357 }
0358 
0359 template <typename Derived>
0360 const MatrixSquareRootReturnValue<Derived> MatrixBase<Derived>::sqrt() const
0361 {
0362   eigen_assert(rows() == cols());
0363   return MatrixSquareRootReturnValue<Derived>(derived());
0364 }
0365 
0366 } // end namespace Eigen
0367 
0368 #endif // EIGEN_MATRIX_FUNCTION