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

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

r""" 

Examples of semigroups 

""" 

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

# 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.structure.parent import Parent 

from sage.structure.unique_representation import UniqueRepresentation 

from sage.structure.element_wrapper import ElementWrapper 

from sage.categories.all import Semigroups 

from sage.sets.family import Family 

 

class LeftZeroSemigroup(UniqueRepresentation, Parent): 

r""" 

An example of a semigroup. 

 

This class illustrates a minimal implementation of a semigroup. 

 

EXAMPLES:: 

 

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

An example of a semigroup: the left zero semigroup 

 

This is the semigroup that contains all sorts of objects:: 

 

sage: S.some_elements() 

[3, 42, 'a', 3.4, 'raton laveur'] 

 

with product rule given by $a \times b = a$ for all $a, b$:: 

 

sage: S('hello') * S('world') 

'hello' 

sage: S(3)*S(1)*S(2) 

3 

sage: S(3)^12312321312321 

3 

 

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_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""" 

The left zero semigroup 

 

EXAMPLES:: 

 

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

An example of a semigroup: the left zero semigroup 

 

TESTS:: 

 

sage: TestSuite(S).run() 

 

""" 

Parent.__init__(self, category = Semigroups()) 

 

def _repr_(self): 

r""" 

 

EXAMPLES:: 

 

sage: Semigroups().example()._repr_() 

'An example of a semigroup: the left zero semigroup' 

 

""" 

return "An example of a semigroup: the left zero semigroup" 

 

def product(self, x, y): 

r""" 

Returns the product of ``x`` and ``y`` in the semigroup, as per 

:meth:`Semigroups.ParentMethods.product`. 

 

EXAMPLES:: 

 

sage: S = Semigroups().example() 

sage: S('hello') * S('world') 

'hello' 

sage: S(3)*S(1)*S(2) 

3 

 

""" 

assert x in self 

assert y in self 

return x 

 

def an_element(self): 

r""" 

Returns an element of the semigroup. 

 

EXAMPLES:: 

 

sage: Semigroups().example().an_element() 

42 

 

""" 

return self(42) 

 

def some_elements(self): 

r""" 

Returns a list of some elements of the semigroup. 

 

EXAMPLES:: 

 

sage: Semigroups().example().some_elements() 

[3, 42, 'a', 3.4, 'raton laveur'] 

 

""" 

return [self(i) for i in [3, 42, "a", 3.4, "raton laveur"]] 

 

class Element(ElementWrapper): 

def is_idempotent(self): 

r""" 

Trivial implementation of ``Semigroups.Element.is_idempotent`` 

since all elements of this semigroup are idempotent! 

 

EXAMPLES:: 

 

sage: S = Semigroups().example() 

sage: S.an_element().is_idempotent() 

True 

sage: S(17).is_idempotent() 

True 

 

""" 

return True 

 

 

class FreeSemigroup(UniqueRepresentation, Parent): 

r""" 

An example of semigroup. 

 

The purpose of this class is to provide a minimal template for 

implementing of a semigroup. 

 

EXAMPLES:: 

 

sage: S = Semigroups().example("free"); S 

An example of a semigroup: the free semigroup generated by ('a', 'b', 'c', 'd') 

 

This is the free semigroup generated by:: 

 

sage: S.semigroup_generators() 

Family ('a', 'b', 'c', 'd') 

 

and with product given by concatenation:: 

 

sage: S('dab') * S('acb') 

'dabacb' 

 

TESTS:: 

 

sage: TestSuite(S).run() 

""" 

def __init__(self, alphabet=('a','b','c','d')): 

r""" 

The free semigroup. 

 

INPUT: 

 

- ``alphabet`` -- a tuple of strings: the generators of the semigroup 

 

EXAMPLES:: 

 

sage: from sage.categories.examples.semigroups import FreeSemigroup 

sage: F = FreeSemigroup(('a','b','c')); F 

An example of a semigroup: the free semigroup generated by ('a', 'b', 'c') 

 

TESTS:: 

 

sage: F == loads(dumps(F)) 

True 

 

""" 

self.alphabet = alphabet 

Parent.__init__(self, category = Semigroups().FinitelyGenerated()) 

 

def _repr_(self): 

r""" 

EXAMPLES:: 

 

sage: from sage.categories.examples.semigroups import FreeSemigroup 

sage: FreeSemigroup(('a','b','c'))._repr_() 

"An example of a semigroup: the free semigroup generated by ('a', 'b', 'c')" 

 

