Hello,

I needed to get all elements of a vector-space over a finite field. Here are 
three approaches, each ~ 10x faster than the previous:

A = random_matrix(GF(5), 5, 10)
#------
timeit('all2 = A.row_space().list()')
5 loops, best of 3: 1.07 s per loop
#------
timeit('all = [i*A for i in VectorSpace(GF(5),5)]')
5 loops, best of 3: 166 ms per loop
#------
def free_mod_iter(module):
    R = module.base_ring()
    L = module.gens()
    if len(L) == 1:
        for i in R:
            yield i * L[0]
    else:
        el = L[0]
        for rest in gen(L[1:], R): 
            for i in R:
                yield rest + i * el

timeit('list(free_mod_iter(A.row_space()))')
25 loops, best of 3: 14.7 ms per loop

sage: list(free_mod_iter(A.row_space())).sort() == 
A.row_space().list().sort()
True

Is the 3rd solution, already in Sage? It's basically the same as a generator 
for rational points on elliptic curves that Robert Bradshaw showed me at 
Sage Days 22. If not, it probably should be, but I am not sure where it fits 
(free_module.py is a 5k loc).

-- 
To post to this group, send an email to sage-devel@googlegroups.com
To unsubscribe from this group, send an email to 
sage-devel+unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sage-devel
URL: http://www.sagemath.org

Reply via email to