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 matrix power, for higher dimensional data."""
25
26
27 from numpy.lib.stride_tricks import as_strided
28 from numpy import float64, int16, zeros
29 from numpy.linalg import matrix_power
30
31
33 """ Method to create the helper index matrix, to help figuring out the index to store in the data matrix. """
34
35
36 NE, NS, NM, NO, ND, Row, Col = data.shape
37
38
39 index = zeros([NE, NS, NM, NO, ND, 5], int16)
40
41 for ei in range(NE):
42 for si in range(NS):
43 for mi in range(NM):
44 for oi in range(NO):
45 for di in range(ND):
46 index[ei, si, mi, oi, di] = ei, si, mi, oi, di
47
48 return index
49
50
52 """Calculate the exact matrix power by striding through higher dimensional data. This of dimension [NE][NS][NM][NO][ND][X][X].
53
54 Here X is the Row and Column length, of the outer square matrix.
55
56 @param data: The square matrix to calculate the matrix exponential of.
57 @type data: numpy float array of rank [NE][NS][NM][NO][ND][X][X]
58 @keyword power: The matrix exponential power array, which hold the power integer to which to raise the outer matrix X,X to.
59 @type power: numpy int array of rank [NE][NS][NM][NO][ND]
60 @return: The matrix pwer. This will have the same dimensionality as the data matrix.
61 @rtype: numpy float array of rank [NE][NS][NM][NO][ND][X][X]
62 """
63
64
65 NE, NS, NM, NO, ND, Row, Col = data.shape
66
67
68 calc = zeros([NE, NS, NM, NO, ND, Row, Col], float64)
69
70
71 data_view = stride_help_square_matrix_rank_NE_NS_NM_NO_ND_x_x(data)
72
73
74 power_view = stride_help_element_rank_NE_NS_NM_NO_ND(power)
75
76
77 index = create_index(data)
78 index_view = stride_help_array_rank_NE_NS_NM_NO_ND_x(index)
79
80
81 for data_i, power_i, index_i in zip(data_view, power_view, index_view):
82
83 data_power_i = matrix_power(data_i, int(power_i))
84
85
86 ei, si, mi, oi, di = index_i
87
88
89 calc[ei, si, mi, oi, di, :] = data_power_i
90
91 return calc
92
93
95 """ Method to stride through the data matrix, extracting the outer array with nr of elements as Column length. """
96
97
98 NE, NS, NM, NO, ND, Col = data.shape
99
100
101 Nr_mat = NE * NS * NM * NO * ND
102
103
104 shape = (Nr_mat, Col)
105
106
107 itz = data.itemsize
108
109
110 bbe = 1 * itz
111
112
113 bbr = Col * itz
114
115
116 strides = (bbr, bbe)
117
118
119 data_view = as_strided(data, shape=shape, strides=strides)
120
121 return data_view
122
123
125 """ Method to stride through the data matrix, extracting the outer element. """
126
127
128 NE, NS, NM, NO, Col = data.shape
129
130
131 Nr_mat = NE * NS * NM * NO * Col
132
133
134 shape = (Nr_mat, 1)
135
136
137 itz = data.itemsize
138
139
140 bbe = Col * itz
141
142
143 bbr = 1 * itz
144
145
146 strides = (bbr, bbe)
147
148
149 data_view = as_strided(data, shape=shape, strides=strides)
150
151 return data_view
152
153
155 """ Helper function calculate the strides to go through the data matrix, extracting the outer Row X Col matrix. """
156
157
158 NE, NS, NM, NO, ND, Row, Col = data.shape
159
160
161 Nr_mat = NE * NS * NM * NO * ND
162
163
164 shape = (Nr_mat, Row, Col)
165
166
167 itz = data.itemsize
168
169
170 bbe = 1 * itz
171
172
173 bbr = Col * itz
174
175
176 bbm = Row * Col * itz
177
178
179 strides = (bbm, bbr, bbe)
180
181
182 data_view = as_strided(data, shape=shape, strides=strides)
183
184 return data_view
185