Package maths_fns :: Module kronecker_product
[hide private]
[frames] | no frames]

Source Code for Module maths_fns.kronecker_product

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2009 Edward d'Auvergne                                        # 
  4  #                                                                             # 
  5  # This file is part of the program relax.                                     # 
  6  #                                                                             # 
  7  # relax 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 2 of the License, or           # 
 10  # (at your option) any later version.                                         # 
 11  #                                                                             # 
 12  # relax 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 relax; if not, write to the Free Software                        # 
 19  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   # 
 20  #                                                                             # 
 21  ############################################################################### 
 22   
 23  # Module docstring. 
 24  """Module for the calculation of the Kronecker product.""" 
 25   
 26  # Python imports. 
 27  from numpy import concatenate, outer 
 28   
 29   
 30   
31 -def kron_prod(A, B):
32 """Calculate the Kronecker product of the matrices A and B. 33 34 @param A: ixj matrix. 35 @type A: rank-2 numpy array 36 @param B: kxl matrix. 37 @type B: rank-2 numpy array 38 @return: The Kronecker product. 39 @rtype: ikxjl rank-2 numpy array 40 """ 41 42 # The outer product. 43 C = outer(A, B) 44 45 # Redefine the shape. 46 orig_shape = C.shape 47 C.shape = A.shape + B.shape 48 49 # Generate and return the Kronecker product matrix. 50 transpose_23(C) 51 C.shape = orig_shape 52 return C
53 54
55 -def transpose_12(matrix):
56 """Perform the 1,2 transpose of a rank-4, 3D tensor. 57 58 @param matrix: The rank-4 tensor either in (9, 9) shape for a matrix or the (3, 3, 3, 3) shape 59 for the tensor form. 60 @type matrix: numpy array 61 """ 62 63 # Reshape if necessary. 64 reshape = False 65 if matrix.shape == (9, 9): 66 reshape = True 67 matrix.shape = (3, 3, 3, 3) 68 69 # Perform the transpose. 70 for i in range(3): 71 for j in range(i, 3): 72 for k in range(3): 73 for l in range(3): 74 # Store the element. 75 element = matrix[i, j, k, l] 76 77 # Overwrite. 78 matrix[i, j, k, l] = matrix[j, i, k, l] 79 matrix[j, i, k, l] = element 80 81 # Undo the reshape. 82 if reshape: 83 matrix.shape = (9, 9)
84 85
86 -def transpose_13(matrix):
87 """Perform the 1,3 transpose of a rank-4, 3D tensor. 88 89 @param matrix: The rank-4 tensor either in (9, 9) shape for a matrix or the (3, 3, 3, 3) shape 90 for the tensor form. 91 @type matrix: numpy array 92 """ 93 94 # Reshape if necessary. 95 reshape = False 96 if matrix.shape == (9, 9): 97 reshape = True 98 matrix.shape = (3, 3, 3, 3) 99 100 # Perform the transpose. 101 for i in range(3): 102 for j in range(3): 103 for k in range(i, 3): 104 for l in range(3): 105 # Store the element. 106 element = matrix[i, j, k, l] 107 108 # Overwrite. 109 matrix[i, j, k, l] = matrix[k, j, i, l] 110 matrix[k, j, i, l] = element 111 112 # Undo the reshape. 113 if reshape: 114 matrix.shape = (9, 9)
115 116
117 -def transpose_14(matrix):
118 """Perform the 1,4 transpose of a rank-4, 3D tensor. 119 120 @param matrix: The rank-4 tensor either in (9, 9) shape for a matrix or the (3, 3, 3, 3) shape 121 for the tensor form. 122 @type matrix: numpy array 123 """ 124 125 # Reshape if necessary. 126 reshape = False 127 if matrix.shape == (9, 9): 128 reshape = True 129 matrix.shape = (3, 3, 3, 3) 130 131 # Perform the transpose. 132 for i in range(3): 133 for j in range(3): 134 for k in range(3): 135 for l in range(i, 3): 136 # Store the element. 137 element = matrix[i, j, k, l] 138 139 # Overwrite. 140 matrix[i, j, k, l] = matrix[l, j, k, i] 141 matrix[l, j, k, i] = element 142 143 # Undo the reshape. 144 if reshape: 145 matrix.shape = (9, 9)
146 147
148 -def transpose_23(matrix):
149 """Perform the 2,3 transpose of a rank-4, 3D tensor. 150 151 @param matrix: The rank-4 tensor either in (9, 9) shape for a matrix or the (3, 3, 3, 3) shape 152 for the tensor form. 153 @type matrix: numpy array 154 """ 155 156 # Reshape if necessary. 157 reshape = False 158 if matrix.shape == (9, 9): 159 reshape = True 160 matrix.shape = (3, 3, 3, 3) 161 162 # Perform the transpose. 163 for i in range(3): 164 for j in range(3): 165 for k in range(j, 3): 166 for l in range(3): 167 # Store the element. 168 element = matrix[i, j, k, l] 169 170 # Overwrite. 171 matrix[i, j, k, l] = matrix[i, k, j, l] 172 matrix[i, k, j, l] = element 173 174 # Undo the reshape. 175 if reshape: 176 matrix.shape = (9, 9)
177 178
179 -def transpose_24(matrix):
180 """Perform the 2,4 transpose of a rank-4, 3D tensor. 181 182 @param matrix: The rank-4 tensor either in (9, 9) shape for a matrix or the (3, 3, 3, 3) shape 183 for the tensor form. 184 @type matrix: numpy array 185 """ 186 187 # Reshape if necessary. 188 reshape = False 189 if matrix.shape == (9, 9): 190 reshape = True 191 matrix.shape = (3, 3, 3, 3) 192 193 # Perform the transpose. 194 for i in range(3): 195 for j in range(3): 196 for k in range(3): 197 for l in range(j, 3): 198 # Store the element. 199 element = matrix[i, j, k, l] 200 201 # Overwrite. 202 matrix[i, j, k, l] = matrix[i, l, k, j] 203 matrix[i, l, k, j] = element 204 205 # Undo the reshape. 206 if reshape: 207 matrix.shape = (9, 9)
208 209
210 -def transpose_34(matrix):
211 """Perform the 3,4 transpose of a rank-4, 3D tensor. 212 213 @param matrix: The rank-4 tensor either in (9, 9) shape for a matrix or the (3, 3, 3, 3) shape 214 for the tensor form. 215 @type matrix: numpy array 216 """ 217 218 # Reshape if necessary. 219 reshape = False 220 if matrix.shape == (9, 9): 221 reshape = True 222 matrix.shape = (3, 3, 3, 3) 223 224 # Perform the transpose. 225 for i in range(3): 226 for j in range(3): 227 for k in range(3): 228 for l in range(k, 3): 229 # Store the element. 230 element = matrix[i, j, k, l] 231 232 # Overwrite. 233 matrix[i, j, k, l] = matrix[i, j, l, k] 234 matrix[i, j, l, k] = element 235 236 # Undo the reshape. 237 if reshape: 238 matrix.shape = (9, 9)
239