SciPy interpolate sproot missing a zero crossing

Viewed 102

I've got a weird situation where scipy.interpolate.sproot is missing a root for some cases. I can't find any obvious reason, but it seems quite easy to reproduce.

I'm using SciPy 1.4.1 and Python 3.7.6

This code snippet will produce a few cases that work and a few cases that don't:

import numpy as np
import scipy.interpolate as spInterp
import matplotlib.pyplot as plt

testX = np.linspace(0, 1, 1000)
testY = np.sin(testX*5*np.pi)

for i in range(10):
    i *= 20
    off = testY[int(i)]
    tck = spInterp.splrep(testX, testY-off)
    bspl = spInterp.BSpline(tck[0], tck[1], tck[2])
    roots = spInterp.sproot(bspl)
    plt.plot(testX, bspl(testX))
    plt.axhline(0)
    for r in roots:
        plt.axvline(r)
    plt.show()

The figure shows one of the plots produced, the vertical lines show the identified roots and it's clear where one is missing.

offset sinusoid with unidentified root

EDIT: Found an issue on the SciPy github that mentions this and a possible work around: https://github.com/scipy/scipy/issues/8376

0 Answers
Related