Goc*_*hit 11 python matlab scipy
我想通过scipy计算样条插值的系数.在MATLAB中:
x=[0:3];
y=[0,1,4,0];
spl=spline(x,y);
disp(spl.coefs);
Run Code Online (Sandbox Code Playgroud)
它将返回:
ans =
-1.5000 5.5000 -3.0000 0
-1.5000 1.0000 3.5000 1.0000
-1.5000 -3.5000 1.0000 4.0000
Run Code Online (Sandbox Code Playgroud)
但我不能通过在scipy中使用interpolate.splrep来做到这一点.你能告诉我如何计算它吗?
我不确定有没有办法从scipy中获得那些系数.是什么scipy.interpolate.splrep让你对绳结的B样条系数.Matlab的样条曲线给出的似乎是描述连接传入点的三次方程的部分多项式系数,这使我相信Matlab样条曲线是基于控制点的样条曲线,例如Hermite或Catmull-Rom而不是b样条.
然而,scipy.interpolate.interpolate.spltopp确实提供了获得b样条的部分多项式系数的方法.不幸的是,它看起来效果不佳.
>>> import scipy.interpolate
>>> x = [0, 1, 2, 3]
>>> y = [0, 1, 4, 0]
>>> tck = scipy.interpolate.splrep(x, y)
>>> tck
Out:
(array([ 0., 0., 0., 0., 3., 3., 3., 3.]),
array([ 3.19142761e-16, -3.00000000e+00, 1.05000000e+01,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
0.00000000e+00, 0.00000000e+00]),
3)
>>> pp = scipy.interpolate.interpolate.spltopp(tck[0][1:-1], tck[1], tck[2])
>>> pp.coeffs.T
Out:
array([[ -4.54540394e-322, 0.00000000e+000, 0.00000000e+000,
0.00000000e+000],
[ -4.54540394e-322, 0.00000000e+000, 0.00000000e+000,
0.00000000e+000],
[ -4.54540394e-322, 0.00000000e+000, 0.00000000e+000,
0.00000000e+000],
[ 0.00000000e+000, 0.00000000e+000, 0.00000000e+000,
0.00000000e+000],
[ 0.00000000e+000, 0.00000000e+000, 0.00000000e+000,
0.00000000e+000]])
Run Code Online (Sandbox Code Playgroud)
请注意,每个结有一组系数,而不是传递给每个原始点的系数.另外,将系数乘以b样条基矩阵似乎不是很有用.
>>> bsbm = array([[-1, 3, -3, 1], [ 3, -6, 3, 0], [-3, 0, 3, 0],
[ 1, 4, 1, 0]]) * 1.0/6
Out:
array([[-0.16666667, 0.5 , -0.5 , 0.16666667],
[ 0.5 , -1. , 0.5 , 0. ],
[-0.5 , 0. , 0.5 , 0. ],
[ 0.16666667, 0.66666667, 0.16666667, 0. ]])
>>> dot(pp.coeffs.T, bsbm)
Out:
array([[ 7.41098469e-323, -2.27270197e-322, 2.27270197e-322,
-7.41098469e-323],
[ 7.41098469e-323, -2.27270197e-322, 2.27270197e-322,
-7.41098469e-323],
[ 7.41098469e-323, -2.27270197e-322, 2.27270197e-322,
-7.41098469e-323],
[ 0.00000000e+000, 0.00000000e+000, 0.00000000e+000,
0.00000000e+000],
[ 0.00000000e+000, 0.00000000e+000, 0.00000000e+000,
0.00000000e+000]])
Run Code Online (Sandbox Code Playgroud)
FORTRAN分段多项式包,PPPack,具有bsplpp从B样条转换为分段多项式形式的命令,可以满足您的需要.不幸的是,目前还没有针对PPPack的Python包装器.