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

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

## -*- encoding: utf-8 -*- 

r""" 

Homset categories 

""" 

from __future__ import absolute_import 

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

# Copyright (C) 2014 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, cached_function 

from sage.misc.lazy_attribute import lazy_attribute 

from sage.categories.category import Category, JoinCategory 

from sage.categories.category_singleton import Category_singleton 

from sage.categories.category_with_axiom import CategoryWithAxiom 

from sage.categories.covariant_functorial_construction import FunctorialConstructionCategory 

 

import sage.categories.category_with_axiom 

 

class HomsetsCategory(FunctorialConstructionCategory): 

 

_functor_category = "Homsets" 

 

@classmethod 

def default_super_categories(cls, category): 

""" 

Return the default super categories of ``category.Homsets()``. 

 

INPUT: 

 

- ``cls`` -- the category class for the functor `F` 

- ``category`` -- a category `Cat` 

 

OUTPUT: a category 

 

As for the other functorial constructions, if ``category`` 

implements a nested ``Homsets`` class, this method is used in 

combination with ``category.Homsets().extra_super_categories()`` 

to compute the super categories of ``category.Homsets()``. 

 

EXAMPLES: 

 

If ``category`` has one or more full super categories, then 

the join of their respective homsets category is returned. In 

this example, this join consists of a single category:: 

 

sage: from sage.categories.homsets import HomsetsCategory 

sage: from sage.categories.additive_groups import AdditiveGroups 

 

sage: C = AdditiveGroups() 

sage: C.full_super_categories() 

[Category of additive inverse additive unital additive magmas, 

Category of additive monoids] 

sage: H = HomsetsCategory.default_super_categories(C); H 

Category of homsets of additive monoids 

sage: type(H) 

<class 'sage.categories.additive_monoids.AdditiveMonoids.Homsets_with_category'> 

 

and, given that nothing specific is currently implemented for 

homsets of additive groups, ``H`` is directly the category 

thereof:: 

 

sage: C.Homsets() 

Category of homsets of additive monoids 

 

Similarly for rings: a ring homset is just a homset of unital 

magmas and additive magmas:: 

 

sage: Rings().Homsets() 

Category of homsets of unital magmas and additive unital additive magmas 

 

Otherwise, if ``category`` implements a nested class 

``Homsets``, this method returns the category of all homsets:: 

 

sage: AdditiveMagmas.Homsets 

<class 'sage.categories.additive_magmas.AdditiveMagmas.Homsets'> 

sage: HomsetsCategory.default_super_categories(AdditiveMagmas()) 

Category of homsets 

 

which gives one of the super categories of 

``category.Homsets()``:: 

 

sage: AdditiveMagmas().Homsets().super_categories() 

[Category of additive magmas, Category of homsets] 

sage: AdditiveMagmas().AdditiveUnital().Homsets().super_categories() 

[Category of additive unital additive magmas, Category of homsets] 

 

the other coming from ``category.Homsets().extra_super_categories()``:: 

 

sage: AdditiveMagmas().Homsets().extra_super_categories() 

[Category of additive magmas] 

 

Finally, as a last resort, this method returns a stub category 

modelling the homsets of this category:: 

 

sage: hasattr(Posets, "Homsets") 

False 

sage: H = HomsetsCategory.default_super_categories(Posets()); H 

Category of homsets of posets 

sage: type(H) 

<class 'sage.categories.homsets.HomsetsOf_with_category'> 

sage: Posets().Homsets() 

Category of homsets of posets 

 

TESTS:: 

 

sage: Objects().Homsets().super_categories() 

[Category of homsets] 

sage: Sets().Homsets().super_categories() 

[Category of homsets] 

sage: (Magmas() & Posets()).Homsets().super_categories() 

[Category of homsets] 

""" 

if category.full_super_categories(): 

return Category.join([getattr(cat, cls._functor_category)() 

for cat in category.full_super_categories()]) 

else: 

functor_category = getattr(category.__class__, cls._functor_category) 

if isinstance(functor_category, type) and issubclass(functor_category, Category): 

return Homsets() 

else: 

return HomsetsOf(Category.join(category.structure())) 

 

def _test_homsets_category(self, **options): 

r""" 

Run generic tests on this homsets category 

 

.. SEEALSO:: :class:`TestSuite`. 

 

EXAMPLES:: 

 

sage: Sets().Homsets()._test_homsets_category() 

""" 

# TODO: remove if unneeded 

#from sage.categories.objects import Objects 

#from sage.categories.sets_cat import Sets 

tester = self._tester(**options) 

tester.assertTrue(self.is_subcategory(Category.join(self.base_category().structure()).Homsets())) 

tester.assertTrue(self.is_subcategory(Homsets())) 

 

@cached_method 

def base(self): 

""" 

If this homsets category is subcategory of a category with a base, return that base. 

 

.. TODO:: Is this really useful? 

 

EXAMPLES:: 

 

sage: ModulesWithBasis(ZZ).Homsets().base() 

Integer Ring 

 

""" 

from sage.categories.category_types import Category_over_base 

for C in self._all_super_categories_proper: 

if isinstance(C,Category_over_base): 

return C.base() 

raise AttributeError("This hom category has no base") 

 

class HomsetsOf(HomsetsCategory): 

