2

To get the impedance profile for a circuit, I am using its admittance (G) and capacitance (C) matrices.

I am using Python to compute impedance profile at different frequencies but the issue is that I need to invert the matrix to get impedance from admittance and this turns out to be a very expensive operation in a loop.

For a large matrix (1000*1000) I have, inverting a matrix using numpy.linalg.inv needs approx 1 second and therefore for a loop running 10^6 iterations below, I get unreasonably large execution time.

Need some guidance on ways to optimise this or if there's anyway to bypass matrix inversion and yet get the impedance matrix.

My code is like the code below:

A = j*2*3.14*C #j 2pi C to create susceptance matrix when multiplied by freq below.
for freq in range (1, 100000000, 100):
    Y = G + A * freq #admittance matrix
    Z = np.linalg.inv(Y) #inverse the matrix to get impedance matrix
    output[freq] = Z

I have already tried numpy.linalg.solve instead of using inversion. But the execution time remains unreasonably large.

Ξένη Γήινος
  • 2,181
  • 1
  • 9
  • 35
Dev
  • 21
  • 1
  • 1
    `np.linalg.inv` is expensive because it runs in `O(n**3)` time. It is the main bottleneck here. AFAIK, It is already very optimized as long as you use a fast BLAS (this is the case by default on most machines). You can check all your cores are doing some work during the computation. There are no really faster algorithm known so this is basically the best you can do (though the code could be slightly faster with some minor optimizations). Running 100_000 matrix inversion of size 1000x1000 is not reasonable. Are the matrices special (e.g. sparse)? – Jérôme Richard Jul 23 '23 at 13:49
  • Yes, the matrices are sparse (most of the elements are zero). Can we exploit some properties to reduce the computation here? – Dev Jul 23 '23 at 15:12
  • 1
    It might help a bit. Which matrices are sparse and how sparse are they (i.e. ratio of zero over the number of item)? Especially `Y` matters. – Jérôme Richard Jul 23 '23 at 19:40
  • What about using GPU? See the [cupy.linalg.inv](https://docs.cupy.dev/en/stable/reference/generated/cupy.linalg.inv.html) for example. – relent95 Jul 24 '23 at 04:10
  • Using a GPU can indeed significantly speed up the computation if used correctly (not enough to make this fast though). Mainstream (client-side) GPUs are fast for 32-bit floats but very slow for 64-bit floats. This means you certainly need to use 32-bit floats prior to use GPUs. Server-side GPUs are fast for 64-bit floats (though generally a bit faster for 32-bit ones) but also much more expensive. Note that 32-bit floats are generally faster on CPU too (x2). – Jérôme Richard Jul 24 '23 at 12:42
  • 1
    As it stands this is non-reproducible: we don't have your variables C etc. Please provide representative data samples so that we can run this locally. – Reinderien Jul 26 '23 at 02:39

0 Answers0