Skip to content

Commit

Permalink
sagemathgh-37783: various changes about is_field and is_ring
Browse files Browse the repository at this point in the history
    
just miscellanous changes about not using `is_ring`, replacing some
`is_field` by `in Fields`

and having more precise categories in descent algebras

also sorting the imports on the way

### 📝 Checklist

- [x] The title is concise and informative.
- [x] The description explains in detail what this PR is about.
    
URL: sagemath#37783
Reported by: Frédéric Chapoton
Reviewer(s): Matthias Köppe, Travis Scrimshaw
  • Loading branch information
Release Manager committed Apr 20, 2024
2 parents e5758a0 + b807676 commit ca4ec76
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 123 deletions.
96 changes: 43 additions & 53 deletions src/sage/combinat/descent_algebra.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,26 @@
# https://www.gnu.org/licenses/
# ****************************************************************************

from sage.misc.cachefunc import cached_method
from sage.misc.bindable_class import BindableClass
from sage.misc.lazy_attribute import lazy_attribute
from sage.structure.parent import Parent
from sage.structure.unique_representation import UniqueRepresentation
from sage.arith.misc import factorial
from sage.categories.algebras import Algebras
from sage.categories.commutative_rings import CommutativeRings
from sage.categories.realizations import Realizations, Category_realization_of_parent
from sage.categories.fields import Fields
from sage.categories.finite_dimensional_algebras_with_basis import FiniteDimensionalAlgebrasWithBasis
from sage.rings.integer_ring import ZZ
from sage.rings.rational_field import QQ
from sage.arith.misc import factorial
from sage.combinat.free_module import CombinatorialFreeModule
from sage.combinat.permutation import Permutations
from sage.categories.realizations import Realizations, Category_realization_of_parent
from sage.combinat.composition import Compositions
from sage.combinat.free_module import CombinatorialFreeModule
from sage.combinat.integer_matrices import IntegerMatrices
from sage.combinat.ncsf_qsym.ncsf import NonCommutativeSymmetricFunctions
from sage.combinat.permutation import Permutations
from sage.combinat.subset import SubsetsSorted
from sage.combinat.symmetric_group_algebra import SymmetricGroupAlgebra
from sage.combinat.ncsf_qsym.ncsf import NonCommutativeSymmetricFunctions
from sage.misc.bindable_class import BindableClass
from sage.misc.cachefunc import cached_method
from sage.misc.lazy_attribute import lazy_attribute
from sage.rings.integer_ring import ZZ
from sage.rings.rational_field import QQ
from sage.structure.parent import Parent
from sage.structure.unique_representation import UniqueRepresentation


class DescentAlgebra(UniqueRepresentation, Parent):
Expand Down Expand Up @@ -135,12 +136,33 @@ def __init__(self, R, n):
EXAMPLES::
sage: TestSuite(DescentAlgebra(QQ, 4)).run()
TESTS::
sage: B = DescentAlgebra(QQ, 4).B()
sage: B.is_commutative()
False
sage: B = DescentAlgebra(QQ, 1).B()
sage: B.is_commutative()
True
sage: B = DescentAlgebra(QQ, 4).B()
sage: B in Fields()
False
sage: B = DescentAlgebra(QQ, 1).B()
sage: B in Fields()
True
"""
self._n = n
self._category = FiniteDimensionalAlgebrasWithBasis(R)
cat = FiniteDimensionalAlgebrasWithBasis(R)
if R in CommutativeRings() and n <= 2:
cat = cat.Commutative()
if R in Fields() and n <= 1:
cat &= Fields()
self._category = cat
Parent.__init__(self, base=R, category=self._category.WithRealizations())

def _repr_(self):
def _repr_(self) -> str:
r"""
Return a string representation of ``self``.
Expand Down Expand Up @@ -240,7 +262,7 @@ def _element_constructor_(self, x):
return CombinatorialFreeModule._element_constructor_(self, x)

# We need to overwrite this since our basis elements must be indexed by tuples
def _repr_term(self, S):
def _repr_term(self, S) -> str:
r"""
EXAMPLES::
Expand All @@ -264,7 +286,7 @@ def product_on_basis(self, S, T):
return self(self.to_B_basis(S) * self.to_B_basis(T))

@cached_method
def one_basis(self):
def one_basis(self) -> tuple:
r"""
Return the identity element, as per
``AlgebrasWithBasis.ParentMethods.one_basis``.
Expand Down Expand Up @@ -310,7 +332,8 @@ def to_B_basis(self, S):

