Advertisement
Guest User

Untitled

a guest
Dec 15th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.78 KB | None | 0 0
  1. def B(x, k, i, t):
  2.    if k == 0:
  3.       return 1.0 if t[i] <= x < t[i+1] else 0.0
  4.    if t[i+k] == t[i]:
  5.       c1 = 0.0
  6.    else:
  7.       c1 = (x - t[i])/(t[i+k] - t[i]) * B(x, k-1, i, t)
  8.    if t[i+k+1] == t[i+1]:
  9.       c2 = 0.0
  10.    else:
  11.       c2 = (t[i+k+1] - x)/(t[i+k+1] - t[i+1]) * B(x, k-1, i+1, t)
  12.    return c1 + c2
  13.  
  14.  
  15. def bspline(x, t, c, k):
  16.    n = len(t) - k - 1
  17.    assert (n >= k+1) and (len(c) >= n)
  18.    return sum(c[i] * B(x, k, i, t) for i in range(n))
  19.  
  20.  
  21. k = 2
  22. t = [0, 1, 2, 3, 4, 5, 6]
  23. c = [-1, 2, 0, -1]
  24.  
  25. bspline(2.5, t, c, k)
  26.  
  27. import matplotlib.pyplot as plt
  28. import numpy as np
  29. fig, ax = plt.subplots()
  30. xx = np.linspace(1.5, 4.5, 50)
  31. ax.plot(xx, [bspline(x, t, c ,k) for x in xx], 'r-', lw=3, label='naive')
  32. ax.grid(True)
  33. ax.legend(loc='best')
  34. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement