[SciPy-User] help interpreting univariate spline
Anne Archibald
peridot.faceted@gmail....
Fri Apr 30 09:04:55 CDT 2010
On 26 April 2010 15:26, Elliot Hallmark <permafacture@gmail.com> wrote:
> On Fri, Apr 23, 2010 at 6:53 PM, Elliot Hallmark <permafacture@gmail.com> wrote:
>
>> I am wanting to use a scipy interpolation routine to generate
>> polynomials approximating function I have sampled.
>> ...
>> import numpy as np
>>from scipy.interpolate import splrep, UnivariateSpline
>
>>x = np.linspace(1, 10, 100)
>>y = 1/x #approximate a hyperbola
>>g = UnivariateSpline(x, y, w=None, k=3, s=.0005)
>>print g.get_knots()
>>print g.get_coeffs()
>
>>and the output was:
>>[ 1. 2.18181818 3.27272727 5.54545455 10. ]
>>
>>[ 0.98462432 0.66575572 0.423 0.25461626 0.13929847 0.11763985
> 0.09929961]
>
>>That is 5 knots and 7 coefficients for a degree=3 spline. naively, I
>>was expecting 4 coefficents for each interval between knots.
>
> I found this post:
>
> http://mail.scipy.org/pipermail/scipy-user/2009-June/021572.html
>
> This seems very round-a-bout and ignores the piecewise nature of the
> spline. I mean, really? is there no direct or documented way to get
> the polynomial coefficents from a spline in python? If not, then this
> code is the best I have found. For my purposes I have modified it
> (see below)
There is indeed no direct way to get the polynomials in a familiar
basis. (I'll point out that you lose accuracy - possibly a lot of it -
by converting polynomials from one representation to another.) If all
you want to do is solve the polynomials, though, scipy already
provides root-finding functionality in its splines, based on routines
built into FITPACK. Also integration and differentiation.
If you really want the polynomials, though, the tck representation
scipy uses is semi-standard for representing splines; you may find the
non-object-oriented interface (splrep, splev, etc.) somewhat less
opaque in this respect. If you do decide to decipher the results, keep
in mind that with the knots held fixed, it's a linear representation
of the space of piecewise-cubic functions, so if you can find
representations for your basis functions (e.g. 1, x, x**2, x**3) you
can easily work out the conversion. And since the interpolating spline
for each of those functions is itself, all you need to do is four
interpolations on a fixed set of knots.
Anne
More information about the SciPy-User
mailing list