""" 

Default class for homsets of a category. 

 

This is used when a category `C` defines some additional structure 

but not a homset category of its own. Indeed, unlike for covariant 

functorial constructions, we cannot represent the homset category 

of `C` by just the join of the homset categories of its super 

categories. 

 

EXAMPLES:: 

 

sage: C = (Magmas() & Posets()).Homsets(); C 

Category of homsets of magmas and posets 

sage: type(C) 

<class 'sage.categories.homsets.HomsetsOf_with_category'> 

 

TESTS:: 

 

sage: TestSuite(C).run() 

sage: C = Rings().Homsets() 

sage: TestSuite(C).run(skip=['_test_category_graph']) 

sage: TestSuite(C).run() 

""" 

_base_category_class = (Category,) 

 

def _repr_object_names(self): 

""" 

EXAMPLES:: 

 

sage: Semigroups().Homsets() 

Category of homsets of magmas 

sage: (Magmas() & AdditiveMagmas() & Posets()).Homsets() 

Category of homsets of magmas and additive magmas and posets 

sage: Rings().Homsets() 

Category of homsets of unital magmas and additive unital additive magmas 

""" 

base_category = self.base_category() 

try: 

object_names = base_category._repr_object_names() 

except ValueError: 

assert isinstance(base_category, JoinCategory) 

object_names = ' and '.join(cat._repr_object_names() for cat in base_category.super_categories()) 

return "homsets of %s"%(object_names) 

 

def super_categories(self): 

r""" 

Return the super categories of ``self``. 

 

A stub homset category admits a single super category, namely 

the category of all homsets. 

 

EXAMPLES:: 

 

sage: C = (Magmas() & Posets()).Homsets(); C 

Category of homsets of magmas and posets 

sage: type(C) 

<class 'sage.categories.homsets.HomsetsOf_with_category'> 

sage: C.super_categories() 

[Category of homsets] 

""" 

return [Homsets()] 

 

class Homsets(Category_singleton): 

""" 

The category of all homsets. 

 

EXAMPLES:: 

 

sage: from sage.categories.homsets import Homsets 

sage: Homsets() 

Category of homsets 

 

This is a subcategory of ``Sets()``:: 

 

sage: Homsets().super_categories() 

[Category of sets] 

 

By this, we assume that all homsets implemented in Sage are sets, 

or equivalently that we only implement locally small categories. 

See :wikipedia:`Category_(mathematics)`. 

 

:trac:`17364`: every homset category shall be a subcategory of the 

category of all homsets:: 

 

sage: Schemes().Homsets().is_subcategory(Homsets()) 

True 

sage: AdditiveMagmas().Homsets().is_subcategory(Homsets()) 

True 

sage: AdditiveMagmas().AdditiveUnital().Homsets().is_subcategory(Homsets()) 

True 

 

This is tested in :meth:`HomsetsCategory._test_homsets_category`. 

""" 

def super_categories(self): 

""" 

Return the super categories of ``self``. 

 

EXAMPLES:: 

 

sage: from sage.categories.homsets import Homsets 

sage: Homsets() 

Category of homsets 

""" 

from .sets_cat import Sets 

return [Sets()] 

 

class SubcategoryMethods: 

 

def Endset(self): 

""" 

Return the subcategory of the homsets of ``self`` that are endomorphism sets. 

 

EXAMPLES:: 

 

sage: Sets().Homsets().Endset() 

Category of endsets of sets 

 

sage: Posets().Homsets().Endset() 

Category of endsets of posets 

""" 

return self._with_axiom("Endset") 

 

class Endset(CategoryWithAxiom): 

""" 

The category of all endomorphism sets. 

 

This category serves too purposes: making sure that the 

``Endset`` axiom is implemented in the category where it's 

defined, namely ``Homsets``, and specifying that ``Endsets`` 

are monoids. 

 

EXAMPLES:: 

 

sage: from sage.categories.homsets import Homsets 

sage: Homsets().Endset() 

Category of endsets 

""" 

def extra_super_categories(self): 

""" 

Implement the fact that endsets are monoids. 

 

.. SEEALSO:: :meth:`CategoryWithAxiom.extra_super_categories` 

 

EXAMPLES:: 

 

sage: from sage.categories.homsets import Homsets 

sage: Homsets().Endset().extra_super_categories() 

[Category of monoids] 

""" 

from .monoids import Monoids 

return [Monoids()] 

 

class ParentMethods: 

def is_endomorphism_set(self): 

""" 

Return ``True`` as ``self`` is in the category 

of ``Endsets``. 

 

EXAMPLES:: 

 

sage: P.<t> = ZZ[] 

sage: E = End(P) 

sage: E.is_endomorphism_set() 

True 

""" 

return True 

 

class ParentMethods: 

def is_endomorphism_set(self): 

""" 

Return ``True`` if the domain and codomain of ``self`` are the same 

object. 

 

EXAMPLES:: 

 

sage: P.<t> = ZZ[] 

sage: f = P.hom([1/2*t]) 

sage: f.parent().is_endomorphism_set() 

False 

sage: g = P.hom([2*t]) 

sage: g.parent().is_endomorphism_set() 

True 

""" 

sD = self.domain() 

sC = self.codomain() 

if sC is None or sD is None: 

raise RuntimeError("Domain or codomain of this homset have been deallocated") 

return sD is sC