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

r""" 

Examples of parents endowed with multiple realizations 

""" 

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

# 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.misc.bindable_class import BindableClass 

from sage.categories.all import Rings, Algebras, AlgebrasWithBasis 

from sage.categories.realizations import Category_realization_of_parent 

from sage.structure.unique_representation import UniqueRepresentation 

from sage.structure.parent import Parent 

from sage.sets.set import Set 

from sage.combinat.free_module import CombinatorialFreeModule 

from sage.combinat.subset import Subsets 

 

class SubsetAlgebra(UniqueRepresentation, Parent): 

r""" 

An example of parent endowed with several realizations 

 

We consider an algebra `A(S)` whose bases are indexed by the 

subsets `s` of a given set `S`. We consider three natural basis of 

this algebra: ``F``, ``In``, and ``Out``. In the first basis, the 

product is given by the union of the indexing sets. That is, for any 

`s, t\subset S` 

 

.. MATH:: 

 

F_s F_t = F_{s\cup t} 

 

The ``In`` basis and ``Out`` basis are defined respectively by: 

 

.. MATH:: 

 

In_s = \sum_{t\subset s} F_t 

\qquad\text{and}\qquad 

F_s = \sum_{t\supset s} Out_t 

 

Each such basis gives a realization of `A`, where the elements are 

represented by their expansion in this basis. 

 

This parent, and its code, demonstrate how to implement this 

algebra and its three realizations, with coercions and mixed 

arithmetic between them. 

 

.. SEEALSO:: 

 

- :func:`Sets().WithRealizations <sage.categories.with_realizations.WithRealizations>` 

- the `Implementing Algebraic Structures 

<../../../../../thematic_tutorials/tutorial-implementing-algebraic-structures>`_ 

thematic tutorial. 

 

EXAMPLES:: 

 

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

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

sage: A.base_ring() 

Rational Field 

 

The three bases of ``A``:: 

 

sage: F = A.F() ; F 

The subset algebra of {1, 2, 3} over Rational Field in the Fundamental basis 

sage: In = A.In() ; In 

The subset algebra of {1, 2, 3} over Rational Field in the In basis 

sage: Out = A.Out(); Out 

The subset algebra of {1, 2, 3} over Rational Field in the Out basis 

 

One can quickly define all the bases using the following shortcut:: 

 

sage: A.inject_shorthands() 

Defining F as shorthand for The subset algebra of {1, 2, 3} over Rational Field in the Fundamental basis 

Defining In as shorthand for The subset algebra of {1, 2, 3} over Rational Field in the In basis 

Defining Out as shorthand for The subset algebra of {1, 2, 3} over Rational Field in the Out basis 

 

Accessing the basis elements is done with :meth:`basis()` method:: 

 

sage: F.basis().list() 

[F[{}], F[{1}], F[{2}], F[{3}], F[{1, 2}], F[{1, 3}], F[{2, 3}], F[{1, 2, 3}]] 

 

To access a particular basis element, you can use the :meth:`from_set` 

method:: 

 

sage: F.from_set(2,3) 

F[{2, 3}] 

sage: In.from_set(1,3) 

In[{1, 3}] 

 

or as a convenient shorthand, one can use the following notation:: 

 

sage: F[2,3] 

F[{2, 3}] 

sage: In[1,3] 

In[{1, 3}] 

 

Some conversions:: 

 

sage: F(In[2,3]) 

F[{}] + F[{2}] + F[{3}] + F[{2, 3}] 

sage: In(F[2,3]) 

In[{}] - In[{2}] - In[{3}] + In[{2, 3}] 

 

sage: Out(F[3]) 

Out[{3}] + Out[{1, 3}] + Out[{2, 3}] + Out[{1, 2, 3}] 

sage: F(Out[3]) 

F[{3}] - F[{1, 3}] - F[{2, 3}] + F[{1, 2, 3}] 

 

sage: Out(In[2,3]) 

Out[{}] + Out[{1}] + 2*Out[{2}] + 2*Out[{3}] + 2*Out[{1, 2}] + 2*Out[{1, 3}] + 4*Out[{2, 3}] + 4*Out[{1, 2, 3}] 

 

We can now mix expressions:: 

 

sage: (1 + Out[1]) * In[2,3] 

Out[{}] + 2*Out[{1}] + 2*Out[{2}] + 2*Out[{3}] + 2*Out[{1, 2}] + 2*Out[{1, 3}] + 4*Out[{2, 3}] + 4*Out[{1, 2, 3}] 

 

""" 

 

