Ask Your Question
1

Generating special random matrices in sage

asked 2025-01-28 17:27:55 +0200

sagequstions gravatar image

updated 2025-01-28 17:28:05 +0200

Is there an easy way to generate a random $n \times n$-matrix in Sage that is lower triangular with ones on the diagonal and the entries are all in QQ or ZZ(or the natural numbers)?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2025-01-28 23:04:54 +0200

Max Alekseyev gravatar image

Here is an example:

n = 5
Matrix(ZZ,n,n,lambda i,j: 1 if i==j else ZZ.random_element() if i>j else 0)
edit flag offensive delete link more

Comments

Thank you, is there also a way to get only natural numbers instead of integers? I tried this, but it does not seem to work:

n = 14
PP = PositiveIntegers()
M=Matrix(PP,n,n,lambda i,j: 1 if i==j else PP.random_element() if i>j else 0)
sagequstions gravatar imagesagequstions ( 2025-01-29 01:44:24 +0200 )edit

You can use abs(ZZ.random_element()).

Max Alekseyev gravatar imageMax Alekseyev ( 2025-01-29 03:40:20 +0200 )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: 2025-01-28 17:27:55 +0200

Seen: 236 times

Last updated: Jan 28