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

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 close merge delete

Sort by » oldest newest most voted

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)

more

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!

( 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

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

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

( 2017-01-03 22:03:13 +0100 )edit

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]])

more

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.

( 2014-11-28 20:12:13 +0100 )edit