08.02.2015 Views

Sage Reference Manual: Matrices and Spaces of Matrices - Mirrors

Sage Reference Manual: Matrices and Spaces of Matrices - Mirrors

Sage Reference Manual: Matrices and Spaces of Matrices - Mirrors

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Sage</strong> <strong>Reference</strong> <strong>Manual</strong>: <strong>Matrices</strong> <strong>and</strong> <strong>Spaces</strong> <strong>of</strong> <strong>Matrices</strong>, Release 6.1.1<br />

sage: r<strong>and</strong>om_matrix(ZZ, 5, x=-10, y=10, density=0.75)<br />

[-6 1 0 0 0]<br />

[ 9 0 0 4 1]<br />

[-6 0 0 -8 0]<br />

[ 0 4 0 6 0]<br />

[ 1 -9 0 0 -8]<br />

sage: r<strong>and</strong>om_matrix(ZZ, 5, x=20, y=30, density=0.75)<br />

[ 0 28 0 27 0]<br />

[25 28 20 0 0]<br />

[ 0 21 0 21 0]<br />

[ 0 28 22 0 0]<br />

[ 0 0 0 26 24]<br />

It is possible to construct sparse matrices, where it may now be advisable (but not required) to control the density<br />

<strong>of</strong> nonzero entries.<br />

sage: A=r<strong>and</strong>om_matrix(ZZ, 5, 5)<br />

sage: A.is_sparse()<br />

False<br />

sage: A=r<strong>and</strong>om_matrix(ZZ, 5, 5, sparse=True)<br />

sage: A.is_sparse()<br />

True<br />

sage: r<strong>and</strong>om_matrix(ZZ, 5, 5, density=0.3, sparse=True)<br />

[ 4 0 0 0 -1]<br />

[ 0 0 0 0 -7]<br />

[ 0 0 2 0 0]<br />

[ 0 0 1 0 -4]<br />

[ 0 0 0 0 0]<br />

For algorithm testing you might want to control the number <strong>of</strong> bits, say 10,000 entries, each limited to 16 bits.<br />

sage: A = r<strong>and</strong>om_matrix(ZZ, 100, 100, x=2^16); A<br />

100 x 100 dense matrix over Integer Ring (type ’print A.str()’ to see all <strong>of</strong> the entries)<br />

R<strong>and</strong>om rational matrices. Now num_bound <strong>and</strong> den_bound control the generation <strong>of</strong> r<strong>and</strong>om elements, by<br />

specifying limits on the absolute value <strong>of</strong> numerators <strong>and</strong> denominators (respectively). Entries will be positive<br />

<strong>and</strong> negative (map the absolute value function through the entries to get all positive values), <strong>and</strong> zeros are<br />

avoided unless the density is set. If either the numerator or denominator bound (or both) is not used, then the<br />

values default to the distribution for ZZ described above that is most frequently positive or negative one.<br />

sage: r<strong>and</strong>om_matrix(QQ, 2, 8, num_bound=20, den_bound=4)<br />

[ -1/2 6 13 -12 -2/3 -1/4 5 5]<br />

[ -9/2 5/3 19 15/2 19/2 20/3 -13/4 0]<br />

sage: r<strong>and</strong>om_matrix(QQ, 4, density = 0.5, sparse=True)<br />

[ 0 71 0 -1/2]<br />

[ 0 0 0 0]<br />

[31/85 0 -31/2 0]<br />

[ 1 -1/4 0 0]<br />

sage: A = r<strong>and</strong>om_matrix(QQ, 3, 10, num_bound = 99, den_bound = 99)<br />

sage: positives = map(abs, A.list())<br />

sage: matrix(QQ, 3, 10, positives)<br />

[61/18 47/41 1/22 1/2 75/68 6/7 1 1/2 72/41 7/3]<br />

[33/13 9/2 40/21 45/46 17/22 1 70/79 97/71 7/24 12/5]<br />

[ 13/8 8/25 1/3 61/14 92/45 4/85 3/38 95/16 82/71 1/5]<br />

48 Chapter 2. Matrix Constructor

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!