Ticket #11657: trac_11657-zero-vector-speedup.patch

File trac_11657-zero-vector-speedup.patch, 3.7 KB (added by rbeezer, 11 years ago)
  • sage/modules/free_module_element.pyx

    # 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  
    261261        (0, 0, 0, 0)
    262262        sage: w.parent()
    263263        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
    264281
    265282    Here we illustrate the creation of sparse vectors by using a
    266283    dictionary. ::
     
    289306        Traceback (most recent call last):
    290307        ...
    291308        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       
    293318    And it is an error to specify an index in a dictionary
    294319    that is greater than or equal to a requested degree. ::
    295320
     
    387412            if not maxindex < arg1:
    388413                raise ValueError("dictionary of entries has a key (index) exceeding the requested degree")
    389414        # 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
    392417        degree = arg1
    393418        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()
    395423        else:
    396424            if not isinstance(arg2, dict) and len(arg2) != degree:
    397425                raise ValueError, "incompatible degrees in vector constructor"
     
    596624        sage: zero_vector(-3)
    597625        Traceback (most recent call last):
    598626        ...
    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
    600628
    601629    Garbage instead of a ring will be recognized as such. ::
    602630
     
    611639        arg0 = sage.rings.integer_ring.IntegerRing()
    612640    if not (sage.rings.integer.is_Integer(arg1) or isinstance(arg1,(int,long))):
    613641        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))
    614645    if not is_Ring(arg0):
    615646        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()
    617648
    618649def random_vector(ring, degree=None, *args, **kwds):
    619650    r"""