Ask Your Question
0

Forming unique collections of matrices

asked 2022-10-26 18:51:02 +0100

mak3521 gravatar image

updated 2022-10-26 18:53:39 +0100

I am facing following two similar kind of problems:

Problem(1) Suppose I have the following code:

   A=matrix(2,2,[1,1,1,1])
   B=matrix(2,2,[2,2,2,2])
   C=matrix(2,2,[3,3,3,3])
   D=matrix(2,2,[4,4,4,4])

   L1=[A,B,C]
   L2=[C,B,A]
   L3=[A,C,D]

   show(L1,L2,L3)

Then in the output I want L1 and L3 only, as L1 and L2 contain same matrices.

Problem(2): Now suppose we have the following code:

P=matrix(2,2,[5,5,5,5])
Q=matrix(2,2,[6,6,6,6])
R=matrix(2,2,[7,7,7,7])
S=matrix(2,2,[8,8,8,8])

T1=[P,P,Q]
T2=[Q,P,P]
T3=[P,R,S]

show(T1,T2,T3)

Here also, in the output I want T1 and T3 only, as T1 and T2 contain same matrices. Note that I want T1 as it is, that is, P should be there twice.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2022-10-26 20:15:54 +0100

updated 2022-10-26 20:16:26 +0100

The short answer is to use sets. The longer answer involves some technical details: in Sage, matrices are "mutable" (see https://doc.sagemath.org/html/en/refe... for some details), and mutable things can't be elements of sets. So you can do this:

def immutable_copy(mat):  # could use a shorter name
    """
    Return a copy of matrix ``mat`` which is immutable.
    """
    return matrix(mat, immutable=True)

Then given your lists L1, L2, L3, you can test this:

set(immutable_copy(mat) for mat in L1) == set(immutable_copy(mat) for mat in L2)

It will be True for L1 and L2, false for other pairs. So you could, for example, do this:

answer = [] # what you actually want to print
seen = []   # sets you've seen so far
for L in [L1, L2, L3]:
    S = set(immutable_copy(mat) for mat in L)
    if S not in seen:
        answer.append(L)
        seen.append(S)
show(*answer)
edit flag offensive delete link more

Comments

Okay.Thanks a lot. I had posed a general question to get an idea how to do such a thing. Now, I will see how to implement that idea in a code on which I am working. If there is any problem I will get back.

mak3521 gravatar imagemak3521 ( 2022-10-27 20:31:00 +0100 )edit

@John Palmieri : Why not use Sets (with a capital S) ?

Also, why not use reduce ?

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2022-10-27 22:40:39 +0100 )edit

@Emmanuel Charpentier: maybe I'm missing something, but if I do S1 = Set(L1) and S2 = Set(L2), then S1 == S2 returns False. If I convert the matrices to being immutable, then it works the same as set, at least as far as this is concerned. So maybe I should have used Set instead?

reduce sounds good; can you provide details? It's not something I ever use myself, so I don't think of it.

John Palmieri gravatar imageJohn Palmieri ( 2022-10-27 23:21:15 +0100 )edit

I tried to implement the idea in the following code(which gives all combinations of three unique 3rd order (-1,1) circulant matrices sum of whose squares equals a given circulant matrix) but could not. Kindly guide . Also, there might be a better way to write the code which I have written. Suggest those also. The code is:

n=3
T=Tuples((-1,1),n)
X=matrix.circulant([1,-3,5]) # Given circulant matrix

MAT=[]
for a in  T:

    A=matrix.circulant(a)
    MAT.append(A)

Counter=0
for a in MAT:
   for b in MAT:
        for c in MAT:
            if ((a!=b and a!=c) and  b!=c) and ((a^2)+(b^2)+(c^2)==X):

                     Counter+=1
                     show("(",Counter,").",(a,b,c))
mak3521 gravatar imagemak3521 ( 2022-10-28 05:12:45 +0100 )edit

@mak3521: what is the connection between your recent comment and the rest of the discussion here?

John Palmieri gravatar imageJohn Palmieri ( 2022-10-28 08:13:44 +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

1 follower

Stats

Asked: 2022-10-26 18:51:02 +0100

Seen: 232 times

Last updated: Oct 26 '22