# Ticket #12923: trac_12923-free_mod_cmp_fix-fh.patch

File trac_12923-free_mod_cmp_fix-fh.patch, 7.7 KB (added by hivert, 10 years ago)
• ## sage/modules/free_module_element.pyx

```# HG changeset patch
# User Florent Hivert <Florent.Hivert@univ-rouen.fr>
# Date 1336493777 -7200
# Node ID ac1954fbee6158b2ab860197587d7315b474092f
# Parent  89016c4f363a2e1830954853be2fd48a16d8d1f2
Comparison of vectors is O(n) even in the simple cases

diff --git a/sage/modules/free_module_element.pyx b/sage/modules/free_module_element.pyx```
 a cdef class FreeModuleElement(element_Vec """ EXAMPLES:: sage: v = vector(QQ, [0,0,0,0]) sage: v = vector(SR, [0,0,0,0]) sage: v == 0 True sage: v == 1 False sage: v == v True sage: w = vector(QQ, [-1,0,0,0]) sage: w = vector(SR, [-1,x,pi,0]) sage: w < v True sage: w > v cdef class FreeModuleElement(element_Vec c = cmp(left[i], right[i]) if c: return c return 0 # see sage/structure/element.pyx def __richcmp__(left, right, int op): """ TESTS:: sage: F. = PolynomialRing(QQ, 'y') sage: type(vector(F, [0]*4, sparse=True)) sage: vector(F, [0,0,0,y]) == vector(F, [0,0,0,y]) True sage: vector(F, [0,0,0,0]) == vector(F, [0,2,0,y]) False """ return (left)._richcmp(right, op) def __getitem__(self, i): """ Return `i`-th entry or slice of self. cdef class FreeModuleElement_generic_den else: return self._entries cdef int _cmp_c_impl(left, Element right) except -2: """ Compare two free module elements with identical parents. Free module elements are compared in lexicographic order on the underlying list of coefficients. Two free module elements are equal if their coefficients are the same. (This is true even if one is sparse and one is dense.) """ return cmp(left._entries, (right)._entries) def __call__(self, *args, **kwargs): """ Calling a free module element returns the result of calling each cdef class FreeModuleElement_generic_spa cdef int _cmp_c_impl(left, Element right) except -2: """ Compare two sparse free module elements. Free module elements are compared in lexicographic order on the underlying list of coefficients. Two free module elements are equal if their coefficients are the same. (This is true even if one is sparse and one is dense.) TESTS:: sage: v = vector([1,2/3,pi], sparse=True) sage: w = vector([1,2/3,pi], sparse=True) sage: w == v True """ a = left._entries.items() a.sort() cdef class FreeModuleElement_generic_spa b.sort() return cmp(a,b) # see sage/structure/element.pyx def __richcmp__(left, right, int op): """ TESTS:: sage: v = vector([1,2/3,pi], sparse=True) sage: v == v True """ return (left)._richcmp(right, op) # __hash__ is not properly inherited if comparison is changed def __hash__(self): """ TESTS:: sage: v = vector([1,2/3,pi], sparse=True) sage: v.set_immutable() sage: isinstance(hash(v), int) True """ return FreeModuleElement.__hash__(self) def iteritems(self): """ Return iterator over the entries of self.
• ## sage/modules/vector_integer_dense.pyx

`diff --git a/sage/modules/vector_integer_dense.pyx b/sage/modules/vector_integer_dense.pyx`
 a cdef class Vector_integer_dense(free_mod return 1 return 0 # see sage/structure/element.pyx def __richcmp__(left, right, int op): """ TEST:: sage: w = vector(ZZ, [-1,0,0,0]) sage: w == w True """ return (left)._richcmp(right, op) # __hash__ is not properly inherited if comparison is changed def __hash__(self): """ TEST:: sage: w = vector(ZZ, [-1,0,0,0]) sage: w.set_immutable() sage: isinstance(hash(w), int) True """ return free_module_element.FreeModuleElement.__hash__(self) def __len__(self): return self._degree
• ## sage/modules/vector_mod2_dense.pyx

`diff --git a/sage/modules/vector_mod2_dense.pyx b/sage/modules/vector_mod2_dense.pyx`
 a cdef class Vector_mod2_dense(free_module return 0 return mzd_cmp(left._entries, (right)._entries) # see sage/structure/element.pyx def __richcmp__(left, right, int op): """ TEST:: sage: w = vector(GF(2), [-1,0,0,0]) sage: w == w True """ return (left)._richcmp(right, op) # __hash__ is not properly inherited if comparison is changed def __hash__(self): """ TEST:: sage: w = vector(GF(2), [-1,0,0,0]) sage: w.set_immutable() sage: isinstance(hash(w), int) True """ return free_module_element.FreeModuleElement.__hash__(self) def __len__(self): """ EXAMPLES::
• ## sage/modules/vector_modn_dense.pyx

`diff --git a/sage/modules/vector_modn_dense.pyx b/sage/modules/vector_modn_dense.pyx`
 a cdef class Vector_modn_dense(free_module elif l > r: return 1 return 0 # see sage/structure/element.pyx def __richcmp__(left, right, int op): """ TEST:: sage: w = vector(GF(11), [-1,0,0,0]) sage: w == w True """ return (left)._richcmp(right, op) # __hash__ is not properly inherited if comparison is changed def __hash__(self): """ TEST:: sage: w = vector(GF(11), [-1,0,0,0]) sage: w.set_immutable() sage: isinstance(hash(w), int) True """ return free_module_element.FreeModuleElement.__hash__(self) def __len__(self): return self._degree
• ## sage/modules/vector_rational_dense.pyx

`diff --git a/sage/modules/vector_rational_dense.pyx b/sage/modules/vector_rational_dense.pyx`
 a cdef class Vector_rational_dense(free_mo return 1 return 0 # see sage/structure/element.pyx def __richcmp__(left, right, int op): """ TEST:: sage: w = vector(QQ, [-1,0,0,0]) sage: w == w True """ return (left)._richcmp(right, op) # __hash__ is not properly inherited if comparison is changed def __hash__(self): """ TEST:: sage: w = vector(QQ, [-1,0,0,0]) sage: w.set_immutable() sage: isinstance(hash(w), int) True """ return free_module_element.FreeModuleElement.__hash__(self) def __len__(self): return self._degree