Ticket #13187: trac_13187.patch

File trac_13187.patch, 10.2 KB (added by aapitzsch, 7 years ago)
  • sage/rings/polynomial/polynomial_ring_constructor.py

    # HG changeset patch
    # User André Apitzsch <andre.apitzsch@st.ovgu.de>
    # Date 1341077870 -7200
    # Node ID 0127a49fb99bcbce38bb22938c6ec712f6740282
    # Parent  ef777986e8a5aa5ff04d778cf96746fbe8989aae
    Trac 13187: accept a symbolic variable as name value in PolynomialRing
    
    diff --git a/sage/rings/polynomial/polynomial_ring_constructor.py b/sage/rings/polynomial/polynomial_ring_constructor.py
    a b  
    1616
    1717#################################################################
    1818#
    19 #   Sage: System for Algebra and Geometry Experimentation   
     19#   Sage: System for Algebra and Geometry Experimentation
    2020#
    2121#       Copyright (C) 2006 William Stein <wstein@gmail.com>
    2222#
     
    9595
    9696    NOTE:
    9797
    98     The following rules were introduced in trac ticket #9944, in order
     98    The following rules were introduced in :trac:`9944`, in order
    9999    to preserve the "unique parent assumption" in Sage (i.e., if two
    100100    parents evaluate equal then they should actually be identical).
    101101
     
    103103      the argument ``sparse=False`` is silently ignored in that case.
    104104    - If the given implementation does not exist for rings with the given number
    105105      of generators and the given sparsity, then an error results.
    106                  
     106
    107107    OUTPUT:
    108108
    109109    ``PolynomialRing(base_ring, name, sparse=False)`` returns a univariate
    110110    polynomial ring; also, PolynomialRing(base_ring, names, sparse=False)
    111     yields a univariate polynomial ring, if names is a list or tuple 
    112     providing exactly one name. All other input formats return a 
    113     multivariate polynomial ring. 
     111    yields a univariate polynomial ring, if names is a list or tuple
     112    providing exactly one name. All other input formats return a
     113    multivariate polynomial ring.
    114114
    115115    UNIQUENESS and IMMUTABILITY: In Sage there is exactly one
    116116    single-variate polynomial ring over each base ring in each choice
     
    148148
    149149            sage: print f
    150150            x^2 - 2*y^2
    151            
    152151
    153152    SQUARE BRACKETS NOTATION: You can alternatively create a single or
    154153    multivariate polynomial ring over a ring `R` by writing ``R['varname']`` or
    155154    ``R['var1,var2,var3,...']``.  This square brackets notation doesn't allow
    156     for setting any of the optional arguments.   
     155    for setting any of the optional arguments.
    157156
    158157    EXAMPLES:
    159158
    160     1. ``PolynomialRing(base_ring, name,    sparse=False)``
    161    
     159    1. ``PolynomialRing(base_ring, name, sparse=False)``
     160
    162161       ::
    163162
    164163        sage: PolynomialRing(QQ, 'w')
     
    170169        sage: R.<w> = PolynomialRing(QQ)
    171170        sage: (1 + w)^3
    172171        w^3 + 3*w^2 + 3*w + 1
    173        
     172
    174173       You must specify a name::
    175174
    176175        sage: PolynomialRing(QQ)
     
    208207
    209208        sage: QQ['x'] == QQ['y']
    210209        False
    211        
     210
    212211       Sage has two implementations of univariate polynomials over the
    213212       integers, one based on NTL and one based on FLINT.  The default
    214213       is FLINT. Note that FLINT uses a "more dense" representation for
     
    273272        sage: R == S
    274273        False
    275274
    276        Note that a univariate polynomial ring is returned, if the list 
    277        of names is of length one. If it is of length zero, a multivariate 
     275       Note that a univariate polynomial ring is returned, if the list
     276       of names is of length one. If it is of length zero, a multivariate
    278277       polynomial ring with no variables is returned.
    279278
    280279       ::
    281        
     280
    282281        sage: PolynomialRing(QQ,["x"])
    283282        Univariate Polynomial Ring in x over Rational Field
    284283        sage: PolynomialRing(QQ,[])
     
    293292
    294293        sage: PolynomialRing(QQ, 'x', 10)
    295294        Multivariate Polynomial Ring in x0, x1, x2, x3, x4, x5, x6, x7, x8, x9 over Rational Field
    296        
     295
    297296        sage: PolynomialRing(GF(7), 'y', 5)
    298297        Multivariate Polynomial Ring in y0, y1, y2, y3, y4 over Finite Field of size 7
    299298
     
    304303       explicit number is given.
    305304
    306305       ::
    307    
     306
    308307        sage: PolynomialRing(QQ,"x",1)
    309308        Multivariate Polynomial Ring in x over Rational Field
    310309        sage: PolynomialRing(QQ,"x",0)
     
    322321       method, all those variable names are available for interactive use::
    323322
    324323        sage: R.inject_variables()
    325         Defining x2, x3, x5, x7, x11, x13, x17, x19, x23, x29, x31, x37, x41, x43, x47, x53, x59, x61, x67, x71, x73, x79, x83, x89, x97       
     324        Defining x2, x3, x5, x7, x11, x13, x17, x19, x23, x29, x31, x37, x41, x43, x47, x53, x59, x61, x67, x71, x73, x79, x83, x89, x97
    326325        sage: (x2 + x41 + x71)^2
    327326        x2^2 + 2*x2*x41 + x41^2 + 2*x2*x71 + 2*x41*x71 + x71^2
    328327
    329328    TESTS:
    330329
    331     We test here some changes introduced in #9944.
     330    We test here some changes introduced in :trac:`9944`.
    332331
    333332    If there is no dense implementation for the given number of
    334333    variables, then requesting a dense ring results yields the
     
    360359        sage: R.0 == 0
    361360        True
    362361
     362    We verify that :trac:`13187` is fixed::
     363
     364        sage: var('t')
     365        t
     366        sage: PolynomialRing(ZZ, name=t) == PolynomialRing(ZZ, name='t')
     367        True
     368
    363369    """
    364370    import sage.rings.polynomial.polynomial_ring as m
    365371
    366     if is_Element(arg1) and not isinstance(arg1, (int, long, Integer)):
    367         arg1 = repr(arg1)
    368     if is_Element(arg2) and not isinstance(arg2, (int, long, Integer)):
    369         arg2 = repr(arg2)
    370 
    371372    if isinstance(arg1, (int, long, Integer)):
    372373        arg1, arg2 = arg2, arg1
    373374
     
    376377    elif not name is None:
    377378        arg1 = name
    378379
     380    if is_Element(arg1) and not isinstance(arg1, (int, long, Integer)):
     381        arg1 = repr(arg1)
     382    if is_Element(arg2) and not isinstance(arg2, (int, long, Integer)):
     383        arg2 = repr(arg2)
     384
    379385    if not m.ring.is_Ring(base_ring):
    380         raise TypeError, 'base_ring must be a ring'
     386        raise TypeError('base_ring must be a ring')
    381387
    382388    if arg1 is None:
    383         raise TypeError, "You must specify the names of the variables."
     389        raise TypeError("You must specify the names of the variables.")
    384390
    385391    R = None
    386392    if isinstance(arg1, (list, tuple)):
     
    390396    if isinstance(arg2, (int, long, Integer)):
    391397        # 3. PolynomialRing(base_ring, names, n, order='degrevlex'):
    392398        if not isinstance(arg1, (list, tuple, str)):
    393             raise TypeError, "You *must* specify the names of the variables."
     399            raise TypeError("You *must* specify the names of the variables.")
    394400        n = int(arg2)
    395401        names = arg1
    396402        R = _multi_variate(base_ring, names, n, sparse, order, implementation)
     
    399405        if not ',' in arg1:
    400406            # 1. PolynomialRing(base_ring, name, sparse=False):
    401407            if not arg2 is None:
    402                 raise TypeError, "if second arguments is a string with no commas, then there must be no other non-optional arguments"
     408                raise TypeError("if second arguments is a string with no commas, then there must be no other non-optional arguments")
    403409            name = arg1
    404410            R = _single_variate(base_ring, name, sparse, implementation)
    405411        else:
    406412            # 2-4. PolynomialRing(base_ring, names, order='degrevlex'):
    407413            if not arg2 is None:
    408                 raise TypeError, "invalid input to PolynomialRing function; please see the docstring for that function"
     414                raise TypeError("invalid input to PolynomialRing function; please see the docstring for that function")
    409415            names = arg1.split(',')
    410416            n = len(names)
    411417            R = _multi_variate(base_ring, names, n, sparse, order, implementation)
    412418    elif isinstance(arg1, (list, tuple)):
    413             # PolynomialRing(base_ring, names (list or tuple), order='degrevlex'):       
     419            # PolynomialRing(base_ring, names (list or tuple), order='degrevlex'):
    414420            names = arg1
    415421            n = len(names)
    416             R = _multi_variate(base_ring, names, n, sparse, order, implementation)       
     422            R = _multi_variate(base_ring, names, n, sparse, order, implementation)
    417423
    418424    if arg1 is None and arg2 is None:
    419         raise TypeError, "you *must* specify the indeterminates (as not None)."
     425        raise TypeError("you *must* specify the indeterminates (as not None).")
    420426    if R is None:
    421         raise TypeError, "invalid input (%s, %s, %s) to PolynomialRing function; please see the docstring for that function"%(
    422             base_ring, arg1, arg2)
     427        raise TypeError("invalid input (%s, %s, %s) to PolynomialRing function; please see the docstring for that function"%(
     428            base_ring, arg1, arg2))
    423429
    424430    return R
    425431
     
    438444def _get_from_cache(key):
    439445    try:
    440446        return _cache[key] #()
    441     except TypeError, msg:
    442         raise TypeError, 'key = %s\n%s'%(key,msg)
     447    except TypeError as msg:
     448        raise TypeError('key = %s\n%s'%(key,msg))
    443449    except KeyError:
    444450        return None
    445451
     
    447453    try:
    448454        #_cache[key] = weakref.ref(R)
    449455         _cache[key] = R
    450     except TypeError, msg:
    451         raise TypeError, 'key = %s\n%s'%(key,msg)
     456    except TypeError as msg:
     457        raise TypeError('key = %s\n%s'%(key,msg))
    452458
    453459
    454460def _single_variate(base_ring, name, sparse, implementation):
     
    456462    name = normalize_names(1, name)
    457463    key = (base_ring, name, sparse, implementation if not sparse else None)
    458464    R = _get_from_cache(key)
    459     if not R is None: return R
     465    if not R is None:
     466        return R
    460467
    461468    if isinstance(base_ring, ring.CommutativeRing):
    462469        if is_IntegerModRing(base_ring) and not sparse:
     
    488495        else:
    489496            R = m.PolynomialRing_commutative(base_ring, name, sparse)
    490497    else:
    491         R = m.PolynomialRing_general(base_ring, name, sparse)       
     498        R = m.PolynomialRing_general(base_ring, name, sparse)
    492499
    493500    if hasattr(R, '_implementation_names'):
    494501        for name in R._implementation_names:
     
    507514    # even though it is meaningless.
    508515
    509516    if implementation is not None:
    510         raise ValueError, "The %s implementation is not known for multivariate polynomial rings"%implementation
     517        raise ValueError("The %s implementation is not known for multivariate polynomial rings"%implementation)
    511518
    512519    names = normalize_names(n, names)
    513520