""" 

return "An example of a semigroup: the free semigroup generated by %s"%(self.alphabet,) 

 

def product(self, x, y): 

r""" 

Returns the product of ``x`` and ``y`` in the semigroup, as per 

:meth:`Semigroups.ParentMethods.product`. 

 

EXAMPLES:: 

 

sage: F = Semigroups().example('free') 

sage: F.an_element() * F('a')^5 

'abcdaaaaa' 

 

""" 

assert x in self 

assert y in self 

return self(x.value + y.value) 

 

@cached_method 

def semigroup_generators(self): 

r""" 

Returns the generators of the semigroup. 

 

EXAMPLES:: 

 

sage: F = Semigroups().example('free') 

sage: F.semigroup_generators() 

Family ('a', 'b', 'c', 'd') 

 

""" 

return Family([self(i) for i in self.alphabet]) 

 

def an_element(self): 

r""" 

Returns an element of the semigroup. 

 

EXAMPLES:: 

 

sage: F = Semigroups().example('free') 

sage: F.an_element() 

'abcd' 

 

""" 

return self(''.join(self.alphabet)) 

 

def _element_constructor_(self, x): 

r""" 

Construct an element of this semigroup from the data ``x``. 

 

INPUT: 

 

- ``x`` -- a string 

 

EXAMPLES:: 

 

sage: F = Semigroups().example('free'); F 

An example of a semigroup: the free semigroup generated by ('a', 'b', 'c', 'd') 

sage: F._element_constructor_('a') 

'a' 

sage: F._element_constructor_('bad') 

'bad' 

 

TESTS:: 

 

sage: F._element_constructor_('z') 

Traceback (most recent call last): 

... 

assert a in self.alphabet 

AssertionError 

sage: bad = F._element_constructor_('bad'); bad 

'bad' 

sage: bad in F 

True 

 

sage: S = Semigroups().Subquotients().example() 

sage: type(S._element_constructor_(17)) 

<class 'sage.categories.examples.semigroups.QuotientOfLeftZeroSemigroup_with_category.element_class'> 

 

""" 

for a in x: 

assert a in self.alphabet 

return self.element_class(self, x) 

 

class Element(ElementWrapper): 

r""" 

The class for elements of the free semigroup. 

""" 

wrapped_class = str 

 

 

class QuotientOfLeftZeroSemigroup(UniqueRepresentation, Parent): 

r""" 

Example of a quotient semigroup 

 

EXAMPLES:: 

 

sage: S = Semigroups().Subquotients().example(); S 

An example of a (sub)quotient semigroup: a quotient of the left zero semigroup 

 

This is the quotient of:: 

 

sage: S.ambient() 

An example of a semigroup: the left zero semigroup 

 

obtained by setting `x=42` for any `x\geq 42`:: 

 

sage: S(100) 

42 

sage: S(100) == S(42) 

True 

 

The product is inherited from the ambient semigroup:: 

 

sage: S(1)*S(2) == S(1) 

True 

 

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_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 _element_constructor_(self, x): 

r""" 

Convert ``x`` into an element of this semigroup. 

 

EXAMPLES:: 

 

sage: S = Semigroups().Subquotients().example() 

sage: S._element_constructor_(17) 

17 

 

TESTS:: 

 

sage: S = Semigroups().Subquotients().example() 

sage: type(S._element_constructor_(17)) 

<class 'sage.categories.examples.semigroups.QuotientOfLeftZeroSemigroup_with_category.element_class'> 

 

""" 

return self.retract(self.ambient()(x)) 

 

def __init__(self, category = None): 

r""" 

This quotient of the left zero semigroup of integers obtained by 

setting `x=42` for any `x\geq 42`. 

 

EXAMPLES:: 

 

sage: S = Semigroups().Subquotients().example(); S 

An example of a (sub)quotient semigroup: a quotient of the left zero semigroup 

sage: S.ambient() 

An example of a semigroup: the left zero semigroup 

sage: S(100) 

42 

sage: S(100) == S(42) 

True 

sage: S(1)*S(2) == S(1) 

True 

 

TESTS:: 

 

sage: TestSuite(S).run() 

""" 

if category is None: 

category = Semigroups().Quotients() 

Parent.__init__(self, category = category) 

 

def _repr_(self): 

r""" 

 

EXAMPLES:: 

 

sage: Semigroups().Subquotients().example()._repr_() 

'An example of a (sub)quotient semigroup: a quotient of the left zero semigroup' 

 

""" 

