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

1580

1581

1582

1583

1584

1585

1586

1587

1588

1589

1590

1591

1592

1593

1594

1595

1596

1597

1598

1599

1600

1601

1602

1603

1604

1605

1606

1607

1608

1609

1610

1611

1612

1613

1614

1615

1616

1617

1618

1619

1620

1621

1622

1623

1624

1625

1626

1627

1628

1629

1630

1631

1632

1633

1634

1635

1636

1637

1638

1639

1640

1641

1642

1643

1644

1645

1646

1647

1648

1649

1650

1651

1652

1653

1654

1655

1656

1657

1658

1659

1660

1661

1662

1663

1664

1665

1666

1667

1668

1669

1670

1671

1672

1673

1674

1675

1676

1677

1678

1679

1680

1681

1682

1683

1684

1685

1686

1687

1688

1689

1690

1691

1692

1693

1694

1695

1696

1697

1698

1699

1700

1701

1702

1703

1704

1705

1706

1707

1708

1709

1710

1711

1712

1713

1714

1715

1716

1717

1718

1719

1720

1721

1722

1723

1724

1725

1726

1727

1728

1729

1730

1731

1732

1733

1734

1735

1736

1737

1738

1739

1740

1741

1742

1743

1744

1745

1746

1747

1748

1749

1750

1751

1752

1753

1754

1755

1756

1757

1758

1759

1760

1761

1762

1763

1764

1765

1766

1767

1768

1769

1770

1771

1772

1773

1774

1775

1776

1777

1778

1779

1780

1781

1782

1783

1784

1785

1786

1787

1788

1789

1790

1791

1792

1793

1794

1795

1796

1797

1798

1799

1800

1801

1802

1803

1804

1805

1806

1807

1808

1809

1810

1811

1812

1813

1814

1815

1816

1817

1818

1819

1820

1821

1822

1823

1824

1825

1826

1827

1828

1829

1830

1831

1832

1833

1834

1835

1836

1837

1838

1839

1840

1841

1842

1843

1844

1845

1846

1847

1848

1849

1850

1851

1852

1853

1854

1855

1856

1857

1858

1859

1860

1861

1862

1863

1864

1865

1866

1867

1868

1869

1870

1871

1872

1873

1874

1875

1876

1877

1878

1879

1880

1881

1882

1883

1884

1885

1886

1887

1888

1889

1890

1891

1892

1893

1894

1895

1896

1897

1898

1899

1900

1901

1902

1903

1904

1905

1906

1907

1908

1909

1910

1911

1912

1913

1914

1915

1916

1917

1918

1919

1920

1921

1922

1923

1924

1925

1926

1927

1928

1929

1930

1931

1932

1933

1934

1935

1936

1937

1938

1939

1940

1941

1942

1943

1944

1945

1946

1947

1948

1949

1950

1951

1952

1953

1954

1955

1956

1957

1958

1959

1960

1961

1962

1963

1964

1965

1966

1967

1968

1969

1970

1971

1972

1973

1974

1975

1976

1977

1978

1979

1980

1981

1982

1983

1984

1985

1986

1987

1988

1989

1990

1991

1992

1993

1994

1995

1996

1997

1998

1999

2000

2001

2002

2003

2004

2005

2006

2007

2008

2009

2010

2011

2012

2013

2014

2015

2016

2017

2018

2019

2020

2021

2022

2023

2024

2025

2026

2027

2028

2029

2030

2031

2032

2033

2034

2035

2036

2037

2038

2039

2040

2041

2042

2043

2044

2045

2046

2047

2048

2049

2050

2051

2052

2053

2054

2055

2056

2057

2058

2059

2060

2061

2062

2063

2064

2065

2066

2067

2068

2069

2070

2071

2072

2073

2074

2075

2076

2077

2078

2079

2080

2081

2082

2083

2084

2085

2086

2087

2088

2089

2090

2091

2092

2093

2094

2095

2096

2097

2098

2099

2100

2101

2102

2103

2104

2105

2106

2107

2108

2109

2110

2111

2112

2113

2114

2115

2116

2117

2118

2119

2120

2121

2122

2123

2124

2125

2126

2127

2128

2129

2130

2131

2132

2133

2134

2135

2136

2137

2138

2139

2140

2141

2142

2143

2144

2145

2146

2147

2148

2149

2150

2151

2152

2153

2154

2155

2156

2157

2158

2159

2160

2161

2162

2163

2164

2165

2166

2167

2168

2169

2170

2171

2172

2173

2174

2175

2176

2177

2178

2179

2180

2181

2182

2183

2184

2185

2186

2187

2188

2189

2190

2191

2192

2193

2194

2195

2196

2197

2198

2199

2200

2201

2202

2203

2204

2205

2206

2207

2208

2209

2210

2211

2212

2213

2214

2215

2216

2217

2218

2219

2220

2221

2222

2223

2224

2225

2226

2227

2228

2229

2230

2231

2232

2233

2234

2235

2236

2237

2238

2239

2240

2241

2242

2243

2244

2245

2246

2247

2248

2249

2250

2251

2252

2253

2254

2255

2256

2257

2258

2259

2260

2261

2262

2263

2264

2265

2266

2267

2268

2269

2270

2271

2272

2273

2274

2275

2276

2277

2278

2279

2280

2281

2282

2283

2284

2285

2286

2287

2288

2289

2290

2291

2292

2293

2294

2295

2296

2297

2298

2299

2300

2301

2302

2303

2304

2305

2306

2307

2308

2309

2310

2311

2312

2313

2314

2315

2316

2317

2318

2319

2320

2321

2322

2323

2324

2325

2326

2327

2328

2329

2330

2331

2332

2333

2334

2335

2336

2337

2338

2339

2340

2341

2342

2343

2344

2345

2346

2347

2348

2349

2350

2351

2352

2353

2354

2355

2356

2357

2358

2359

2360

2361

2362

2363

2364

2365

2366

2367

2368

2369

2370

2371

2372

2373

2374

2375

2376

2377

2378

2379

2380

2381

2382

2383

2384

2385

2386

2387

2388

2389

2390

2391

2392

2393

2394

2395

2396

2397

2398

2399

2400

2401

2402

2403

2404

2405

2406

2407

2408

2409

2410

2411

2412

2413

2414

2415

2416

2417

2418

2419

2420

2421

2422

2423

2424

2425

2426

2427

2428

2429

2430

2431

2432

2433

2434

2435

2436

2437

2438

2439

2440

2441

2442

2443

2444

2445

2446

2447

2448

2449

2450

2451

2452

2453

2454

2455

2456

2457

2458

2459

2460

2461

2462

2463

2464

2465

2466

2467

2468

2469

2470

2471

2472

2473

2474

2475

2476

2477

2478

2479

2480

2481

2482

2483

2484

2485

2486

2487

2488

2489

2490

2491

2492

2493

2494

2495

2496

2497

2498

2499

2500

2501

2502

2503

2504

2505

2506

2507

2508

2509

2510

2511

2512

2513

2514

2515

2516

2517

2518

2519

2520

2521

2522

2523

2524

2525

2526

2527

2528

2529

2530

2531

2532

2533

2534

2535

2536

2537

2538

2539

2540

2541

2542

2543

2544

2545

2546

2547

2548

2549

2550

2551

2552

2553

2554

2555

2556

2557

2558

2559

2560

2561

2562

2563

2564

2565

2566

2567

2568

2569

2570

2571

2572

2573

2574

2575

2576

2577

2578

2579

2580

2581

2582

2583

2584

2585

2586

2587

2588

2589

2590

2591

2592

2593

2594

2595

2596

2597

2598

2599

2600

2601

2602

2603

2604

2605

2606

2607

2608

2609

2610

2611

2612

2613

2614

2615

2616

2617

2618

2619

2620

2621

2622

2623

2624

2625

2626

2627

2628

2629

2630

2631

2632

2633

2634

2635

2636

2637

2638

2639

2640

2641

2642

2643

2644

2645

2646

2647

2648

2649

2650

2651

2652

2653

2654

2655

2656

2657

2658

2659

2660

2661

2662

2663

2664

2665

2666

2667

2668

2669

2670

2671

2672

2673

2674

2675

2676

2677

2678

2679

2680

2681

2682

2683

2684

2685

2686

2687

2688

2689

2690

2691

2692

2693

2694

2695

2696

2697

2698

2699

2700

2701

2702

2703

2704

2705

2706

2707

2708

2709

2710

2711

2712

2713

2714

2715

2716

2717

2718

2719

2720

2721

2722

2723

2724

2725

2726

2727

2728

2729

2730

2731

2732

2733

2734

2735

2736

2737

2738

2739

2740

2741

2742

2743

2744

2745

2746

2747

2748

2749

2750

2751

2752

2753

2754

2755

2756

2757

2758

2759

2760

2761

2762

2763

2764

2765

2766

2767

2768

2769

2770

2771

2772

2773

2774

2775

2776

2777

2778

2779

2780

2781

2782

2783

2784

2785

2786

2787

2788

2789

2790

2791

2792

2793

2794

2795

2796

2797

2798

2799

2800

2801

2802

2803

2804

2805

2806

2807

2808

2809

2810

2811

2812

2813

2814

2815

2816

2817

2818

2819

2820

2821

2822

2823

2824

2825

2826

2827

2828

2829

2830

2831

2832

2833

2834

2835

2836

2837

2838

2839

2840

2841

2842

2843

2844

2845

2846

2847

2848

2849

2850

2851

2852

2853

2854

2855

2856

2857

2858

2859

2860

2861

2862

2863

2864

2865

2866

2867

2868

2869

2870

2871

2872

2873

2874

2875

2876

2877

2878

2879

2880

2881

2882

2883

2884

2885

2886

2887

2888

2889

2890

2891

2892

2893

2894

2895

2896

2897

2898

2899

2900

2901

2902

2903

2904

2905

2906

2907

2908

2909

2910

2911

2912

2913

2914

2915

2916

2917

2918

2919

2920

2921

2922

2923

2924

2925

2926

2927

2928

2929

2930

2931

2932

2933

2934

2935

2936

2937

2938

2939

2940

2941

2942

2943

2944

2945

2946

2947

2948

2949

2950

2951

2952

2953

2954

2955

2956

2957

2958

2959

2960

2961

2962

2963

2964

2965

2966

2967

2968

2969

2970

2971

2972

2973

2974

2975

2976

2977

2978

2979

2980

2981

2982

2983

2984

2985

2986

2987

2988

2989

2990

2991

2992

2993

2994

2995

2996

2997

2998

2999

3000

3001

3002

3003

3004

3005

3006

3007

3008

3009

3010

3011

3012

3013

3014

3015

3016

3017

3018

3019

3020

3021

3022

3023

3024

3025

3026

3027

3028

3029

3030

3031

3032

3033

3034

3035

3036

3037

3038

3039

3040

3041

3042

3043

3044

3045

3046

3047

3048

3049

3050

3051

3052

3053

3054

3055

3056

3057

3058

3059

3060

3061

3062

3063

3064

3065

3066

3067

3068

3069

3070

3071

3072

3073

3074

3075

3076

3077

3078

3079

3080

3081

3082

3083

3084

3085

3086

3087

3088

3089

3090

3091

3092

3093

3094

3095

3096

3097

3098

3099

3100

3101

3102

3103

3104

3105

3106

3107

3108

3109

3110

3111

3112

3113

3114

3115

3116

3117

3118

3119

3120

3121

3122

3123

3124

3125

3126

3127

3128

3129

3130

3131

3132

3133

3134

3135

3136

3137

3138

3139

3140

3141

3142

3143

3144

3145

3146

3147

3148

3149

3150

3151

3152

3153

3154

3155

3156

3157

3158

3159

3160

3161

3162

3163

3164

3165

3166

3167

3168

3169

3170

3171

3172

3173

3174

3175

3176

3177

3178

3179

3180

3181

3182

3183

3184

3185

3186

3187

3188

3189

3190

3191

3192

3193

3194

3195

3196

3197

3198

3199

3200

3201

3202

3203

3204

3205

3206

3207

3208

3209

3210

3211

3212

3213

3214

3215

3216

3217

3218

3219

3220

3221

3222

3223

3224

3225

3226

3227

3228

3229

3230

3231

3232

3233

3234

3235

3236

3237

3238

3239

3240

3241

3242

3243

3244

3245

3246

3247

3248

3249

3250

3251

3252

3253

3254

3255

3256

3257

3258

3259

3260

3261

3262

3263

3264

3265

3266

3267

3268

3269

3270

3271

3272

3273

3274

3275

3276

3277

3278

3279

3280

3281

3282

3283

3284

3285

3286

3287

3288

3289

3290

3291

3292

3293

3294

3295

3296

3297

3298

3299

3300

3301

3302

3303

3304

3305

3306

3307

3308

3309

3310

3311

3312

3313

3314

3315

3316

3317

3318

3319

3320

3321

3322

3323

3324

3325

3326

3327

3328

3329

3330

3331

3332

3333

3334

3335

3336

3337

3338

3339

3340

3341

3342

3343

3344

3345

3346

3347

3348

3349

3350

3351

3352

3353

3354

3355

3356

3357

3358

3359

3360

3361

3362

3363

3364

3365

3366

3367

3368

3369

3370

3371

3372

3373

3374

3375

3376

3377

3378

3379

3380

3381

3382

3383

3384

3385

3386

3387

3388

3389

3390

3391

3392

3393

3394

3395

3396

3397

3398

3399

3400

3401

3402

3403

3404

3405

3406

3407

3408

3409

3410

3411

3412

3413

3414

3415

3416

3417

3418

3419

3420

3421

3422

3423

3424

3425

3426

3427

3428

3429

3430

3431

3432

3433

3434

3435

3436

3437

3438

3439

3440

3441

3442

3443

3444

3445

3446

3447

3448

3449

3450

3451

3452

3453

3454

3455

3456

3457

3458

3459

3460

3461

3462

3463

3464

3465

3466

3467

3468

3469

3470

3471

3472

3473

3474

3475

3476

3477

3478

3479

3480

3481

3482

3483

3484

3485

3486

3487

3488

3489

3490

3491

3492

3493

3494

3495

3496

3497

3498

3499

3500

3501

3502

3503

3504

3505

3506

3507

3508

3509

3510

3511

3512

3513

3514

3515

3516

3517

3518

3519

3520

3521

3522

3523

3524

3525

3526

3527

3528

3529

3530

3531

3532

3533

3534

3535

3536

3537

3538

3539

3540

3541

3542

3543

3544

3545

3546

3547

3548

3549

3550

3551

3552

3553

3554

3555

3556

3557

3558

3559

3560

3561

3562

3563

3564

3565

3566

3567

3568

3569

3570

3571

3572

3573

3574

3575

3576

3577

3578

3579

3580

3581

3582

3583

3584

3585

3586

3587

3588

3589

3590

3591

3592

3593

3594

3595

3596

3597

3598

3599

3600

3601

3602

3603

3604

3605

3606

3607

3608

3609

3610

3611

3612

3613

3614

3615

3616

3617

3618

3619

3620

3621

3622

3623

3624

3625

3626

3627

# -*- coding: utf-8 -*- 

r""" 

Tamari Interval-posets 

 

This module implements Tamari interval-posets: combinatorial objects which 

represent intervals of the Tamari order. They have been introduced in 

[ChP2015]_ and allow for many combinatorial operations on Tamari intervals. 

In particular, they are linked to :class:`DyckWords` and :class:`BinaryTrees`. 

An introduction into Tamari interval-posets is given in Chapter 7 

of [Pons2013]_. 

 

The Tamari lattice can be defined as a lattice structure on either of several 

classes of Catalan objects, especially binary trees and Dyck paths 

[TamBrack1962]_ [HuangTamari1972]_ [Sta-EC2]_. An interval can be seen as 

a pair of comparable elements. The number of intervals has been given in 

[ChapTamari08]_. 

 

REFERENCES: 

 

.. [ChP2015] Grégory Châtel and Viviane Pons. 

*Counting smaller elements in the tamari and m-tamari lattices*. 

Journal of Combinatorial Theory, Series A. (2015). :arxiv:`1311.3922`. 

 

.. [Pons2013] Viviane Pons, 

*Combinatoire algébrique liée aux ordres sur les permutations*. 

PhD Thesis. (2013). :arxiv:`1310.1805v1`. 

 

.. [TamBrack1962] Dov Tamari. 

*The algebra of bracketings and their enumeration*. 

Nieuw Arch. Wisk. (1962). 

 

.. [HuangTamari1972] Samuel Huang and Dov Tamari. 

*Problems of associativity: A simple proof for the lattice property 

of systems ordered by a semi-associative law*. 

J. Combinatorial Theory Ser. A. (1972). 

http://www.sciencedirect.com/science/article/pii/0097316572900039 . 

 

.. [ChapTamari08] Frédéric Chapoton. 

*Sur le nombre d'intervalles dans les treillis de Tamari*. 

Sem. Lothar. Combin. (2008). 

:arxiv:`math/0602368v1`. 

 

.. [FPR15] Wenjie Fang and Louis-François Préville-Ratelle, 

*From generalized Tamari intervals to non-separable planar maps*. 

:arxiv:`1511.05937` 

 

.. [Pons2018] Viviane Pons, 

*The Rise-Contact involution on Tamari intervals* 

 

.. [Rog2018] Baptiste Rognerud, 

*Exceptional and modern intervals of the Tamari lattice*. 

:arxiv:`1801.04097` 

 

AUTHORS: 

 

- Viviane Pons 2014: initial implementation 

- Frédéric Chapoton 2014: review 

- Darij Grinberg 2014: review 

- Travis Scrimshaw 2014: review 

""" 

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

# Copyright (C) 2013 Viviane Pons <viviane.pons@univie.ac.at>, 

# 

# 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 __future__ import print_function 

from six.moves import range 

from six import add_metaclass 

 

from sage.categories.enumerated_sets import EnumeratedSets 

from sage.categories.finite_enumerated_sets import FiniteEnumeratedSets 

from sage.categories.posets import Posets 

from sage.combinat.posets.posets import Poset, FinitePoset 

from sage.categories.finite_posets import FinitePosets 

from sage.combinat.binary_tree import BinaryTrees 

