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

600

601

602

603

604

605

606

607

608

609

610

r""" 

Monoids 

""" 

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

# Copyright (C) 2005 David Kohel <kohel@maths.usyd.edu> 

# William Stein <wstein@math.ucsd.edu> 

# 2008 Teresa Gomez-Diaz (CNRS) <Teresa.Gomez-Diaz@univ-mlv.fr> 

# 2008-2009 Florent Hivert <florent.hivert at univ-rouen.fr> 

# 2008-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 six.moves import range 

 

from sage.misc.cachefunc import cached_method 

from sage.misc.misc_c import prod 

from sage.categories.category_with_axiom import CategoryWithAxiom 

from sage.categories.semigroups import Semigroups 

from sage.misc.lazy_import import LazyImport 

from sage.categories.subquotients import SubquotientsCategory 

from sage.categories.cartesian_product import CartesianProductsCategory 

from sage.categories.algebra_functor import AlgebrasCategory 

from sage.categories.with_realizations import WithRealizationsCategory 

from sage.categories.finite_enumerated_sets import FiniteEnumeratedSets 

from sage.arith.power import generic_power 

 

 

class Monoids(CategoryWithAxiom): 

r""" 

The category of (multiplicative) monoids. 

 

A *monoid* is a unital :class:`semigroup <Semigroups>`, that is a 

set endowed with a multiplicative binary operation `*` which is 

associative and admits a unit (see :wikipedia:`Monoid`). 

 

EXAMPLES:: 

 

sage: Monoids() 

Category of monoids 

sage: Monoids().super_categories() 

[Category of semigroups, Category of unital magmas] 

sage: Monoids().all_super_categories() 

[Category of monoids, 

Category of semigroups, 

Category of unital magmas, Category of magmas, 

Category of sets, 

Category of sets with partial maps, 

Category of objects] 

 

sage: Monoids().axioms() 

frozenset({'Associative', 'Unital'}) 

sage: Semigroups().Unital() 

Category of monoids 

 

sage: Monoids().example() 

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

 

TESTS:: 

 

sage: C = Monoids() 

sage: TestSuite(C).run() 

 

:: 

 

sage: S = Monoids().example() 

sage: x = S("aa") 

sage: x^0, x^1, x^2, x^3, x^4, x^5 

('', 'aa', 'aaaa', 'aaaaaa', 'aaaaaaaa', 'aaaaaaaaaa') 

""" 

 

_base_category_class_and_axiom = (Semigroups, "Unital") 

 

Finite = LazyImport('sage.categories.finite_monoids', 'FiniteMonoids', at_startup=True) 

Inverse = LazyImport('sage.categories.groups', 'Groups', at_startup=True) 

 

@staticmethod 

def free(index_set=None, names=None, **kwds): 

r""" 

Return a free monoid on `n` generators or with the generators 

indexed by a set `I`. 

 

A free monoid is constructed by specifing either: 

 

- the number of generators and/or the names of the generators 

- the indexing set for the generators 

 

INPUT: 

 

- ``index_set`` -- (optional) an index set for the generators; if 

an integer, then this represents `\{0, 1, \ldots, n-1\}` 

 

- ``names`` -- a string or list/tuple/iterable of strings 

(default: ``'x'``); the generator names or name prefix 

 

EXAMPLES:: 

 

sage: Monoids.free(index_set=ZZ) 

Free monoid indexed by Integer Ring 

sage: Monoids().free(ZZ) 

Free monoid indexed by Integer Ring 

sage: F.<x,y,z> = Monoids().free(); F 

Free monoid indexed by {'x', 'y', 'z'} 

""" 

if names is not None: 

if isinstance(names, str): 

from sage.rings.all import ZZ 

if ',' not in names and index_set in ZZ: 

names = [names + repr(i) for i in range(index_set)] 

else: 

names = names.split(',') 

names = tuple(names) 

if index_set is None: 

index_set = names 

 

from sage.monoids.indexed_free_monoid import IndexedFreeMonoid 

return IndexedFreeMonoid(index_set, names=names, **kwds) 

 

class ParentMethods: 

 

def one_element(self): 

r""" 

Backward compatibility alias for :meth:`one`. 

 

TESTS:: 

 

sage: S = Monoids().example() 

sage: S.one_element() 

doctest:...: DeprecationWarning: .one_element() is deprecated. Please use .one() instead. 

See http://trac.sagemath.org/17694 for details. 

'' 

""" 

from sage.misc.superseded import deprecation 

deprecation(17694, ".one_element() is deprecated. Please use .one() instead.") 

return self.one() 

 

def semigroup_generators(self): 

""" 

Return the generators of ``self`` as a semigroup. 

 

The generators of a monoid `M` as a semigroup are the generators 

of `M` as a monoid and the unit. 

 

EXAMPLES:: 

 

sage: M = Monoids().free([1,2,3]) 

sage: M.semigroup_generators() 

Family (1, F[1], F[2], F[3]) 

""" 

