Skip to content

Commit f4a0f31

Browse files
committed
Efficient matrix, diagonal matrix multiplication for radial filters
1 parent 474b106 commit f4a0f31

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

micarray/modal/radial.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,10 @@ def diagonal_mode_mat(bk):
193193
Returns
194194
-------
195195
Bk : (M, (N+1)**2, (N+1)**2) numpy.ndarray
196-
Multidimensional array containing diagnonal matrices with input
196+
Multidimensional array containing diagnonal matrices with input
197197
vector on main diagonal.
198198
"""
199-
bk = _repeat_n_m(bk)
199+
bk = repeat_n_m(bk)
200200
if len(bk.shape) == 1:
201201
bk = bk[np.newaxis, :]
202202
K, N = bk.shape
@@ -206,6 +206,24 @@ def diagonal_mode_mat(bk):
206206
return np.squeeze(Bk)
207207

208208

209-
def _repeat_n_m(v):
209+
def repeat_n_m(v):
210+
"""Repeat elements in a vector .
211+
212+
Returns a vector with the elements of the vector *v* repeated *n* times,
213+
where *n* denotes the position of the element in *v*. The function can
214+
be used to order the coefficients in the vector according to the order of
215+
spherical harmonics. If *v* is a matrix, it is treated as a stack of
216+
vectors residing in the last index and broadcast accordingly.
217+
218+
Parameters
219+
----------
220+
v : (,N+1) numpy.ndarray
221+
Input vector or stack of input vectors.
222+
223+
Returns
224+
-------
225+
: (,(N+1)**2) numpy.ndarray
226+
Vector or stack of vectors containing repetated values.
227+
"""
210228
krlist = [np.tile(v, (2*i+1, 1)).T for i, v in enumerate(v.T.tolist())]
211229
return np.squeeze(np.concatenate(krlist, axis=-1))

micarray/util.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,34 @@ def asarray_1d(a, **kwargs):
6060
elif result.ndim > 1:
6161
raise ValueError("array must be one-dimensional")
6262
return result
63+
64+
65+
def matdiagmul(A, b):
66+
"""Efficient multiplication of matrix and diagonal matrix .
67+
68+
Returns the multiplication of a matrix *A* and a diagonal matrix. The
69+
diagonal matrix is given by the vector *b* containing its elements on
70+
the main diagonal. If *b* is a matrix, it is treated as a stack of vectors
71+
residing in the last index and broadcast accordingly.
72+
73+
Parameters
74+
----------
75+
A : array_like
76+
Input matrix.
77+
b : array_like
78+
Main diagonal elements or stack of main diagonal elements.
79+
80+
Returns
81+
-------
82+
array_like
83+
Result of matrix multiplication.
84+
"""
85+
if len(b.shape) == 1:
86+
b = b[np.newaxis, :]
87+
K, N = b.shape
88+
M, N = A.shape
89+
90+
C = np.zeros([K, M, N], dtype=A.dtype)
91+
for k in range(K):
92+
C[k, :, :] = A * b[k, :]
93+
return C

0 commit comments

Comments
 (0)