def __init__(self, R, S): 

r""" 

EXAMPLES:: 

 

sage: from sage.categories.examples.with_realizations import SubsetAlgebra 

sage: A = SubsetAlgebra(QQ, Set((1,2,3))); A 

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

sage: Sets().WithRealizations().example() is A 

True 

sage: TestSuite(A).run() 

 

TESTS:: 

 

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

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

sage: F, In, Out = A.realizations() 

sage: type(F.coerce_map_from(In)) 

<class 'sage.modules.with_basis.morphism.TriangularModuleMorphismByLinearity_with_category'> 

sage: type(In.coerce_map_from(F)) 

<class 'sage.modules.with_basis.morphism.TriangularModuleMorphismByLinearity_with_category'> 

sage: type(F.coerce_map_from(Out)) 

<class 'sage.modules.with_basis.morphism.TriangularModuleMorphismByLinearity_with_category'> 

sage: type(Out.coerce_map_from(F)) 

<class 'sage.modules.with_basis.morphism.TriangularModuleMorphismByLinearity_with_category'> 

sage: In.coerce_map_from(Out) 

Composite map: 

From: The subset algebra of {1, 2, 3} over Rational Field in the Out basis 

To: The subset algebra of {1, 2, 3} over Rational Field in the In basis 

Defn: Generic morphism: 

From: The subset algebra of {1, 2, 3} over Rational Field in the Out basis 

To: The subset algebra of {1, 2, 3} over Rational Field in the Fundamental basis 

then 

Generic morphism: 

From: The subset algebra of {1, 2, 3} over Rational Field in the Fundamental basis 

To: The subset algebra of {1, 2, 3} over Rational Field in the In basis 

sage: Out.coerce_map_from(In) 

Composite map: 

From: The subset algebra of {1, 2, 3} over Rational Field in the In basis 

To: The subset algebra of {1, 2, 3} over Rational Field in the Out basis 

Defn: Generic morphism: 

From: The subset algebra of {1, 2, 3} over Rational Field in the In basis 

To: The subset algebra of {1, 2, 3} over Rational Field in the Fundamental basis 

then 

Generic morphism: 

From: The subset algebra of {1, 2, 3} over Rational Field in the Fundamental basis 

To: The subset algebra of {1, 2, 3} over Rational Field in the Out basis 

""" 

assert(R in Rings()) 

self._base = R # Won't be needed when CategoryObject won't override anymore base_ring 

self._S = S 

Parent.__init__(self, category = Algebras(R).Commutative().WithRealizations()) 

 

# Initializes the bases and change of bases of ``self`` 

 

F = self.F() 

In = self.In() 

Out = self.Out() 

 

category = self.Bases() 

key = lambda x: self.indices_key(x) 

In_to_F = In.module_morphism(F.sum_of_monomials * Subsets, 

codomain=F, category=category, 

triangular='upper', unitriangular=True, 

key=key) 

In_to_F .register_as_coercion() 

(~In_to_F).register_as_coercion() 

 

F_to_Out = F.module_morphism(Out.sum_of_monomials * self.supsets, 

codomain=Out, category=category, 

triangular='lower', unitriangular=True, 

key=key) 

F_to_Out .register_as_coercion() 

(~F_to_Out).register_as_coercion() 

 

_shorthands = ["F", "In", "Out"] 

 

def a_realization(self): 

r""" 

Returns the default realization of ``self`` 

 

EXAMPLES:: 

 

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

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

sage: A.a_realization() 

The subset algebra of {1, 2, 3} over Rational Field in the Fundamental basis 

""" 

return self.F() 

 

def base_set(self): 

r""" 

EXAMPLES:: 

 

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

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

sage: A.base_set() 

{1, 2, 3} 

""" 

return self._S 

 

def indices(self): 

r""" 

The objects that index the basis elements of this algebra. 

 

EXAMPLES:: 

 

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

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

sage: A.indices() 

Subsets of {1, 2, 3} 

""" 

return Subsets(self._S) 

 

def indices_key(self, x): 

