1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 """Module for the calculation of the Kronecker product."""
25
26
27 from numpy import concatenate, outer
28
29
30
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
43 C = outer(A, B)
44
45
46 orig_shape = C.shape
47 C.shape = A.shape + B.shape
48
49
50 transpose_23(C)
51 C.shape = orig_shape
52 return C
53
54
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
64 reshape = False
65 if matrix.shape == (9, 9):
66 reshape = True
67 matrix.shape = (3, 3, 3, 3)
68
69
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
75 element = matrix[i, j, k, l]
76
77
78 matrix[i, j, k, l] = matrix[j, i, k, l]
79 matrix[j, i, k, l] = element
80
81
82 if reshape:
83 matrix.shape = (9, 9)
84
85
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
95 reshape = False
96 if matrix.shape == (9, 9):
97 reshape = True
98 matrix.shape = (3, 3, 3, 3)
99
100
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
106 element = matrix[i, j, k, l]
107
108
109 matrix[i, j, k, l] = matrix[k, j, i, l]
110 matrix[k, j, i, l] = element
111
112
113 if reshape:
114 matrix.shape = (9, 9)
115
116
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
126 reshape = False
127 if matrix.shape == (9, 9):
128 reshape = True
129 matrix.shape = (3, 3, 3, 3)
130
131
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
137 element = matrix[i, j, k, l]
138
139
140 matrix[i, j, k, l] = matrix[l, j, k, i]
141 matrix[l, j, k, i] = element
142
143
144 if reshape:
145 matrix.shape = (9, 9)
146
147
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
157 reshape = False
158 if matrix.shape == (9, 9):
159 reshape = True
160 matrix.shape = (3, 3, 3, 3)
161
162
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
168 element = matrix[i, j, k, l]
169
170
171 matrix[i, j, k, l] = matrix[i, k, j, l]
172 matrix[i, k, j, l] = element
173
174
175 if reshape:
176 matrix.shape = (9, 9)
177
178
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
188 reshape = False
189 if matrix.shape == (9, 9):
190 reshape = True
191 matrix.shape = (3, 3, 3, 3)
192
193
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
199 element = matrix[i, j, k, l]
200
201
202 matrix[i, j, k, l] = matrix[i, l, k, j]
203 matrix[i, l, k, j] = element
204
205
206 if reshape:
207 matrix.shape = (9, 9)
208
209
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
219 reshape = False
220 if matrix.shape == (9, 9):
221 reshape = True
222 matrix.shape = (3, 3, 3, 3)
223
224
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
230 element = matrix[i, j, k, l]
231
232
233 matrix[i, j, k, l] = matrix[i, j, l, k]
234 matrix[i, j, l, k] = element
235
236
237 if reshape:
238 matrix.shape = (9, 9)
239