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

863

864

865

866

867

868

869

870

871

872

873

874

875

876

877

878

879

880

881

882

883

884

885

886

887

888

889

890

891

892

893

894

895

896

897

898

899

900

901

902

903

904

905

906

907

908

909

910

911

912

913

914

915

916

917

918

919

920

921

922

923

924

925

926

927

928

929

930

931

932

933

934

935

936

937

938

939

940

941

942

943

944

945

946

947

948

949

950

951

952

953

954

955

956

957

958

959

960

961

962

963

964

965

966

967

968

969

970

971

972

973

974

975

976

977

978

979

980

981

982

983

984

985

986

987

988

989

990

991

992

993

994

995

996

997

998

999

1000

1001

1002

1003

1004

1005

1006

1007

1008

1009

1010

1011

1012

1013

1014

1015

1016

1017

1018

1019

1020

1021

1022

1023

1024

1025

1026

1027

1028

1029

1030

1031

1032

1033

1034

1035

1036

1037

1038

1039

1040

1041

1042

1043

1044

1045

1046

1047

1048

1049

1050

1051

1052

1053

1054

1055

1056

1057

1058

1059

1060

1061

1062

1063

1064

1065

1066

1067

1068

1069

1070

1071

1072

1073

1074

1075

1076

1077

1078

1079

1080

1081

1082

1083

1084

1085

1086

1087

1088

1089

1090

1091

1092

1093

1094

1095

1096

1097

1098

1099

1100

1101

1102

1103

1104

1105

1106

1107

1108

1109

1110

1111

1112

1113

1114

1115

1116

1117

1118

1119

1120

1121

1122

1123

1124

1125

1126

1127

1128

1129

1130

1131

1132

1133

1134

1135

1136

1137

1138

1139

1140

1141

1142

1143

1144

1145

1146

1147

1148

1149

1150

1151

1152

1153

1154

1155

1156

1157

1158

1159

1160

1161

1162

1163

1164

1165

1166

1167

1168

1169

1170

1171

1172

1173

1174

1175

1176

1177

1178

1179

1180

1181

1182

1183

1184

1185

1186

1187

1188

1189

1190

1191

1192

1193

1194

1195

1196

1197

1198

1199

1200

1201

1202

1203

1204

1205

1206

1207

1208

1209

1210

1211

1212

1213

1214

1215

1216

1217

1218

1219

1220

1221

1222

1223

1224

1225

1226

1227

1228

1229

1230

1231

1232

1233

1234

1235

1236

1237

1238

1239

1240

1241

1242

1243

1244

1245

1246

1247

1248

1249

1250

1251

1252

1253

1254

1255

1256

1257

1258

1259

1260

1261

1262

1263

1264

1265

1266

1267

1268

1269

1270

1271

1272

1273

1274

1275

1276

1277

1278

1279

1280

1281

1282

1283

1284

1285

1286

1287

1288

1289

1290

1291

1292

1293

1294

1295

1296

1297

1298

1299

1300

1301

1302

1303

1304

1305

1306

1307

1308

1309

1310

1311

1312

1313

1314

1315

1316

1317

1318

1319

1320

1321

1322

1323

1324

1325

1326

1327

1328

1329

1330

1331

1332

1333

1334

1335

1336

1337

1338

1339

1340

1341

1342

1343

1344

1345

1346

1347

1348

1349

1350

1351

1352

1353

1354

1355

1356

1357

1358

1359

1360

1361

1362

1363

1364

1365

1366

1367

1368

1369

1370

1371

1372

1373

1374

1375

1376

1377

1378

1379

1380

1381

1382

1383

1384

1385

1386

1387

1388

1389

1390

1391

1392

1393

1394

1395

1396

1397

1398

1399

1400

1401

1402

1403

1404

1405

1406

1407

1408

1409

1410

1411

1412

1413

1414

1415

1416

1417

1418

1419

1420

1421

1422

1423

1424

1425

1426

1427

1428

1429

1430

1431

1432

1433

1434

1435

1436

1437

1438

1439

1440

1441

1442

1443

1444

1445

1446

1447

1448

1449

1450

1451

1452

1453

1454

1455

1456

1457

1458

1459

1460

1461

1462

1463

1464

1465

1466

1467

1468

1469

1470

1471

1472

1473

1474

1475

1476

1477

1478

1479

1480

1481

1482

1483

1484

1485

1486

1487

1488

1489

1490

1491

1492

1493

1494

1495

1496

1497

1498

1499

1500

1501

1502

1503

1504

1505

1506

1507

1508

1509

1510

1511

1512

1513

1514

1515

1516

1517

1518

1519

1520

1521

1522

1523

1524

1525

1526

1527

1528

1529

1530

1531

1532

1533

1534

1535

1536

1537

1538

1539

1540

1541

1542

1543

1544

1545

1546

1547

1548

1549

1550

1551

1552

1553

1554

1555

1556

1557

1558

1559

1560

1561

1562

1563

1564

1565

1566

1567

1568

1569

1570

1571

1572

1573

1574

1575

1576

1577

1578

1579

r""" 

Parking Functions 

 

INFORMALLY (reference [Beck]_): 

 

Imagine a one-way cul-de-sac with `n` parking spots. We will give the 

first parking spot the number 1, the next one number 2, etc., down to 

the last one, number `n`. Initially they are all free, but there are 

`n` cars approaching the street, and they would all like to park there. 

To make life interesting, every car has a parking preference, and we 

record the preferences in a sequence; For example, if `n = 3`, the 

sequence `(2, 1, 1)` means that the first car would like to park at 

spot number 2, the second car prefers parking spot number 1, and the 

last car would also like to part at number 1. The street is very 

narrow, so there is no way to back up. Now each car enters the street 

and approaches its preferred parking spot; if it is free, it parks 

there, and if not, it moves down the street to the first available 

spot. We call a sequence a parking function (of length `n`) if all 

cars end up finding a parking spot. For example, the sequence `(2, 1, 

1)` is a parking sequence (of length 3), whereas the sequence `(2, 3, 

2)` is not. 

 

FORMALLY: 

 

A parking function of size `n` is a sequence `(a_1, \ldots, a_n)` of 

positive integers such that if `b_1 \leq b_2 \leq \cdots \leq b_n` is 

the increasing rearrangement of `a_1, \ldots, a_n`, then `b_i \leq i`. 

 

A parking function of size `n` is a pair `(L, D)` of two sequences `L` 

and `D` where `L` is a permutation and `D` is an area sequence of a 

Dyck path of size n such that `D[i] \geq 0`, `D[i+1] \leq D[i]+1` and 

if `D[i+1] = D[i]+1` then `L[i+1] > L[i]`. 

 

The number of parking functions of size `n` is equal to the number of 

rooted forests on `n` vertices and is equal to `(n+1)^{n-1}`. 

 

REFERENCES: 

 

.. [Beck] \M. Beck, Stanford Math Circle - Parking Functions, October 2010, 

http://math.stanford.edu/circle/parkingBeck.pdf 

 

.. [Hag08] The `q,t` -- Catalan Numbers and the Space of Diagonal Harmonics: 

With an Appendix on the Combinatorics of Macdonald Polynomials, James Haglund, 

University of Pennsylvania, Philadelphia -- AMS, 2008, 167 pp. 

 

.. [Shin] \H. Shin, Forests and Parking Functions, slides from talk September 24, 2008, 

http://www.emis.de/journals/SLC/wpapers/s61vortrag/shin.pdf 

 

.. [GXZ] \A. M. Garsia, G. Xin, M. Zabrocki, A three shuffle case of the 

compositional parking function conjecture, :arxiv:`1208.5796v1` 

 

AUTHORS: 

 

- used non-decreasing_parking_functions code by Florent Hivert (2009 - 04) 

- Dorota Mazur (2012 - 09) 

""" 

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

