Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

How to remove nested for loops while filling a matrix

$
0
0

I am trying to fill a larger matrix (A) by using the entries of a smaller matrix (B), the relevant python code is

dim = N_Om * 2 * aA = np.zeros(dim * dim, dtype="complex").reshape(dim, dim)  # Large matrixfor alpha in range(0, 2):    for l in range(1, l_max + 1):        B = get_B(l, alpha)  # small matrix of rank NOm        for i in range(0, N_Om):            for j in range(0, N_Om):                A[                    i * 2 * a+ alpha * a+ (l - 1) * (l + 1) : i * 2 * a+ (2 * l + 1)+ (l - 1) * (l + 1)+ alpha * a,                    j * 2 * a+ alpha * a+ (l - 1) * (l + 1) : j * 2 * a+ (2 * l + 1)+ alpha * a+ (l - 1) * (l + 1),                ] = B[i, j] * np.eye(                    2 * l + 1                )  # filling a diag mat for specific l and alpha

Unfortunately, the nested for loops with respect to i and j cause performance issues. Is it possible to replace these for loops with respect to i and j by some vectorized operation?


Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>