Skip to content

Commit

Permalink
Trac #32413: Remove unnecessary uses of SR and symbolic functions in …
Browse files Browse the repository at this point in the history
…sage.categories, sage.coding

`git grep -E '[.](symbolic|calculus|functions).*import' src/sage/coding`
reveals a number of calls to the symbolic calculus subsystem of Sage
that can be avoided, similar to what is done in #32411

URL: https://trac.sagemath.org/32413
Reported by: mkoeppe
Ticket author(s): Frédéric Chapoton
Reviewer(s): Matthias Koeppe
  • Loading branch information
Release Manager committed Sep 7, 2021
2 parents de5c329 + f96c696 commit f91cda2
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 43 deletions.
6 changes: 3 additions & 3 deletions src/sage/categories/coalgebras_with_basis.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,13 @@ def coproduct_iterated(self, n=1):
return self
if n == 1:
return self.coproduct()
from sage.functions.all import ceil
from sage.rings.integer import Integer

# Use coassociativity of `\Delta` to perform many coproducts simultaneously.
fn = Integer(n - 1) // 2
cn = ceil(Integer(n - 1) / 2)
split = lambda a,b: tensor([a.coproduct_iterated(fn), b.coproduct_iterated(cn)])
cn = Integer(n - 1) // 2 if n % 2 else Integer(n) // 2
split = lambda a, b: tensor([a.coproduct_iterated(fn),
b.coproduct_iterated(cn)])
return self.coproduct().apply_multilinear_morphism(split)

class Super(SuperModulesCategory):
Expand Down
21 changes: 12 additions & 9 deletions src/sage/categories/examples/finite_enumerated_sets.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
r"""
Examples of finite enumerated sets
"""
#*****************************************************************************
# ****************************************************************************
# Copyright (C) 2009 Florent Hivert <[email protected]>
#
# Distributed under the terms of the GNU General Public License (GPL)
# http://www.gnu.org/licenses/
#******************************************************************************
# https://www.gnu.org/licenses/
# *****************************************************************************
from sage.structure.parent import Parent
from sage.structure.unique_representation import UniqueRepresentation
from sage.categories.finite_enumerated_sets import FiniteEnumeratedSets
from sage.rings.integer import Integer
from sage.rings.integer_ring import IntegerRing
from sage.functions.other import sqrt


class Example(UniqueRepresentation, Parent):
r"""
Expand Down Expand Up @@ -75,8 +75,9 @@ def __init__(self):
Category of facade finite enumerated sets
sage: TestSuite(C).run()
"""
self._set = [Integer(_) for _ in [1,2,3]]
Parent.__init__(self, facade = IntegerRing(), category = FiniteEnumeratedSets())
self._set = [Integer(_) for _ in [1, 2, 3]]
Parent.__init__(self, facade=IntegerRing(),
category=FiniteEnumeratedSets())

def _repr_(self):
"""
Expand Down Expand Up @@ -109,9 +110,10 @@ def __iter__(self):
"""
return iter(self._set)


class IsomorphicObjectOfFiniteEnumeratedSet(UniqueRepresentation, Parent):

def __init__(self, ambient = Example()):
def __init__(self, ambient=Example()):
"""
TESTS::
Expand All @@ -123,7 +125,8 @@ def __init__(self, ambient = Example()):
sage: TestSuite(C).run()
"""
self._ambient = ambient
Parent.__init__(self, facade = IntegerRing(), category = FiniteEnumeratedSets().IsomorphicObjects())
Parent.__init__(self, facade=IntegerRing(),
category=FiniteEnumeratedSets().IsomorphicObjects())

def ambient(self):
"""
Expand Down Expand Up @@ -156,7 +159,7 @@ def lift(self, x):
sage: C.lift(9)
3
"""
return sqrt(x)
return x.sqrt()

def retract(self, x):
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
r"""
Examples of graded connected Hopf algebras with basis
"""
#*****************************************************************************
# ****************************************************************************
# Copyright (C) 2015 Jean-Baptiste Priez <[email protected]>
#
# Distributed under the terms of the GNU General Public License (GPL)
# http://www.gnu.org/licenses/
#*****************************************************************************
# https://www.gnu.org/licenses/
# ****************************************************************************

from sage.categories.graded_hopf_algebras_with_basis import GradedHopfAlgebrasWithBasis
from sage.combinat.free_module import CombinatorialFreeModule
from sage.functions.other import binomial
from sage.arith.misc import binomial
from sage.misc.cachefunc import cached_method
from sage.sets.non_negative_integers import NonNegativeIntegers

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1120,7 +1120,7 @@ def chevalley_eilenberg_complex(self, M=None, dual=False, sparse=True, ncpus=Non
" (the trivial module)")

from itertools import combinations
from sage.functions.other import binomial
from sage.arith.misc import binomial
from sage.matrix.matrix_space import MatrixSpace
R = self.base_ring()
zero = R.zero()
Expand Down
2 changes: 1 addition & 1 deletion src/sage/categories/lie_conformal_algebras.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ def _test_jacobi(self, **options):
tester = self._tester(**options)
S = tester.some_elements()
from sage.misc.misc import some_tuples
from sage.functions.other import binomial
from sage.arith.misc import binomial
pz = tester._instance.zero()
for x,y,z in some_tuples(S, 3, tester._max_runs):
brxy = x.bracket(y)
Expand Down
2 changes: 1 addition & 1 deletion src/sage/categories/super_lie_conformal_algebras.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def _test_jacobi(self, **options):
elements.append(s)
S = elements
from sage.misc.misc import some_tuples
from sage.functions.other import binomial
from sage.arith.misc import binomial
pz = tester._instance.zero()
for x,y,z in some_tuples(S, 3, tester._max_runs):
if x.is_zero() or y.is_zero():
Expand Down
8 changes: 4 additions & 4 deletions src/sage/coding/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@
# 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 copy import copy

