Ask Your Question
1

Forming Combinations with conditions

asked 2022-09-20 06:47:18 +0100

sgmth gravatar image

Suppose B is a finite collection of distinct square matrices of nth order.

And , A a subcollection of B.

I want unique combinations of four distinct elements -three from A and one from B.

I tried this

X=Combinations(A,3)
Y=Combinations(B,1)
for i in range(len(X)):
    for j in range(3):
        for k in range(len(Y)):
            if ((X[i])[j])!=(Y[k]):
                show(X[i]+Y[k])

I know problem is with my "if" command.

My question is how to change the “if” command so that none of the matrices in X[i] equals Y[k], so that we get distinct matrices in a combination.

Further, how to get unique combinations.

Thanks.

edit retag flag offensive close merge delete

Comments

Let $C$ be the set difference of $B$ and $A$. Then construct combinations of 3 elements from $A$ and one from $C$, or all 4 elements from $A$.

Max Alekseyev gravatar imageMax Alekseyev ( 2022-09-20 17:01:29 +0100 )edit

I think that it is acceptable if all four elements end up coming from A, so either choose the combination c from A first and then take the set difference of B and c and choose one element from that, or choose one element y from B and take combinations from B \setminus {y}.

John Palmieri gravatar imageJohn Palmieri ( 2022-09-20 18:51:34 +0100 )edit

Yes, it is acceptable if all elements come from A. Thanks for your further insights into the problem.

sgmth gravatar imagesgmth ( 2022-09-20 19:38:16 +0100 )edit

1 Answer

Sort by » oldest newest most voted
1

answered 2022-09-20 16:52:47 +0100

updated 2022-09-20 18:49:21 +0100

Let's use sets. Matrices must be made "immutable" to be members of sets, so:

B_list = [random_matrix(ZZ, 3) for n in range(8)]
for b in B_list:
    b.set_immutable()
B = set(B_list)

A_list = B_list[:5]
A = set(A_list)

A combination of size 1 is just an element of the set, or if you like, Y will range over all subsets of B of size 1. Then we can choose X to range over all size 3 subsets of A but with Y removed:

for y in B:
    X = Combinations(A.difference(set([y])), 3)
    for x in X:
        # now combine the combination x with the matrix y however you would like
        print(x, y)

You can also form a set of these combinations and update it with a new set formed with x and y — a set since presumably you don't care about the order among y and the elements of x. Since sets ignore duplicates, you will only get unique combinations.

edit flag offensive delete link more

Comments

Thanks for your invaluable insights into the problem and for your crystal clear explanation .

sgmth gravatar imagesgmth ( 2022-09-20 19:42:55 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2022-09-20 06:47:18 +0100

Seen: 220 times

Last updated: Sep 20 '22