0
votes

I have this code:

from itertools import product    
from numpy import zeros
Ysys = zeros((2*n_bus, 2*n_bus))
for a, b in product(range(n_bus), range(n_bus)):
    Ysys[2*a, 2*b] = Yseries[a, b].real
    Ysys[2*a, 2*b+1] = -Yseries[a, b].imag
    Ysys[2*a+1, 2*b] = Yseries[a, b].imag
    Ysys[2*a+1, 2*b+1] = Yseries[a, b].real

Yseries is a square sparse matrix of n_bus by n_bus dimensions that contains complex numbers. Essentially I want to "explode" the complex matrix into a float matrix.

My for loop is inefficient since I should know in advance which are the row, column coordinates of every data entry. Ideally a would be an array with all the row coordinates of every entry, and b should be an array with all the column coordinates of every entry.

My problem is that I don't understand the arrays indices and indptr contained in my CSR matrx Yseries.

Regardless of all that I've said, I'd appreciate indications of how to vectorize the for loop.

1

1 Answers

1
votes

I solved it by passing the matrix to COO sparse type:

m = Yseries.tocoo()
a = m.row
b = m.col
Ysys[2 * a, 2 * b] = m.data.real
Ysys[2 * a, 2 * b + 1] = -m.data.imag
Ysys[2 * a + 1, 2 * b] = m.data.imag
Ysys[2 * a + 1, 2 * b + 1] = m.data.real

After measuring the execution time, for dimension = 30 I get:

  • For loop (original question): 0.06154400000000004 s
  • Vectorized (this answer): 0.00011099999999997223 s