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

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

645

646

647

648

649

650

651

652

653

654

655

656

657

658

659

660

661

662

663

664

665

666

667

668

669

670

671

672

673

674

675

676

677

678

679

680

681

682

683

684

685

686

687

688

689

690

691

692

693

694

695

696

697

698

699

700

701

702

703

704

705

706

707

708

709

710

711

712

713

714

715

716

717

718

719

720

721

722

723

724

725

726

727

728

729

730

731

732

733

734

735

736

737

738

739

740

741

742

743

744

745

746

747

748

749

750

751

752

753

754

755

756

757

758

759

760

761

762

763

764

765

766

767

768

769

770

771

772

773

774

775

776

777

778

779

780

781

782

783

784

785

786

787

788

789

790

791

792

793

794

795

796

797

798

799

800

801

802

803

804

805

806

807

808

809

810

811

812

813

814

815

816

817

818

819

820

821

822

823

824

825

826

827

828

829

830

831

832

833

834

835

836

837

838

839

840

841

842

843

844

845

846

847

848

849

850

851

852

853

854

855

856

857

858

859

860

861

862

r""" 

Composition Tableaux 

 

AUTHORS: 

 

- Chris Berg, Jeff Ferreira (2012-9): Initial version 

""" 

from six.moves import range 

from six import add_metaclass 

 

from sage.sets.disjoint_union_enumerated_sets import DisjointUnionEnumeratedSets 

from sage.sets.non_negative_integers import NonNegativeIntegers 

from sage.sets.family import Family 

from sage.misc.classcall_metaclass import ClasscallMetaclass 

from sage.categories.finite_enumerated_sets import FiniteEnumeratedSets 

from sage.structure.parent import Parent 

from sage.structure.unique_representation import UniqueRepresentation 

from sage.combinat.composition import Composition, Compositions 

from sage.combinat.partition import Partition 

from sage.combinat.combinat import CombinatorialElement 

from sage.rings.integer import Integer 

from sage.combinat.backtrack import GenericBacktracker 

import copy 

 

 

@add_metaclass(ClasscallMetaclass) 

class CompositionTableau(CombinatorialElement): 

r""" 

A composition tableau. 

 

A *composition tableau* `t` of shape `I = (I_1, \ldots, I_{\ell})` is an 

array of boxes in rows, `I_i` boxes in row `i`, filled with positive 

integers such that: 

 

1) the entries in the rows of `t` weakly decrease from left to right, 

2) the left-most column of `t` strictly increase from top to bottom. 

3) Add zero entries to the rows of `t` until the resulting array is 

rectangular of shape `\ell \times m`. For `1 \leq i < j \leq \ell, 

2 \leq k \leq m` and `(t(j,k) \neq 0`, and also if `t(j,k) \geq t(i,k))` 

implies `t(j,k) > t(i,k-1).` 

 

INPUT: 

 

- ``t`` -- A list of lists 

 

EXAMPLES:: 

 

sage: CompositionTableau([[1],[2,2]]) 

[[1], [2, 2]] 

sage: CompositionTableau([[1],[3,2],[4,4]]) 

[[1], [3, 2], [4, 4]] 

sage: CompositionTableau([]) 

[] 

""" 

@staticmethod 

def __classcall_private__(self, t): 

r""" 

This ensures that a composition tableau is only ever constructed as 

an ``element_class`` call of an appropriate parent. 

 

TESTS:: 

 

sage: t = CompositionTableau([[1],[2,2]]) 

sage: TestSuite(t).run() 

 

sage: t.parent() 

Composition Tableaux 

sage: t.category() 

Category of elements of Composition Tableaux 

""" 

if isinstance(t, CompositionTableau): 

return t 

return CompositionTableaux_all().element_class(CompositionTableaux_all(), t) 

 

def __init__(self, parent, t): 

r""" 

Initialize a composition tableau. 

 

TESTS:: 

 

sage: t = CompositionTableaux()([[1],[2,2]]) 

sage: s = CompositionTableaux(3)([[1],[2,2]]) 

sage: s==t 

True 

sage: t.parent() 

Composition Tableaux 

sage: s.parent() 

Composition Tableaux of size 3 and maximum entry 3 

sage: r = CompositionTableaux()(s) 

sage: r.parent() 

Composition Tableaux 

""" 

