-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbsplines.py
More file actions
88 lines (76 loc) · 2.2 KB
/
bsplines.py
File metadata and controls
88 lines (76 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# -*- coding: UTF-8 -*-
#! /usr/bin/python
import numpy as np
from scipy.interpolate import splev
import matplotlib.pyplot as plt
def make_knots(n_elements,p,kind="open"):
"""
creates a knot vector
"""
if kind == "open":
T = np.linspace(0.,1.,n_elements+1)
T = [0.] *p + list(T) + [1.] * p
T = np.array(T)
else:
raise NotImplemented("not implemented")
return T
class Bspline(object):
def __init__(self, T, p):
"""
initialize splines for given knot sequence t and degree p
"""
self.T = T
self.p = p
self.N = len(T) - p - 1
self.c = np.zeros(self.N)
def __call__(self, x, i=None, n_deriv=0):
"""
evaluate b-spline starting at node i at x
"""
if i is not None:
c = np.zeros_like(self.T)
if i < 0:
c[self.N + i]=1.
else:
c[i]=1.
else:
c = self.c
tck = (self.T, c, self.p)
return splev(x, tck, der=n_deriv)
def greville(self):
"""
Returns the Greville points
"""
p = self.p
T = self.T
# TODO implement a pure python function and not use igakit
from igakit import igalib
return igalib.bsp.Greville(p, T)
def plot(self, nx=100):
"""
Plots all splines constructed from a knot sequence
"""
T = self.T
p = self.p
N = self.N
x = np.linspace(0.0,1.0,nx)
y = np.zeros((N,nx), dtype=np.double)
for i in range(0,N):
y[i]=self(x, i=i)
plt.plot(x,y[i])
class UniformBspline(Bspline):
# TODO add xmin and xmax as arguments for the interval bouondaries
def __init__(self, p, N):
"""
creates a uniform bspline
"""
L = list(range(-p, N + p + 1))
T = np.array(L, dtype=np.float)
Bspline.__init__(self, T, p)
class CardinalBspline(UniformBspline):
def __init__(self, p):
"""
creates a uniform bspline
"""
N = p + 1
UniformBspline.__init__(self, p, N)