Skip to content
DeepForgeby svx
All problems
HardCalculusca-133

Cubic Spline Segment Value

Problem

Evaluate a cubic Hermite spline segment on [x0, x1] with endpoint values y0, y1 and endpoint slopes m0, m1.

With h = x1 - x0 and t = (x - x0)/h, use the basis S = h00*y0 + h10*h*m0 + h01*y1 + h11*h*m1, where h00 = 2t^3 - 3t^2 + 1, h10 = t^3 - 2t^2 + t, h01 = -2t^3 + 3t^2, h11 = t^3 - t^2. Return S(x).

Hint

At t = 0 the basis reduces to (y0, m0 h) and at t = 1 to (y1, m1 h).

Test cases

case 1
in: [0,0,1,2,4,3,1]
expected: 1.5
case 2
in: [0,1,0,1,2,0,0.25]
expected: 1.15625
case 3
in: [0,1,2,1,5,6,0]
expected: 1
case 4
in: [0,1,2,1,5,6,1]
expected: 5
case 5
in: [0,0,0,1,1,0,0.5]
expected: 0.5
Starter codePython
def cubic_spline_value(x0, y0, m0, x1, y1, m1, x):
    # Your code here
    pass

5 test cases · run your code against them in the editor