# Copyright (C) 2012 Dorota Mazur <dorota@yorku.ca> 

# 

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

# as published by the Free Software Foundation; either version 2 of 

# the License, or (at your option) any later version. 

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

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

from six.moves import range 

 

from sage.rings.integer import Integer 

from sage.rings.all import QQ 

from copy import copy 

from sage.combinat.combinat import (CombinatorialClass, CombinatorialObject, 

InfiniteAbstractCombinatorialClass) 

from sage.combinat.permutation import Permutation, Permutations 

from sage.combinat.dyck_word import DyckWord 

from sage.combinat.combinatorial_map import combinatorial_map 

from sage.misc.prandom import randint 

from sage.rings.finite_rings.integer_mod_ring import Zmod 

 

 

def ParkingFunctions(n=None): 

r""" 

Return the combinatorial class of Parking Functions. 

 

A *parking function* of size `n` is a sequence `(a_1, \ldots,a_n)` 

of positive integers such that if `b_1 \leq b_2 \leq \cdots \leq b_n` is 

the increasing rearrangement of `a_1, \ldots, a_n`, then `b_i \leq i`. 

 

A *parking function* of size `n` is a pair `(L, D)` of two sequences 

`L` and `D` where `L` is a permutation and `D` is an area sequence 

of a Dyck Path of size n such that `D[i] \geq 0`, `D[i+1] \leq D[i]+1` 

and if `D[i+1] = D[i]+1` then `L[i+1] > L[i]`. 

 

The number of parking functions of size `n` is equal to the number 

of rooted forests on `n` vertices and is equal to `(n+1)^{n-1}`. 

 

EXAMPLES: 

 

Here are all parking functions of size 3:: 

 

sage: from sage.combinat.parking_functions import ParkingFunctions 

sage: ParkingFunctions(3).list() 

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

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

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

 

If no size is specified, then ParkingFunctions returns the 

combinatorial class of all parking functions. :: 

 

sage: PF = ParkingFunctions(); PF 

Parking functions 

sage: [] in PF 

True 

sage: [1] in PF 

True 

sage: [2] in PF 

False 

sage: [1,3,1] in PF 

True 

sage: [1,4,1] in PF 

False 

 

If the size `n` is specified, then ParkingFunctions returns 

the combinatorial class of all parking functions of size `n`. 

 

:: 

 

sage: PF = ParkingFunctions(0) 

sage: PF.list() 

[[]] 

sage: PF = ParkingFunctions(1) 

sage: PF.list() 

[[1]] 

sage: PF = ParkingFunctions(3) 

sage: PF.list() 

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

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

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

 

:: 

 

sage: PF3 = ParkingFunctions(3); PF3 

Parking functions of size 3 

sage: [] in PF3 

False 

sage: [1] in PF3 

False 

sage: [1,3,1] in PF3 

True 

sage: [1,4,1] in PF3 

False 

 

TESTS:: 

 

sage: PF = ParkingFunctions(5) 

sage: len(PF.list()) == PF.cardinality() 

True 

""" 

if n is None: 

return ParkingFunctions_all() 

 

if not isinstance(n, (Integer, int)) or n < 0: 

raise ValueError("%s is not a non-negative integer." % n) 

return ParkingFunctions_n(n) 

 

 

def is_a(x, n=None): 

r""" 

Check whether a list is a parking function. 

 

If a size `n` is specified, checks if a list is a parking function 

of size `n`. 

 

TESTS:: 

 

sage: from sage.combinat.parking_functions import is_a 

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

True 

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

True 

sage: is_a([1,1,4]) 

False 

sage: is_a([3,1,1], 3) 

True 

""" 

if not isinstance(x, list): # from Florent Hivert non_decreasing_parking_function 

return False 

A = sorted(x) 

from sage.combinat.non_decreasing_parking_function import is_a 

return is_a(A, n) 

 

 

class ParkingFunctions_all(InfiniteAbstractCombinatorialClass): 

def __init__(self): 

""" 

TESTS:: 

 

sage: from sage.combinat.parking_functions import ParkingFunctions 

sage: DW = ParkingFunctions() 

sage: DW == loads(dumps(DW)) 

True 

""" 

pass 

 

def __repr__(self): 

""" 

TESTS:: 

 

sage: repr(ParkingFunctions()) 

'Parking functions' 

""" 

return "Parking functions" 

 

def __contains__(self, x): 

""" 

TESTS:: 

 

sage: [] in ParkingFunctions() 

True 

sage: [1] in ParkingFunctions() 

True 

sage: [2] in ParkingFunctions() 

False 

sage: [1,3,1] in ParkingFunctions() 

True 

sage: [1,4,1] in ParkingFunctions() 

False 

""" 

if isinstance(x, ParkingFunction_class): 

return True 

return is_a(x) 

 

def _infinite_cclass_slice(self, n): 

""" 

Needed by InfiniteAbstractCombinatorialClass to buid __iter__. 

 

TESTS:: 

 

sage: (ParkingFunctions())._infinite_cclass_slice(4) == ParkingFunctions(4) 

True 

sage: it = iter(ParkingFunctions()) # indirect doctest 

sage: [next(it) for i in range(8)] 

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

""" 

return ParkingFunctions_n(n) 

 

 

class ParkingFunctions_n(CombinatorialClass): 

r""" 

The combinatorial class of parking functions of size `n`. 

 

A *parking function* of size `n` is a sequence `(a_1, \ldots,a_n)` 

of positive integers such that if `b_1 \leq b_2 \leq \cdots \leq b_n` is 

the increasing rearrangement of `a_1, \ldots, a_n`, then `b_i \leq i`. 

 

A *parking function* of size `n` is a pair `(L, D)` of two sequences 

`L` and `D` where `L` is a permutation and `D` is an area sequence 

of a Dyck Path of size `n` such that `D[i] \geq 0`, `D[i+1] \leq D[i]+1` 

and if `D[i+1] = D[i]+1` then `L[i+1] > L[i]`. 

 

The number of parking functions of size `n` is equal to the number 

of rooted forests on `n` vertices and is equal to `(n+1)^{n-1}`. 

 

EXAMPLES:: 

 

sage: PF = ParkingFunctions(3) 

sage: PF.list() 

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

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

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

 

sage: [ParkingFunctions(i).cardinality() for i in range(6)] 

[1, 1, 3, 16, 125, 1296] 

 

.. warning:: 

 

The precise order in which the parking function are generated or 

listed is not fixed, and may change in the future. 

""" 

def __init__(self, n): 

""" 

TESTS:: 

 

sage: PF = ParkingFunctions(3) 

sage: PF == loads(dumps(PF)) 

True 

""" 

self.n = n 

 

def __repr__(self): 

""" 

TESTS:: 

 

sage: repr(ParkingFunctions(3)) 

'Parking functions of size 3' 

""" 

return "Parking functions of size %s" % self.n 

 

def __contains__(self, x): 

""" 

TESTS:: 

 

sage: PF3 = ParkingFunctions(3); PF3 

Parking functions of size 3 

sage: [] in PF3 

False 

sage: [1] in PF3 

False 

sage: [1,3,1] in PF3 

True 

sage: [1,1,1] in PF3 

True 

sage: [1,4,1] in PF3 

False 

sage: all(p in PF3 for p in PF3) 

True 

""" 

if isinstance(x, ParkingFunction_class): 

return True 

return is_a(x, self.n) 

 

def cardinality(self): 

r""" 

Return the number of parking functions of size ``n``. 

 

The cardinality is equal to `(n+1)^{n-1}`. 

 

EXAMPLES:: 

 

sage: [ParkingFunctions(i).cardinality() for i in range(6)] 

[1, 1, 3, 16, 125, 1296] 

""" 

return Integer((self.n + 1) ** (self.n - 1)) 

 

def __iter__(self): 

""" 

Return an iterator for parking functions of size `n`. 

 

.. warning:: 

 

The precise order in which the parking function are 

generated is not fixed, and may change in the future. 

 

EXAMPLES:: 

 

sage: PF = ParkingFunctions(0) 

sage: [e for e in PF] # indirect doctest 

[[]] 

sage: PF = ParkingFunctions(1) 

sage: [e for e in PF] # indirect doctest 

[[1]] 

sage: PF = ParkingFunctions(2) 

sage: [e for e in PF] # indirect doctest 

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

sage: PF = ParkingFunctions(3) 

sage: [e for e in PF] # indirect doctest 

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

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

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

 

TESTS:: 

 

sage: PF = ParkingFunctions(5) 

sage: [e for e in PF] == PF.list() 

True 

sage: PF = ParkingFunctions(6) 

sage: [e for e in PF] == PF.list() 

True 

""" 

def iterator_rec(n): 

""" 

TESTS:: 

 

sage: PF = ParkingFunctions(2) 

sage: [e for e in PF] # indirect doctest 

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

""" 

if n == 0: 

yield [] 

return 

if n == 1: 

yield [1] 

return 

for res1 in iterator_rec(n - 1): 

for i in range(res1[-1], n + 1): 

res = copy(res1) 

res.append(i) 

yield res 

return 

for res in iterator_rec(self.n): 

for pi in Permutations(res): 

yield ParkingFunction(list(pi)) 

return 

 

def random_element(self): 

r""" 

Return a random parking function of size `n`. 

 

The algorithm uses a circular parking space with `n+1` 

spots. Then all `n` cars can park and there remains one empty 

spot. Spots are then renumbered so that the empty spot is `0`. 

 

The probability distribution is uniform on the set of 

`(n+1)^{n-1}` parking functions of size `n`. 

 

EXAMPLES:: 

 

sage: pf = ParkingFunctions(8) 

sage: a = pf.random_element(); a # random 

[5, 7, 2, 4, 2, 5, 1, 3] 

sage: a in pf 

True 

""" 

n = self.n 

Zm = Zmod(n + 1) 

fun = [Zm(randint(0, n)) for i in range(n)] 

free = [Zm(j) for j in range(n + 1)] 

for car in fun: 

position = car 

while not(position in free): 

position += Zm.one() 

free.remove(position) 

return ParkingFunction([(i - free[0]).lift() for i in fun]) 

 

 

def ParkingFunction(pf=None, labelling=None, area_sequence=None, 

labelled_dyck_word=None): 

r""" 

Return the combinatorial class of Parking Functions. 

 

A *parking function* of size `n` is a sequence `(a_1, \ldots,a_n)` 

of positive integers such that if `b_1 \leq b_2 \leq \cdots \leq b_n` is 

the increasing rearrangement of `a_1, \ldots, a_n`, then `b_i \leq i`. 

 

A *parking function* of size `n` is a pair `(L, D)` of two sequences 

`L` and `D` where `L` is a permutation and `D` is an area sequence 

of a Dyck Path of size `n` such that `D[i] \geq 0`, `D[i+1] \leq D[i]+1` 

and if `D[i+1] = D[i]+1` then `L[i+1] > L[i]`. 

 

The number of parking functions of size `n` is equal to the number 

of rooted forests on `n` vertices and is equal to `(n+1)^{n-1}`. 

 

INPUT: 

 

- ``pf`` -- (default: None) a list whose increasing rearrangement satisfies `b_i \leq i` 

 

- ``labelling`` -- (default: None) a labelling of the Dyck path 

 

- ``area_sequence`` -- (default: None) an area sequence of a Dyck path 

 

- ``labelled_dyck_word`` -- (default: None) a Dyck word with 1's replaced by labelling 

 

OUTPUT: 

 

- A parking function 

 

EXAMPLES:: 

 

sage: ParkingFunction([]) 

[] 

sage: ParkingFunction([1]) 

[1] 

sage: ParkingFunction([2]) 

Traceback (most recent call last): 

... 

ValueError: [2] is not a parking function. 

sage: ParkingFunction([1,2]) 

[1, 2] 

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

[1, 1, 2] 

sage: ParkingFunction([1,4,1]) 

Traceback (most recent call last): 

... 

ValueError: [1, 4, 1] is not a parking function. 

sage: ParkingFunction(labelling=[3,1,2], area_sequence=[0,0,1]) 

[2, 2, 1] 

sage: ParkingFunction([2,2,1]).to_labelled_dyck_word() 

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

sage: ParkingFunction(labelled_dyck_word = [3,0,1,2,0,0]) 

[2, 2, 1] 

sage: ParkingFunction(labelling=[3,1,2], area_sequence=[0,1,1]) 

Traceback (most recent call last): 

... 

ValueError: [3, 1, 2] is not a valid labeling of area sequence [0, 1, 1] 

""" 

if pf is not None: 

return ParkingFunction_class(pf) 

elif labelling is not None: 

if (area_sequence is None): 

raise ValueError("must also provide area sequence along with labelling.") 

if (len(area_sequence) != len(labelling)): 

raise ValueError("%s must be the same size as the labelling %s" % (area_sequence, labelling)) 

if any(area_sequence[i] < area_sequence[i+1] and labelling[i] > labelling[i + 1] for i in range(len(labelling) - 1)): 

raise ValueError("%s is not a valid labeling of area sequence %s" % (labelling, area_sequence)) 

