def make_interp_per_full_matr(x, y, t, k): x, y, t = map(np.asarray, (x, y, t)) n = x.size nt = t.size - k - 1 # have `n` conditions for `nt` coefficients; need nt-n derivatives assert nt - n == k - 1 # LHS: the collocation matrix + derivatives at edges A = np.zeros((nt, nt), dtype=np.float_) # derivatives at x[0]: offset = 0 if x[0] == t[k]: left = k else: left = np.searchsorted(t, x[0]) - 1 if x[-1] == t[k]: left2 = k else: left2 = np.searchsorted(t, x[-1]) - 1 for i in range(k - 1): bb = _bspl.evaluate_all_bspl(t, k, x[0], left, nu=i + 1) A[i, left - k:left + 1] = bb bb = _bspl.evaluate_all_bspl(t, k, x[-1], left2, nu=i + 1) A[i, left2 - k:left2 + 1] = -bb offset += 1 # RHS y = np.r_[[0] * (k - 1), y] # collocation matrix for j in range(n): xval = x[j] # find interval if xval == t[k]: left = k else: left = np.searchsorted(t, xval) - 1 # fill a row bb = _bspl.evaluate_all_bspl(t, k, xval, left) A[j + offset, left - k:left + 1] = bb c = sl.solve(A, y) return c
def make_interp_per_full_matr(x, y, t, k): x, y, t = map(np.asarray, (x, y, t)) n = x.size nt = t.size - k - 1 # have `n` conditions for `nt` coefficients; need nt-n derivatives assert nt - n == k - 1 # LHS: the collocation matrix + derivatives at edges A = np.zeros((nt, nt), dtype=np.float_) # derivatives at x[0]: offset = 0 if x[0] == t[k]: left = k else: left = np.searchsorted(t, x[0]) - 1 if x[-1] == t[k]: left2 = k else: left2 = np.searchsorted(t, x[-1]) - 1 for i in range(k-1): bb = _bspl.evaluate_all_bspl(t, k, x[0], left, nu=i+1) A[i, left-k:left+1] = bb bb = _bspl.evaluate_all_bspl(t, k, x[-1], left2, nu=i+1) A[i, left2-k:left2+1] = -bb offset += 1 # RHS y = np.r_[[0]*(k-1), y] # collocation matrix for j in range(n): xval = x[j] # find interval if xval == t[k]: left = k else: left = np.searchsorted(t, xval) - 1 # fill a row bb = _bspl.evaluate_all_bspl(t, k, xval, left) A[j + offset, left-k:left+1] = bb c = sl.solve(A, y) return c
def make_interp_full_matr(x, y, t, k): """Assemble an spline order k with knots t to interpolate y(x) using full matrices. Not-a-knot BC only. This routine is here for testing only (even though it's functional). """ assert x.size == y.size assert t.size == x.size + k + 1 n = x.size A = np.zeros((n, n), dtype=np.float_) for j in range(n): xval = x[j] if xval == t[k]: left = k else: left = np.searchsorted(t, xval) - 1 # fill a row bb = _bspl.evaluate_all_bspl(t, k, xval, left) A[j, left-k:left+1] = bb c = sl.solve(A, y) return c
def make_lsq_full_matrix(x, y, t, k=3): """Make the least-square spline, full matrices.""" x, y, t = map(np.asarray, (x, y, t)) m = x.size n = t.size - k - 1 A = np.zeros((m, n), dtype=np.float_) for j in range(m): xval = x[j] # find interval if xval == t[k]: left = k else: left = np.searchsorted(t, xval) - 1 # fill a row bb = _bspl.evaluate_all_bspl(t, k, xval, left) A[j, left-k:left+1] = bb # have observation matrix, can solve the LSQ problem B = np.dot(A.T, A) Y = np.dot(A.T, y) c = sl.solve(B, Y) return c, (A, Y)
def make_lsq_full_matrix(x, y, t, k=3): """Make the least-square spline, full matrices.""" x, y, t = map(np.asarray, (x, y, t)) m = x.size n = t.size - k - 1 A = np.zeros((m, n), dtype=np.float_) for j in range(m): xval = x[j] # find interval if xval == t[k]: left = k else: left = np.searchsorted(t, xval) - 1 # fill a row bb = _bspl.evaluate_all_bspl(t, k, xval, left) A[j, left - k:left + 1] = bb # have observation matrix, can solve the LSQ problem B = np.dot(A.T, A) Y = np.dot(A.T, y) c = sl.solve(B, Y) return c, (A, Y)
def make_interp_full_matr(x, y, t, k): """Assemble an spline order k with knots t to interpolate y(x) using full matrices. Not-a-knot BC only. This routine is here for testing only (even though it's functional). """ assert x.size == y.size assert t.size == x.size + k + 1 n = x.size A = np.zeros((n, n), dtype=np.float_) for j in range(n): xval = x[j] if xval == t[k]: left = k else: left = np.searchsorted(t, xval) - 1 # fill a row bb = _bspl.evaluate_all_bspl(t, k, xval, left) A[j, left - k:left + 1] = bb c = sl.solve(A, y) return c
def make_matrix(x, y, t, k): ''' Returns diagonals and block elements of the matrix formed by points (x, y) for coefficients for B-Spline curve. Parameters ---------- x : 1-D array, shape(n,) x coordinate of points. y : 1-D array, shape(n,). y coordiante of points. t : 1-D array, shape(n+2*k,) Node vector. k : int Degree of B-splines. Returns ------- A : 2-D array, shape(k,n-1) Diagonals of the original matrix of SLE. ur : 2-D array, shape(offset,offset) Upper right block of the original matrix of SLE. ll : 2-D array, shape(offset,offset) Lower left block of the original matrix of SLE. Notes ----- SLE - system of linear equations. 'n' should be greater than 'k', otherwise corner block elements will intersect diagonals. offset = (k - 1) / 2 ''' if k % 2 == 0: raise ValueError("Degree of B-spline should be odd") n = x.size yc = np.copy(y) yc = yc[:-1] A = np.zeros((k, n - 1)) for i in range(n - 1): A[:, i] = _bspl.evaluate_all_bspl(t, k, x[i], i + k)[:-1][::-1] offset = int((k - 1) / 2) + (k + 1) % 2 ur = np.zeros((offset, offset)) ll = np.zeros((offset, offset)) for i in range(1, offset + 1): A[offset - i] = np.roll(A[offset - i], i) if k % 2 == 1 or i < offset: A[offset + i] = np.roll(A[offset + i], -i) ur[-i:, i - 1] = np.copy(A[offset + i, -i:]) ll[-i, :i] = np.copy(A[offset - i, :i]) ur = ur.T for i in range(1, offset): ll[:, i] = np.roll(ll[:, i], i) ur[:, -i - 1] = np.roll(ur[:, -i - 1], -i) return A, ur, ll