I have a numpy array of integer tuples of length 3 (x) and a numpy array of integer tuples of length 2 (y).
x = numpy.array([[3, 4, 5], [5, 12, 13], [6, 8, 10], [7, 24, 25]]) #first 4 elem
y = numpy.array([[3, 4], [4, 5], [3, 5], [5, 12]]) # first 4 elem
I am trying to compare elements within array y: [a, b] and [b, c] and [a, c] that are subsets of a single element [a, b, c] within array x. I call this function union. My loop heavy method to find the union is shown below. This is not too good for my arrays that contain 200K minimum elements.
def union(x, y):
for intx in range (len(x)):
cond1 = cond2 = cond3 = 0
for inty in range (len(y)):
if (y[inty][0] == x[intx][0] and y[inty][1] == x[intx][1]): #[a, b] & [a, b, c]
print ("condition 1 passed")
cond1 = 1
if (y[inty][0] == x[intx][1] and y[inty][1] == x[intx][2]): #[b, c] & [a, b, c]
print ("condition 2 passed")
cond2 = 1
if (y[inty][0] == x[intx][0] and y[inty][1] == x[intx][2]): #[a, c] & [a, b, c]
print ("condition 3 passed")
cond3 = 1
if (cond1 & cond2 & cond3):
print("union found with ", x[intx])
cond1 = cond2 = cond3 = 0
return
>>> union(x,y)
condition 1 passed
condition 2 passed
condition 3 passed
union found with [3 4 5]
condition 1 passed
UPDATE #1: Example 1: This set of x and y have no union:
x = numpy.array([[21, 220, 221]])
y = numpy.array([[21, 220], [20, 21], [220,3021], [1220,3621], [60,221]])
UPDATE #2: Example 2: This set of x and y have no union:
x = numpy.array([[43, 924, 925]])
y = numpy.array([[43, 924], [924, 1643], [924,4307], [72, 925]])
Example 3: Here is a set of x and y that have a union of [4, 8, 16].
x = numpy.array([[4, 8, 16], [8, 4, 16]])
y = numpy.array([[4, 8], [8, 16], [4, 16]])
Example 4: Here is a set of x and y that have a union of [12, 14, 15].
x = numpy.array([[12, 13, 15], [12, 14, 15]])
y = numpy.array([[12, 14], [12, 13], [12, 15], [14, 15]])
Summary: In general terms, array x and y will have a union of [a, b, c] if
x = numpy.array([[a, b, c], ...])
y = numpy.array([[a, b], [b, c], [a, c],...])
or random ordering in y
y = numpy.array([[...[b, c], [a, c], ... [a, b]])
So my question: Is there a numpy way to do an array-wise operation? For example, numpy.logical_and suggests that x1 and x2 must be the same shape. It's not straightforward to me to replace my if statements with isdisjoint, which is a faster method. https://stackoverflow.com/a/24478521/8275288