from sage.combinat.binary_tree import LabelledBinaryTrees, LabelledBinaryTree 

from sage.combinat.dyck_word import DyckWords 

from sage.combinat.permutation import Permutation 

from sage.misc.inherit_comparison import InheritComparisonClasscallMetaclass 

from sage.misc.cachefunc import cached_method 

from sage.misc.latex import latex 

from sage.misc.lazy_attribute import lazy_attribute 

from sage.rings.integer import Integer 

from sage.rings.all import NN 

from sage.sets.non_negative_integers import NonNegativeIntegers 

from sage.sets.disjoint_union_enumerated_sets import DisjointUnionEnumeratedSets 

from sage.sets.family import Family 

from sage.structure.element import Element 

from sage.structure.global_options import GlobalOptions 

from sage.structure.parent import Parent 

from sage.structure.richcmp import op_NE, op_EQ, op_LT, op_LE, op_GT, op_GE 

from sage.structure.unique_representation import UniqueRepresentation 

from sage.graphs.digraph import DiGraph 

 

 

@add_metaclass(InheritComparisonClasscallMetaclass) 

class TamariIntervalPoset(Element): 

r""" 

The class of Tamari interval-posets. 

 

An interval-poset is a labelled poset of size `n`, with labels 

`1, 2, \ldots, n`, satisfying the following conditions: 

 

- if `a < c` (as integers) and `a` precedes `c` in the poset, then, 

for all `b` such that `a < b < c`, `b` precedes `c`, 

 

- if `a < c` (as integers) and `c` precedes `a` in the poset, then, 

for all `b` such that `a < b < c`, `b` precedes `a`. 

 

We use the word "precedes" here to distinguish the poset order and 

the natural order on numbers. "Precedes" means "is smaller than 

with respect to the poset structure"; this does not imply a 

covering relation. 

 

Interval-posets of size `n` are in bijection with intervals of 

the Tamari lattice of binary trees of size `n`. Specifically, if 

`P` is an interval-poset of size `n`, then the set of linear 

extensions of `P` (as permutations in `S_n`) is an interval in the 

right weak order (see 

:meth:`~sage.combinat.permutation.Permutation.permutohedron_lequal`), 

and is in fact the preimage of an interval in the Tamari lattice (of 

binary trees of size `n`) under the operation which sends a 

permutation to its right-to-left binary search tree 

(:meth:`~sage.combinat.permutation.Permutation.binary_search_tree` 

with the ``left_to_right`` variable set to ``False``) 

without its labelling. 

 

INPUT: 

 

- ``size`` -- an integer, the size of the interval-posets (number of 

vertices) 

 

- ``relations`` -- a list (or tuple) of pairs ``(a,b)`` (themselves 

lists or tuples), each representing a relation of the form 

'`a` precedes `b`' in the poset. 

 

- ``check`` -- (default: ``True``) whether to check the interval-poset 

condition or not. 

 

.. WARNING:: 

 

The ``relations`` input can be a list or tuple, but not an 

iterator (nor should its entries be iterators). 

 

NOTATION: 

 

Here and in the following, the signs `<` and `>` always refer to 

the natural ordering on integers, whereas the word "precedes" refers 

to the order of the interval-poset. "Minimal" and "maximal" refer 

to the natural ordering on integers. 

 

The *increasing relations* of an interval-poset `P` mean the pairs 

`(a, b)` of elements of `P` such that `a < b` as integers and `a` 

precedes `b` in `P`. The *initial forest* of `P` is the poset 

obtained by imposing (only) the increasing relations on the ground 

set of `P`. It is a sub-interval poset of `P`, and is a forest with 

its roots on top. This forest is usually given the structure of a 

planar forest by ordering brother nodes by their labels; it then has 

the property that if its nodes are traversed in post-order 

(see :meth:`~sage.combinat.abstract_tree.AbstractTree.post_order_traversal`, 

and traverse the trees of the forest from left to right as well), 

then the labels encountered are `1, 2, \ldots, n` in this order. 

 

The *decreasing relations* of an interval-poset `P` mean the pairs 

`(a, b)` of elements of `P` such that `b < a` as integers and `a` 

precedes `b` in `P`. The *final forest* of `P` is the poset 

obtained by imposing (only) the decreasing relations on the ground 

set of `P`. It is a sub-interval poset of `P`, and is a forest with 

its roots on top. This forest is usually given the structure of a 

planar forest by ordering brother nodes by their labels; it then has 

the property that if its nodes are traversed in pre-order 

(see :meth:`~sage.combinat.abstract_tree.AbstractTree.pre_order_traversal`, 

and traverse the trees of the forest from left to right as well), 

then the labels encountered are `1, 2, \ldots, n` in this order. 

 

EXAMPLES:: 

 

sage: TamariIntervalPoset(0,[]) 

The Tamari interval of size 0 induced by relations [] 

sage: TamariIntervalPoset(3,[]) 

The Tamari interval of size 3 induced by relations [] 

sage: TamariIntervalPoset(3,[(1,2)]) 

The Tamari interval of size 3 induced by relations [(1, 2)] 

sage: TamariIntervalPoset(3,[(1,2),(2,3)]) 

The Tamari interval of size 3 induced by relations [(1, 2), (2, 3)] 

sage: TamariIntervalPoset(3,[(1,2),(2,3),(1,3)]) 

The Tamari interval of size 3 induced by relations [(1, 2), (2, 3)] 

sage: TamariIntervalPoset(3,[(1,2),(3,2)]) 

The Tamari interval of size 3 induced by relations [(1, 2), (3, 2)] 

sage: TamariIntervalPoset(3,[[1,2],[2,3]]) 

The Tamari interval of size 3 induced by relations [(1, 2), (2, 3)] 

sage: TamariIntervalPoset(3,[[1,2],[2,3],[1,2],[1,3]]) 

The Tamari interval of size 3 induced by relations [(1, 2), (2, 3)] 

 

sage: TamariIntervalPoset(3,[(3,4)]) 

Traceback (most recent call last): 

... 

ValueError: The relations do not correspond to the size of the poset. 

 

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

Traceback (most recent call last): 

... 

ValueError: The graph is not directed acyclic 

 

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

Traceback (most recent call last): 

... 

ValueError: This does not satisfy the Tamari interval-poset condition. 

 

It is also possible to transform a poset directly into an interval-poset:: 

 

sage: TIP = TamariIntervalPosets() 

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

sage: TIP(p) 

The Tamari interval of size 3 induced by relations [(1, 2)] 

sage: TIP(Poset({1: []})) 

The Tamari interval of size 1 induced by relations [] 

sage: TIP(Poset({})) 

The Tamari interval of size 0 induced by relations [] 

""" 

@staticmethod 

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

r""" 

Ensure that interval-posets created by the enumerated sets and 

directly are the same and that they are instances of 

:class:`TamariIntervalPoset`. 

 

TESTS:: 

 

sage: ip = TamariIntervalPoset(4,[(2,4),(3,4),(2,1),(3,1)]) 

sage: ip.parent() 

Interval-posets 

sage: type(ip) 

<class 'sage.combinat.interval_posets.TamariIntervalPosets_all_with_category.element_class'> 

 

sage: ip2 = TamariIntervalPosets()(4,[(2,4),(3,4),(2,1),(3,1)]) 

sage: ip2.parent() is ip.parent() 

True 

sage: type(ip) is type(ip2) 

True 

 

sage: ip3 = TamariIntervalPosets(4)([(2,4),(3,4),(2,1),(3,1)]) 

sage: ip3.parent() is ip.parent() 

False 

sage: type(ip3) is type(ip) 

True 

""" 

P = TamariIntervalPosets_all() 

return P.element_class(P, *args, **opts) 

 

def __init__(self, parent, size, relations, check=True): 

r""" 

TESTS:: 

 

sage: TamariIntervalPoset(3,[(1,2),(3,2)]).parent() 

Interval-posets 

""" 

self._size = size 

self._poset = Poset((list(range(1, size + 1)), relations)) 

if self._poset.cardinality() != size: 

# This can happen as the Poset constructor automatically adds 

# in elements from the relations. 

raise ValueError("The relations do not correspond to the size of the poset.") 

 

if check and not TamariIntervalPosets.check_poset(self._poset): 

raise ValueError("This does not satisfy the Tamari interval-poset condition.") 

 

Element.__init__(self, parent) 

 

self._cover_relations = tuple(self._poset.cover_relations()) 

self._latex_options = dict() 

 

def set_latex_options(self, D): 

r""" 

Set the latex options for use in the ``_latex_`` function. The 

default values are set in the ``__init__`` function. 

 

- ``tikz_scale`` -- (default: 1) scale for use with the tikz package 

 

- ``line_width`` -- (default: 1 * ``tikz_scale``) value representing the 

line width 

 

- ``color_decreasing`` -- (default: red) the color for decreasing 

relations 

 

- ``color_increasing`` -- (default: blue) the color for increasing 

relations 

 

- ``hspace`` -- (default: 1) the difference between horizontal 

coordinates of adjacent vertices 

 

- ``vspace`` -- (default: 1) the difference between vertical 

coordinates of adjacent vertices 

 

INPUT: 

 

- ``D`` -- a dictionary with a list of latex parameters to change 

 

EXAMPLES:: 

 

sage: ip = TamariIntervalPoset(4,[(2,4),(3,4),(2,1),(3,1)]) 

sage: ip.latex_options()["color_decreasing"] 

'red' 

sage: ip.set_latex_options({"color_decreasing":'green'}) 

sage: ip.latex_options()["color_decreasing"] 

'green' 

sage: ip.set_latex_options({"color_increasing":'black'}) 

sage: ip.latex_options()["color_increasing"] 

'black' 

 

To change the default options for all interval-posets, use the 

parent's latex options:: 

 

sage: ip = TamariIntervalPoset(4,[(2,4),(3,4),(2,1),(3,1)]) 

sage: ip2 = TamariIntervalPoset(4,[(1,2),(2,3)]) 

sage: ip.latex_options()["color_decreasing"] 

'red' 

sage: ip2.latex_options()["color_decreasing"] 

'red' 

sage: TamariIntervalPosets.options(latex_color_decreasing='green') 

sage: ip.latex_options()["color_decreasing"] 

'green' 

sage: ip2.latex_options()["color_decreasing"] 

'green' 

 

Next we set a local latex option and show the global option does not 

override it:: 

 

sage: ip.set_latex_options({"color_decreasing": 'black'}) 

sage: ip.latex_options()["color_decreasing"] 

'black' 

sage: TamariIntervalPosets.options(latex_color_decreasing='blue') 

sage: ip.latex_options()["color_decreasing"] 

'black' 

sage: ip2.latex_options()["color_decreasing"] 

'blue' 

sage: TamariIntervalPosets.options._reset() 

""" 

for opt in D: 

self._latex_options[opt] = D[opt] 

 

def latex_options(self): 

r""" 

Return the latex options for use in the ``_latex_`` function as a 

dictionary. The default values are set using the options. 

 

- ``tikz_scale`` -- (default: 1) scale for use with the tikz package 

 

- ``line_width`` -- (default: 1) value representing the line width 

(additionally scaled by ``tikz_scale``) 

 

- ``color_decreasing`` -- (default: ``'red'``) the color for 

decreasing relations 

 

- ``color_increasing`` -- (default: ``'blue'``) the color for 

increasing relations 

 

- ``hspace`` -- (default: 1) the difference between horizontal 

coordinates of adjacent vertices 

 

- ``vspace`` -- (default: 1) the difference between vertical 

coordinates of adjacent vertices 

 

EXAMPLES:: 

 

sage: ip = TamariIntervalPoset(4,[(2,4),(3,4),(2,1),(3,1)]) 

sage: ip.latex_options()['color_decreasing'] 

'red' 

sage: ip.latex_options()['hspace'] 

1 

""" 

d = self._latex_options.copy() 

if "tikz_scale" not in d: 

d["tikz_scale"] = self.parent().options["latex_tikz_scale"] 

if "line_width" not in d: 

d["line_width"] = self.parent().options["latex_line_width_scalar"] * d["tikz_scale"] 

if "color_decreasing" not in d: 

d["color_decreasing"] = self.parent().options["latex_color_decreasing"] 

if "color_increasing" not in d: 

d["color_increasing"] = self.parent().options["latex_color_increasing"] 

if "hspace" not in d: 

d["hspace"] = self.parent().options["latex_hspace"] 

if "vspace" not in d: 

d["vspace"] = self.parent().options["latex_vspace"] 

return d 

 

def _find_node_positions(self, hspace=1, vspace=1): 

""" 

Compute a nice embedding. 

 

If `x` precedes `y`, then `y` will always be placed on top of `x` 

and/or to the right of `x`. 

Decreasing relations tend to be drawn vertically and increasing 

relations horizontally. 

The algorithm tries to avoid superposition but on big 

interval-posets, it might happen. 

 

OUTPUT: 

 

a dictionary {vertex: (x,y)} 

 

EXAMPLES:: 

 

sage: ti = TamariIntervalPosets(4)[2] 

sage: list(ti._find_node_positions().values()) 

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

""" 

node_positions = {} 

 

to_draw = [(1, 0)] 

current_parent = [self.increasing_parent(1)] 

parenty = [0] 

x = 0 

y = 0 

for i in range(2, self.size() + 1): 

decreasing_parent = self.decreasing_parent(i) 

increasing_parent = self.increasing_parent(i) 

while to_draw and (decreasing_parent is None or 

decreasing_parent < to_draw[-1][0]): 

n = to_draw.pop() 

node_positions[n[0]] = [x, n[1]] 

if i != current_parent[-1]: 

if (not self.le(i, i - 1) and decreasing_parent is not None): 

x += hspace 

if current_parent[-1] is not None: 

y -= vspace 

else: 

y -= vspace 

if increasing_parent != current_parent[-1]: 

current_parent.append(increasing_parent) 

parenty.append(y) 

nodey = y 

else: 

current_parent.pop() 

x += hspace 

nodey = parenty.pop() 

if not current_parent or increasing_parent != current_parent[-1]: 

current_parent.append(increasing_parent) 

parenty.append(nodey) 

to_draw.append((i, nodey)) 

 

for n in to_draw: 

node_positions[n[0]] = [x, n[1]] 

return node_positions 

 

def plot(self, **kwds): 

""" 

Return a picture. 

 

The picture represents the Hasse diagram, where the covers are 

colored in blue if they are increasing and in red if they are 

decreasing. 

 

This uses the same coordinates as the latex view. 

 

EXAMPLES:: 

 

sage: ti = TamariIntervalPosets(4)[2] 

sage: ti.plot() 

Graphics object consisting of 6 graphics primitives 

""" 

c0 = 'blue' # self.latex_options()["color_increasing"] 

c1 = 'red' # self.latex_options()["color_decreasing"] 

G = self.poset().hasse_diagram() 

G.set_pos(self._find_node_positions()) 

for a, b, c in G.edges(): 

if a < b: 

G.set_edge_label(a, b, 0) 

else: 

G.set_edge_label(a, b, 1) 

return G.plot(color_by_label={0: c0, 1: c1}, **kwds) 

 

def _latex_(self): 

r""" 

A latex representation of ``self`` using the tikzpicture package. 

 

This picture shows the union of the Hasse diagrams of the 

initial and final forests. 

 

If `x` precedes `y`, then `y` will always be placed on top of `x` 

and/or to the right of `x`. 

Decreasing relations tend to be drawn vertically and increasing 

relations horizontally. 

The algorithm tries to avoid superposition but on big 

interval-posets, it might happen. 

 

You can use ``self.set_latex_options()`` to change default latex 

options. Or you can use the parent's options. 

 

EXAMPLES:: 

 

sage: ip = TamariIntervalPoset(4,[(2,4),(3,4),(2,1),(3,1)]) 

sage: print(ip._latex_()) 

\begin{tikzpicture}[scale=1] 

\node(T1) at (1,0) {1}; 

\node(T2) at (0,-1) {2}; 

\node(T3) at (1,-2) {3}; 

\node(T4) at (2,-1) {4}; 

\draw[line width = 0.5, color=red] (T3) -- (T1); 

\draw[line width = 0.5, color=red] (T2) -- (T1); 

\draw[line width = 0.5, color=blue] (T2) -- (T4); 

\draw[line width = 0.5, color=blue] (T3) -- (T4); 

\end{tikzpicture} 

""" 

latex.add_package_to_preamble_if_available("tikz") 

latex_options = self.latex_options() 

start = "\\begin{tikzpicture}[scale=" + str(latex_options['tikz_scale']) + "]\n" 

end = "\\end{tikzpicture}" 

vspace = latex_options["vspace"] 

hspace = latex_options["hspace"] 

 

def draw_node(j, x, y): 

r""" 

Internal method to draw vertices 

""" 

return "\\node(T" + str(j) + ") at (" + str(x) + "," + str(y) + ") {" + str(j) + "};\n" 

 

def draw_increasing(i, j): 

r""" 

Internal method to draw increasing relations 

""" 

return "\\draw[line width = " + str(latex_options["line_width"]) + ", color=" + latex_options["color_increasing"] + "] (T" + str(i) + ") -- (T" + str(j) + ");\n" 

 

def draw_decreasing(i, j): 

r""" 

Internal method to draw decreasing relations 

""" 

return "\\draw[line width = " + str(latex_options["line_width"]) + ", color=" + latex_options["color_decreasing"] + "] (T" + str(i) + ") -- (T" + str(j) + ");\n" 

 

if self.size() == 0: 

nodes = "\\node(T0) at (0,0){$\emptyset$};" 

relations = "" 

else: 

positions = self._find_node_positions(hspace, vspace) 

nodes = "" # latex for node declarations 

relations = "" # latex for drawing relations 

for i in range(1, self.size() + 1): 

nodes += draw_node(i, *positions[i]) 

for i, j in self.decreasing_cover_relations(): 

relations += draw_decreasing(i, j) 

for i, j in self.increasing_cover_relations(): 

relations += draw_increasing(i, j) 

 

return start + nodes + relations + end 

 

def poset(self): 