if isinstance(t, CompositionTableau): 

CombinatorialElement.__init__(self, parent, t._list) 

return 

 

# CombinatorialObject verifies that t is a list 

# We must verify t is a list of lists 

if not all(isinstance(row, list) for row in t): 

raise ValueError("A composition tableau must be a list of lists.") 

 

if not [len(_) for _ in t] in Compositions(): 

raise ValueError("A composition tableau must be a list of non-empty lists.") 

 

# Verify rows weakly decrease from left to right 

for row in t: 

if any(row[i] < row[i+1] for i in range(len(row)-1)): 

raise ValueError("Rows must weakly decrease from left to right.") 

 

# Verify leftmost column strictly increases from top to bottom 

first_col = [row[0] for row in t if t!=[[]]] 

if any(first_col[i] >= first_col[i+1] for i in range(len(t)-1)): 

raise ValueError("Leftmost column must strictly increase from top to bottom.") 

 

# Verify triple condition 

l = len(t) 

m = max([len(_) for _ in t]+[0]) 

TT = [row+[0]*(m-len(row)) for row in t] 

for i in range(l): 

for j in range(i+1,l): 

for k in range(1,m): 

if TT[j][k] != 0 and TT[j][k] >= TT[i][k] and TT[j][k] <= TT[i][k-1]: 

raise ValueError("Triple condition must be satisfied.") 

 

CombinatorialElement.__init__(self, parent, t) 

 

def _repr_diagram(self): 

r""" 

Return a string representation of ``self`` as an array. 

 

EXAMPLES:: 

 

sage: t = CompositionTableau([[1],[3,2],[4,4]]) 

sage: print(t._repr_diagram()) 

1 

3 2 

4 4 

""" 

return '\n'.join(("".join(("%3s"%str(x) for x in row)) for row in self)) 

 

def __call__(self, *cell): 

r""" 

Return the value in the corresponding cell of ``self``. 

 

EXAMPLES:: 

 

sage: t = CompositionTableau([[1],[3,2],[4,4]]) 

sage: t(1,1) 

2 

sage: t(2,0) 

4 

sage: t(2,2) 

Traceback (most recent call last): 

... 

IndexError: The cell (2,2) is not contained in [[1], [3, 2], [4, 4]] 

""" 

try: 

i,j = cell 

except ValueError: 

i,j = cell[0] 

 

try: 

return self[i][j] 

except IndexError: 

raise IndexError("The cell (%d,%d) is not contained in %s"%(i,j,self)) 

 

def pp(self): 

r""" 

Return a pretty print string of ``self``. 

 

EXAMPLES:: 

 

sage: CompositionTableau([[1],[3,2],[4,4]]).pp() 

1 

3 2 

4 4 

""" 

print(self._repr_diagram()) 

 

def size(self): 

r""" 

Return the number of boxes in ``self``. 

 

EXAMPLES:: 

 

sage: CompositionTableau([[1],[3,2],[4,4]]).size() 

5 

""" 

return sum([len(row) for row in self]) 

 

def weight(self): 

r""" 

Return a composition where entry `i` is the number of times that `i` appears in 

``self``. 

 

EXAMPLES:: 

 

sage: CompositionTableau([[1],[3,2],[4,4]]).weight() 

[1, 1, 1, 2, 0] 

""" 

w = {i:0 for i in range(1,self.size()+1)} 

for row in self: 

for i in row: 

w[i] += 1 

return Composition([w[i] for i in range(1,self.size()+1)]) 

 

def descent_set(self): 

r""" 

Return the set of all `i` that do *not* have `i+1` appearing strictly 

to the left of `i` in ``self``. 

 

EXAMPLES:: 

 

sage: CompositionTableau([[1],[3,2],[4,4]]).descent_set() 

[1, 3] 

""" 

cols = {} 

for row in self: 

for (col,i) in enumerate(row): 

cols[i] = col 

des_set = sorted([i for i in cols if i+1 in cols and cols[i+1] >= cols[i]]) 

return des_set 

 

def descent_composition(self): 

