I've attached a patch that takes care of 1) only and updated http://sagetrac.org/sage_trac/ticket/980 . The individual degree distribution is a little better: {{{ sage: GF(10007)['x,y,q'].random_element(6,10) -2005*x^6 + 2400*x^4*y^2 - 3609*x^3*y^3 + 488*x*y^5 - 3093*x^4*y*q + 3482*x*y*q^3 - 989*x^2*y*q - 3529*x*q^3 - 3957*x }}}
But note that most of the time for the example at the beginning of the thread, we still get 6,7 out of 9 terms most of the time. I still don't think this is a bug: we are not creating a polynomial with 9 terms in it, but picking one with at most 9 terms. {{{ sage: GF(10007)['x,y'].random_element(4,9) 797*x^4 - 439*x^2*y^2 - 1457*x*y^3 - 2348*y^4 - 1721*x^3 - 1885*x^2*y - 1760*x*y^2 + 310*y }}} didier 2007/10/24, Steffen <[EMAIL PROTECTED]>: > > > > On Oct 24, 5:45 am, "didier deshommes" <[EMAIL PROTECTED]> wrote: > > 2007/10/23, Steffen <[EMAIL PROTECTED]>: > > > > > Exactly, thats one of two points. The maximum degree in every variable > > > is (maximum total degree of resulting polynomial) / (number of > > > varialbes of the polynomial). Thus for example GF(10007) > > > ['x,y,z'].random_element(5,9) will be limited in every variable to > > > degree 5/3 = 1 !!!. This is not what the upper definition says. > > > The second point is about the number of coefficients that are set to > > > 0. This might a point to argue about, but if I create a random > > > polynomial with a (maximum number of terms to generate) then I expect > > > that the 0 occurs with the same probability and thus as often as every > > > other element. Thats why I am not happy if 20% or more of the > > > parameters are 0. > > > > I filed out a bug report about those 2 issues: > > 1) Degrees can severely restricted. > > 2) The polynomials returned can be too sparse > > > > I plan to post a patch addressing your concerns (I can especially see > > how 1) can be annoying). Random poly generation will most likely be > > slower, so for now I'm planning to keep the current behavior around, > > even if it is not the default. > > > > didier > > Thanks, I will wait for didiers patch. If I then have any additional > requirements I will implement them via an optional flag or smth > similar. > > Steffen > > > > > --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to sage-devel@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sage-devel URLs: http://sage.scipy.org/sage/ and http://modular.math.washington.edu/sage/ -~----------~----~----~----~------~----~------~--~---
diff -r 1a19787af98d sage/rings/polynomial/multi_polynomial_ring_generic.pyx --- a/sage/rings/polynomial/multi_polynomial_ring_generic.pyx Sun Oct 21 10:13:32 2007 -0700 +++ b/sage/rings/polynomial/multi_polynomial_ring_generic.pyx Wed Oct 24 18:23:14 2007 -0400 @@ -453,23 +453,31 @@ cdef class MPolynomialRing_generic(sage. sage: R.random_element(41) # random -4*x^6*y^4*z^4*a^6*b^3*c^6*d^5 + 1/2*x^4*y^3*z^5*a^4*c^5*d^6 - 5*x^3*z^3*a^6*b^4*c*d^5 + 10*x^2*y*z^5*a^4*b^2*c^3*d^4 - 5*x^3*y^5*z*b^2*c^5*d - + sage: f=ZZ['q,w,e,r,t,y'].random_element(degree=200,terms=19) + sage: f.degree() <= 200 + True + AUTHOR: -- didier deshommes """ - # General strategy: - # generate n-tuples of numbers with each element in the tuple - # not greater than (degree/n) so that the degree - # (ie, the sum of the elements in the tuple) does not exceed - # their total degree - + n = self.__ngens # length of the n-tuple - max_deg = int(degree/n) # max degree for each term R = self.base_ring() - # restrict exponents to positive integers only - exponents = [ tuple([ZZ.random_element(0,max_deg+1) for _ in range(n)]) - for _ in range(terms) ] + exponents = [] + for i in range(terms): + exps = [] + deg = degree + for i in range(n): + exp = ZZ.random_element(0,deg+1) + if exp <= deg: + exps.append(exp) + deg -= exp + else: + exps.append(0) + + exponents.append(tuple(exps)) + coeffs = [] for _ in range(terms): c = R.random_element(*args,**kwds)