ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 20 Nov 2014 02:02:50 +0100Generating random polynomials in R/Ihttps://ask.sagemath.org/question/24921/generating-random-polynomials-in-ri/I'm trying to generate random polynomials in ring R/I. Here's an example:
P.<x> = PolynomialRing(ZZ)
R = P.quotient(x**256 + 1)
I = R.ideal(g) # for some appropriate polynomial g
S = QuotientRing(R, I)
S.random_element()
Instead of this last command outputting a polynomial, it outputs a small integer. Looking through the sage code, the `QuotientRing_nc` class doesn't appear to be overloading `random_element`, so I guess the default `random_element` method from the `Ring` class is being called, which just outputs random integers.
Is there any way to generate random polynomials in `S`, or is that functionality not implemented currently?
Thanks.Wed, 19 Nov 2014 21:31:31 +0100https://ask.sagemath.org/question/24921/generating-random-polynomials-in-ri/Answer by tmonteil for <p>I'm trying to generate random polynomials in ring R/I. Here's an example:</p>
<pre><code>P.<x> = PolynomialRing(ZZ)
R = P.quotient(x**256 + 1)
I = R.ideal(g) # for some appropriate polynomial g
S = QuotientRing(R, I)
S.random_element()
</code></pre>
<p>Instead of this last command outputting a polynomial, it outputs a small integer. Looking through the sage code, the <code>QuotientRing_nc</code> class doesn't appear to be overloading <code>random_element</code>, so I guess the default <code>random_element</code> method from the <code>Ring</code> class is being called, which just outputs random integers.</p>
<p>Is there any way to generate random polynomials in <code>S</code>, or is that functionality not implemented currently?</p>
<p>Thanks.</p>
https://ask.sagemath.org/question/24921/generating-random-polynomials-in-ri/?answer=24922#post-id-24922 The problem with the ``.random_element()`` methods in Sage is that the
distribution is usually far from being uniform. In some cases, it is very hard
to sample according to a particular distribution (even theoretically).
In this case the distribution is indeed concentrated to the integers, as it is specified in the documentation, see:
sage: S.random_element?
Note however that the elements are not integers but belong to S:
sage: S.random_element().parent()
Quotient of Univariate Quotient Polynomial Ring in xbar over Integer Ring with modulus x^256 + 1 by the ideal (xbar^4 - 3)
sage: S.random_element().parent() == S
True
Other distributions are not implemented for such quotient rings, so there is room for interesting development, which is more than welcome !
In the meantime, you can still do something like:
sage: S(ZZ[x].random_element(degree=256))
Thu, 20 Nov 2014 02:02:50 +0100https://ask.sagemath.org/question/24921/generating-random-polynomials-in-ri/?answer=24922#post-id-24922