r""" 

Return ``self`` as a labelled poset. 

 

An interval-poset is indeed constructed from a labelled poset which 

is stored internally. This method allows to access the poset and 

all the associated methods. 

 

EXAMPLES:: 

 

sage: ip = TamariIntervalPoset(4,[(1,2),(3,2),(2,4),(3,4)]) 

sage: pos = ip.poset(); pos 

Finite poset containing 4 elements 

sage: pos.maximal_chains() 

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

sage: pos.maximal_elements() 

[4] 

sage: pos.is_lattice() 

False 

""" 

return self._poset 

 

def __hash__(self): 

""" 

Return the hash of ``self``. 

 

EXAMPLES:: 

 

sage: len(set(hash(u) for u in TamariIntervalPosets(4))) 

68 

""" 

pair = (self.size(), tuple(tuple(e) for e in self._cover_relations)) 

return hash(pair) 

 

@cached_method 

def increasing_cover_relations(self): 

r""" 

Return the cover relations of the initial forest of ``self`` 

(the poset formed by keeping only the relations of the form 

`a` precedes `b` with `a < b`). 

 

The initial forest of ``self`` is a forest with its roots 

being on top. It is also called the increasing poset of ``self``. 

 

.. WARNING:: 

 

This method computes the cover relations of the initial 

forest. This is not identical with the cover relations of 

``self`` which happen to be increasing! 

 

.. SEEALSO:: 

 

:meth:`initial_forest` 

 

EXAMPLES:: 

 

sage: TamariIntervalPoset(4,[(1,2),(3,2),(2,4),(3,4)]).increasing_cover_relations() 

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

sage: TamariIntervalPoset(3,[(1,2),(1,3),(2,3)]).increasing_cover_relations() 

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

""" 

relations = [] 

size = self.size() 

for i in range(1, size): 

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

if self.le(i, j): 

relations.append((i, j)) 

break 

return relations 

 

def increasing_roots(self): 

r""" 

Return the root vertices of the initial forest of ``self``, 

i.e., the vertices `a` of ``self`` such that there is no 

`b > a` with `a` precedes `b`. 

 

OUTPUT: 

 

The list of all roots of the initial forest of ``self``, in 

decreasing order. 

 

EXAMPLES:: 

 

sage: ip = TamariIntervalPoset(6,[(3,2),(4,3),(5,2),(6,5),(1,2),(3,5),(4,5)]); ip 

The Tamari interval of size 6 induced by relations [(1, 2), (3, 5), (4, 5), (6, 5), (5, 2), (4, 3), (3, 2)] 

sage: ip.increasing_roots() 

[6, 5, 2] 

sage: ip.initial_forest().increasing_roots() 

[6, 5, 2] 

""" 

size = self.size() 

if size == 0: 

return [] 

roots = [size] 

root = size 

for i in range(size - 1, 0, -1): 

if not self.le(i, root): 

roots.append(i) 

root = i 

return roots 

 

def increasing_children(self, v): 

r""" 

Return the children of ``v`` in the initial forest of ``self``. 

 

INPUT: 

 

- ``v`` -- an integer representing a vertex of ``self`` 

(between 1 and ``size``) 

 

OUTPUT: 

 

The list of all children of ``v`` in the initial forest of 

``self``, in decreasing order. 

 

EXAMPLES:: 

 

sage: ip = TamariIntervalPoset(6,[(3,2),(4,3),(5,2),(6,5),(1,2),(3,5),(4,5)]); ip 

The Tamari interval of size 6 induced by relations [(1, 2), (3, 5), (4, 5), (6, 5), (5, 2), (4, 3), (3, 2)] 

sage: ip.increasing_children(2) 

[1] 

sage: ip.increasing_children(5) 

[4, 3] 

sage: ip.increasing_children(1) 

[] 

""" 

children = [] 

root = None 

for i in range(v - 1, 0, -1): 

if not self.le(i, v): 

break 

if root is None or not self.le(i, root): 

children.append(i) 

root = i 

return children 

 

def increasing_parent(self, v): 

r""" 

Return the vertex parent of ``v`` in the initial forest of ``self``. 

 

This is the lowest (as integer!) vertex `b > v` such that `v` 

precedes `b`. If there is no such vertex (that is, `v` is an 

increasing root), then ``None`` is returned. 

 

INPUT: 

 

- ``v`` -- an integer representing a vertex of ``self`` 

(between 1 and ``size``) 

 

EXAMPLES:: 

 

sage: ip = TamariIntervalPoset(6,[(3,2),(4,3),(5,2),(6,5),(1,2),(3,5),(4,5)]); ip 

The Tamari interval of size 6 induced by relations [(1, 2), (3, 5), (4, 5), (6, 5), (5, 2), (4, 3), (3, 2)] 

sage: ip.increasing_parent(1) 

2 

sage: ip.increasing_parent(3) 

5 

sage: ip.increasing_parent(4) 

5 

sage: ip.increasing_parent(5) is None 

True 

""" 

parent = None 

for i in range(self.size(), v, -1): 

if self.le(v, i): 

parent = i 

return parent 

 

@cached_method 

def decreasing_cover_relations(self): 

r""" 

Return the cover relations of the final forest of ``self`` 

(the poset formed by keeping only the relations of the form 

`a` precedes `b` with `a > b`). 

 

The final forest of ``self`` is a forest with its roots 

being on top. It is also called the decreasing poset of ``self``. 

 

.. WARNING:: 

 

This method computes the cover relations of the final 

forest. This is not identical with the cover relations of 

``self`` which happen to be decreasing! 

 

.. SEEALSO:: 

 

:meth:`final_forest` 

 

EXAMPLES:: 

 

sage: TamariIntervalPoset(4,[(2,1),(3,2),(3,4),(4,2)]).decreasing_cover_relations() 

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

sage: TamariIntervalPoset(4,[(2,1),(4,3),(2,3)]).decreasing_cover_relations() 

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

sage: TamariIntervalPoset(3,[(2,1),(3,1),(3,2)]).decreasing_cover_relations() 

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

""" 

relations = [] 

for i in range(self.size(), 1, -1): 

for j in range(i - 1, 0, -1): 

if self.le(i, j): 

relations.append((i, j)) 

break 

return relations 

 

def decreasing_roots(self): 

r""" 

Return the root vertices of the final forest of ``self``, 

i.e., the vertices `b` such that there is no `a < b` with `b` 

preceding `a`. 

 

OUTPUT: 

 

The list of all roots of the final forest of ``self``, in 

increasing order. 

 

EXAMPLES:: 

 

sage: ip = TamariIntervalPoset(6,[(3,2),(4,3),(5,2),(6,5),(1,2),(3,5),(4,5)]); ip 

The Tamari interval of size 6 induced by relations [(1, 2), (3, 5), (4, 5), (6, 5), (5, 2), (4, 3), (3, 2)] 

sage: ip.decreasing_roots() 

[1, 2] 

sage: ip.final_forest().decreasing_roots() 

[1, 2] 

""" 

if self.size() == 0: 

return [] 

roots = [1] 

root = 1 

for i in range(2, self.size() + 1): 

if not self.le(i, root): 

roots.append(i) 

root = i 

return roots 

 

def decreasing_children(self, v): 

r""" 

Return the children of ``v`` in the final forest of ``self``. 

 

INPUT: 

 

- ``v`` -- an integer representing a vertex of ``self`` 

(between 1 and ``size``) 

 

OUTPUT: 

 

The list of all children of ``v`` in the final forest of ``self``, 

in increasing order. 

 

EXAMPLES:: 

 

sage: ip = TamariIntervalPoset(6,[(3,2),(4,3),(5,2),(6,5),(1,2),(3,5),(4,5)]); ip 

The Tamari interval of size 6 induced by relations [(1, 2), (3, 5), (4, 5), (6, 5), (5, 2), (4, 3), (3, 2)] 

sage: ip.decreasing_children(2) 

[3, 5] 

sage: ip.decreasing_children(3) 

[4] 

sage: ip.decreasing_children(1) 

[] 

""" 

children = [] 

root = None 

for i in range(v + 1, self.size() + 1): 

if not self.le(i, v): 

break 

if root is None or not self.le(i, root): 

children.append(i) 

root = i 

return children 

 

def decreasing_parent(self, v): 

r""" 

Return the vertex parent of ``v`` in the final forest of ``self``. 

This is the highest (as integer!) vertex `a < v` such that ``v`` 

precedes ``a``. If there is no such vertex (that is, `v` is a 

decreasing root), then ``None`` is returned. 

 

INPUT: 

 

- ``v`` -- an integer representing a vertex of ``self`` (between 

1 and ``size``) 

 

EXAMPLES:: 

 

sage: ip = TamariIntervalPoset(6,[(3,2),(4,3),(5,2),(6,5),(1,2),(3,5),(4,5)]); ip 

The Tamari interval of size 6 induced by relations [(1, 2), (3, 5), (4, 5), (6, 5), (5, 2), (4, 3), (3, 2)] 

sage: ip.decreasing_parent(4) 

3 

sage: ip.decreasing_parent(3) 

2 

sage: ip.decreasing_parent(5) 

2 

sage: ip.decreasing_parent(2) is None 

True 

""" 

parent = None 

for i in range(1, v): 

if self.le(v, i): 

parent = i 

return parent 

 

def le(self, e1, e2): 

r""" 

Return whether ``e1`` precedes or equals ``e2`` in ``self``. 

 

EXAMPLES:: 

 

sage: ip = TamariIntervalPoset(4,[(1,2),(2,3)]) 

sage: ip.le(1,2) 

True 

sage: ip.le(1,3) 

True 

sage: ip.le(2,3) 

True 

sage: ip.le(3,4) 

False 

sage: ip.le(1,1) 

True 

""" 

return self._poset.le(e1, e2) 

 

def lt(self, e1, e2): 

r""" 

Return whether ``e1`` strictly precedes ``e2`` in ``self``. 

 

EXAMPLES:: 

 

sage: ip = TamariIntervalPoset(4,[(1,2),(2,3)]) 

sage: ip.lt(1,2) 

True 

sage: ip.lt(1,3) 

True 

sage: ip.lt(2,3) 

True 

sage: ip.lt(3,4) 

False 

sage: ip.lt(1,1) 

False 

""" 

return self._poset.lt(e1, e2) 

 

def ge(self, e1, e2): 

r""" 

Return whether ``e2`` precedes or equals ``e1`` in ``self``. 

 

EXAMPLES:: 

 

sage: ip = TamariIntervalPoset(4,[(1,2),(2,3)]) 

sage: ip.ge(2,1) 

True 

sage: ip.ge(3,1) 

True 

sage: ip.ge(3,2) 

True 

sage: ip.ge(4,3) 

False 

sage: ip.ge(1,1) 

True 

""" 

return self._poset.ge(e1, e2) 

 

def gt(self, e1, e2): 

r""" 

Return whether ``e2`` strictly precedes ``e1`` in ``self``. 

 

EXAMPLES:: 

 

sage: ip = TamariIntervalPoset(4,[(1,2),(2,3)]) 

sage: ip.gt(2,1) 

True 

sage: ip.gt(3,1) 

True 

sage: ip.gt(3,2) 

True 

sage: ip.gt(4,3) 

False 

sage: ip.gt(1,1) 

False 

""" 

return self._poset.gt(e1, e2) 

 

def size(self): 

r""" 

Return the size (number of vertices) of the interval-poset. 

 

EXAMPLES:: 

 

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

3 

""" 

return self._size 

 

def complement(self): 

r""" 

Return the complement of the interval-poset ``self``. 

 

If `P` is a Tamari interval-poset of size `n`, then the 

*complement* of `P` is defined as the interval-poset `Q` whose 

base set is `[n] = \{1, 2, \ldots, n\}` (just as for `P`), but 

whose order relation has `a` precede `b` if and only if 

`n + 1 - a` precedes `n + 1 - b` in `P`. 

 

In terms of the Tamari lattice, the *complement* is the symmetric 

of ``self``. It is formed from the left-right symmeterized of 

the binary trees of the interval (switching left and right 

subtrees, see 

:meth:`~sage.combinat.binary_tree.BinaryTree.left_right_symmetry`). 

In particular, initial intervals are sent to final intervals and 

vice-versa. 

 

EXAMPLES:: 

 

sage: TamariIntervalPoset(3, [(2, 1), (3, 1)]).complement() 

The Tamari interval of size 3 induced by relations [(1, 3), (2, 3)] 

sage: TamariIntervalPoset(0, []).complement() 

The Tamari interval of size 0 induced by relations [] 

sage: ip = TamariIntervalPoset(4, [(1, 2), (2, 4), (3, 4)]) 

sage: ip.complement() == TamariIntervalPoset(4, [(2, 1), (3, 1), (4, 3)]) 

True 

sage: ip.lower_binary_tree() == ip.complement().upper_binary_tree().left_right_symmetry() 

True 

sage: ip.upper_binary_tree() == ip.complement().lower_binary_tree().left_right_symmetry() 

True 

sage: ip.is_initial_interval() 

True 

sage: ip.complement().is_final_interval() 

True 

""" 

N = self._size + 1 

new_covers = [[N - i[0], N - i[1]] 

for i in self._poset.cover_relations_iterator()] 

return TamariIntervalPoset(N - 1, new_covers, check=False) 

 

def left_branch_involution(self): 

""" 

Return the image of ``self`` by the left-branch involution. 

 

OUTPUT: an interval-poset 

 

.. SEEALSO:: :meth:`rise_contact_involution` 

 

EXAMPLES:: 

 

sage: tip = TamariIntervalPoset(8, [(1,2), (2,4), (3,4), (6,7), (3,2), (5,4), (6,4), (8,7)]) 

sage: t = tip.left_branch_involution(); t 

The Tamari interval of size 8 induced by relations [(1, 6), (2, 6), 

(3, 5), (4, 5), (5, 6), (6, 8), (7, 8), (7, 6), (4, 3), (3, 1), 

(2, 1)] 

sage: t.left_branch_involution() == tip 

True 

 

REFERENCES: 

 

- [Pons2018]_ 

""" 

gt = self.grafting_tree().left_border_symmetry() 

return TamariIntervalPosets.from_grafting_tree(gt) 

 

def rise_contact_involution(self): 

""" 

Return the image of ``self`` by the rise-contact involution. 

 

OUTPUT: an interval-poset 

 

This is defined by conjugating the complement involution 

by the left-branch involution 

 

.. SEEALSO:: :meth:`left_branch_involution`, :meth:`complement` 

 

EXAMPLES:: 

 

sage: tip = TamariIntervalPoset(8, [(1,2), (2,4), (3,4), (6,7), (3,2), (5,4), (6,4), (8,7)]) 

sage: t = tip.rise_contact_involution(); t 

The Tamari interval of size 8 induced by relations [(2, 8), (3, 8), 

(4, 5), (5, 7), (6, 7), (7, 8), (8, 1), (7, 2), (6, 2), (5, 3), 

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

sage: t.rise_contact_involution() == tip 

True 

sage: tip.lower_dyck_word().number_of_touch_points() == t.upper_dyck_word().number_of_initial_rises() 

True 

sage: tip.number_of_tamari_inversions() == t.number_of_tamari_inversions() 

True 

 

REFERENCES: 

 

- [Pons2018]_ 

""" 

t = self.left_branch_involution().complement() 

return t.left_branch_involution() 

 

def insertion(self, i): 

r""" 

Return the Tamari insertion of an integer `i` into the 

interval-poset ``self``. 

 

If `P` is a Tamari interval-poset of size `n` and `i` is an 

integer with `1 \leq i \leq n+1`, then the Tamari insertion of 

`i` into `P` is defined as the Tamari interval-poset of size 

`n+1` which corresponds to the interval `[C_1, C_2]` on the 

Tamari lattice, where the binary trees `C_1` and `C_2` are 

defined as follows: We write the interval-poset `P` as 

`[B_1, B_2]` for two binary trees `B_1` and `B_2`. We label 

the vertices of each of these two trees with the integers 

`1, 2, \ldots, i-1, i+1, i+2, \ldots, n+1` in such a way that 

the trees are binary search trees (this labelling is unique). 

Then, we insert `i` into each of these trees (in the way as 

explained in 

:meth:`~sage.combinat.binary_tree.LabelledBinaryTree.binary_search_insert`). 

The shapes of the resulting two trees are denoted `C_1` and 

`C_2`. 

 

An alternative way to construct the insertion of `i` into 

`P` is by relabeling each vertex `u` of `P` satisfying 

`u \geq i` (as integers) as `u+1`, and then adding a vertex 

`i` which should precede `i-1` and `i+1`. 

 

.. TODO:: 

 

To study this, it would be more natural to define 

interval-posets on arbitrary ordered sets rather than just 

on `\{1, 2, \ldots, n\}`. 

 

EXAMPLES:: 

 

sage: ip = TamariIntervalPoset(4, [(2, 3), (4, 3)]); ip 

The Tamari interval of size 4 induced by relations [(2, 3), (4, 3)] 

sage: ip.insertion(1) 

The Tamari interval of size 5 induced by relations [(1, 2), (3, 4), (5, 4)] 

sage: ip.insertion(2) 

The Tamari interval of size 5 induced by relations [(2, 3), (3, 4), (5, 4), (2, 1)] 

sage: ip.insertion(3) 

The Tamari interval of size 5 induced by relations [(2, 4), (3, 4), (5, 4), (3, 2)] 

sage: ip.insertion(4) 

The Tamari interval of size 5 induced by relations [(2, 3), (4, 5), (5, 3), (4, 3)] 

sage: ip.insertion(5) 

The Tamari interval of size 5 induced by relations [(2, 3), (5, 4), (4, 3)] 

 

sage: ip = TamariIntervalPoset(0, []) 

sage: ip.insertion(1) 

The Tamari interval of size 1 induced by relations [] 

 

sage: ip = TamariIntervalPoset(1, []) 

sage: ip.insertion(1) 

The Tamari interval of size 2 induced by relations [(1, 2)] 

sage: ip.insertion(2) 

The Tamari interval of size 2 induced by relations [(2, 1)] 

 

TESTS: 

 

Verifying that the two ways of computing insertion are 

equivalent:: 

 

sage: def insert_alternative(T, i): 

....: # Just another way to compute the insertion of i into T. 

....: from sage.combinat.binary_tree import LabelledBinaryTree 

....: B1 = T.lower_binary_tree().canonical_labelling() 

....: B2 = T.upper_binary_tree().canonical_labelling() 

....: # We should relabel the trees to "make space" for a label i, 

....: # but we don't, because it doesn't make a difference: The 

....: # binary search insertion will go precisely the same, because 

....: # an integer equal to the label of the root gets sent onto 

....: # the left branch. 

....: C1 = B1.binary_search_insert(i) 

....: C2 = B2.binary_search_insert(i) 

....: return TamariIntervalPosets.from_binary_trees(C1, C2) 

sage: def test_equivalence(n): 

....: for T in TamariIntervalPosets(n): 

....: for i in range(1, n + 2): 

....: if not (insert_alternative(T, i) == T.insertion(i)): 

....: print(T, i) 

....: return False 

....: return True 

sage: test_equivalence(3) 

True 

""" 

