Ticket #13187: trac_13187.patch
File trac_13187.patch, 10.2 KB (added by , 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 16 16 17 17 ################################################################# 18 18 # 19 # Sage: System for Algebra and Geometry Experimentation 19 # Sage: System for Algebra and Geometry Experimentation 20 20 # 21 21 # Copyright (C) 2006 William Stein <wstein@gmail.com> 22 22 # … … 95 95 96 96 NOTE: 97 97 98 The following rules were introduced in trac ticket #9944, in order98 The following rules were introduced in :trac:`9944`, in order 99 99 to preserve the "unique parent assumption" in Sage (i.e., if two 100 100 parents evaluate equal then they should actually be identical). 101 101 … … 103 103 the argument ``sparse=False`` is silently ignored in that case. 104 104  If the given implementation does not exist for rings with the given number 105 105 of generators and the given sparsity, then an error results. 106 106 107 107 OUTPUT: 108 108 109 109 ``PolynomialRing(base_ring, name, sparse=False)`` returns a univariate 110 110 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. 114 114 115 115 UNIQUENESS and IMMUTABILITY: In Sage there is exactly one 116 116 singlevariate polynomial ring over each base ring in each choice … … 148 148 149 149 sage: print f 150 150 x^2  2*y^2 151 152 151 153 152 SQUARE BRACKETS NOTATION: You can alternatively create a single or 154 153 multivariate polynomial ring over a ring `R` by writing ``R['varname']`` or 155 154 ``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. 157 156 158 157 EXAMPLES: 159 158 160 1. ``PolynomialRing(base_ring, name, 161 159 1. ``PolynomialRing(base_ring, name, sparse=False)`` 160 162 161 :: 163 162 164 163 sage: PolynomialRing(QQ, 'w') … … 170 169 sage: R.<w> = PolynomialRing(QQ) 171 170 sage: (1 + w)^3 172 171 w^3 + 3*w^2 + 3*w + 1 173 172 174 173 You must specify a name:: 175 174 176 175 sage: PolynomialRing(QQ) … … 208 207 209 208 sage: QQ['x'] == QQ['y'] 210 209 False 211 210 212 211 Sage has two implementations of univariate polynomials over the 213 212 integers, one based on NTL and one based on FLINT. The default 214 213 is FLINT. Note that FLINT uses a "more dense" representation for … … 273 272 sage: R == S 274 273 False 275 274 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 278 277 polynomial ring with no variables is returned. 279 278 280 279 :: 281 280 282 281 sage: PolynomialRing(QQ,["x"]) 283 282 Univariate Polynomial Ring in x over Rational Field 284 283 sage: PolynomialRing(QQ,[]) … … 293 292 294 293 sage: PolynomialRing(QQ, 'x', 10) 295 294 Multivariate Polynomial Ring in x0, x1, x2, x3, x4, x5, x6, x7, x8, x9 over Rational Field 296 295 297 296 sage: PolynomialRing(GF(7), 'y', 5) 298 297 Multivariate Polynomial Ring in y0, y1, y2, y3, y4 over Finite Field of size 7 299 298 … … 304 303 explicit number is given. 305 304 306 305 :: 307 306 308 307 sage: PolynomialRing(QQ,"x",1) 309 308 Multivariate Polynomial Ring in x over Rational Field 310 309 sage: PolynomialRing(QQ,"x",0) … … 322 321 method, all those variable names are available for interactive use:: 323 322 324 323 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 326 325 sage: (x2 + x41 + x71)^2 327 326 x2^2 + 2*x2*x41 + x41^2 + 2*x2*x71 + 2*x41*x71 + x71^2 328 327 329 328 TESTS: 330 329 331 We test here some changes introduced in #9944.330 We test here some changes introduced in :trac:`9944`. 332 331 333 332 If there is no dense implementation for the given number of 334 333 variables, then requesting a dense ring results yields the … … 360 359 sage: R.0 == 0 361 360 True 362 361 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 363 369 """ 364 370 import sage.rings.polynomial.polynomial_ring as m 365 371 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 371 372 if isinstance(arg1, (int, long, Integer)): 372 373 arg1, arg2 = arg2, arg1 373 374 … … 376 377 elif not name is None: 377 378 arg1 = name 378 379 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 379 385 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') 381 387 382 388 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.") 384 390 385 391 R = None 386 392 if isinstance(arg1, (list, tuple)): … … 390 396 if isinstance(arg2, (int, long, Integer)): 391 397 # 3. PolynomialRing(base_ring, names, n, order='degrevlex'): 392 398 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.") 394 400 n = int(arg2) 395 401 names = arg1 396 402 R = _multi_variate(base_ring, names, n, sparse, order, implementation) … … 399 405 if not ',' in arg1: 400 406 # 1. PolynomialRing(base_ring, name, sparse=False): 401 407 if not arg2 is None: 402 raise TypeError , "if second arguments is a string with no commas, then there must be no other nonoptional arguments"408 raise TypeError("if second arguments is a string with no commas, then there must be no other nonoptional arguments") 403 409 name = arg1 404 410 R = _single_variate(base_ring, name, sparse, implementation) 405 411 else: 406 412 # 24. PolynomialRing(base_ring, names, order='degrevlex'): 407 413 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") 409 415 names = arg1.split(',') 410 416 n = len(names) 411 417 R = _multi_variate(base_ring, names, n, sparse, order, implementation) 412 418 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'): 414 420 names = arg1 415 421 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) 417 423 418 424 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).") 420 426 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)) 423 429 424 430 return R 425 431 … … 438 444 def _get_from_cache(key): 439 445 try: 440 446 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)) 443 449 except KeyError: 444 450 return None 445 451 … … 447 453 try: 448 454 #_cache[key] = weakref.ref(R) 449 455 _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)) 452 458 453 459 454 460 def _single_variate(base_ring, name, sparse, implementation): … … 456 462 name = normalize_names(1, name) 457 463 key = (base_ring, name, sparse, implementation if not sparse else None) 458 464 R = _get_from_cache(key) 459 if not R is None: return R 465 if not R is None: 466 return R 460 467 461 468 if isinstance(base_ring, ring.CommutativeRing): 462 469 if is_IntegerModRing(base_ring) and not sparse: … … 488 495 else: 489 496 R = m.PolynomialRing_commutative(base_ring, name, sparse) 490 497 else: 491 R = m.PolynomialRing_general(base_ring, name, sparse) 498 R = m.PolynomialRing_general(base_ring, name, sparse) 492 499 493 500 if hasattr(R, '_implementation_names'): 494 501 for name in R._implementation_names: … … 507 514 # even though it is meaningless. 508 515 509 516 if implementation is not None: 510 raise ValueError , "The %s implementation is not known for multivariate polynomial rings"%implementation517 raise ValueError("The %s implementation is not known for multivariate polynomial rings"%implementation) 511 518 512 519 names = normalize_names(n, names) 513 520