0

I am calling scipy.integrate.quad about 500 times for different integrations (x and y values). This takes a total of 46 seconds. However, 19 seconds is used by _evaluate._check_bounds alone and _prepare_x._asarray_validated takes up another 11 seconds.

Can I somehow skip those checks and perform the same Operation in 16 seconds?

vahdet
  • 6,357
  • 9
  • 51
  • 106
torpedo
  • 283
  • 2
  • 15
  • *"19 seconds is used by `_evaluate._check_bounds`"* That function is part of `scipy.interpolate.interp1d`. Are you using `interp1d` in the functions that you are integrating? – Warren Weckesser Mar 29 '19 at 14:25
  • yes, the function to integrate is the return of interp1d(x,y), where x and y are numpy vectors that describe my function (I don't have exact equations). – torpedo Mar 29 '19 at 14:31
  • What do you use for the `kind` argument of `interp1d`? – Warren Weckesser Mar 29 '19 at 14:47
  • I am not using the kind argument, so it is using linear – torpedo Mar 29 '19 at 14:48
  • 1
    In that case, using `quad` and `interp1d` is overkill. For example, for the definite integral over the full interval on which you have your x and y points, you could use [`numpy.trapz`](https://docs.scipy.org/doc/numpy/reference/generated/numpy.trapz.html). It will give the same result as `quad` applied to the linearly interpolated data. – Warren Weckesser Mar 29 '19 at 14:54
  • Have you considered wrapping directly around the fortran routine from quadpack? – newkid Mar 29 '19 at 15:49
  • To add to what @Warren Weckesser said, `np.interp` is much faster then interp1d for linear interpolation. – ev-br Mar 30 '19 at 18:50
  • With numpy.trapz I can not define the integral limits. I assume it is interpolating over all x and y. My x and y may cover a little bit of a larger range than the integral limits – torpedo Apr 01 '19 at 07:51

2 Answers2

0

Considerable performance gain is possible if you write your own wrapper for qagpe in python. scipy.integrate.quadwraps around the qagpe subroutine from Quadpack.

About developing your own wrapper and calling shared libraries in Python. This is a helpful link.

newkid
  • 1,368
  • 1
  • 11
  • 27
0

quadpy (one of my projects) vectorizes computation over the domains and the function values, so that should give you a substantial speed-up over calling scipy.quad many times.

Nico Schlömer
  • 53,797
  • 27
  • 201
  • 249