Simd
Simd

Reputation: 21274

Fast way to apply function elementwise to a numpy array

I have a sets of numpy arrays which I create using

for longtuple in itertools.product([0,1], repeat = n + m -1 ):
    outputs = set(np.convolve(v, longtuple, 'valid').tostring() for v in itertools.product([0,1], repeat = m))
    if (len(outputs) == 2**m):
        print "Hooray!"

However I would actually like to take every element x of np.convolve(v, longtuple, 'valid') and apply x >> k & 1 (for values of k that I will specify) and then add that resulting array to the set instead. Is there an efficient way to do this?


My use of set and tostring() is simply to see if there are any duplicates. I am not sure it is correct however.

Upvotes: 0

Views: 346

Answers (1)

BrenBarn
BrenBarn

Reputation: 251363

You can just take the result of convolve and apply your expression to it:

set((np.convolve(v, longtuple, 'valid') >> k & 1).tostring() for v in itertools.product([0,1], repeat = m))

Upvotes: 1

Related Questions