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

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

r""" 

Example of facade set 

""" 

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

# Copyright (C) 2010 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.categories.sets_cat import Sets 

from sage.categories.monoids import Monoids 

from sage.structure.parent import Parent 

from sage.structure.unique_representation import UniqueRepresentation 

from sage.rings.integer_ring import ZZ 

from sage.rings.infinity import infinity 

from sage.sets.finite_enumerated_set import FiniteEnumeratedSet 

 

class PositiveIntegerMonoid(UniqueRepresentation, Parent): 

r""" 

 

An example of a facade parent: the positive integers viewed as a 

multiplicative monoid 

 

This class illustrates a minimal implementation of a facade parent 

which models a subset of a set. 

 

EXAMPLES:: 

 

sage: S = Sets().Facade().example(); S 

An example of facade set: the monoid of positive integers 

 

TESTS:: 

 

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_nonzero_equal() . . . 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_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): 

r""" 

EXAMPLES:: 

 

sage: from sage.categories.examples.facade_sets import PositiveIntegerMonoid 

sage: S = PositiveIntegerMonoid(); S 

An example of facade set: the monoid of positive integers 

 

TESTS:: 

 

sage: TestSuite(S).run() 

 

""" 

Parent.__init__(self, facade = ZZ, category = Monoids()) 

 

def _repr_(self): 

r""" 

 

EXAMPLES:: 

 

sage: S = Sets().Facade().example() # indirect doctest 

 

""" 

return "An example of facade set: the monoid of positive integers" 

 

def _element_constructor_(self, object): 

r""" 

Construction of elements 

 

Since ``self`` is a strict subset of the parent it is a facade 

for, it is mandatory to override this method. This method 

indirectly influences membership testing (see 

:meth:`Parent.__contains__`). 

 

EXAMPLES:: 

 

sage: S = Sets().Facade().example(); S 

An example of facade set: the monoid of positive integers 

sage: S(1) 

1 

sage: S(int(1)) 

1 

sage: S(2/1) 

2 

sage: (parent(S(1)) == ZZ, parent(S(int(1))) == ZZ, parent(S(2/1))) 

(True, True, Integer Ring) 

sage: S(1), S(int(1)), S(2/1) 

(1, 1, 2) 

sage: 1 in S 

True 

sage: 2/1 in S, int(1) in S 

(True, True) 

sage: -1 in S, 1/2 in S 

(False, False) 

""" 

object = ZZ(object) 

if object > ZZ(0): 

return object 

else: 

raise ValueError("%s should be positive") 

 

class IntegersCompletion(UniqueRepresentation, Parent): 

r""" 

An example of a facade parent: the set of integers completed with 

`+-\infty` 

 

This class illustrates a minimal implementation of a facade parent 

that models the union of several other parents. 

 

EXAMPLES:: 

 

sage: S = Sets().Facade().example("union"); S 

An example of a facade set: the integers completed by +-infinity 

 

TESTS:: 

 

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

running ._test_an_element() . . . 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_nonzero_equal() . . . 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_eq() . . . pass 

running ._test_new() . . . pass 

running ._test_not_implemented_methods() . . . pass 

running ._test_pickling() . . . pass 

running ._test_some_elements() . . . pass 

""" 

def __init__(self): 

r""" 

EXAMPLES:: 

 

sage: from sage.categories.examples.facade_sets import IntegersCompletion 

sage: S = IntegersCompletion(); S 

An example of a facade set: the integers completed by +-infinity 

 

TESTS:: 

 

sage: TestSuite(S).run() 

 

""" 

# We can't use InfinityRing, because this ring contains 3 

# elements besides +-infinity. We can't use Set either for the 

# moment, because Set([1,2])(1) raises an error 

Parent.__init__(self, facade = (ZZ, FiniteEnumeratedSet([-infinity, +infinity])), category = Sets()) 

 

def _repr_(self): 

r""" 

 

EXAMPLES:: 

 

sage: S = Sets().Facade().example() # indirect doctest 

 

""" 

return "An example of a facade set: the integers completed by +-infinity"