[SciPy-User] scipy.interpolate.KroghInterpolator: output confusion.

Anne Archibald peridot.faceted@gmail....
Fri Oct 16 04:36:39 CDT 2009

2009/10/16 Antonio Valentino <antonio.valentino@tiscali.it>:
> Hi Anne,
> Il giorno Fri, 16 Oct 2009 03:40:09 -0400
> Anne Archibald <peridot.faceted@gmail.com> ha scritto:
>> 2009/10/16 Marco Nicoletti <nicoletti@consorzio-innova.it>:
>> > Dear all,
>> >
>> > I have found something that loks like a bug (or at least a
>> > confusing staff) in the output of KroghIntepolator class.
>> >
>> > The problem is the following:
>> >
>> > case 1
>> > from scipy import interpolate
>> > import numpy as np
>> > xi = [2, 3, 4]
>> > yi = [10,13,15]
>> > yi_der = [1, 2, 2.5]
>> > Y = np.asarray([yi, yi_der]).transpose()
>> > krogh = interpolate.KroghInterpolator(xi, Y)
>> > print krogh(2.3)
>> >>> [ 11.005    1.3525]
>> > Evaluating one point 2.3, it returns a 1-D array with array([value,
>> > derivative value]).
>> This is not what the interpolator is doing. You have constructed an
>> interpolator that takes no derivative information, but produces vector
>> values. To specify derivative information, you must repeat the x value
>> in question. So to do the interpolation you were intending, you should
>> do:
>> krogh =
>> interpolate.KroghInterpolator([2,2,3,3,4,4],[10,1,13,2,15,2.5])
>> Calling this object then yields scalars. If you want derivatives, you
>> can ask for as many as you want with krogh.derivatives, which will
>> provide you with an array, or krogh.derivative, which will provide you
>> with a particular one.
>> This way of specifying derivatives is a little peculiar, but it is
>> what the underlying algorithm needs, and it also allows you to have
>> different numbers of derivatives at each point (as with the example in
>> the docstring).
> the example seems to conflicts with the rest of the docstring that
> clearly states:
> """
>    Parameters
>    ----------
>    xi : array-like, length N
>        known x-coordinates
>    yi : array-like, N by R
>        known y-coordinates, interpreted as vectors of length R,
>        or scalars if R=1
> """

The docstring is a little confusing, I admit, because some yi may be
derivative values rather than y-values. But they are indeed vectors of
length R. This allows you to, for example, construct a cubic curve in
space, so that p(t) = (x,y,z).

> xi: N
> yi: NxR with R = number of derivatives available at each point.

This is not what the docstring says, and this is not how the code
works. You do not provide derivative values in this way. Plot the
polynomial values you get out and you will see.

> Is it a bug in the documentation?

Obviously the docstring isn't clear enough, since users are being
confused by it.

> I think that the one described in the docstring is a more handy
> interface with respect to the one of the example.

The problem with the interface you are suggesting, in which you
provide a matrix of function and derivative values, is that it forces
you to specify the same number of derivatives at each point. The
current interface allows you to specify more derivatives at some
points than others. If you want to convert, you can do something like:

xs = np.repeat(xi,2)
ys = np.ravel(np.dstack((yi,ypi)))

In the docstring itself you see an example of such construction:

>>> KroghInterpolator([0,0,1],[0,2,0])

This constructs a quadratic that is zero at zero, zero at one, and has
a derivative of 2 at one - that is, the quadratic 2*x**2-2*x. There is
no constraint on the derivative at 1.


More information about the SciPy-User mailing list