r""" 

A key function on a set which gives a linear extension 

of the inclusion order. 

 

INPUT: 

 

- ``x`` -- set 

 

EXAMPLES:: 

 

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

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

sage: sorted(A.indices(), key=A.indices_key) 

[{}, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}] 

""" 

return (len(x), list(x)) 

 

def supsets(self, set): 

r""" 

Returns all the subsets of `S` containing ``set`` 

 

INPUT: 

 

- ``set`` -- a subset of the base set `S` of ``self`` 

 

EXAMPLES:: 

 

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

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

sage: A.supsets(Set((2,))) 

[{1, 2, 3}, {2, 3}, {1, 2}, {2}] 

""" 

S = self.base_set() 

return list(S.difference(s) for s in Subsets(S.difference(set))) 

 

def _repr_(self): 

r""" 

EXAMPLES:: 

 

sage: Sets().WithRealizations().example() # indirect doctest 

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

""" 

return "The subset algebra of %s over %s"%(self.base_set(), self.base_ring()) 

 

class Bases(Category_realization_of_parent): 

r""" 

The category of the realizations of the subset algebra 

""" 

 

def super_categories(self): 

r""" 

EXAMPLES:: 

 

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

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

sage: C = A.Bases(); C 

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

sage: C.super_categories() 

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

Join of Category of algebras with basis over Rational Field and 

Category of commutative algebras over Rational Field and 

Category of realizations of unital magmas] 

""" 

A = self.base() 

category = Algebras(A.base_ring()).Commutative() 

return [A.Realizations(), 

category.Realizations().WithBasis()] 

 

 

class ParentMethods: 

 

def from_set(self, *args): 

r""" 

Construct the monomial indexed by the set containing the 

elements passed as arguments. 

 

EXAMPLES:: 

 

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

The subset algebra of {1, 2, 3} over Rational Field in the In basis 

sage: In.from_set(2,3) 

In[{2, 3}] 

 

As a shorthand, one can construct elements using the following 

notation:: 

 

sage: In[2,3] 

In[{2, 3}] 

""" 

return self.monomial(Set(args)) 

 

def __getitem__(self, s): 

r""" 

This method implements a convenient shorthand for constructing 

basis elements of this algebra. 

 

EXAMPLES:: 

 

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

sage: In = A.In() 

sage: In[2,3] 

In[{2, 3}] 

sage: F = A.F() 

sage: F[1,3] 

F[{1, 3}] 

""" 

from sage.rings.integer import Integer 

if isinstance(s, Integer): 

return self.from_set(*(s,)) 

else: 

return self.from_set(*s) 

 

# This could go in the super category VectorSpaces().Realizations() 

def _repr_(self): 

r""" 

EXAMPLES:: 

 

sage: Sets().WithRealizations().example().In() # indirect doctest 

The subset algebra of {1, 2, 3} over Rational Field in the In basis 

""" 

return "%s in the %s basis" % (self.realization_of(), self._realization_name()) 

 

# Could this go in the super category Monoids().Realizations() ? 

@cached_method 

def one(self): 

r""" 

Returns the unit of this algebra. 

 

This default implementation takes the unit in the 

fundamental basis, and coerces it in ``self``. 

 

EXAMPLES:: 

 

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

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

sage: In = A.In(); Out = A.Out() 

sage: In.one() 

In[{}] 

sage: Out.one() 

Out[{}] + Out[{1}] + Out[{2}] + Out[{3}] + Out[{1, 2}] + Out[{1, 3}] + Out[{2, 3}] + Out[{1, 2, 3}] 

""" 

return self(self.realization_of().F().one()) 

 

class Fundamental(CombinatorialFreeModule, BindableClass): 

r""" 

The Subset algebra, in the fundamental basis 

 

INPUT: 

 

- ``A`` -- a parent with realization in :class:`SubsetAlgebra` 

 

EXAMPLES:: 

 

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

sage: A.F() 

The subset algebra of {1, 2, 3} over Rational Field in the Fundamental basis 

sage: A.Fundamental() 

The subset algebra of {1, 2, 3} over Rational Field in the Fundamental basis 

""" 

 

def __init__(self, A): 

