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

""" 

Species structures 

 

We will illustrate the use of the structure classes using the 

"balls and bars" model for integer compositions. An integer 

composition of 6 such as [2, 1, 3] can be represented in this model 

as 'oooooo' where the 6 o's correspond to the balls and the 2 's 

correspond to the bars. If BB is our species for this model, the it 

satisfies the following recursive definition: 

 

BB = o + o\*BB + o\*|\*BB 

 

Here we define this species using the default structures:: 

 

sage: ball = species.SingletonSpecies(); o = var('o') 

sage: bar = species.EmptySetSpecies() 

sage: BB = CombinatorialSpecies() 

sage: BB.define(ball + ball*BB + ball*bar*BB) 

sage: BB.isotypes([o]*3).list() 

[o*(o*o), o*((o*{})*o), (o*{})*(o*o), (o*{})*((o*{})*o)] 

 

If we ignore the parentheses, we can read off that the integer 

compositions are [3], [2, 1], [1, 2], and [1, 1, 1]. 

""" 

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

# Copyright (C) 2008 Mike Hansen <mhansen@gmail.com>, 

# 

# Distributed under the terms of the GNU General Public License (GPL) 

# 

# This code is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 

# General Public License for more details. 

# 

# The full text of the GPL is available at: 

# 

# http://www.gnu.org/licenses/ 

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

from sage.combinat.combinat import CombinatorialClass, CombinatorialObject 

from sage.rings.integer import Integer 

from copy import copy 

 

 

class GenericSpeciesStructure(CombinatorialObject): 

def __init__(self, parent, labels, list): 

""" 

This is a base class from which the classes for the structures inherit. 

 

EXAMPLES:: 

 

sage: from sage.combinat.species.structure import GenericSpeciesStructure 

sage: a = GenericSpeciesStructure(None, [2,3,4], [1,2,3]) 

sage: a 

[2, 3, 4] 

sage: a.parent() is None 

True 

sage: a == loads(dumps(a)) 

True 

""" 

self._parent = parent 

self._labels = labels 

CombinatorialObject.__init__(self, list) 

 

def parent(self): 

""" 

Returns the species that this structure is associated with. 

 

EXAMPLES:: 

 

sage: L = species.LinearOrderSpecies() 

sage: a,b = L.structures([1,2]) 

sage: a.parent() 

Linear order species 

""" 

try: 

return self._parent 

except AttributeError: 

raise NotImplementedError 

 

def __repr__(self): 

""" 

EXAMPLES:: 

 

sage: from sage.combinat.species.structure import GenericSpeciesStructure 

sage: a = GenericSpeciesStructure(None, [2,3,4], [1,2,3]) 

sage: a 

[2, 3, 4] 

""" 

return repr([self._relabel(i) for i in self._list]) 

 

def __eq__(self, other): 

""" 

Check whether ``self`` is equal to ``other``. 

 

EXAMPLES:: 

 

sage: T = species.BinaryTreeSpecies() 

sage: t = T.structures([1,2,3])[0]; t 

1*(2*3) 

sage: t[0], t[1][0] 

(1, 2) 

sage: t[0] == t[1][0] 

False 

""" 

if not isinstance(other, GenericSpeciesStructure): 

return False 

return self._list == other._list and self.labels() == other.labels() 

 

def __ne__(self, other): 

""" 

Check whether ``self`` is not equal to ``other``. 

 

EXAMPLES:: 

 

sage: T = species.BinaryTreeSpecies() 

sage: t = T.structures([1,2,3])[0]; t 

1*(2*3) 

sage: t[0], t[1][0] 

(1, 2) 

sage: t[0] != t[1][0] 

True 

""" 

return not (self == other) 

 

def labels(self): 

""" 

Returns the labels used for this structure. 

 

.. note:: 

 

This includes labels which may not "appear" in this 

particular structure. 

 

EXAMPLES:: 

 

sage: P = species.SubsetSpecies() 

sage: s = P.structures(["a", "b", "c"]).random_element() 

sage: s.labels() 

['a', 'b', 'c'] 

""" 

return copy(self._labels) 

 

def change_labels(self, labels): 

""" 

Return a relabelled structure. 

 

INPUT: 

 

- ``labels``, a list of labels. 

 

OUTPUT: 

 

A structure with the i-th label of self replaced with the i-th 

label of the list. 

 

EXAMPLES:: 

 

sage: P = species.SubsetSpecies() 

sage: S = P.structures(["a", "b", "c"]) 

sage: [s.change_labels([1,2,3]) for s in S] 

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

""" 

c = copy(self) 

c._labels = labels 

return c 

 

def _relabel(self, i): 

