python - How to vectorize this function? -


here integration routine found on question see use scipy.integrate.quad integrate complex numbers. happy functionality wish able perform integration on input function different values of parameter 't' , using vectorization.

import numpy np scipy.integrate import quad  def complex_quadrature(func, a, b, t):     def real_func(x,t):         return np.real(func(x,t))     def imag_func(x,t):         return np.imag(func(x,t))     real_integral = quad(real_func, a, b, args=(t))     imag_integral = quad(imag_func, a, b, args=(t))     return (real_integral[0] + 1j*imag_integral[0], real_integral[1:], imag_integral[1:])   vcomplex_quadrature = np.vectorize(complex_quadrature)   t = np.linspace(1,4,4)  print(vcomplex_quadrature(lambda x, t: (np.exp(1j*x*t)), 0, np.pi/2, t)) 

can provide this?

you want fourier transform. use numpy.fft or scipy.fft instead.

for more general function, there no way. np.vectorize convenience function loop under hood, no performance gain here.

any integral can expressed ode. express independent integrals set of odes , feed them solver in scipy, damage accuracy because solver uses adaptive step. when have several equations, choice of step same of them, , estimated on general behaviour, instead of "personalised" individual step.

the news problem embarrassingly parallelisable, means can write parallel map , use cores in machine. give better performance boost numpy vectorisation.

for simple parallelisation in python, use joblib


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -