From 69e7af663abafd7deebd68c6f396ba5a363b9381 Mon Sep 17 00:00:00 2001 From: user202729 <25191436+user202729@users.noreply.github.com> Date: Tue, 22 Oct 2024 16:47:19 +0700 Subject: [PATCH 1/3] Remove redundant modulo operation in vector_modn_dense --- src/sage/modules/vector_modn_dense.pyx | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/sage/modules/vector_modn_dense.pyx b/src/sage/modules/vector_modn_dense.pyx index 79e27b5b3b9..4c7d933321e 100644 --- a/src/sage/modules/vector_modn_dense.pyx +++ b/src/sage/modules/vector_modn_dense.pyx @@ -168,8 +168,22 @@ cdef class Vector_modn_dense(free_module_element.FreeModuleElement): self._init(parent.degree(), parent, parent.base_ring().order()) def __init__(self, parent, x, coerce=True, copy=True): + """ + Create an element. + + Note that ``coerce=False`` is dangerous:: + + sage: V = VectorSpace(GF(7), 3) + sage: v = V([2, 9, -5], coerce=False) + sage: v[0] == v[1] + False + sage: v[0]+1 == v[1]+1 + True + sage: v[0] == v[2] + False + """ cdef Py_ssize_t i - cdef mod_int a, p + cdef mod_int a if isinstance(x, xrange): x = tuple(x) if isinstance(x, (list, tuple)): @@ -177,10 +191,9 @@ cdef class Vector_modn_dense(free_module_element.FreeModuleElement): raise TypeError("x must be a list of the right length") if coerce: R = parent.base_ring() - p = R.order() for i from 0 <= i < self._degree: a = int(R(x[i])) - self._entries[i] = a % p + self._entries[i] = a else: for i from 0 <= i < self._degree: self._entries[i] = x[i] From 68196350db3bbd771576cdaa9ee8b62aa356b624 Mon Sep 17 00:00:00 2001 From: user202729 <25191436+user202729@users.noreply.github.com> Date: Sun, 27 Oct 2024 09:38:12 +0700 Subject: [PATCH 2/3] Minor change --- src/sage/modules/vector_modn_dense.pyx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sage/modules/vector_modn_dense.pyx b/src/sage/modules/vector_modn_dense.pyx index 4c7d933321e..e21360964ba 100644 --- a/src/sage/modules/vector_modn_dense.pyx +++ b/src/sage/modules/vector_modn_dense.pyx @@ -184,7 +184,7 @@ cdef class Vector_modn_dense(free_module_element.FreeModuleElement): """ cdef Py_ssize_t i cdef mod_int a - if isinstance(x, xrange): + if isinstance(x, range): x = tuple(x) if isinstance(x, (list, tuple)): if len(x) != self._degree: From c97a63962198c2e87552350ce51e15997fc1ae8d Mon Sep 17 00:00:00 2001 From: user202729 <25191436+user202729@users.noreply.github.com> Date: Fri, 1 Nov 2024 09:04:06 +0700 Subject: [PATCH 3/3] Apply suggestions from code review Co-authored-by: Travis Scrimshaw --- src/sage/modules/vector_modn_dense.pyx | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/sage/modules/vector_modn_dense.pyx b/src/sage/modules/vector_modn_dense.pyx index e21360964ba..f129632bd03 100644 --- a/src/sage/modules/vector_modn_dense.pyx +++ b/src/sage/modules/vector_modn_dense.pyx @@ -171,13 +171,15 @@ cdef class Vector_modn_dense(free_module_element.FreeModuleElement): """ Create an element. + TESTS: + Note that ``coerce=False`` is dangerous:: sage: V = VectorSpace(GF(7), 3) sage: v = V([2, 9, -5], coerce=False) sage: v[0] == v[1] False - sage: v[0]+1 == v[1]+1 + sage: v[0] + 1 == v[1] + 1 True sage: v[0] == v[2] False