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

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

  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, float64, zeros 
 24  from unittest import TestCase 
 25   
 26  # relax module imports. 
 27  from lib.linear_algebra.kronecker_product import kron_prod, transpose_12, transpose_13, transpose_14, transpose_23, transpose_24, transpose_34 
 28   
 29   
30 -class Test_kronecker_product(TestCase):
31 """Unit tests for the target_functions.kronecker_product relax module.""" 32
33 - def setUp(self):
34 """Set up data used by the unit tests.""" 35 36 # A rank-4, 3D tensor in string form (and rank-2, 9D). 37 self.daeg_str = [ 38 ['0000', '0001', '0002', '0010', '0011', '0012', '0020', '0021', '0022'], 39 ['0100', '0101', '0102', '0110', '0111', '0112', '0120', '0121', '0122'], 40 ['0200', '0201', '0202', '0210', '0211', '0212', '0220', '0221', '0222'], 41 42 ['1000', '1001', '1002', '1010', '1011', '1012', '1020', '1021', '1022'], 43 ['1100', '1101', '1102', '1110', '1111', '1112', '1120', '1121', '1122'], 44 ['1200', '1201', '1202', '1210', '1211', '1212', '1220', '1221', '1222'], 45 46 ['2000', '2001', '2002', '2010', '2011', '2012', '2020', '2021', '2022'], 47 ['2100', '2101', '2102', '2110', '2111', '2112', '2120', '2121', '2122'], 48 ['2200', '2201', '2202', '2210', '2211', '2212', '2220', '2221', '2222'], 49 ] 50 print("The initial tensor:") 51 self.print_nice(self.daeg_str) 52 self.daeg = self.to_numpy(self.daeg_str)
53 54
55 - def print_nice(self, daeg):
56 """Formatted printout of the tensor.""" 57 58 # Loop over the rows. 59 for i in range(9): 60 # Empty row. 61 if i != 0 and not i % 3: 62 print(' |' + '-'*17 + '|' + '-'*17 + '|' + '-'*17) 63 64 # Loop over the columns. 65 line = '' 66 for j in range(9): 67 # Block separator. 68 if not j % 3: 69 line = line + ' | ' 70 71 # The matrix element. 72 if isinstance(daeg[i][j], str): 73 line = line + daeg[i][j] + " " 74 else: 75 val = "%s" % int(daeg[i, j]) 76 string = ['0', '0', '0', '0'] 77 for k in range(1, len(val)+1): 78 string[-k] = val[-k] 79 string = '%s%s%s%s' % (string[0], string[1], string[2], string[3]) 80 line = line + string + " " 81 82 print(line + '|') 83 print('')
84 85
86 - def string_transpose(self, index1, index2):
87 """Manually transpose self.daeg_str using the 2 given indices.""" 88 89 # Initialise the matrix. 90 daegT = [] 91 92 # The string indices. 93 indices = list(range(4)) 94 temp = indices[index1-1] 95 indices[index1-1] = indices[index2-1] 96 indices[index2-1] = temp 97 98 # Loop over the elements. 99 for i in range(9): 100 daegT.append([]) 101 for j in range(9): 102 elem = self.daeg_str[i][j] 103 daegT[-1].append('%s%s%s%s' % (elem[indices[0]], elem[indices[1]], elem[indices[2]], elem[indices[3]])) 104 105 # Return. 106 return daegT
107 108
109 - def to_numpy(self, tensor):
110 """Convert the string version of the tensor into a numpy version.""" 111 112 # Initialise. 113 new = zeros((9, 9), float64) 114 115 # Loop over the elements. 116 for i in range(9): 117 for j in range(9): 118 new[i, j] = float(tensor[i][j]) 119 120 # Return the tensor. 121 return new
122 123
124 - def test_kron_prod(self):
125 """Test the Kronecker product function kron_prod().""" 126 127 # The 3D, rank-2 matrices. 128 R1 = array([[1, 4, 5], [-4, 2, 6], [-5, -6, 3]], float64) 129 R2 = array([[0, 1, 0], [0, 0, 0], [0, 0, 0]], float64) 130 131 # The Kronecker product. 132 C = kron_prod(R1, R2) 133 134 # The real Kronecker product! 135 D = array([ 136 [ 0, 1, 0, 0, 4, 0, 0, 5, 0], 137 [ 0, 0, 0, 0, 0, 0, 0, 0, 0], 138 [ 0, 0, 0, 0, 0, 0, 0, 0, 0], 139 [ 0, -4, 0, 0, 2, 0, 0, 6, 0], 140 [ 0, 0, 0, 0, 0, 0, 0, 0, 0], 141 [ 0, 0, 0, 0, 0, 0, 0, 0, 0], 142 [ 0, -5, 0, 0, -6, 0, 0, 3, 0], 143 [ 0, 0, 0, 0, 0, 0, 0, 0, 0], 144 [ 0, 0, 0, 0, 0, 0, 0, 0, 0]], float64) 145 146 # Print outs. 147 print("R1:\n%s" % R1) 148 print("R2:\n%s" % R2) 149 print("C:\n%s" % C) 150 print("D:\n%s" % D) 151 152 # Checks. 153 self.assertEqual(C.shape, (9, 9)) 154 for i in range(9): 155 for j in range(9): 156 self.assertEqual(C[i, j], D[i, j])
157 158
159 - def test_transpose_12(self):
160 """Check the 1,2 transpose of a rank-4, 3D tensor.""" 161 162 # Manually create the string rep of the transpose. 163 daegT = self.string_transpose(1, 2) 164 print("The real 1,2 transpose:") 165 self.print_nice(daegT) 166 167 # Convert to numpy. 168 daegT = self.to_numpy(daegT) 169 170 # Check. 171 print("The numerical 1,2 transpose:") 172 transpose_12(self.daeg) 173 self.print_nice(self.daeg) 174 for i in range(9): 175 for j in range(9): 176 print("i = %2s, j = %2s, daeg[i,j] = %s" % (i, j, daegT[i, j])) 177 self.assertEqual(self.daeg[i, j], daegT[i, j])
178 179
180 - def test_transpose_13(self):
181 """Check the 1,3 transpose of a rank-4, 3D tensor.""" 182 183 # Manually create the string rep of the transpose. 184 daegT = self.string_transpose(1, 3) 185 print("The real 1,3 transpose:") 186 self.print_nice(daegT) 187 188 # Convert to numpy. 189 daegT = self.to_numpy(daegT) 190 191 # Check. 192 print("The numerical 1,3 transpose:") 193 transpose_13(self.daeg) 194 self.print_nice(self.daeg) 195 for i in range(9): 196 for j in range(9): 197 print("i = %2s, j = %2s, daeg[i,j] = %s" % (i, j, daegT[i, j])) 198 self.assertEqual(self.daeg[i, j], daegT[i, j])
199 200
201 - def test_transpose_14(self):
202 """Check the 1,4 transpose of a rank-4, 3D tensor.""" 203 204 # Manually create the string rep of the transpose. 205 daegT = self.string_transpose(1, 4) 206 print("The real 1,4 transpose:") 207 self.print_nice(daegT) 208 209 # Convert to numpy. 210 daegT = self.to_numpy(daegT) 211 212 # Check. 213 print("The numerical 1,4 transpose:") 214 transpose_14(self.daeg) 215 self.print_nice(self.daeg) 216 for i in range(9): 217 for j in range(9): 218 print("i = %2s, j = %2s, daeg[i,j] = %s" % (i, j, daegT[i, j])) 219 self.assertEqual(self.daeg[i, j], daegT[i, j])
220 221
222 - def test_transpose_23(self):
223 """Check the 2,3 transpose of a rank-4, 3D tensor.""" 224 225 # Manually create the string rep of the transpose. 226 daegT = self.string_transpose(2, 3) 227 print("The real 2,3 transpose:") 228 self.print_nice(daegT) 229 230 # Convert to numpy. 231 daegT = self.to_numpy(daegT) 232 233 # Check. 234 print("The numerical 2,3 transpose:") 235 transpose_23(self.daeg) 236 self.print_nice(self.daeg) 237 for i in range(9): 238 for j in range(9): 239 print("i = %2s, j = %2s, daeg[i,j] = %s" % (i, j, daegT[i, j])) 240 self.assertEqual(self.daeg[i, j], daegT[i, j])
241 242
243 - def test_transpose_24(self):
244 """Check the 2,4 transpose of a rank-4, 3D tensor.""" 245 246 # Manually create the string rep of the transpose. 247 daegT = self.string_transpose(2, 4) 248 print("The real 2,4 transpose:") 249 self.print_nice(daegT) 250 251 # Convert to numpy. 252 daegT = self.to_numpy(daegT) 253 254 # Check. 255 print("The numerical 2,4 transpose:") 256 transpose_24(self.daeg) 257 self.print_nice(self.daeg) 258 for i in range(9): 259 for j in range(9): 260 print("i = %2s, j = %2s, daeg[i,j] = %s" % (i, j, daegT[i, j])) 261 self.assertEqual(self.daeg[i, j], daegT[i, j])
262 263
264 - def test_transpose_34(self):
265 """Check the 3,4 transpose of a rank-4, 3D tensor.""" 266 267 # Manually create the string rep of the transpose. 268 daegT = self.string_transpose(3, 4) 269 print("The real 3,4 transpose:") 270 self.print_nice(daegT) 271 272 # Convert to numpy. 273 daegT = self.to_numpy(daegT) 274 275 # Check. 276 print("The numerical 3,4 transpose:") 277 transpose_34(self.daeg) 278 self.print_nice(self.daeg) 279 for i in range(9): 280 for j in range(9): 281 print("i = %2s, j = %2s, daeg[i,j] = %s" % (i, j, daegT[i, j])) 282 self.assertEqual(self.daeg[i, j], daegT[i, j])
283 284
286 """Check that the transposes revert back to the original matrix.""" 287 288 # Make a copy of the frame order matrix. 289 daeg_orig = self.to_numpy(self.daeg_str) 290 291 # List of transpose functions. 292 Tij = [transpose_12, transpose_13, transpose_14, transpose_23, transpose_24, transpose_34] 293 294 # Check the transpose reversions. 295 for transpose in Tij: 296 # Transpose twice. 297 transpose(self.daeg) 298 transpose(self.daeg) 299 300 # Check. 301 for i in range(9): 302 for j in range(9): 303 print("i = %2s, j = %2s, daeg[i,j] = %s" % (i, j, daeg_orig[i, j])) 304 self.assertEqual(self.daeg[i, j], daeg_orig[i, j])
305