r""" 

Return the composition corresponding to the set of all `i` that do 

not have `i+1` appearing strictly to the left of `i` in ``self``. 

 

EXAMPLES:: 

 

sage: CompositionTableau([[1],[3,2],[4,4]]).descent_composition() 

[1, 2, 2] 

""" 

return Composition(from_subset=(self.descent_set(), self.size())) 

 

def shape_composition(self): 

r""" 

Return a Composition object which is the shape of ``self``. 

 

EXAMPLES:: 

 

sage: CompositionTableau([[1,1],[3,2],[4,4,3]]).shape_composition() 

[2, 2, 3] 

sage: CompositionTableau([[2,1],[3],[4]]).shape_composition() 

[2, 1, 1] 

""" 

return Composition([len(row) for row in self]) 

 

def shape_partition(self): 

r""" 

Return a partition which is the shape of ``self`` sorted into weakly 

decreasing order. 

 

EXAMPLES:: 

 

sage: CompositionTableau([[1,1],[3,2],[4,4,3]]).shape_partition() 

[3, 2, 2] 

sage: CompositionTableau([[2,1],[3],[4]]).shape_partition() 

[2, 1, 1] 

""" 

return Partition(sorted([len(row) for row in self], reverse=True)) 

 

def is_standard(self): 

r""" 

Return ``True`` if ``self`` is a standard composition tableau and 

``False`` otherwise. 

 

EXAMPLES:: 

 

sage: CompositionTableau([[1,1],[3,2],[4,4,3]]).is_standard() 

False 

sage: CompositionTableau([[2,1],[3],[4]]).is_standard() 

True 

""" 

entries = sum(self,[]) 

return sorted(entries) == list(range(1, self.size() + 1)) 

 

class CompositionTableaux(UniqueRepresentation, Parent): 

r""" 

Composition tableaux. 

 

INPUT: 

 

Keyword arguments: 

 

- ``size`` -- the size of the composition tableaux 

- ``shape`` -- the shape of the composition tableaux 

- ``max_entry`` -- the maximum entry for the composition tableaux 

 

Positional arguments: 

 

- The first argument is interpreted as ``size`` or ``shape`` depending on 

whether it is an integer or a composition. 

 

EXAMPLES:: 

 

sage: CT = CompositionTableaux(3); CT 

Composition Tableaux of size 3 and maximum entry 3 

sage: list(CT) 

[[[1], [2], [3]], 

[[1], [2, 2]], 

[[1], [3, 2]], 

[[1], [3, 3]], 

[[2], [3, 3]], 

[[1, 1], [2]], 

[[1, 1], [3]], 

[[2, 1], [3]], 

[[2, 2], [3]], 

[[1, 1, 1]], 

[[2, 1, 1]], 

[[2, 2, 1]], 

[[2, 2, 2]], 

[[3, 1, 1]], 

[[3, 2, 1]], 

[[3, 2, 2]], 

[[3, 3, 1]], 

[[3, 3, 2]], 

[[3, 3, 3]]] 

 

sage: CT = CompositionTableaux([1,2,1]); CT 

Composition tableaux of shape [1, 2, 1] and maximum entry 4 

sage: list(CT) 

[[[1], [2, 2], [3]], 

[[1], [2, 2], [4]], 

[[1], [3, 2], [4]], 

[[1], [3, 3], [4]], 

[[2], [3, 3], [4]]] 

 

sage: CT = CompositionTableaux(shape=[1,2,1],max_entry=3); CT 

Composition tableaux of shape [1, 2, 1] and maximum entry 3 

sage: list(CT) 

[[[1], [2, 2], [3]]] 

 

sage: CT = CompositionTableaux(2,max_entry=3); CT 

Composition Tableaux of size 2 and maximum entry 3 

sage: list(CT) 

[[[1], [2]], 

[[1], [3]], 

[[2], [3]], 

[[1, 1]], 

[[2, 1]], 

[[2, 2]], 

[[3, 1]], 

[[3, 2]], 

[[3, 3]]] 

 

sage: CT = CompositionTableaux(0); CT 

Composition Tableaux of size 0 and maximum entry 0 

sage: list(CT) 

[[]] 

""" 

@staticmethod 

def __classcall_private__(cls, *args, **kwargs): 

