[SciPy-user] Arrayfns in numpy?

Alok Singhal as8ca@virginia....
Tue Mar 6 07:19:20 CST 2007

On 06/03/07: 10:54, Stephen Kelly wrote:
>    I don't want to depend on scipy for a small task like this. I want to keep
>    dependencies small.
>    I looked at the searchsorted function, but don't see how it would be
>    useful.

Given a sorted arry a, and some values v,

ind = searchsorted(a, v)

returns ind such that a[ind[i]] > v[i] > a[ind[i]-1] (I hope I got
that right :-) ).

>    My data is not regularly spaced. It is X-ray diffraction data in
>    which each intensity has been shifted slightly. I am currently using
>    arrayfns from Numeric to get data with regular spacing. Could you give
>    more information on how to interpolate the data? I don't know where to
>    start.

Here is how I would go about it:

import numpy

# Test data (non-uniform spacing in x)
x = numpy.array([0.0, 3.5, 6.0, 10.0, 15.0, 25.0])
y = numpy.array([13.0, 6.0, -6.0, 0.0, 4.0, 18.0])

# The data values for which interpolation is required.  xx[0] should
# be > x[0], and xx[-1] should be < x[-1].  Otherwise, undefined
# behavior.
xx = numpy.mgrid[0.5:25:0.5]

# High indices
hi = numpy.searchsorted(x, xx)

# Low indices
lo = hi - 1

slopes = (y[hi] - y[lo])/(x[hi] - x[lo])

# Interpolated data
yy = y[lo] + slopes*(xx - x[lo])

# To plot using matplotlib:
import pylab
pylab.plot(x, y, 'ro', xx, yy, 'b')

>    As an aside, here's some things in Numeric that aren't in numpy. Is this
>    oversight, or are there no plans to implement them?
>    1. arrayfns module
>    2. UserArray module with UserArray class (comparible to UserDict,
>    UserList).

I don't know the answer to that - maybe interpolation etc., are more
suited to be in a 'scientific library' than an 'array library', so the
creators of scipy/numpy moved the functionality of those modules to
scipy and removed the modules from numpy?  I don't know what else
(except interpolation) did the UserArray class/module have, so I can't
say for sure.


Alok Singhal                               *   *          
Graduate Student, dept. of Astronomy   *           *     *
University of Virginia                                    
http://www.astro.virginia.edu/~as8ca/              *    * 

More information about the SciPy-user mailing list