n = self._size 

if not 0 < i <= n + 1: 

raise ValueError("integer to be inserted not " 

"in the appropriate interval") 

 

def add1(u): 

if u >= i: 

return u + 1 

return u 

rels = [(add1(a), add1(b)) 

for (a, b) in self.decreasing_cover_relations()] 

rels += [(add1(a), add1(b)) 

for (a, b) in self.increasing_cover_relations()] 

rels += [(k, k - 1) for k in [i] if i > 1] 

rels += [(k, k + 1) for k in [i] if i <= n] 

return TamariIntervalPoset(n + 1, rels) 

 

def _repr_(self): 

r""" 

TESTS:: 

 

sage: TamariIntervalPoset(3,[(2,1),(3,1)]) 

The Tamari interval of size 3 induced by relations [(3, 1), (2, 1)] 

sage: TamariIntervalPoset(3,[(3,1),(2,1)]) 

The Tamari interval of size 3 induced by relations [(3, 1), (2, 1)] 

sage: TamariIntervalPoset(3,[(2,3),(2,1)]) 

The Tamari interval of size 3 induced by relations [(2, 3), (2, 1)] 

""" 

msg = "The Tamari interval of size {} induced by relations {}" 

return msg.format(self.size(), 

self.increasing_cover_relations() + 

self.decreasing_cover_relations()) 

 

def _ascii_art_(self): 

""" 

Return an ascii art picture of ``self``. 

 

This is a picture of the Hasse diagram. Vertices from `1` to `n` 

are placed on the diagonal from top-left to bottom-right. 

Then increasing covers are drawn above the diagonal 

and decreasing covers are drawn below the diagonal. 

 

EXAMPLES:: 

 

sage: T = TamariIntervalPosets(5)[56] 

sage: ascii_art(T) 

O-----------+ 

O--------+ 

+--O--+ | 

O--+ 

O 

sage: T.poset().cover_relations() 

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

""" 

n = self.size() 

M = [[' O ' if i == j else ' ' for i in range(n)] for j in range(n)] 

 

def superpose(x, y, b): 

# put symbol b at position x, y 

# on top of existing symbols there 

i = x - 1 

j = y - 1 

a = M[i][j] 

if a == ' ': 

M[i][j] = b 

elif a == '-+ ': 

if b == a: 

pass 

elif b == '---': 

M[i][j] = '-+-' 

elif b == ' | ': 

M[i][j] = '-+ ' 

elif a == ' +-': 

if b == a: 

pass 

elif b == '---': 

M[i][j] = '-+-' 

elif b == ' | ': 

M[i][j] = ' +-' 

elif a == '---': 

if b == a: 

pass 

elif b == '-+ ': 

M[i][j] = '-+-' 

elif b == ' +-': 

M[i][j] = '-+-' 

elif a == ' | ': 

if b == a: 

pass 

elif b == '-+ ': 

M[i][j] = '-+ ' 

elif b == ' +-': 

M[i][j] = ' +-' 

 

def superpose_node(i, right=True): 

i -= 1 # for indexing 

if M[i][i] == ' O ': 

if right: 

M[i][i] = ' O-' 

else: 

M[i][i] = '-O ' 

elif M[i][i] == ' O-' and not right: 

M[i][i] = '-O-' 

elif M[i][i] == '-O ' and right: 

M[i][i] = '-O-' 

 

for i, j in self.poset().hasse_diagram().edges(labels=False): 

if i > j: 

superpose_node(i, False) 

superpose(i, j, ' +-') 

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

superpose(k, j, ' | ') 

superpose(i, k, '---') 

else: 

superpose_node(i, True) 

superpose(i, j, '-+ ') 

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

superpose(i, k, '---') 

superpose(k, j, ' | ') 

 

from sage.typeset.ascii_art import AsciiArt 

return AsciiArt([''.join(ligne) for ligne in M]) 

 

def _unicode_art_(self): 

""" 

Return an unicode picture of ``self``. 

 

This is a picture of the Hasse diagram. Vertices from `1` to `n` are 

placed on the diagonal from top-left to bottom-right. 

Then increasing covers are drawn above the diagonal 

and decreasing covers are drawn below the diagonal. 

 

EXAMPLES:: 

 

sage: T = TamariIntervalPosets(5)[56] 

sage: unicode_art(T) 

o───╮ 

o──┤ 

╰o╮│ 

o┤ 

o 

sage: T.poset().cover_relations() 

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

""" 

n = self.size() 

M = [[u'o' if i == j else u' ' for i in range(n)] for j in range(n)] 

 

def superpose(x, y, b): 

# put symbol b at position x, y 

# on top of existing symbols there 

i = x - 1 

j = y - 1 

a = M[i][j] 

if a == ' ': 

M[i][j] = b 

elif a == u'╮': 

if b == a: 

pass 

elif b == u'─': 

M[i][j] = u'┬' 

elif b == u'│': 

M[i][j] = u'┤' 

elif a == u'╰': 

if b == a: 

pass 

elif b == u'─': 

M[i][j] = u'┴' 

elif b == u'│': 

M[i][j] = u'├' 

elif a == u'─': 

if b == a: 

pass 

elif b == u'╮': 

M[i][j] = u'┬' 

elif b == u'╰': 

M[i][j] = u'┴' 

elif a == u'│': 

if b == a: 

pass 

elif b == u'╮': 

M[i][j] = u'┤' 

elif b == u'╰': 

M[i][j] = u'├' 

 

for i, j in self.poset().hasse_diagram().edges(labels=False): 

if i > j: 

superpose(i, j, u'╰') 

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

superpose(k, j, u'│') 

superpose(i, k, u'─') 

else: 

superpose(i, j, u'╮') 

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

superpose(i, k, u'─') 

superpose(k, j, u'│') 

 

from sage.typeset.unicode_art import UnicodeArt 

return UnicodeArt([''.join(ligne) for ligne in M]) 

 

def _richcmp_(self, other, op): 

r""" 

TESTS:: 

 

sage: TamariIntervalPoset(0,[]) == TamariIntervalPoset(0,[]) 

True 

sage: TamariIntervalPoset(1,[]) == TamariIntervalPoset(0,[]) 

False 

sage: TamariIntervalPoset(3,[(1,2),(3,2)]) == TamariIntervalPoset(3,[(3,2),(1,2)]) 

True 

sage: TamariIntervalPoset(3,[(1,2),(3,2)]) == TamariIntervalPoset(3,[(1,2)]) 

False 

sage: TamariIntervalPoset(3,[(1,2),(3,2)]) != TamariIntervalPoset(3,[(3,2),(1,2)]) 

False 

 

sage: ip1 = TamariIntervalPoset(4,[(1,2),(2,3),(4,3)]) 

sage: ip2 = TamariIntervalPoset(4,[(1,2),(2,3)]) 

sage: ip1 <= ip2 

True 

sage: ip1 <= ip1 

True 

sage: ip2 <= ip1 

False 

""" 

if not isinstance(other, TamariIntervalPoset): 

return op == op_NE 

if op == op_EQ: 

return (self.size() == other.size() and 

self._cover_relations == other._cover_relations) 

if op == op_NE: 

return not(self.size() == other.size() and 

self._cover_relations == other._cover_relations) 

if op == op_LT: 

return self.parent().lt(self, other) 

if op == op_LE: 

return self.parent().le(self, other) 

if op == op_GT: 

return self.parent().gt(self, other) 

if op == op_GE: 

return self.parent().ge(self, other) 

 

def __iter__(self): 

r""" 

Iterate through the vertices of ``self``. 

 

EXAMPLES:: 

 

sage: ip = TamariIntervalPoset(4,[(1,2),(3,2)]) 

sage: [i for i in ip] 

[1, 2, 3, 4] 

""" 

return iter(range(1, self.size() + 1)) 

 

def contains_interval(self, other): 

r""" 

Return whether the interval represented by ``other`` is contained 

in ``self`` as an interval of the Tamari lattice. 

 

In terms of interval-posets, it means that all relations of ``self`` 

are relations of ``other``. 

 

INPUT: 

 

- ``other`` -- an interval-poset 

 

EXAMPLES:: 

 

sage: ip1 = TamariIntervalPoset(4,[(1,2),(2,3),(4,3)]) 

sage: ip2 = TamariIntervalPoset(4,[(2,3)]) 

sage: ip2.contains_interval(ip1) 

True 

sage: ip3 = TamariIntervalPoset(4,[(2,1)]) 

sage: ip2.contains_interval(ip3) 

False 

sage: ip4 = TamariIntervalPoset(3,[(2,3)]) 

sage: ip2.contains_interval(ip4) 

False 

""" 

if other.size() != self.size(): 

return False 

for (i, j) in self._cover_relations: 

if not other.le(i, j): 

return False 

return True 

 

def lower_contains_interval(self, other): 

r""" 

Return whether the interval represented by ``other`` is contained 

in ``self`` as an interval of the Tamari lattice and if they share 

the same lower bound. 

 

As interval-posets, it means that ``other`` contains the relations 

of ``self`` plus some extra increasing relations. 

 

INPUT: 

 

- ``other`` -- an interval-poset 

 

EXAMPLES:: 

 

sage: ip1 = TamariIntervalPoset(4,[(1,2),(2,3),(4,3)]); 

sage: ip2 = TamariIntervalPoset(4,[(4,3)]) 

sage: ip2.lower_contains_interval(ip1) 

True 

sage: ip2.contains_interval(ip1) and ip2.lower_binary_tree() == ip1.lower_binary_tree() 

True 

sage: ip3 = TamariIntervalPoset(4,[(4,3),(2,1)]) 

sage: ip2.contains_interval(ip3) 

True 

sage: ip2.lower_binary_tree() == ip3.lower_binary_tree() 

False 

sage: ip2.lower_contains_interval(ip3) 

False 

""" 

if not self.contains_interval(other): 

return False 

for (i, j) in other.decreasing_cover_relations(): 

if not self.le(i, j): 

return False 

return True 

 

def upper_contains_interval(self, other): 

r""" 

Return whether the interval represented by ``other`` is contained 

in ``self`` as an interval of the Tamari lattice and if they share 

the same upper bound. 

 

As interval-posets, it means that ``other`` contains the relations 

of ``self`` plus some extra decreasing relations. 

 

INPUT: 

 

- ``other`` -- an interval-poset 

 

EXAMPLES:: 

 

sage: ip1 = TamariIntervalPoset(4,[(1,2),(2,3),(4,3)]) 

sage: ip2 = TamariIntervalPoset(4,[(1,2),(2,3)]) 

sage: ip2.upper_contains_interval(ip1) 

True 

sage: ip2.contains_interval(ip1) and ip2.upper_binary_tree() == ip1.upper_binary_tree() 

True 

sage: ip3 = TamariIntervalPoset(4,[(1,2),(2,3),(3,4)]) 

sage: ip2.upper_contains_interval(ip3) 

False 

sage: ip2.contains_interval(ip3) 

True 

sage: ip2.upper_binary_tree() == ip3.upper_binary_tree() 

False 

""" 

if not self.contains_interval(other): 

return False 

for (i, j) in other.increasing_cover_relations(): 

if not self.le(i, j): 

return False 

return True 

 

def is_linear_extension(self, perm): 

r""" 

Return whether the permutation ``perm`` is a linear extension 

of ``self``. 

 

INPUT: 

 

- ``perm`` -- a permutation of the size of ``self`` 

 

EXAMPLES:: 

 

sage: ip = TamariIntervalPoset(4,[(1,2),(2,3),(4,3)]) 

sage: ip.is_linear_extension([1,4,2,3]) 

True 

sage: ip.is_linear_extension(Permutation([1,4,2,3])) 

True 

sage: ip.is_linear_extension(Permutation([1,4,3,2])) 

False 

""" 

return self._poset.is_linear_extension(perm) 

 

def contains_binary_tree(self, binary_tree): 

r""" 

Return whether the interval represented by ``self`` contains 

the binary tree ``binary_tree``. 

 

INPUT: 

 

- ``binary_tree`` -- a binary tree 

 

.. SEEALSO:: :meth:`contains_dyck_word` 

 

EXAMPLES:: 

 

sage: ip = TamariIntervalPoset(4,[(2,4),(3,4),(2,1),(3,1)]) 

sage: ip.contains_binary_tree(BinaryTree([[None,[None,[]]],None])) 

True 

sage: ip.contains_binary_tree(BinaryTree([None,[[[],None],None]])) 

True 

sage: ip.contains_binary_tree(BinaryTree([[],[[],None]])) 

False 

sage: ip.contains_binary_tree(ip.lower_binary_tree()) 

True 

sage: ip.contains_binary_tree(ip.upper_binary_tree()) 

True 

sage: all(ip.contains_binary_tree(bt) for bt in ip.binary_trees()) 

True 

 

""" 

return self.is_linear_extension(binary_tree.to_132_avoiding_permutation()) 

 

def contains_dyck_word(self, dyck_word): 

r""" 

Return whether the interval represented by ``self`` contains 

the Dyck word ``dyck_word``. 

 

INPUT: 

 

- ``dyck_word`` -- a Dyck word 

 

.. SEEALSO:: :meth:`contains_binary_tree` 

 

EXAMPLES:: 

 

sage: ip = TamariIntervalPoset(4,[(2,4),(3,4),(2,1),(3,1)]) 

sage: ip.contains_dyck_word(DyckWord([1,1,1,0,0,0,1,0])) 

True 

sage: ip.contains_dyck_word(DyckWord([1,1,0,1,0,1,0,0])) 

True 

sage: ip.contains_dyck_word(DyckWord([1,0,1,1,0,1,0,0])) 

False 

sage: ip.contains_dyck_word(ip.lower_dyck_word()) 

True 

sage: ip.contains_dyck_word(ip.upper_dyck_word()) 

True 

sage: all(ip.contains_dyck_word(bt) for bt in ip.dyck_words()) 

True 

""" 

return self.contains_binary_tree(dyck_word.to_binary_tree_tamari()) 

 

def intersection(self, other): 

r""" 

Return the interval-poset formed by combining the relations from 

both ``self`` and ``other``. It corresponds to the intersection 

of the two corresponding intervals of the Tamari lattice. 

 

INPUT: 

 

- ``other`` -- an interval-poset of the same size as ``self`` 

 

EXAMPLES:: 

 

sage: ip1 = TamariIntervalPoset(4,[(1,2),(2,3)]) 

sage: ip2 = TamariIntervalPoset(4,[(4,3)]) 

sage: ip1.intersection(ip2) 

The Tamari interval of size 4 induced by relations [(1, 2), (2, 3), (4, 3)] 

sage: ip3 = TamariIntervalPoset(4,[(2,1)]) 

sage: ip1.intersection(ip3) 

Traceback (most recent call last): 

... 

ValueError: This intersection is empty, it does not correspond to an interval-poset. 

sage: ip4 = TamariIntervalPoset(3,[(2,3)]) 

sage: ip2.intersection(ip4) 

Traceback (most recent call last): 

... 

ValueError: Intersections are only possible on interval-posets of the same size. 

""" 

if other.size() != self.size(): 

raise ValueError("Intersections are only possible on interval-posets of the same size.") 

try: 

return TamariIntervalPoset(self.size(), self._cover_relations + other._cover_relations) 

except ValueError: 

raise ValueError("This intersection is empty, it does not correspond to an interval-poset.") 

 

def initial_forest(self): 

r""" 

Return the initial forest of ``self``, i.e., the interval-poset 

formed from only the increasing relations of ``self``. 

 

.. SEEALSO:: :meth:`final_forest` 

 

EXAMPLES:: 

 

sage: TamariIntervalPoset(4,[(1,2),(3,2),(2,4),(3,4)]).initial_forest() 

The Tamari interval of size 4 induced by relations [(1, 2), (2, 4), (3, 4)] 

sage: ip = TamariIntervalPoset(4,[(1,2),(2,3)]) 

sage: ip.initial_forest() == ip 

True 

""" 

return TamariIntervalPoset(self.size(), self.increasing_cover_relations(), check=False) 

 

def final_forest(self): 

r""" 

Return the final forest of ``self``, i.e., the interval-poset 

formed with only the decreasing relations of ``self``. 

 

.. SEEALSO:: :meth:`initial_forest` 

 

EXAMPLES:: 

 

sage: TamariIntervalPoset(4,[(2,1),(3,2),(3,4),(4,2)]).final_forest() 

The Tamari interval of size 4 induced by relations [(4, 2), (3, 2), (2, 1)] 

sage: ip = TamariIntervalPoset(3,[(2,1),(3,1)]) 

sage: ip.final_forest() == ip 

True 

""" 

return TamariIntervalPoset(self.size(), self.decreasing_cover_relations(), check=False) 

 

def is_initial_interval(self): 

r""" 

Return if ``self`` corresponds to an initial interval of the Tamari 

lattice, i.e. if its lower end is the smallest element of the lattice. 

It consists of checking that ``self`` does not contain any decreasing 

relations. 

 

.. SEEALSO:: :meth:`is_final_interval` 

 

EXAMPLES:: 

 

sage: ip = TamariIntervalPoset(4, [(1, 2), (2, 4), (3, 4)]) 

sage: ip.is_initial_interval() 

True 

sage: ip.lower_dyck_word() 

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

sage: ip = TamariIntervalPoset(4, [(1, 2), (2, 4), (3, 4), (3, 2)]) 

sage: ip.is_initial_interval() 

False 

sage: ip.lower_dyck_word() 

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

sage: all(DyckWord([1,0,1,0,1,0]).tamari_interval(dw).is_initial_interval() for dw in DyckWords(3)) 

True 

""" 

return not self.decreasing_cover_relations() 

 

def is_final_interval(self): 

r""" 

Return if ``self`` corresponds to a final interval of the Tamari 

lattice, i.e. if its upper end is the largest element of the lattice. 

It consists of checking that ``self`` does not contain any increasing 

relations. 

 

.. SEEALSO:: :meth:`is_initial_interval` 

 

EXAMPLES:: 

 

sage: ip = TamariIntervalPoset(4, [(4, 3), (3, 1), (2, 1)]) 

sage: ip.is_final_interval() 

True 

sage: ip.upper_dyck_word() 

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

sage: ip = TamariIntervalPoset(4, [(4, 3), (3, 1), (2, 1), (2, 3)]) 

sage: ip.is_final_interval() 

False 

sage: ip.upper_dyck_word() 

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

sage: all(dw.tamari_interval(DyckWord([1, 1, 1, 0, 0, 0])).is_final_interval() for dw in DyckWords(3)) 

True 

""" 

return not self.increasing_cover_relations() 

 

def lower_binary_tree(self): 

r""" 

Return the lowest binary tree in the interval of the Tamari 

lattice represented by ``self``. 

 

This is a binary tree. It is the shape of the unique binary 

search tree whose left-branch ordered forest (i.e., the result 

of applying 

:meth:`~sage.combinat.binary_tree.BinaryTree.to_ordered_tree_left_branch` 

and cutting off the root) is the final forest of ``self``. 

 

.. SEEALSO:: :meth:`lower_dyck_word` 

 

EXAMPLES:: 

 

sage: ip = TamariIntervalPoset(6,[(3,2),(4,3),(5,2),(6,5),(1,2),(4,5)]); ip 

The Tamari interval of size 6 induced by relations [(1, 2), (4, 5), (6, 5), (5, 2), (4, 3), (3, 2)] 

sage: ip.lower_binary_tree() 

[[., .], [[., [., .]], [., .]]] 

sage: TamariIntervalPosets.final_forest(ip.lower_binary_tree()) == ip.final_forest() 

True 

sage: ip == TamariIntervalPosets.from_binary_trees(ip.lower_binary_tree(),ip.upper_binary_tree()) 

True 

""" 

return self.min_linear_extension().binary_search_tree_shape(left_to_right=False) 

 

def lower_dyck_word(self): 

r""" 

Return the lowest Dyck word in the interval of the Tamari lattice 

represented by ``self``. 

 

.. SEEALSO:: :meth:`lower_binary_tree` 

 

EXAMPLES:: 

 

sage: ip = TamariIntervalPoset(6,[(3,2),(4,3),(5,2),(6,5),(1,2),(4,5)]); ip 

The Tamari interval of size 6 induced by relations [(1, 2), (4, 5), (6, 5), (5, 2), (4, 3), (3, 2)] 

sage: ip.lower_dyck_word() 

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

sage: TamariIntervalPosets.final_forest(ip.lower_dyck_word()) == ip.final_forest() 

True 

sage: ip == TamariIntervalPosets.from_dyck_words(ip.lower_dyck_word(),ip.upper_dyck_word()) 

True 

""" 

return self.lower_binary_tree().to_dyck_word_tamari() 

 

def upper_binary_tree(self): 

r""" 

Return the highest binary tree in the interval of the Tamari 

lattice represented by ``self``. 

 

This is a binary tree. It is the shape of the unique binary 

search tree whose right-branch ordered forest (i.e., the result 

of applying 

:meth:`~sage.combinat.binary_tree.BinaryTree.to_ordered_tree_right_branch` 

and cutting off the root) is the initial forest of ``self``. 

 

.. SEEALSO:: :meth:`upper_dyck_word` 

 

EXAMPLES:: 

 

sage: ip = TamariIntervalPoset(6,[(3,2),(4,3),(5,2),(6,5),(1,2),(4,5)]); ip 

The Tamari interval of size 6 induced by relations [(1, 2), (4, 5), (6, 5), (5, 2), (4, 3), (3, 2)] 

sage: ip.upper_binary_tree() 

[[., .], [., [[., .], [., .]]]] 

sage: TamariIntervalPosets.initial_forest(ip.upper_binary_tree()) == ip.initial_forest() 

True 

sage: ip == TamariIntervalPosets.from_binary_trees(ip.lower_binary_tree(),ip.upper_binary_tree()) 

True 

""" 

return self.max_linear_extension().binary_search_tree_shape(left_to_right=False) 

 

def upper_dyck_word(self): 

r""" 

Return the highest Dyck word in the interval of the Tamari lattice 

represented by ``self``. 

 

.. SEEALSO:: :meth:`upper_binary_tree` 

 

EXAMPLES:: 

 

sage: ip = TamariIntervalPoset(6,[(3,2),(4,3),(5,2),(6,5),(1,2),(4,5)]); ip 

The Tamari interval of size 6 induced by relations [(1, 2), (4, 5), (6, 5), (5, 2), (4, 3), (3, 2)] 

sage: ip.upper_dyck_word() 

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

sage: TamariIntervalPosets.initial_forest(ip.upper_dyck_word()) == ip.initial_forest() 

True 

sage: ip == TamariIntervalPosets.from_dyck_words(ip.lower_dyck_word(),ip.upper_dyck_word()) 

True 

""" 

return self.upper_binary_tree().to_dyck_word_tamari() 

 

def sub_poset(self, start, end): 

r""" 

Return the renormalized sub-poset of ``self`` consisting solely 

of integers from ``start`` (inclusive) to ``end`` (not inclusive). 

 

"Renormalized" means that these integers are relabelled 

`1,2,\ldots,k` in the obvious way (i.e., by subtracting 

``start - 1``). 

 

INPUT: 

 

- ``start`` -- an integer, the starting vertex (inclusive) 

- ``end`` -- an integer, the ending vertex (not inclusive) 

 

EXAMPLES:: 

 

sage: ip = TamariIntervalPoset(6,[(3,2),(4,3),(5,2),(6,5),(1,2),(3,5),(4,5)]); ip 

The Tamari interval of size 6 induced by relations [(1, 2), (3, 5), (4, 5), (6, 5), (5, 2), (4, 3), (3, 2)] 

sage: ip.sub_poset(1,3) 

The Tamari interval of size 2 induced by relations [(1, 2)] 

sage: ip.sub_poset(1,4) 

The Tamari interval of size 3 induced by relations [(1, 2), (3, 2)] 

sage: ip.sub_poset(1,5) 

The Tamari interval of size 4 induced by relations [(1, 2), (4, 3), (3, 2)] 

sage: ip.sub_poset(1,7) == ip 

True 

sage: ip.sub_poset(1,1) 

The Tamari interval of size 0 induced by relations [] 

""" 

if start < 1 or start > end or end > self.size() + 1: 

raise ValueError("Invalid starting or ending value, accepted: 1 <= start <= end <= size+1") 

if start == end: 

return TamariIntervalPoset(0, []) 

relations = [(i - start + 1, j - start + 1) for (i, j) in self.increasing_cover_relations() if i >= start and j < end] 

relations.extend([(j - start + 1, i - start + 1) for (j, i) in self.decreasing_cover_relations() if i >= start and j < end]) 

return TamariIntervalPoset(end - start, relations, check=False) 

 

def min_linear_extension(self): 

r""" 

Return the minimal permutation for the right weak order which is 

a linear extension of ``self``. 

 

This is also the minimal permutation in the sylvester 

class of ``self.lower_binary_tree()`` and is a 312-avoiding 

permutation. 

 

The right weak order is also known as the right permutohedron 

order. See 

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

for its definition. 

 

EXAMPLES:: 

 

sage: ip = TamariIntervalPoset(4,[(1,2),(2,3),(4,3)]) 

sage: ip.min_linear_extension() 

[1, 2, 4, 3] 

sage: ip = TamariIntervalPoset(6,[(3,2),(4,3),(5,2),(6,5),(1,2),(4,5)]) 

sage: ip.min_linear_extension() 

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

sage: ip = TamariIntervalPoset(0,[]) 

sage: ip.min_linear_extension() 

[] 

sage: ip = TamariIntervalPoset(5, [(1, 4), (2, 4), (3, 4), (5, 4)]); ip 

The Tamari interval of size 5 induced by relations [(1, 4), (2, 4), (3, 4), (5, 4)] 

sage: ip.min_linear_extension() 

[1, 2, 3, 5, 4] 

""" 

# The min linear extension is built by postfix-reading the 

# final forest of ``self``. 

final_forest = DiGraph([list(self), 

self.decreasing_cover_relations()], 

format='vertices_and_edges') 

 

def add(perm, i): 

r""" 

Internal recursive method to compute the min linear extension. 

""" 

for j in sorted(final_forest.neighbors_in(i)): 

add(perm, j) 

perm.append(i) 

perm = [] 

for i in sorted(final_forest.sinks()): 

add(perm, i) 

return Permutation(perm) 

 

def max_linear_extension(self): 

r""" 

Return the maximal permutation for the right weak order which is 

a linear extension of ``self``. 

 

This is also the maximal permutation in the sylvester 

class of ``self.upper_binary_tree()`` and is a 132-avoiding 

permutation. 

 

The right weak order is also known as the right permutohedron 

order. See 

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

for its definition. 

 

EXAMPLES:: 

 

sage: ip = TamariIntervalPoset(4,[(1,2),(2,3),(4,3)]) 

sage: ip.max_linear_extension() 

[4, 1, 2, 3] 

sage: ip = TamariIntervalPoset(6,[(3,2),(4,3),(5,2),(6,5),(1,2),(4,5)]); ip 

The Tamari interval of size 6 induced by relations [(1, 2), (4, 5), (6, 5), (5, 2), (4, 3), (3, 2)] 

sage: ip.max_linear_extension() 

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

sage: ip = TamariIntervalPoset(0,[]); ip 

The Tamari interval of size 0 induced by relations [] 

sage: ip.max_linear_extension() 

[] 

sage: ip = TamariIntervalPoset(5, [(1, 4), (2, 4), (3, 4), (5, 4)]); ip 

The Tamari interval of size 5 induced by relations [(1, 4), (2, 4), (3, 4), (5, 4)] 

sage: ip.max_linear_extension() 

[5, 3, 2, 1, 4] 

""" 

# The max linear extension is built by right-to-left 

# postfix-reading the initial forest of ``self``. 

initial_forest = DiGraph([list(self), 

self.increasing_cover_relations()], 

format='vertices_and_edges') 

 

def add(perm, i): 

r""" 

Internal recursive method to compute the max linear extension. 

""" 

for j in sorted(initial_forest.neighbors_in(i), reverse=True): 

add(perm, j) 

perm.append(i) 

perm = [] 

for i in sorted(initial_forest.sinks(), reverse=True): 

add(perm, i) 

return Permutation(perm) 

 

def linear_extensions(self): 

r""" 

Return an iterator on the permutations which are linear 

extensions of ``self``. 

 

They form an interval of the right weak order (also called the 

right permutohedron order -- see 

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

for a definition). 

 

EXAMPLES:: 

 

sage: ip = TamariIntervalPoset(3,[(1,2),(3,2)]) 

sage: list(ip.linear_extensions()) 

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

sage: ip = TamariIntervalPoset(4,[(1,2),(2,3),(4,3)]) 

sage: list(ip.linear_extensions()) 

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

""" 

for ext in self._poset.linear_extensions(): 

yield Permutation(ext) 

 

def lower_contained_intervals(self): 

r""" 

If ``self`` represents the interval `[t_1, t_2]` of the Tamari 

lattice, return an iterator on all intervals `[t_1,t]` with 

`t \leq t_2` for the Tamari lattice. 

 

In terms of interval-posets, it corresponds to adding all possible 

relations of the form `n` precedes `m` with `n<m`. 

 

EXAMPLES:: 

 

sage: ip = TamariIntervalPoset(4,[(2,4),(3,4),(2,1),(3,1)]) 

sage: list(ip.lower_contained_intervals()) 

[The Tamari interval of size 4 induced by relations [(2, 4), (3, 4), (3, 1), (2, 1)], 

The Tamari interval of size 4 induced by relations [(1, 4), (2, 4), (3, 4), (3, 1), (2, 1)], 

The Tamari interval of size 4 induced by relations [(2, 3), (3, 4), (3, 1), (2, 1)], 

The Tamari interval of size 4 induced by relations [(1, 4), (2, 3), (3, 4), (3, 1), (2, 1)]] 

sage: ip = TamariIntervalPoset(4,[]) 

sage: len(list(ip.lower_contained_intervals())) 

14 

""" 

size = self._size 

yield self 

r""" 

we try to add links recursively in this order : 

1 -> 2 

2 -> 3 

1 -> 3 

3 -> 4 

2 -> 4 

1 -> 4 

... 

("Link" means "relation of the poset".) 

 

One useful feature of interval-posets is that if you add a single 

new relation -- say, `x` precedes `y` -- to an existing 

interval-poset and take the transitive closure, and if the axioms 

of an interval-poset are still satisfied for `(a,c) = (x,y)` and 

for `(a,c) = (y,x)`, then the transitive closure is an 

interval-poset (i.e., roughly speaking, the other new relations 

forced by `x` preceding `y` under transitive closure cannot 

invalidate the axioms). This is helpful when extending 

interval-posets, and is the reason why this and other iterators 

don't yield invalid interval-posets. 

""" 

def add_relations(poset, n, m): 

r""" 

Internal recursive method to generate all possible intervals. 

At every step during the iteration, we have n < m and every 

i satisfying n < i < m satisfies that i precedes m in the 

poset ``poset`` (except when m > size). 

""" 

if n <= 0: 

# if n<=0, then we go to the next m 

n = m 

m += 1 

if m > size: 

# if m>size, it's finished 

return 

 

if poset.le(n, m): 

# there is already a link n->m, so we go to the next n 

for pos in add_relations(poset, n - 1, m): 

yield pos 

elif poset.le(m, n): 

# there is an inverse link m->n, we know we won't be able 

# to create a link i->m with i<=n, so we go to the next m 

for pos in add_relations(poset, m, m + 1): 

yield pos 

else: 

# there is no link n->m 

# first option : we don't create the link and go to the next m 

# (since the lack of a link n->m forbids any links i->m 

# with i<n) 

for pos in add_relations(poset, m, m + 1): 

yield pos 

# second option : we create the link 

# (this is allowed because links i->m already exist for all 

# n<i<m, or else we wouldn't be here) 

poset = TamariIntervalPoset(poset.size(), poset._cover_relations + ((n, m),)) 

yield poset 

# and then, we go to the next n 

for pos in add_relations(poset, n - 1, m): 

yield pos 

 

for inter in add_relations(self, 1, 2): 

yield inter 

 

def interval_cardinality(self): 

r""" 

Return the cardinality of the interval, i.e., the number of elements 

(binary trees or Dyck words) in the interval represented by ``self``. 

 

Not to be confused with :meth:`size` which is the number of 

vertices. 

 

.. SEEALSO:: :meth:`binary_trees` 

 

EXAMPLES:: 

 

sage: TamariIntervalPoset(4,[(2,4),(3,4),(2,1),(3,1)]).interval_cardinality() 

4 

sage: TamariIntervalPoset(4,[]).interval_cardinality() 

14 

sage: TamariIntervalPoset(4,[(1,2),(2,3),(3,4)]).interval_cardinality() 

1 

""" 

return len(list(self.lower_contained_intervals())) 

 

def binary_trees(self): 

r""" 

Return an iterator on all the binary trees in the interval 

represented by ``self``. 

 

.. SEEALSO:: :meth:`interval_cardinality` 

 

EXAMPLES:: 

 

sage: list(TamariIntervalPoset(4,[(2,4),(3,4),(2,1),(3,1)]).binary_trees()) 

[[., [[., [., .]], .]], 

[[., [., [., .]]], .], 

[., [[[., .], .], .]], 

[[., [[., .], .]], .]] 

sage: set(TamariIntervalPoset(4,[]).binary_trees()) == set(BinaryTrees(4)) 

True 

""" 

for ip in self.lower_contained_intervals(): 

yield ip.upper_binary_tree() 

 

def dyck_words(self): 

r""" 

Return an iterator on all the Dyck words in the interval 

represented by ``self``. 

 

EXAMPLES:: 

 

sage: list(TamariIntervalPoset(4,[(2,4),(3,4),(2,1),(3,1)]).dyck_words()) 

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

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

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

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

sage: set(TamariIntervalPoset(4,[]).dyck_words()) == set(DyckWords(4)) 

True 

""" 

for ip in self.lower_contained_intervals(): 

yield ip.upper_dyck_word() 

 

def maximal_chain_tamari_intervals(self): 

r""" 

Return an iterator on the upper contained intervals of one 

longest chain of the Tamari interval represented by ``self``. 

 

If ``self`` represents the interval `[T_1,T_2]` of the Tamari 

lattice, this returns intervals `[T',T_2]` with `T'` following 

one longest chain between `T_1` and `T_2`. 

 

To obtain a longest chain, we use the Tamari inversions of ``self``. 

The elements of the chain are obtained by adding one by one the 

relations `(b,a)` from each Tamari inversion `(a,b)` to ``self``, 

where the Tamari inversions are taken in lexicographic order. 

 

EXAMPLES:: 

 

sage: ip = TamariIntervalPoset(4,[(2,4),(3,4),(2,1),(3,1)]) 

sage: list(ip.maximal_chain_tamari_intervals()) 

[The Tamari interval of size 4 induced by relations [(2, 4), (3, 4), (3, 1), (2, 1)], 

The Tamari interval of size 4 induced by relations [(2, 4), (3, 4), (4, 1), (3, 1), (2, 1)], 

The Tamari interval of size 4 induced by relations [(2, 4), (3, 4), (4, 1), (3, 2), (2, 1)]] 

sage: ip = TamariIntervalPoset(4,[]) 

sage: list(ip.maximal_chain_tamari_intervals()) 

[The Tamari interval of size 4 induced by relations [], 

The Tamari interval of size 4 induced by relations [(2, 1)], 

The Tamari interval of size 4 induced by relations [(3, 1), (2, 1)], 

The Tamari interval of size 4 induced by relations [(4, 1), (3, 1), (2, 1)], 

The Tamari interval of size 4 induced by relations [(4, 1), (3, 2), (2, 1)], 

The Tamari interval of size 4 induced by relations [(4, 2), (3, 2), (2, 1)], 

The Tamari interval of size 4 induced by relations [(4, 3), (3, 2), (2, 1)]] 

""" 