n = self.realization_of()._n
C = Compositions(n)
return B.sum_of_terms([(C.from_subset(T, n), (-1)**(len(S) - len(T)))
lenS = len(S)
return B.sum_of_terms([(C.from_subset(T, n), (-1)**(lenS - len(T)))
for T in SubsetsSorted(S)])

def to_symmetric_group_algebra_on_basis(self, S):
Expand Down Expand Up @@ -839,7 +862,7 @@ def __init__(self, base):
"""
Category_realization_of_parent.__init__(self, base)

def _repr_(self):
def _repr_(self) -> str:
r"""
Return the representation of ``self``.
Expand All @@ -852,7 +875,7 @@ def _repr_(self):
"""
return "Category of bases of {}".format(self.base())

def super_categories(self):
def super_categories(self) -> list:
r"""
The super categories of ``self``.
Expand All @@ -868,7 +891,7 @@ def super_categories(self):
return [self.base()._category, Realizations(self.base())]

class ParentMethods:
def _repr_(self):
def _repr_(self) -> str:
"""
Text representation of this basis of a descent algebra.
Expand Down Expand Up @@ -914,39 +937,6 @@ def __getitem__(self, p):
p = [p]
return self.monomial(C(p))

def is_field(self, proof=True):
"""
Return whether this descent algebra is a field.
EXAMPLES::
sage: B = DescentAlgebra(QQ, 4).B()
sage: B.is_field()
False
sage: B = DescentAlgebra(QQ, 1).B()
sage: B.is_field()
True
"""
if self.realization_of()._n <= 1:
return self.base_ring().is_field()
return False

def is_commutative(self) -> bool:
"""
Return whether this descent algebra is commutative.
EXAMPLES::
sage: B = DescentAlgebra(QQ, 4).B()
sage: B.is_commutative()
False
sage: B = DescentAlgebra(QQ, 1).B()
sage: B.is_commutative()
True
"""
return (self.base_ring() in CommutativeRings()
and self.realization_of()._n <= 2)

@lazy_attribute
def to_symmetric_group_algebra(self):
"""
Expand Down
12 changes: 5 additions & 7 deletions src/sage/geometry/polyhedron/parent.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,9 +481,8 @@ def Vrepresentation_space(self):
if self.base_ring() in Fields():
from sage.modules.free_module import VectorSpace
return VectorSpace(self.base_ring(), self.ambient_dim())
else:
from sage.modules.free_module import FreeModule
return FreeModule(self.base_ring(), self.ambient_dim())
from sage.modules.free_module import FreeModule
return FreeModule(self.base_ring(), self.ambient_dim())

ambient_space = Vrepresentation_space

Expand Down Expand Up @@ -522,7 +521,6 @@ def _repr_base_ring(self):
sage: Polyhedra(K, 4)._repr_base_ring() # needs sage.rings.number_field
'(Number Field in sqrt3 with defining polynomial x^2 - 3 with sqrt3 = 1.732050807568878?)'
"""

if self.base_ring() is ZZ:
return 'ZZ'
if self.base_ring() is QQ:
Expand All @@ -536,9 +534,9 @@ def _repr_base_ring(self):
else:
if self.base_ring() is AA:
return 'AA'
return '({0})'.format(self.base_ring())
return f'({self.base_ring()})'

def _repr_ambient_module(self):
def _repr_ambient_module(self) -> str:
"""
Return an abbreviated string representation of the ambient
space.
Expand Down Expand Up @@ -886,7 +884,7 @@ def _coerce_base_ring(self, other):
from sage.structure.element import Element
if isinstance(other, Element):
other = other.parent()
if hasattr(other, "is_ring") and other.is_ring():
if other in Rings():
other_ring = other
else:
try:
Expand Down
26 changes: 13 additions & 13 deletions src/sage/modular/pollack_stevens/dist.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,26 @@ REFERENCES:
# the License, or (at your option) any later version.
# https://www.gnu.org/licenses/
# ****************************************************************************
import operator

from sage.structure.richcmp cimport richcmp_not_equal, rich_to_bool
from sage.rings.integer_ring import ZZ
from sage.rings.rational_field import QQ
from sage.rings.power_series_ring import PowerSeriesRing
from sage.rings.finite_rings.integer_mod_ring import Zmod
from sage.arith.misc import binomial, bernoulli
from sage.matrix.matrix cimport Matrix
from sage.categories.fields import Fields
from sage.matrix.constructor import matrix
from sage.structure.element cimport Element
import operator
from sage.rings.padics.padic_generic import pAdicGeneric
from sage.rings.integer cimport Integer
from sage.matrix.matrix cimport Matrix
from sage.misc.verbose import verbose
from sage.modular.pollack_stevens.sigma0 import Sigma0
from sage.rings.finite_rings.integer_mod_ring import Zmod
from sage.rings.infinity import Infinity
from sage.rings.integer cimport Integer
from sage.rings.integer_ring import ZZ
from sage.rings.padics.padic_generic import pAdicGeneric
from sage.rings.power_series_ring import PowerSeriesRing
from sage.rings.rational_field import QQ
from sage.structure.element cimport Element
from sage.structure.richcmp cimport richcmp_not_equal, rich_to_bool

#from sage.libs.flint.ulong_extras cimport *

from sage.modular.pollack_stevens.sigma0 import Sigma0

cdef long overflow = 1 << (4 * sizeof(long) - 1)
cdef long underflow = -overflow
cdef long maxordp = (1L << (sizeof(long) * 8 - 2)) - 1
Expand Down Expand Up @@ -1166,7 +1166,7 @@ cdef class Dist_vector(Dist):
p = self.parent().prime()
cdef Dist_vector ans
if p == 0:
if R.is_field():
if R in Fields():
ans = self._new_c()
ans.ordp = 0
ans._moments = V(v)
Expand Down
40 changes: 20 additions & 20 deletions src/sage/modular/pollack_stevens/distributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,31 @@
(1 + O(11^5), 2 + O(11^4), 3 + O(11^3), 4 + O(11^2), 5 + O(11))
"""
# ****************************************************************************
# *************************************************************************
# Copyright (C) 2012 Robert Pollack <[email protected]>
#
# Distributed under the terms of the GNU General Public License (GPL)
# as published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
# https://www.gnu.org/licenses/
# ****************************************************************************
# *************************************************************************

from sage.categories.fields import Fields
from sage.categories.modules import Modules
from sage.misc.cachefunc import cached_method
from sage.misc.lazy_import import lazy_import
from sage.modules.module import Module
from sage.structure.parent import Parent
from sage.rings.rational_field import QQ
from sage.rings.integer_ring import ZZ
from sage.misc.cachefunc import cached_method
from sage.categories.modules import Modules
from sage.structure.factory import UniqueFactory

from sage.rings.rational_field import QQ
from sage.rings.ring import Ring
from sage.structure.factory import UniqueFactory
from sage.structure.parent import Parent
from .sigma0 import _default_adjuster

lazy_import('sage.modular.pollack_stevens.dist', 'get_dist_classes')
lazy_import('sage.rings.padics.factory', ['ZpCA', 'QpCR'])
lazy_import('sage.rings.padics.padic_generic', 'pAdicGeneric')

from .sigma0 import _default_adjuster


class OverconvergentDistributions_factory(UniqueFactory):
"""
Expand Down Expand Up @@ -284,7 +283,7 @@ def __init__(self, k, p=None, prec_cap=None, base=None, character=None,
"""
if not isinstance(base, Ring):
raise TypeError("base must be a ring")
#from sage.rings.padics.pow_computer import PowComputer
# from sage.rings.padics.pow_computer import PowComputer
# should eventually be the PowComputer on ZpCA once that uses longs.
Dist, WeightKAction = get_dist_classes(p, prec_cap, base,
self.is_symk(), implementation)
Expand Down Expand Up @@ -318,9 +317,9 @@ def _element_constructor_(self, val, **kwargs):
sage: v = V([1,2,3,4,5,6,7]); v
(1, 2, 3, 4, 5, 6, 7)
"""
ordp = kwargs.get('ord',0)
check = kwargs.get('check',True)
normalize = kwargs.get('normalize',True)
ordp = kwargs.get('ord', 0)
check = kwargs.get('check', True)
normalize = kwargs.get('normalize', True)
return self.Element(val, self, ordp, check, normalize)

def _coerce_map_from_(self, other):
Expand Down Expand Up @@ -410,13 +409,14 @@ def prime(self):

def weight(self):
"""
Return the weight of this distribution space. The standard
caveat applies, namely that the weight of `Sym^k` is
defined to be `k`, not `k+2`.
Return the weight of this distribution space.
The standard caveat applies, namely that the weight of `Sym^k`
is defined to be `k`, not `k+2`.
OUTPUT:
- nonnegative integer
nonnegative integer
EXAMPLES::
Expand Down Expand Up @@ -473,7 +473,7 @@ def lift(self, p=None, M=None, new_base_ring=None):
"""
if self._character is not None:
if self._character.base_ring() != QQ:
# need to change coefficient ring for character
# need to change coefficient ring for character
raise NotImplementedError
if M is None:
M = self._prec_cap + 1
Expand Down Expand Up @@ -690,7 +690,7 @@ def _repr_(self):
elif self.base_ring() is ZZ:
V = 'Z^2'
elif isinstance(self.base_ring(), pAdicGeneric) and self.base_ring().degree() == 1:
if self.base_ring().is_field():
if self.base_ring() in Fields():
V = 'Q_%s^2' % self._p
else:
V = 'Z_%s^2' % self._p
Expand Down
Loading

0 comments on commit ca4ec76

Please sign in to comment.