I am writing an algorithm that has a common scenario. I have two large arrays of integers. Call them k and j (because thats what I called them in my test code).
I take each element of k, and I take it's union with each element of j, BUT ONLY IF the intersection of the two elements is zero.
Example:
j = np.zeros(3, dtype=int)
k = np.zeros(3, dtype=int)
k[0] = 2
k[1] = 6
k[2] = 10
j[0] = 2
j[1] = 8
j[2] = 1
for i in xrange(3):
mask = (j&k[i] == 0)
nextpart = (j|k[i])[mask]
print i, nextpart
print ""
0 [10 3]
1 [14 7]
2 [11]
Is this method actually efficient? My algorithm is mostly based upon this operation right here, so it will make a large difference to know the best way to do this.
For every two arrays, I want to output all of the results into a single 1d array. As we can see, with the method above, I am outputting to different arrays. I imagine I can use some sort of concatenation method, but I also suspect based on the little I know about arrays that this might be relatively time consuming. I could use some advice on how to accomplish this efficiently.
Thank you.
The question 2 is a bit confusing. Anyway, you can do $ and | operations efficiently using broadcasting.
import numpy as np
j = np.zeros(3, dtype=int)
k = np.zeros(3, dtype=int)
k[0] = 2
k[1] = 6
k[2] = 10
j[0] = 2
j[1] = 8
j[2] = 1
# alter shape to broadcast
k_ = k[None].T
r = (j|k_)[j&k_==0]
print(r)
This results in
[10 3 14 7 11]