""" 

EXAMPLES:: 

 

sage: from sage.combinat.species.structure import GenericSpeciesStructure 

sage: a = GenericSpeciesStructure(None, [2,3,4], [1,2,3]) 

sage: a._relabel(1) 

2 

sage: a._relabel([1,2,3]) 

[1, 2, 3] 

""" 

if isinstance(i, (int, Integer)): 

return self._labels[i-1] 

else: 

return i 

 

def is_isomorphic(self, x): 

""" 

EXAMPLES:: 

 

sage: S = species.SetSpecies() 

sage: a = S.structures([1,2,3]).random_element(); a 

{1, 2, 3} 

sage: b = S.structures(['a','b','c']).random_element(); b 

{'a', 'b', 'c'} 

sage: a.is_isomorphic(b) 

True 

""" 

if self.__class__ != x.__class__: 

return False 

if self.parent() != x.parent(): 

return False 

 

#We don't care about the labels for isomorphism testing 

if self.canonical_label()._list == x.canonical_label()._list: 

return True 

else: 

return False 

 

#For backward compatibility. This should be removed in the near 

#future since I doubt that there is any code that depends directly on 

#SpeciesStructure. 

SpeciesStructure = GenericSpeciesStructure 

 

class SpeciesStructureWrapper(GenericSpeciesStructure): 

def __init__(self, parent, s, **options): 

""" 

This is a class for the structures of species such as the sum 

species that do not provide "additional" structure. For example, 

if you have the sum `C` of species `A` and `B`, 

then a structure of `C` will either be either something from `A` or `B`. 

Instead of just returning one of these directly, a "wrapper" is 

put around them so that they have their parent is `C` rather than `A` or 

`B`:: 

 

sage: X = species.SingletonSpecies() 

sage: X2 = X+X 

sage: s = X2.structures([1]).random_element(); s 

1 

sage: s.parent() 

Sum of (Singleton species) and (Singleton species) 

sage: from sage.combinat.species.structure import SpeciesStructureWrapper 

sage: issubclass(type(s), SpeciesStructureWrapper) 

True 

 

EXAMPLES:: 

 

sage: E = species.SetSpecies(); B = E+E 

sage: s = B.structures([1,2,3]).random_element() 

sage: s.parent() 

Sum of (Set species) and (Set species) 

sage: s == loads(dumps(s)) 

True 

""" 

self._parent = parent 

self._s = s 

self._options = options 

GenericSpeciesStructure.__init__(self, parent, s._labels, s._list) 

 

def __getattr__(self, attr): 

""" 

EXAMPLES:: 

 

sage: E = species.SetSpecies(); B = E+E 

sage: s = B.structures([1,2,3]).random_element() 

sage: s 

{1, 2, 3} 

""" 

if attr == "_s": 

return None 

 

return getattr(self._s, attr) 

 

def __repr__(self): 

""" 

Returns the repr of the object which this one wraps. 

 

EXAMPLES:: 

 

sage: E = species.SetSpecies() 

sage: s = (E+E).structures([1,2,3]).random_element(); s 

{1, 2, 3} 

""" 

return repr(self._s) 

 

def transport(self, perm): 

""" 

EXAMPLES:: 

 

sage: P = species.PartitionSpecies() 

sage: s = (P+P).structures([1,2,3]).random_element(); s 

{{1, 3}, {2}} 

sage: s.transport(PermutationGroupElement((2,3))) 

{{1, 2}, {3}} 

""" 

return self.__class__(self._parent, self._s.transport(perm), **self._options) 

 

def canonical_label(self): 

""" 

EXAMPLES:: 

 

sage: P = species.PartitionSpecies() 

sage: s = (P+P).structures([1,2,3]).random_element(); s 

{{1, 3}, {2}} 

sage: s.canonical_label() 

{{1, 2}, {3}} 

""" 

return self.__class__(self._parent, self._s.canonical_label(), **self._options) 

 

def change_labels(self, labels): 

""" 

Return a relabelled structure. 

 

INPUT: 

 

- ``labels``, a list of labels. 

 

OUTPUT: 

 

A structure with the i-th label of self replaced with the i-th 

label of the list. 

 

EXAMPLES:: 

 

sage: X = species.SingletonSpecies() 

sage: X2 = X+X 

sage: s = X2.structures([1]).random_element(); s 

1 

sage: s.change_labels(['a']) 

'a' 

""" 

c = GenericSpeciesStructure.change_labels(self, labels) 

c._s = c._s.change_labels(labels) 

return c 

 

 

############################################################## 

 

 

class SpeciesWrapper(CombinatorialClass): 

def __init__(self, species, labels, iterator, generating_series, name, structure_class): 