return from_labelling_and_area_sequence(labelling, area_sequence) 

elif labelled_dyck_word is not None: 

return from_labelled_dyck_word(labelled_dyck_word) 

elif area_sequence is not None: 

DW = DyckWord(area_sequence) 

return ParkingFunction(labelling=list(range(1, DW.size() + 1)), 

area_sequence=DW) 

 

raise ValueError("did not manage to make this into a parking function") 

 

 

class ParkingFunction_class(CombinatorialObject): 

def __init__(self, lst): 

""" 

TESTS:: 

 

sage: ParkingFunction([1, 1, 2, 2, 5, 6]) 

[1, 1, 2, 2, 5, 6] 

""" 

if not is_a(lst): 

raise ValueError("%s is not a parking function." % lst) 

CombinatorialObject.__init__(self, lst) 

 

def __getitem__(self, n): 

""" 

Return the `n^{th}` item in the underlying list. 

 

.. NOTE:: 

 

Note that this is different than the image of ``n`` under 

function. It is "off by one" in that it agrees with sage 

indexing starting at 0. 

 

EXAMPLES:: 

 

sage: PF = ParkingFunction([1, 1, 2, 2, 5, 6]) 

sage: PF[0] 

1 

sage: PF[2] 

2 

""" 

return self._list[n] 

 

def __call__(self, n): 

""" 

Return the image of ``n`` under the parking function. 

 

EXAMPLES:: 

 

sage: PF = ParkingFunction([1, 1, 2, 2, 5, 6]) 

sage: PF(3) 

2 

sage: PF(6) 

6 

""" 

return self._list[n-1] 

 

def diagonal_reading_word(self): 

r""" 

Return a diagonal word of the labelled Dyck path corresponding to parking 

function (see [Hag08]_ p. 75). 

 

INPUT: 

 

- ``self`` -- parking function word 

 

OUTPUT: 

 

- returns a word, read diagonally from NE to SW of the pretty print of the 

labelled Dyck path that corresponds to ``self`` and the same size as ``self`` 

 

EXAMPLES:: 

 

sage: PF = ParkingFunction([6, 1, 5, 2, 2, 1, 5]) 

sage: PF.diagonal_reading_word() 

[5, 1, 7, 4, 6, 3, 2] 

 

:: 

 

sage: ParkingFunction([1, 1, 1]).diagonal_reading_word() 

[3, 2, 1] 

sage: ParkingFunction([1, 2, 3]).diagonal_reading_word() 

[3, 2, 1] 

sage: ParkingFunction([1, 1, 3, 4]).diagonal_reading_word() 

[2, 4, 3, 1] 

 

:: 

 

sage: ParkingFunction([1, 1, 1]).diagonal_word() 

[3, 2, 1] 

sage: ParkingFunction([1, 2, 3]).diagonal_word() 

[3, 2, 1] 

sage: ParkingFunction([1, 4, 3, 1]).diagonal_word() 

[4, 2, 3, 1] 

""" 

L = self.to_labelling_permutation() 

D = self.to_area_sequence() 

m = max(D) 

return Permutation([L[-j - 1] for i in range(m + 1) 

for j in range(len(L)) if D[-j - 1] == m - i]) 

 

diagonal_word = diagonal_reading_word 

 

def parking_permutation(self): # indices are cars, entries are parking spaces 

r""" 

Return the sequence of parking spots that are taken by cars 1 

through `n` and corresponding to the parking function. 

 

For example, ``parking_permutation(PF) = [6, 1, 5, 2, 3, 4, 

7]`` means that spot 6 is taken by car 1, spot 1 by car 2, 

spot 5 by car 3, spot 2 is taken by car 4, spot 3 is taken by 

car 5, spot 4 is taken by car 6 and spot 7 is taken by car 7. 

 

INPUT: 

 

- ``self`` -- parking function word 

 

OUTPUT: 

 

- the permutation of parking spots that corresponds to 

the parking function and which is the same size as parking 

function 

 

EXAMPLES:: 

 

sage: PF = ParkingFunction([6, 1, 5, 2, 2, 1, 5]) 

sage: PF.parking_permutation() 

[6, 1, 5, 2, 3, 4, 7] 

 

:: 

 

sage: ParkingFunction([3,1,1,4]).parking_permutation() 

[3, 1, 2, 4] 

sage: ParkingFunction([4,1,1,1]).parking_permutation() 

[4, 1, 2, 3] 

sage: ParkingFunction([2,1,4,1]).parking_permutation() 

[2, 1, 4, 3] 

""" 

return self.cars_permutation().inverse() 

 

@combinatorial_map(name='to car permutation') 

def cars_permutation(self): # indices are parking spaces, entries are car labels 

r""" 

Return the sequence of cars that take parking spots 1 through `n` 

and corresponding to the parking function. 

 

For example, ``cars_permutation(PF) = [2, 4, 5, 6, 3, 1, 7]`` 

means that car 2 takes spots 1, car 4 takes spot 2, ..., car 1 takes spot 6 and 

car 7 takes spot 7. 

 

INPUT: 

 

- ``self`` -- parking function word 

 

OUTPUT: 

 

- the permutation of cars corresponding to the parking function 

and which is the same size as parking function 

 

EXAMPLES:: 

 

sage: PF = ParkingFunction([6, 1, 5, 2, 2, 1, 5]) 

sage: PF.cars_permutation() 

[2, 4, 5, 6, 3, 1, 7] 

 

:: 

 

sage: ParkingFunction([3,1,1,4]).cars_permutation() 

[2, 3, 1, 4] 

sage: ParkingFunction([4,1,1,1]).cars_permutation() 

[2, 3, 4, 1] 

sage: ParkingFunction([2,1,4,1]).cars_permutation() 

[2, 1, 4, 3] 

""" 

out = {} 

for i in range(len(self)): 

j = 0 

while self[i] + j in out: 

j += 1 

out[self[i] + j] = i 

return Permutation([out[i + 1] + 1 for i in range(len(self))]) 

 

def jump_list(self): # cars displacements 

r""" 

Return the displacements of cars that corresponds to the parking function. 

 

For example, ``jump_list(PF) = [0, 0, 0, 0, 1, 3, 2]`` 

means that car 1 through 4 parked in their preferred spots, 

car 5 had to park one spot farther (jumped or was displaced by one spot), 

car 6 had to jump 3 spots, and car 7 had to jump two spots. 

 

INPUT: 

 

- ``self`` -- parking function word 

 

OUTPUT: 

 

- the displacements sequence of parked cars which corresponds 

to the parking function and which is the same size as parking function 

 

EXAMPLES:: 

 

sage: PF = ParkingFunction([6, 1, 5, 2, 2, 1, 5]) 

sage: PF.jump_list() 

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

 

:: 

 

sage: ParkingFunction([3,1,1,4]).jump_list() 

[0, 0, 1, 0] 

sage: ParkingFunction([4,1,1,1]).jump_list() 

[0, 0, 1, 2] 

sage: ParkingFunction([2,1,4,1]).jump_list() 

[0, 0, 0, 2] 

""" 