return "An example of a (sub)quotient semigroup: a quotient of the left zero semigroup" 

 

def ambient(self): 

r""" 

Returns the ambient semigroup. 

 

EXAMPLES:: 

 

sage: S = Semigroups().Subquotients().example() 

sage: S.ambient() 

An example of a semigroup: the left zero semigroup 

 

""" 

return Semigroups().example() 

 

def lift(self, x): 

r""" 

Lift the element ``x`` into the ambient semigroup. 

 

INPUT: 

 

- ``x`` -- an element of ``self``. 

 

OUTPUT: 

 

- an element of ``self.ambient()``. 

 

EXAMPLES:: 

 

sage: S = Semigroups().Subquotients().example() 

sage: x = S.an_element(); x 

42 

sage: S.lift(x) 

42 

sage: S.lift(x) in S.ambient() 

True 

sage: y = S.ambient()(100); y 

100 

sage: S.lift(S(y)) 

42 

 

""" 

assert x in self 

return x.value 

 

def the_answer(self): 

r""" 

Returns the Answer to Life, the Universe, and Everything as an 

element of this semigroup. 

 

EXAMPLES:: 

 

sage: S = Semigroups().Subquotients().example() 

sage: S.the_answer() 

42 

 

""" 

return self.retract(self.ambient()(42)) 

 

def an_element(self): 

r""" 

Returns an element of the semigroup. 

 

EXAMPLES:: 

 

sage: S = Semigroups().Subquotients().example() 

sage: S.an_element() 

42 

 

""" 

return self.the_answer() 

 

def some_elements(self): 

r""" 

Returns a list of some elements of the semigroup. 

 

EXAMPLES:: 

 

sage: S = Semigroups().Subquotients().example() 

sage: S.some_elements() 

[1, 2, 3, 8, 42, 42] 

 

""" 

return [self.retract(self.ambient()(i)) 

for i in [1, 2, 3, 8, 42, 100]] 

 

def retract(self, x): 

r""" 

Returns the retract ``x`` onto an element of this semigroup. 

 

INPUT: 

 

- ``x`` -- an element of the ambient semigroup (``self.ambient()``). 

 

OUTPUT: 

 

- an element of ``self``. 

 

EXAMPLES:: 

 

sage: S = Semigroups().Subquotients().example() 

sage: L = S.ambient() 

sage: S.retract(L(17)) 

17 

sage: S.retract(L(42)) 

42 

sage: S.retract(L(171)) 

42 

 

TESTS:: 

 

sage: S.retract(L(171)) in S 

True 

 

""" 

from sage.rings.integer_ring import ZZ 

assert x in self.ambient() and x.value in ZZ 

if x.value > 42: 

return self.the_answer() 

else: 

return self.element_class(self, x) 

 

class Element(ElementWrapper): 

pass 

 

class IncompleteSubquotientSemigroup(UniqueRepresentation,Parent): 

def __init__(self, category = None): 

r""" 

An incompletely implemented subquotient semigroup, for testing purposes 

 

EXAMPLES:: 

 

sage: S = sage.categories.examples.semigroups.IncompleteSubquotientSemigroup() 

sage: S 

A subquotient of An example of a semigroup: the left zero semigroup 

 

TESTS:: 

 

sage: S._test_not_implemented_methods() 

Traceback (most recent call last): 

... 

AssertionError: Not implemented method: lift 

 

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

running ._test_an_element() . . . pass 

running ._test_associativity() . . . fail 

Traceback (most recent call last): 

... 

NotImplementedError: <abstract method retract at ...> 

------------------------------------------------------------ 

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

running ._test_new() . . . pass 

running ._test_not_implemented_methods() . . . fail 

Traceback (most recent call last): 

... 

AssertionError: Not implemented method: lift 

------------------------------------------------------------ 

running ._test_pickling() . . . pass 

running ._test_some_elements() . . . pass 

The following tests failed: _test_associativity, _test_not_implemented_methods 

""" 

Parent.__init__(self, category=Semigroups().Subquotients().or_subcategory(category)) 

 

def ambient(self): 

r""" 

Returns the ambient semigroup. 

 

EXAMPLES:: 

 

sage: S = Semigroups().Subquotients().example() 

sage: S.ambient() 

An example of a semigroup: the left zero semigroup 

 

""" 

return Semigroups().example() 

 

class Element(ElementWrapper): 

pass