'Compute a definite integral of the spline.
Parameters
a : float
Lower limit of integration.
b : float
Upper limit of integration.
extrapolate : bool or \'periodic\', optional
whether to extrapolate beyond the base interval,
``t[k] .. t[-k-1]``, or take the spline to be zero outside of the
base interval. If \'periodic\', periodic extrapolation is used.
If None (default), use `self.extrapolate`.
Returns
I : array_like
Definite integral of the spline over the interval ``[a, b]``.
Examples
Construct the linear spline ``x if x < 1 else 2 - x`` on the base
interval :math:`[0, 2]`, and integrate it
>>> from scipy.interpolate import BSpline
>>> b = BSpline.basis_element([0, 1, 2])
>>> b.integrate(0, 1)
array(0.5)
If the integration limits are outside of the base interval, the result
is controlled by the `extrapolate` parameter
>>> b.integrate(-1, 1)
array(0.0)
>>> b.integrate(-1, 1, extrapolate=False)
array(0.5)
>>> import matplotlib.pyplot as plt
>>> fig, ax = plt.subplots()
>>> ax.grid(True)
>>> ax.axvline(0, c=\'r\', lw=5, alpha=0.5)  # base interval
>>> ax.axvline(2, c=\'r\', lw=5, alpha=0.5)
>>> xx = [-1, 1, 2]
>>> ax.plot(xx, b(xx))
>>> plt.show()'

def integrate(self, a, b, extrapolate=None):

    if (extrapolate is None):
        extrapolate = self.extrapolate
    self._ensure_c_contiguous()
    sign = 1
    if (b < a):
        (a, b) = (b, a)
        sign = (-1)
    n = ((self.t.size - self.k) - 1)
    if ((extrapolate != 'periodic') and (not extrapolate)):
        a = max(a, self.t[self.k])
        b = min(b, self.t[n])
        if (self.c.ndim == 1):
            (t, c, k) = self.tck
            (integral, wrk) = _dierckx._splint(t, c, k, a, b)
            return (integral * sign)
    out = np.empty((2, prod(self.c.shape[1:])), dtype=self.c.dtype)
    c = self.c
    ct = (len(self.t) - len(c))
    if (ct > 0):
        c = np.r_[(c, np.zeros(((ct,) + c.shape[1:])))]
    (ta, ca, ka) = _fitpack_impl.splantider((self.t, c, self.k), 1)
    if (extrapolate == 'periodic'):
        (ts, te) = (self.t[self.k], self.t[n])
        period = (te - ts)
        interval = (b - a)
        (n_periods, left) = divmod(interval, period)
        if (n_periods > 0):
            x = np.asarray([ts, te], dtype=np.float_)
            _bspl.evaluate_spline(ta, ca.reshape(ca.shape[0], (-1)), ka, x, 0, False, out)
            integral = (out[1] - out[0])
            integral *= n_periods
        else:
            integral = np.zeros((1, prod(self.c.shape[1:])), dtype=self.c.dtype)
        a = (ts + ((a - ts) % period))
        b = (a + left)
        if (b <= te):
            x = np.asarray([a, b], dtype=np.float_)
            _bspl.evaluate_spline(ta, ca.reshape(ca.shape[0], (-1)), ka, x, 0, False, out)
            integral += (out[1] - out[0])
        else:
            x = np.asarray([a, te], dtype=np.float_)
            _bspl.evaluate_spline(ta, ca.reshape(ca.shape[0], (-1)), ka, x, 0, False, out)
            integral += (out[1] - out[0])
            x = np.asarray([ts, ((ts + b) - te)], dtype=np.float_)
            _bspl.evaluate_spline(ta, ca.reshape(ca.shape[0], (-1)), ka, x, 0, False, out)
            integral += (out[1] - out[0])
    else:
        x = np.asarray([a, b], dtype=np.float_)
        _bspl.evaluate_spline(ta, ca.reshape(ca.shape[0], (-1)), ka, x, 0, extrapolate, out)
        integral = (out[1] - out[0])
    integral *= sign
    return integral.reshape(ca.shape[1:])
