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