diff --git a/unsupported/Eigen/src/MatrixFunctions/MatrixFunction.h b/unsupported/Eigen/src/MatrixFunctions/MatrixFunction.h index eb7c71eed..6a5961468 100644 --- a/unsupported/Eigen/src/MatrixFunctions/MatrixFunction.h +++ b/unsupported/Eigen/src/MatrixFunctions/MatrixFunction.h @@ -116,8 +116,7 @@ class MatrixFunction /** \ingroup MatrixFunctions_Module - * \brief Partial specialization of MatrixFunction for real matrices. - * \internal + * \brief Partial specialization of MatrixFunction for real matrices \internal */ template class MatrixFunction @@ -159,8 +158,7 @@ class MatrixFunction /** \ingroup MatrixFunctions_Module - * \brief Partial specialization of MatrixFunction for complex matrices - * \internal + * \brief Partial specialization of MatrixFunction for complex matrices \internal */ template class MatrixFunction @@ -176,8 +174,8 @@ class MatrixFunction typedef typename ei_stem_function::type StemFunction; typedef Matrix VectorType; typedef Matrix IntVectorType; - typedef std::list listOfScalars; - typedef std::list listOfLists; + typedef std::list Cluster; + typedef std::list ListOfClusters; typedef Matrix DynMatrixType; public: @@ -192,59 +190,173 @@ class MatrixFunction private: - // Prevent copying - MatrixFunction(const MatrixFunction&); - MatrixFunction& operator=(const MatrixFunction&); - - void separateBlocksInSchur(MatrixType& T, MatrixType& U, VectorXi& blockSize); - void permuteSchur(const IntVectorType& permutation, MatrixType& T, MatrixType& U); - void swapEntriesInSchur(int index, MatrixType& T, MatrixType& U); - void computeTriangular(const MatrixType& T, MatrixType& result, const VectorXi& blockSize); - void computeBlockAtomic(const MatrixType& T, MatrixType& result, const VectorXi& blockSize); + void computeSchurDecomposition(const MatrixType& A); + void partitionEigenvalues(); + typename ListOfClusters::iterator findCluster(Scalar key); + void computeClusterSize(); + void computeBlockStart(); + void constructPermutation(); + void permuteSchur(); + void swapEntriesInSchur(int index); + void computeBlockAtomic(); + Block block(const MatrixType& A, int i, int j); + void computeOffDiagonal(); DynMatrixType solveTriangularSylvester(const DynMatrixType& A, const DynMatrixType& B, const DynMatrixType& C); - void divideInBlocks(const VectorType& v, listOfLists* result); - void constructPermutation(const VectorType& diag, const listOfLists& blocks, - VectorXi& blockSize, IntVectorType& permutation); + StemFunction *m_f; /**< \brief Stem function for matrix function under consideration */ + MatrixType m_T; /**< \brief Triangular part of Schur decomposition */ + MatrixType m_U; /**< \brief Unitary part of Schur decomposition */ + MatrixType m_fT; /**< \brief %Matrix function applied to #m_T */ + ListOfClusters m_clusters; /**< \brief Partition of eigenvalues into clusters of ei'vals "close" to each other */ + VectorXi m_eivalToCluster; /**< \brief m_eivalToCluster[i] = j means i-th ei'val is in j-th cluster */ + VectorXi m_clusterSize; /**< \brief Number of eigenvalues in each clusters */ + VectorXi m_blockStart; /**< \brief Row index at which block corresponding to i-th cluster starts */ + IntVectorType m_permutation; /**< \brief Permutation which groups ei'vals in the same cluster together */ + + /** \brief Maximum distance allowed between eigenvalues to be considered "close". + * + * This is morally a \c static \c const \c Scalar, but only + * integers can be static constant class members in C++. The + * separation constant is set to 0.01, a value taken from the + * paper by Davies and Higham. */ static const RealScalar separation() { return static_cast(0.01); } - StemFunction *m_f; }; template MatrixFunction::MatrixFunction(const MatrixType& A, StemFunction f, MatrixType* result) : m_f(f) { - if (A.rows() == 1) { - result->resize(1,1); - (*result)(0,0) = f(A(0,0), 0); - } else { - const ComplexSchur schurOfA(A); - MatrixType T = schurOfA.matrixT(); - MatrixType U = schurOfA.matrixU(); - VectorXi blockSize; - separateBlocksInSchur(T, U, blockSize); - MatrixType fT; - computeTriangular(T, fT, blockSize); - *result = U * fT * U.adjoint(); + computeSchurDecomposition(A); + partitionEigenvalues(); + computeClusterSize(); + computeBlockStart(); + constructPermutation(); + permuteSchur(); + computeBlockAtomic(); + computeOffDiagonal(); + *result = m_U * m_fT * m_U.adjoint(); +} + +/** \brief Store the Schur decomposition of \p A in #m_T and #m_U */ +template +void MatrixFunction::computeSchurDecomposition(const MatrixType& A) +{ + const ComplexSchur schurOfA(A); + m_T = schurOfA.matrixT(); + m_U = schurOfA.matrixU(); +} + +/** \brief Partition eigenvalues in clusters of ei'vals close to each other + * + * This function computes #m_clusters. This is a partition of the + * eigenvalues of #m_T in clusters, such that + * # Any eigenvalue in a certain cluster is at most separation() away + * from another eigenvalue in the same cluster. + * # The distance between two eigenvalues in different clusters is + * more than separation(). + * The implementation follows Algorithm 4.1 in the paper of Davies + * and Higham. + */ +template +void MatrixFunction::partitionEigenvalues() +{ + const int rows = m_T.rows(); + VectorType diag = m_T.diagonal(); // contains eigenvalues of A + + for (int i=0; ibegin(), qi->end(), diag(j)) == qi->end()) { + typename ListOfClusters::iterator qj = findCluster(diag(j)); + if (qj == m_clusters.end()) { + qi->push_back(diag(j)); + } else { + qi->insert(qi->end(), qj->begin(), qj->end()); + m_clusters.erase(qj); + } + } + } } } +/** \brief Find cluster in #m_clusters containing some value + * \param[in] key Value to find + * \returns Iterator to cluster containing \c key, or + * \c m_clusters.end() if no cluster in m_clusters contains \c key. + */ template -void MatrixFunction::separateBlocksInSchur(MatrixType& T, MatrixType& U, VectorXi& blockSize) +typename MatrixFunction::ListOfClusters::iterator MatrixFunction::findCluster(Scalar key) { - const VectorType d = T.diagonal(); - listOfLists blocks; - divideInBlocks(d, &blocks); - - IntVectorType permutation; - constructPermutation(d, blocks, blockSize, permutation); - permuteSchur(permutation, T, U); + typename Cluster::iterator j; + for (typename ListOfClusters::iterator i = m_clusters.begin(); i != m_clusters.end(); ++i) { + j = std::find(i->begin(), i->end(), key); + if (j != i->end()) + return i; + } + return m_clusters.end(); } +/** \brief Compute #m_clusterSize and #m_eivalToCluster using #m_clusters */ template -void MatrixFunction::permuteSchur(const IntVectorType& permutation, MatrixType& T, MatrixType& U) +void MatrixFunction::computeClusterSize() { - IntVectorType p = permutation; + const int rows = m_T.rows(); + VectorType diag = m_T.diagonal(); + const int numClusters = m_clusters.size(); + + m_clusterSize.setZero(numClusters); + m_eivalToCluster.resize(rows); + int clusterIndex = 0; + for (typename ListOfClusters::const_iterator cluster = m_clusters.begin(); cluster != m_clusters.end(); ++cluster) { + for (int i = 0; i < diag.rows(); ++i) { + if (std::find(cluster->begin(), cluster->end(), diag(i)) != cluster->end()) { + ++m_clusterSize[clusterIndex]; + m_eivalToCluster[i] = clusterIndex; + } + } + ++clusterIndex; + } +} + +/** \brief Compute #m_blockStart using #m_clusterSize */ +template +void MatrixFunction::computeBlockStart() +{ + m_blockStart.resize(m_clusterSize.rows()); + m_blockStart(0) = 0; + for (int i = 1; i < m_clusterSize.rows(); i++) { + m_blockStart(i) = m_blockStart(i-1) + m_clusterSize(i-1); + } +} + +/** \brief Compute #m_permutation using #m_eivalToCluster and #m_blockStart */ +template +void MatrixFunction::constructPermutation() +{ + VectorXi indexNextEntry = m_blockStart; + m_permutation.resize(m_T.rows()); + for (int i = 0; i < m_T.rows(); i++) { + int cluster = m_eivalToCluster[i]; + m_permutation[i] = indexNextEntry[cluster]; + ++indexNextEntry[cluster]; + } +} + +/** \brief Permute Schur decomposition in #m_U and #m_T according to #m_permutation */ +template +void MatrixFunction::permuteSchur() +{ + IntVectorType p = m_permutation; for (int i = 0; i < p.rows() - 1; i++) { int j; for (j = i; j < p.rows(); j++) { @@ -252,46 +364,70 @@ void MatrixFunction::permuteSchur(const IntVectorType& permutation } ei_assert(p(j) == i); for (int k = j-1; k >= i; k--) { - swapEntriesInSchur(k, T, U); + swapEntriesInSchur(k); std::swap(p.coeffRef(k), p.coeffRef(k+1)); } } } -// swap T(index, index) and T(index+1, index+1) +/** \brief Swap rows \a index and \a index+1 in Schur decomposition in #m_U and #m_T */ template -void MatrixFunction::swapEntriesInSchur(int index, MatrixType& T, MatrixType& U) +void MatrixFunction::swapEntriesInSchur(int index) { PlanarRotation rotation; - rotation.makeGivens(T(index, index+1), T(index+1, index+1) - T(index, index)); - T.applyOnTheLeft(index, index+1, rotation.adjoint()); - T.applyOnTheRight(index, index+1, rotation); - U.applyOnTheRight(index, index+1, rotation); + rotation.makeGivens(m_T(index, index+1), m_T(index+1, index+1) - m_T(index, index)); + m_T.applyOnTheLeft(index, index+1, rotation.adjoint()); + m_T.applyOnTheRight(index, index+1, rotation); + m_U.applyOnTheRight(index, index+1, rotation); } +/** \brief Compute block diagonal part of #m_fT. + * + * This routine computes the matrix function #m_f applied to the block + * diagonal part of #m_T, with the blocking given by #m_blockStart. The + * result is stored in #m_fT. The off-diagonal parts of #m_fT are set + * to zero. + */ template -void MatrixFunction::computeTriangular(const MatrixType& T, MatrixType& result, const VectorXi& blockSize) +void MatrixFunction::computeBlockAtomic() { - MatrixType expT; - ei_matrix_exponential(T, &expT); - computeBlockAtomic(T, result, blockSize); - VectorXi blockStart(blockSize.rows()); - blockStart(0) = 0; - for (int i = 1; i < blockSize.rows(); i++) { - blockStart(i) = blockStart(i-1) + blockSize(i-1); + m_fT.resize(m_T.rows(), m_T.cols()); + m_fT.setZero(); + MatrixFunctionAtomic mfa(m_f); + for (int i = 0; i < m_clusterSize.rows(); ++i) { + block(m_fT, i, i) = mfa.compute(block(m_T, i, i)); } - for (int diagIndex = 1; diagIndex < blockSize.rows(); diagIndex++) { - for (int blockIndex = 0; blockIndex < blockSize.rows() - diagIndex; blockIndex++) { +} + +/** \brief Return block of matrix according to blocking given by #m_blockStart */ +template +Block MatrixFunction::block(const MatrixType& A, int i, int j) +{ + return A.block(m_blockStart(i), m_blockStart(j), m_clusterSize(i), m_clusterSize(j)); +} + +/** \brief Compute part of #m_fT above block diagonal. + * + * This routine assumes that the block diagonal part of #m_fT (which + * equals #m_f applied to #m_T) has already been computed and computes + * the part above the block diagonal. The part below the diagonal is + * zero, because #m_T is upper triangular. + */ +template +void MatrixFunction::computeOffDiagonal() +{ + for (int diagIndex = 1; diagIndex < m_clusterSize.rows(); diagIndex++) { + for (int blockIndex = 0; blockIndex < m_clusterSize.rows() - diagIndex; blockIndex++) { // compute (blockIndex, blockIndex+diagIndex) block - DynMatrixType A = T.block(blockStart(blockIndex), blockStart(blockIndex), blockSize(blockIndex), blockSize(blockIndex)); - DynMatrixType B = -T.block(blockStart(blockIndex+diagIndex), blockStart(blockIndex+diagIndex), blockSize(blockIndex+diagIndex), blockSize(blockIndex+diagIndex)); - DynMatrixType C = result.block(blockStart(blockIndex), blockStart(blockIndex), blockSize(blockIndex), blockSize(blockIndex)) * T.block(blockStart(blockIndex), blockStart(blockIndex+diagIndex), blockSize(blockIndex), blockSize(blockIndex+diagIndex)); - C -= T.block(blockStart(blockIndex), blockStart(blockIndex+diagIndex), blockSize(blockIndex), blockSize(blockIndex+diagIndex)) * result.block(blockStart(blockIndex+diagIndex), blockStart(blockIndex+diagIndex), blockSize(blockIndex+diagIndex), blockSize(blockIndex+diagIndex)); + DynMatrixType A = block(m_T, blockIndex, blockIndex); + DynMatrixType B = -block(m_T, blockIndex+diagIndex, blockIndex+diagIndex); + DynMatrixType C = block(m_fT, blockIndex, blockIndex) * block(m_T, blockIndex, blockIndex+diagIndex); + C -= block(m_T, blockIndex, blockIndex+diagIndex) * block(m_fT, blockIndex+diagIndex, blockIndex+diagIndex); for (int k = blockIndex + 1; k < blockIndex + diagIndex; k++) { - C += result.block(blockStart(blockIndex), blockStart(k), blockSize(blockIndex), blockSize(k)) * T.block(blockStart(k), blockStart(blockIndex+diagIndex), blockSize(k), blockSize(blockIndex+diagIndex)); - C -= T.block(blockStart(blockIndex), blockStart(k), blockSize(blockIndex), blockSize(k)) * result.block(blockStart(k), blockStart(blockIndex+diagIndex), blockSize(k), blockSize(blockIndex+diagIndex)); + C += block(m_fT, blockIndex, k) * block(m_T, k, blockIndex+diagIndex); + C -= block(m_T, blockIndex, k) * block(m_fT, k, blockIndex+diagIndex); } - result.block(blockStart(blockIndex), blockStart(blockIndex+diagIndex), blockSize(blockIndex), blockSize(blockIndex+diagIndex)) = solveTriangularSylvester(A, B, C); + block(m_fT, blockIndex, blockIndex+diagIndex) = solveTriangularSylvester(A, B, C); } } } @@ -364,110 +500,14 @@ typename MatrixFunction::DynMatrixType MatrixFunction -void MatrixFunction::computeBlockAtomic(const MatrixType& T, MatrixType& result, const VectorXi& blockSize) -{ - int blockStart = 0; - result.resize(T.rows(), T.cols()); - result.setZero(); - MatrixFunctionAtomic mfa(m_f); - for (int i = 0; i < blockSize.rows(); i++) { - result.block(blockStart, blockStart, blockSize(i), blockSize(i)) - = mfa.compute(T.block(blockStart, blockStart, blockSize(i), blockSize(i))); - blockStart += blockSize(i); - } -} - -template -typename std::list >::iterator ei_find_in_list_of_lists(typename std::list >& ll, Scalar x) -{ - typename std::list::iterator j; - for (typename std::list >::iterator i = ll.begin(); i != ll.end(); i++) { - j = std::find(i->begin(), i->end(), x); - if (j != i->end()) - return i; - } - return ll.end(); -} - -// Alg 4.1 -template -void MatrixFunction::divideInBlocks(const VectorType& v, listOfLists* result) -{ - const int n = v.rows(); - for (int i=0; iend()) { - listOfScalars l; - l.push_back(v(i)); - result->push_back(l); - qi = result->end(); - qi--; - } - // Look for other element to add to the set - for (int j=i+1; jbegin(), qi->end(), v(j)) == qi->end()) { - typename listOfLists::iterator qj = ei_find_in_list_of_lists(*result, v(j)); - if (qj == result->end()) { - qi->push_back(v(j)); - } else { - qi->insert(qi->end(), qj->begin(), qj->end()); - result->erase(qj); - } - } - } - } -} - -// Construct permutation P, such that P(D) has eigenvalues clustered together -template -void MatrixFunction::constructPermutation(const VectorType& diag, const listOfLists& blocks, - VectorXi& blockSize, IntVectorType& permutation) -{ - const int n = diag.rows(); - const int numBlocks = blocks.size(); - - // For every block in blocks, mark and count the entries in diag that - // appear in that block - blockSize.setZero(numBlocks); - IntVectorType entryToBlock(n); - int blockIndex = 0; - for (typename listOfLists::const_iterator block = blocks.begin(); block != blocks.end(); block++) { - for (int i = 0; i < diag.rows(); i++) { - if (std::find(block->begin(), block->end(), diag(i)) != block->end()) { - blockSize[blockIndex]++; - entryToBlock[i] = blockIndex; - } - } - blockIndex++; - } - - // Compute index of first entry in every block as the sum of sizes - // of all the preceding blocks - VectorXi indexNextEntry(numBlocks); - indexNextEntry[0] = 0; - for (blockIndex = 1; blockIndex < numBlocks; blockIndex++) { - indexNextEntry[blockIndex] = indexNextEntry[blockIndex-1] + blockSize[blockIndex-1]; - } - - // Construct permutation - permutation.resize(n); - for (int i = 0; i < n; i++) { - int block = entryToBlock[i]; - permutation[i] = indexNextEntry[block]; - indexNextEntry[block]++; - } -} - template EIGEN_STRONG_INLINE void ei_matrix_function(const MatrixBase& M, typename ei_stem_function::Scalar>::type f, typename MatrixBase::PlainMatrixType* result) { ei_assert(M.rows() == M.cols()); - MatrixFunction::PlainMatrixType>(M, f, result); + typedef typename MatrixBase::PlainMatrixType PlainMatrixType; + MatrixFunction(M, f, result); } #endif // EIGEN_MATRIX_FUNCTION