Sage: Ticket #10477: Add a random vector constructor
https://trac.sagemath.org/ticket/10477
<p>
<code>random_matrix()</code> will build a variety of random matrices, employing the <code>random_element()</code> method for the base ring. This patch provides the basic functionality for creating random vectors, or random free module elements, mimicking the constructor for matrices.
</p>
<p>
This depends trivially on <a class="closed ticket" href="https://trac.sagemath.org/ticket/10364" title="enhancement: Vector constructor improvements (closed: fixed)">#10364</a> since both write into the same line of <code>all.py</code>.
</p>
en-usSagehttps://trac.sagemath.org/chrome/site/logo_sagemath_trac.png
https://trac.sagemath.org/ticket/10477
Trac 1.1.6rbeezerWed, 15 Dec 2010 02:33:39 GMTattachment set
https://trac.sagemath.org/ticket/10477
https://trac.sagemath.org/ticket/10477
<ul>
<li><strong>attachment</strong>
set to <em>trac_10477-random-vector-constructor.patch</em>
</li>
</ul>
TicketrbeezerWed, 15 Dec 2010 02:35:41 GMTstatus, description changed
https://trac.sagemath.org/ticket/10477#comment:1
https://trac.sagemath.org/ticket/10477#comment:1
<ul>
<li><strong>status</strong>
changed from <em>new</em> to <em>needs_review</em>
</li>
<li><strong>description</strong>
modified (<a href="/ticket/10477?action=diff&version=1">diff</a>)
</li>
</ul>
TicketrbeezerMon, 20 Dec 2010 02:07:44 GMTstatus changed
https://trac.sagemath.org/ticket/10477#comment:2
https://trac.sagemath.org/ticket/10477#comment:2
<ul>
<li><strong>status</strong>
changed from <em>needs_review</em> to <em>needs_work</em>
</li>
</ul>
<p>
Replying to <a class="ticket" href="https://trac.sagemath.org/ticket/10477#comment:1" title="Comment 1">rbeezer</a>:
</p>
<p>
This might work better if it just called the <code>random_element()</code> method for vector spaces. I'm going to move this to "needs work" until I get a chance to investigate.
</p>
TicketrbeezerMon, 20 Dec 2010 04:39:21 GMTstatus changed
https://trac.sagemath.org/ticket/10477#comment:3
https://trac.sagemath.org/ticket/10477#comment:3
<ul>
<li><strong>status</strong>
changed from <em>needs_work</em> to <em>needs_review</em>
</li>
</ul>
<p>
Replying to <a class="ticket" href="https://trac.sagemath.org/ticket/10477#comment:2" title="Comment 2">rbeezer</a>:
</p>
<blockquote class="citation">
<p>
This might work better if it just called the <code>random_element()</code> method for vector spaces. I'm going to move this to "needs work" until I get a chance to investigate.
</p>
</blockquote>
<p>
<code>.random_element()</code> allows setting the density of the result (fraction of zeros), while this routine allows for sparse representations. The present routine could be extended to include a probability argument to allow for controlling the density. Run times look similar.
</p>
<pre class="wiki">sage: time for i in range(5000): random_vector(QQ, 100)
CPU times: user 4.22 s, sys: 0.05 s, total: 4.27 s
Wall time: 4.28 s
sage: V=QQ^100
sage: time for i in range(5000): V.random_element()
CPU times: user 3.95 s, sys: 0.00 s, total: 3.95 s
Wall time: 3.96 s
</pre><p>
So I think this is ready (again) for review.
</p>
TicketkcrismanTue, 11 Jan 2011 16:37:27 GMTcc set
https://trac.sagemath.org/ticket/10477#comment:4
https://trac.sagemath.org/ticket/10477#comment:4
<ul>
<li><strong>cc</strong>
<em>kcrisman</em> added
</li>
</ul>
TicketflawrenceThu, 10 Mar 2011 06:48:34 GMT
https://trac.sagemath.org/ticket/10477#comment:5
https://trac.sagemath.org/ticket/10477#comment:5
<p>
Hi Rob,
</p>
<p>
This looks good! Two comments, the second of which is petty:
</p>
<ul><li>I think the default ring should be <code>RR</code> rather than <code>ZZ</code>. I can think of lots of uses for a vector of random reals, but not as many for a vector of integers. I'm open to persuasion though!
</li></ul><ul><li>Before doing <code>degree = ring</code> it would be good to check that <code>degree==None</code>. Otherwise the following slips through without raising an error:
<pre class="wiki">sage: random_vector(11,2)
(0, 1, -3, 2, -9, -1, -1, 0, 1, 1, 1)
</pre></li></ul>
TicketrbeezerThu, 10 Mar 2011 19:05:18 GMTattachment set
https://trac.sagemath.org/ticket/10477
https://trac.sagemath.org/ticket/10477
<ul>
<li><strong>attachment</strong>
set to <em>trac_10477-random-vector-constructor-v2.patch</em>
</li>
</ul>
TicketrbeezerThu, 10 Mar 2011 19:06:26 GMT
https://trac.sagemath.org/ticket/10477#comment:6
https://trac.sagemath.org/ticket/10477#comment:6
<p>
Hi Felix,
</p>
<p>
Thanks for the catch on the <code>degree</code> argument. If no ring is specified, and a value is in the second slot (so assigned to the degree keyword) I have shoved it into the front-end of the argument list. A new doctest shows this in action. (Inserting a new random doctest then meant changing <em>all</em> of the subsequent random output.)
</p>
<p>
So with this change in the new patch we have:
</p>
<p>
Before:
</p>
<pre class="wiki">sage: random_vector(10, 50)
(1, 2, 1, 1, 3, 1, -2, -1, -63, 1)
</pre><p>
After:
</p>
<pre class="wiki">sage: random_vector(10, 50)
(48, 24, 48, 33, 6, 9, 41, 14, 43, 44)
</pre><p>
I think every one of the matrix constructors (random or otherwise) defaults to the integers. I'd guess two reasons for this - Sage's roots in number theory, or matrices (vectors) over other rings will usually play nicely with integers (in other words, the integers will coerce smoothly into lots of rings).
</p>
<p>
In any event, I used the integers only for some measure of consistency with the matrix constructors.
</p>
<p>
Rob
</p>
TicketflawrenceSat, 12 Mar 2011 12:24:50 GMTstatus changed; reviewer set
https://trac.sagemath.org/ticket/10477#comment:7
https://trac.sagemath.org/ticket/10477#comment:7
<ul>
<li><strong>status</strong>
changed from <em>needs_review</em> to <em>positive_review</em>
</li>
<li><strong>reviewer</strong>
set to <em>Felix Lawrence</em>
</li>
</ul>
<p>
Fair enough, it's best to be consistent with the other constructors. With the new patch, the code works well and passed long doctests. Positive review.
</p>
TicketjdemeyerThu, 24 Mar 2011 20:39:12 GMTstatus changed; resolution, merged set
https://trac.sagemath.org/ticket/10477#comment:8
https://trac.sagemath.org/ticket/10477#comment:8
<ul>
<li><strong>status</strong>
changed from <em>positive_review</em> to <em>closed</em>
</li>
<li><strong>resolution</strong>
set to <em>fixed</em>
</li>
<li><strong>merged</strong>
set to <em>sage-4.7.alpha3</em>
</li>
</ul>
Ticket