Author: bugman Date: Tue Oct 15 11:46:02 2013 New Revision: 21110 URL: http://svn.gna.org/viewcvs/relax?rev=21110&view=rev Log: Merged revisions 21107-21109 via svnmerge from svn+ssh://bugman@xxxxxxxxxxx/svn/relax/trunk ........ r21107 | bugman | 2013-10-15 11:31:40 +0200 (Tue, 15 Oct 2013) | 5 lines Created the lib.check_types.is_complex() function. This is used to determine if a number is a Python or numpy complex type. ........ r21108 | bugman | 2013-10-15 11:34:11 +0200 (Tue, 15 Oct 2013) | 5 lines The lib.linear_algebra.matrix_exponential.matrix_exponential() function now uses lib.check_types.is_complex(). This fixes the function for complex matrices. ........ r21109 | bugman | 2013-10-15 11:45:29 +0200 (Tue, 15 Oct 2013) | 3 lines Created a new unit test for lib.linear_algebra.matrix_exponential.matrix_exponential() for complex matrices. ........ Modified: branches/relax_disp/ (props changed) branches/relax_disp/lib/check_types.py branches/relax_disp/lib/linear_algebra/matrix_exponential.py branches/relax_disp/test_suite/unit_tests/_lib/_linear_algebra/test_matrix_exponential.py Propchange: branches/relax_disp/ ------------------------------------------------------------------------------ --- svnmerge-integrated (original) +++ svnmerge-integrated Tue Oct 15 11:46:02 2013 @@ -1,1 +1,1 @@ -/trunk:1-21105 +/trunk:1-21109 Modified: branches/relax_disp/lib/check_types.py URL: http://svn.gna.org/viewcvs/relax/branches/relax_disp/lib/check_types.py?rev=21110&r1=21109&r2=21110&view=diff ============================================================================== --- branches/relax_disp/lib/check_types.py (original) +++ branches/relax_disp/lib/check_types.py Tue Oct 15 11:46:02 2013 @@ -29,7 +29,11 @@ file = None except ImportError: io_module = False -from numpy import float32, float64 +from numpy import complex64, complex128, float32, float64 +try: + from numpy import complex256 +except ImportError: + complex256 = complex128 # Support for 32-bit numpy versions. try: from numpy import float16 except ImportError: @@ -38,6 +42,31 @@ from numpy import float128 except ImportError: float128 = float64 # Support for 32-bit numpy versions. + + +def is_complex(num): + """Check if the given number is a Python or numpy complex. + + @param num: The number to check. + @type num: anything. + @return: True if the number is a complex, False otherwise. + @rtype: bool + """ + + # Standard complex. + if isinstance(num, complex): + return True + + # Numpy complex numbers. + if isinstance(num, complex64): + return True + if isinstance(num, complex128): + return True + if isinstance(num, complex256): + return True + + # Not a complex. + return False def is_filetype(obj): Modified: branches/relax_disp/lib/linear_algebra/matrix_exponential.py URL: http://svn.gna.org/viewcvs/relax/branches/relax_disp/lib/linear_algebra/matrix_exponential.py?rev=21110&r1=21109&r2=21110&view=diff ============================================================================== --- branches/relax_disp/lib/linear_algebra/matrix_exponential.py (original) +++ branches/relax_disp/lib/linear_algebra/matrix_exponential.py Tue Oct 15 11:46:02 2013 @@ -26,6 +26,9 @@ from numpy import diag, dot, exp, iscomplex from numpy.linalg import eig, inv +# relax module imports. +from lib.check_types import is_complex + def matrix_exponential(A): """Calculate the exact matrix exponential using the eigenvalue decomposition approach. @@ -37,7 +40,7 @@ """ # Is the original matrix real? - complex_flag = iscomplex(A[0][0]) + complex_flag = is_complex(A[0, 0]) # The eigenvalue decomposition. W, V = eig(A) Modified: branches/relax_disp/test_suite/unit_tests/_lib/_linear_algebra/test_matrix_exponential.py URL: http://svn.gna.org/viewcvs/relax/branches/relax_disp/test_suite/unit_tests/_lib/_linear_algebra/test_matrix_exponential.py?rev=21110&r1=21109&r2=21110&view=diff ============================================================================== --- branches/relax_disp/test_suite/unit_tests/_lib/_linear_algebra/test_matrix_exponential.py (original) +++ branches/relax_disp/test_suite/unit_tests/_lib/_linear_algebra/test_matrix_exponential.py Tue Oct 15 11:46:02 2013 @@ -20,7 +20,7 @@ ############################################################################### # Python module imports. -from numpy import array, float64, zeros +from numpy import array, complex64, float64, zeros from unittest import TestCase # relax module imports. @@ -32,7 +32,7 @@ def test_matrix_exponential(self): - """Test the Kronecker product function kron_prod().""" + """Test the matrix exponential function matrix_exponential() with real matrices.""" # The 3D, rank-2 matrices. R1 = array([[1, 4, 5], @@ -54,8 +54,37 @@ eR1_test = matrix_exponential(R1) eR2_test = matrix_exponential(R2) + # Printouts. + print("Real matrix:\n%s" % eR1) + print("Calculated matrix:\n%s" % eR1_test) + # Checks. for i in range(3): for j in range(3): self.assertAlmostEqual(eR1_test[i, j], eR1[i, j]) self.assertAlmostEqual(eR2_test[i, j], eR2[i, j]) + + + def test_matrix_exponential2(self): + """Test the matrix exponential function matrix_exponential() with complex matrices.""" + + # The 3D, rank-2 matrix. + R1 = array([[-0.024156250059605+0.j, 0.021093750372529+0.j], + [ 0.021093750372529+0.j, -0.024156250059605-0.587233662605286j]], complex64) + + # The real matrix exponentials. + eR1 = array([[ 0.976344227790833 -4.17836126871407e-05j, 0.0194285903126001 -0.00587434694170952j], + [ 0.0194285865873098 -0.00587435066699982j, 0.812806785106659 -0.540918707847595j]], complex64) + + # The maths. + eR1_test = matrix_exponential(R1) + + # Printouts. + print("Real matrix:\n[%20.15g %20.15gj, %20.15g %20.15gj],\n[%20.15g %20.15gj, %20.15g %20.15gj]\n" % (eR1[0, 0].real, eR1[0, 0].imag, eR1[0, 1].real, eR1[0, 1].imag, eR1[1, 0].real, eR1[1, 0].imag, eR1[1, 1].real, eR1[1, 1].imag)) + print("Calculated matrix:\n[%20.15g %20.15gj, %20.15g %20.15gj],\n[%20.15g %20.15gj, %20.15g %20.15gj]\n" % (eR1_test[0, 0].real, eR1_test[0, 0].imag, eR1_test[0, 1].real, eR1_test[0, 1].imag, eR1_test[1, 0].real, eR1_test[1, 0].imag, eR1_test[1, 1].real, eR1_test[1, 1].imag)) + + # Checks. + for i in range(2): + for j in range(2): + self.assertAlmostEqual(eR1_test[i, j].real, eR1[i, j].real) + self.assertAlmostEqual(eR1_test[i, j].imag, eR1[i, j].imag)