Ask Your Question

Revision history [back]

You could do it one row at a time by choosing random vectors:

def random_nonsingular_matrix(base_ring=QQ, size=3):
    V = base_ring**size
    vectors = []
    for i in range(size):
        v = V.random_element()
        while v in V.span(vectors):
            v = V.random_element()
        vectors.append(v)
    return(matrix(vectors))

Examples:

sage: random_nonsingular_matrix(size=4).determinant()
1337/102
sage: random_nonsingular_matrix(size=4).determinant()
127/16
sage: random_nonsingular_matrix(size=4).determinant()
815/16
sage: random_nonsingular_matrix(size=4).determinant()
96533/326

It's unfortunately much slower than random_matrix:

sage: %timeit random_nonsingular_matrix(size=20)
10 loops, best of 3: 84.9 ms per loop
sage: %timeit random_matrix(QQ, 20, 20)
10000 loops, best of 3: 58.2 ┬Ás per loop