r""" 

This is a factory class which returns the appropriate parent based on 

arguments. See the documentation for :class:`CompositionTableaux` for 

more information. 

 

TESTS:: 

 

sage: CT = CompositionTableaux(3); CT 

Composition Tableaux of size 3 and maximum entry 3 

sage: CT = CompositionTableaux(size=3); CT 

Composition Tableaux of size 3 and maximum entry 3 

sage: CT = CompositionTableaux([1,2]); CT 

Composition tableaux of shape [1, 2] and maximum entry 3 

sage: CT = CompositionTableaux(shape=[1,2]); CT 

Composition tableaux of shape [1, 2] and maximum entry 3 

sage: CT = CompositionTableaux(shape=[]); CT 

Composition tableaux of shape [] and maximum entry 0 

sage: CT = CompositionTableaux(0); CT 

Composition Tableaux of size 0 and maximum entry 0 

sage: CT = CompositionTableaux(max_entry=3); CT 

Composition tableaux with maximum entry 3 

sage: CT = CompositionTableaux([1,2],max_entry=3); CT 

Composition tableaux of shape [1, 2] and maximum entry 3 

sage: CT = CompositionTableaux(size=2,shape=[1,2]); CT 

Traceback (most recent call last): 

... 

ValueError: size and shape are different sizes 

""" 

# Process keyword arguments first 

n = kwargs.get('n', None) 

size = kwargs.get('size', n) 

 

comp = kwargs.get('comp', None) 

shape = kwargs.get('shape', comp) 

 

max_entry = kwargs.get('max_entry', None) 

 

# Process positional arguments 

if args: 

# The first arg could be either a size or a shape 

if isinstance(args[0], (int, Integer)): 

if size is not None: 

raise ValueError("size was specified more than once") 

else: 

size = args[0] 

else: 

if shape is not None: 

raise ValueError("the shape was specified more than once") 

shape = args[0] 

 

# Consistency checks 

if size is not None: 

if not isinstance(size, (int, Integer)): 

raise ValueError("size must be an integer") 

elif size < 0: 

raise ValueError("size must be non-negative") 

 

if shape is not None: 

# use in (and not isinstance) below so that lists can be used as 

# shorthand 

if not shape in Compositions(): 

raise ValueError("shape must be a composition") 

if any(i == 0 for i in shape): 

raise ValueError("shape must have non-zero parts") 

shape = Composition(shape) 

 

if (size is not None) and (shape is not None): 

if sum(shape) != size: 

raise ValueError("size and shape are different sizes") 

 

if max_entry is not None: 

if not isinstance(max_entry, (int, Integer)): 

raise ValueError("max_entry must be an integer") 

elif max_entry <= 0: 

raise ValueError("max_entry must be positive") 

 

# Dispatch to appropriate class 

if (shape is not None): 

return CompositionTableaux_shape(shape, max_entry) 

 

if (size is not None): 

return CompositionTableaux_size(size, max_entry) 

 

return CompositionTableaux_all(max_entry) 

 

def __init__(self, **kwds): 

r""" 

Initialize ``self``. 

 

TESTS:: 

 

sage: CT = CompositionTableaux() 

sage: TestSuite(CT).run() 

""" 

if 'max_entry' in kwds: 

self.max_entry = kwds['max_entry'] 

kwds.pop('max_entry') 

else: 

self.max_entry = None 

super(CompositionTableaux, self).__init__(**kwds) 

 

Element = CompositionTableau 

 

def _element_constructor_(self, t): 

r""" 

Construct an object from ``t`` as an element of ``self``, if 

possible. 

 

INPUT: 

 

- ``t`` -- data which can be interpreted as a composition tableau 

 

OUTPUT: 

 

- The corresponding CompositionTableau object 

 

TESTS:: 

 

sage: CT = CompositionTableaux(3) 

sage: CT([[1],[2,2]]).parent() is CT 

True 

sage: CT([[1],[1,2]]) 

Traceback (most recent call last): 

... 

ValueError: [[1], [1, 2]] is not an element of Composition Tableaux of size 3 and maximum entry 3. 

""" 

if not t in self: 

raise ValueError("%s is not an element of %s."%(t, self)) 

 

