Package lib :: Package linear_algebra :: Module matrix_exponential
[hide private]
[frames] | no frames]

Source Code for Module lib.linear_algebra.matrix_exponential

 1  ############################################################################### 
 2  #                                                                             # 
 3  # Copyright (C) 2013 Edward d'Auvergne                                        # 
 4  # Copyright (C) 2014 Troels E. Linnet                                         # 
 5  #                                                                             # 
 6  # This file is part of the program relax (http://www.nmr-relax.com).          # 
 7  #                                                                             # 
 8  # This program is free software: you can redistribute it and/or modify        # 
 9  # it under the terms of the GNU General Public License as published by        # 
10  # the Free Software Foundation, either version 3 of the License, or           # 
11  # (at your option) any later version.                                         # 
12  #                                                                             # 
13  # This program is distributed in the hope that it will be useful,             # 
14  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
16  # GNU General Public License for more details.                                # 
17  #                                                                             # 
18  # You should have received a copy of the GNU General Public License           # 
19  # along with this program.  If not, see <http://www.gnu.org/licenses/>.       # 
20  #                                                                             # 
21  ############################################################################### 
22   
23  # Module docstring. 
24  """Module for the calculation of the matrix exponential.""" 
25   
26  # Python module imports. 
27  from numpy import array, diag, dot, exp 
28  from numpy.linalg import eig, inv 
29   
30  # relax module imports. 
31  from lib.check_types import is_complex 
32   
33   
34 -def matrix_exponential(A):
35 """Calculate the exact matrix exponential using the eigenvalue decomposition approach. 36 37 @param A: The square matrix to calculate the matrix exponential of. 38 @type A: numpy rank-2 array 39 @return: The matrix exponential. This will have the same dimensionality as the A matrix. 40 @rtype: numpy rank-2 array 41 """ 42 43 # Is the original matrix real? 44 complex_flag = is_complex(A[0, 0]) 45 46 # The eigenvalue decomposition. 47 W, V = eig(A) 48 49 # Calculate the exact exponential. 50 eA = dot(dot(V, diag(exp(W))), inv(V)) 51 52 # Return the complex matrix. 53 if complex_flag: 54 return array(eA) 55 56 # Return only the real part. 57 else: 58 return array(eA.real)
59