10 #ifndef EIGEN_MATRIX_FUNCTION_H
11 #define EIGEN_MATRIX_FUNCTION_H
13 #include "StemFunction.h"
16 #include "./InternalHeaderCheck.h"
23 static const float matrix_function_separation = 0.1f;
31 template <
typename MatrixType>
32 class MatrixFunctionAtomic
36 typedef typename MatrixType::Scalar Scalar;
37 typedef typename stem_function<Scalar>::type StemFunction;
42 MatrixFunctionAtomic(StemFunction f) : m_f(f) { }
48 MatrixType compute(
const MatrixType& A);
54 template <
typename MatrixType>
55 typename NumTraits<typename MatrixType::Scalar>::Real matrix_function_compute_mu(
const MatrixType& A)
57 typedef typename plain_col_type<MatrixType>::type VectorType;
58 Index rows = A.rows();
59 const MatrixType N = MatrixType::Identity(rows, rows) - A;
60 VectorType e = VectorType::Ones(rows);
61 N.template triangularView<Upper>().solveInPlace(e);
62 return e.cwiseAbs().maxCoeff();
65 template <
typename MatrixType>
66 MatrixType MatrixFunctionAtomic<MatrixType>::compute(
const MatrixType& A)
69 typedef typename NumTraits<Scalar>::Real RealScalar;
70 Index rows = A.rows();
71 Scalar avgEival = A.trace() / Scalar(RealScalar(rows));
72 MatrixType Ashifted = A - avgEival * MatrixType::Identity(rows, rows);
73 RealScalar mu = matrix_function_compute_mu(Ashifted);
74 MatrixType F = m_f(avgEival, 0) * MatrixType::Identity(rows, rows);
75 MatrixType P = Ashifted;
77 for (
Index s = 1; double(s) < 1.1 * double(rows) + 10.0; s++) {
78 Fincr = m_f(avgEival,
static_cast<int>(s)) * P;
80 P = Scalar(RealScalar(1)/RealScalar(s + 1)) * P * Ashifted;
83 const RealScalar F_norm = F.cwiseAbs().rowwise().sum().maxCoeff();
84 const RealScalar Fincr_norm = Fincr.cwiseAbs().rowwise().sum().maxCoeff();
85 if (Fincr_norm < NumTraits<Scalar>::epsilon() * F_norm) {
87 RealScalar rfactorial = 1;
88 for (
Index r = 0; r < rows; r++) {
90 for (
Index i = 0; i < rows; i++)
91 mx = (std::max)(mx, std::abs(m_f(Ashifted(i, i) + avgEival,
static_cast<int>(s+r))));
93 rfactorial *= RealScalar(r);
94 delta = (std::max)(delta, mx / rfactorial);
96 const RealScalar P_norm = P.cwiseAbs().rowwise().sum().maxCoeff();
97 if (mu * delta * P_norm < NumTraits<Scalar>::epsilon() * F_norm)
109 template <
typename Index,
typename ListOfClusters>
110 typename ListOfClusters::iterator matrix_function_find_cluster(
Index key, ListOfClusters& clusters)
112 typename std::list<Index>::iterator j;
113 for (
typename ListOfClusters::iterator i = clusters.begin(); i != clusters.end(); ++i) {
114 j = std::find(i->begin(), i->end(), key);
118 return clusters.end();
132 template <
typename EivalsType,
typename Cluster>
133 void matrix_function_partition_eigenvalues(
const EivalsType& eivals, std::list<Cluster>& clusters)
135 typedef typename EivalsType::RealScalar RealScalar;
136 for (
Index i=0; i<eivals.rows(); ++i) {
138 typename std::list<Cluster>::iterator qi = matrix_function_find_cluster(i, clusters);
139 if (qi == clusters.end()) {
142 clusters.push_back(l);
148 for (
Index j=i+1; j<eivals.rows(); ++j) {
149 if (
abs(eivals(j) - eivals(i)) <= RealScalar(matrix_function_separation)
150 && std::find(qi->begin(), qi->end(), j) == qi->end()) {
151 typename std::list<Cluster>::iterator qj = matrix_function_find_cluster(j, clusters);
152 if (qj == clusters.end()) {
155 qi->insert(qi->end(), qj->begin(), qj->end());
164 template <
typename ListOfClusters,
typename Index>
165 void matrix_function_compute_cluster_size(
const ListOfClusters& clusters, Matrix<Index, Dynamic, 1>& clusterSize)
167 const Index numClusters =
static_cast<Index>(clusters.size());
168 clusterSize.setZero(numClusters);
169 Index clusterIndex = 0;
170 for (
typename ListOfClusters::const_iterator cluster = clusters.begin(); cluster != clusters.end(); ++cluster) {
171 clusterSize[clusterIndex] = cluster->size();
177 template <
typename VectorType>
178 void matrix_function_compute_block_start(
const VectorType& clusterSize, VectorType& blockStart)
180 blockStart.resize(clusterSize.rows());
182 for (
Index i = 1; i < clusterSize.rows(); i++) {
183 blockStart(i) = blockStart(i-1) + clusterSize(i-1);
188 template <
typename EivalsType,
typename ListOfClusters,
typename VectorType>
189 void matrix_function_compute_map(
const EivalsType& eivals,
const ListOfClusters& clusters, VectorType& eivalToCluster)
191 eivalToCluster.resize(eivals.rows());
192 Index clusterIndex = 0;
193 for (
typename ListOfClusters::const_iterator cluster = clusters.begin(); cluster != clusters.end(); ++cluster) {
194 for (
Index i = 0; i < eivals.rows(); ++i) {
195 if (std::find(cluster->begin(), cluster->end(), i) != cluster->end()) {
196 eivalToCluster[i] = clusterIndex;
204 template <
typename DynVectorType,
typename VectorType>
205 void matrix_function_compute_permutation(
const DynVectorType& blockStart,
const DynVectorType& eivalToCluster, VectorType& permutation)
207 DynVectorType indexNextEntry = blockStart;
208 permutation.resize(eivalToCluster.rows());
209 for (
Index i = 0; i < eivalToCluster.rows(); i++) {
210 Index cluster = eivalToCluster[i];
211 permutation[i] = indexNextEntry[cluster];
212 ++indexNextEntry[cluster];
217 template <
typename VectorType,
typename MatrixType>
218 void matrix_function_permute_schur(VectorType& permutation, MatrixType& U, MatrixType& T)
220 for (
Index i = 0; i < permutation.rows() - 1; i++) {
222 for (j = i; j < permutation.rows(); j++) {
223 if (permutation(j) == i)
break;
225 eigen_assert(permutation(j) == i);
226 for (
Index k = j-1; k >= i; k--) {
227 JacobiRotation<typename MatrixType::Scalar> rotation;
228 rotation.makeGivens(T(k, k+1), T(k+1, k+1) - T(k, k));
229 T.applyOnTheLeft(k, k+1, rotation.adjoint());
230 T.applyOnTheRight(k, k+1, rotation);
231 U.applyOnTheRight(k, k+1, rotation);
232 std::swap(permutation.coeffRef(k), permutation.coeffRef(k+1));
243 template <
typename MatrixType,
typename AtomicType,
typename VectorType>
244 void matrix_function_compute_block_atomic(
const MatrixType& T, AtomicType& atomic,
const VectorType& blockStart,
const VectorType& clusterSize, MatrixType& fT)
246 fT.setZero(T.rows(), T.cols());
247 for (
Index i = 0; i < clusterSize.rows(); ++i) {
248 fT.block(blockStart(i), blockStart(i), clusterSize(i), clusterSize(i))
249 = atomic.compute(T.block(blockStart(i), blockStart(i), clusterSize(i), clusterSize(i)));
275 template <
typename MatrixType>
276 MatrixType matrix_function_solve_triangular_sylvester(
const MatrixType& A,
const MatrixType& B,
const MatrixType& C)
278 eigen_assert(A.rows() == A.cols());
279 eigen_assert(A.isUpperTriangular());
280 eigen_assert(B.rows() == B.cols());
281 eigen_assert(B.isUpperTriangular());
282 eigen_assert(C.rows() == A.rows());
283 eigen_assert(C.cols() == B.rows());
285 typedef typename MatrixType::Scalar Scalar;
291 for (
Index i = m - 1; i >= 0; --i) {
292 for (
Index j = 0; j < n; ++j) {
299 Matrix<Scalar,1,1> AXmatrix = A.row(i).tail(m-1-i) * X.col(j).tail(m-1-i);
308 Matrix<Scalar,1,1> XBmatrix = X.row(i).head(j) * B.col(j).head(j);
312 X(i,j) = (C(i,j) - AX - XB) / (A(i,i) + B(j,j));
324 template <
typename MatrixType,
typename VectorType>
325 void matrix_function_compute_above_diagonal(
const MatrixType& T,
const VectorType& blockStart,
const VectorType& clusterSize, MatrixType& fT)
327 typedef internal::traits<MatrixType> Traits;
328 typedef typename MatrixType::Scalar Scalar;
329 static const int Options = MatrixType::Options;
330 typedef Matrix<Scalar, Dynamic, Dynamic, Options, Traits::RowsAtCompileTime, Traits::ColsAtCompileTime> DynMatrixType;
332 for (
Index k = 1; k < clusterSize.rows(); k++) {
333 for (
Index i = 0; i < clusterSize.rows() - k; i++) {
335 DynMatrixType A = T.block(blockStart(i), blockStart(i), clusterSize(i), clusterSize(i));
336 DynMatrixType B = -T.block(blockStart(i+k), blockStart(i+k), clusterSize(i+k), clusterSize(i+k));
337 DynMatrixType C = fT.block(blockStart(i), blockStart(i), clusterSize(i), clusterSize(i))
338 * T.block(blockStart(i), blockStart(i+k), clusterSize(i), clusterSize(i+k));
339 C -= T.block(blockStart(i), blockStart(i+k), clusterSize(i), clusterSize(i+k))
340 * fT.block(blockStart(i+k), blockStart(i+k), clusterSize(i+k), clusterSize(i+k));
341 for (
Index m = i + 1; m < i + k; m++) {
342 C += fT.block(blockStart(i), blockStart(m), clusterSize(i), clusterSize(m))
343 * T.block(blockStart(m), blockStart(i+k), clusterSize(m), clusterSize(i+k));
344 C -= T.block(blockStart(i), blockStart(m), clusterSize(i), clusterSize(m))
345 * fT.block(blockStart(m), blockStart(i+k), clusterSize(m), clusterSize(i+k));
347 fT.block(blockStart(i), blockStart(i+k), clusterSize(i), clusterSize(i+k))
348 = matrix_function_solve_triangular_sylvester(A, B, C);
368 template <typename MatrixType, int IsComplex = NumTraits<typename internal::traits<MatrixType>::Scalar>::IsComplex>
369 struct matrix_function_compute
381 template <
typename AtomicType,
typename ResultType>
382 static void run(
const MatrixType& A, AtomicType& atomic, ResultType &result);
391 template <
typename MatrixType>
392 struct matrix_function_compute<MatrixType, 0>
394 template <
typename MatA,
typename AtomicType,
typename ResultType>
395 static void run(
const MatA& A, AtomicType& atomic, ResultType &result)
397 typedef internal::traits<MatrixType> Traits;
398 typedef typename Traits::Scalar Scalar;
399 static const int Rows = Traits::RowsAtCompileTime, Cols = Traits::ColsAtCompileTime;
400 static const int MaxRows = Traits::MaxRowsAtCompileTime, MaxCols = Traits::MaxColsAtCompileTime;
402 typedef std::complex<Scalar> ComplexScalar;
403 typedef Matrix<ComplexScalar, Rows, Cols, 0, MaxRows, MaxCols> ComplexMatrix;
405 ComplexMatrix CA = A.template cast<ComplexScalar>();
406 ComplexMatrix Cresult;
407 matrix_function_compute<ComplexMatrix>::run(CA, atomic, Cresult);
408 result = Cresult.real();
415 template <
typename MatrixType>
416 struct matrix_function_compute<MatrixType, 1>
418 template <
typename MatA,
typename AtomicType,
typename ResultType>
419 static void run(
const MatA& A, AtomicType& atomic, ResultType &result)
421 typedef internal::traits<MatrixType> Traits;
425 eigen_assert(schurOfA.info()==
Success);
426 MatrixType T = schurOfA.matrixT();
427 MatrixType U = schurOfA.matrixU();
430 std::list<std::list<Index> > clusters;
431 matrix_function_partition_eigenvalues(T.diagonal(), clusters);
434 Matrix<Index, Dynamic, 1> clusterSize;
435 matrix_function_compute_cluster_size(clusters, clusterSize);
438 Matrix<Index, Dynamic, 1> blockStart;
439 matrix_function_compute_block_start(clusterSize, blockStart);
442 Matrix<Index, Dynamic, 1> eivalToCluster;
443 matrix_function_compute_map(T.diagonal(), clusters, eivalToCluster);
446 Matrix<Index, Traits::RowsAtCompileTime, 1> permutation;
447 matrix_function_compute_permutation(blockStart, eivalToCluster, permutation);
450 matrix_function_permute_schur(permutation, U, T);
454 matrix_function_compute_block_atomic(T, atomic, blockStart, clusterSize, fT);
455 matrix_function_compute_above_diagonal(T, blockStart, clusterSize, fT);
456 result = U * (fT.template triangularView<Upper>() * U.adjoint());
473 :
public ReturnByValue<MatrixFunctionReturnValue<Derived> >
476 typedef typename Derived::Scalar Scalar;
477 typedef typename internal::stem_function<Scalar>::type StemFunction;
480 typedef typename internal::ref_selector<Derived>::type DerivedNested;
495 template <
typename ResultType>
496 inline void evalTo(ResultType& result)
const
498 typedef typename internal::nested_eval<Derived, 10>::type NestedEvalType;
499 typedef internal::remove_all_t<NestedEvalType> NestedEvalTypeClean;
500 typedef internal::traits<NestedEvalTypeClean> Traits;
501 typedef std::complex<typename NumTraits<Scalar>::Real> ComplexScalar;
504 typedef internal::MatrixFunctionAtomic<DynMatrixType> AtomicType;
505 AtomicType atomic(m_f);
507 internal::matrix_function_compute<typename NestedEvalTypeClean::PlainObject>::run(m_A, atomic, result);
510 Index rows()
const {
return m_A.rows(); }
511 Index cols()
const {
return m_A.cols(); }
514 const DerivedNested m_A;
519 template<
typename Derived>
520 struct traits<MatrixFunctionReturnValue<Derived> >
522 typedef typename Derived::PlainObject ReturnType;
530 template <
typename Derived>
533 eigen_assert(rows() == cols());
534 return MatrixFunctionReturnValue<Derived>(derived(), f);
537 template <
typename Derived>
540 eigen_assert(rows() == cols());
541 typedef typename internal::stem_function<Scalar>::ComplexScalar ComplexScalar;
542 return MatrixFunctionReturnValue<Derived>(derived(), internal::stem_function_sin<ComplexScalar>);
545 template <
typename Derived>
548 eigen_assert(rows() == cols());
549 typedef typename internal::stem_function<Scalar>::ComplexScalar ComplexScalar;
550 return MatrixFunctionReturnValue<Derived>(derived(), internal::stem_function_cos<ComplexScalar>);
553 template <
typename Derived>
556 eigen_assert(rows() == cols());
557 typedef typename internal::stem_function<Scalar>::ComplexScalar ComplexScalar;
558 return MatrixFunctionReturnValue<Derived>(derived(), internal::stem_function_sinh<ComplexScalar>);
561 template <
typename Derived>
564 eigen_assert(rows() == cols());
565 typedef typename internal::stem_function<Scalar>::ComplexScalar ComplexScalar;
566 return MatrixFunctionReturnValue<Derived>(derived(), internal::stem_function_cosh<ComplexScalar>);
const MatrixFunctionReturnValue< Derived > sin() const
Definition: MatrixFunction.h:538
const MatrixFunctionReturnValue< Derived > matrixFunction(StemFunction f) const
Definition: MatrixFunction.h:531
const MatrixFunctionReturnValue< Derived > cos() const
Definition: MatrixFunction.h:546
const MatrixFunctionReturnValue< Derived > cosh() const
Definition: MatrixFunction.h:562
const MatrixFunctionReturnValue< Derived > sinh() const
Definition: MatrixFunction.h:554
Proxy for the matrix function of some matrix (expression).
Definition: MatrixFunction.h:474
void evalTo(ResultType &result) const
Compute the matrix function.
Definition: MatrixFunction.h:496
MatrixFunctionReturnValue(const Derived &A, StemFunction f)
Constructor.
Definition: MatrixFunction.h:489
Namespace containing all symbols from the Eigen library.
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_abs_op< typename Derived::Scalar >, const Derived > abs(const Eigen::ArrayBase< Derived > &x)