return self.element_class(self, t) 

 

def __contains__(self, T): 

r""" 

Return ``True`` if ``T`` can be interpreted as 

:class:`CompositionTableau`. 

 

TESTS:: 

 

sage: [[1],[2,2]] in CompositionTableaux(3) 

True 

sage: [[1],[2,2]] in CompositionTableaux(shape=[1,2]) 

True 

sage: CompositionTableau([[1],[2,2]]) in CompositionTableaux() 

True 

sage: [[1],[2,2],[2]] in CompositionTableaux() 

False 

""" 

if isinstance(T, CompositionTableau): 

return True 

 

# leftmost column of T strictly increases from top to bottom 

first_col = [row[0] for row in T] 

if any(first_col[i] >= first_col[i+1] for i in range(len(T)-1)): 

return False 

# rows of T weakly decrease from left to right 

for row in T: 

if any(row[i] < row[i+1] for i in range(len(row)-1)): 

return False 

# for 1 <= i < j <= len(comp), for 2 <= k <= m, 

# T[j,k] \neq 0 and T[j,k] >= T[i,k] ==> T[j,k] > T[i,k-1] 

l = len(T) 

m = max([len(_) for _ in T]+[0]) 

TT = [row+[0]*(m-len(row)) for row in T] 

for i in range(l): 

for j in range(i+1,l): 

for k in range(1,m): 

if TT[j][k] != 0 and TT[j][k] >= TT[i][k] and TT[j][k] <= TT[i][k-1]: 

return False 

return True 

 

class CompositionTableaux_all(CompositionTableaux, DisjointUnionEnumeratedSets): 

r""" 

All composition tableaux. 

""" 

def __init__(self, max_entry=None): 

r""" 

Initialize ``self``. 

 

TESTS:: 

 

sage: CT = CompositionTableaux() 

sage: TestSuite(CT).run() 

""" 

self.max_entry = max_entry 

CT_n = lambda n: CompositionTableaux_size(n, max_entry) 

DisjointUnionEnumeratedSets.__init__(self, 

Family(NonNegativeIntegers(), CT_n), 

facade=True, keepkey = False) 

 

def _repr_(self): 

r""" 

TESTS:: 

 

sage: CompositionTableaux(3) 

Composition Tableaux of size 3 and maximum entry 3 

 

sage: CompositionTableaux() 

Composition Tableaux 

""" 

if self.max_entry is not None: 

return "Composition tableaux with maximum entry %s"%str(self.max_entry) 

return "Composition Tableaux" 

 

def an_element(self): 

r""" 

Return a particular element of ``self``. 

 

EXAMPLES:: 

 

sage: CT = CompositionTableaux() 

sage: CT.an_element() 

[[1, 1], [2]] 

""" 

return self.element_class(self, [[1, 1], [2]]) 

 

class CompositionTableaux_size(CompositionTableaux): 

r""" 

Composition tableaux of a fixed size `n`. 

 

INPUT: 

 

- ``n`` -- a nonnegative integer. 

- ``max_entry`` -- a nonnegative integer. This keyword argument defaults to ``n``. 

 

OUTPUT: 

 

- The class of composition tableaux of size ``n``. 

""" 

def __init__(self, n, max_entry=None): 

r""" 

Initializes the class of composition tableaux of size ``n``. 

 

TESTS:: 

 

sage: CT = CompositionTableaux(4) 

sage: TestSuite(CT).run() 

""" 

if max_entry is None: 

max_entry = n 

super(CompositionTableaux_size, self).__init__(max_entry=max_entry, 

category=FiniteEnumeratedSets()) 

self.size = n 

 

def __contains__(self,x): 

r""" 

TESTS:: 

 

sage: [[1],[2,2]] in CompositionTableaux(3) 

True 

sage: [[1],[2,2]] in CompositionTableaux(4) 

False 

""" 

return CompositionTableaux.__contains__(self, x) and sum(map(len,x)) == self.size 

 

def __iter__(self): 

