Ask Your Question
0

Regarding remove() function

asked 2023-03-11 13:11:21 +0100

mak3521 gravatar image

I want to remove all symmetric matrices from the list of all possible 0,1 matrices of (say) 2nd order. When I run this code, not all symmetric matrices are removed. Where am I wrong?`

        T=Tuples((0,1),4)
        W=[matrix(2,2,v) for v in T]
        [W.remove(s) for s in W if s.is_symmetric() is true]
        show(W)
edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
2

answered 2023-03-11 16:26:08 +0100

Max Alekseyev gravatar image

updated 2023-03-11 16:35:42 +0100

Single-line solution:

W = [M for v in Tuples((0,1),4) if not (M:=Matrix(2,2,v)).is_symmetric()]
edit flag offensive delete link more

Comments

Thanks a lot Max

mak3521 gravatar imagemak3521 ( 2023-03-11 18:32:53 +0100 )edit
1

It's slightly faster in this case to not construct the matrix unless necessary: W = [Matrix(2,2,v) for v in Tuples((0,1),4) if v[1] != v[2]]. This approach is of course more awkward when dealing with larger matrices.

John Palmieri gravatar imageJohn Palmieri ( 2023-03-11 21:39:47 +0100 )edit
1

And this is slightly faster: [Matrix(2,2,v) for v in [v for v in Tuples((0,1),4) if v[1] != v[2]]]: construct the appropriate list of tuples first and then convert to matrices. Maybe a bad idea if the list of tuples is going to be long. (I don't know why this should be any faster.)

John Palmieri gravatar imageJohn Palmieri ( 2023-03-11 21:43:07 +0100 )edit

Thanks John. Yes, your idea is good. Yes, for higher orders this is going to be complicated.

mak3521 gravatar imagemak3521 ( 2023-03-12 11:56:18 +0100 )edit

@John: Then one can simply do [Matrix(2,2,v[:2]+[1-v[1],v[2]]) for v in Tuples((0,1),3)].

Max Alekseyev gravatar imageMax Alekseyev ( 2023-03-12 19:41:04 +0100 )edit
1

answered 2023-03-11 16:08:36 +0100

achrzesz gravatar image

updated 2023-03-12 17:55:40 +0100

In your code W defines the range of the loop but changes inside.

It can be improved as follows:

T=Tuples((0,1),4)
W=[matrix(2,2,v) for v in T]
V=W.copy()
[V.remove(s) for s in W if s.is_symmetric()]
V

[
[0 1]  [1 1]  [0 0]  [1 0]  [0 1]  [1 1]  [0 0]  [1 0]
[0 0], [0 0], [1 0], [1 0], [0 1], [0 1], [1 1], [1 1]
]

but it is better not to construct to many matrices:

import itertools
def  f():
    f1=lambda x:matrix(2,2,x)
    f2=lambda v:v[1]!=v[2]
    T=(item for item in itertools.product([0,1], repeat=4))
    return list(map(f1,filter(f2,T)))
f()

[
[0 0]  [0 0]  [0 1]  [0 1]  [1 0]  [1 0]  [1 1]  [1 1]
[1 0], [1 1], [0 0], [0 1], [1 0], [1 1], [0 0], [0 1]
]

timeit('f()')

625 loops, best of 3: 40.7 μs per loop

In intel python, replacing matrix by numpy.array one can obtain two times faster code

edit flag offensive delete link more

Comments

1

Why not use list comprehension right away?

[m for m in mm if not m.is_symmetric()]

Max Alekseyev gravatar imageMax Alekseyev ( 2023-03-11 16:19:23 +0100 )edit

Thanks a lot achrzesz

mak3521 gravatar imagemak3521 ( 2023-03-11 18:32:21 +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: 2023-03-11 13:11:21 +0100

Seen: 336 times

Last updated: Mar 12 '23