MatrixFunction.h
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2009-2011 Jitse Niesen <jitse@maths.leeds.ac.uk>
00005 //
00006 // This Source Code Form is subject to the terms of the Mozilla
00007 // Public License v. 2.0. If a copy of the MPL was not distributed
00008 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
00009 
00010 #ifndef EIGEN_MATRIX_FUNCTION
00011 #define EIGEN_MATRIX_FUNCTION
00012 
00013 #include "StemFunction.h"
00014 #include "MatrixFunctionAtomic.h"
00015 
00016 
00017 namespace Eigen { 
00018 
00034 template <typename MatrixType, 
00035           typename AtomicType,  
00036           int IsComplex = NumTraits<typename internal::traits<MatrixType>::Scalar>::IsComplex>
00037 class MatrixFunction
00038 {  
00039   public:
00040 
00049     MatrixFunction(const MatrixType& A, AtomicType& atomic);
00050 
00059     template <typename ResultType> 
00060     void compute(ResultType &result);    
00061 };
00062 
00063 
00067 template <typename MatrixType, typename AtomicType>
00068 class MatrixFunction<MatrixType, AtomicType, 0>
00069 {  
00070   private:
00071 
00072     typedef internal::traits<MatrixType> Traits;
00073     typedef typename Traits::Scalar Scalar;
00074     static const int Rows = Traits::RowsAtCompileTime;
00075     static const int Cols = Traits::ColsAtCompileTime;
00076     static const int Options = MatrixType::Options;
00077     static const int MaxRows = Traits::MaxRowsAtCompileTime;
00078     static const int MaxCols = Traits::MaxColsAtCompileTime;
00079 
00080     typedef std::complex<Scalar> ComplexScalar;
00081     typedef Matrix<ComplexScalar, Rows, Cols, Options, MaxRows, MaxCols> ComplexMatrix;
00082 
00083   public:
00084 
00090     MatrixFunction(const MatrixType& A, AtomicType& atomic) : m_A(A), m_atomic(atomic) { }
00091 
00101     template <typename ResultType>
00102     void compute(ResultType& result) 
00103     {
00104       ComplexMatrix CA = m_A.template cast<ComplexScalar>();
00105       ComplexMatrix Cresult;
00106       MatrixFunction<ComplexMatrix, AtomicType> mf(CA, m_atomic);
00107       mf.compute(Cresult);
00108       result = Cresult.real();
00109     }
00110 
00111   private:
00112     typename internal::nested<MatrixType>::type m_A; 
00113     AtomicType& m_atomic; 
00115     MatrixFunction& operator=(const MatrixFunction&);
00116 };
00117 
00118       
00122 template <typename MatrixType, typename AtomicType>
00123 class MatrixFunction<MatrixType, AtomicType, 1>
00124 {
00125   private:
00126 
00127     typedef internal::traits<MatrixType> Traits;
00128     typedef typename MatrixType::Scalar Scalar;
00129     typedef typename MatrixType::Index Index;
00130     static const int RowsAtCompileTime = Traits::RowsAtCompileTime;
00131     static const int ColsAtCompileTime = Traits::ColsAtCompileTime;
00132     static const int Options = MatrixType::Options;
00133     typedef typename NumTraits<Scalar>::Real RealScalar;
00134     typedef Matrix<Scalar, Traits::RowsAtCompileTime, 1> VectorType;
00135     typedef Matrix<Index, Traits::RowsAtCompileTime, 1> IntVectorType;
00136     typedef Matrix<Index, Dynamic, 1> DynamicIntVectorType;
00137     typedef std::list<Scalar> Cluster;
00138     typedef std::list<Cluster> ListOfClusters;
00139     typedef Matrix<Scalar, Dynamic, Dynamic, Options, RowsAtCompileTime, ColsAtCompileTime> DynMatrixType;
00140 
00141   public:
00142 
00143     MatrixFunction(const MatrixType& A, AtomicType& atomic);
00144     template <typename ResultType> void compute(ResultType& result);
00145 
00146   private:
00147 
00148     void computeSchurDecomposition();
00149     void partitionEigenvalues();
00150     typename ListOfClusters::iterator findCluster(Scalar key);
00151     void computeClusterSize();
00152     void computeBlockStart();
00153     void constructPermutation();
00154     void permuteSchur();
00155     void swapEntriesInSchur(Index index);
00156     void computeBlockAtomic();
00157     Block<MatrixType> block(MatrixType& A, Index i, Index j);
00158     void computeOffDiagonal();
00159     DynMatrixType solveTriangularSylvester(const DynMatrixType& A, const DynMatrixType& B, const DynMatrixType& C);
00160 
00161     typename internal::nested<MatrixType>::type m_A; 
00162     AtomicType& m_atomic; 
00163     MatrixType m_T; 
00164     MatrixType m_U; 
00165     MatrixType m_fT; 
00166     ListOfClusters m_clusters; 
00167     DynamicIntVectorType m_eivalToCluster; 
00168     DynamicIntVectorType m_clusterSize; 
00169     DynamicIntVectorType m_blockStart; 
00170     IntVectorType m_permutation; 
00178     static const RealScalar separation() { return static_cast<RealScalar>(0.1); }
00179 
00180     MatrixFunction& operator=(const MatrixFunction&);
00181 };
00182 
00188 template <typename MatrixType, typename AtomicType>
00189 MatrixFunction<MatrixType,AtomicType,1>::MatrixFunction(const MatrixType& A, AtomicType& atomic)
00190   : m_A(A), m_atomic(atomic)
00191 {
00192   /* empty body */
00193 }
00194 
00200 template <typename MatrixType, typename AtomicType>
00201 template <typename ResultType>
00202 void MatrixFunction<MatrixType,AtomicType,1>::compute(ResultType& result) 
00203 {
00204   computeSchurDecomposition();
00205   partitionEigenvalues();
00206   computeClusterSize();
00207   computeBlockStart();
00208   constructPermutation();
00209   permuteSchur();
00210   computeBlockAtomic();
00211   computeOffDiagonal();
00212   result = m_U * m_fT * m_U.adjoint();
00213 }
00214 
00216 template <typename MatrixType, typename AtomicType>
00217 void MatrixFunction<MatrixType,AtomicType,1>::computeSchurDecomposition()
00218 {
00219   const ComplexSchur<MatrixType> schurOfA(m_A);  
00220   m_T = schurOfA.matrixT();
00221   m_U = schurOfA.matrixU();
00222 }
00223 
00235 template <typename MatrixType, typename AtomicType>
00236 void MatrixFunction<MatrixType,AtomicType,1>::partitionEigenvalues()
00237 {
00238   const Index rows = m_T.rows();
00239   VectorType diag = m_T.diagonal(); // contains eigenvalues of A
00240 
00241   for (Index i=0; i<rows; ++i) {
00242     // Find set containing diag(i), adding a new set if necessary
00243     typename ListOfClusters::iterator qi = findCluster(diag(i));
00244     if (qi == m_clusters.end()) {
00245       Cluster l;
00246       l.push_back(diag(i));
00247       m_clusters.push_back(l);
00248       qi = m_clusters.end();
00249       --qi;
00250     }
00251 
00252     // Look for other element to add to the set
00253     for (Index j=i+1; j<rows; ++j) {
00254       if (internal::abs(diag(j) - diag(i)) <= separation() && std::find(qi->begin(), qi->end(), diag(j)) == qi->end()) {
00255         typename ListOfClusters::iterator qj = findCluster(diag(j));
00256         if (qj == m_clusters.end()) {
00257           qi->push_back(diag(j));
00258         } else {
00259           qi->insert(qi->end(), qj->begin(), qj->end());
00260           m_clusters.erase(qj);
00261         }
00262       }
00263     }
00264   }
00265 }
00266 
00272 template <typename MatrixType, typename AtomicType>
00273 typename MatrixFunction<MatrixType,AtomicType,1>::ListOfClusters::iterator MatrixFunction<MatrixType,AtomicType,1>::findCluster(Scalar key)
00274 {
00275   typename Cluster::iterator j;
00276   for (typename ListOfClusters::iterator i = m_clusters.begin(); i != m_clusters.end(); ++i) {
00277     j = std::find(i->begin(), i->end(), key);
00278     if (j != i->end())
00279       return i;
00280   }
00281   return m_clusters.end();
00282 }
00283 
00285 template <typename MatrixType, typename AtomicType>
00286 void MatrixFunction<MatrixType,AtomicType,1>::computeClusterSize()
00287 {
00288   const Index rows = m_T.rows();
00289   VectorType diag = m_T.diagonal(); 
00290   const Index numClusters = static_cast<Index>(m_clusters.size());
00291 
00292   m_clusterSize.setZero(numClusters);
00293   m_eivalToCluster.resize(rows);
00294   Index clusterIndex = 0;
00295   for (typename ListOfClusters::const_iterator cluster = m_clusters.begin(); cluster != m_clusters.end(); ++cluster) {
00296     for (Index i = 0; i < diag.rows(); ++i) {
00297       if (std::find(cluster->begin(), cluster->end(), diag(i)) != cluster->end()) {
00298         ++m_clusterSize[clusterIndex];
00299         m_eivalToCluster[i] = clusterIndex;
00300       }
00301     }
00302     ++clusterIndex;
00303   }
00304 }
00305 
00307 template <typename MatrixType, typename AtomicType>
00308 void MatrixFunction<MatrixType,AtomicType,1>::computeBlockStart()
00309 {
00310   m_blockStart.resize(m_clusterSize.rows());
00311   m_blockStart(0) = 0;
00312   for (Index i = 1; i < m_clusterSize.rows(); i++) {
00313     m_blockStart(i) = m_blockStart(i-1) + m_clusterSize(i-1);
00314   }
00315 }
00316 
00318 template <typename MatrixType, typename AtomicType>
00319 void MatrixFunction<MatrixType,AtomicType,1>::constructPermutation()
00320 {
00321   DynamicIntVectorType indexNextEntry = m_blockStart;
00322   m_permutation.resize(m_T.rows());
00323   for (Index i = 0; i < m_T.rows(); i++) {
00324     Index cluster = m_eivalToCluster[i];
00325     m_permutation[i] = indexNextEntry[cluster];
00326     ++indexNextEntry[cluster];
00327   }
00328 }  
00329 
00331 template <typename MatrixType, typename AtomicType>
00332 void MatrixFunction<MatrixType,AtomicType,1>::permuteSchur()
00333 {
00334   IntVectorType p = m_permutation;
00335   for (Index i = 0; i < p.rows() - 1; i++) {
00336     Index j;
00337     for (j = i; j < p.rows(); j++) {
00338       if (p(j) == i) break;
00339     }
00340     eigen_assert(p(j) == i);
00341     for (Index k = j-1; k >= i; k--) {
00342       swapEntriesInSchur(k);
00343       std::swap(p.coeffRef(k), p.coeffRef(k+1));
00344     }
00345   }
00346 }
00347 
00349 template <typename MatrixType, typename AtomicType>
00350 void MatrixFunction<MatrixType,AtomicType,1>::swapEntriesInSchur(Index index)
00351 {
00352   JacobiRotation<Scalar> rotation;
00353   rotation.makeGivens(m_T(index, index+1), m_T(index+1, index+1) - m_T(index, index));
00354   m_T.applyOnTheLeft(index, index+1, rotation.adjoint());
00355   m_T.applyOnTheRight(index, index+1, rotation);
00356   m_U.applyOnTheRight(index, index+1, rotation);
00357 }  
00358 
00365 template <typename MatrixType, typename AtomicType>
00366 void MatrixFunction<MatrixType,AtomicType,1>::computeBlockAtomic()
00367 { 
00368   m_fT.resize(m_T.rows(), m_T.cols());
00369   m_fT.setZero();
00370   for (Index i = 0; i < m_clusterSize.rows(); ++i) {
00371     block(m_fT, i, i) = m_atomic.compute(block(m_T, i, i));
00372   }
00373 }
00374 
00376 template <typename MatrixType, typename AtomicType>
00377 Block<MatrixType> MatrixFunction<MatrixType,AtomicType,1>::block(MatrixType& A, Index i, Index j)
00378 {
00379   return A.block(m_blockStart(i), m_blockStart(j), m_clusterSize(i), m_clusterSize(j));
00380 }
00381 
00389 template <typename MatrixType, typename AtomicType>
00390 void MatrixFunction<MatrixType,AtomicType,1>::computeOffDiagonal()
00391 { 
00392   for (Index diagIndex = 1; diagIndex < m_clusterSize.rows(); diagIndex++) {
00393     for (Index blockIndex = 0; blockIndex < m_clusterSize.rows() - diagIndex; blockIndex++) {
00394       // compute (blockIndex, blockIndex+diagIndex) block
00395       DynMatrixType A = block(m_T, blockIndex, blockIndex);
00396       DynMatrixType B = -block(m_T, blockIndex+diagIndex, blockIndex+diagIndex);
00397       DynMatrixType C = block(m_fT, blockIndex, blockIndex) * block(m_T, blockIndex, blockIndex+diagIndex);
00398       C -= block(m_T, blockIndex, blockIndex+diagIndex) * block(m_fT, blockIndex+diagIndex, blockIndex+diagIndex);
00399       for (Index k = blockIndex + 1; k < blockIndex + diagIndex; k++) {
00400         C += block(m_fT, blockIndex, k) * block(m_T, k, blockIndex+diagIndex);
00401         C -= block(m_T, blockIndex, k) * block(m_fT, k, blockIndex+diagIndex);
00402       }
00403       block(m_fT, blockIndex, blockIndex+diagIndex) = solveTriangularSylvester(A, B, C);
00404     }
00405   }
00406 }
00407 
00431 template <typename MatrixType, typename AtomicType>
00432 typename MatrixFunction<MatrixType,AtomicType,1>::DynMatrixType MatrixFunction<MatrixType,AtomicType,1>::solveTriangularSylvester(
00433   const DynMatrixType& A, 
00434   const DynMatrixType& B, 
00435   const DynMatrixType& C)
00436 {
00437   eigen_assert(A.rows() == A.cols());
00438   eigen_assert(A.isUpperTriangular());
00439   eigen_assert(B.rows() == B.cols());
00440   eigen_assert(B.isUpperTriangular());
00441   eigen_assert(C.rows() == A.rows());
00442   eigen_assert(C.cols() == B.rows());
00443 
00444   Index m = A.rows();
00445   Index n = B.rows();
00446   DynMatrixType X(m, n);
00447 
00448   for (Index i = m - 1; i >= 0; --i) {
00449     for (Index j = 0; j < n; ++j) {
00450 
00451       // Compute AX = \sum_{k=i+1}^m A_{ik} X_{kj}
00452       Scalar AX;
00453       if (i == m - 1) {
00454         AX = 0; 
00455       } else {
00456         Matrix<Scalar,1,1> AXmatrix = A.row(i).tail(m-1-i) * X.col(j).tail(m-1-i);
00457         AX = AXmatrix(0,0);
00458       }
00459 
00460       // Compute XB = \sum_{k=1}^{j-1} X_{ik} B_{kj}
00461       Scalar XB;
00462       if (j == 0) {
00463         XB = 0; 
00464       } else {
00465         Matrix<Scalar,1,1> XBmatrix = X.row(i).head(j) * B.col(j).head(j);
00466         XB = XBmatrix(0,0);
00467       }
00468 
00469       X(i,j) = (C(i,j) - AX - XB) / (A(i,i) + B(j,j));
00470     }
00471   }
00472   return X;
00473 }
00474 
00487 template<typename Derived> class MatrixFunctionReturnValue
00488 : public ReturnByValue<MatrixFunctionReturnValue<Derived> >
00489 {
00490   public:
00491 
00492     typedef typename Derived::Scalar Scalar;
00493     typedef typename Derived::Index Index;
00494     typedef typename internal::stem_function<Scalar>::type StemFunction;
00495 
00502     MatrixFunctionReturnValue(const Derived& A, StemFunction f) : m_A(A), m_f(f) { }
00503 
00509     template <typename ResultType>
00510     inline void evalTo(ResultType& result) const
00511     {
00512       typedef typename Derived::PlainObject PlainObject;
00513       typedef internal::traits<PlainObject> Traits;
00514       static const int RowsAtCompileTime = Traits::RowsAtCompileTime;
00515       static const int ColsAtCompileTime = Traits::ColsAtCompileTime;
00516       static const int Options = PlainObject::Options;
00517       typedef std::complex<typename NumTraits<Scalar>::Real> ComplexScalar;
00518       typedef Matrix<ComplexScalar, Dynamic, Dynamic, Options, RowsAtCompileTime, ColsAtCompileTime> DynMatrixType;
00519       typedef MatrixFunctionAtomic<DynMatrixType> AtomicType;
00520       AtomicType atomic(m_f);
00521 
00522       const PlainObject Aevaluated = m_A.eval();
00523       MatrixFunction<PlainObject, AtomicType> mf(Aevaluated, atomic);
00524       mf.compute(result);
00525     }
00526 
00527     Index rows() const { return m_A.rows(); }
00528     Index cols() const { return m_A.cols(); }
00529 
00530   private:
00531     typename internal::nested<Derived>::type m_A;
00532     StemFunction *m_f;
00533 
00534     MatrixFunctionReturnValue& operator=(const MatrixFunctionReturnValue&);
00535 };
00536 
00537 namespace internal {
00538 template<typename Derived>
00539 struct traits<MatrixFunctionReturnValue<Derived> >
00540 {
00541   typedef typename Derived::PlainObject ReturnType;
00542 };
00543 }
00544 
00545 
00546 /********** MatrixBase methods **********/
00547 
00548 
00549 template <typename Derived>
00550 const MatrixFunctionReturnValue<Derived> MatrixBase<Derived>::matrixFunction(typename internal::stem_function<typename internal::traits<Derived>::Scalar>::type f) const
00551 {
00552   eigen_assert(rows() == cols());
00553   return MatrixFunctionReturnValue<Derived>(derived(), f);
00554 }
00555 
00556 template <typename Derived>
00557 const MatrixFunctionReturnValue<Derived> MatrixBase<Derived>::sin() const
00558 {
00559   eigen_assert(rows() == cols());
00560   typedef typename internal::stem_function<Scalar>::ComplexScalar ComplexScalar;
00561   return MatrixFunctionReturnValue<Derived>(derived(), StdStemFunctions<ComplexScalar>::sin);
00562 }
00563 
00564 template <typename Derived>
00565 const MatrixFunctionReturnValue<Derived> MatrixBase<Derived>::cos() const
00566 {
00567   eigen_assert(rows() == cols());
00568   typedef typename internal::stem_function<Scalar>::ComplexScalar ComplexScalar;
00569   return MatrixFunctionReturnValue<Derived>(derived(), StdStemFunctions<ComplexScalar>::cos);
00570 }
00571 
00572 template <typename Derived>
00573 const MatrixFunctionReturnValue<Derived> MatrixBase<Derived>::sinh() const
00574 {
00575   eigen_assert(rows() == cols());
00576   typedef typename internal::stem_function<Scalar>::ComplexScalar ComplexScalar;
00577   return MatrixFunctionReturnValue<Derived>(derived(), StdStemFunctions<ComplexScalar>::sinh);
00578 }
00579 
00580 template <typename Derived>
00581 const MatrixFunctionReturnValue<Derived> MatrixBase<Derived>::cosh() const
00582 {
00583   eigen_assert(rows() == cols());
00584   typedef typename internal::stem_function<Scalar>::ComplexScalar ComplexScalar;
00585   return MatrixFunctionReturnValue<Derived>(derived(), StdStemFunctions<ComplexScalar>::cosh);
00586 }
00587 
00588 } // end namespace Eigen
00589 
00590 #endif // EIGEN_MATRIX_FUNCTION


win_eigen
Author(s): Daniel Stonier
autogenerated on Mon Oct 6 2014 12:25:19