mailr21105 - in /trunk/lib/linear_algebra: __init__.py matrix_exponential.py


Others Months | Index by Date | Thread Index
>>   [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Header


Content

Posted by edward on October 15, 2013 - 11:07:
Author: bugman
Date: Tue Oct 15 11:07:54 2013
New Revision: 21105

URL: http://svn.gna.org/viewcvs/relax?rev=21105&view=rev
Log:
Implemented the lib.linear_algebra.matrix_exponential.matrix_exponential() 
function.

This handles square matrices in either complex or real form.


Added:
    trunk/lib/linear_algebra/matrix_exponential.py
Modified:
    trunk/lib/linear_algebra/__init__.py

Modified: trunk/lib/linear_algebra/__init__.py
URL: 
http://svn.gna.org/viewcvs/relax/trunk/lib/linear_algebra/__init__.py?rev=21105&r1=21104&r2=21105&view=diff
==============================================================================
--- trunk/lib/linear_algebra/__init__.py (original)
+++ trunk/lib/linear_algebra/__init__.py Tue Oct 15 11:07:54 2013
@@ -23,5 +23,6 @@
 """The relax-lib NMR package - a library of functions for advanced linear 
algebra not present in numpy."""
 
 __all__ = [
-    'kronecker_product'
+    'kronecker_product',
+    'matrix_exponential'
 ]

Added: trunk/lib/linear_algebra/matrix_exponential.py
URL: 
http://svn.gna.org/viewcvs/relax/trunk/lib/linear_algebra/matrix_exponential.py?rev=21105&view=auto
==============================================================================
--- trunk/lib/linear_algebra/matrix_exponential.py (added)
+++ trunk/lib/linear_algebra/matrix_exponential.py Tue Oct 15 11:07:54 2013
@@ -1,0 +1,54 @@
+###############################################################################
+#                                                                            
 #
+# Copyright (C) 2013 Edward d'Auvergne                                       
 #
+#                                                                            
 #
+# This file is part of the program relax (http://www.nmr-relax.com).         
 #
+#                                                                            
 #
+# This program is free software: you can redistribute it and/or modify       
 #
+# it under the terms of the GNU General Public License as published by       
 #
+# the Free Software Foundation, either version 3 of the License, or          
 #
+# (at your option) any later version.                                        
 #
+#                                                                            
 #
+# This program is distributed in the hope that it will be useful,            
 #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of             
 #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              
 #
+# GNU General Public License for more details.                               
 #
+#                                                                            
 #
+# You should have received a copy of the GNU General Public License          
 #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.      
 #
+#                                                                            
 #
+###############################################################################
+
+# Module docstring.
+"""Module for the calculation of the matrix exponential."""
+
+# Python module imports.
+from numpy import diag, dot, exp, iscomplex
+from numpy.linalg import eig, inv
+
+
+def matrix_exponential(A):
+    """Calculate the exact matrix exponential using the eigenvalue 
decomposition approach.
+
+    @param A:   The square matrix to calculate the matrix exponential of.
+    @type A:    numpy rank-2 array
+    @return:    The matrix exponential.  This will have the same 
dimensionality as the A matrix.
+    @rtype:     numpy rank-2 array
+    """
+
+    # Is the original matrix real?
+    complex_flag = iscomplex(A[0][0])
+
+    # The eigenvalue decomposition.
+    W, V = eig(A)
+
+    # Calculate the exact exponential.
+    eA = dot(dot(V, diag(exp(W))), inv(V))
+
+    # Return the complex matrix.
+    if complex_flag:
+        return eA
+
+    # Return only the real part.
+    else:
+        return eA.real




Related Messages


Powered by MHonArc, Updated Tue Oct 15 11:20:01 2013