G = self.monoid_generators() 

from sage.categories.finite_enumerated_sets import FiniteEnumeratedSets 

if G not in FiniteEnumeratedSets(): 

raise NotImplementedError("currently only implemented for finitely generated monoids") 

from sage.sets.family import Family 

return Family((self.one(),) + tuple(G)) 

 

def prod(self, args): 

r""" 

n-ary product of elements of ``self``. 

 

INPUT: 

 

- ``args`` -- a list (or iterable) of elements of ``self`` 

 

Returns the product of the elements in ``args``, as an element of 

``self``. 

 

EXAMPLES:: 

 

sage: S = Monoids().example() 

sage: S.prod([S('a'), S('b')]) 

'ab' 

""" 

return prod(args, self.one()) 

 

def _test_prod(self, **options): 

r""" 

Run basic tests for the product method :meth:`prod` of ``self``. 

 

See the documentation for :class:`TestSuite` for information on 

further options. 

 

INPUT: 

 

- ``options`` -- any keyword arguments accepted by :meth:`_tester` 

 

EXAMPLES: 

 

By default, this method tests only the elements returned by 

``self.some_elements()``:: 

 

sage: S = Monoids().example() 

sage: S._test_prod() 

 

However, the elements tested can be customized with the 

``elements`` keyword argument:: 

 

sage: S._test_prod(elements = (S('a'), S('b'))) 

""" 

tester = self._tester(**options) 

tester.assertTrue(self.prod([]) == self.one()) 

for x in tester.some_elements(): 

tester.assertTrue(self.prod([x]) == x) 

tester.assertTrue(self.prod([x, x]) == x**2) 

tester.assertTrue(self.prod([x, x, x]) == x**3) 

 

 

def submonoid(self, generators, category=None): 

r""" 

Return the multiplicative submonoid generated by ``generators``. 

 

INPUT: 

 

- ``generators`` -- a finite family of elements of 

``self``, or a list, iterable, ... that can be converted 

into one (see :class:`Family`). 

 

- ``category`` -- a category 

 

This is a shorthand for 

:meth:`Semigroups.ParentMethods.subsemigroup` that 

specifies that this is a submonoid, and in particular that 

the unit is ``self.one()``. 

 

EXAMPLES:: 

 

sage: R = IntegerModRing(15) 

sage: M = R.submonoid([R(3),R(5)]); M 

A submonoid of (Ring of integers modulo 15) with 2 generators 

sage: M.list() 

[1, 3, 5, 9, 0, 10, 12, 6] 

 

Not the presence of the unit, unlike in:: 

 

sage: S = R.subsemigroup([R(3),R(5)]); S 

A subsemigroup of (Ring of integers modulo 15) with 2 generators 

sage: S.list() 

[3, 5, 9, 0, 10, 12, 6] 

 

This method is really a shorthand for subsemigroup:: 

 

sage: M2 = R.subsemigroup([R(3),R(5)], one=R.one()) 

sage: M2 is M 

True 

 

 

""" 

return self.subsemigroup(generators, one=self.one()) 

 

class ElementMethods: 

 

def is_one(self): 

r""" 

Return whether ``self`` is the one of the monoid. 

 

The default implementation is to compare with ``self.one()``. 

 

TESTS:: 

 

sage: S = Monoids().example() 

sage: S.one().is_one() 

True 

sage: S("aa").is_one() 

False 

""" 

return self == self.parent().one() 

 

def _pow_int(self, n): 

r""" 

Return ``self`` to the `n^{th}` power. 

 

INPUT: 

 

- ``n`` -- a nonnegative integer 

 

EXAMPLES:: 

 

sage: S = Monoids().example() 

sage: S("a") ^ 5 

'aaaaa' 

""" 

return generic_power(self, n) 

 

def _pow_naive(self, n): 

r""" 

Return ``self`` to the `n^{th}` power (naive implementation). 

 

INPUT: 

 

- ``n`` -- a nonnegative integer 

 

This naive implementation does not use binary 

exponentiation; there are cases where this is actually 

faster due to size explosion. 

 

EXAMPLES:: 

 

sage: S = Monoids().example() 

sage: x = S("aa") 

sage: [x._pow_naive(i) for i in range(6)] 

['', 'aa', 'aaaa', 'aaaaaa', 'aaaaaaaa', 'aaaaaaaaaa'] 

""" 

if not n: 

return self.parent().one() 

result = self 

for i in range(n - 1): 

result *= self 

return result 

 

def powers(self, n): 

