jmquintana79
1/15/2018 - 6:02 AM

most optimal loop function

The most optimal loop for function with arrays.

import numpy as np

# define function
def f(x,y,p):
    if x<y: return x+y-p
    elif x>=y: return x*y/p
    
# define arrays
n = 1000000.
a = np.arange(0.,n,0.1)
b = np.arange(-n,n,0.2)

# define the vectorized function
vf = np.vectorize(f,otypes=[np.float],cache=False, excluded=['p'])
# launch the vectorized function
resutl = vf(a,b,2)