Skip to content

Commit

Permalink
Trac #28933: no longer use _cmp_ in vectors
Browse files Browse the repository at this point in the history
after #28694 for matrices

as another step towards getting rid of {{{_cmp_}}}

URL: https://trac.sagemath.org/28933
Reported by: chapoton
Ticket author(s): Frédéric Chapoton
Reviewer(s): Travis Scrimshaw
  • Loading branch information
Release Manager committed Jan 5, 2020
2 parents 51e7813 + 6ac35b8 commit b731e8b
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 48 deletions.
24 changes: 10 additions & 14 deletions src/sage/coding/linear_code.py
Expand Up @@ -192,21 +192,19 @@ class should inherit from this class. Also ``AbstractLinearCode`` should never
sage: C == loads(dumps(C))
True
"""
#******************************************************************************
# *****************************************************************************
# Copyright (C) 2005 David Joyner <wdjoyner@gmail.com>
# 2006 William Stein <wstein@gmail.com>
#
# Distributed under the terms of the GNU General Public License (GPL),
# version 2 or later (at your preference).
#
# http://www.gnu.org/licenses/
#******************************************************************************
# https://www.gnu.org/licenses/
# *****************************************************************************
from __future__ import division, print_function, absolute_import

from six.moves import range
from six import iteritems

import inspect
from copy import copy

from sage.cpython.string import bytes_to_str
Expand All @@ -230,12 +228,10 @@ class should inherit from this class. Also ``AbstractLinearCode`` should never
from sage.misc.all import prod
from sage.misc.functional import is_even
from sage.misc.cachefunc import cached_method
from sage.misc.sageinspect import sage_getargspec
from sage.misc.randstate import current_randstate
from sage.combinat.subset import Subsets
from sage.features.gap import GapPackage
from sage.coding.abstract_code import *

from sage.coding.abstract_code import AbstractCode
from .encoder import Encoder
from .decoder import Decoder

Expand Down Expand Up @@ -2343,7 +2339,7 @@ def permutation_automorphism_group(self, algorithm="partition"):
weights[wt].append(c)
weights.pop(0)
AutGps = []
for wt, words in iteritems(weights):
for wt, words in weights.items():
M = MatrixStruct(matrix(words))
autgp = M.automorphism_group()
L = [[j+1 for j in gen] for gen in autgp[0]]
Expand Down Expand Up @@ -3714,7 +3710,7 @@ class LinearCodeSyndromeDecoder(Decoder):
And now, we build a third syndrome decoder, whose ``maximum_error_weight``
is bigger than both the covering radius and half the minimum distance::
sage: D = C.decoder("Syndrome", maximum_error_weight = 5)
sage: D = C.decoder("Syndrome", maximum_error_weight = 5) # long time
sage: D.decoder_type()
{'complete', 'hard-decision', 'might-error'}
sage: D.decoding_radius()
Expand Down Expand Up @@ -4035,12 +4031,12 @@ def syndrome_table(self):
sage: D = codes.decoders.LinearCodeSyndromeDecoder(C)
sage: D.syndrome_table()
{(0, 0, 0): (0, 0, 0, 0, 0, 0, 0),
(0, 0, 1): (0, 0, 0, 1, 0, 0, 0),
(0, 1, 0): (0, 1, 0, 0, 0, 0, 0),
(0, 1, 1): (0, 0, 0, 0, 0, 1, 0),
(1, 0, 0): (1, 0, 0, 0, 0, 0, 0),
(1, 0, 1): (0, 0, 0, 0, 1, 0, 0),
(0, 1, 0): (0, 1, 0, 0, 0, 0, 0),
(1, 1, 0): (0, 0, 1, 0, 0, 0, 0),
(0, 0, 1): (0, 0, 0, 1, 0, 0, 0),
(1, 0, 1): (0, 0, 0, 0, 1, 0, 0),
(0, 1, 1): (0, 0, 0, 0, 0, 1, 0),
(1, 1, 1): (0, 0, 0, 0, 0, 0, 1)}
"""
return self._lookup_table
Expand Down
12 changes: 6 additions & 6 deletions src/sage/modules/vector_integer_dense.pyx
Expand Up @@ -57,7 +57,7 @@ from cysignals.memory cimport check_allocarray, sig_free
from cysignals.signals cimport sig_on, sig_off

from sage.structure.element cimport Element, ModuleElement, RingElement, Vector

from sage.structure.richcmp cimport rich_to_bool
from sage.rings.integer cimport Integer

cimport sage.modules.free_module_element as free_module_element
Expand Down Expand Up @@ -138,7 +138,7 @@ cdef class Vector_integer_dense(free_module_element.FreeModuleElement):
mpz_clear(self._entries[i])
sig_free(self._entries)

cpdef int _cmp_(left, right) except -2:
cpdef _richcmp_(left, right, int op):
"""
EXAMPLES::
Expand All @@ -159,13 +159,13 @@ cdef class Vector_integer_dense(free_module_element.FreeModuleElement):
"""
cdef Py_ssize_t i
cdef int c
for i from 0 <= i < left.degree():
for i in range(left.degree()):
c = mpz_cmp(left._entries[i], (<Vector_integer_dense>right)._entries[i])
if c < 0:
return -1
return rich_to_bool(op, -1)
elif c > 0:
return 1
return 0
return rich_to_bool(op, 1)
return rich_to_bool(op, 0)

