3

I have a complex 2D array (HH) of which I want to do the inverse and I only want its first term, iterated in 3 for loop, . For M, C, K (2D arrays) of reasonable size it doesn't take long, but if I increase their size the wait becomes infinite. One loop inside the third loop to calculate HH[0,0] takes around 0.19 sec, imagine to iterate 2x20x500 times it takes a bunch of time. How can I speed up the process?

Since I can't upload M, C and K I replace them with this: inv((np.random.rand(1000,1000)+np.random.rand(1000,1000)*1j)

import numpy as np
import numpy.linalg.inv as inv

num_harms = 2 # or 3                                                                            
num_modes = 20                                                                                                                                                                         
freq = np.linspace(1.0,4.0,500) #range force freq                                                     
w = 2*np.pi*freq                                                                                                                                                                 
H11_1 = np.zeros((num_harms,num_modes,len(w)),dtype = 'complex_') # dim(num_harms,num_modes,step_freq)
for h in range(0,num_harms):                                                                       
    for j in range(0,num_modes):                                                                   
        for iw in range(0,len(w)):     
            # HH = inv((-((h+1)*w[iw])**2)*M[j]+(1j*(h+1)*w[iw]*C[j])+K[j])
            HH = inv((np.random.rand(1000,1000)+np.random.rand(1000,1000)*1j)
            H11_1[h,j,iw] = HH[0,0]

I also tried this to make it little faster:

H11_1 = np.zeros((num_harms,num_modes,len(w)),dtype = 'complex_') # dim(num_harms,num_modes,step_freq) 
for h in range(0,num_harms):
    for j in range(0,num_modes):
        for iw in range(0,len(w)):    
            H = ((-((h+1)*w[iw])**2)*M[j]+(1j*(h+1)*w[iw]*C[j])+K[j])            
            H_1 = (np.linalg.det(A))
            H11_1[h,j,iw] = H_1

But it appears this warning:

RuntimeWarning: overflow encountered in det                                                          
r = _umath_linalg.det(a, signature=signature)

I debug it to see H_1 value and it is: (-inf-infj)

Bruce
  • 31
  • 2
  • What do you mean by inverse? Are you talking about `M**(-1)`? Beside this, please provide a minimal **working** example (for the first code). They are many unknown variables and `len(w) = 500` clearly does not works. – Jérôme Richard Apr 30 '21 at 21:57
  • By inverse I mean the numpy fucntion numpy.linalg,inv() – Bruce Apr 30 '21 at 23:14
  • If you only need HH^(-1)[0,0] you don't need to inverse the whole matrix. I just hate to pick up my old textbook, but it can probably be done in O(N) time. – Bing Wang Apr 30 '21 at 23:27
  • To get the element H[0,0] I need the inverse of the whole matrix HH and not just the reciprocal of the first element. – Bruce Apr 30 '21 at 23:45
  • No you don't need to inverse the whole thing. Read this https://math.stackexchange.com/questions/1517510/finding-the-specific-entry-of-inverse-matrix. I recall that is – Bing Wang May 02 '21 at 12:28
  • One possibility would be to use whatever you know about your actual matrices (rather than your random example). Can you say anything about M[], C[] and K[]? – dmuir May 07 '21 at 12:05

0 Answers0