out = [] 

pi = self.parking_permutation() 

for i in range(len(self)): 

out.append(pi[i] - self[i]) 

return out 

 

def jump(self): # sum of all jumps, sum of all displacements 

r""" 

Return the sum of the differences between the parked and 

preferred parking spots. 

 

See [Shin]_ p. 18. 

 

INPUT: 

 

- ``self`` -- a parking function word 

 

OUTPUT: 

 

- the sum of the differences between the parked and preferred parking 

spots 

 

EXAMPLES:: 

 

sage: PF = ParkingFunction([6, 1, 5, 2, 2, 1, 5]) 

sage: PF.jump() 

6 

 

:: 

 

sage: ParkingFunction([3,1,1,4]).jump() 

1 

sage: ParkingFunction([4,1,1,1]).jump() 

3 

sage: ParkingFunction([2,1,4,1]).jump() 

2 

""" 

return sum(self.jump_list()) 

 

def lucky_cars(self): # the set of cars that can park in their preferred spots 

r""" 

Return the cars that can park in their preferred spots. For example, 

``lucky_cars(PF) = [1, 2, 7]`` means that cars 1, 2 and 7 parked in their 

preferred spots and all the other cars did not. 

 

INPUT: 

 

- ``self`` -- parking function word 

 

OUTPUT: 

 

- the cars that can park in their preferred spots 

 

EXAMPLES:: 

 

sage: PF = ParkingFunction([6, 1, 5, 2, 2, 1, 5]) 

sage: PF.lucky_cars() 

[1, 2, 3, 4] 

 

:: 

 

sage: ParkingFunction([3,1,1,4]).lucky_cars() 

[1, 2, 4] 

sage: ParkingFunction([4,1,1,1]).lucky_cars() 

[1, 2] 

sage: ParkingFunction([2,1,4,1]).lucky_cars() 

[1, 2, 3] 

""" 

w = self.jump_list() 

return [i + 1 for i in range(len(w)) if w[i] == 0] 

 

def luck(self): # the number of lucky cars 

r""" 

Return the number of cars that parked in their preferred parking spots 

(see [Shin]_ p. 33). 

 

INPUT: 

 

- ``self`` -- parking function word 

 

OUTPUT: 

 

- the number of cars that parked in their preferred parking spots 

 

EXAMPLES:: 

 

sage: PF = ParkingFunction([6, 1, 5, 2, 2, 1, 5]) 

sage: PF.luck() 

4 

 

:: 

 

sage: ParkingFunction([3,1,1,4]).luck() 

3 

sage: ParkingFunction([4,1,1,1]).luck() 

2 

sage: ParkingFunction([2,1,4,1]).luck() 

3 

""" 

return len(self.lucky_cars()) 

 

def primary_dinversion_pairs(self): 

r""" 

Return the primary descent inversion pairs of a labelled Dyck path corresponding 

to the parking function. 

 

INPUT: 

 

- ``self`` -- parking function word 

 

OUTPUT: 

 

- the pairs `(i, j)` such that `i < j`, and `i^{th}` area = `j^{th}` area, 

and `i^{th}` label < `j^{th}` label 

 

EXAMPLES:: 

 

sage: PF = ParkingFunction([6, 1, 5, 2, 2, 1, 5]) 

sage: PF.primary_dinversion_pairs() 

[(0, 4), (1, 5), (2, 5)] 

 

:: 

 

sage: ParkingFunction([3,1,1,4]).primary_dinversion_pairs() 

[(0, 3), (2, 3)] 

sage: ParkingFunction([4,1,1,1]).primary_dinversion_pairs() 

[] 

sage: ParkingFunction([2,1,4,1]).primary_dinversion_pairs() 

[(0, 3)] 

""" 

L = self.to_labelling_permutation() 

D = self.to_area_sequence() 

return [(i, j) for j in range(len(D)) for i in range(j) 

if D[i] == D[j] and L[i] < L[j]] 

 

def secondary_dinversion_pairs(self): 

r""" 

Return the secondary descent inversion pairs of a labelled Dyck path 

corresponding to the parking function. 

 

INPUT: 

 

- ``self`` -- parking function word 

 

OUTPUT: 

 

- the pairs `(i, j)` such that `i < j`, and `i^{th}` area = `j^{th}` area +1, 

and `i^{th}` label > `j^{th}` label 

 

EXAMPLES:: 

 

sage: PF = ParkingFunction([6, 1, 5, 2, 2, 1, 5]) 

sage: PF.secondary_dinversion_pairs() 

[(1, 4), (2, 4), (3, 6)] 

 

:: 

 

sage: ParkingFunction([3,1,1,4]).secondary_dinversion_pairs() 

[(1, 2)] 

sage: ParkingFunction([4,1,1,1]).secondary_dinversion_pairs() 

[(1, 3)] 

sage: ParkingFunction([2,1,4,1]).secondary_dinversion_pairs() 

[(1, 3)] 

""" 

L = self.to_labelling_permutation() 

D = self.to_area_sequence() 

return [(i, j) for j in range(len(D)) for i in range(j) 

if D[i] == D[j] + 1 and L[i] > L[j]] 

 

def dinversion_pairs(self): 

r""" 

Return the descent inversion pairs of a labelled Dyck path 

corresponding to the parking function. 

 

INPUT: 

 

- ``self`` -- parking function word 

 

OUTPUT: 

 

- the primary and secondary diversion pairs 

 

EXAMPLES:: 

 

sage: PF = ParkingFunction([6, 1, 5, 2, 2, 1, 5]) 

sage: PF.dinversion_pairs() 

[(0, 4), (1, 5), (2, 5), (1, 4), (2, 4), (3, 6)] 

 

:: 

 

sage: ParkingFunction([3,1,1,4]).dinversion_pairs() 

[(0, 3), (2, 3), (1, 2)] 

sage: ParkingFunction([4,1,1,1]).dinversion_pairs() 

[(1, 3)] 

sage: ParkingFunction([2,1,4,1]).dinversion_pairs() 

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

""" 

return self.primary_dinversion_pairs() + self.secondary_dinversion_pairs() 

 

def dinv(self): 

r""" 

Return the number of inversions of a labelled Dyck path corresponding 

to the parking function (see [Hag08]_ p. 74). 

 

Same as the cardinality of :meth:`dinversion_pairs`. 

 

INPUT: 

 

- ``self`` -- parking function word 

 

OUTPUT: 

 

- the number of dinversion pairs 

 

EXAMPLES:: 

 

sage: PF = ParkingFunction([6, 1, 5, 2, 2, 1, 5]) 

sage: PF.dinv() 

6 

 

:: 

 

sage: ParkingFunction([3,1,1,4]).dinv() 

3 

sage: ParkingFunction([4,1,1,1]).dinv() 

1 

sage: ParkingFunction([2,1,4,1]).dinv() 

2 

""" 

return len(self.dinversion_pairs()) 

 

def area(self): 

