It is my understanding that the itertools functions are written in C. If i wanted to speed this example code up:
import numpy as np
from itertools import combinations_with_replacement
def combinatorics(LargeArray):
newArray = np.empty((LargeArray.shape[0],LargeArray.shape[0]))
for x, y in combinations_with_replacement(xrange(LargeArray.shape[0]), r=2):
z = LargeArray[x] + LargeArray[y]
newArray[x, y] = z
return newArray
Since combinations_with_replacement is written in C, does that imply that it can’t be sped up? Please advise.
Thanks in advance.
It’s true that
combinations_with_replacementis written in C, which means that you’re not likely to speed up the implementation of that part of the code. But most of your code isn’t spent on finding the combinations: it’s on theforloop that does the additions. You really, really, really want to avoid that kind of loop if at all possible when you’re using numpy. This version will do almost the same thing, through the magic of broadcasting:For example:
The difference is that
combinatoricsleaves the lower triangle as random gibberish, wheresumsmakes the matrix symmetric. If you really wanted to avoid adding everything twice, you probably could, but I can’t think of how to do it off the top of my head.Oh, and the other difference: