diff --git a/src/sage/monoids/automatic_semigroup.py b/src/sage/monoids/automatic_semigroup.py index 2d0b595dd7f..4f351d372ef 100644 --- a/src/sage/monoids/automatic_semigroup.py +++ b/src/sage/monoids/automatic_semigroup.py @@ -116,7 +116,8 @@ class AutomaticSemigroup(UniqueRepresentation, Parent): sage: [ x.lift() for x in M.list() ] [1, 3, 5, 9] - sage: G = M.cayley_graph(side = "twosided"); G + sage: # needs sage.graphs + sage: G = M.cayley_graph(side="twosided"); G Looped multi-digraph on 4 vertices sage: G.edges(sort=True, key=str) [([1, 1], [1, 1], (2, 'left')), @@ -142,14 +143,16 @@ class AutomaticSemigroup(UniqueRepresentation, Parent): sage: M.j_transversal_of_idempotents() [[1, 1], []] - sage: list(map(attrcall('pseudo_order'), M.list())) + sage: list(map(attrcall('pseudo_order'), M.list())) # needs sage.graphs [[1, 0], [3, 1], [2, 0], [2, 1]] We can also use it to get submonoids from groups. We check that in the symmetric group, a transposition and a long cycle generate the whole group:: + sage: # needs sage.groups sage: G5 = SymmetricGroup(5) - sage: N = AutomaticSemigroup(Family({1: G5([2,1,3,4,5]), 2: G5([2,3,4,5,1])}), one=G5.one()) + sage: N = AutomaticSemigroup(Family({1: G5([2,1,3,4,5]), 2: G5([2,3,4,5,1])}), + ....: one=G5.one()) sage: N.repr_element_method("reduced_word") sage: N.cardinality() == G5.cardinality() True @@ -161,23 +164,26 @@ class AutomaticSemigroup(UniqueRepresentation, Parent): We can also create a semigroup of matrices, where we define the multiplication as matrix multiplication:: - sage: M1=matrix([[0,0,1],[1,0,0],[0,1,0]]) - sage: M2=matrix([[0,0,0],[1,1,0],[0,0,1]]) + sage: # needs sage.modules + sage: M1 = matrix([[0,0,1],[1,0,0],[0,1,0]]) + sage: M2 = matrix([[0,0,0],[1,1,0],[0,0,1]]) sage: M1.set_immutable() sage: M2.set_immutable() sage: def prod_m(x,y): ....: z=x*y ....: z.set_immutable() ....: return z - sage: Mon = AutomaticSemigroup([M1,M2], mul=prod_m, category=Monoids().Finite().Subobjects()) + sage: Mon = AutomaticSemigroup([M1,M2], mul=prod_m, + ....: category=Monoids().Finite().Subobjects()) sage: Mon.cardinality() 24 - sage: C = Mon.cayley_graph() - sage: C.is_directed_acyclic() + sage: C = Mon.cayley_graph() # needs sage.graphs + sage: C.is_directed_acyclic() # needs sage.graphs False Let us construct and play with the 0-Hecke Monoid:: + sage: # needs sage.graphs sage.modules sage: W = WeylGroup(['A',4]); W.rename("W") sage: ambient_monoid = FiniteSetMaps(W, action="right") sage: pi = W.simple_projections(length_increasing=True).map(ambient_monoid) @@ -199,9 +205,9 @@ class AutomaticSemigroup(UniqueRepresentation, Parent): We check that the 0-Hecke monoid is `J`-trivial and contains `2^4` idempotents:: - sage: len(M.idempotents()) + sage: len(M.idempotents()) # needs sage.graphs sage.modules 16 - sage: all(len(j) == 1 for j in M.j_classes()) + sage: all(len(j) == 1 for j in M.j_classes()) # needs sage.graphs sage.modules True TESTS:: @@ -210,9 +216,9 @@ class AutomaticSemigroup(UniqueRepresentation, Parent): True sage: g[1] == g[1]*g[1]*g[1] True - sage: M.__class__ + sage: M.__class__ # needs sage.graphs sage.modules - sage: TestSuite(M).run() + sage: TestSuite(M).run() # needs sage.graphs sage.modules sage: from sage.monoids.automatic_semigroup import AutomaticSemigroup sage: R = IntegerModRing(34) @@ -263,7 +269,10 @@ def __classcall_private__(cls, generators, ambient=None, one=None, mul=operator. sage: M.ambient() == R True sage: AutomaticSemigroup((0,)).category() - Join of Category of finitely generated semigroups and Category of subquotients of semigroups and Category of commutative magmas and Category of subobjects of sets + Join of Category of finitely generated semigroups + and Category of subquotients of semigroups + and Category of commutative magmas + and Category of subobjects of sets sage: AutomaticSemigroup((0,), one=1).category() Join of Category of subquotients of monoids and Category of commutative monoids and @@ -279,8 +288,8 @@ def __classcall_private__(cls, generators, ambient=None, one=None, mul=operator. sage: AutomaticSemigroup((0,), one=0, mul=operator.add).category() Join of Category of monoids and Category of subobjects of sets - sage: S5 = SymmetricGroup(5) - sage: AutomaticSemigroup([S5((1,2))]).category() + sage: S5 = SymmetricGroup(5) # needs sage.groups + sage: AutomaticSemigroup([S5((1,2))]).category() # needs sage.groups Join of Category of finite groups and Category of subquotients of monoids and Category of finite finitely generated semigroups and @@ -400,8 +409,9 @@ def _repr_(self): sage: AutomaticSemigroup(Family({1: R(3), 2: R(5)}), mul=operator.add, one=R.zero()) A semigroup with 2 generators - sage: S5 = SymmetricGroup(5); S5.rename("S5") - sage: AutomaticSemigroup(Family({1: S5((1,2))}), category=Groups().Finite().Subobjects()) + sage: S5 = SymmetricGroup(5); S5.rename("S5") # needs sage.groups + sage: AutomaticSemigroup(Family({1: S5((1,2))}), # needs sage.groups + ....: category=Groups().Finite().Subobjects()) A subgroup of (S5) with 1 generators """ categories = [Groups(), Monoids(), Semigroups()] @@ -468,8 +478,9 @@ def ambient(self): sage: M.ambient() Ring of integers modulo 12 - sage: M1=matrix([[0,0,1],[1,0,0],[0,1,0]]) - sage: M2=matrix([[0,0,0],[1,1,0],[0,0,1]]) + sage: # needs sage.modules + sage: M1 = matrix([[0,0,1],[1,0,0],[0,1,0]]) + sage: M2 = matrix([[0,0,0],[1,1,0],[0,0,1]]) sage: M1.set_immutable() sage: M2.set_immutable() sage: def prod_m(x,y): @@ -488,9 +499,11 @@ def retract(self, ambient_element, check=True): EXAMPLES:: + sage: # needs sage.groups sage: from sage.monoids.automatic_semigroup import AutomaticSemigroup sage: S5 = SymmetricGroup(5); S5.rename("S5") - sage: M = AutomaticSemigroup(Family({1:S5((1,2)), 2:S5((1,2,3,4))}), one=S5.one()) + sage: M = AutomaticSemigroup(Family({1:S5((1,2)), 2:S5((1,2,3,4))}), + ....: one=S5.one()) sage: m = M.retract(S5((3,1))); m (1,3) sage: m.parent() is M @@ -504,7 +517,7 @@ def retract(self, ambient_element, check=True): TESTS:: - sage: len(M._retract.cache.keys()) + sage: len(M._retract.cache.keys()) # needs sage.groups 24 """ element = self._retract(ambient_element) @@ -527,14 +540,15 @@ def _retract(self, ambient_element): EXAMPLES:: sage: from sage.monoids.automatic_semigroup import AutomaticSemigroup - sage: S5 = SymmetricGroup(5) - sage: S4 = AutomaticSemigroup(Family({1:S5((1,2)), 2:S5((1,2,3,4))}), one=S5.one()) - sage: S4._retract(S5((3,1))) + sage: S5 = SymmetricGroup(5) # needs sage.groups + sage: S4 = AutomaticSemigroup(Family({1:S5((1,2)), 2:S5((1,2,3,4))}), # needs sage.groups + ....: one=S5.one()) + sage: S4._retract(S5((3,1))) # needs sage.groups (1,3) No check is done:: - sage: S4._retract(S5((4,5))) + sage: S4._retract(S5((4,5))) # needs sage.groups (4,5) """ return self.element_class(self, ambient_element) @@ -769,9 +783,11 @@ def from_reduced_word(self, l): EXAMPLES:: + sage: # needs sage.groups sage: from sage.monoids.automatic_semigroup import AutomaticSemigroup sage: G4 = SymmetricGroup(4) - sage: M = AutomaticSemigroup(Family({1:G4((1,2)), 2:G4((1,2,3,4))}), one=G4.one()) + sage: M = AutomaticSemigroup(Family({1:G4((1,2)), 2:G4((1,2,3,4))}), + ....: one=G4.one()) sage: M.from_reduced_word([2, 1, 2, 2, 1]).lift() (1,3) sage: M.from_reduced_word([2, 1, 2, 2, 1]) == M.retract(G4((3,1))) @@ -784,7 +800,7 @@ def from_reduced_word(self, l): def construct(self, up_to=None, n=None): """ - Construct the elements of the ``self``. + Construct the elements of ``self``. INPUT: @@ -800,6 +816,7 @@ def construct(self, up_to=None, n=None): EXAMPLES:: + sage: # needs sage.groups sage.modules sage: from sage.monoids.automatic_semigroup import AutomaticSemigroup sage: W = WeylGroup(['A',3]); W.rename("W") sage: ambient_monoid = FiniteSetMaps(W, action="right") diff --git a/src/sage/monoids/free_monoid_element.py b/src/sage/monoids/free_monoid_element.py index e540f408f1d..42d295eab76 100644 --- a/src/sage/monoids/free_monoid_element.py +++ b/src/sage/monoids/free_monoid_element.py @@ -144,6 +144,7 @@ def _latex_(self) -> str: Check that :issue:`14509` is fixed:: + sage: # needs sage.symbolic sage: K.< alpha,b > = FreeAlgebra(SR) sage: latex(alpha*b) \alpha b @@ -171,14 +172,14 @@ def __call__(self, *x, **kwds): """ EXAMPLES:: - sage: M.=FreeMonoid(3) + sage: M. = FreeMonoid(3) sage: (x*y).subs(x=1,y=2,z=14) 2 sage: (x*y).subs({x:z,y:z}) z^2 - sage: M1=MatrixSpace(ZZ,1,2) - sage: M2=MatrixSpace(ZZ,2,1) - sage: (x*y).subs({x:M1([1,2]),y:M2([3,4])}) + sage: M1 = MatrixSpace(ZZ,1,2) # needs sage.modules + sage: M2 = MatrixSpace(ZZ,2,1) # needs sage.modules + sage: (x*y).subs({x: M1([1,2]), y: M2([3,4])}) # needs sage.modules [11] sage: M. = FreeMonoid(2) diff --git a/src/sage/monoids/hecke_monoid.py b/src/sage/monoids/hecke_monoid.py index 179446d24e6..b76867d6e0d 100644 --- a/src/sage/monoids/hecke_monoid.py +++ b/src/sage/monoids/hecke_monoid.py @@ -1,3 +1,4 @@ +# sage.doctest: needs sage.groups """ Hecke Monoids """ diff --git a/src/sage/monoids/monoid.py b/src/sage/monoids/monoid.py index c0330ad0eb3..f6862e2615e 100644 --- a/src/sage/monoids/monoid.py +++ b/src/sage/monoids/monoid.py @@ -1,3 +1,4 @@ +# sage.doctest: needs sage.combinat r""" Monoids """ diff --git a/src/sage/monoids/string_monoid_element.py b/src/sage/monoids/string_monoid_element.py index 0299359c7f7..6a7719c9d7c 100644 --- a/src/sage/monoids/string_monoid_element.py +++ b/src/sage/monoids/string_monoid_element.py @@ -20,12 +20,14 @@ # https://www.gnu.org/licenses/ # **************************************************************************** -# import operator +from sage.misc.lazy_import import lazy_import from sage.rings.integer import Integer -from sage.rings.real_mpfr import RealField -from .free_monoid_element import FreeMonoidElement from sage.structure.richcmp import richcmp +lazy_import('sage.rings.real_mpfr', 'RealField') + +from .free_monoid_element import FreeMonoidElement + def is_StringMonoidElement(x): r""" diff --git a/src/sage/monoids/string_ops.py b/src/sage/monoids/string_ops.py index e34164712fb..500762cb3e5 100644 --- a/src/sage/monoids/string_ops.py +++ b/src/sage/monoids/string_ops.py @@ -8,7 +8,10 @@ # https://www.gnu.org/licenses/ # **************************************************************************** -from sage.rings.real_mpfr import RealField +from sage.misc.lazy_import import lazy_import + +lazy_import('sage.rings.real_mpfr', 'RealField') + from .string_monoid_element import StringMonoidElement diff --git a/src/sage/monoids/trace_monoid.py b/src/sage/monoids/trace_monoid.py index bc7edc7ab08..78ca0c58359 100644 --- a/src/sage/monoids/trace_monoid.py +++ b/src/sage/monoids/trace_monoid.py @@ -42,22 +42,23 @@ from itertools import repeat, chain, product +from sage.combinat.words.alphabet import Alphabet from sage.misc.cachefunc import cached_method +from sage.misc.lazy_import import lazy_import from sage.misc.misc_c import prod - -from sage.graphs.digraph import DiGraph -from sage.graphs.graph import Graph from sage.monoids.free_monoid import FreeMonoid from sage.monoids.monoid import Monoid_class from sage.rings.integer_ring import ZZ from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing from sage.rings.power_series_ring import PowerSeriesRing from sage.rings.infinity import infinity -from sage.combinat.words.alphabet import Alphabet from sage.structure.element import MonoidElement from sage.structure.element_wrapper import ElementWrapper from sage.structure.unique_representation import UniqueRepresentation +lazy_import('sage.graphs.digraph', 'DiGraph') +lazy_import('sage.graphs.graph', 'Graph') + class TraceMonoidElement(ElementWrapper, MonoidElement): r""" @@ -219,7 +220,7 @@ def dependence_graph(self): sage: I = (('a','d'), ('d','a'), ('b','c'), ('c','b')) sage: M. = TraceMonoid(I=I) sage: x = b * a * d * a * c * b - sage: x.dependence_graph() + sage: x.dependence_graph() # needs sage.graphs Digraph on 6 vertices """ elements = self._flat_elements() @@ -265,7 +266,7 @@ def hasse_diagram(self, algorithm="naive"): sage: I = (('a','d'), ('d','a'), ('b','c'), ('c','b')) sage: M. = TraceMonoid(I=I) sage: x = b * a * d * a * c * b - sage: x.hasse_diagram() + sage: x.hasse_diagram() # needs sage.graphs Digraph on 6 vertices TESTS:: @@ -274,10 +275,10 @@ def hasse_diagram(self, algorithm="naive"): sage: I = (('a','d'), ('d','a'), ('b','c'), ('c','b')) sage: M. = TraceMonoid(I=I) sage: x = b * a * d * a * c * b - sage: x.hasse_diagram(algorithm='naive') == x.hasse_diagram(algorithm='min') + sage: x.hasse_diagram(algorithm='naive') == x.hasse_diagram(algorithm='min') # needs sage.graphs True sage: y = b * a^3 * d * a * c * b^2 - sage: y.hasse_diagram(algorithm='naive') == y.hasse_diagram(algorithm='min') + sage: y.hasse_diagram(algorithm='naive') == y.hasse_diagram(algorithm='min') # needs sage.graphs True """ if algorithm == "naive": @@ -306,7 +307,7 @@ def min_hasse_diagram(self): sage: I = (('a','d'), ('d','a'), ('b','c'), ('c','b')) sage: M. = TraceMonoid(I=I) sage: x = b * a * d * a * c * b - sage: x.min_hasse_diagram() + sage: x.min_hasse_diagram() # needs sage.graphs Digraph on 6 vertices """ elements = self._flat_elements() @@ -364,7 +365,7 @@ def naive_hasse_diagram(self): sage: I = (('a','d'), ('d','a'), ('b','c'), ('c','b')) sage: M. = TraceMonoid(I=I) sage: x = b * a * d * a * c * b - sage: x.naive_hasse_diagram() + sage: x.naive_hasse_diagram() # needs sage.graphs Digraph on 6 vertices """ d = self.dependence_graph() @@ -484,7 +485,7 @@ class TraceMonoid(UniqueRepresentation, Monoid_class): sage: from sage.monoids.trace_monoid import TraceMonoid sage: M. = TraceMonoid(I=(('a','c'), ('c','a'))) - sage: M.number_of_words(3) == len(M.words(3)) + sage: M.number_of_words(3) == len(M.words(3)) # needs sage.graphs True """ Element = TraceMonoidElement @@ -815,7 +816,7 @@ def dependence_graph(self): sage: from sage.monoids.trace_monoid import TraceMonoid sage: F. = FreeMonoid() sage: M. = TraceMonoid(F, I=((a,c), (c,a))) - sage: M.dependence_graph() == Graph({a:[a,b], b:[b], c:[c,b]}) + sage: M.dependence_graph() == Graph({a:[a,b], b:[b], c:[c,b]}) # needs sage.graphs True """ return Graph({frozenset((e1, e2)) if e1 != e2 else (e1, e2) @@ -837,7 +838,7 @@ def independence_graph(self): sage: from sage.monoids.trace_monoid import TraceMonoid sage: F. = FreeMonoid() sage: M. = TraceMonoid(F, I=((a,c), (c,a))) - sage: M.independence_graph() == Graph({a:[c], b:[], c:[]}) + sage: M.independence_graph() == Graph({a:[c], b:[], c:[]}) # needs sage.graphs True """ verts = list(self._free_monoid.gens()) @@ -862,7 +863,7 @@ def dependence_polynomial(self, t=None): sage: from sage.monoids.trace_monoid import TraceMonoid sage: I = (('a','d'), ('d','a'), ('b','c'), ('c','b')) sage: M. = TraceMonoid(I=I) - sage: M.dependence_polynomial() + sage: M.dependence_polynomial() # needs sage.graphs 1/(2*t^2 - 4*t + 1) """ if t is None: @@ -890,7 +891,7 @@ def number_of_words(self, length): sage: from sage.monoids.trace_monoid import TraceMonoid sage: I = (('a','d'), ('d','a'), ('b','c'), ('c','b')) sage: M. = TraceMonoid(I=I) - sage: M.number_of_words(3) + sage: M.number_of_words(3) # needs sage.graphs 48 """ psr = PowerSeriesRing(ZZ, default_prec=length + 1) @@ -930,10 +931,8 @@ def words(self, length): sage: from sage.monoids.trace_monoid import TraceMonoid sage: M. = TraceMonoid(I=(('a','b'), ('b','a'), ('b', 'c'), ('c', 'b'))) - sage: for i in range(10): + sage: for i in range(10): # needs sage.graphs ....: assert len(M.words(i)) == M.number_of_words(i) - sage: True - True """ if length < 0: raise ValueError("bad length of words; expected zero or positive number")