r""" 

Return the area of the labelled Dyck path corresponding to the 

parking function. 

 

INPUT: 

 

- ``self`` -- parking function word 

 

OUTPUT: 

 

- the sum of squares under and over the main diagonal the Dyck Path, 

corresponding to the parking function 

 

EXAMPLES:: 

 

sage: PF = ParkingFunction([6, 1, 5, 2, 2, 1, 5]) 

sage: PF.area() 

6 

 

:: 

 

sage: ParkingFunction([3,1,1,4]).area() 

1 

sage: ParkingFunction([4,1,1,1]).area() 

3 

sage: ParkingFunction([2,1,4,1]).area() 

2 

""" 

return sum(self.to_area_sequence()) 

 

@combinatorial_map(name='to ides composition') 

def ides_composition(self): 

r""" 

Return the 

:meth:`~sage.combinat.permutation.Permutation.descents_composition` 

of the inverse of the :meth:`diagonal_reading_word` of 

corresponding parking function. 

 

For example, ``ides_composition(PF) = [4, 2, 1]`` means that 

the descents of the inverse of the permutation 

:meth:`diagonal_reading_word` of the parking function with 

word ``PF`` are at the 4th and 6th positions. 

 

INPUT: 

 

- ``self`` -- parking function word 

 

OUTPUT: 

 

- the descents composition of the inverse of the 

:meth:`diagonal_reading_word` of the parking function 

 

EXAMPLES:: 

 

sage: PF = ParkingFunction([6, 1, 5, 2, 2, 1, 5]) 

sage: PF.ides_composition() 

[2, 1, 1, 2, 1] 

 

:: 

 

sage: ParkingFunction([3,1,1,4]).ides_composition() 

[2, 2] 

sage: ParkingFunction([4,1,1,1]).ides_composition() 

[2, 1, 1] 

sage: ParkingFunction([4,3,1,1]).ides_composition() 

[3, 1] 

""" 

return self.diagonal_reading_word().inverse().descents_composition() 

 

def ides(self): 

r""" 

Return the :meth:`~sage.combinat.permutation.Permutation.descents` sequence 

of the inverse of the :meth:`diagonal_reading_word` of ``self``. 

 

.. WARNING:: 

 

Here we use the standard convention that descent labels 

start at `1`. This behaviour has been changed in 

:trac:`20555`. 

 

For example, ``ides(PF) = [2, 3, 4, 6]`` means that descents are at the 2nd, 3rd, 

4th and 6th positions in the inverse of the 

:meth:`diagonal_reading_word` of the parking function (see [GXZ]_ p. 2). 

 

INPUT: 

 

- ``self`` -- parking function word 

 

OUTPUT: 

 

- the descents sequence of the inverse of the 

:meth:`diagonal_reading_word` of the parking function 

 

EXAMPLES:: 

 

sage: PF = ParkingFunction([6, 1, 5, 2, 2, 1, 5]) 

sage: PF.ides() 

[2, 3, 4, 6] 

 

:: 

 

sage: ParkingFunction([3,1,1,4]).ides() 

[2] 

sage: ParkingFunction([4,1,1,1]).ides() 

[2, 3] 

sage: ParkingFunction([4,3,1,1]).ides() 

[3] 

""" 

return self.diagonal_reading_word().inverse().descents() 

 

def touch_points(self): 

r""" 

Return the sequence of touch points which corresponds to the labelled Dyck path 

after initial step. 

 

For example, ``touch_points(PF) = [4, 7]`` means that after 

the initial step, the path touches the main diagonal at points 

`(4, 4)` and `(7, 7)`. 

 

INPUT: 

 

- ``self`` -- parking function word 

 

OUTPUT: 

 

- the sequence of touch points after the initial step of the 

labelled Dyck path that corresponds to the parking function 

 

EXAMPLES:: 

 

sage: PF = ParkingFunction([6, 1, 5, 2, 2, 1, 5]) 

sage: PF.touch_points() 

[4, 7] 

 

:: 

 

sage: ParkingFunction([3,1,1,4]).touch_points() 

[2, 3, 4] 

sage: ParkingFunction([4,1,1,1]).touch_points() 

[3, 4] 

sage: ParkingFunction([2,1,4,1]).touch_points() 

[3, 4] 

""" 

return self.to_dyck_word().touch_points() 

 

@combinatorial_map(name='to touch composition') 

def touch_composition(self): 

r""" 

Return the composition of the labelled Dyck path corresponding 

to the parking function. 

 

For example, ``touch_composition(PF) = [4, 3]`` means that the 

first touch is four diagonal units from the starting point, 

and the second is three units further (see [GXZ]_ p. 2). 

 

INPUT: 

 

- ``self`` -- parking function word 

 

OUTPUT: 

 

- the length between the corresponding touch points which 

of the labelled Dyck path that corresponds to the parking function 

 

EXAMPLES:: 

 

sage: PF = ParkingFunction([6, 1, 5, 2, 2, 1, 5]) 

sage: PF.touch_composition() 

[4, 3] 

 

:: 

 

sage: ParkingFunction([3,1,1,4]).touch_composition() 

[2, 1, 1] 

sage: ParkingFunction([4,1,1,1]).touch_composition() 

[3, 1] 

sage: ParkingFunction([2,1,4,1]).touch_composition() 

[3, 1] 

""" 

return self.to_dyck_word().touch_composition() 

 

diagonal_composition = touch_composition 

 

@combinatorial_map(name='to labelling permutation') 

def to_labelling_permutation(self): 

r""" 

Return the labelling of the support Dyck path of the parking function. 

 

INPUT: 

 

- ``self`` -- parking function word 

 

OUTPUT: 

 

- the labelling of the Dyck path 

 

EXAMPLES:: 

 

sage: PF = ParkingFunction([6, 1, 5, 2, 2, 1, 5]) 

sage: PF.to_labelling_permutation() 

[2, 6, 4, 5, 3, 7, 1] 

 

:: 

 

sage: ParkingFunction([3,1,1,4]).to_labelling_permutation() 

[2, 3, 1, 4] 

sage: ParkingFunction([4,1,1,1]).to_labelling_permutation() 

[2, 3, 4, 1] 

sage: ParkingFunction([2,1,4,1]).to_labelling_permutation() 

[2, 4, 1, 3] 

""" 

from sage.combinat.words.word import Word 

return Word(self).standard_permutation().inverse() 

 

def to_area_sequence(self): 

r""" 

Return the area sequence of the support Dyck path of the 

parking function. 

 

INPUT: 

 

- ``self`` -- parking function word 

 

OUTPUT: 

 

- the area sequence of the Dyck path 

 

EXAMPLES:: 

 

sage: PF = ParkingFunction([6, 1, 5, 2, 2, 1, 5]) 

sage: PF.to_area_sequence() 

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

 

:: 

 

sage: ParkingFunction([3,1,1,4]).to_area_sequence() 

[0, 1, 0, 0] 

sage: ParkingFunction([4,1,1,1]).to_area_sequence() 

[0, 1, 2, 0] 

sage: ParkingFunction([2,1,4,1]).to_area_sequence() 

[0, 1, 1, 0] 

""" 

