2017-08-28 202 views
0

this question我询问社区如何计算样条基础。我的目标是通过预先计算bspline basis来计算快于splev的样条,并生成一条曲线通过做basiscontrol point点积。从a new scipy.interpolate.BSpline interpolator被添加到scipyIt comes with a basis_element function,我猜可以用它来返回用来计算样条的基础。如何从scipy.interpolate.BSpline提取BSpline基础

因此,例如,使用的代码from here与下面的输入:

import numpy as np 

# Control points 
cv = np.array([[ 50., 25., 0.], 
     [ 59., 12., 0.], 
     [ 50., 10., 0.], 
     [ 57., 2., 0.], 
     [ 40., 4., 0.], 
     [ 40., 14., 0.]]) 

kv = [0, 0, 0, 0, 1, 2, 3, 3, 3, 3] # knot vector 
n = 10 # 10 samples (keeping it simple) 
degree = 3 # Curve degree 

我可以计算下面的B样条基:

[[ 1.   0.   0.   0.   0.   0.  ] 
[ 0.2962963 0.56481481 0.13271605 0.00617284 0.   0.  ] 
[ 0.03703704 0.51851852 0.39506173 0.04938272 0.   0.  ] 
[ 0.   0.25  0.58333333 0.16666667 0.   0.  ] 
[ 0.   0.07407407 0.54938272 0.36728395 0.00925926 0.  ] 
[ 0.   0.00925926 0.36728395 0.54938272 0.07407407 0.  ] 
[ 0.   0.   0.16666667 0.58333333 0.25  0.  ] 
[ 0.   0.   0.04938272 0.39506173 0.51851852 0.03703704] 
[ 0.   0.   0.00617284 0.13271605 0.56481481 0.2962963 ] 
[ 0.   0.   0.   0.   0.   1.  ]] 

使用np.dotbasiscontrol points返回10个样品上曲线:

[[ 50.   25.   0.  ] 
[ 55.12654321 15.52469136 0.  ] 
[ 55..19753086 0.  ] 
[ 53.41666667 9.16666667 0.  ] 
[ 53.14506173 7.15432099 0.  ] 
[ 53.1882716 5.179.  ] 
[ 51.58333333 3.83333333 0.  ] 
[ 47.20987654 3.87654321 0.  ] 
[ 42.3179.7345679 0.  ] 
[ 40.   14.   0.  ]] 

问题:是否可以从scipy.interpolate.BSpline中提取上述基础?

很显然,我必须使用它错了,因为当我尝试,我得到这样的:

from scipy.interpolate import BSpline 
b = BSpline.basis_element(kv) 
print b(np.linspace(kv[0],kv[-1],n)) # i'm not sure what these values represent 
[ 0.   0.00256299 0.04495618 0.16555213 0.28691315 0.28691315 
    0.16555213 0.04495618 0.00256299 0.  ] 

回答

2

BSpline.basis_element作为其参数的内部节。

在你的榜样,你补齐疙瘩,而且没有做什么你想的那样:

In [3]: t = [0, 0, 0, 0, 1, 2, 3, 3, 3, 3] 

In [4]: b = BSpline.basis_element(t) 

In [5]: b.k 
Out[5]: 8 

所以这是一个8阶样条。

如果你想要一个二次样条,你会做

In [7]: b1 = BSpline.basis_element([0, 1, 2, 3]) 

In [8]: b1.k 
Out[8]: 2 

In [9]: b1.t 
Out[9]: array([-1., -1., 0., 1., 2., 3., 4., 4.]) 

困惑?方法很简单:https://github.com/scipy/scipy/blob/v0.19.1/scipy/interpolate/_bsplines.py#L243-L302

BSpline.basis_element返回的可调用实际上是一个b样条函数函数。与数组参数调用它的结果是那么相当于直接运行在你的阵列中的每个元素的循环的BSpline文档字符串的示例代码,https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.BSpline.html

编辑:如果你COX-的变体后是de Boor在给定点处计算所有非零样条的算法,那么你可以看看_bspl.evaluate_all_bsplines函数,https://github.com/scipy/scipy/blob/v0.19.1/scipy/interpolate/_bspl.pyx#L161 (它本身只是一个C程序的包装,它完成所有繁重的工作;注意它是硬件击败后面一个表现明智)。

然而,它不是公共职能,所以它不是保证金在未来的版本中可用。如果您对它有很好的使用,并提供面向用户的API的建议,请将讨论交给scipy bug跟踪器。

+0

感谢您澄清。我被术语混淆了。 – Fnord