Was not sure how exactly to word the question but here is the problem:
I essentially have an empty array S that is of shape n * n. I also have an array of data R, of shape n * m. My goal is for S[i][j] = some func combining R[i] R[j].
This is the code I wrote to accomplish what I want:
for i in range(n):
for j in range(n):
S[i][j] = foo(R[i], R[j])
I'd like to know if there is an existing numpy function that can do something like this, so I do not have to write the for loops.
In numpy we generally write
footo to be "array aware" to avoid needing to loop over our inputs. For example, you could take this functioncrossAddFand write an array aware versioncrossAddArray:That being said, I realize that we sometimes need to work with functions that weren't meant for numpy that we can't easily re-write or change. In that case
numpy.vectorizecan be useful. Keep in mind thatvectorizecan be slower than other implementations of the same functionality. Here's an example:My examples also use numpy's broadcasting feature it might be worth reading about if you're not already familiar with it.