yield self 

n = self.size() 

cover_relations = list(self._cover_relations) 

for inv in self.tamari_inversions_iter(): 

cover_relations.append((inv[1], inv[0])) 

yield TamariIntervalPoset(n, cover_relations, check=False) 

 

def maximal_chain_binary_trees(self): 

r""" 

Return an iterator on the binary trees forming a longest chain of 

``self`` (regarding ``self`` as an interval of the Tamari 

lattice). 

 

EXAMPLES:: 

 

sage: ip = TamariIntervalPoset(4,[(2,4),(3,4),(2,1),(3,1)]) 

sage: list(ip.maximal_chain_binary_trees()) 

[[[., [[., .], .]], .], [., [[[., .], .], .]], [., [[., [., .]], .]]] 

sage: ip = TamariIntervalPoset(4,[]) 

sage: list(ip.maximal_chain_binary_trees()) 

[[[[[., .], .], .], .], 

[[[., [., .]], .], .], 

[[., [[., .], .]], .], 

[., [[[., .], .], .]], 

[., [[., [., .]], .]], 

[., [., [[., .], .]]], 

[., [., [., [., .]]]]] 

""" 

for it in self.maximal_chain_tamari_intervals(): 

yield it.lower_binary_tree() 

 

def maximal_chain_dyck_words(self): 

r""" 

Return an iterator on the Dyck words forming a longest chain of 

``self`` (regarding ``self`` as an interval of the Tamari 

lattice). 

 

EXAMPLES:: 

 

sage: ip = TamariIntervalPoset(4,[(2,4),(3,4),(2,1),(3,1)]) 

sage: list(ip.maximal_chain_dyck_words()) 

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

sage: ip = TamariIntervalPoset(4,[]) 

sage: list(ip.maximal_chain_dyck_words()) 

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

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

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

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

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

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

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

""" 

for it in self.maximal_chain_tamari_intervals(): 

yield it.lower_dyck_word() 

 

def tamari_inversions(self): 

r""" 

Return the Tamari inversions of ``self``. 

 

A Tamari inversion is 

a pair of vertices `(a,b)` with `a < b` such that: 

 

- the decreasing parent of `b` is strictly smaller than `a` (or 

does not exist), and 

- the increasing parent of `a` is strictly bigger than `b` (or 

does not exist). 

 

"Smaller" and "bigger" refer to the numerical values of the 

elements, not to the poset order. 

 

This method returns the list of all Tamari inversions in 

lexicographic order. 

 

The number of Tamari inversions is the length of the 

longest chain of the Tamari interval represented by ``self``. 

 

Indeed, when an interval consists of just one binary tree, it has 

no inversion. One can also prove that if a Tamari interval 

`I' = [T_1', T_2']` is a proper subset of a Tamari interval 

`I = [T_1, T_2]`, then the inversion number of `I'` is strictly 

lower than the inversion number of `I`. And finally, by adding 

the relation `(b,a)` to the interval-poset where `(a,b)` is the 

first inversion of `I` in lexicographic order, one reduces the 

inversion number by exactly `1`. 

 

.. SEEALSO:: 

 

:meth:`tamari_inversions_iter`, :meth:`number_of_tamari_inversions` 

 

EXAMPLES:: 

 

sage: ip = TamariIntervalPoset(3,[]) 

sage: ip.tamari_inversions() 

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

sage: ip = TamariIntervalPoset(3,[(2,1)]) 

sage: ip.tamari_inversions() 

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

sage: ip = TamariIntervalPoset(3,[(1,2)]) 

sage: ip.tamari_inversions() 

[(2, 3)] 

sage: ip = TamariIntervalPoset(3,[(1,2),(3,2)]) 

sage: ip.tamari_inversions() 

[] 

sage: ip = TamariIntervalPoset(4,[(2,4),(3,4),(2,1),(3,1)]) 

sage: ip.tamari_inversions() 

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

sage: ip = TamariIntervalPoset(4,[]) 

sage: ip.tamari_inversions() 

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

sage: all(len(TamariIntervalPosets.from_binary_trees(bt,bt).tamari_inversions())==0 for bt in BinaryTrees(3)) 

True 

sage: all(len(TamariIntervalPosets.from_binary_trees(bt,bt).tamari_inversions())==0 for bt in BinaryTrees(4)) 

True 

 

""" 

return list(self.tamari_inversions_iter()) 

 

def tamari_inversions_iter(self): 

r""" 

Iterate over the Tamari inversions of ``self``, in 

lexicographic order. 

 

See :meth:`tamari_inversions` for the definition of the terms 

involved. 

 

EXAMPLES:: 

 

sage: T = TamariIntervalPoset(5, [[1,2],[3,4],[3,2],[5,2],[4,2]]) 

sage: list(T.tamari_inversions_iter()) 

[(4, 5)] 

 

sage: T = TamariIntervalPoset(8, [(2, 7), (3, 7), (4, 7), (5, 7), (6, 7), (8, 7), (6, 4), (5, 4), (4, 3), (3, 2)]) 

sage: list(T.tamari_inversions_iter()) 

[(1, 2), (1, 7), (5, 6)] 

 

sage: T = TamariIntervalPoset(1, []) 

sage: list(T.tamari_inversions_iter()) 

[] 

 

sage: T = TamariIntervalPoset(0, []) 

sage: list(T.tamari_inversions_iter()) 

[] 

""" 

final_forest = DiGraph([list(self), 

self.decreasing_cover_relations()], 

format='vertices_and_edges') 

initial_forest = DiGraph([list(self), 

self.increasing_cover_relations()], 

format='vertices_and_edges') 

n1 = self.size() + 1 

for a in range(1, self.size()): # a == n will never work 

try: 

ipa = next(initial_forest.neighbor_out_iterator(a)) 

max_b_1 = ipa 

except StopIteration: 

max_b_1 = n1 

for b in range(a + 1, max_b_1): 

try: 

dpb = next(final_forest.neighbor_out_iterator(b)) 

if dpb < a: 

yield (a, b) 

except StopIteration: 

yield (a, b) 

 

def number_of_tamari_inversions(self): 

r""" 

Return the number of Tamari inversions of ``self``. 

 

This is also the length the longest chain of the Tamari 

interval represented by ``self``. 

 

EXAMPLES:: 

 

sage: ip = TamariIntervalPoset(4,[(2,4),(3,4),(2,1),(3,1)]) 

sage: ip.number_of_tamari_inversions() 

2 

sage: ip = TamariIntervalPoset(4,[]) 

sage: ip.number_of_tamari_inversions() 

6 

sage: ip = TamariIntervalPoset(3,[]) 

sage: ip.number_of_tamari_inversions() 

3 

""" 

return len(self.tamari_inversions()) 

 

def number_of_new_components(self): 

""" 

Return the number of terms in the decomposition in new interval-posets. 

 

Every interval-poset has a unique decomposition as a planar tree 

of new interval-posets, as explained in [ChapTamari08]_. This function 

just computes the number of terms, not the planar tree nor 

the terms themselves. 

 

.. SEEALSO:: :meth:`is_new`, :meth:`new_decomposition` 

 

EXAMPLES:: 

 

sage: TIP4 = TamariIntervalPosets(4) 

sage: nb = [u.number_of_new_components() for u in TIP4] 

sage: [nb.count(i) for i in range(1, 5)] 

[12, 21, 21, 14] 

""" 

t_low = self.lower_binary_tree().to_tilting() 

t_up = self.upper_binary_tree().to_tilting() 

return sum(1 for p in t_low if p in t_up) 

 

def new_decomposition(self): 

""" 

Return the decomposition of the interval-poset into 

new interval-posets. 

 

Every interval-poset has a unique decomposition as a planar 

tree of new interval-posets, as explained in 

[ChapTamari08]_. This function computes the terms of this 

decomposition, but not the planar tree. 

 

For the number of terms, you can use instead the method 

:meth:`number_of_new_components`. 

 

OUTPUT: 

 

a list of new interval-posets. 

 

.. SEEALSO:: 

 

:meth:`number_of_new_components`, :meth:`is_new` 

 

EXAMPLES:: 

 

sage: ex = TamariIntervalPosets(4)[11] 

sage: ex.number_of_new_components() 

3 

sage: ex.new_decomposition() 

[The Tamari interval of size 1 induced by relations [], 

The Tamari interval of size 2 induced by relations [], 

The Tamari interval of size 1 induced by relations []] 

 

TESTS:: 

 

sage: ex = TamariIntervalPosets(4).random_element() 

sage: dec = ex.new_decomposition() 

sage: len(dec) == ex.number_of_new_components() 

True 

sage: all(u.is_new() for u in dec) 

True 

""" 

from sage.combinat.binary_tree import BinaryTree 

t_low = self.lower_binary_tree().to_tilting() 

t_up = self.upper_binary_tree().to_tilting() 

common = [p for p in t_low if p in t_up] 

 

def extract_tree(x, y, tilt, common): 

""" 

Extract a tree with root at position xy (recursive). 

""" 

left_tree = None 

for k in range(y - 1, x, -1): 

if (x, k) in tilt: 

if (x, k) not in common: 

left_tree = extract_tree(x, k, tilt, common) 

break 

right_tree = None 

for k in range(x + 1, y): 

if (k, y) in tilt: 

if (k, y) not in common: 

right_tree = extract_tree(k, y, tilt, common) 

break 

return BinaryTree([left_tree, right_tree], check=False) 

 

TIP = self.parent() 

return [TIP.from_binary_trees(extract_tree(cx, cy, t_low, common), 

extract_tree(cx, cy, t_up, common)) 

for cx, cy in common] 

 

def decomposition_to_triple(self): 

""" 

Decompose an interval-poset into a triple (``left``, ``right``, ``r``). 

 

For the inverse method, see 

:meth:`TamariIntervalPosets.recomposition_from_triple`. 

 

OUTPUT: 

 

a triple (``left``, ``right``, ``r``) where ``left`` and 

``right`` are interval-posets and ``r`` (an integer) is the 

parameter of the decomposition. 

 

EXAMPLES:: 

 

sage: tip = TamariIntervalPoset(8, [(1,2), (2,4), (3,4), (6,7), (3,2), (5,4), (6,4), (8,7)]) 

sage: tip.decomposition_to_triple() 

(The Tamari interval of size 3 induced by relations [(1, 2), (3, 2)], 

The Tamari interval of size 4 induced by relations [(2, 3), (4, 3)], 

2) 

sage: tip == TamariIntervalPosets.recomposition_from_triple(*tip.decomposition_to_triple()) 

True 

 

REFERENCES: 

 

- [ChP2015]_ 

""" 

n = self.size() 

if n == 0: 

return None 

root = self.increasing_roots()[-1] 

r = len(self.decreasing_children(root)) 

left = self.sub_poset(1, root) 

right = self.sub_poset(root + 1, n + 1) 

return left, right, r 

 

def grafting_tree(self): 

""" 

Return the grafting tree of the interval-poset. 

 

For the inverse method, 

see :meth:`TamariIntervalPosets.from_grafting_tree`. 

 

EXAMPLES:: 

 

sage: tip = TamariIntervalPoset(8, [(1,2), (2,4), (3,4), (6,7), (3,2), (5,4), (6,4), (8,7)]) 

sage: tip.grafting_tree() 

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

sage: tip == TamariIntervalPosets.from_grafting_tree(tip.grafting_tree()) 

True 

 

REFERENCES: 

 

- [Pons2018]_ 

""" 

n = self.size() 

if n == 0: 

return LabelledBinaryTree(None) 

left, right, r = self.decomposition_to_triple() 

return LabelledBinaryTree([left.grafting_tree(), 

right.grafting_tree()], label=r) 

 

def is_new(self): 

""" 

Return whether ``self`` is a new Tamari interval. 

 

Here 'new' means that the interval is not contained in any 

facet of the associahedron. 

This condition is invariant under complementation. 

 

They have been considered in section 9 of [ChapTamari08]_. 

 

.. SEEALSO:: :meth:`is_modern` 

 

EXAMPLES:: 

 

sage: TIP4 = TamariIntervalPosets(4) 

sage: len([u for u in TIP4 if u.is_new()]) 

12 

 

sage: TIP3 = TamariIntervalPosets(3) 

sage: len([u for u in TIP3 if u.is_new()]) 

3 

""" 

c_up = self.upper_binary_tree().single_edge_cut_shapes() 

c_down = self.lower_binary_tree().single_edge_cut_shapes() 

return not any(x in c_up for x in c_down) 

 

def is_simple(self): 

""" 

Return whether ``self`` is a simple Tamari interval. 

 

Here 'simple' means that the interval contains a unique binary tree. 

 

These intervals define the simple modules over the 

incidence algebras of the Tamari lattices. 

 

.. SEEALSO:: :meth:`is_final_interval`, :meth:`is_initial_interval` 

 

EXAMPLES:: 

 

sage: TIP4 = TamariIntervalPosets(4) 

sage: len([u for u in TIP4 if u.is_simple()]) 

14 

 

sage: TIP3 = TamariIntervalPosets(3) 

sage: len([u for u in TIP3 if u.is_simple()]) 

5 

""" 

return self.upper_binary_tree() == self.lower_binary_tree() 

 

def is_synchronized(self): 

""" 

Return whether ``self`` is a synchronized Tamari interval. 

 

This means that the upper and lower binary trees have the same canopee. 

This condition is invariant under complementation. 

 

This has been considered in [FPR15]_. The numbers of 

synchronized intervals are given by the sequence :oeis:`A000139`. 

 

EXAMPLES:: 

 

sage: len([T for T in TamariIntervalPosets(3) 

....: if T.is_synchronized()]) 

6 

""" 

up = self.upper_binary_tree() 

down = self.lower_binary_tree() 

return down.canopee() == up.canopee() 

 

def is_modern(self): 

r""" 

Return whether ``self`` is a modern Tamari interval. 

 

This is defined by exclusion of a simple pattern in the Hasse diagram, 

namely there is no configuration `y \rightarrow x \leftarrow z` 

with `1 \leq y < x < z \leq n`. 

 

This condition is invariant under complementation. 

 

.. SEEALSO:: :meth:`is_new`, :meth:`is_infinitely_modern` 

 

EXAMPLES:: 

 

sage: len([T for T in TamariIntervalPosets(3) if T.is_modern()]) 

12 

 

REFERENCES: 

 

- [Rog2018]_ 

""" 

G = self.poset().hasse_diagram() 

for x in G: 

nx = G.neighbors_in(x) 

nx.append(x) 

if min(nx) < x < max(nx): 

return False 

return True 

 

def is_infinitely_modern(self): 

r""" 

Return whether ``self`` is an infinitely-modern Tamari interval. 

 

This is defined by the exclusion of the configuration 

`i \rightarrow i + 1` and `j + 1 \rightarrow j` with `i < j`. 

 

This condition is invariant under complementation. 

 

.. SEEALSO:: :meth:`is_new`, :meth:`is_modern` 

 

EXAMPLES:: 

 

sage: len([T for T in TamariIntervalPosets(3) 

....: if T.is_infinitely_modern()]) 

12 

 

REFERENCES: 

 

- [Rog2018]_ 

""" 

n = self.size() 

found = False 

for i in range(1, n): 

if self.le(i, i + 1): 

found = True 

continue 

if self.le(i + 1, i) and found: 

return False 

return True 

 

def is_exceptional(self): 

r""" 

Return whether ``self`` is an exceptional Tamari interval. 

 

This is defined by exclusion of a simple pattern in the Hasse diagram, 

namely there is no configuration ``y <-- x --> z`` 

with `1 \leq y < x < z \leq n`. 

 

This condition is invariant under complementation. 

 

EXAMPLES:: 

 

sage: len([T for T in TamariIntervalPosets(3) 

....: if T.is_exceptional()]) 

12 

""" 

G = self.poset().hasse_diagram() 

for x in G: 

nx = G.neighbors_out(x) 

nx.append(x) 

if min(nx) < x < max(nx): 

return False 

return True 

 

def is_dexter(self): 

r""" 

Return whether ``self`` is a dexter Tamari interval. 

 

This is defined by an exclusion pattern in the Hasse diagram. 

See the code for the exact description. 

 

This condition is not invariant under complementation. 

 

EXAMPLES:: 

 

sage: len([T for T in TamariIntervalPosets(3) if T.is_dexter()]) 

12 

""" 

G = self.poset().hasse_diagram() 

n = self.size() 

for x in range(2, n): 

nx = G.neighbors_out(x) 

nx.append(x) 

y = min(nx) 

if y < x and any(z > x for z in G.neighbor_out_iterator(y)): 

return False 

return True 

 

def is_connected(self): 

""" 

Return whether ``self`` is a connected Tamari interval. 

 

This means that the Hasse diagram is connected. 

 

EXAMPLES:: 

 

sage: len([T for T in TamariIntervalPosets(3) if T.is_connected()]) 

8 

""" 

return self.poset().is_connected() 

 

 

# Abstract class to serve as a Factory ; no instances are created. 

class TamariIntervalPosets(UniqueRepresentation, Parent): 

r""" 

Factory for interval-posets. 

 

INPUT: 

 

- ``size`` -- (optional) an integer 

 

OUTPUT: 

 

- the set of all interval-posets (of the given ``size`` if specified) 

 

EXAMPLES:: 

 

sage: TamariIntervalPosets() 

Interval-posets 

 

sage: TamariIntervalPosets(2) 

Interval-posets of size 2 

 

.. NOTE:: 

 

This is a factory class whose constructor returns instances of 

subclasses. 

""" 

@staticmethod 

def __classcall_private__(cls, n=None): 