r""" 

EXAMPLES:: 

 

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

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

sage: F = A.F(); F 

The subset algebra of {1, 2, 3} over Rational Field in the Fundamental basis 

sage: TestSuite(F).run() 

""" 

CombinatorialFreeModule.__init__(self, 

A.base_ring(), A.indices(), 

category=A.Bases(), prefix='F', sorting_key=A.indices_key) 

 

def product_on_basis(self, left, right): 

r""" 

Product of basis elements, as per :meth:`AlgebrasWithBasis.ParentMethods.product_on_basis`. 

 

INPUT: 

 

- ``left``, ``right`` -- sets indexing basis elements 

 

EXAMPLES:: 

 

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

The subset algebra of {1, 2, 3} over Rational Field in the Fundamental basis 

sage: S = F.basis().keys(); S 

Subsets of {1, 2, 3} 

sage: F.product_on_basis(S([]), S([])) 

F[{}] 

sage: F.product_on_basis(S({1}), S({3})) 

F[{1, 3}] 

sage: F.product_on_basis(S({1,2}), S({2,3})) 

F[{1, 2, 3}] 

""" 

return self.monomial(left.union(right)) 

 

def one_basis(self): 

r""" 

Returns the index of the basis element which is equal to '1'. 

 

EXAMPLES:: 

 

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

The subset algebra of {1, 2, 3} over Rational Field in the Fundamental basis 

sage: F.one_basis() 

{} 

sage: F.one() 

F[{}] 

""" 

return Set([]) 

 

# Bypass the definition in SubsetAlgebra.Bases.ParentMethods 

# by the usual one from basis; alternatively one could just 

# define one instead of one_basis. 

one = AlgebrasWithBasis.ParentMethods.one 

 

F = Fundamental 

 

class In(CombinatorialFreeModule, BindableClass): 

r""" 

The Subset Algebra, in the ``In`` basis 

 

INPUT: 

 

- ``A`` -- a parent with realization in :class:`SubsetAlgebra` 

 

EXAMPLES:: 

 

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

sage: A.In() 

The subset algebra of {1, 2, 3} over Rational Field in the In basis 

 

TESTS: 

 

The product in this basis is computed by converting to the fundamental 

basis, computing the product there, and then converting back:: 

 

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

The subset algebra of {1, 2, 3} over Rational Field in the In basis 

sage: x = In.an_element() 

sage: y = In.an_element() 

sage: In.product 

<bound method ....product_by_coercion ...> 

sage: In.product.__module__ 

'sage.categories.magmas' 

sage: In.product(x, y) 

-21*In[{}] - 2*In[{1}] + 19*In[{2}] + 53*In[{1, 2}] 

""" 

 

def __init__(self, A): 

r""" 

EXAMPLES:: 

 

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

The subset algebra of {1, 2, 3} over Rational Field in the In basis 

sage: TestSuite(In).run() 

""" 

CombinatorialFreeModule.__init__(self, 

A.base_ring(), A.indices(), 

category=A.Bases(), prefix='In', sorting_key=A.indices_key) 

 

class Out(CombinatorialFreeModule, BindableClass): 

r""" 

The Subset Algebra, in the `Out` basis 

 

INPUT: 

 

- ``A`` -- a parent with realization in :class:`SubsetAlgebra` 

 

EXAMPLES:: 

 

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

sage: A.Out() 

The subset algebra of {1, 2, 3} over Rational Field in the Out basis 

 

TESTS: 

 

The product in this basis is computed by converting to the fundamental 

basis, computing the product there, and then converting back:: 

 

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

The subset algebra of {1, 2, 3} over Rational Field in the Out basis 

sage: x = Out.an_element() 

sage: y = Out.an_element() 

sage: Out.product 

<bound method ....product_by_coercion ...> 

sage: Out.product.__module__ 

'sage.categories.magmas' 

sage: Out.product(x, y) 

Out[{}] + 4*Out[{1}] + 9*Out[{2}] + Out[{1, 2}] 

""" 

 

def __init__(self, A): 

r""" 

EXAMPLES:: 

 

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

The subset algebra of {1, 2, 3} over Rational Field in the Out basis 

sage: TestSuite(Out).run() 

""" 

CombinatorialFreeModule.__init__(self, 

A.base_ring(), A.indices(), 

category=A.Bases(), prefix='Out', sorting_key=A.indices_key)