w = sorted(self) 

return [i+1-w[i] for i in range(len(w))] 

 

def to_labelling_area_sequence_pair(self): 

r""" 

Return a pair consisting of a labelling and an area sequence 

of a Dyck path which corresponds to the given parking 

function. 

 

INPUT: 

 

- ``self`` -- the parking function word 

 

OUTPUT: 

 

- returns a pair ``(L, D)`` where ``L`` is a labelling and ``D`` is the 

area sequence of the underlying Dyck path 

 

EXAMPLES:: 

 

sage: PF = ParkingFunction([6, 1, 5, 2, 2, 1, 5]) 

sage: PF.to_labelling_area_sequence_pair() 

([2, 6, 4, 5, 3, 7, 1], [0, 1, 1, 2, 0, 1, 1]) 

 

:: 

 

sage: ParkingFunction([1, 1, 1]).to_labelling_area_sequence_pair() 

([1, 2, 3], [0, 1, 2]) 

sage: ParkingFunction([1, 2, 3]).to_labelling_area_sequence_pair() 

([1, 2, 3], [0, 0, 0]) 

sage: ParkingFunction([1, 1, 2]).to_labelling_area_sequence_pair() 

([1, 2, 3], [0, 1, 1]) 

sage: ParkingFunction([1, 1, 3, 1]).to_labelling_area_sequence_pair() 

([1, 2, 4, 3], [0, 1, 2, 1]) 

""" 

return (self.to_labelling_permutation(), self.to_area_sequence()) 

 

@combinatorial_map(name='to dyck word') 

def to_dyck_word(self): 

r""" 

Return the support Dyck word of the parking function. 

 

INPUT: 

 

- ``self`` -- parking function word 

 

OUTPUT: 

 

- the Dyck word of the corresponding parking function 

 

.. SEEALSO:: :meth:`DyckWord` 

 

EXAMPLES:: 

 

sage: PF = ParkingFunction([6, 1, 5, 2, 2, 1, 5]) 

sage: PF.to_dyck_word() 

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

 

:: 

 

sage: ParkingFunction([3,1,1,4]).to_dyck_word() 

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

sage: ParkingFunction([4,1,1,1]).to_dyck_word() 

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

sage: ParkingFunction([2,1,4,1]).to_dyck_word() 

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

""" 

return DyckWord(area_sequence=self.to_area_sequence()) 

 

def to_labelled_dyck_word(self): 

r""" 

Return the labelled Dyck word corresponding to the parking function. 

 

This is a representation of the parking function as a list 

where the entries of 1 in the Dyck word are replaced with the 

corresponding label. 

 

INPUT: 

 

- ``self`` -- parking function word 

 

OUTPUT: 

 

- the labelled Dyck word of the corresponding parking function 

which is twice the size of parking function word 

 

EXAMPLES:: 

 

sage: PF = ParkingFunction([6, 1, 5, 2, 2, 1, 5]) 

sage: PF.to_labelled_dyck_word() 

[2, 6, 0, 4, 5, 0, 0, 0, 3, 7, 0, 1, 0, 0] 

 

:: 

 

sage: ParkingFunction([3,1,1,4]).to_labelled_dyck_word() 

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

sage: ParkingFunction([4,1,1,1]).to_labelled_dyck_word() 

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

sage: ParkingFunction([2,1,4,1]).to_labelled_dyck_word() 

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

""" 

dw = self.to_dyck_word() 

out = list(copy(self.to_labelling_permutation())) 

for i in range(2 * len(out)): 

if dw[i] == 0: 

out.insert(i, 0) 

return out 

 

def to_labelling_dyck_word_pair(self): 

r""" 

Return the pair ``(L, D)`` where ``L`` is a labelling and 

``D`` is the Dyck word of the parking function. 

 

INPUT: 

 

- ``self`` -- parking function word 

 

OUTPUT: 

 

- the pair ``(L, D)``, where ``L`` is the labelling and ``D`` is 

the Dyck word of the parking function 

 

.. SEEALSO:: :meth:`DyckWord` 

 

EXAMPLES:: 

 

sage: PF = ParkingFunction([6, 1, 5, 2, 2, 1, 5]) 

sage: PF.to_labelling_dyck_word_pair() 

([2, 6, 4, 5, 3, 7, 1], [1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0]) 

 

:: 

 

sage: ParkingFunction([3,1,1,4]).to_labelling_dyck_word_pair() 

([2, 3, 1, 4], [1, 1, 0, 0, 1, 0, 1, 0]) 

sage: ParkingFunction([4,1,1,1]).to_labelling_dyck_word_pair() 

([2, 3, 4, 1], [1, 1, 1, 0, 0, 0, 1, 0]) 

sage: ParkingFunction([2,1,4,1]).to_labelling_dyck_word_pair() 

([2, 4, 1, 3], [1, 1, 0, 1, 0, 0, 1, 0]) 

""" 

return (self.to_labelling_permutation(), self.to_dyck_word()) 

 

@combinatorial_map(name='to non-decreasing parking function') 

def to_NonDecreasingParkingFunction(self): 

r""" 

Return the non-decreasing parking function which underlies the 

parking function. 

 

INPUT: 

 

- ``self`` -- parking function word 

 

OUTPUT: 

 

- a sorted parking function 

 

.. SEEALSO:: :meth:`NonDecreasingParkingFunction` 

 

EXAMPLES:: 

 

sage: PF = ParkingFunction([6, 1, 5, 2, 2, 1, 5]) 

sage: PF.to_NonDecreasingParkingFunction() 

[1, 1, 2, 2, 5, 5, 6] 

 

:: 

 

sage: ParkingFunction([3,1,1,4]).to_NonDecreasingParkingFunction() 

[1, 1, 3, 4] 

sage: ParkingFunction([4,1,1,1]).to_NonDecreasingParkingFunction() 

[1, 1, 1, 4] 

sage: ParkingFunction([2,1,4,1]).to_NonDecreasingParkingFunction() 

[1, 1, 2, 4] 

sage: ParkingFunction([4,1,2,1]).to_NonDecreasingParkingFunction() 

[1, 1, 2, 4] 

""" 

return ParkingFunction(sorted(self)) 

 

def characteristic_quasisymmetric_function(self, q=None, 

R=QQ['q', 't'].fraction_field()): 

