Ask Your Question
2

Generate a random non-singular matrix?

asked 2019-02-28 23:44:45 -0500

anonymous user

Anonymous

I do this in a loop now:

while:  
    A = random_matrix(...)  
    if(not A.is_singular()):  
        ....

But this is innefficient. How do I generate random matrices with more concrete properties in sage?

edit retag flag offensive close merge delete

2 answers

Sort by » oldest newest most voted
1

answered 2019-03-01 00:04:04 -0500

slelievre gravatar image

The documentation helps with that.

It can be accessed using

sage: random_matrix?

and in particular hints at

sage: random_matrix(algorithm='unimodular')

which will return a matrix of determinant one.

edit flag offensive delete link more

Comments

What about constructing a matrix of determinant K?

rijndaelxyz gravatar imagerijndaelxyz ( 2019-03-01 00:43:34 -0500 )edit

One could start from a determinant one matrix and multiply one row or one column by K.

One could then multiply by another random determinant one matrix (on the left or on the right or both) to mix things up a bit more.

slelievre gravatar imageslelievre ( 2019-03-01 10:35:13 -0500 )edit
1

answered 2019-03-01 13:39:47 -0500

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
edit flag offensive delete link more

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: 2019-02-28 23:44:45 -0500

Seen: 63 times

Last updated: Mar 01