- #1
maverick280857
- 1,789
- 4
Hi,
I am working on a device simulation algorithm, and am implementing it in Python on my laptop that runs Arch Linux. A particular step requires me to perform a computation of the form
[tex]\tilde{G} = G \Sigma_{c}^{in} G^{\dagger}[/tex]
where [itex]G[/itex] and [itex]\Sigma_{c}^{in}[/itex] are both [itex]N \times N[/itex] matrices, and [itex]\Sigma_{c}^{in}[/itex] is in fact a diagonal matrix.
N is of the order of 5040
Now, if I do this naively, the command to do it in Python is
(The addition is for purposes of sub-band summation, and isn't relevant to the question I'm posing.)
but this takes about 2 hours when it works (sometimes it just crashes my laptop).
Now if I utilize the fact that [itex]\Sigma_{c}^{in}[/itex] is diagonal, then I can do something more naive and write a for loop of the form
But this doesn't look like a nice piece of code: it has 3 nested for loops, and looks a rather ancient way of doing the following calculation
[tex]\tilde{G}_{ij} = \sum_{\alpha}\sum_{\beta} G_{i\alpha}(\Sigma_{c}^{in})_{\alpha\beta}(G^{\dagger})_{\beta j} = \sum_{\alpha,\beta} G_{i\alpha}(\Sigma_{c}^{in})_{\alpha\alpha}\delta_{\alpha\beta} (G^{\dagger})_{\beta j} =\sum_{\alpha} G_{i\alpha}(\Sigma_{c}^{in})_{\alpha\alpha}G^{*}_{j\alpha}[/tex]
Is there some way to vectorize this?
In general, is there a better way to perform this computation?
I am working on a device simulation algorithm, and am implementing it in Python on my laptop that runs Arch Linux. A particular step requires me to perform a computation of the form
[tex]\tilde{G} = G \Sigma_{c}^{in} G^{\dagger}[/tex]
where [itex]G[/itex] and [itex]\Sigma_{c}^{in}[/itex] are both [itex]N \times N[/itex] matrices, and [itex]\Sigma_{c}^{in}[/itex] is in fact a diagonal matrix.
N is of the order of 5040
Now, if I do this naively, the command to do it in Python is
Code:
Gtilde_n = Gtilde_n + dot( dot(G, sigcIn), conjugate(transpose(G)) )
(The addition is for purposes of sub-band summation, and isn't relevant to the question I'm posing.)
but this takes about 2 hours when it works (sometimes it just crashes my laptop).
Now if I utilize the fact that [itex]\Sigma_{c}^{in}[/itex] is diagonal, then I can do something more naive and write a for loop of the form
Code:
for row in range(0,N):
for col in range(0,N):
sum = 0
for alpha in range(0,N):
sum = sum + G[row,alpha]*sigcIn[alpha,alpha]*conjugate(G[col,alpha])
Gtilde_n[row,col] = Gtilde_n[row,col] + sum
But this doesn't look like a nice piece of code: it has 3 nested for loops, and looks a rather ancient way of doing the following calculation
[tex]\tilde{G}_{ij} = \sum_{\alpha}\sum_{\beta} G_{i\alpha}(\Sigma_{c}^{in})_{\alpha\beta}(G^{\dagger})_{\beta j} = \sum_{\alpha,\beta} G_{i\alpha}(\Sigma_{c}^{in})_{\alpha\alpha}\delta_{\alpha\beta} (G^{\dagger})_{\beta j} =\sum_{\alpha} G_{i\alpha}(\Sigma_{c}^{in})_{\alpha\alpha}G^{*}_{j\alpha}[/tex]
Is there some way to vectorize this?
In general, is there a better way to perform this computation?
Last edited: