Package test_suite :: Package unit_tests :: Package _lib :: Package _linear_algebra :: Module test_matrix_exponential
[hide private]
[frames] | no frames]

Source Code for Module test_suite.unit_tests._lib._linear_algebra.test_matrix_exponential

 1  ############################################################################### 
 2  #                                                                             # 
 3  # Copyright (C) 2009-2013 Edward d'Auvergne                                   # 
 4  #                                                                             # 
 5  # This file is part of the program relax (http://www.nmr-relax.com).          # 
 6  #                                                                             # 
 7  # This program is free software: you can redistribute it and/or modify        # 
 8  # it under the terms of the GNU General Public License as published by        # 
 9  # the Free Software Foundation, either version 3 of the License, or           # 
10  # (at your option) any later version.                                         # 
11  #                                                                             # 
12  # This program is distributed in the hope that it will be useful,             # 
13  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
15  # GNU General Public License for more details.                                # 
16  #                                                                             # 
17  # You should have received a copy of the GNU General Public License           # 
18  # along with this program.  If not, see <http://www.gnu.org/licenses/>.       # 
19  #                                                                             # 
20  ############################################################################### 
21   
22  # Python module imports. 
23  from numpy import array, complex64, float64, zeros 
24  from unittest import TestCase 
25   
26  # relax module imports. 
27  from lib.linear_algebra.matrix_exponential import matrix_exponential 
28   
29   
30 -class Test_matrix_exponential(TestCase):
31 """Unit tests for the lib.linear_algebra.matrix_exponential relax module.""" 32 33
34 - def test_matrix_exponential(self):
35 """Test the matrix exponential function matrix_exponential() with real matrices.""" 36 37 # The 3D, rank-2 matrices. 38 R1 = array([[1, 4, 5], 39 [-4, 2, 6], 40 [-5, -6, 3]], float64) 41 R2 = array([[0, 1, 0], 42 [0, 0, 0], 43 [0, 0, 0]], float64) 44 45 # The real matrix exponentials. 46 eR1 = array([[-1.242955024379687, -3.178944439554645, 6.804083368075889], 47 [-6.545353831891249, -2.604941866769356, 1.228233941393001], 48 [ 0.975355249080821, -7.711099455690256, -3.318642157729292]], float64) 49 eR2 = array([[ 1., 0., 0.], 50 [ 0., 1., 0.], 51 [ 0., 0., 1.]], float64) 52 53 # The maths. 54 eR1_test = matrix_exponential(R1) 55 eR2_test = matrix_exponential(R2) 56 57 # Printouts. 58 print("Real matrix:\n%s" % eR1) 59 print("Calculated matrix:\n%s" % eR1_test) 60 61 # Checks. 62 for i in range(3): 63 for j in range(3): 64 self.assertAlmostEqual(eR1_test[i, j], eR1[i, j]) 65 self.assertAlmostEqual(eR2_test[i, j], eR2[i, j])
66 67
69 """Test the matrix exponential function matrix_exponential() with complex matrices.""" 70 71 # The 3D, rank-2 matrix. 72 R1 = array([[-0.024156250059605+0.j, 0.021093750372529+0.j], 73 [ 0.021093750372529+0.j, -0.024156250059605-0.587233662605286j]], complex64) 74 75 # The real matrix exponentials. 76 eR1 = array([[ 0.976344227790833 -4.17836126871407e-05j, 0.0194285903126001 -0.00587434694170952j], 77 [ 0.0194285865873098 -0.00587435066699982j, 0.812806785106659 -0.540918707847595j]], complex64) 78 79 # The maths. 80 eR1_test = matrix_exponential(R1) 81 82 # Printouts. 83 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)) 84 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)) 85 86 # Checks. 87 for i in range(2): 88 for j in range(2): 89 self.assertAlmostEqual(eR1_test[i, j].real, eR1[i, j].real, 5) 90 self.assertAlmostEqual(eR1_test[i, j].imag, eR1[i, j].imag, 5)
91