微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

scipy splrep 是否为 t 给出错误的最后一个元素?

如何解决scipy splrep 是否为 t 给出错误的最后一个元素?

以下示例尝试使用 splrep 的输出从有限元(基元)重建估计。它似乎可以达到极限。

这里使用 splrep 和 base_elements 的目的是什么。 base_element 的规范有点奇怪,只有结点。

from pylab import *
import numpy as np
from scipy.interpolate import splrep,splev,BSpline

# degree of spline
k = 1

np.random.seed(1)
x = np.linspace(0,1,11)
y = np.sin(x) + np.random.randn(11)
t = np.linspace(0.1,0.9,6)

n_knots = len(t)

def t_extend(t,n):
    tmin = round(t.min())
    tmin = tmin - 1 if tmin == t.min() else tmin
    tmax = round(t.max())
    tmax = tmax + 1 if tmax == t.max() else tmax
    l = [tmin] * n
    # r = [tmax] * n
    r = [tmax] * (n - 1) + [tmax + 1]
    t = np.hstack([l,t,r])
    return t

t_ex = t_extend(t,k + 1)

tck = splrep(x,y,t=t,task=-1,k=k,xb=0,xe=1)
f = lambda x: splev(x,tck)
# NOTE: the last k + 1 elements of c are zero (unused)
c = tck[1][:-(k + 1)]

xx = np.linspace(0,1001)

basis = list()
for i in range(len(t_ex) - (k + 1)):
    knots = t_ex[i:i + k + 2]
    assert len(knots) == (k + 2)
    b = BSpline.basis_element(knots,extrapolate=False)
    basis.append(b)

assert len(c) == len(basis)

B = np.zeros((len(xx),len(c)))
for i in range(len(c)):
    B[:,i] = np.nan_to_num(basis[i](xx),0)

yb = B @ c
    
fig = figure(figsize=(12,6))
plot(x,'o')
plot(xx,f(xx),'-',alpha=0.5,label='sprep')
plot(xx,yb,label='basis element reconstruction')
_ = legend()
title('why do boundary terms disagree?')

fig = figure(figsize=(30,4))
ax = fig.subplots(1,len(c))
for i in range(len(c)):
    ax[i].plot(xx,B[:,i],'k-',alpha=0.25)
    
    
tck[0],t_ex

出于某种原因,splrep 给出了一个以 1,1 结尾的 tck[0] (t),但您需要一个以 1,2 结尾的以匹配结果。

(array([0.,0.,0.1,0.26,0.42,0.58,0.74,1.,1.  ]),array([0.,2.  ]))

附加的图像是不包含修复时重建的样子。

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。