random_element conflicts

54 views
Skip to first unread message

Niles Johnson

unread,
Jul 11, 2010, 7:45:54 PM7/11/10
to sage-...@googlegroups.com
Hello all,

I noticed some varying definitions of random_element for ZZ, QQ, and
RR (and probably other rings). This causes misleading documentation
(whch doctests don't catch, because the output is random!) and, for
power series rings over RR, an error. My initial thought was to just
open a ticket for power series over RR and fix that bug, but once I
started looking at the different definitions of random_element, I
wasn't sure what would be best. Ideas? Details below.

best,
Niles


First, note the different arguments expected by random_element for ZZ,
QQ, and RR:

Definition: ZZ.random_element(self, x=None, y=None, distribution=None)
Source:
def random_element(self, x=None, y=None, distribution=None):
r"""
Return a random integer.

ZZ.random_element()
return an integer using the default
distribution described below
ZZ.random_element(n)
return an
integer uniformly distributed between 0 and n-1, inclusive.
ZZ.random_element(min, max)
return an integer uniformly
distributed between min and max-1, inclusive.


Definition: QQ.random_element(self, num_bound=None,
den_bound=None, distribution=None)
Source:
def random_element(self, num_bound=None, den_bound=None, distribution=None):

here:
numerator is between -num_bound and num_bound+1,
denominator is between 1 and den_bound+1
if num_bound is supplied, den_bound defaults to equal num_bound


Definition: RR.random_element(self, min=-1, max=1, distribution=None)
Source:
def random_element(self, min=-1, max=1, distribution=None):
"""
Returns a uniformly distributed random number between min and max
(default -1 to 1).

Now here is how random_element is implemented for polynomial rings.
The docstring makes no substantial promises, and hence no lies:

sage: PZ = PolynomialRing(ZZ,'v')
sage: PQ = PolynomialRing(QQ,'v')
Definition: PQ.random_element(self, degree=2, *args, **kwds)
Source:
def random_element(self, degree=2, *args, **kwds):
r"""
Return a random polynomial.

INPUT:

- ``degree`` - Integer with degree (default: 2)
or a tuple of integers with minimum and maximum degrees

- ``*args, **kwds`` - Passed on to the ``random_element`` method for
the base ring

sage: PQ.random_element(5)
-4*v^4 - 1/44*v^3 - 5/2*v^2 - 1/5*v + 1
sage: PZ.random_element(5,100) # coefficients all positive
6*v^5 + 57*v^4 + 79*v^3 + 95*v^2 + 69*v + 13
sage: PQ.random_element(5,100)
39/22*v^5 - 15/4*v^4 - 43/98*v^3 + 30/71*v^2 - 47/54*v - 37/83
sage: PR.random_element(5,100)
44.7199850245856*v^5 + 51.5361019677145*v^4 + 28.8884279668796*v^3 +
20.5640533623856*v^2 + 92.6606456093422*v + 27.7377902950395

And here's what we have for power series rings:

sage: SQ = PowerSeriesRing(QQ,'v')
sage: SR = PowerSeriesRing(RR,'v')
Definition: SQ.random_element(self, prec, bound=None)
Source:
def random_element(self, prec, bound=None):
r"""
Return a random power series.

INPUT:


- ``prec`` - an integer

- ``bound`` - an integer (default: None, which tries
to spread choice across ring, if implemented)


sage: SQ.random_element(5)
1/2*v + 1/2*v^2 - 1/6*v^3 + 2*v^4 + O(v^5)

sage: SQ.random_element(5,100) # docstring promises coefficients are
uniformly distributed between -100 and 100
-7/3 + 5/8*v + 37/60*v^2 + 33/8*v^3 + 77/89*v^4 + O(v^5)

sage: SR.random_element(5) # broken
Traceback (most recent call last):
...
TypeError: unsupported operand type(s) for -: 'int' and 'NoneType'

sage: SR.random_element(5,100) # works, but not as indicated by documentation
95.2417086255616 + 13.9139610613937*v + 45.3045671712598*v^2 +
94.5179368088424*v^3 + 80.5135466443438*v^4 + O(v^5)
sage: SR.random_element(5,100)
88.5832192873596 + 4.57768492583799*v + 28.1830520242686*v^2 +
53.1959782753104*v^3 + 17.2203119065628*v^4 + O(v^5)
sage: SR.random_element(5,100)
98.9397896920050 + 50.3734868594305*v + 3.45686853617011*v^2 +
15.0639985942490*v^3 + 9.04148241799453*v^4 + O(v^5)
sage: SR.random_element(5,100)
20.7895461898698 + 79.4963437949055*v + 72.6510673159177*v^2 +
52.7149245489294*v^3 + 79.5260956048129*v^4 + O(v^5)
sage: SR.random_element(5,-100)
-63.5408783894400 - 49.8386015442635*v - 78.7058878043647*v^2 -
46.1262189039151*v^3 - 53.2891372935381*v^4 + O(v^5)

William Stein

unread,
Jul 12, 2010, 4:12:36 AM7/12/10
to sage-devel

You should fix that, just as you planned to do. I think it's the way
it is for historical reasons, since there was a time when the rings
only had one implementation of random_element. Change things to be
like with polynomial rings.

-- William

> --
> To post to this group, send an email to sage-...@googlegroups.com
> To unsubscribe from this group, send an email to sage-devel+...@googlegroups.com
> For more options, visit this group at http://groups.google.com/group/sage-devel
> URL: http://www.sagemath.org
>

--
William Stein
Professor of Mathematics
University of Washington
http://wstein.org

Reply all
Reply to author
Forward
0 new messages