How is multarray.correlate2 (a, v, mode) implemented?

On my way to understand how the Numpy.correlate() function really works, I get an implementation for it in pure Python, but what I saw was very disappointing:

 def correlate(a, v, mode='valid', old_behavior=False): mode = _mode_from_name(mode) if old_behavior: warnings.warn("""Warning.""", DeprecationWarning) return multiarray.correlate(a, v, mode) else: return multiarray.correlate2(a, v, mode) 

So, I started looking for an implementation of the multiarray.correlate2(a, v, mode) function, but, unfortunately, I cannot find it. I will just say that I am looking for it because I myself am trying to implement an autocorrelation function, and I miss the functionality similar to the mode='full' parameter in Numpy.correlate() , which forces the function to return the result as 1D. Thank you for your help in advance.

+6
source share
1 answer

The speed of python code can be very slow compared to other languages ​​like c. numpy aims to provide high-performance operations on arrays, so the developers decided to implement some operations in c .

Unfortunately, it will not find the python correlate implementation in the numpy database, but if you are familiar with the c and python extension modules, you can find the corresponding code here .

In different modes, the length of the output array is indicated. 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 zeros:

 np.correlate(a, [0, 0] + v + [0, 0]) 

returns the same result:

 array([ 0.5, 2. , 3.5, 3. , 0. ]) 
+7
source

Source: https://habr.com/ru/post/979549/


All Articles