Speed up X property of Circuit class
@@ -742,31 +742,15 @@ class Circuit():
----
There is a numerical bottleneck in this function,
when creating the block diagonal matrice [X] from the [X]_k matrices.
- The difficulty comes from the fact that the shape of each [X]_k
- matrix is `fxnxn`, so there is a loop over the frequencies
- to create f times a block matrix [X]. I am still looking for a
- vectorized implementation but didn't find it.
-
"""
- # Xk = []
- # for cnx in self.connections:
- # Xk.append(self._Xk(cnx))
- # Xk = np.array(Xk)
-
- # #X = np.zeros(len(C.frequency), )
- # Xf = []
- # for (idx, f) in enumerate(self.frequency):
- # Xf.append(block_diag(*Xk[:,idx,:]))
- # return np.array(Xf) # shape: (nb_frequency, nb_inter*nb_n, nb_inter*nb_n)
-
- # Slightly faster version
Xks = [self._Xk(cnx) for cnx in self.connections]
Xf = np.zeros((len(self.frequency), self.dim, self.dim), dtype='complex')
- # TODO: avoid this for loop which is a bottleneck for large frequencies
- for idx in np.nditer(np.arange(len(self.frequency))):
- mat_list = [Xk[idx,:] for Xk in Xks]
- Xf[idx,:] = block_diag(*mat_list) # bottleneck
+ off = np.array([0, 0])
+ for Xk in Xks:
+ Xf[:, off[0]:off[0] + Xk.shape[1], off[1]:off[1]+Xk.shape[2]] = Xk
+ off += Xk.shape[1:]
+
return Xf
@property
