How multiarray.correlate2(a, v, mode) is actually implemented?
The speed of python code can be very poor compared to other languages like c. numpy
aims to provide highly performant operations on arrays, therefore the developers decided to implement some operations in c
.
Unfortunately, won't find a python implementation of correlate
in numpy
's code base, but if you are familiar with C
and python
's extension modules, you can have find the relevant code here.
The different modes just specify the length of the output array. You can simulate them by transforming your inputs:
import numpy as np
a = [1, 2, 3]
v = [0, 1, 0.5]
np.correlate(a, v, mode="full")
returns:
array([ 0.5, 2. , 3.5, 3. , 0. ])
You can get the same result by filling v
with zeros:
np.correlate(a, [0, 0] + v + [0, 0])
returns the same result:
array([ 0.5, 2. , 3.5, 3. , 0. ])