Derivative of manipulated spline

0 votes
I am using the InterpolatedUnivariateSpline function from the scipy.interpolate library. I know that there exists a function the evaluate the derivative of the resulting spline. What I am interested in is the derivative of the log of the spline. Is there a way to calculate this directly?
Oct 4, 2018 in Python by eatcodesleeprepeat
• 4,790 points

1 answer to this question.

0 votes

FlyingTeller's suggestion is probably optimal: the derivative of log(f) is f'/f which is easy enough to implement:

import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import InterpolatedUnivariateSpline
x = np.arange(1, 9)
y = np.sqrt(x)                                  # something to use as y-values
spl = InterpolatedUnivariateSpline(x, y)

logder = lambda x: spl.derivative()(x)/spl(x)   # derivative of log of spline

t = np.linspace(x.min(), x.max())
plt.plot(t, logder(t))


Constructing a spline based on the logarithm of data is also a reasonable approach, but it is not the same thing as the logarithm of the original spline.

if you can define a function, depending on a spline, which can be differentiated by python (analytically)

Differentiating an arbitrary function analytically is out of scope for SciPy. In the above example, I had to know that the derivative of log(x) is 1/x; SciPy does not know that. SymPy is a library for symbolic math operations such as derivatives.

It's possible to use SymPy to find the derivative of a function symbolically and then turn it, using lambdify, into a callable function that SciPy or matplotlib, etc can use.

One can also work with splines in an entirely symbolic way using SymPy but it's slow.

answered Oct 4, 2018 by Priyaj
• 58,140 points

Related Questions In Python

+2 votes
3 answers

what is the practical use of polymorphism in Python?

Polymorphism is the ability to present the ...READ MORE

answered Mar 31, 2018 in Python by anto.trigg4
• 3,440 points
+4 votes
6 answers

Use of "continue" in python

The break statement is used to "break" ...READ MORE

answered Jul 16, 2018 in Python by Omkar
• 69,170 points
+2 votes
2 answers

In a list of dictionaries, how can I find the minimum calue in a common dictionary field.

There are several options. Here is a ...READ MORE

answered Apr 10, 2018 in Python by charlie_brown
• 7,780 points
+1 vote
8 answers

Count the frequency of an item in a python list

To count the number of appearances: from collections ...READ MORE

answered Oct 18, 2018 in Python by tinitales
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 2,418 views
0 votes
0 answers
0 votes
1 answer

Derivative of manipulated spline

import numpy as np import matplotlib.pyplot as plt from ...READ MORE

answered Oct 8, 2018 in Python by Priyaj
• 58,140 points
+1 vote
1 answer

How to replace id with attribute corresponding to id of another table?

Use the following query statement and let ...READ MORE

answered Aug 8, 2018 in Python by Priyaj
• 58,140 points