r""" 

TESTS:: 

 

sage: from sage.combinat.interval_posets import TamariIntervalPosets_all, TamariIntervalPosets_size 

sage: isinstance(TamariIntervalPosets(2), TamariIntervalPosets_size) 

True 

sage: isinstance(TamariIntervalPosets(), TamariIntervalPosets_all) 

True 

sage: TamariIntervalPosets(2) is TamariIntervalPosets_size(2) 

True 

sage: TamariIntervalPosets() is TamariIntervalPosets_all() 

True 

""" 

if n is None: 

return TamariIntervalPosets_all() 

 

if n not in NN: 

raise ValueError("n must be a non negative integer") 

return TamariIntervalPosets_size(Integer(n)) 

 

# add options to class 

class options(GlobalOptions): 

r""" 

Set and display the options for Tamari interval-posets. 

 

If no parameters are set, then the function returns a copy of 

the options dictionary. 

 

The ``options`` to Tamari interval-posets can be accessed as the method 

:meth:`TamariIntervalPosets.options` of :class:`TamariIntervalPosets` 

and related parent classes. 

 

@OPTIONS@ 

 

EXAMPLES:: 

 

sage: TIP = TamariIntervalPosets 

sage: TIP.options.latex_color_decreasing 

red 

sage: TIP.options.latex_color_decreasing='green' 

sage: TIP.options.latex_color_decreasing 

green 

sage: TIP.options._reset() 

sage: TIP.options.latex_color_decreasing 

red 

""" 

NAME = 'TamariIntervalPosets' 

module = 'sage.combinat.interval_posets' 

latex_tikz_scale = dict(default=1, 

description='the default value for the tikz scale when latexed', 

checker=lambda x: True) # More trouble than it's worth to check 

latex_line_width_scalar = dict(default=0.5, 

description='the default value for the line width as a' 

'multiple of the tikz scale when latexed', 

checker=lambda x: True) # More trouble than it's worth to check 

latex_color_decreasing = dict(default="red", 

description='the default color of decreasing relations when latexed', 

checker=lambda x: True) # More trouble than it's worth to check 

latex_color_increasing = dict(default="blue", 

description='the default color of increasing relations when latexed', 

checker=lambda x: True) # More trouble than it's worth to check 

latex_hspace = dict(default=1, 

description='the default difference between horizontal' 

' coordinates of vertices when latexed', 

checker=lambda x: True) # More trouble than it's worth to check 

latex_vspace = dict(default=1, 

description='the default difference between vertical' 

' coordinates of vertices when latexed', 

checker=lambda x: True) # More trouble than it's worth to check 

 

@staticmethod 

def check_poset(poset): 

r""" 

Check if the given poset ``poset`` is a interval-poset, that is, 

if it satisfies the following properties: 

 

- Its labels are exactly `1, \ldots, n` where `n` is its size. 

- If `a < c` (as numbers) and `a` precedes `c`, then `b` precedes 

`c` for all `b` such that `a < b < c`. 

- If `a < c` (as numbers) and `c` precedes `a`, then `b` precedes 

`a` for all `b` such that `a < b < c`. 

 

INPUT: 

 

- ``poset`` -- a finite labeled poset 

 

EXAMPLES:: 

 

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

sage: TamariIntervalPosets.check_poset(p) 

True 

sage: p = Poset(([2,3],[(3,2)])) 

sage: TamariIntervalPosets.check_poset(p) 

False 

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

sage: TamariIntervalPosets.check_poset(p) 

False 

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

sage: TamariIntervalPosets.check_poset(p) 

False 

""" 

if not set(poset._elements) == set(range(1, poset.cardinality() + 1)): 

return False 

 

for i in range(1, poset.cardinality() + 1): 

stop = False 

for j in range(i - 1, 0, -1): 

if not poset.le(j, i): 

stop = True # j does not precede i so no j'<j should 

elif stop: 

return False 

stop = False 

for j in range(i + 1, poset.cardinality() + 1): 

if not poset.le(j, i): 

stop = True # j does not precede i so no j'>j should 

elif stop: 

return False 

return True 

 

@staticmethod 

def final_forest(element): 

r""" 

Return the final forest of a binary tree, an interval-poset or a 

Dyck word. 

 

A final forest is an interval-poset corresponding to a final 

interval of the Tamari lattice, i.e., containing only decreasing 

relations. 

 

It can be constructed from a binary tree by its binary 

search tree labeling with the rule: `b` precedes 

`a` in the final forest iff `b` is in the right subtree of `a` 

in the binary search tree. 

 

INPUT: 

 

- ``element`` -- a binary tree, a Dyck word or an interval-poset 

 

EXAMPLES:: 

 

sage: ip = TamariIntervalPoset(4,[(1,2),(2,3),(4,3)]) 

sage: TamariIntervalPosets.final_forest(ip) 

The Tamari interval of size 4 induced by relations [(1, 2), (2, 3)] 

 

From binary trees:: 

 

sage: bt = BinaryTree(); bt 

. 

sage: TamariIntervalPosets.final_forest(bt) 

The Tamari interval of size 0 induced by relations [] 

sage: bt = BinaryTree([]); bt 

[., .] 

sage: TamariIntervalPosets.final_forest(bt) 

The Tamari interval of size 1 induced by relations [] 

sage: bt = BinaryTree([[],None]); bt 

[[., .], .] 

sage: TamariIntervalPosets.final_forest(bt) 

The Tamari interval of size 2 induced by relations [] 

sage: bt = BinaryTree([None,[]]); bt 

[., [., .]] 

sage: TamariIntervalPosets.final_forest(bt) 

The Tamari interval of size 2 induced by relations [(2, 1)] 

sage: bt = BinaryTree([[],[]]); bt 

[[., .], [., .]] 

sage: TamariIntervalPosets.final_forest(bt) 

The Tamari interval of size 3 induced by relations [(3, 2)] 

sage: bt = BinaryTree([[None,[[],None]],[]]); bt 

[[., [[., .], .]], [., .]] 

sage: TamariIntervalPosets.final_forest(bt) 

The Tamari interval of size 5 induced by relations [(5, 4), (3, 1), (2, 1)] 

 

From Dyck words:: 

 

sage: dw = DyckWord([1,0]) 

sage: TamariIntervalPosets.final_forest(dw) 

The Tamari interval of size 1 induced by relations [] 

sage: dw = DyckWord([1,1,0,1,0,0,1,1,0,0]) 

sage: TamariIntervalPosets.final_forest(dw) 

The Tamari interval of size 5 induced by relations [(5, 4), (3, 1), (2, 1)] 

""" 

if isinstance(element, TamariIntervalPoset): 

return element.initial_forest() 

elif element in DyckWords(): 

binary_tree = element.to_binary_tree_tamari() 

elif element in BinaryTrees() or element in LabelledBinaryTrees(): 

binary_tree = element 

else: 

raise ValueError("Do not know how to construct the initial forest of {}".format(element)) 

 

def get_relations(bt, start=1): 

r""" 

Recursive method to get the binary tree final forest relations 

with only one recursive reading of the tree. 

 

The vertices are being labelled with integers starting with 

``start``. 

 

OUTPUT: 

 

- the indexes of the nodes on the left border of the tree 

(these become the roots of the forest) 

- the relations of the final forest (as a list of tuples) 

- the next available index for a node (size of tree + 

``start``) 

""" 

if not bt: 

return [], [], start # leaf 

roots, relations, index = get_relations(bt[0], start=start) 

rroots, rrelations, rindex = get_relations(bt[1], start=index + 1) 

roots.append(index) 

relations.extend(rrelations) 

relations.extend([(j, index) for j in rroots]) 

return roots, relations, rindex 

 

roots, relations, index = get_relations(binary_tree) 

return TamariIntervalPoset(index - 1, relations) 

 

@staticmethod 

def initial_forest(element): 

r""" 

Return the initial forest of a binary tree, an interval-poset or 

a Dyck word. 

 

An initial forest is an interval-poset corresponding to an initial 

interval of the Tamari lattice, i.e., containing only increasing 

relations. 

 

It can be constructed from a binary tree by its binary 

search tree labeling with the rule: `a` precedes `b` in the 

initial forest iff `a` is in the left subtree of `b` in the 

binary search tree. 

 

INPUT: 

 

- ``element`` -- a binary tree, a Dyck word or an interval-poset 

 

EXAMPLES:: 

 

sage: ip = TamariIntervalPoset(4,[(1,2),(2,3),(4,3)]) 

sage: TamariIntervalPosets.initial_forest(ip) 

The Tamari interval of size 4 induced by relations [(1, 2), (2, 3)] 

 

with binary trees:: 

 

sage: bt = BinaryTree(); bt 

. 

sage: TamariIntervalPosets.initial_forest(bt) 

The Tamari interval of size 0 induced by relations [] 

sage: bt = BinaryTree([]); bt 

[., .] 

sage: TamariIntervalPosets.initial_forest(bt) 

The Tamari interval of size 1 induced by relations [] 

sage: bt = BinaryTree([[],None]); bt 

[[., .], .] 

sage: TamariIntervalPosets.initial_forest(bt) 

The Tamari interval of size 2 induced by relations [(1, 2)] 

sage: bt = BinaryTree([None,[]]); bt 

[., [., .]] 

sage: TamariIntervalPosets.initial_forest(bt) 

The Tamari interval of size 2 induced by relations [] 

sage: bt = BinaryTree([[],[]]); bt 

[[., .], [., .]] 

sage: TamariIntervalPosets.initial_forest(bt) 

The Tamari interval of size 3 induced by relations [(1, 2)] 

sage: bt = BinaryTree([[None,[[],None]],[]]); bt 

[[., [[., .], .]], [., .]] 

sage: TamariIntervalPosets.initial_forest(bt) 

The Tamari interval of size 5 induced by relations [(1, 4), (2, 3), (3, 4)] 

 

from Dyck words:: 

 

sage: dw = DyckWord([1,0]) 

sage: TamariIntervalPosets.initial_forest(dw) 

The Tamari interval of size 1 induced by relations [] 

sage: dw = DyckWord([1,1,0,1,0,0,1,1,0,0]) 

sage: TamariIntervalPosets.initial_forest(dw) 

The Tamari interval of size 5 induced by relations [(1, 4), (2, 3), (3, 4)] 

""" 

if isinstance(element, TamariIntervalPoset): 

return element.initial_forest() 

elif element in DyckWords(): 

binary_tree = element.to_binary_tree_tamari() 

elif element in BinaryTrees() or element in LabelledBinaryTrees(): 

binary_tree = element 

else: 

raise ValueError("Do not know how to construct the initial forest of {}".format(element)) 

 

def get_relations(bt, start=1): 

r""" 

Recursive method to get the binary tree initial forest 

relations with only one recursive reading of the tree. 

 

The vertices are being labelled with integers starting with 

``start``. 

 

OUTPUT: 

 

- the indexes of the nodes on the right border of the tree 

(these become the roots of the forest) 

- the relations of the initial forest (as a list of tuples) 

- the next available index for a node (size of tree + 

``start``) 

""" 

if not bt: 

return [], [], start # leaf 

lroots, lrelations, index = get_relations(bt[0], start=start) 

roots, relations, rindex = get_relations(bt[1], start=index + 1) 

roots.append(index) 

relations.extend(lrelations) 

relations.extend([(j, index) for j in lroots]) 

return roots, relations, rindex 

 

roots, relations, index = get_relations(binary_tree) 

return TamariIntervalPoset(index - 1, relations) 

 

@staticmethod 

def from_binary_trees(tree1, tree2): 

r""" 

Return the interval-poset corresponding to the interval 

[``tree1``, ``tree2``] of the Tamari lattice. Raise an exception if 

``tree1`` is not `\leq` ``tree2`` in the Tamari lattice. 

 

INPUT: 

 

- ``tree1`` -- a binary tree 

- ``tree2`` -- a binary tree greater or equal than ``tree1`` for 

the Tamari lattice 

 

EXAMPLES:: 

 

sage: tree1 = BinaryTree([[],None]) 

sage: tree2 = BinaryTree([None,[]]) 

sage: TamariIntervalPosets.from_binary_trees(tree1,tree2) 

The Tamari interval of size 2 induced by relations [] 

sage: TamariIntervalPosets.from_binary_trees(tree1,tree1) 

The Tamari interval of size 2 induced by relations [(1, 2)] 

sage: TamariIntervalPosets.from_binary_trees(tree2,tree2) 

The Tamari interval of size 2 induced by relations [(2, 1)] 

 

sage: tree1 = BinaryTree([[],[[None,[]],[]]]) 

sage: tree2 = BinaryTree([None,[None,[None,[[],[]]]]]) 

sage: TamariIntervalPosets.from_binary_trees(tree1,tree2) 

The Tamari interval of size 6 induced by relations [(4, 5), (6, 5), (5, 2), (4, 3), (3, 2)] 

 

sage: tree3 = BinaryTree([None,[None,[[],[None,[]]]]]) 

sage: TamariIntervalPosets.from_binary_trees(tree1,tree3) 

Traceback (most recent call last): 

... 

ValueError: The two binary trees are not comparable on the Tamari lattice. 

sage: TamariIntervalPosets.from_binary_trees(tree1,BinaryTree()) 

Traceback (most recent call last): 

... 

ValueError: The two binary trees are not comparable on the Tamari lattice. 

""" 

initial_forest = TamariIntervalPosets.initial_forest(tree2) 

final_forest = TamariIntervalPosets.final_forest(tree1) 

try: 

return initial_forest.intersection(final_forest) 

except Exception: 

raise ValueError("The two binary trees are not comparable on the Tamari lattice.") 

 

@staticmethod 

def from_dyck_words(dw1, dw2): 

r""" 

Return the interval-poset corresponding to the interval 

[``dw1``, ``dw2``] of the Tamari lattice. Raise an exception if the 

two Dyck words ``dw1`` and ``dw2`` do not satisfy 

``dw1`` `\leq` ``dw2`` in the Tamari lattice. 

 

INPUT: 

 

- ``dw1`` -- a Dyck word 

- ``dw2`` -- a Dyck word greater or equal than ``dw1`` for 

the Tamari lattice 

 

EXAMPLES:: 

 

sage: dw1 = DyckWord([1,0,1,0]) 

sage: dw2 = DyckWord([1,1,0,0]) 

sage: TamariIntervalPosets.from_dyck_words(dw1,dw2) 

The Tamari interval of size 2 induced by relations [] 

sage: TamariIntervalPosets.from_dyck_words(dw1,dw1) 

The Tamari interval of size 2 induced by relations [(1, 2)] 

sage: TamariIntervalPosets.from_dyck_words(dw2,dw2) 

The Tamari interval of size 2 induced by relations [(2, 1)] 

 

sage: dw1 = DyckWord([1,0,1,1,1,0,0,1,1,0,0,0]) 

sage: dw2 = DyckWord([1,1,1,1,0,1,1,0,0,0,0,0]) 

sage: TamariIntervalPosets.from_dyck_words(dw1,dw2) 

The Tamari interval of size 6 induced by relations [(4, 5), (6, 5), (5, 2), (4, 3), (3, 2)] 

 

sage: dw3 = DyckWord([1,1,1,0,1,1,1,0,0,0,0,0]) 

sage: TamariIntervalPosets.from_dyck_words(dw1,dw3) 

Traceback (most recent call last): 

... 

ValueError: The two Dyck words are not comparable on the Tamari lattice. 

sage: TamariIntervalPosets.from_dyck_words(dw1,DyckWord([1,0])) 

Traceback (most recent call last): 

... 

ValueError: The two Dyck words are not comparable on the Tamari lattice. 

""" 

tree1 = dw1.to_binary_tree_tamari() 

tree2 = dw2.to_binary_tree_tamari() 

try: 

return TamariIntervalPosets.from_binary_trees(tree1, tree2) 

except Exception: 

raise ValueError("The two Dyck words are not comparable on the Tamari lattice.") 

 

@staticmethod 

def recomposition_from_triple(left, right, r): 

""" 

Recompose an interval-poset from a triple (``left``, ``right``, ``r``). 

 

For the inverse method, 

see :meth:`TamariIntervalPoset.decomposition_to_triple`. 

 

INPUT: 

 

- ``left`` -- an interval-poset 

- ``right`` -- an interval-poset 

- ``r`` -- the parameter of the decomposition, an integer 

 

OUTPUT: an interval-poset 

 

EXAMPLES:: 

 

sage: T1 = TamariIntervalPoset(3, [(1, 2), (3, 2)]) 

sage: T2 = TamariIntervalPoset(4, [(2, 3), (4, 3)]) 

sage: TamariIntervalPosets.recomposition_from_triple(T1, T2, 2) 

The Tamari interval of size 8 induced by relations [(1, 2), (2, 4), 

(3, 4), (6, 7), (8, 7), (6, 4), (5, 4), (3, 2)] 

 

REFERENCES: 

 

- [Pons2018]_ 

""" 

root = left.size() + 1 

rel = left.poset().cover_relations() 

rel.extend((i, root) for i in left) 

rel.extend((root + a, root + b) 

for a, b in right.poset().cover_relations()) 

decroot = right.decreasing_roots()[:r] 

rel.extend((root + i, root) for i in decroot) 

return TamariIntervalPoset(left.size() + right.size() + 1, rel) 

 

@staticmethod 

def from_grafting_tree(tree): 

""" 

Return an interval-poset from a grafting tree. 

 

For the inverse method, 

see :meth:`TamariIntervalPoset.grafting_tree`. 

 

EXAMPLES:: 

 

sage: tip = TamariIntervalPoset(8, [(1,2), (2,4), (3,4), (6,7), (3,2), (5,4), (6,4), (8,7)]) 

sage: t = tip.grafting_tree() 

sage: TamariIntervalPosets.from_grafting_tree(t) == tip 

True 

 

REFERENCES: 

 

- [Pons2018]_ 

""" 

if tree.is_empty(): 

return TamariIntervalPoset(0, []) 

r = tree.label() 

left = TamariIntervalPosets.from_grafting_tree(tree[0]) 

right = TamariIntervalPosets.from_grafting_tree(tree[1]) 

return TamariIntervalPosets.recomposition_from_triple(left, right, r) 

 

@staticmethod 

def from_minimal_schnyder_wood(graph): 

