1   
 2   
 3   
 4   
 5   
 6   
 7   
 8   
 9   
10   
11   
12   
13   
14   
15   
16   
17   
18   
19   
20   
21   
22   
23  from numpy import array, complex64, float64 
24  from unittest import TestCase 
25   
26   
27  from lib.linear_algebra.matrix_exponential import matrix_exponential 
28   
29   
31      """Unit tests for the lib.linear_algebra.matrix_exponential relax module.""" 
32   
33   
35          """Test the matrix exponential function matrix_exponential() with real matrices.""" 
36   
37           
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           
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           
54          eR1_test = matrix_exponential(R1) 
55          eR2_test = matrix_exponential(R2) 
56   
57           
58          print("Real matrix:\n%s" % eR1) 
59          print("Calculated matrix:\n%s" % eR1_test) 
60   
61           
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           
72          R1 = array([[-0.024156250059605+0.j, 0.021093750372529+0.j], 
73                      [ 0.021093750372529+0.j, -0.024156250059605-0.587233662605286j]], complex64) 
74   
75           
76          eR1 = array([[ 0.976344227790833 -4.17836126871407e-05j,  0.0194285903126001 -0.00587434694170952j], 
77                       [ 0.0194285865873098 -0.00587435066699982j,  0.812806785106659  -0.540918707847595j]], complex64) 
78   
79           
80          eR1_test = matrix_exponential(R1) 
81   
82           
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           
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