Ask Your Question

Revision history [back]

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/reference/matrices/sage/matrix/matrix0.html?highlight=set_immutable#sage.matrix.matrix0.Matrix.set_immutable 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)

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/reference/matrices/sage/matrix/matrix0.html?highlight=set_immutable#sage.matrix.matrix0.Matrix.set_immutable 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)