r""" 

Return the characteristic quasisymmetric function of ``self``. 

 

The characteristic function of the Parking Function is the sum 

over all permutation labellings of the Dyck path `q^{dinv(PF)} 

F_{ides(PF)}` where `ides(PF)` (:meth:`ides_composition`) is 

the descent composition of diagonal reading word of the 

parking function. 

 

INPUT: 

 

- ``q`` -- (default: ``q = R('q')``) a parameter for the 

generating function power 

 

- ``R`` -- (default: ``R = QQ['q','t'].fraction_field()``) the 

base ring to do the calculations over 

 

OUTPUT: 

 

- an element of the quasisymmetric functions over the ring ``R`` 

 

EXAMPLES:: 

 

sage: R = QQ['q','t'].fraction_field() 

sage: (q,t) = R.gens() 

sage: cqf = sum(t**PF.area()*PF.characteristic_quasisymmetric_function() for PF in ParkingFunctions(3)); cqf 

(q^3+q^2*t+q*t^2+t^3+q*t)*F[1, 1, 1] + (q^2+q*t+t^2+q+t)*F[1, 2] + (q^2+q*t+t^2+q+t)*F[2, 1] + F[3] 

sage: s = SymmetricFunctions(R).s() 

sage: s(cqf.to_symmetric_function()) 

(q^3+q^2*t+q*t^2+t^3+q*t)*s[1, 1, 1] + (q^2+q*t+t^2+q+t)*s[2, 1] + s[3] 

sage: s(cqf.to_symmetric_function()).nabla(power = -1) 

s[1, 1, 1] 

 

:: 

 

sage: p = ParkingFunction([3, 1, 2]) 

sage: p.characteristic_quasisymmetric_function() 

q*F[2, 1] 

sage: pf = ParkingFunction([1,2,7,2,1,2,3,2,1]) 

sage: pf.characteristic_quasisymmetric_function() 

q^2*F[1, 1, 1, 2, 1, 3] 

""" 

from sage.combinat.ncsf_qsym.qsym import QuasiSymmetricFunctions 

if q is None: 

q = R('q') 

else: 

if not q in R: 

raise ValueError("q=%s must be an element of the base ring %s" % (q, R)) 

F = QuasiSymmetricFunctions(R).Fundamental() 

return q ** self.dinv() * F(self.ides_composition()) 

 

def pretty_print(self, underpath=True): 

r""" 

Displays a parking function as a lattice path consisting of a 

Dyck path and a labelling with the labels displayed along the 

edges of the Dyck path. 

 

INPUT: 

 

- ``underpath`` -- if the length of the parking function is 

less than or equal to 9 then display the labels under the 

path if ``underpath`` is True otherwise display them to the 

right of the path (default: ``True``) 

 

EXAMPLES:: 

 

sage: PF = ParkingFunction([6, 1, 5, 2, 2, 1, 5]) 

sage: PF.pretty_print() 

___ 

_|1x 

|7x . 

_____|3 . . 

|5x x . . . 

_|4x . . . . 

|6x . . . . . 

|2 . . . . . . 

 

sage: PF = ParkingFunction([6, 1, 5, 2, 2, 1, 5]) 

sage: PF.pretty_print(underpath = false) 

___ 

_| x 1 

| x . 7 

_____| . . 3 

| x x . . . 5 

_| x . . . . 4 

| x . . . . . 6 

| . . . . . . 2 

 

:: 

 

sage: ParkingFunction([3, 1, 1, 4]).pretty_print() 

_ 

_|4 

___|1 . 

|3x . . 

|2 . . . 

 

sage: ParkingFunction([1,1,1]).pretty_print() 

_____ 

|3x x 

|2x . 

|1 . . 

 

sage: ParkingFunction([4,1,1,1]).pretty_print() 

_ 

_____|1 

|4x x . 

|3x . . 

|2 . . . 

 

sage: ParkingFunction([2,1,4,1]).pretty_print() 

_ 

___|3 

_|1x . 

|4x . . 

|2 . . . 

 

sage: ParkingFunction([2,1,4,1]).pretty_print(underpath = false) 

_ 

___| 3 

_| x . 1 

| x . . 4 

| . . . 2 

 

sage: pf = ParkingFunction([1,2,3,7,3,2,1,2,3,2,1]) 

sage: pf.pretty_print() 

_________ 

_______| x x x x 4 

| x x x x x x x . 9 

| x x x x x x . . 5 

_| x x x x x . . . 3 

| x x x x x . . . . 10 

| x x x x . . . . . 8 

| x x x . . . . . . 6 

_| x x . . . . . . . 2 

| x x . . . . . . . . 11 

| x . . . . . . . . . 7 

| . . . . . . . . . . 1 

""" 

L = self.to_labelling_permutation() 

dw = self.to_dyck_word() 

if len(L) <= 9: 

dw.pretty_print(labelling=L, underpath=underpath) 

else: 

dw.pretty_print(labelling=L, underpath=False) 

 

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

# CONSTRUCTIONS 

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

 

 

def from_labelling_and_area_sequence(L, D): 

r""" 

Return the parking function corresponding to the labelling area 

sequence pair. 

 

INPUT: 

 

- ``L`` -- a labelling permutation 

 

- ``D`` -- an area sequence for a Dyck word 

 

OUTPUT: 

 

- the parking function corresponding the labelling permutation ``L`` 

and ``D`` an area sequence of the corresponding Dyck path 

 

EXAMPLES:: 

 

sage: from sage.combinat.parking_functions import from_labelling_and_area_sequence 

sage: from_labelling_and_area_sequence([2, 6, 4, 5, 3, 7, 1], [0, 1, 1, 2, 0, 1, 1]) 

[6, 1, 5, 2, 2, 1, 5] 

 

:: 

 

sage: from_labelling_and_area_sequence([1, 2, 3], [0, 1, 2]) 

[1, 1, 1] 

sage: from_labelling_and_area_sequence([1, 2, 3], [0, 0, 0]) 

[1, 2, 3] 

sage: from_labelling_and_area_sequence([1, 2, 3], [0, 1, 1]) 

[1, 1, 2] 

sage: from_labelling_and_area_sequence([1, 2, 4, 3], [0, 1, 2, 1]) 

[1, 1, 3, 1] 

""" 

return ParkingFunction_class([L.index(i) + 1 - D[L.index(i)] 

for i in range(1, len(L) + 1)]) 

 

 

def from_labelled_dyck_word(LDW): 

r""" 

Return the parking function corresponding to the labelled Dyck word. 

 

INPUT: 

 

- ``LDW`` -- labelled Dyck word 

 

OUTPUT: 

 

- the parking function corresponding to the labelled Dyck 

word that is half the size of ``LDW`` 

 

EXAMPLES:: 

 

sage: from sage.combinat.parking_functions import from_labelled_dyck_word 

sage: LDW = [2, 6, 0, 4, 5, 0, 0, 0, 3, 7, 0, 1, 0, 0] 

sage: from_labelled_dyck_word(LDW) 

[6, 1, 5, 2, 2, 1, 5] 

 

:: 

 

sage: from_labelled_dyck_word([2, 3, 0, 0, 1, 0, 4, 0]) 

[3, 1, 1, 4] 

sage: from_labelled_dyck_word([2, 3, 4, 0, 0, 0, 1, 0]) 

[4, 1, 1, 1] 

sage: from_labelled_dyck_word([2, 4, 0, 1, 0, 0, 3, 0]) 

[2, 1, 4, 1] 

""" 

L = [ell for ell in LDW if ell != 0] 

D = DyckWord([Integer(not x.is_zero()) for x in LDW]) 

return from_labelling_and_area_sequence(L, D.to_area_sequence())