How can I set and get coefficients of bspline intepolant Functions?

262 views
Skip to first unread message

程迪

unread,
Aug 23, 2019, 3:35:53 AM8/23/19
to CasADi
Hi everyone, 

I am trying to use pchip interpolation to avoid overshoot. But this is not implemented in casadi now. So I decide to use scipy's implementation to generate coefficients and transfer the coefficients to cascadi to generate Function object. 

Here is an example:

"""
bspline(str name, [[float]] knots, [float] coeffs, [int] degree, int m, dict opts) -> Function
"""
import casadi as ca

name
= 'lut_test'
knots
= [[0,3]]
coeffs
= [1,1,1] #
degree
= [2] # polynomial degree
m
= 1 # dimension

lut
= ca.Function.bspline(name,knots,coeffs,degree)
lut
(1)

I expected to get

# (x-0)**3+(x-0)**2+(x-0)
lut
(1)=3

BUT I get some error:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-83-f83e334e4f2a> in <module>
     
8 m = 1 # dimension
     
9
---> 10 func = ca.Function.bspline(name,knots,coeffs,degree)

~/share/casadi/casadi.py in bspline(*args)
 
12953
 
12954         """
> 12955         return _casadi.Function_bspline(*args)
  12956
  12957     bspline = staticmethod(bspline)

RuntimeError: Error in Function::bspline for 'lut_test' [BSpline] at .../casadi/core/function.cpp:698:
.../casadi/core/function_internal.cpp:124: Error calling BSpline::init for 'lut_test':
.../casadi/core/bspline.cpp:136: Assertion "
coeffs_size_==coeffs_.size()" failed:
Expected coefficient size -2, got 4 instead.

Where is the negative expected coefficient size comes from? Any help?

I also posted an issue: https://github.com/casadi/casadi/issues/2477

程迪

unread,
Aug 24, 2019, 8:17:37 AM8/24/19
to CasADi
Sorry, I have some confusion of concepts. I mixed the bspline with piecewise-polynomial

Refer to scipy, there are three kind of interpolating tools: 
1. PPoly: piecewise polynomial. `pchip` belongs to this kind and is 1st order continuously differentiable.
2. BPoly: piecewise Bernstein polynomial. This is a special flavor of piecewise polynomial. BPoly and PPoly can be converted to each other.
3. BSpline: B-spline is globally influenced but locally supported, they cannot be converted to BPoly or PPoly easily. But it can be very smooth and have continuous curvature.

so bspline() cannot be used to implement evaluation of `pchip` 
Reply all
Reply to author
Forward
0 new messages