I'm trying to implement a specific binary search algorithm. "Results" should be an empty set in the beginning, and during the search, Results variable will become a union with the new results that we get.
Basically:
results = set()
for result in search():
results = results.union(result)
But such code won't really work with Numpy arrays, so we use np.union1d
for this purpose:
results = np.array([])
for result in search():
result = np.union1d(results, result)
The code above doesn't really work either, since if we have for example two vectors a = [1,2,3]
and b=[3,4,5]
, np.union1d(a, b)
will return:
[1, 2, 3, 4, 5]
But I want it to return:
[[1, 2, 3], [3,4,5]]
Since there are no duplicate vectors, if we had for example union([[1, 2, 3], [3,4,5]], [1,2,3])
, return value shall remain:
[[1, 2, 3], [3,4,5]]
So I would say that I require a numpy array based union.
I also considered using np.append(a, b)
and then np.unique(x)
, but both of the functions project lower dimensional array to higher dimensional one. np.append
also has axis=0
property, which retains dimension of all arrays inserted, but I couldn't efficiently implement it without getting dimension error.
Question:
How can I efficiently implement a vector based set? So that points in the union will be considered as vectors instead of scalars, and will retain their vector form and dimension.