cdef get_unsafe(self, Py_ssize_t i):
"""
Expand Down
29 changes: 16 additions & 13 deletions src/sage/modules/vector_mod2_dense.pyx
Expand Up @@ -24,21 +24,21 @@ TESTS::
True
"""

#*****************************************************************************
# ****************************************************************************
# Copyright (C) 2009 Martin Albrecht <M.R.Albrecht@rhul.ac.uk>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
# http://www.gnu.org/licenses/
#*****************************************************************************
# https://www.gnu.org/licenses/
# ****************************************************************************

from sage.rings.finite_rings.integer_mod cimport IntegerMod_int, IntegerMod_abstract
from sage.rings.integer cimport Integer
from sage.rings.rational cimport Rational
from sage.structure.element cimport Element, ModuleElement, RingElement, Vector

from sage.structure.richcmp cimport rich_to_bool
cimport sage.modules.free_module_element as free_module_element
from .free_module_element import vector

Expand Down Expand Up @@ -211,19 +211,20 @@ cdef class Vector_mod2_dense(free_module_element.FreeModuleElement):
"""
EXAMPLES::
sage: VS = VectorSpace(GF(2),10^3)
sage: import gc
sage: for i in range(10):
....: v = VS.random_element()
....: del v
....: _ = gc.collect()
sage: VS = VectorSpace(GF(2),10^3)
sage: import gc
sage: for i in range(10):
....: v = VS.random_element()
....: del v
....: _ = gc.collect()
"""
if self._entries:
mzd_free(self._entries)

cpdef int _cmp_(left, right) except -2:
cpdef _richcmp_(left, right, int op):
"""
EXAMPLES::
sage: v = vector(GF(2), [0,0,0,0])
sage: v == 0
True
Expand All @@ -240,9 +241,11 @@ cdef class Vector_mod2_dense(free_module_element.FreeModuleElement):
sage: w == w
True
"""
cdef int c
if left._degree == 0:
return 0
return mzd_cmp(left._entries, (<Vector_mod2_dense>right)._entries)
return rich_to_bool(op, 0)
c = mzd_cmp(left._entries, (<Vector_mod2_dense>right)._entries)
return rich_to_bool(op, c)

cdef get_unsafe(self, Py_ssize_t i):
"""
Expand Down
18 changes: 9 additions & 9 deletions src/sage/modules/vector_modn_dense.pyx
Expand Up @@ -102,18 +102,18 @@ AUTHOR:
- William Stein (2007)
"""

#*****************************************************************************
# ****************************************************************************
# Copyright (C) 2007 William Stein <wstein@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
# http://www.gnu.org/licenses/
#*****************************************************************************
# https://www.gnu.org/licenses/
# ****************************************************************************

from cysignals.memory cimport check_allocarray, sig_free

from sage.structure.richcmp cimport rich_to_bool
from sage.rings.finite_rings.stdint cimport INTEGER_MOD_INT64_LIMIT

MAX_MODULUS = INTEGER_MOD_INT64_LIMIT
Expand Down Expand Up @@ -195,7 +195,7 @@ cdef class Vector_modn_dense(free_module_element.FreeModuleElement):
def __dealloc__(self):
sig_free(self._entries)

cpdef int _cmp_(left, right) except -2:
cpdef _richcmp_(left, right, int op):
"""
EXAMPLES::
Expand All @@ -212,14 +212,14 @@ cdef class Vector_modn_dense(free_module_element.FreeModuleElement):
"""
cdef Py_ssize_t i
cdef mod_int l, r
for i from 0 <= i < left.degree():
for i in range(left.degree()):
l = left._entries[i]
r = (<Vector_modn_dense>right)._entries[i]
if l < r:
return -1
return rich_to_bool(op, -1)
elif l > r:
return 1
return 0
return rich_to_bool(op, 1)
return rich_to_bool(op, 0)

cdef get_unsafe(self, Py_ssize_t i):
"""
Expand Down
12 changes: 6 additions & 6 deletions src/sage/modules/vector_rational_dense.pyx
Expand Up @@ -58,7 +58,7 @@ from cysignals.memory cimport check_allocarray, sig_free
from cysignals.signals cimport sig_on, sig_off

from sage.structure.element cimport Element, ModuleElement, RingElement, Vector

from sage.structure.richcmp cimport rich_to_bool
from sage.rings.integer cimport Integer
from sage.rings.rational cimport Rational

Expand Down Expand Up @@ -163,7 +163,7 @@ cdef class Vector_rational_dense(free_module_element.FreeModuleElement):
mpq_clear(self._entries[i])
sig_free(self._entries)

cpdef int _cmp_(left, right) except -2:
cpdef _richcmp_(left, right, int op):
"""
EXAMPLES::
Expand All @@ -185,13 +185,13 @@ cdef class Vector_rational_dense(free_module_element.FreeModuleElement):
"""
cdef Py_ssize_t i
cdef int c
for i from 0 <= i < left.degree():
for i in range(left.degree()):
c = mpq_cmp(left._entries[i], (<Vector_rational_dense>right)._entries[i])
if c < 0:
return -1
return rich_to_bool(op, -1)
elif c > 0:
return 1
return 0
return rich_to_bool(op, 1)
return rich_to_bool(op, 0)

cdef get_unsafe(self, Py_ssize_t i):
"""
Expand Down

0 comments on commit b731e8b

Please sign in to comment.