2

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

1 Answer 1

5

Well lets test it.

import numpy as np

def test(input):
    return input

def memoize(function):
    cache = {}
    def decorated_function(*args):
        if args in cache:
            print 'cached'
            return cache[args]
        else:
            print 'not cached'
            val = function(*args)
            cache[args] = val
            return val
    return decorated_function

test = memoize(test)
print test(9)
print test(9)
test = np.vectorize(test)
print test(9)
print test(10)
print test(10)

I get this on my machine.

not cached
9
cached
9
cached
cached
9
not cached
10
cached
10

so yes, it is memoize, on my machine using numpy 1.6.1

Sign up to request clarification or add additional context in comments.

2 Comments

Oh well, that was clever! Thank you!
no problem, memoization is quite cool, though be careful since it can consume quite a bit of memory depending on what types of objects you are working with... people tend to use dedicated objects instead of dictionaries to control/monitor memory and destroy/remove objects when memory is low or they haven't accessed them in a while.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.