# how to convert Spline to Piecewise?

I want to get the result analytic expression of a spline. How to get it? Thanks for help.

edit retag close merge delete

Sort by » oldest newest most voted

spline from GSL is a numerical tool. To obtain symbolic values you can use maxima:

sage: maxima('load(interpol)')
sage: maxima('p:[[7,2],[8,2],[3,2],[6,7]]')
[[7,2],[8,2],[3,2],[6,7]]
sage: # Sage symbolic expressions
sage: p1=maxima('part(cspline(p),1,1)').sage();p1
-95/279*x^3 + 95/31*x^2 - 415/93*x - 3
sage: p2=maxima('part(cspline(p),2,1)').sage();p2
-140/93*x^3 + 1120/31*x^2 - 26740/93*x + 23582/31
sage: p3=maxima('part(cspline(p),3,1)').sage();p3
235/93*x^3 - 1505/31*x^2 + 28385/93*x - 19293/31
sage: # maxima expression for comparison
sage: maxima('cspline(p)')
(-95*x^3/279+95*x^2/31-415*x/93-3)*charfun2(x,minf,6)+(-140*x^3/93+1120*x^2/31-26740*x/93+23582/31)*charfun2(x,7,inf)+(235*x^3/93-1505*x^2/31+28385*x/93-19293/31)*charfun2(x,6,7)

more

Do you think it would be worth wrapping these, and would they agree with the GSL ones?

Some primitive comparison GSL/maxima:

#Sage spline
p=[[7,2],[8,2],[3,2],[6,7]]
f=spline(p)
[f(k) for k in[3..8]]    # k have to be in [3,8]
[2.0, 6.39068100358423, 8.738351254480287, 7.0, 2.0, 1.9999999999999996]
[f(k+0.5) for k in[3..7]]
[4.323028673835125, 7.947580645161291, 8.507616487455198, 4.318548387096774, 1.4354838709677418]

/*maxima cspline*/
p:[[7,2],[8,2],[3,2],[6,7]];
c:cspline(p);
makelist(ev(subst(k,x,c),numer),k,3,8);    /* k can be outside of [3,8]*/
[1.999999999999998,6.39068100358423,8.738351254480285,6.999999999999545,2.000000000000341,2.000000000000455]
makelist(ev(subst(k+0.5,x,c),numer),k,3,7);
[4.323028673835125,7.947580645161292,8.507616487455195,4.318548387096712,1.435483870968369]

more

Yeah, maybe connecting this with Piecewise, primitive as that is...