Ticket #9020: trac_9020-random_polynomial_degree.patch

File trac_9020-random_polynomial_degree.patch, 2.6 KB (added by burcin, 3 years ago)
  • sage/rings/polynomial/polynomial_ring.py

    # HG changeset patch
    # User Burcin Erocal <burcin@erocal.org>
    # Date 1274565527 -7200
    # Node ID 12969bf93758acd4b3b4281393f136691476e891
    # Parent  57ce3e6b24e6b3a54bed2a1c78b315ff31639a8a
    trac 9020: Let random_polynomial() method of univariate polynomial rings take a tuple as the degree argument specifying minimum and maximum degrees.
    
    diff --git a/sage/rings/polynomial/polynomial_ring.py b/sage/rings/polynomial/polynomial_ring.py
    a b  
    109109from sage.libs.pari.all import pari_gen 
    110110import sage.misc.defaults 
    111111import sage.misc.latex as latex 
     112from sage.misc.prandom import randint 
    112113from sage.rings.real_mpfr import is_RealField 
    113114from polynomial_real_mpfr_dense import PolynomialRealDense 
    114115from sage.rings.polynomial.polynomial_singular_interface import PolynomialRing_singular_repr 
     
    885886         
    886887        INPUT: 
    887888         
    888         -  ``degree`` - Integer (default: 2) 
     889        -  ``degree`` - Integer with degree (default: 2) 
     890           or a tuple of integers with minimum and maximum degrees 
    889891         
    890892        -  ``*args, **kwds`` - Passed on to the ``random_element`` method for  
    891893           the base ring 
     
    907909            -2*x^5 + 2*x^4 - 3*x^3 + 1 
    908910            sage: R.random_element(6) 
    909911            x^4 - x^3 + x - 2 
     912 
     913        If a tuple of two integers is given for the degree argument, a random 
     914        integer will be chosen between the first and second element of the 
     915        tuple as the degree:: 
     916 
     917            sage: R.random_element(degree=(0,8)) 
     918            2*x^7 - x^5 + 4*x^4 - 5*x^3 + x^2 + 14*x - 1 
     919            sage: R.random_element(degree=(0,8)) 
     920            -2*x^3 + x^2 + x + 4 
     921 
     922        TESTS:: 
     923 
     924            sage: R.random_element(degree=[5]) 
     925            Traceback (most recent call last): 
     926            ... 
     927            ValueError: degree argument must be an integer or a tuple of 2 integers (min_degree, max_degree) 
     928 
     929            sage: R.random_element(degree=(5,4)) 
     930            Traceback (most recent call last): 
     931            ... 
     932            ValueError: minimum degree must be less or equal than maximum degree 
    910933        """ 
     934        if isinstance(degree, (list, tuple)): 
     935            if len(degree) != 2: 
     936                raise ValueError, "degree argument must be an integer or a tuple of 2 integers (min_degree, max_degree)" 
     937            if degree[0] > degree[1]: 
     938                raise ValueError, "minimum degree must be less or equal than maximum degree" 
     939            degree = randint(*degree) 
    911940        R = self.base_ring() 
    912941        return self([R.random_element(*args, **kwds) for _ in xrange(degree+1)]) 
    913942