from sage.structure.sage_object import SageObject
from sage.rings.integer import Integer
Expand All @@ -55,8 +56,7 @@
from sage.misc.abstract_method import abstract_method
from sage.categories.cartesian_product import cartesian_product
from sage.modules.free_module import VectorSpace
from sage.functions.other import binomial
from copy import copy
from sage.arith.misc import binomial


def random_error_vector(n, F, error_positions):
Expand Down
2 changes: 1 addition & 1 deletion src/sage/coding/codecan/autgroup_can_label.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ and to the action of the symmetric group only::

from sage.coding.codecan.codecan import PartitionRefinementLinearCode
from sage.combinat.permutation import Permutation
from sage.functions.other import factorial
from sage.arith.misc import factorial


def _cyclic_shift(n, p):
Expand Down
7 changes: 4 additions & 3 deletions src/sage/coding/grs_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
# (at your option) any later version.
# https://www.gnu.org/licenses/
# ****************************************************************************
from copy import copy

from sage.categories.cartesian_product import cartesian_product

Expand All @@ -63,14 +64,14 @@
from sage.misc.functional import symbolic_sum
from sage.misc.misc_c import prod

from copy import copy
from sage.functions.other import binomial, floor
from sage.functions.other import binomial
from sage.symbolic.ring import SR

from .linear_code import AbstractLinearCode
from .encoder import Encoder
from .decoder import Decoder, DecodingError


class GeneralizedReedSolomonCode(AbstractLinearCode):
r"""
Representation of a (Generalized) Reed-Solomon code.
Expand Down Expand Up @@ -1563,7 +1564,7 @@ def _partial_xgcd(self, a, b, PolRing):
sage: D._partial_xgcd(a, b, P)
(10*x^2 + 3*x + 5, 1)
"""
stop = floor(self.code().dimension() + self.code().length()) // 2
stop = (self.code().dimension() + self.code().length()) // 2
s = PolRing.one()
prev_s = PolRing.zero()

Expand Down
22 changes: 11 additions & 11 deletions src/sage/coding/guruswami_sudan/interpolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@
- David Lucas, ported the original implementation in Sage
"""

#*****************************************************************************
# ****************************************************************************
# Copyright (C) 2015 David Lucas <[email protected]>
# 2015 Johan S. R. Nielsen <[email protected]>
#
# 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.functions.other import ceil, binomial
from sage.arith.misc import binomial
from sage.matrix.constructor import matrix
from sage.misc.misc_c import prod

Expand Down Expand Up @@ -68,7 +68,7 @@ def _monomial_list(maxdeg, l, wy):
EXAMPLES::
sage: from sage.coding.guruswami_sudan.interpolation import _monomial_list
sage: _monomial_list(8, 1, 3)
sage: list(_monomial_list(8, 1, 3))
[(0, 0),
(1, 0),
(2, 0),
Expand All @@ -83,11 +83,10 @@ def _monomial_list(maxdeg, l, wy):
(3, 1),
(4, 1)]
"""
monomials = []
for y in range(0, l+1):
for x in range(0, ceil(maxdeg - y*wy)):
monomials.append((x, y))
return monomials
for y in range(l + 1):
for x in range(maxdeg - y * wy):
yield (x, y)


def _interpolation_matrix_given_monomials(points, s, monomials):
r"""
Expand Down Expand Up @@ -216,10 +215,11 @@ def _interpolation_matrix_problem(points, tau, parameters, wy):
)
"""
s, l = parameters[0], parameters[1]
monomials = _monomial_list(_interpolation_max_weighted_deg(len(points), tau, s), l, wy)
monomials = list(_monomial_list(_interpolation_max_weighted_deg(len(points), tau, s), l, wy))
M = _interpolation_matrix_given_monomials(points, s, monomials)
return (M, monomials)


def gs_interpolation_linalg(points, tau, parameters, wy):
r"""
Compute an interpolation polynomial Q(x,y) for the Guruswami-Sudan algorithm
Expand Down
11 changes: 6 additions & 5 deletions src/sage/coding/reed_muller_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,21 @@
- :class:`ReedMullerVectorEncoder`, an encoder with a vectorial message space (for both the two code classes)
- :class:`ReedMullerPolynomialEncoder`, an encoder with a polynomial message space (for both the code classes)
"""
#*****************************************************************************
# ****************************************************************************
# Copyright (C) 2016 Parthasarathi Panda <[email protected]>
#
# 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 operator import mul
from functools import reduce

from sage.matrix.constructor import matrix
from sage.functions.other import binomial
from sage.arith.misc import binomial
from sage.coding.linear_code import AbstractLinearCode, LinearCodeSyndromeDecoder
from sage.coding.encoder import Encoder
from sage.combinat.subset import Subsets
Expand All @@ -38,7 +40,6 @@
from sage.modules.free_module_element import vector
from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing
from sage.misc.cachefunc import cached_method
from functools import reduce


def _binomial_sum(n, k):
Expand Down

0 comments on commit f91cda2

Please sign in to comment.