""" 

Return a Tamari interval built from a minimal Schnyder wood. 

 

This is an implementation of Bernardi and Bonichon's bijection 

[BerBon]_. 

 

INPUT: 

 

a minimal Schnyder wood, given as a graph with colored and 

oriented edges, without the three exterior unoriented edges 

 

The three boundary vertices must be 'a', 'b' and 'c'. 

 

One assumes moreover that the embedding around 'a' is the 

list of neighbors of 'a' and not just a cyclic permutation of that. 

 

Beware that the embedding convention used here is the opposite of 

the one used by the plot method. 

 

OUTPUT: 

 

a Tamari interval-poset 

 

EXAMPLES: 

 

A small example:: 

 

sage: TIP = TamariIntervalPosets 

sage: G = DiGraph([(0,'a',0),(0,'b',1),(0,'c',2)], format='list_of_edges') 

sage: G.set_embedding({'a':[0],'b':[0],'c':[0],0:['a','b','c']}) 

sage: TIP.from_minimal_schnyder_wood(G) 

The Tamari interval of size 1 induced by relations [] 

 

An example from page 14 of [BerBon]_:: 

 

sage: c0 = [(0,'a'),(1,0),(2,0),(4,3),(3,'a'),(5,3)] 

sage: c1 = [(5,'b'),(3,'b'),(4,5),(1,3),(2,3),(0,3)] 

sage: c2 = [(0,'c'),(1,'c'),(3,'c'),(4,'c'),(5,'c'),(2,1)] 

sage: ed = [(u,v,0) for u,v in c0] 

sage: ed += [(u,v,1) for u,v in c1] 

sage: ed += [(u,v,2) for u,v in c2] 

sage: G = DiGraph(ed, format='list_of_edges') 

sage: embed = {'a':[3,0],'b':[5,3],'c':[0,1,3,4,5]} 

sage: data_emb = [[3,2,1,'c','a'],[2,3,'c',0],[3,1,0]] 

sage: data_emb += [['b',5,4,'c',1,2,0,'a'],[5,'c',3],['b','c',4,3]] 

sage: for k in range(6): 

....: embed[k] = data_emb[k] 

sage: G.set_embedding(embed) 

sage: TIP.from_minimal_schnyder_wood(G) 

The Tamari interval of size 6 induced by relations [(1, 4), (2, 4), (3, 4), (5, 6), (6, 4), (5, 4), (3, 1), (2, 1)] 

 

An example from page 18 of [BerBon]_:: 

 

sage: c0 = [(0,'a'),(1,0),(2,'a'),(3,2),(4,2),(5,'a')] 

sage: c1 = [(5,'b'),(2,'b'),(4,'b'),(3,4),(1,2),(0,2)] 

sage: c2 = [(0,'c'),(1,'c'),(3,'c'),(4,'c'),(2,'c'),(5,2)] 

sage: ed = [(u,v,0) for u,v in c0] 

sage: ed += [(u,v,1) for u,v in c1] 

sage: ed += [(u,v,2) for u,v in c2] 

sage: G = DiGraph(ed, format='list_of_edges') 

sage: embed = {'a':[5,2,0],'b':[4,2,5],'c':[0,1,2,3,4]} 

sage: data_emb = [[2,1,'c','a'],[2,'c',0],[3,'c',1,0,'a',5,'b',4]] 

sage: data_emb += [[4,'c',2],['b','c',3,2],['b',2,'a']] 

sage: for k in range(6): 

....: embed[k] = data_emb[k] 

sage: G.set_embedding(embed) 

sage: TIP.from_minimal_schnyder_wood(G) 

The Tamari interval of size 6 induced by relations [(1, 3), (2, 3), (4, 5), (5, 3), (4, 3), (2, 1)] 

 

Another small example:: 

 

sage: c0 = [(0,'a'),(2,'a'),(1,0)] 

sage: c1 = [(2,'b'),(1,'b'),(0,2)] 

sage: c2 = [(0,'c'),(1,'c'),(2,1)] 

sage: ed = [(u,v,0) for u,v in c0] 

sage: ed += [(u,v,1) for u,v in c1] 

sage: ed += [(u,v,2) for u,v in c2] 

sage: G = DiGraph(ed, format='list_of_edges') 

sage: embed = {'a':[2,0],'b':[1,2],'c':[0,1]} 

sage: data_emb = [[2,1,'c','a'],['c',0,2,'b'],['b',1,0,'a']] 

sage: for k in range(3): 

....: embed[k] = data_emb[k] 

sage: G.set_embedding(embed) 

sage: TIP.from_minimal_schnyder_wood(G) 

The Tamari interval of size 3 induced by relations [(2, 3), (2, 1)] 

 

REFERENCES: 

 

.. [BerBon] Olivier Bernardi and Nicolas Bonichon, *Intervals in Catalan 

lattices and realizers of triangulations*, JCTA 116 (2009) 

""" 

from sage.graphs.digraph import DiGraph 

from sage.combinat.dyck_word import DyckWord 

color_a = graph.incoming_edges('a')[0][2] 

color_b = graph.incoming_edges('b')[0][2] 

 

embedding = graph.get_embedding() 

graph0 = DiGraph([e for e in graph.edges(sort=False) 

if e[2] == color_a], 

format='list_of_edges') 

restricted_embedding = {u: [v for v in embedding[u] 

if v in graph0.neighbors_in(u) or 

v in graph0.neighbors_out(u)] 

for u in graph0} 

 

voisins_in = {} 

for u in graph0: 

if u != 'a': 

bad_emb = restricted_embedding[u] 

sortie = graph0.neighbors_out(u)[0] 

idx = bad_emb.index(sortie) 

restricted_embedding[u] = bad_emb[idx:] + bad_emb[:idx] 

voisins_in[u] = restricted_embedding[u][1:] 

else: 

voisins_in[u] = list(restricted_embedding[u]) 

voisins_in[u].reverse() # pour les avoir dans le bon sens 

 

graph0.set_embedding(restricted_embedding) 

 

def clockwise_labelling(gr, vertex): 

if len(gr) == 1: 

return [vertex] 

else: 

lbl = [vertex] 

for w in voisins_in[vertex]: 

lbl += clockwise_labelling(gr, w) 

return lbl 

 

def profil(gr, vertex): 

if len(gr) == 1: 

return [] 

else: 

lbl = [] 

for w in voisins_in[vertex]: 

lbl += [1] + profil(gr, w) + [0] 

return lbl 

 

dyckword_bottom = profil(graph0, 'a') 

# this is the profile of the planar graph graph0 

 

liste = clockwise_labelling(graph0, 'a')[1:] 

relabelling = {l: i for i, l in enumerate(liste)} 

for l in ['a', 'b', 'c']: 

relabelling[l] = l 

new_graph = graph.relabel(relabelling, inplace=False) 

 

dyckword_top = [] 

for i in range(1, len(graph) - 3): 

indegree1 = len([u for u in new_graph.incoming_edges(i) 

if u[2] == color_b]) 

dyckword_top += [1] + [0] * indegree1 

indegree1 = len([u for u in new_graph.incoming_edges('b') 

if u[2] == color_b]) 

dyckword_top += [1] + [0] * indegree1 

 

dyckword_bottom = DyckWord(dyckword_bottom) 

dyckword_top = DyckWord(dyckword_top) 

TIP = TamariIntervalPosets(len(dyckword_bottom) // 2) 

return TIP.from_dyck_words(dyckword_bottom, dyckword_top) 

 

def __call__(self, *args, **keywords): 

r""" 

Allows for a poset to be directly transformed into an interval-poset. 

 

It is some kind of coercion but cannot be made through the coercion 

system because posets do not have parents. 

 

EXAMPLES:: 

 

sage: TIP = TamariIntervalPosets() 

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

sage: TIP(p) 

The Tamari interval of size 3 induced by relations [(1, 2)] 

sage: TIP(TIP(p)) 

The Tamari interval of size 3 induced by relations [(1, 2)] 

sage: TIP(3,[(1,2)]) 

The Tamari interval of size 3 induced by relations [(1, 2)] 

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

sage: TIP(p) 

Traceback (most recent call last): 

... 

ValueError: This does not satisfy the Tamari interval-poset condition. 

""" 

if isinstance(args[0], TamariIntervalPoset): 

return args[0] 

if len(args) == 1 and isinstance(args[0], FinitePoset): 

return self.element_class(self, args[0].cardinality(), args[0].cover_relations()) 

 

return super(TamariIntervalPosets, self).__call__(*args, **keywords) 

 

def le(self, el1, el2): 

r""" 

Poset stucture on the set of interval-posets through interval 

containment. 

 

Return whether the interval represented by ``el1`` is contained in 

the interval represented by ``el2``. 

 

INPUT: 

 

- ``el1`` -- an interval-poset 

- ``el2`` -- an interval-poset 

 

EXAMPLES:: 

 

sage: ip1 = TamariIntervalPoset(4,[(1,2),(2,3),(4,3)]) 

sage: ip2 = TamariIntervalPoset(4,[(1,2),(2,3)]) 

sage: TamariIntervalPosets().le(ip1,ip2) 

True 

sage: TamariIntervalPosets().le(ip2,ip1) 

False 

""" 

return el2.contains_interval(el1) 

 

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

# Enumerated set of all Tamari Interval-posets 

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

 

 

class TamariIntervalPosets_all(DisjointUnionEnumeratedSets, TamariIntervalPosets): 

r""" 

The enumerated set of all Tamari interval-posets. 

""" 

def __init__(self): 

r""" 

TESTS:: 

 

sage: from sage.combinat.interval_posets import TamariIntervalPosets_all 

sage: S = TamariIntervalPosets_all() 

sage: S.cardinality() 

+Infinity 

 

sage: it = iter(S) 

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

[The Tamari interval of size 0 induced by relations [], 

The Tamari interval of size 1 induced by relations [], 

The Tamari interval of size 2 induced by relations [], 

The Tamari interval of size 2 induced by relations [(2, 1)], 

The Tamari interval of size 2 induced by relations [(1, 2)]] 

sage: next(it).parent() 

Interval-posets 

sage: S(0,[]) 

The Tamari interval of size 0 induced by relations [] 

 

sage: S is TamariIntervalPosets_all() 

True 

sage: TestSuite(S).run() # long time (7s) 

""" 

DisjointUnionEnumeratedSets.__init__( 

self, Family(NonNegativeIntegers(), TamariIntervalPosets_size), 

facade=True, keepkey=False, category=(Posets(), EnumeratedSets())) 

 

def _repr_(self): 

r""" 

TESTS:: 

 

sage: TamariIntervalPosets() 

Interval-posets 

""" 

return "Interval-posets" 

 

def _element_constructor_(self, size, relations): 

r""" 

EXAMPLES:: 

 

sage: TIP = TamariIntervalPosets() 

sage: TIP(3,[(1,2)]) 

The Tamari interval of size 3 induced by relations [(1, 2)] 

""" 

return self.element_class(self, size, relations) 

 

def __contains__(self, x): 

r""" 

TESTS:: 

 

sage: S = TamariIntervalPosets() 

sage: 1 in S 

False 

sage: S(0,[]) in S 

True 

""" 

return isinstance(x, self.element_class) 

 

Element = TamariIntervalPoset 

 

 

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

# Enumerated set of Tamari interval-posets of a given size 

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

class TamariIntervalPosets_size(TamariIntervalPosets): 

r""" 

The enumerated set of interval-posets of a given size. 

""" 

def __init__(self, size): 

r""" 

TESTS:: 

 

sage: S = TamariIntervalPosets(3) 

sage: assert S is TamariIntervalPosets(3) 

sage: for i in range(5): TestSuite(TamariIntervalPosets(i)).run() 

""" 

# there is a natural order on interval-posets through inclusions 

# that is why we use the FinitePosets category 

super(TamariIntervalPosets_size, self).__init__(category=(FinitePosets(), FiniteEnumeratedSets())) 

 

self._size = size 

 

def _repr_(self): 

r""" 

TESTS:: 

 

sage: TamariIntervalPosets(3) 

Interval-posets of size 3 

""" 

return "Interval-posets of size {}".format(self._size) 

 

def __contains__(self, x): 

r""" 

TESTS:: 

 

sage: S = TamariIntervalPosets(3) 

sage: 1 in S 

False 

sage: S([]) in S 

True 

""" 

return isinstance(x, self.element_class) and x.size() == self._size 

 

def cardinality(self): 

r""" 

The cardinality of ``self``. That is, the number of 

interval-posets of size `n`. 

 

The formula was given in [ChapTamari08]_: 

 

.. MATH:: 

 

\frac{2(4n+1)!}{(n+1)!(3n+2)!} 

= \frac{2}{n(n+1)} \binom{4n+1}{n-1}. 

 

EXAMPLES:: 

 

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

[1, 1, 3, 13, 68, 399] 

""" 

from sage.arith.all import binomial 

n = self._size 

if n == 0: 

return Integer(1) 

return (2 * binomial(4 * n + 1, n - 1)) // (n * (n + 1)) 

# return Integer(2 * factorial(4*n+1)/(factorial(n+1)*factorial(3*n+2))) 

 

def __iter__(self): 

r""" 

Recursive generation: we iterate through all interval-posets of 

size ``size - 1`` and add all possible relations to the last 

vertex. 

 

This works thanks to the fact that the restriction of an 

interval-poset of size `n` to the subset `\{1, 2, \ldots, k\}` for 

a fixed `k \leq n` is an interval-poset. 

 

TESTS:: 

 

sage: TIP1 = TamariIntervalPosets(1) 

sage: list(TIP1) 

[The Tamari interval of size 1 induced by relations []] 

sage: TIP2 = TamariIntervalPosets(2) 

sage: list(TIP2) 

[The Tamari interval of size 2 induced by relations [], 

The Tamari interval of size 2 induced by relations [(2, 1)], 

The Tamari interval of size 2 induced by relations [(1, 2)]] 

sage: TIP3 = TamariIntervalPosets(3) 

sage: list(TIP3) 

[The Tamari interval of size 3 induced by relations [], 

The Tamari interval of size 3 induced by relations [(3, 2)], 

The Tamari interval of size 3 induced by relations [(2, 3)], 

The Tamari interval of size 3 induced by relations [(1, 3), (2, 3)], 

The Tamari interval of size 3 induced by relations [(2, 1)], 

The Tamari interval of size 3 induced by relations [(3, 2), (2, 1)], 

The Tamari interval of size 3 induced by relations [(3, 1), (2, 1)], 

The Tamari interval of size 3 induced by relations [(2, 3), (2, 1)], 

The Tamari interval of size 3 induced by relations [(2, 3), (3, 1), (2, 1)], 

The Tamari interval of size 3 induced by relations [(1, 3), (2, 3), (2, 1)], 

The Tamari interval of size 3 induced by relations [(1, 2)], 

The Tamari interval of size 3 induced by relations [(1, 2), (3, 2)], 

The Tamari interval of size 3 induced by relations [(1, 2), (2, 3)]] 

sage: all(len(list(TamariIntervalPosets(i)))==TamariIntervalPosets(i).cardinality() for i in range(6)) 

True 

""" 

n = self._size 

if n <= 1: 

yield TamariIntervalPoset(n, [], check=False) 

return 

 

for tip in TamariIntervalPosets(n - 1): 

new_tip = TamariIntervalPoset(n, tip._cover_relations, check=False) 

yield new_tip # we have added an extra vertex but no relations 

 

# adding a decreasing relation n>>m2 with m2<n and no 

# increasing relations 

for m2 in range(n - 1, 0, -1): 

if new_tip.le(n - 1, m2): 

yield TamariIntervalPoset(n, new_tip._cover_relations + ((n, m2),), check=False) 

 

for m in range(n - 1, 0, -1): 

# adding an increasing relation m>>n 

if not new_tip.le(m, n): 

new_tip = TamariIntervalPoset(n, new_tip._cover_relations + ((m, n),), check=False) 

yield new_tip 

else: 

continue 

 

# further adding a decreasing relation n>>m2 with m2<m 

for m2 in range(m - 1, 0, -1): 

if new_tip.le(n - 1, m2): 

yield TamariIntervalPoset(n, new_tip._cover_relations + ((n, m2),), check=False) 

 

def random_element(self): 

""" 

Return a random Tamari interval of fixed size. 

 

This is obtained by first creating a random rooted 

planar triangulation, then computing its unique 

minimal Schnyder wood, then applying a bijection 

of Bernardi and Bonichon [BerBon]_. 

 

Because the random rooted planar triangulation is 

chosen uniformly at random, the Tamari interval is 

also chosen according to the uniform distribution. 

 

EXAMPLES:: 

 

sage: T = TamariIntervalPosets(4).random_element() 

sage: T.parent() 

Interval-posets 

sage: u = T.lower_dyck_word(); u # random 

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

sage: v = T.lower_dyck_word(); v # random 

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

sage: len(u) 

8 

""" 

from sage.graphs.schnyder import minimal_schnyder_wood 

from sage.graphs.generators.random import RandomTriangulation 

n = self._size 

tri = RandomTriangulation(n + 3) 

TIP = TamariIntervalPosets 

schnyder = minimal_schnyder_wood(tri, root_edge=('a', 'b'), 

check=False) 

return TIP.from_minimal_schnyder_wood(schnyder) 

 

@lazy_attribute 

def _parent_for(self): 

r""" 

The parent of the element generated by ``self``. 

 

TESTS:: 

 

sage: TIP3 = TamariIntervalPosets(3) 

sage: TIP3._parent_for 

Interval-posets 

""" 

return TamariIntervalPosets_all() 

 

# This is needed because this is a facade parent via DisjointUnionEnumeratedSets 

@lazy_attribute 

def element_class(self): 

r""" 

TESTS:: 

 

sage: S = TamariIntervalPosets(3) 

sage: S.element_class 

<class 'sage.combinat.interval_posets.TamariIntervalPosets_all_with_category.element_class'> 

sage: S.first().__class__ == TamariIntervalPosets().first().__class__ 

True 

""" 

return self._parent_for.element_class 

 

def _element_constructor_(self, relations): 

r""" 

EXAMPLES:: 

 

sage: TIP3 = TamariIntervalPosets(3) 

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

The Tamari interval of size 3 induced by relations [(1, 2)] 

sage: TIP3([(3,4)]) 

Traceback (most recent call last): 

... 

ValueError: The relations do not correspond to the size of the poset. 

""" 

return self.element_class(self, self._size, relations)