r""" 

EXAMPLES:: 

 

sage: [t for t in CompositionTableaux(3)] 

[[[1], [2], [3]], 

[[1], [2, 2]], 

[[1], [3, 2]], 

[[1], [3, 3]], 

[[2], [3, 3]], 

[[1, 1], [2]], 

[[1, 1], [3]], 

[[2, 1], [3]], 

[[2, 2], [3]], 

[[1, 1, 1]], 

[[2, 1, 1]], 

[[2, 2, 1]], 

[[2, 2, 2]], 

[[3, 1, 1]], 

[[3, 2, 1]], 

[[3, 2, 2]], 

[[3, 3, 1]], 

[[3, 3, 2]], 

[[3, 3, 3]]] 

 

sage: CompositionTableaux(3)[0].parent() is CompositionTableaux(3) 

True 

""" 

for comp in Compositions(self.size): 

for T in CompositionTableaux_shape(comp,self.max_entry): 

yield self.element_class(self, T) 

 

def _repr_(self): 

r""" 

TESTS:: 

 

sage: CompositionTableaux(3) 

Composition Tableaux of size 3 and maximum entry 3 

""" 

return "Composition Tableaux of size %s and maximum entry %s"%(str(self.size), str(self.max_entry)) 

 

def _an_element_(self): 

r""" 

Return a particular element of ``self``. 

 

EXAMPLES:: 

 

sage: CT = CompositionTableaux(4) 

sage: CT.an_element() 

[[1, 1, 1], [2]] 

sage: CompositionTableaux(0).an_element() 

[] 

sage: CompositionTableaux(1).an_element() 

[[1]] 

""" 

if self.size == 0: 

return self.element_class(self, []) 

if self.size == 1: 

return self.element_class(self,[[1]]) 

 

return self.element_class(self, [[1]*(self.size-1),[2]]) 

 

class CompositionTableaux_shape(CompositionTableaux): 

r""" 

Composition tableaux of a fixed shape ``comp`` with a given max entry. 

 

INPUT: 

 

- ``comp`` -- a composition. 

- ``max_entry`` -- a nonnegative integer. This keyword argument defaults 

to the size of ``comp``. 

""" 

def __init__(self, comp, max_entry=None): 

""" 

Initialize ``sefl``. 

 

TESTS:: 

 

sage: CT = CompositionTableaux([1,2]) 

sage: TestSuite(CT).run() 

 

sage: CT = CompositionTableaux([1,2], max_entry=4) 

sage: TestSuite(CT).run() 

""" 

if max_entry is None: 

max_entry = sum(comp) 

super(CompositionTableaux_shape, self).__init__(max_entry = max_entry, 

category = FiniteEnumeratedSets()) 

self.shape = comp 

 

def __iter__(self): 

r""" 

An iterator for composition tableaux of a given shape. 

 

EXAMPLES:: 

 

sage: [t for t in CompositionTableaux([1,2])] 

[[[1], [2, 2]], [[1], [3, 2]], [[1], [3, 3]], [[2], [3, 3]]] 

sage: [t for t in CompositionTableaux([1,2],max_entry=4)] 

[[[1], [2, 2]], 

[[1], [3, 2]], 

[[1], [3, 3]], 

[[1], [4, 2]], 

[[1], [4, 3]], 

[[1], [4, 4]], 

[[2], [3, 3]], 

[[2], [4, 3]], 

[[2], [4, 4]], 

[[3], [4, 4]]] 

""" 

if sum(self.shape) == 0: 

yield CompositionTableau([]) 

else: 

for z in CompositionTableauxBacktracker(self.shape, self.max_entry): 

yield CompositionTableau(z) 

 

def __contains__(self, x): 

r""" 

TESTS:: 

 

sage: [[2],[4,3]] in CompositionTableaux([1,2]) 

True 

sage: [[2],[3,2]] in CompositionTableaux([1,2]) 

False 

""" 

return CompositionTableaux.__contains__(self, x) and [len(_) for _ in x] == self.shape 

 

def _repr_(self): 

r""" 

TESTS:: 

 

sage: CompositionTableaux([1,2,1]) 

Composition tableaux of shape [1, 2, 1] and maximum entry 4 

sage: CompositionTableaux([1,2,1],max_entry=3) 

Composition tableaux of shape [1, 2, 1] and maximum entry 3 

""" 

return "Composition tableaux of shape %s and maximum entry %s" % (str(self.shape), str(self.max_entry)) 

 

