Ticket #10364: trac_10364-zero-vector-constructor.patch
File trac_10364-zero-vector-constructor.patch, 5.1 KB (added by , 12 years ago) |
---|
-
sage/modules/all.py
# HG changeset patch # User Rob Beezer <beezer@ups.edu> # Date 1291263137 28800 # Node ID 19dd48186f9c010a1d9f251231ce450ccb24c0fd # Parent b152ad32dee500bac61c4fdb8991946f9fa41c58 10364: zero vector constructor, error checks for vector constructor diff -r b152ad32dee5 -r 19dd48186f9c sage/modules/all.py
a b 17 17 18 18 from free_quadratic_module import FreeQuadraticModule, QuadraticSpace, InnerProductSpace, is_FreeQuadraticModule 19 19 20 from free_module_element import is_FreeModuleElement, vector, free_module_element 20 from free_module_element import is_FreeModuleElement, vector, free_module_element, zero_vector 21 21 22 22 from free_module_homspace import is_FreeModuleHomspace 23 23 -
sage/modules/free_module_element.pyx
diff -r b152ad32dee5 -r 19dd48186f9c sage/modules/free_module_element.pyx
a b 272 272 sage: v = vector(QQ, 8, {0:1/2, 4:-6}); v 273 273 Traceback (most recent call last): 274 274 ... 275 ValueError: incompatible degrees in vector constructor275 TypeError: cannot specify the degree of a vector while entries are given by a dictionary 276 276 277 277 Instead, provide a "terminal" element (likely a zero) to fill out 278 278 the vector to the desired number of entries. :: … … 284 284 sage: v.is_sparse() 285 285 True 286 286 287 It is an error to specify a negative degree. :: 288 289 sage: vector(RR, -4, [1.0, 2.0, 3.0, 4.0]) 290 Traceback (most recent call last): 291 ... 292 ValueError: cannot specify the degree of a vector as a negative integer (-4) 293 287 294 Any 1 dimensional numpy array of type float or complex may be 288 295 passed to vector. The result will be a vector in the appropriate 289 296 dimensional vector space over the real double field or the complex … … 340 347 return arg1._vector_(arg0) 341 348 342 349 if sage.rings.integer.is_Integer(arg1) or isinstance(arg1,(int,long)): 350 if arg1 < 0: 351 raise ValueError("cannot specify the degree of a vector as a negative integer (%s)" % arg1) 352 if isinstance(arg2, dict): 353 raise TypeError("cannot specify the degree of a vector while entries are given by a dictionary") 343 354 if arg2 is None: 344 355 arg1 = [0]*arg1 345 356 else: … … 438 449 X[key] = value 439 450 return prepare(X, R) 440 451 452 def zero_vector(arg0, arg1=None): 453 r""" 454 Returns a vector or free module element with a specified number of zeros. 455 456 CALL FORMATS: 457 458 1. zero_vector(degree) 459 460 2. zero_vector(ring, degree) 461 462 INPUT: 463 464 - ``degree`` - the number of zero entries in the vector or 465 free module element 466 467 - ``ring`` - default ``ZZ`` - the base ring of the vector 468 space or module containing the constructed zero vector 469 470 OUTPUT: 471 A vector or free module element with ``degree`` entries, 472 all equal to zero and belonging to the ring if specified. 473 If no ring is given, a free module element over ``ZZ`` 474 is returned. 475 476 EXAMPLES: 477 478 A zero vector over the field of rationals. :: 479 480 sage: v = zero_vector(QQ, 5); v 481 (0, 0, 0, 0, 0) 482 sage: v.parent() 483 Vector space of dimension 5 over Rational Field 484 485 A free module zero element. :: 486 487 sage: w = zero_vector(Integers(6), 3); w 488 (0, 0, 0) 489 sage: w.parent() 490 Ambient free module of rank 3 over Ring of integers modulo 6 491 492 If no ring is given, the integers are used. :: 493 494 sage: u = zero_vector(9); u 495 (0, 0, 0, 0, 0, 0, 0, 0, 0) 496 sage: u.parent() 497 Ambient free module of rank 9 over the principal ideal domain Integer Ring 498 499 Non-integer degrees produce an error. :: 500 501 sage: zero_vector(5.6) 502 Traceback (most recent call last): 503 ... 504 ValueError: constructing a zero vector requires the degree as an integer, not 5.60000000000000 505 506 Negative degrees also give an error. :: 507 508 sage: zero_vector(-3) 509 Traceback (most recent call last): 510 ... 511 ValueError: cannot specify the degree of a vector as a negative integer (-3) 512 513 Garbage instead of a ring will be recognized as such. :: 514 515 sage: zero_vector(x^2, 5) 516 Traceback (most recent call last): 517 ... 518 ValueError: elements of a zero vector belong in a ring, not x^2 519 """ 520 # default to a zero vector over the integers (ZZ) if no ring given 521 if arg1 is None: 522 arg1 = arg0 523 arg0 = sage.rings.integer_ring.IntegerRing() 524 if not (sage.rings.integer.is_Integer(arg1) or isinstance(arg1,(int,long))): 525 raise ValueError("constructing a zero vector requires the degree as an integer, not %s" % arg1) 526 if not is_Ring(arg0): 527 raise ValueError("elements of a zero vector belong in a ring, not %s" % arg0) 528 return vector(arg0, arg1) 529 441 530 cdef class FreeModuleElement(element_Vector): # abstract base class 442 531 """ 443 532 An element of a generic free module.