Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

""" 

Examples of finite monoids 

""" 

#***************************************************************************** 

# Copyright (C) 2008-2009 Nicolas M. Thiery <nthiery at users.sf.net> 

# 

# Distributed under the terms of the GNU General Public License (GPL) 

# http://www.gnu.org/licenses/ 

#****************************************************************************** 

 

from sage.misc.cachefunc import cached_method 

from sage.sets.family import Family 

from sage.structure.parent import Parent 

from sage.structure.unique_representation import UniqueRepresentation 

from sage.structure.element_wrapper import ElementWrapper 

from sage.categories.all import Monoids 

from sage.rings.integer import Integer 

from sage.rings.integer_ring import ZZ 

 

class IntegerModMonoid(UniqueRepresentation, Parent): 

r""" 

An example of a finite monoid: the integers mod `n` 

 

This class illustrates a minimal implementation of a finite monoid. 

 

EXAMPLES:: 

 

sage: S = FiniteMonoids().example(); S 

An example of a finite multiplicative monoid: the integers modulo 12 

 

sage: S.category() 

Category of finitely generated finite enumerated monoids 

 

We conclude by running systematic tests on this monoid:: 

 

sage: TestSuite(S).run(verbose = True) 

running ._test_an_element() . . . pass 

running ._test_associativity() . . . pass 

running ._test_cardinality() . . . pass 

running ._test_category() . . . pass 

running ._test_elements() . . . 

Running the test suite of self.an_element() 

running ._test_category() . . . pass 

running ._test_eq() . . . pass 

running ._test_new() . . . pass 

running ._test_not_implemented_methods() . . . pass 

running ._test_pickling() . . . pass 

pass 

running ._test_elements_eq_reflexive() . . . pass 

running ._test_elements_eq_symmetric() . . . pass 

running ._test_elements_eq_transitive() . . . pass 

running ._test_elements_neq() . . . pass 

running ._test_enumerated_set_contains() . . . pass 

running ._test_enumerated_set_iter_cardinality() . . . pass 

running ._test_enumerated_set_iter_list() . . . pass 

running ._test_eq() . . . pass 

running ._test_new() . . . pass 

running ._test_not_implemented_methods() . . . pass 

running ._test_one() . . . pass 

running ._test_pickling() . . . pass 

running ._test_prod() . . . pass 

running ._test_some_elements() . . . pass 

""" 

 

def __init__(self, n = 12): 

r""" 

EXAMPLES:: 

 

sage: M = FiniteMonoids().example(6); M 

An example of a finite multiplicative monoid: the integers modulo 6 

 

TESTS:: 

 

sage: TestSuite(M).run() 

 

""" 

self.n = n 

Parent.__init__(self, category=Monoids().Finite().FinitelyGenerated()) 

 

def _repr_(self): 

r""" 

TESTS:: 

 

sage: M = FiniteMonoids().example() 

sage: M._repr_() 

'An example of a finite multiplicative monoid: the integers modulo 12' 

""" 

return "An example of a finite multiplicative monoid: the integers modulo %s"%self.n 

 

def semigroup_generators(self): 

r""" 

 

Returns a set of generators for ``self``, as per 

:meth:`Semigroups.ParentMethods.semigroup_generators`. 

Currently this returns all integers mod `n`, which is of 

course far from optimal! 

 

EXAMPLES:: 

 

sage: M = FiniteMonoids().example() 

sage: M.semigroup_generators() 

Family (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11) 

""" 

return Family(tuple(self(ZZ(i)) for i in range(self.n))) 

 

@cached_method 

def one(self): 

r""" 

Return the one of the monoid, as per :meth:`Monoids.ParentMethods.one`. 

 

EXAMPLES:: 

 

sage: M = FiniteMonoids().example() 

sage: M.one() 

1 

 

""" 

return self(ZZ.one()) 

 

def product(self, x, y): 

r""" 

Return the product of two elements `x` and `y` of the monoid, as 

per :meth:`Semigroups.ParentMethods.product`. 

 

EXAMPLES:: 

 

sage: M = FiniteMonoids().example() 

sage: M.product(M(3), M(5)) 

3 

""" 

return self((x.value * y.value) % self.n) 

 

def an_element(self): 

r""" 

Returns an element of the monoid, as per :meth:`Sets.ParentMethods.an_element`. 

 

EXAMPLES:: 

 

sage: M = FiniteMonoids().example() 

sage: M.an_element() 

6 

""" 

return self(ZZ(42) % self.n) 

 

class Element (ElementWrapper): 

wrapped_class = Integer 

 

Example = IntegerModMonoid