def an_element(self): 

r""" 

Return a particular element of :class:`CompositionTableaux_shape`. 

 

EXAMPLES:: 

 

sage: CT = CompositionTableaux([1,2,1]) 

sage: CT.an_element() 

[[1], [2, 2], [3]] 

""" 

if self.shape == []: 

return self.element_class(self, []) 

t = [[i]*len for (i,len) in enumerate(self.shape,start=1)] 

return self.element_class(self, t) 

 

class CompositionTableauxBacktracker(GenericBacktracker): 

r""" 

A backtracker class for generating sets of composition tableaux. 

""" 

def __init__(self, shape, max_entry=None): 

""" 

EXAMPLES:: 

 

sage: from sage.combinat.composition_tableau import CompositionTableauxBacktracker 

sage: n = CompositionTableauxBacktracker([1,3,2]) 

sage: n._ending_position 

(2, 1) 

sage: n._initial_state 

(0, 0) 

""" 

self._shape = shape 

self._n = sum(shape) 

self._initial_data = [ [None]*s for s in shape ] 

if max_entry is None: 

max_entry=sum(shape) 

self.max_entry=max_entry 

 

# The ending position will be at the lowest box which is farthest right 

ending_row = len(shape)-1 

ending_col = shape[-1]-1 

self._ending_position = (ending_row, ending_col) 

 

# Get the highest box that is farthest left 

starting_row = 0 

starting_col = 0 

 

GenericBacktracker.__init__(self, self._initial_data, (starting_row, starting_col)) 

 

def _rec(self, obj, state): 

r""" 

EXAMPLES:: 

 

sage: from sage.combinat.composition_tableau import CompositionTableauxBacktracker 

sage: n = CompositionTableauxBacktracker([1,3,2]) 

sage: obj = [ [None], [None, None, None], [None, None] ] 

sage: list(n._rec(obj, n._initial_state)) 

[([[1], [None, None, None], [None, None]], (1, 0), False), 

([[2], [None, None, None], [None, None]], (1, 0), False), 

([[3], [None, None, None], [None, None]], (1, 0), False), 

([[4], [None, None, None], [None, None]], (1, 0), False), 

([[5], [None, None, None], [None, None]], (1, 0), False), 

([[6], [None, None, None], [None, None]], (1, 0), False)] 

""" 

#Append zeros to a copy of obj 

obj_copy = copy.deepcopy(obj) 

for a in range(len(obj_copy)): 

for b in range(len(max(obj_copy))-len(obj_copy[a])): 

obj_copy[a].append(0) 

 

#We need to set the i,j^th entry. 

i, j = state 

 

#Get the next state 

new_state = self.get_next_pos(i, j) 

yld = True if new_state is None else False 

 

for k in range(1,self.max_entry +1): 

#We check to make sure that k does not violate the rule weak decrease in rows 

if j!=0 and obj[i][j-1] < k: 

continue 

 

#We check to make sure that k does not violate strict increase in first column 

if j == 0 and i != 0 and k <= obj[i-1][j]: 

continue 

 

#We check to make sure that k does not violate the Triple Rule 

if j != 0 and i != 0 and any(k == obj_copy[m][j] for m in range(i)): 

continue 

if j != 0 and i != 0 and any(obj_copy[m][j] < k and k <= obj_copy[m][j-1] for m in range(i)): 

continue 

 

#Fill in the in the i,j box with k 

obj[i][j] = k 

obj_copy[i][j] = k 

 

#Yield the object 

yield copy.deepcopy(obj), new_state, yld 

 

def get_next_pos(self, ii, jj): 

r""" 

EXAMPLES:: 

 

sage: from sage.combinat.composition_tableau import CompositionTableauxBacktracker 

sage: T = CompositionTableau([[2,1],[5,4,3,2],[6],[7,7,6]]) 

sage: n = CompositionTableauxBacktracker(T.shape_composition()) 

sage: n.get_next_pos(1,1) 

(1, 2) 

""" 

if (ii, jj) == self._ending_position: 

return None 

 

for j in range(jj+1, self._shape[ii]): 

if self._shape[ii] >= j: 

return ii, j 

 

return ii+1, 0