r""" 

Return the list `[x^0, x^1, \ldots, x^{n-1}]`. 

 

EXAMPLES:: 

 

sage: A = Matrix([[1, 1], [-1, 0]]) 

sage: A.powers(6) 

[ 

[1 0] [ 1 1] [ 0 1] [-1 0] [-1 -1] [ 0 -1] 

[0 1], [-1 0], [-1 -1], [ 0 -1], [ 1 0], [ 1 1] 

] 

""" 

if n < 0: 

raise ValueError("negative number of powers requested") 

elif n == 0: 

return [] 

x = self.parent().one() 

l = [x] 

for i in range(n - 1): 

x = x * self 

l.append(x) 

return l 

 

class Commutative(CategoryWithAxiom): 

""" 

Category of commutative (abelian) monoids. 

 

A monoid `M` is *commutative* if `xy = yx` for all `x,y \in M`. 

""" 

@staticmethod 

def free(index_set=None, names=None, **kwds): 

r""" 

Return a free abelian monoid on `n` generators or with 

the generators indexed by a set `I`. 

 

A free monoid is constructed by specifing either: 

 

- the number of generators and/or the names of the generators, or 

- the indexing set for the generators. 

 

INPUT: 

 

- ``index_set`` -- (optional) an index set for the generators; if 

an integer, then this represents `\{0, 1, \ldots, n-1\}` 

 

- ``names`` -- a string or list/tuple/iterable of strings 

(default: ``'x'``); the generator names or name prefix 

 

EXAMPLES:: 

 

sage: Monoids.Commutative.free(index_set=ZZ) 

Free abelian monoid indexed by Integer Ring 

sage: Monoids().Commutative().free(ZZ) 

Free abelian monoid indexed by Integer Ring 

sage: F.<x,y,z> = Monoids().Commutative().free(); F 

Free abelian monoid indexed by {'x', 'y', 'z'} 

""" 

if names is not None: 

if isinstance(names, str): 

from sage.rings.all import ZZ 

if ',' not in names and index_set in ZZ: 

names = [names + repr(i) for i in range(index_set)] 

else: 

names = names.split(',') 

names = tuple(names) 

if index_set is None: 

index_set = names 

 

from sage.monoids.indexed_free_monoid import IndexedFreeAbelianMonoid 

return IndexedFreeAbelianMonoid(index_set, names=names, **kwds) 

 

class WithRealizations(WithRealizationsCategory): 

 

class ParentMethods: 

 

def one(self): 

r""" 

Return the unit of this monoid. 

 

This default implementation returns the unit of the 

realization of ``self`` given by 

:meth:`~Sets.WithRealizations.ParentMethods.a_realization`. 

 

EXAMPLES:: 

 

sage: A = Sets().WithRealizations().example(); A 

The subset algebra of {1, 2, 3} over Rational Field 

sage: A.one.__module__ 

'sage.categories.monoids' 

sage: A.one() 

F[{}] 

 

TESTS:: 

 

sage: A.one() is A.a_realization().one() 

True 

sage: A._test_one() 

""" 

return self.a_realization().one() 

 

class Subquotients(SubquotientsCategory): 

 

class ParentMethods: 

 

def one(self): 

""" 

Returns the multiplicative unit of this monoid, 

obtained by retracting that of the ambient monoid. 

 

EXAMPLES:: 

 

sage: S = Monoids().Subquotients().example() # todo: not implemented 

sage: S.one() # todo: not implemented 

""" 

return self.retract(self.ambient().one()) 

 

class Algebras(AlgebrasCategory): 

 

def extra_super_categories(self): 

""" 

EXAMPLES:: 

 

sage: Monoids().Algebras(QQ).extra_super_categories() 

[Category of monoids] 

sage: Monoids().Algebras(QQ).super_categories() 

[Category of algebras with basis over Rational Field, 

Category of semigroup algebras over Rational Field, 

Category of unital magma algebras over Rational Field] 

""" 

return [Monoids()] 

 

class ParentMethods: 

 

@cached_method 

def one_basis(self): 

""" 

Return the unit of the monoid, which indexes the unit of 

this algebra, as per 

:meth:`AlgebrasWithBasis.ParentMethods.one_basis() 

<sage.categories.algebras_with_basis.AlgebrasWithBasis.ParentMethods.one_basis>`. 

 

EXAMPLES:: 

 

sage: A = Monoids().example().algebra(ZZ) 

sage: A.one_basis() 

'' 

sage: A.one() 

B[''] 

sage: A(3) 

3*B[''] 

""" 

return self.basis().keys().one() 

 

@cached_method 

def algebra_generators(self): 

