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

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

r""" 

Sets With a Grading 

""" 

from __future__ import absolute_import 

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

# Copyright (C) 2010-2012 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.misc.abstract_method import abstract_method 

from .category_types import Category 

from sage.categories.sets_cat import Sets 

from sage.categories.enumerated_sets import EnumeratedSets 

from sage.sets.non_negative_integers import NonNegativeIntegers 

 

class SetsWithGrading(Category): 

r""" 

The category of sets with a grading. 

 

A *set with a grading* is a set `S` equipped with a 

grading by some other set `I` (by default the set `\NN` of the 

non-negative integers): 

 

.. MATH:: 

 

S = \biguplus_{i\in I} S_i 

 

where the *graded components* `S_i` are (usually finite) 

sets. The *grading* function maps each element `s` of 

`S` to its *grade* `i`, so that `s\in S_i`. 

 

From implementation point of view, if the graded set is enumerated then 

each graded component should be enumerated (there is a check in the method 

:meth:`~SetsWithGrading.ParentMethods._test_graded_components`). The 

contrary needs not be true. 

 

To implement this category, a parent must either implement 

:meth:`~SetsWithGrading.ParentMethods.graded_component()` or 

:meth:`~SetsWithGrading.ParentMethods.subset()`. If only 

:meth:`~SetsWithGrading.ParentMethods.subset()` is implemented, the first 

argument must be the grading for compatibility with 

:meth:`~SetsWithGrading.ParentMethods.graded_component()`. Additionally 

either the parent must implement 

:meth:`~SetsWithGrading.ParentMethods.grading()` or its elements must 

implement a method ``grade()``. See the example 

:class:`sage.categories.examples.sets_with_grading.NonNegativeIntegers`. 

 

Finally, if the graded set is enumerated (see 

:class:`~sage.categories.enumerated_sets.EnumeratedSets`) then each graded 

component should be enumerated. The contrary needs not be true. 

 

EXAMPLES: 

 

A typical example of a set with a grading is the set of non-negative 

integers graded by themselves:: 

 

sage: N = SetsWithGrading().example(); N 

Non negative integers 

sage: N.category() 

Category of facade sets with grading 

sage: N.grading_set() 

Non negative integers 

 

The *grading function* is given by ``N.grading``:: 

 

sage: N.grading(4) 

4 

 

The graded component `S_i` is the set of all integer partitions of 

`i`:: 

 

sage: N.graded_component(grade = 5) 

{5} 

sage: N.graded_component(grade = 42) 

{42} 

 

Here are some information about this category:: 

 

sage: SetsWithGrading() 

Category of sets with grading 

sage: SetsWithGrading().super_categories() 

[Category of sets] 

sage: SetsWithGrading().all_super_categories() 

[Category of sets with grading, 

Category of sets, 

Category of sets with partial maps, 

Category of objects] 

 

.. TODO:: 

 

- This should be moved to ``Sets().WithGrading()``. 

- Should the grading set be a parameter for this category? 

- Does the enumeration need to be compatible with the grading? Be 

careful that the fact that graded components are allowed to be finite 

or infinite make the answer complicated. 

 

TESTS:: 

 

sage: C = SetsWithGrading() 

sage: TestSuite(C).run() 

""" 

 

@cached_method 

def super_categories(self): 

""" 

EXAMPLES:: 

 

sage: SetsWithGrading().super_categories() 

[Category of sets] 

""" 

return [Sets()] 

 

class ParentMethods: 

 

def _test_graded_components(self, **options): 

r""" 

Test that some graded components of ``self`` are parent with 

initialized category and that the parent has a properly implemented 

``grading()`` method. 

 

EXAMPLES:: 

 

sage: SetsWithGrading().example()._test_graded_components() 

""" 

tester = self._tester(**options) 

for grade in self.grading_set().some_elements(): 

G = self.graded_component(grade) 

if self in EnumeratedSets(): 

tester.assertTrue(G in EnumeratedSets()) 

else: 

tester.assertTrue(G in Sets()) 

for elt in G.some_elements(): 

tester.assertEqual(self.grading(elt), grade) 

 

def grading_set(self): 

""" 

Return the set ``self`` is graded by. By default, this is 

the set of non-negative integers. 

 

EXAMPLES:: 

 

sage: SetsWithGrading().example().grading_set() 

Non negative integers 

""" 

return NonNegativeIntegers() 

 

# TODO: 

# - Should this method be in EnumeratedSets? With a default implementation 

# a la ``filter``? 

# - Do we want to enforce implementing subset rather than graded_component? 

@abstract_method(optional=True) 

def subset(self, *args, **options): 

""" 

Return the subset of ``self`` described by the given parameters. 

 

.. SEEALSO:: 

 

-:meth:`graded_component()` 

 

EXAMPLES:: 

 

sage: W = WeightedIntegerVectors([3,2,1]); W 

Integer vectors weighted by [3, 2, 1] 

sage: W.subset(4) 

Integer vectors of 4 weighted by [3, 2, 1] 

""" 

 

def graded_component(self, grade): 

""" 

Return the graded component of ``self`` with grade ``grade``. 

 

The default implementation just calls the method :meth:`subset()` 

with the first argument ``grade``. 

 

EXAMPLES:: 

 

sage: N = SetsWithGrading().example(); N 

Non negative integers 

sage: N.graded_component(3) 

{3} 

""" 

return self.subset(grade) 

 

def grading(self, elt): 

""" 

Return the grading of the element ``elt`` of ``self``. 

 

This default implementation calls ``elt.grade()``. 

 

EXAMPLES:: 

 

sage: N = SetsWithGrading().example(); N 

Non negative integers 

sage: N.grading(4) 

4 

""" 

return elt.grade() 

 

def generating_series(self): 

""" 

Default implementation for generating series. 

 

OUTPUT: 

 

A series, indexed by the grading set. 

 

EXAMPLES:: 

 

sage: N = SetsWithGrading().example(); N 

Non negative integers 

sage: N.generating_series() 

1/(-z + 1) 

""" 

from sage.combinat.species.series import LazyPowerSeriesRing 

from sage.rings.integer_ring import ZZ 

R = LazyPowerSeriesRing(ZZ) 

R(self.graded_component(grade).cardinality() for grade in self.grading_set()) 

 

# TODO: 

# * asymptotic behavior: we need an object for asymptotic behavior and 

# a default name for the method that should be here. Such method will 

# have two goals (and perhaps need two implementations): give a 

# theorem on asymptotic and be a tool to determine a strategy for 

# algorithms.