| 1 | initial version |
In yor 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:
mm=(matrix(2,2,m) for m in Tuples((0,1),4))
mu=(m for m in mm if not m.is_symmetric())
list(mu)
| 2 | No.2 Revision |
In yor 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:
mm=(matrix(2,2,m) for m in Tuples((0,1),4))
mu=(m for m in mm if not m.is_symmetric())
list(mu)
| 3 | No.3 Revision |
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:
mm=(matrix(2,2,m) import itertools
def f():
f1=lambda x:matrix(2,2,x)
f2=lambda v:v[1]!=v[2]
T=(item for m item in Tuples((0,1),4))
mu=(m for m in mm if not m.is_symmetric())
list(mu)
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
Copyright Sage, 2010. Some rights reserved under creative commons license. Content on this site is licensed under a Creative Commons Attribution Share Alike 3.0 license.