I have a function is_prime(n) which returns True if n is prime and False otherwise. In NumPy I am looping, checking if an array contains primes, and the start of the array will be identical through every iteration, so I want to memoize the is_prime(n) function to avoid a lot of unnecessary calculations.
Since I have an array, I want to vectorize is_prime(n) so I can apply it on arrays element by element, NumPy style. I do this with one line from the NumPy tutorial (shown later)
I also use a memoization template I found on the net:
def memoize(function):
cache = {}
def decorated_function(*args):
if args in cache:
return cache[args]
else:
val = function(*args)
cache[args] = val
return val
return decorated_function
Then:
is_prime = memoize(is_prime)
BUT, is V_prime now correctly memoized if i now vectorize the memoized is_prime function?:
V_prime = np.vectorize(is_prime)
Thank you