# HG changeset patch
# User Rob Beezer <beezer@ups.edu>
# Date 1314215239 25200
# Node ID ede5bc78b3a4e63921aa70688db64d10eeaf6072
# Parent 2a2abbcad325ccca9399981ceddf5897eb467e64
11657: speed-up zero vector creation
diff --git a/sage/modules/free_module_element.pyx b/sage/modules/free_module_element.pyx
a
|
b
|
|
261 | 261 | (0, 0, 0, 0) |
262 | 262 | sage: w.parent() |
263 | 263 | Vector space of dimension 4 over Finite Field of size 7 |
| 264 | |
| 265 | If you want a zero vector the fastest method is to call the |
| 266 | :meth:`~sage.modules.free_module.FreeModule_generic.zero_vector` |
| 267 | method directly on a free module or vector space, as this |
| 268 | constructor must do some type-checking. Almost as fast as |
| 269 | the ``zero_vector()`` method is the :func:`~sage.modules.free_module_element.zero_vector` |
| 270 | constructor, which defaults to the integers. :: |
| 271 | |
| 272 | sage: (ZZ^5).zero_vector() |
| 273 | (0, 0, 0, 0, 0) |
| 274 | sage: zero_vector(ZZ, 5) |
| 275 | (0, 0, 0, 0, 0) |
| 276 | sage: z = zero_vector(5); z |
| 277 | (0, 0, 0, 0, 0) |
| 278 | sage: z.parent() |
| 279 | Ambient free module of rank 5 over |
| 280 | the principal ideal domain Integer Ring |
264 | 281 | |
265 | 282 | Here we illustrate the creation of sparse vectors by using a |
266 | 283 | dictionary. :: |
… |
… |
|
289 | 306 | Traceback (most recent call last): |
290 | 307 | ... |
291 | 308 | ValueError: cannot specify the degree of a vector as a negative integer (-4) |
292 | | |
| 309 | |
| 310 | It is an error to create a zero vector but not provide |
| 311 | a ring as the first argument. :: |
| 312 | |
| 313 | sage: vector('junk', 20) |
| 314 | Traceback (most recent call last): |
| 315 | ... |
| 316 | TypeError: first argument must be base ring of zero vector, not junk |
| 317 | |
293 | 318 | And it is an error to specify an index in a dictionary |
294 | 319 | that is greater than or equal to a requested degree. :: |
295 | 320 | |
… |
… |
|
387 | 412 | if not maxindex < arg1: |
388 | 413 | raise ValueError("dictionary of entries has a key (index) exceeding the requested degree") |
389 | 414 | # arg1 is now a legitimate degree |
390 | | # replace it with a zero list or a dictionary, or a size-checked iterable |
391 | | # so then down to just two arguments |
| 415 | # With no arg2, we can try to return a zero vector |
| 416 | # else we size-check arg2 and slide it into arg1 |
392 | 417 | degree = arg1 |
393 | 418 | if arg2 is None: |
394 | | arg1 = [0]*degree |
| 419 | if not is_Ring(arg0): |
| 420 | msg = "first argument must be base ring of zero vector, not {0}" |
| 421 | raise TypeError(msg.format(arg0)) |
| 422 | return (arg0**degree).zero_vector() |
395 | 423 | else: |
396 | 424 | if not isinstance(arg2, dict) and len(arg2) != degree: |
397 | 425 | raise ValueError, "incompatible degrees in vector constructor" |
… |
… |
|
596 | 624 | sage: zero_vector(-3) |
597 | 625 | Traceback (most recent call last): |
598 | 626 | ... |
599 | | ValueError: cannot specify the degree of a vector as a negative integer (-3) |
| 627 | ValueError: degree of zero vector must be non-negative, not -3 |
600 | 628 | |
601 | 629 | Garbage instead of a ring will be recognized as such. :: |
602 | 630 | |
… |
… |
|
611 | 639 | arg0 = sage.rings.integer_ring.IntegerRing() |
612 | 640 | if not (sage.rings.integer.is_Integer(arg1) or isinstance(arg1,(int,long))): |
613 | 641 | raise ValueError("constructing a zero vector requires the degree as an integer, not %s" % arg1) |
| 642 | if arg1 < 0: |
| 643 | msg = "degree of zero vector must be non-negative, not {0}" |
| 644 | raise ValueError(msg.format(arg1)) |
614 | 645 | if not is_Ring(arg0): |
615 | 646 | raise ValueError("elements of a zero vector belong in a ring, not %s" % arg0) |
616 | | return vector(arg0, arg1) |
| 647 | return (arg0**arg1).zero_vector() |
617 | 648 | |
618 | 649 | def random_vector(ring, degree=None, *args, **kwds): |
619 | 650 | r""" |