""" 

This is a abstract base class for the set of structures of a 

species as well as the set of isotypes of the species. 

 

.. note:: 

 

One typically does not use :class:`SpeciesWrapper` 

directly, but instead instantiates one of its subclasses: 

:class:`StructuresWrapper` or :class:`IsotypesWrapper`. 

 

EXAMPLES:: 

 

sage: from sage.combinat.species.structure import SpeciesWrapper 

sage: F = species.SetSpecies() 

sage: S = SpeciesWrapper(F, [1,2,3], "_structures", "generating_series", 'Structures', None) 

sage: S 

Structures for Set species with labels [1, 2, 3] 

sage: S.list() 

[{1, 2, 3}] 

sage: S.cardinality() 

1 

""" 

self._species = species 

self._labels = labels 

self._iterator = iterator 

self._generating_series = generating_series 

self._name = "%s for %s with labels %s" % (name, species, labels) 

self._structure_class = structure_class if structure_class is not None else species._default_structure_class 

 

def labels(self): 

""" 

Returns the labels used on these structures. If `X` is the 

species, then :meth:`labels` returns the preimage of these 

structures under the functor `X`. 

 

EXAMPLES:: 

 

sage: F = species.SetSpecies() 

sage: F.structures([1,2,3]).labels() 

[1, 2, 3]  

""" 

return copy(self._labels) 

 

def __iter__(self): 

""" 

EXAMPLES:: 

 

sage: F = species.SetSpecies() 

sage: F.structures([1,2,3]).list() 

[{1, 2, 3}] 

""" 

#If the min and max are set, then we want to make sure 

#that the iterator respects those bounds. 

if (self._species._min is not None and 

len(self._labels) < self._species._min): 

return iter([]) 

 

if (self._species._max is not None and 

len(self._labels) >= self._species._max): 

return iter([]) 

 

#We check to see if the 

try: 

if self.cardinality() == 0: 

return iter([]) 

except RuntimeError: 

raise NotImplementedError 

 

return getattr(self._species, self._iterator)(self._structure_class, self._labels) 

 

def cardinality(self): 

""" 

Returns the number of structures in this set. 

 

EXAMPLES:: 

 

sage: F = species.SetSpecies() 

sage: F.structures([1,2,3]).cardinality() 

1 

""" 

return getattr(self._species, self._generating_series)().count(len(self._labels)) 

 

class StructuresWrapper(SpeciesWrapper): 

def __init__(self, species, labels, structure_class): 

""" 

A base class for the set of structures of a species with given 

set of labels. An object of this type is returned when you 

call the :meth:`structures` method of a species. 

 

EXAMPLES:: 

 

sage: F = species.SetSpecies() 

sage: S = F.structures([1,2,3]) 

sage: S == loads(dumps(S)) 

True 

""" 

SpeciesWrapper.__init__(self, species, labels, 

"_structures", 

"generating_series", 

"Structures", 

structure_class) 

 

class IsotypesWrapper(SpeciesWrapper): 

def __init__(self, species, labels, structure_class): 

""" 

A base class for the set of isotypes of a species with given 

set of labels. An object of this type is returned when you 

call the :meth:`isotypes` method of a species. 

 

EXAMPLES:: 

 

sage: F = species.SetSpecies() 

sage: S = F.isotypes([1,2,3]) 

sage: S == loads(dumps(S)) 

True 

""" 

SpeciesWrapper.__init__(self, species, labels, 

"_isotypes", 

"isotype_generating_series", 

"Isomorphism types", 

structure_class) 

 

 

class SimpleStructuresWrapper(SpeciesWrapper): 

def __init__(self, species, labels, structure_class): 

""" 

.. warning:: 

 

This is deprecated and currently not used for anything. 

 

EXAMPLES:: 

 

sage: F = species.SetSpecies() 

sage: S = F.structures([1,2,3]) 

sage: S == loads(dumps(S)) 

True 

""" 

SpeciesWrapper.__init__(self, species, labels, 

"_simple_structures_selector", 

"generating_series", 

"Simple structures", 

structure_class) 

 

 

class SimpleIsotypesWrapper(SpeciesWrapper): 

def __init__(self, species, labels, structure_class): 

""" 

.. warning:: 

 

This is deprecated and currently not used for anything. 

 

EXAMPLES:: 

 

sage: F = species.SetSpecies() 

sage: S = F.structures([1,2,3]) 

sage: S == loads(dumps(S)) 

True 

""" 

SpeciesWrapper.__init__(self, species, labels, 

"_simple_isotypes_selector", 

"isotype_generating_series", 

"Simple isomorphism types", 

structure_class)