Ask Your Question
2

How can I recover the command to generate an object already created randomly?

asked 2014-11-28 02:00:04 +0100

mathochist gravatar image

I'm working in Sage side by side with a linear algebra test I'm writing in LaTeX (using sagetex). I'm generating random matrices until I find one I really like. Say I get this in Sage:

B = random_matrix(ZZ, 3, 3, algorithm='echelonizable', rank=3, upper_bound=10); B 
[ 1 -2  1]
[ 0  1 -2]
[-1 -2  8]

Is there a way to get Sage to generate the command to generate this matrix nonrandomly, i.e, return the command

B = matrix(QQ, [[1, -2 ,1], [0, 1, -2], [-1, -2, 8]])

so that I can copy and paste it into a sagesilent environment in my .tex file?

I can't think of what I might search for in the Sage documentation for such a command so I must ask here. Many thanks.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2014-11-28 12:24:49 +0100

ndomes gravatar image

Do you need a plain string or latex representation?

sage: B = random_matrix(ZZ, 3, 3, algorithm='echelonizable', rank=3, upper_bound=10)
sage: print B
sage: S = "matrix(QQ,%s)"%B.rows(); S; B = eval(S); B; latex(B) 

[ 1  3 -9]
[-1 -2  6]
[ 0 -1  4]
'matrix(QQ,[(1, 3, -9), (-1, -2, 6), (0, -1, 4)])'
[ 1  3 -9]
[-1 -2  6]
[ 0 -1  4]
\left(\begin{array}{rrr}
1 & 3 & -9 \\
-1 & -2 & 6 \\
0 & -1 & 4
\end{array}\right)
edit flag offensive delete link more

Comments

The string was all I needed;

"matrix(QQ,%s)"%B.rows()

is exactly what I was looking for.

I'm glad to know what will do the trick now, but I don't completely understand it. Can you explain the %s and the %B.rows() notation, particularly the % part of it, or else point me to the documentation for it? Again it's proving difficult to search for. Thanks again, though; it's a big help... I was getting tired of copying the matrix and then filling in a bunch of commas by hand!

mathochist gravatar imagemathochist ( 2014-11-28 20:11:21 +0100 )edit

It's the old way of python string formatting. In this context % is a string formatting operator. For more details see python tutorials, for example: http://www.diveintopython.net/native_data_types/formatting_strings.html

ndomes gravatar imagendomes ( 2014-11-28 23:05:28 +0100 )edit

In the new way of formatting: "matrix(QQ,{})".format(B.rows())

paulmasson gravatar imagepaulmasson ( 2017-01-03 22:03:13 +0100 )edit
3

answered 2014-11-28 14:32:19 +0100

tmonteil gravatar image

updated 2017-01-03 21:47:52 +0100

Pseudo-random number generators use a seed to start with, and this seed can be controlled by Sage. For example:

sage: set_random_seed(42)
sage: B = random_matrix(ZZ, 3, 3, algorithm='echelonizable', rank=3, upper_bound=10); B
[ 5 -4  7]
[-5  6 -2]
[-4  3 -6]
sage: B = random_matrix(ZZ, 3, 3, algorithm='echelonizable', rank=3, upper_bound=10); B
[ 1 -3  4]
[ 0  1 -1]
[ 0 -3  4]
sage: B = random_matrix(ZZ, 3, 3, algorithm='echelonizable', rank=3, upper_bound=10); B
[-2 -1 -9]
[ 1  0  5]
[ 1 -2  8]

Now if i try again, i got the same sequence, because i use the same seed:

sage: set_random_seed(42)
sage: B = random_matrix(ZZ, 3, 3, algorithm='echelonizable', rank=3, upper_bound=10); B
[ 5 -4  7]
[-5  6 -2]
[-4  3 -6]
sage: B = random_matrix(ZZ, 3, 3, algorithm='echelonizable', rank=3, upper_bound=10); B
[ 1 -3  4]
[ 0  1 -1]
[ 0 -3  4]
sage: B = random_matrix(ZZ, 3, 3, algorithm='echelonizable', rank=3, upper_bound=10); B
[-2 -1 -9]
[ 1  0  5]
[ 1 -2  8]

For your workflow (waiting to find a nice matrix and pick only that one), you can do as follows, knowing that set_random_seed() without arguments picks a random new seed:

sage: set_random_seed() ; B = random_matrix(ZZ, 3, 3, algorithm='echelonizable', rank=3, upper_bound=10); B
[ 3  2 -9]
[ 1  1 -2]
[ 2  2 -3]
sage: set_random_seed() ; B = random_matrix(ZZ, 3, 3, algorithm='echelonizable', rank=3, upper_bound=10); B
[ 1 -1  2]
[ 0  1 -5]
[-2  0  7]
sage: set_random_seed() ; B = random_matrix(ZZ, 3, 3, algorithm='echelonizable', rank=3, upper_bound=10); B
[ 2 -1  9]
[ 1  0  5]
[ 0 -5 -4]
sage: set_random_seed() ; B = random_matrix(ZZ, 3, 3, algorithm='echelonizable', rank=3, upper_bound=10); B
[ 1  0 -1]
[-1 -1  4]
[ 0  1 -4]

If you like this one, you can ask what is the current seed:

sage: s = initial_seed() ; s
326656179144558892533217995600680485655L

Then you can construct this matrix again:

sage: set_random_seed(326656179144558892533217995600680485655L)
sage: B = random_matrix(ZZ, 3, 3, algorithm='echelonizable', rank=3, upper_bound=10); B
[ 1  0 -1]
[-1 -1  4]
[ 0  1 -4]

EDIT: Actually, the sage_input function does what you want out of the box:

sage: B = random_matrix(ZZ, 3, 3, algorithm='echelonizable', rank=3, upper_bound=10); B 
[ 1 -2  4]
[ 0  1 -5]
[ 0  1 -4]
sage: sage_input(B)
matrix(ZZ, [[1, -2, 4], [0, 1, -5], [0, 1, -4]])
edit flag offensive delete link more

Comments

The other answer helps me better, but I'm glad to know now how I might use the random seeds to my advantage. Thank you.

mathochist gravatar imagemathochist ( 2014-11-28 20:12:13 +0100 )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: 2014-11-28 02:00:04 +0100

Seen: 965 times

Last updated: Jan 03 '17