[Numpy-discussion] Correlate with small arrays

Robert Kern robert.kern@gmail....
Wed Mar 19 13:59:19 CDT 2008

On Wed, Mar 19, 2008 at 12:57 PM, Peter Creasey
<p.e.creasey.00@googlemail.com> wrote:
> Hi,
>  I'm trying to do a PDE style calculation with numpy arrays
>  y = a * x[:-2] + b * x[1:-1] + c * x[2:]
>  with a,b,c constants. I realise I could use correlate for this, i.e
>  y = numpy.correlate(x, array((a, b, c)))
>  however the performance doesn't seem as good (I suspect correlate is
>  optimised for both arguments being long arrays). Is the first thing I
>  wrote probably the best? Or is there a better numpy function for this
>  case?

The relative performance seems to vary depending on the size, but it
seems to me that correlate usually beats the manual implementation,
particularly if you don't measure the array() part, too. len(x)=1000
is the only size where the manual version seems to beat correlate on
my machine.

Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
 -- Umberto Eco

More information about the Numpy-discussion mailing list