OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to vectorize Scipy.integrate.quad()

  • Thread starter Thread starter Aleksejs Fomins
  • Start date Start date
A

Aleksejs Fomins

Guest
I have a 1D function f(t) defined on an interval [t0, t1]. I would like to obtain an integral of this function, uniformly sampled over the above interval with a timestep delta_t. The naive solution for this is to use scipy.integrate.quad inside of a python for-loop

Code:
rez = np.zeros(nStep-1)
for i in range(1, nStep):
    rez[i-1] = scipy.integrate.quad(my_func, t0, t0 + i * delta_t)

I presume that this is not the fastest way to perform such an integral. Is there an intended way to do this operation in a vectorized form?

One improvement I can think of is to do piecewise integration, then sum up, because, for every iteration, the integrator will have to deal with simpler functions (less wiggles).

Code:
rez = np.zeros(nStep-1)
for i in range(1, nStep):
    rez[i-1] = scipy.integrate.quad(my_func, t0 + (i - 1) * delta_t, t0 + i * delta_t)
rez = np.cumsum(rez)
<p>I have a 1D function <code>f(t)</code> defined on an interval <code>[t0, t1]</code>. I would like to obtain an integral of this function, uniformly sampled over the above interval with a timestep <code>delta_t</code>. The naive solution for this is to use <a href="https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.quad.html" rel="nofollow noreferrer">scipy.integrate.quad</a> inside of a python for-loop</p>
<pre><code>rez = np.zeros(nStep-1)
for i in range(1, nStep):
rez[i-1] = scipy.integrate.quad(my_func, t0, t0 + i * delta_t)
</code></pre>
<p>I presume that this is not the fastest way to perform such an integral. Is there an intended way to do this operation in a vectorized form?</p>
<p>One improvement I can think of is to do piecewise integration, then sum up, because, for every iteration, the integrator will have to deal with simpler functions (less wiggles).</p>
<pre><code>rez = np.zeros(nStep-1)
for i in range(1, nStep):
rez[i-1] = scipy.integrate.quad(my_func, t0 + (i - 1) * delta_t, t0 + i * delta_t)
rez = np.cumsum(rez)
</code></pre>
 

Latest posts

Top