Mattia Rovetta
Mattia Rovetta

Reputation: 103

Calculate the smoothness of a SciPy cubic spline

I am trying to calculate the smoothness of cubic splines generated using scipy.interpolate.CubicSpline. The way I want to calculate the smoothness is by taking the integral of the square of the second derivative:

eq

The SciPy CubicSpline accepts the integrate and derivative methods, but they are not enough for the calculation above. What I can easily do is .derivative(2).integrate(0,20)**2, but this is not quite what I need (I am taking the square after the integral is being calculated).

Is there an easy way to perform the calculation above? Am I missing something stupid?

Upvotes: 0

Views: 165

Answers (1)

ev-br
ev-br

Reputation: 26090

You're almost there: you need to cook up a callable for the second derivative squared:

>>> spl = CubicSpline(x, y)
>>> der = spl.derivative(2)
>>> from scipy.integrate import quad
>>> quad(lambda x: der(x)**2, 0, 1)     # <--- here

Upvotes: 1

Related Questions