r""" 

Return generators for this algebra. 

 

For a monoid algebra, the algebra generators are built 

from the monoid generators if available and from the 

semigroup generators otherwise. 

 

.. SEEALSO:: 

 

- :meth:`Semigroups.Algebras.ParentMethods.algebra_generators` 

- :meth:`MagmaticAlgebras.ParentMethods.algebra_generators() 

<.magmatic_algebras.MagmaticAlgebras.ParentMethods.algebra_generators>`. 

 

EXAMPLES:: 

 

sage: M = Monoids().example(); M 

An example of a monoid: 

the free monoid generated by ('a', 'b', 'c', 'd') 

sage: M.monoid_generators() 

Finite family {'a': 'a', 'c': 'c', 'b': 'b', 'd': 'd'} 

sage: M.algebra(ZZ).algebra_generators() 

Finite family {'a': B['a'], 'c': B['c'], 'b': B['b'], 'd': B['d']} 

 

sage: Z12 = Monoids().Finite().example(); Z12 

An example of a finite multiplicative monoid: 

the integers modulo 12 

sage: Z12.monoid_generators() 

Traceback (most recent call last): 

... 

AttributeError: 'IntegerModMonoid_with_category' object 

has no attribute 'monoid_generators' 

sage: Z12.semigroup_generators() 

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

sage: Z12.algebra(QQ).algebra_generators() 

Finite family {0: B[0], 1: B[1], 2: B[2], 3: B[3], 4: B[4], 5: B[5], 

6: B[6], 7: B[7], 8: B[8], 9: B[9], 10: B[10], 11: B[11]} 

 

 

sage: GroupAlgebras(QQ).example(AlternatingGroup(10)).algebra_generators() 

Finite family {0: (8,9,10), 1: (1,2,3,4,5,6,7,8,9)} 

 

sage: A = DihedralGroup(3).algebra(QQ); A 

Algebra of Dihedral group of order 6 as a permutation group 

over Rational Field 

sage: A.algebra_generators() 

Finite family {0: (1,2,3), 1: (1,3)} 

""" 

monoid = self.basis().keys() 

try: 

generators = monoid.monoid_generators() 

except AttributeError: 

generators = monoid.semigroup_generators() 

return generators.map(self.monomial) 

 

class ElementMethods: 

 

def is_central(self): 

r""" 

Return whether the element ``self`` is central. 

 

EXAMPLES:: 

 

sage: SG4=SymmetricGroupAlgebra(ZZ,4) 

sage: SG4(1).is_central() 

True 

sage: SG4(Permutation([1,3,2,4])).is_central() 

False 

sage: A=GroupAlgebras(QQ).example(); A 

Algebra of Dihedral group of order 8 as a permutation group over Rational Field 

sage: sum(i for i in A.basis()).is_central() 

True 

""" 

return all([i*self == self*i for i in self.parent().algebra_generators()]) 

 

class CartesianProducts(CartesianProductsCategory): 

""" 

The category of monoids constructed as Cartesian products of monoids. 

 

This construction gives the direct product of monoids. See 

:wikipedia:`Direct_product` for more information. 

""" 

def extra_super_categories(self): 

""" 

A Cartesian product of monoids is endowed with a natural 

group structure. 

 

EXAMPLES:: 

 

sage: C = Monoids().CartesianProducts() 

sage: C.extra_super_categories() 

[Category of monoids] 

sage: sorted(C.super_categories(), key=str) 

[Category of Cartesian products of semigroups, 

Category of Cartesian products of unital magmas, 

Category of monoids] 

""" 

return [self.base_category()] 

 

class ParentMethods: 

@cached_method 

def monoid_generators(self): 

""" 

Return the generators of ``self``. 

 

EXAMPLES:: 

 

sage: M = Monoids.free([1,2,3]) 

sage: N = Monoids.free(['a','b']) 

sage: C = cartesian_product([M, N]) 

sage: C.monoid_generators() 

Family ((F[1], 1), (F[2], 1), (F[3], 1), 

(1, F['a']), (1, F['b'])) 

 

An example with an infinitely generated group (a better output 

is needed):: 

 

sage: N = Monoids.free(ZZ) 

sage: C = cartesian_product([M, N]) 

sage: C.monoid_generators() 

Lazy family (gen(i))_{i in The Cartesian product of (...)} 

""" 

F = self.cartesian_factors() 

ids = tuple(M.one() for M in F) 

def lift(i, gen): 

cur = list(ids) 

cur[i] = gen 

return self._cartesian_product_of_elements(cur) 

from sage.sets.family import Family 

 

# Finitely generated 

cat = FiniteEnumeratedSets() 

if all(M.monoid_generators() in cat 

or isinstance(M.monoid_generators(), (tuple, list)) for M in F): 

ret = [lift(i, gen) for i,M in enumerate(F) for gen in M.monoid_generators()] 

return Family(ret) 

 

# Infinitely generated 

# This does not return a good output, but it is "correct" 

# TODO: Figure out a better way to do things 

from sage.categories.cartesian_product import cartesian_product 

gens_prod = cartesian_product([Family(M.monoid_generators(), 

lambda g: (i, g)) 

for i,M in enumerate(F)]) 

return Family(gens_prod, lift, name="gen")