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

Source Code for Module test_suite.unit_tests._maths_fns.test_kronecker_product

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