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