0

I need to speed up a python code, I would like to avoid the use of the following for cycle, where "data" matrix has dimension [dim1xdim2]:

for i in range(int(dim1)):
        data_process = data[i,:].reshape((dim2, 1))
        rxx = data_process * np.matrix.getH(np.asmatrix(data_process)) / dim2

Using the 'for cycle' the dimension of the rxx matrix is [dim2xdim2], I would get a 3D "rxx" matrix [dim1xdim2xdim2]. I tried to use the following solution:

 data_new = repeat(data_process0[:, :, newaxis], dim2, axis=2)
 N_2 = data_new.shape[2]
 m1 = data_new - data_new.sum(2, keepdims=1) / N_2
 y_out = einsum('ijk,ilk->ijl', m1, m1) / (N_2 - 1)

In this case I got 3D "y_out" matrix [dim1xdim2xdim2] but this doesn't work in my case.

Thanks

representative sample data:

from numpy import matrix, random, asmatrix, linalg, empty
B = random.random((156, 48))  
A = B.shape
eig_val = empty(A, dtype=complex)
eig_vec = empty((A[0], A[1], A[1]), dtype=complex)
  for i in range(int(A[0])):
     data_process = B[i, :].reshape((A[1], 1))
     rxx = data_process * matrix.getH(asmatrix(data_process)) / A[1]
     eig_val[i:, ...], eig_vec[i:, ...] = linalg.eig(rxx)
Luca R
  • 1
  • 2
  • Use `np.array()` for multi-dim arrays and processing. – Divakar Oct 30 '19 at 10:49
  • @Divakar Sorry, I didn't get your suggestion; could you please better show how to use np.array() to solve my question. Thanks – Luca R Oct 30 '19 at 13:41
  • Your code has `np.matrix`. So, I suggested using `np.array` instead. Also, could you show us a minimal representative sample data. More info - https://stackoverflow.com/help/minimal-reproducible-example – Divakar Oct 30 '19 at 13:42
  • @Divakar I added an example to the end of my post. In few words: is any pythonic way to get all the 156x48 matrix eigvalues and 156x48x48 matrix eigenvector avoiding the for cycle? – Luca R Oct 30 '19 at 15:51

0 Answers0