I have n indices that result in n(n-1)/2 pairwise combinations, e.g. for n=3
(i,j,k) -> (i,j), (i,k), (j,k)
Now for each of these pairs I know the possibilities, e.g.
(i,j) = (1,2), (1,3), (2,2)
(i,k) = (2,2), (1,2), (2,4)
(j,k) = (1,2), (4,3), (2,2)
In other words, in some combination (i,j,k) we must have that (i,j) is (1,2) or (1,3) or (2,2) and the same for the other pairs. I wish to construct all combinations that are possible, so in the above example there are only two possible combinations:
(i,j,k) = (2,2,2)
(i,j,k) = (1,2,2)
I have currently implemented this procedure as follows:
import numpy as np
ij = np.array(([1,2], [1,3], [2,2]))
ik = np.array(([2,2], [1,2], [2,4]))
jk = np.array(([1,2], [4,3], [2,2]))
possibilities = []
possible_i = np.union1d(ij[:,0], ik[:,0])
possible_j = np.union1d(ij[:,1], jk[:,0])
possible_k = np.union1d(ik[:,1], jk[:,1])
for i in possible_i:
for j in possible_j:
if ([i,j] == ij).all(1).any():
for k in possible_k:
if (([i,k] == ik).all(1).any() and
([j,k] == jk).all(1).any()):
print(i,j,k)
Although this works and can be easily adapted to any n, it does not seem very efficient to me as it for example checks the combinations:
1 2 2
1 2 2
1 2 3
1 2 4
1 3 2
1 3 3
1 3 4
2 2 2
2 2 2
2 2 3
2 2 4
Of course, we know after checking that (i,j,k) = (i,2,3) is not valid, we don't have to recheck the other combinations of this form. Is there a more efficient way to tackle this task (that also applies works for higher n)?
