-2

I can not understand what s(xx) does in this code.

import numpy as np
import matplotlib.pyplot as plt
import scipy.signal as sps
import scipy.interpolate as spi

# plot cubic cardinal B-spline (knots 0, 1, 2, 3, 4)
p = 3
xx = np.linspace(0, p+1, 100)
yy = sps.bspline(xx - (p+1)/2, p)
plt.plot(xx, yy)
plt.show()

# plot cubic non-uniform spline (m=5 DOFs)
xi = [0, 1, 3, 4, 6, 7, 8, 10, 11]
c = [2, -1, 1, 0, 1]
s = spi.BSpline(xi, c, p)
m = len(c)
xx = np.linspace(xi[p], xi[m])
yy = s(xx)
plt.plot(xx, yy)
plt.show()

I tried to just run s(xx) part to see what s() does, but it is throwing error s() is not defined, but when I execute the whole code, it works.

martineau
  • 119,623
  • 25
  • 170
  • 301

2 Answers2

2

Look, it comes from a few lines above:

s = spi.BSpline(xi, c, p)

spi.BSpline is returning a callable object as per the docs, that gets invoked like so:

s(xx)
Óscar López
  • 232,561
  • 37
  • 312
  • 386
2

This is s = spi.BSpline(xi, c, p) in your program, that you have defined yourself. Nothing else specific exists in python for s.

lifeisshubh
  • 513
  • 1
  • 5
  • 27
  • 1
    See https://bsplines.org/scientific-programming-with-b-splines: 1) `s = spi.BSpline(xi, c, p)` constructs the spline s; then 2) `yy = s(xx)` evaluates the spline. "s" *WILL* be undefined unless you initialize it with "s = spi.BSpline(xi, c, p)" first. – paulsm4 Apr 02 '19 at 16:38