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

r""" 

Mutable Poset 

 

This module provides a class representing a finite partially ordered 

set (poset) for the purpose of being used as a data structure. Thus 

the posets introduced in this module are mutable, i.e., elements can 

be added and removed from a poset at any time. 

 

To get in touch with Sage's "usual" posets, start with the page 

:mod:`Posets <sage.combinat.posets.__init__>` in the reference manual. 

 

 

.. _mutable_poset_examples: 

 

Examples 

======== 

 

First Steps 

----------- 

 

We start by creating an empty poset. This is simply done by 

 

:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: P = MP() 

sage: P 

poset() 

 

A poset should contain elements, thus let us add them with 

 

:: 

 

sage: P.add(42) 

sage: P.add(7) 

sage: P.add(13) 

sage: P.add(3) 

 

Let us look at the poset again:: 

 

sage: P 

poset(3, 7, 13, 42) 

 

We see that they elements are sorted using `\leq` which exists on the 

integers `\ZZ`. Since this is even a total order, we could have used a 

more efficient data structure. Alternatively, we can write 

:: 

 

sage: MP([42, 7, 13, 3]) 

poset(3, 7, 13, 42) 

 

to add several elements at once on construction. 

 

 

A less boring Example 

--------------------- 

 

Let us continue with a less boring example. We define the class 

 

:: 

 

sage: class T(tuple): 

....: def __le__(left, right): 

....: return all(l <= r for l, r in zip(left, right)) 

 

It is equipped with a `\leq`-operation such that `a \leq b` if all 

entries of `a` are at most the corresponding entry of `b`. For 

example, we have 

 

:: 

 

sage: a = T((1,1)) 

sage: b = T((2,1)) 

sage: c = T((1,2)) 

sage: a <= b, a <= c, b <= c 

(True, True, False) 

 

The last comparison gives ``False``, since the comparison of the 

first component checks whether `2 \leq 1`. 

 

Now, let us add such elements to a poset:: 

 

sage: Q = MP([T((1, 1)), T((3, 3)), T((4, 1)), 

....: T((3, 2)), T((2, 3)), T((2, 2))]); Q 

poset((1, 1), (2, 2), (2, 3), (3, 2), (3, 3), (4, 1)) 

 

In the representation above, the elements are sorted topologically, 

smallest first. This does not (directly) show more structural 

information. We can overcome this and display a "wiring layout" by 

typing:: 

 

sage: print(Q.repr_full(reverse=True)) 

poset((3, 3), (2, 3), (3, 2), (2, 2), (4, 1), (1, 1)) 

+-- oo 

| +-- no successors 

| +-- predecessors: (3, 3), (4, 1) 

+-- (3, 3) 

| +-- successors: oo 

| +-- predecessors: (2, 3), (3, 2) 

+-- (2, 3) 

| +-- successors: (3, 3) 

| +-- predecessors: (2, 2) 

+-- (3, 2) 

| +-- successors: (3, 3) 

| +-- predecessors: (2, 2) 

+-- (2, 2) 

| +-- successors: (2, 3), (3, 2) 

| +-- predecessors: (1, 1) 

+-- (4, 1) 

| +-- successors: oo 

| +-- predecessors: (1, 1) 

+-- (1, 1) 

| +-- successors: (2, 2), (4, 1) 

| +-- predecessors: null 

+-- null 

| +-- successors: (1, 1) 

| +-- no predecessors 

 

Note that we use ``reverse=True`` to let the elements appear from 

largest (on the top) to smallest (on the bottom). 

 

If you look at the output above, you'll see two additional elements, 

namely ``oo`` (`\infty`) and ``null`` (`\emptyset`). So what are these 

strange animals? The answer is simple and maybe you can guess it 

already. The `\infty`-element is larger than every other element, 

therefore a successor of the maximal elements in the poset. Similarly, 

the `\emptyset`-element is smaller than any other element, therefore a 

predecessor of the poset's minimal elements. Both do not have to scare 

us; they are just there and sometimes useful. 

 

 

AUTHORS: 

 

- Daniel Krenn (2015) 

 

ACKNOWLEDGEMENT: 

 

- Daniel Krenn is supported by the Austrian Science Fund (FWF): P 24644-N26. 

 

Classes and their Methods 

========================= 

""" 

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

# Copyright (C) 2015 Daniel Krenn <dev@danielkrenn.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 import itervalues 

 

from sage.structure.sage_object import SageObject 

 

 

class MutablePosetShell(SageObject): 

r""" 

A shell for an element of a :class:`mutable poset <MutablePoset>`. 

 

INPUT: 

 

- ``poset`` -- the poset to which this shell belongs. 

 

- ``element`` -- the element which should be 

contained/encapsulated in this shell. 

 

OUTPUT: 

 

A shell for the given element. 

 

.. NOTE:: 

 

If the :meth:`element` of a shell is ``None``, then this 

element is considered as "special" (see :meth:`is_special`). 

There are two special elements, namely 

 

- a ``'null'`` (an element smaller than each other element; 

it has no predecessors) and 

- an ``'oo'`` (an element larger than each other element; 

it has no successors). 

 

EXAMPLES:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: P = MP() 

sage: P.add(66) 

sage: P 

poset(66) 

sage: s = P.shell(66) 

sage: type(s) 

<class 'sage.data_structures.mutable_poset.MutablePosetShell'> 

 

.. SEEALSO:: 

 

:class:`MutablePoset` 

""" 

def __init__(self, poset, element): 

r""" 

See :class:`MutablePosetShell` for details. 

 

TESTS:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: P = MP() 

sage: from sage.data_structures.mutable_poset import MutablePosetShell 

sage: MutablePosetShell(P, (1, 2)) 

(1, 2) 

""" 

self._poset_ = poset 

self._element_ = element 

self._key_ = self.poset.get_key(element) 

self._predecessors_ = set() 

self._successors_ = set() 

super(MutablePosetShell, self).__init__() 

 

@property 

def poset(self): 

r""" 

The poset to which this shell belongs. 

 

.. SEEALSO:: 

 

:class:`MutablePoset` 

 

TESTS:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: P = MP() 

sage: from sage.data_structures.mutable_poset import MutablePosetShell 

sage: e = MutablePosetShell(P, (1, 2)) 

sage: e.poset is P 

True 

""" 

return self._poset_ 

 

@property 

def element(self): 

r""" 

The element contained in this shell. 

 

.. SEEALSO:: 

 

:meth:`key`, 

:class:`MutablePoset`. 

 

TESTS:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: P = MP() 

sage: from sage.data_structures.mutable_poset import MutablePosetShell 

sage: e = MutablePosetShell(P, (1, 2)) 

sage: e.element 

(1, 2) 

""" 

return self._element_ 

 

@property 

def key(self): 

r""" 

The key of the element contained in this shell. 

 

The key of an element is determined by the mutable poset (the 

parent) via the ``key``-function (see construction of a 

:class:`MutablePoset`). 

 

.. SEEALSO:: 

 

:meth:`element`, 

:class:`MutablePoset`. 

 

TESTS:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: from sage.data_structures.mutable_poset import MutablePosetShell 

sage: P = MP() 

sage: e = MutablePosetShell(P, (1, 2)) 

sage: e.key 

(1, 2) 

sage: Q = MP(key=lambda k: k[0]) 

sage: f = MutablePosetShell(Q, (1, 2)) 

sage: f.key 

1 

 

Test the caching of the key:: 

 

sage: def k(k): 

....: print('key %s' % (k,)) 

....: return k 

sage: R = MP(key=k) 

sage: h = MutablePosetShell(R, (1, 2)) 

key (1, 2) 

sage: h.key; h.key 

(1, 2) 

(1, 2) 

""" 

return self._key_ 

 

def predecessors(self, reverse=False): 

r""" 

Return the predecessors of this shell. 

 

INPUT: 

 

- ``reverse`` -- (default: ``False``) if set, then return 

successors instead. 

 

OUTPUT: 

 

A set. 

 

.. SEEALSO:: 

 

:meth:`successors`, 

:class:`MutablePoset`. 

 

TESTS:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: P = MP() 

sage: from sage.data_structures.mutable_poset import MutablePosetShell 

sage: e = MutablePosetShell(P, (1, 2)) 

sage: e.predecessors() 

set() 

""" 

if reverse: 

return self._successors_ 

return self._predecessors_ 

 

def successors(self, reverse=False): 

r""" 

Return the successors of this shell. 

 

INPUT: 

 

- ``reverse`` -- (default: ``False``) if set, then return 

predecessors instead. 

 

OUTPUT: 

 

A set. 

 

.. SEEALSO:: 

 

:meth:`predecessors`, 

:class:`MutablePoset`. 

 

TESTS:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: P = MP() 

sage: from sage.data_structures.mutable_poset import MutablePosetShell 

sage: e = MutablePosetShell(P, (1, 2)) 

sage: e.successors() 

set() 

""" 

if reverse: 

return self._predecessors_ 

return self._successors_ 

 

def is_special(self): 

r""" 

Return whether this shell contains either the null-element, i.e., the 

element smaller than any possible other element or the 

infinity-element, i.e., the element larger than any possible 

other element. 

 

INPUT: 

 

Nothing. 

 

OUTPUT: 

 

``True`` or ``False``. 

 

.. SEEALSO:: 

 

:meth:`is_null`, 

:meth:`is_oo`, 

:class:`MutablePoset`. 

 

TESTS:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: P = MP() 

sage: P.null.is_special() 

True 

sage: P.oo.is_special() 

True 

""" 

return self.element is None 

 

def is_null(self): 

r""" 

Return whether this shell contains the null-element, i.e., the element 

smaller than any possible other element. 

 

OUTPUT: 

 

``True`` or ``False``. 

 

.. SEEALSO:: 

 

:meth:`is_special`, 

:meth:`is_oo`, 

:meth:`MutablePoset.null`, 

:class:`MutablePoset`. 

 

TESTS:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: P = MP() 

sage: P.null.is_null() 

True 

sage: P.oo.is_null() 

False 

""" 

return self.element is None and not self.predecessors() 

 

def is_oo(self): 

r""" 

Return whether this shell contains the infinity-element, i.e., the element 

larger than any possible other element. 

 

OUTPUT: 

 

``True`` or ``False``. 

 

.. SEEALSO:: 

 

:meth:`is_null`, 

:meth:`is_special`, 

:meth:`MutablePoset.oo`, 

:class:`MutablePoset`. 

 

TESTS:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: P = MP() 

sage: P.null.is_oo() 

False 

sage: P.oo.is_oo() 

True 

""" 

return self.element is None and not self.successors() 

 

def _repr_(self): 

r""" 

Return the representation of this shell. 

 

INPUT: 

 

Nothing. 

 

OUTPUT: 

 

A string. 

 

.. NOTE:: 

 

If the :meth:`element` of this shell is not ``None``, 

this method returns the respective representation string. 

Otherwise, ``'null'`` or ``'oo'`` are returned, 

depending on the non-existence of predecessors or 

successors, respectively. 

 

TESTS:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: P = MP() 

sage: from sage.data_structures.mutable_poset import MutablePosetShell 

sage: repr(MutablePosetShell(P, (1, 2))) # indirect doctest 

'(1, 2)' 

sage: repr(P.null) # indirect doctest 

'null' 

sage: repr(P.oo) # indirect doctest 

'oo' 

""" 

if self.is_null(): 

return 'null' 

elif self.is_oo(): 

return 'oo' 

else: 

return repr(self.element) 

 

def __hash__(self): 

r""" 

Return the hash of this shell. 

 

INPUT: 

 

Nothing. 

 

OUTPUT: 

 

A hash value. 

 

This returns the hash value of the key of the element 

contained in this shell. 

 

TESTS:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: P = MP() 

sage: from sage.data_structures.mutable_poset import MutablePosetShell 

sage: hash(MutablePosetShell(P, (1, 2))) == hash((1, 2)) 

True 

""" 

return hash(self.key) 

 

def le(self, other, reverse=False): 

r""" 

Return whether this shell is less than or equal to ``other``. 

 

INPUT: 

 

- ``other`` -- a shell. 

 

- ``reverse`` -- (default: ``False``) if set, then return 

whether this shell is greater than or equal to ``other``. 

 

OUTPUT: 

 

``True`` or ``False``. 

 

.. NOTE:: 

 

The comparison of the shells is based on the comparison 

of the keys of the elements contained in the shells, 

except for special shells (see :class:`MutablePosetShell`). 

 

.. SEEALSO:: 

 

:meth:`eq`, 

:class:`MutablePoset`. 

 

TESTS:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: P = MP() 

sage: from sage.data_structures.mutable_poset import MutablePosetShell 

sage: e = MutablePosetShell(P, (1, 2)) 

sage: z = P.null 

sage: oo = P.oo 

sage: z <= e # indirect doctest 

True 

sage: e <= oo # indirect doctest 

True 

sage: z <= oo # indirect doctest 

True 

sage: oo <= z # indirect doctest 

False 

sage: oo <= e # indirect doctest 

False 

sage: e <= z # indirect doctest 

False 

sage: z <= z # indirect doctest 

True 

sage: oo <= oo # indirect doctest 

True 

sage: e <= e # indirect doctest 

True 

 

:: 

 

sage: z.le(e, reverse=True) 

False 

sage: e.le(oo, reverse=True) 

False 

sage: z.le(oo, reverse=True) 

False 

sage: oo.le(z, reverse=True) 

True 

sage: oo.le(e, reverse=True) 

True 

sage: e.le(z, reverse=True) 

True 

sage: z.le(z, reverse=True) 

True 

sage: oo.le(oo, reverse=True) 

True 

sage: e.le(e, reverse=True) 

True 

""" 

if reverse: 

return other.le(self, reverse=False) 

 

if self.element is None: 

if not self._predecessors_: 

# null on the left 

return True 

else: 

# oo on the left 

if other.element is None: 

# null or oo on the right 

return not other._successors_ 

else: 

# not null, not oo on the right 

return False 

elif other.element is None: 

# null/oo on the right 

return not other._successors_ 

 

return self.key <= other.key 

 

__le__ = le 

 

def eq(self, other): 

r""" 

Return whether this shell is equal to ``other``. 

 

INPUT: 

 

- ``other`` -- a shell. 

 

OUTPUT: 

 

``True`` or ``False``. 

 

.. NOTE:: 

 

This method compares the keys of the elements contained 

in the (non-special) shells. In particular, 

elements/shells with the same key are considered as equal. 

 

.. SEEALSO:: 

 

:meth:`le`, 

:class:`MutablePoset`. 

 

TESTS:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: P = MP() 

sage: from sage.data_structures.mutable_poset import MutablePosetShell 

sage: e = MutablePosetShell(P, (1, 2)) 

sage: f = MutablePosetShell(P, (2, 1)) 

sage: z = P.null 

sage: oo = P.oo 

sage: z == z 

True 

sage: oo == oo 

True 

sage: e == e 

True 

sage: e == f 

False 

sage: z == e 

False 

sage: e == oo 

False 

sage: oo == z 

False 

 

Comparing elements in different mutable posets is possible; their 

shells are equal if their elements are:: 

 

sage: S = MP([42]); s = S.shell(42) 

sage: T = MP([42]); t = T.shell(42) 

sage: s == t 

True 

sage: S.oo == T.oo 

True 

""" 

if self.element is None and other.element is None: 

return self.is_null() == other.is_null() 

return self.key == other.key 

 

__eq__ = eq 

 

def _copy_all_linked_(self, memo, poset, mapping): 

r""" 

Return a copy of this shell. All shells linked to this shell 

are copied as well. 

 

This is a helper function for :meth:`MutablePoset.copy`. 

 

INPUT: 

 

- ``memo`` -- a dictionary which assigns to the id of the 

calling shell to a copy of it. 

 

- ``poset`` -- the poset to which the newly created shells 

belongs. Note that the elements are not inserted into 

``poset``; this is done in the calling method 

:meth:`MutablePoset._copy_shells_`. 

 

- ``mapping`` -- a function which is applied on each of the elements. 

 

OUTPUT: 

 

A new shell. 

 

.. SEEALSO:: 

 

:meth:`MutablePoset.copy`, 

:class:`MutablePoset`. 

 

TESTS:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: P = MP() 

sage: Q = MP() 

sage: memo = {} 

sage: z = P.null._copy_all_linked_(memo, Q, lambda e: e) 

sage: z.poset is Q 

True 

sage: oo = z.successors().pop() 

sage: oo.is_oo() 

True 

 

Note that :meth:`_copy_all_linked_` does not change the mutable 

poset ``Q`` (this is done in the calling method 

:meth:`MutablePoset._copy_shells_`). Thus we have 

:: 

 

sage: oo is Q.oo 

False 

""" 

try: 

return memo[id(self)] 

except KeyError: 

pass 

 

new = self.__class__(poset, mapping(self.element) 

if self.element is not None else None) 

memo[id(self)] = new 

 

for reverse in (False, True): 

for e in self.successors(reverse): 

new.successors(reverse).add(e._copy_all_linked_(memo, poset, mapping)) 

 

return new 

 

def lower_covers(self, shell, reverse=False): 

r""" 

Return the lower covers of the specified ``shell``; 

the search is started at this (``self``) shell. 

 

A lower cover of `x` is an element `y` of the poset 

such that `y < x` and there is no element `z` of the poset 

so that `y < z < x`. 

 

INPUT: 

 

- ``shell`` -- the shell for which to find the covering shells. 

There is no restriction of ``shell`` being contained in the poset. 

If ``shell`` is contained in the poset, then use the more efficient 

methods :meth:`predecessors` and :meth:`successors`. 

 

- ``reverse`` -- (default: ``False``) if set, then find 

the upper covers (see also :meth:`upper_covers`) 

instead of the lower covers. 

 

OUTPUT: 

 

A set of :class:`shells <MutablePosetShell>`. 

 

.. NOTE:: 

 

Suppose ``reverse`` is ``False``. This method starts at 

the calling shell (``self``) and searches towards ``'oo'``. 

Thus, only shells which are (not necessarily 

direct) successors of this shell are considered. 

 

If ``reverse`` is ``True``, then the reverse direction is 

taken. 

 

EXAMPLES:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: class T(tuple): 

....: def __le__(left, right): 

....: return all(l <= r for l, r in zip(left, right)) 

sage: P = MP([T((1, 1)), T((1, 3)), T((2, 1)), 

....: T((4, 4)), T((1, 2)), T((2, 2))]) 

sage: e = P.shell(T((2, 2))); e 

(2, 2) 

sage: sorted(P.null.lower_covers(e), 

....: key=lambda c: repr(c.element)) 

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

sage: set(_) == e.predecessors() 

True 

sage: sorted(P.oo.upper_covers(e), 

....: key=lambda c: repr(c.element)) 

[(4, 4)] 

sage: set(_) == e.successors() 

True 

 

:: 

 

sage: Q = MP([T((3, 2))]) 

sage: f = next(Q.shells()) 

sage: sorted(P.null.lower_covers(f), 

....: key=lambda c: repr(c.element)) 

[(2, 2)] 

sage: sorted(P.oo.upper_covers(f), 

....: key=lambda c: repr(c.element)) 

[(4, 4)] 

 

.. SEEALSO:: 

 

:meth:`upper_covers`, 

:meth:`predecessors`, 

:meth:`successors`, 

:class:`MutablePoset`. 

""" 

if self == shell: 

return set() 

covers = set().union(*(e.lower_covers(shell, reverse) 

for e in self.successors(reverse) 

if e.le(shell, reverse))) 

return covers or set([self]) 

 

def upper_covers(self, shell, reverse=False): 

r""" 

Return the upper covers of the specified ``shell``; 

the search is started at this (``self``) shell. 

 

An upper cover of `x` is an element `y` of the poset 

such that `x < y` and there is no element `z` of the poset 

so that `x < z < y`. 

 

INPUT: 

 

- ``shell`` -- the shell for which to find the covering shells. 

There is no restriction of ``shell`` being contained in the poset. 

If ``shell`` is contained in the poset, then use the more efficient 

methods :meth:`predecessors` and :meth:`successors`. 

 

- ``reverse`` -- (default: ``False``) if set, then find 

the lower covers (see also :meth:`lower_covers`) 

instead of the upper covers. 

 

OUTPUT: 

 

A set of :class:`shells <MutablePosetShell>`. 

 

.. NOTE:: 

 

Suppose ``reverse`` is ``False``. This method starts at 

the calling shell (``self``) and searches towards ``'null'``. 

Thus, only shells which are (not necessarily 

direct) predecessors of this shell are considered. 

 

If ``reverse`` is ``True``, then the reverse direction is 

taken. 

 

EXAMPLES:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: class T(tuple): 

....: def __le__(left, right): 

....: return all(l <= r for l, r in zip(left, right)) 

sage: P = MP([T((1, 1)), T((1, 3)), T((2, 1)), 

....: T((4, 4)), T((1, 2)), T((2, 2))]) 

sage: e = P.shell(T((2, 2))); e 

(2, 2) 

sage: sorted(P.null.lower_covers(e), 

....: key=lambda c: repr(c.element)) 

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

sage: set(_) == e.predecessors() 

True 

sage: sorted(P.oo.upper_covers(e), 

....: key=lambda c: repr(c.element)) 

[(4, 4)] 

sage: set(_) == e.successors() 

True 

 

:: 

 

sage: Q = MP([T((3, 2))]) 

sage: f = next(Q.shells()) 

sage: sorted(P.null.lower_covers(f), 

....: key=lambda c: repr(c.element)) 

[(2, 2)] 

sage: sorted(P.oo.upper_covers(f), 

....: key=lambda c: repr(c.element)) 

[(4, 4)] 

 

.. SEEALSO:: 

 

:meth:`predecessors`, 

:meth:`successors`, 

:class:`MutablePoset`. 

""" 

return self.lower_covers(shell, not reverse) 

 

def _iter_depth_first_visit_(self, marked, 

reverse=False, key=None, 

condition=None): 

r""" 

Return an iterator over all shells in depth first order. 

 

This is a helper function for :meth:`iter_depth_first`. 

 

INPUT: 

 

- ``marked`` -- a set in which marked shells are stored. 

 

- ``reverse`` -- (default: ``False``) if set, reverses the 

order, i.e., ``False`` searches towards ``'oo'`` and 

``True`` searches towards ``'null'``. 

 

- ``key`` -- (default: ``None``) a function used for sorting 

the direct successors of a shell (used in case of a 

tie). If this is ``None``, no sorting occurs. 

 

- ``condition`` -- (default: ``None``) a function mapping a 

shell to ``True`` (include in iteration) or ``False`` (do 

not include). ``None`` is equivalent to a function returning 

always ``True``. Note that the iteration does not go beyond a 

not included shell. 

 

OUTPUT: 

 

An iterator. 

 

.. NOTE:: 

 

The depth first search starts at this (``self``) shell. Thus 

only this shell and shells greater than (in case of 

``reverse=False``) this shell are visited. 

 

.. SEEALSO:: 

 

:meth:`iter_depth_first`, 

:meth:`iter_topological`, 

:class:`MutablePoset`. 

 

TESTS:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: P = MP() 

sage: P.add(42) 

sage: P.add(5) 

sage: marked = set() 

sage: list(P.oo._iter_depth_first_visit_(marked, reverse=True)) 

[oo, 42, 5, null] 

""" 

if (condition is not None and 

not self.is_special() and not condition(self)): 

return 

if self in marked: 

return 

marked.add(self) 

yield self 

S = self.successors(reverse) 

if key is not None: 

S = sorted(S, key=key) 

for shell in S: 

for e in shell._iter_depth_first_visit_(marked, reverse, 

key, condition): 

yield e 

 

def iter_depth_first(self, reverse=False, key=None, condition=None): 

r""" 

Iterate over all shells in depth first order. 

 

INPUT: 

 

- ``reverse`` -- (default: ``False``) if set, reverses the 

order, i.e., ``False`` searches towards ``'oo'`` and 

``True`` searches towards ``'null'``. 

 

- ``key`` -- (default: ``None``) a function used for sorting 

the direct successors of a shell (used in case of a 

tie). If this is ``None``, no sorting occurs. 

 

- ``condition`` -- (default: ``None``) a function mapping a 

shell to ``True`` (include in iteration) or ``False`` (do 

not include). ``None`` is equivalent to a function returning 

always ``True``. Note that the iteration does not go beyond a 

not included shell. 

 

OUTPUT: 

 

An iterator. 

 

.. NOTE:: 

 

The depth first search starts at this (``self``) shell. Thus 

only this shell and shells greater than (in case of 

``reverse=False``) this shell are visited. 

 

ALGORITHM: 

 

See :wikipedia:`Depth-first_search`. 

 

EXAMPLES:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: class T(tuple): 

....: def __le__(left, right): 

....: return all(l <= r for l, r in zip(left, right)) 

sage: P = MP([T((1, 1)), T((1, 3)), T((2, 1)), 

....: T((4, 4)), T((1, 2)), T((2, 2))]) 

sage: list(P.null.iter_depth_first(reverse=False, key=repr)) 

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

sage: list(P.oo.iter_depth_first(reverse=True, key=repr)) 

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

sage: list(P.null.iter_depth_first( 

....: condition=lambda s: s.element[0] == 1)) 

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

 

.. SEEALSO:: 

 

:meth:`iter_topological`, 

:class:`MutablePoset`. 

""" 

marked = set() 

return self._iter_depth_first_visit_(marked, reverse, key, condition) 

 

def _iter_topological_visit_(self, marked, 

reverse=False, key=None, 

condition=None): 

r""" 

Return an iterator over all shells in topological order. 

 

This is a helper function for :meth:`iter_topological`. 

 

INPUT: 

 

- ``marked`` -- a set in which marked shells are stored. 

 

- ``reverse`` -- (default: ``False``) if set, reverses the 

order, i.e., ``False`` searches towards ``'oo'`` and 

``True`` searches towards ``'null'``. 

 

- ``key`` -- (default: ``None``) a function used for sorting 

the direct predecessors of a shell (used in case of a 

tie). If this is ``None``, no sorting occurs. 

 

- ``condition`` -- (default: ``None``) a function mapping a 

shell to ``True`` (include in iteration) or ``False`` (do 

not include). ``None`` is equivalent to a function returning 

always ``True``. Note that the iteration does not go beyond a 

not included shell. 

 

OUTPUT: 

 

An iterator. 

 

.. NOTE:: 

 

The topological search will only find shells smaller than 

(in case of ``reverse=False``) 

or equal to this (``self``) shell. This is in contrast to 

:meth:`iter_depth_first`. 

 

.. SEEALSO:: 

 

:meth:`iter_depth_first`, 

:meth:`iter_topological`, 

:class:`MutablePoset`. 

 

TESTS:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: P = MP() 

sage: P.add(42) 

sage: P.add(5) 

sage: marked = set() 

sage: list(P.null._iter_topological_visit_(marked, reverse=True)) 

[oo, 42, 5, null] 

""" 

if (condition is not None and 

not self.is_special() and not condition(self)): 

return 

if self in marked: 

return 

marked.add(self) 

S = self.predecessors(reverse) 

if key is not None and len(S) > 1: 

S = sorted(S, key=key) 

for shell in S: 

for e in shell._iter_topological_visit_(marked, reverse, 

key, condition): 

yield e 

yield self 

 

def iter_topological(self, reverse=False, key=None, condition=None): 

r""" 

Iterate over all shells in topological order. 

 

INPUT: 

 

- ``reverse`` -- (default: ``False``) if set, reverses the 

order, i.e., ``False`` searches towards ``'oo'`` and 

``True`` searches towards ``'null'``. 

 

- ``key`` -- (default: ``None``) a function used for sorting 

the direct predecessors of a shell (used in case of a 

tie). If this is ``None``, no sorting occurs. 

 

- ``condition`` -- (default: ``None``) a function mapping a 

shell to ``True`` (include in iteration) or ``False`` (do 

not include). ``None`` is equivalent to a function returning 

always ``True``. Note that the iteration does not go beyond a 

not included shell. 

 

OUTPUT: 

 

An iterator. 

 

.. NOTE:: 

 

The topological search will only find shells smaller than 

(in case of ``reverse=False``) 

or equal to this (``self``) shell. This is in contrast to 

:meth:`iter_depth_first`. 

 

ALGORITHM: 

 

Here a simplified version of the algorithm found in [Tar1976]_ 

and [CLRS2001]_ is used. See also 

:wikipedia:`Topological_sorting`. 

 

EXAMPLES:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: class T(tuple): 

....: def __le__(left, right): 

....: return all(l <= r for l, r in zip(left, right)) 

sage: P = MP([T((1, 1)), T((1, 3)), T((2, 1)), 

....: T((4, 4)), T((1, 2)), T((2, 2))]) 

 

:: 

 

sage: for e in P.shells_topological(include_special=True, 

....: reverse=True): 

....: print(e) 

....: print(list(e.iter_topological(reverse=True, key=repr))) 

oo 

[oo] 

(4, 4) 

[oo, (4, 4)] 

(1, 3) 

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

(2, 2) 

[oo, (4, 4), (2, 2)] 

(1, 2) 

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

(2, 1) 

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

(1, 1) 

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

null 

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

 

:: 

 

sage: for e in P.shells_topological(include_special=True, 

....: reverse=True): 

....: print(e) 

....: print(list(e.iter_topological(reverse=False, key=repr))) 

oo 

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

(4, 4) 

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

(1, 3) 

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

(2, 2) 

[null, (1, 1), (1, 2), (2, 1), (2, 2)] 

(1, 2) 

[null, (1, 1), (1, 2)] 

(2, 1) 

[null, (1, 1), (2, 1)] 

(1, 1) 

[null, (1, 1)] 

null 

[null] 

 

:: 

 

sage: list(P.null.iter_topological( 

....: reverse=True, condition=lambda s: s.element[0] == 1)) 

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

 

.. SEEALSO:: 

 

:meth:`iter_depth_first`, 

:meth:`MutablePoset.shells_topological`, 

:meth:`MutablePoset.elements_topological`, 

:meth:`MutablePoset.keys_topological`, 

:class:`MutablePoset`. 

""" 

marked = set() 

return self._iter_topological_visit_(marked, reverse, key, condition) 

 

def merge(self, element, check=True, delete=True): 

r""" 

Merge the given element with the element contained in this 

shell. 

 

INPUT: 

 

- ``element`` -- an element (of the poset). 

 

- ``check`` -- (default: ``True``) if set, then the 

``can_merge``-function of :class:`MutablePoset` determines 

whether the merge is possible. ``can_merge`` is ``None`` means 

that this check is always passed. 

 

- ``delete`` -- (default: ``True``) if set, then ``element`` 

is removed from the poset after the merge. 

 

OUTPUT: 

 

Nothing. 

 

.. NOTE:: 

 

This operation depends on the parameters ``merge`` and 

``can_merge`` of the :class:`MutablePoset` this shell is 

contained in. These parameters are defined when the poset 

is constructed. 

 

.. NOTE:: 

 

If the ``merge`` function returns ``None``, then this shell 

is removed from the poset. 

 

EXAMPLES:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: def add(left, right): 

....: return (left[0], ''.join(sorted(left[1] + right[1]))) 

sage: def can_add(left, right): 

....: return left[0] <= right[0] 

sage: P = MP([(1, 'a'), (3, 'b'), (2, 'c'), (4, 'd')], 

....: key=lambda c: c[0], merge=add, can_merge=can_add) 

sage: P 

poset((1, 'a'), (2, 'c'), (3, 'b'), (4, 'd')) 

sage: P.shell(2).merge((3, 'b')) 

sage: P 

poset((1, 'a'), (2, 'bc'), (4, 'd')) 

 

.. SEEALSO:: 

 

:meth:`MutablePoset.merge`, 

:class:`MutablePoset`. 

 

TESTS:: 

 

sage: MP([2], merge=operator.add, 

....: can_merge=lambda _, __: False).shell(2).merge(1) 

Traceback (most recent call last): 

... 

RuntimeError: Cannot merge 2 with 1. 

""" 

poset = self.poset 

if poset._merge_ is None: 

# poset._merge_ is None means no merge (poset._merge_ simply 

# returns its first input argument). 

return 

self_element = self.element 

if check: 

if not poset._can_merge_(self_element, element): 

raise RuntimeError('Cannot merge %s with %s.' % 

(self_element, element)) 

new = poset._merge_(self_element, element) 

if new is None: 

poset.discard(poset.get_key(self.element)) 

else: 

self._element_ = new 

if delete: 

poset.remove(poset.get_key(element)) 

 

 

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

 

 

def is_MutablePoset(P): 

r""" 

Test whether ``P`` inherits from :class:`MutablePoset`. 

 

.. SEEALSO:: 

 

:class:`MutablePoset` 

 

TESTS:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: from sage.data_structures.mutable_poset import is_MutablePoset 

sage: P = MP() 

sage: is_MutablePoset(P) 

True 

""" 

return isinstance(P, MutablePoset) 

 

 

class MutablePoset(SageObject): 

r""" 

A data structure that models a mutable poset (partially ordered 

set). 

 

INPUT: 

 

- ``data`` -- data from which to construct the poset. It can be 

any of the following: 

 

#. ``None`` (default), in which case an empty poset is created, 

 

#. a :class:`MutablePoset`, which will be copied during creation, 

 

#. an iterable, whose elements will be in the poset. 

 

- ``key`` -- a function which maps elements to keys. If ``None`` 

(default), this is the identity, i.e., keys are equal to their 

elements. 

 

Two elements with the same keys are considered as equal; so only 

one of these two elements can be in the poset. 

 

This ``key`` is not used for sorting (in contrast to 

sorting-functions, e.g. ``sorted``). 

 

- ``merge`` -- a function which merges its second argument (an 

element) to its first (again an element) and returns the result 

(as an element). If the return value is ``None``, the element is 

removed from the poset. 

 

This hook is called by :meth:`merge`. Moreover it is used during 

:meth:`add` when an element (more precisely its key) is already 

in this poset. 

 

``merge`` is ``None`` (default) is equivalent to ``merge`` 

returning its first argument. Note that it is not allowed that the 

key of the returning element differs from the key of the first 

input parameter. This means ``merge`` must not change the 

position of the element in the poset. 

 

- ``can_merge`` -- a function which checks whether its second argument 

can be merged to its first. 

 

This hook is called by :meth:`merge`. Moreover it is used during 

:meth:`add` when an element (more precisely its key) is already 

in this poset. 

 

``can_merge`` is ``None`` (default) is equivalent to ``can_merge`` 

returning ``True`` in all cases. 

 

OUTPUT: 

 

A mutable poset. 

 

You can find a short introduction and examples 

:mod:`here <sage.data_structures.mutable_poset>`. 

 

EXAMPLES:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

 

We illustrate the different input formats 

 

#. No input:: 

 

sage: A = MP(); A 

poset() 

 

#. A :class:`MutablePoset`:: 

 

sage: B = MP(A); B 

poset() 

sage: B.add(42) 

sage: C = MP(B); C 

poset(42) 

 

#. An iterable:: 

 

sage: C = MP([5, 3, 11]); C 

poset(3, 5, 11) 

 

.. SEEALSO:: 

 

:class:`MutablePosetShell`. 

""" 

def __init__(self, data=None, key=None, merge=None, can_merge=None): 

r""" 

See :class:`MutablePoset` for details. 

 

TESTS:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: MP() 

poset() 

 

:: 

 

sage: P = MP() 

sage: P.add(42) 

sage: MP(P) 

poset(42) 

 

:: 

 

sage: MP([3, 5, 7]) 

poset(3, 5, 7) 

 

:: 

 

sage: MP(33) 

Traceback (most recent call last): 

... 

TypeError: 33 is not iterable; do not know what to do with it. 

""" 

if is_MutablePoset(data): 

if key is not None: 

raise TypeError('Cannot use key when data is a poset.') 

self._copy_shells_(data, lambda e: e) 

 

else: 

self.clear() 

 

if key is None: 

self._key_ = lambda k: k 

else: 

self._key_ = key 

 

self._merge_ = merge 

if can_merge is None: 

self._can_merge_ = lambda _, __: True 

else: 

self._can_merge_ = can_merge 

 

if data is not None: 

try: 

it = iter(data) 

except TypeError: 

raise TypeError('%s is not iterable; do not know what to ' 

'do with it.' % (data,)) 

self.union_update(it) 

super(MutablePoset, self).__init__() 

 

def clear(self): 

r""" 

Remove all elements from this poset. 

 

INPUT: 

 

Nothing. 

 

OUTPUT: 

 

Nothing. 

 

.. SEEALSO:: 

 

:meth:`discard`, 

:meth:`pop`, 

:meth:`remove`. 

 

TESTS:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: P = MP() 

sage: P.add(42); P 

poset(42) 

sage: P.clear() 

sage: print(P.repr_full()) 

poset() 

+-- null 

| +-- no predecessors 

| +-- successors: oo 

+-- oo 

| +-- predecessors: null 

| +-- no successors 

""" 

self._null_ = MutablePosetShell(self, None) 

self._oo_ = MutablePosetShell(self, None) 

self._null_.successors().add(self._oo_) 

self._oo_.predecessors().add(self._null_) 

self._shells_ = {} 

 

def __len__(self): 

r""" 

Return the number of elements contained in this poset. 

 

INPUT: 

 

Nothing. 

 

OUTPUT: 

 

An integer. 

 

.. NOTE:: 

 

The special elements ``'null'`` and ``'oo'`` are not counted. 

 

TESTS:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: P = MP() 

sage: len(P) # indirect doctest 

0 

sage: bool(P) 

False 

sage: P.add(42) 

sage: len(P) 

1 

sage: bool(P) 

True 

""" 

return len(self._shells_) 

 

@property 

def null(self): 

r""" 

The shell `\emptyset` whose element is smaller than any 

other element. 

 

EXAMPLES:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: P = MP() 

sage: z = P.null; z 

null 

sage: z.is_null() 

True 

 

.. SEEALSO:: 

 

:meth:`oo`, 

:meth:`MutablePosetShell.is_null`, 

:meth:`MutablePosetShell.is_special`. 

""" 

return self._null_ 

 

@property 

def oo(self): 

r""" 

The shell `\infty` whose element is larger than any other 

element. 

 

EXAMPLES:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: P = MP() 

sage: oo = P.oo; oo 

oo 

sage: oo.is_oo() 

True 

 

.. SEEALSO:: 

 

:meth:`null`, 

:meth:`MutablePosetShell.is_oo`, 

:meth:`MutablePosetShell.is_special`. 

""" 

return self._oo_ 

 

def shell(self, key): 

r""" 

Return the shell of the element corresponding to ``key``. 

 

INPUT: 

 

``key`` -- the key of an object. 

 

OUTPUT: 

 

An instance of :class:`MutablePosetShell`. 

 

.. NOTE:: 

 

Each element is contained/encapsulated in a shell inside 

the poset. 

 

EXAMPLES:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: P = MP() 

sage: P.add(42) 

sage: e = P.shell(42); e 

42 

sage: type(e) 

<class 'sage.data_structures.mutable_poset.MutablePosetShell'> 

 

.. SEEALSO:: 

 

:meth:`element`, 

:meth:`get_key`. 

""" 

return self._shells_[key] 

 

def element(self, key): 

r""" 

Return the element corresponding to ``key``. 

 

INPUT: 

 

``key`` -- the key of an object. 

 

OUTPUT: 

 

An object. 

 

EXAMPLES:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: P = MP() 

sage: P.add(42) 

sage: e = P.element(42); e 

42 

sage: type(e) 

<type 'sage.rings.integer.Integer'> 

 

.. SEEALSO:: 

 

:meth:`shell`, 

:meth:`get_key`. 

""" 

return self.shell(key).element 

 

def get_key(self, element): 

r""" 

Return the key corresponding to the given element. 

 

INPUT: 

 

- ``element`` -- an object. 

 

OUTPUT: 

 

An object (the key of ``element``). 

 

.. SEEALSO:: 

 

:meth:`element`, 

:meth:`shell`. 

 

TESTS:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: P = MP() 

sage: P.get_key(None) is None 

True 

sage: P.get_key((1, 2)) 

(1, 2) 

sage: Q = MP(key=lambda k: k[0]) 

sage: Q.get_key((1, 2)) 

1 

""" 

if element is None: 

return None 

return self._key_(element) 

 

def _copy_shells_(self, other, mapping): 

r""" 

Copy shells from another poset. 

 

INPUT: 

 

- ``other`` -- the mutable poset from which the shells 

should be copied to this poset. 

 

- ``mapping`` -- a function that is applied to each element. 

 

OUTPUT: 

 

Nothing. 

 

.. SEEALSO:: 

 

:meth:`copy` 

 

TESTS:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: class T(tuple): 

....: def __le__(left, right): 

....: return all(l <= r for l, r in zip(left, right)) 

sage: P = MP() 

sage: P = MP([T((1, 1)), T((1, 3)), T((2, 1)), 

....: T((4, 4)), T((1, 2))]) 

sage: Q = MP() 

sage: Q._copy_shells_(P, lambda e: e) 

sage: P.repr_full() == Q.repr_full() 

True 

""" 

from copy import copy 

self._key_ = copy(other._key_) 

self._merge_ = copy(other._merge_) 

self._can_merge_ = copy(other._can_merge_) 

memo = {} 

self._null_ = other._null_._copy_all_linked_(memo, self, mapping) 

self._oo_ = memo[id(other._oo_)] 

self._shells_ = {f.key: f for f in iter(memo[id(e)] for e in 

itervalues(other._shells_))} 

 

def copy(self, mapping=None): 

r""" 

Create a shallow copy. 

 

INPUT: 

 

- ``mapping`` -- a function which is applied on each of the elements. 

 

OUTPUT: 

 

A poset with the same content as ``self``. 

 

.. SEEALSO:: 

 

:meth:`map`, 

:meth:`mapped`. 

 

TESTS:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: class T(tuple): 

....: def __le__(left, right): 

....: return all(l <= r for l, r in zip(left, right)) 

sage: P = MP([T((1, 1)), T((1, 3)), T((2, 1)), 

....: T((4, 4)), T((1, 2))]) 

sage: Q = copy(P) # indirect doctest 

sage: P.repr_full() == Q.repr_full() 

True 

""" 

if mapping is None: 

 

def mapping(element): 

return element 

new = self.__class__() 

new._copy_shells_(self, mapping) 

return new 

 

__copy__ = copy 

 

def shells(self, include_special=False): 

r""" 

Return an iterator over all shells. 

 

INPUT: 

 

- ``include_special`` -- (default: ``False``) if set, then 

including shells containing a smallest element (`\emptyset`) 

and a largest element (`\infty`). 

 

OUTPUT: 

 

An iterator. 

 

.. NOTE:: 

 

Each element is contained/encapsulated in a shell inside 

the poset. 

 

EXAMPLES:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: P = MP() 

sage: tuple(P.shells()) 

() 

sage: tuple(P.shells(include_special=True)) 

(null, oo) 

 

.. SEEALSO:: 

 

:meth:`shells_topological`, 

:meth:`elements`, 

:meth:`elements_topological`, 

:meth:`keys`, 

:meth:`keys_topological`, 

:meth:`MutablePosetShell.iter_depth_first`, 

:meth:`MutablePosetShell.iter_topological`. 

""" 

if include_special: 

yield self.null 

for e in itervalues(self._shells_): 

yield e 

if include_special: 

yield self.oo 

 

def shells_topological(self, include_special=False, 

reverse=False, key=None): 

r""" 

Return an iterator over all shells in topological order. 

 

INPUT: 

 

- ``include_special`` -- (default: ``False``) if set, then 

including shells containing a smallest element (`\emptyset`) 

and a largest element (`\infty`). 

 

- ``reverse`` -- (default: ``False``) -- if set, reverses the 

order, i.e., ``False`` gives smallest elements first, 

``True`` gives largest first. 

 

- ``key`` -- (default: ``None``) a function used for sorting 

the direct successors of a shell (used in case of a tie). If 

this is ``None``, then the successors are sorted according 

to their representation strings. 

 

OUTPUT: 

 

An iterator. 

 

.. NOTE:: 

 

Each element is contained/encapsulated in a shell inside 

the poset. 

 

EXAMPLES:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: class T(tuple): 

....: def __le__(left, right): 

....: return all(l <= r for l, r in zip(left, right)) 

sage: P = MP([T((1, 1)), T((1, 3)), T((2, 1)), 

....: T((4, 4)), T((1, 2)), T((2, 2))]) 

sage: list(P.shells_topological()) 

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

sage: list(P.shells_topological(reverse=True)) 

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

sage: list(P.shells_topological(include_special=True)) 

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

sage: list(P.shells_topological( 

....: include_special=True, reverse=True)) 

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

 

.. SEEALSO:: 

 

:meth:`shells`, 

:meth:`elements`, 

:meth:`elements_topological`, 

:meth:`keys`, 

:meth:`keys_topological`, 

:meth:`MutablePosetShell.iter_depth_first`, 

:meth:`MutablePosetShell.iter_topological`. 

""" 

if key is None: 

key = repr 

shell = self.oo if not reverse else self.null 

return iter(e for e in shell.iter_topological(reverse, key) 

if include_special or not e.is_special()) 

 

def elements(self, **kwargs): 

r""" 

Return an iterator over all elements. 

 

INPUT: 

 

- ``kwargs`` -- arguments are passed to :meth:`shells`. 

 

OUTPUT: 

 

An iterator. 

 

EXAMPLES:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: P = MP([3, 42, 7]) 

sage: [(v, type(v)) for v in sorted(P.elements())] 

[(3, <type 'sage.rings.integer.Integer'>), 

(7, <type 'sage.rings.integer.Integer'>), 

(42, <type 'sage.rings.integer.Integer'>)] 

 

Note that 

 

:: 

 

sage: it = iter(P) 

sage: sorted(it) 

[3, 7, 42] 

 

returns all elements as well. 

 

.. SEEALSO:: 

 

:meth:`shells`, 

:meth:`shells_topological`, 

:meth:`elements_topological`, 

:meth:`keys`, 

:meth:`keys_topological`, 

:meth:`MutablePosetShell.iter_depth_first`, 

:meth:`MutablePosetShell.iter_topological`. 

""" 

for shell in self.shells(**kwargs): 

yield shell.element 

 

__iter__ = elements 

 

def elements_topological(self, **kwargs): 

r""" 

Return an iterator over all elements in topological order. 

 

INPUT: 

 

- ``kwargs`` -- arguments are passed to :meth:`shells_topological`. 

 

OUTPUT: 

 

An iterator. 

 

EXAMPLES:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: class T(tuple): 

....: def __le__(left, right): 

....: return all(l <= r for l, r in zip(left, right)) 

sage: P = MP([T((1, 1)), T((1, 3)), T((2, 1)), 

....: T((4, 4)), T((1, 2)), T((2, 2))]) 

sage: [(v, type(v)) for v in P.elements_topological()] 

[((1, 1), <class '__main__.T'>), 

((1, 2), <class '__main__.T'>), 

((1, 3), <class '__main__.T'>), 

((2, 1), <class '__main__.T'>), 

((2, 2), <class '__main__.T'>), 

((4, 4), <class '__main__.T'>)] 

 

.. SEEALSO:: 

 

:meth:`shells`, 

:meth:`shells_topological`, 

:meth:`elements`, 

:meth:`keys`, 

:meth:`keys_topological`, 

:meth:`MutablePosetShell.iter_depth_first`, 

:meth:`MutablePosetShell.iter_topological`. 

""" 

for shell in self.shells_topological(**kwargs): 

yield shell.element 

 

def keys(self, **kwargs): 

r""" 

Return an iterator over all keys of the elements. 

 

INPUT: 

 

- ``kwargs`` -- arguments are passed to :meth:`shells`. 

 

OUTPUT: 

 

An iterator. 

 

EXAMPLES:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: P = MP([3, 42, 7], key=lambda c: -c) 

sage: [(v, type(v)) for v in sorted(P.keys())] 

[(-42, <type 'sage.rings.integer.Integer'>), 

(-7, <type 'sage.rings.integer.Integer'>), 

(-3, <type 'sage.rings.integer.Integer'>)] 

 

sage: [(v, type(v)) for v in sorted(P.elements())] 

[(3, <type 'sage.rings.integer.Integer'>), 

(7, <type 'sage.rings.integer.Integer'>), 

(42, <type 'sage.rings.integer.Integer'>)] 

 

sage: [(v, type(v)) for v in sorted(P.shells(), 

....: key=lambda c: c.element)] 

[(3, <class 'sage.data_structures.mutable_poset.MutablePosetShell'>), 

(7, <class 'sage.data_structures.mutable_poset.MutablePosetShell'>), 

(42, <class 'sage.data_structures.mutable_poset.MutablePosetShell'>)] 

 

.. SEEALSO:: 

 

:meth:`shells`, 

:meth:`shells_topological`, 

:meth:`elements`, 

:meth:`elements_topological`, 

:meth:`keys_topological`, 

:meth:`MutablePosetShell.iter_depth_first`, 

:meth:`MutablePosetShell.iter_topological`. 

""" 

for shell in self.shells(**kwargs): 

yield shell.key 

 

def keys_topological(self, **kwargs): 

r""" 

Return an iterator over all keys of the elements in 

topological order. 

 

INPUT: 

 

- ``kwargs`` -- arguments are passed to :meth:`shells_topological`. 

 

OUTPUT: 

 

An iterator. 

 

EXAMPLES:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: P = MP([(1, 1), (2, 1), (4, 4)], 

....: key=lambda c: c[0]) 

sage: [(v, type(v)) for v in P.keys_topological()] 

[(1, <type 'sage.rings.integer.Integer'>), 

(2, <type 'sage.rings.integer.Integer'>), 

(4, <type 'sage.rings.integer.Integer'>)] 

sage: [(v, type(v)) for v in P.elements_topological()] 

[((1, 1), <... 'tuple'>), 

((2, 1), <... 'tuple'>), 

((4, 4), <... 'tuple'>)] 

sage: [(v, type(v)) for v in P.shells_topological()] 

[((1, 1), <class 'sage.data_structures.mutable_poset.MutablePosetShell'>), 

((2, 1), <class 'sage.data_structures.mutable_poset.MutablePosetShell'>), 

((4, 4), <class 'sage.data_structures.mutable_poset.MutablePosetShell'>)] 

 

.. SEEALSO:: 

 

:meth:`shells`, 

:meth:`shells_topological`, 

:meth:`elements`, 

:meth:`elements_topological`, 

:meth:`keys`, 

:meth:`MutablePosetShell.iter_depth_first`, 

:meth:`MutablePosetShell.iter_topological`. 

""" 

for shell in self.shells_topological(**kwargs): 

yield shell.key 

 

def repr(self, include_special=False, reverse=False): 

r""" 

Return a representation of the poset. 

 

INPUT: 

 

- ``include_special`` -- (default: ``False``) a boolean 

indicating whether to include the special elements 

``'null'`` and ``'oo'`` or not. 

 

- ``reverse`` -- (default: ``False``) a boolean. If set, then 

largest elements are displayed first. 

 

OUTPUT: 

 

A string. 

 

.. SEEALSO:: 

 

:meth:`repr_full` 

 

TESTS:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: print(MP().repr()) 

poset() 

""" 

s = 'poset(' 

s += ', '.join(repr(shell) for shell in 

self.shells_topological(include_special, reverse)) 

s += ')' 

return s 

 

def repr_full(self, reverse=False): 

r""" 

Return a representation with ordering details of the poset. 

 

INPUT: 

 

- ``reverse`` -- (default: ``False``) a boolean. If set, then 

largest elements are displayed first. 

 

OUTPUT: 

 

A string. 

 

.. SEEALSO:: 

 

:meth:`repr` 

 

TESTS:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: print(MP().repr_full(reverse=True)) 

poset() 

+-- oo 

| +-- no successors 

| +-- predecessors: null 

+-- null 

| +-- successors: oo 

| +-- no predecessors 

""" 

sortedshells = tuple( 

self.shells_topological(include_special=True, reverse=reverse)) 

strings = [self.repr(include_special=False, reverse=reverse)] 

for shell in sortedshells: 

strings.append('+-- ' + repr(shell)) 

for rev in (not reverse, reverse): 

what = 'successors' if not rev else 'predecessors' 

if shell.successors(rev): 

s = '| +-- ' + what + ': ' 

s += ', '.join(repr(e) for e in 

sortedshells if e in shell.successors(rev)) 

else: 

s = '| +-- no ' + what 

strings.append(s) 

return '\n'.join(strings) 

 

_repr_ = repr 

 

def contains(self, key): 

r""" 

Test whether ``key`` is encapsulated by one of the poset's elements. 

 

INPUT: 

 

- ``key`` -- an object. 

 

OUTPUT: 

 

``True`` or ``False``. 

 

.. SEEALSO:: 

 

:meth:`shells`, 

:meth:`elements`, 

:meth:`keys`. 

 

TESTS:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: class T(tuple): 

....: def __le__(left, right): 

....: return all(l <= r for l, r in zip(left, right)) 

sage: P = MP() 

sage: P.add(T((1, 1))) 

sage: T((1, 1)) in P # indirect doctest 

True 

sage: T((1, 2)) in P # indirect doctest 

False 

""" 

return key in self._shells_ 

 

__contains__ = contains 

 

def add(self, element): 

r""" 

Add the given object as element to the poset. 

 

INPUT: 

 

- ``element`` -- an object (hashable and supporting comparison 

with the operator ``<=``). 

 

OUTPUT: 

 

Nothing. 

 

EXAMPLES:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: class T(tuple): 

....: def __le__(left, right): 

....: return all(l <= r for l, r in zip(left, right)) 

sage: P = MP([T((1, 1)), T((1, 3)), T((2, 1)), 

....: T((4, 4)), T((1, 2))]) 

sage: print(P.repr_full(reverse=True)) 

poset((4, 4), (1, 3), (1, 2), (2, 1), (1, 1)) 

+-- oo 

| +-- no successors 

| +-- predecessors: (4, 4) 

+-- (4, 4) 

| +-- successors: oo 

| +-- predecessors: (1, 3), (2, 1) 

+-- (1, 3) 

| +-- successors: (4, 4) 

| +-- predecessors: (1, 2) 

+-- (1, 2) 

| +-- successors: (1, 3) 

| +-- predecessors: (1, 1) 

+-- (2, 1) 

| +-- successors: (4, 4) 

| +-- predecessors: (1, 1) 

+-- (1, 1) 

| +-- successors: (1, 2), (2, 1) 

| +-- predecessors: null 

+-- null 

| +-- successors: (1, 1) 

| +-- no predecessors 

sage: P.add(T((2, 2))) 

sage: reprP = P.repr_full(reverse=True); print(reprP) 

poset((4, 4), (1, 3), (2, 2), (1, 2), (2, 1), (1, 1)) 

+-- oo 

| +-- no successors 

| +-- predecessors: (4, 4) 

+-- (4, 4) 

| +-- successors: oo 

| +-- predecessors: (1, 3), (2, 2) 

+-- (1, 3) 

| +-- successors: (4, 4) 

| +-- predecessors: (1, 2) 

+-- (2, 2) 

| +-- successors: (4, 4) 

| +-- predecessors: (1, 2), (2, 1) 

+-- (1, 2) 

| +-- successors: (1, 3), (2, 2) 

| +-- predecessors: (1, 1) 

+-- (2, 1) 

| +-- successors: (2, 2) 

| +-- predecessors: (1, 1) 

+-- (1, 1) 

| +-- successors: (1, 2), (2, 1) 

| +-- predecessors: null 

+-- null 

| +-- successors: (1, 1) 

| +-- no predecessors 

 

When adding an element which is already in the poset, nothing happens:: 

 

sage: e = T((2, 2)) 

sage: P.add(e) 

sage: P.repr_full(reverse=True) == reprP 

True 

 

We can influence the behavior when an element with existing key 

is to be inserted in the poset. For example, we can perform an 

addition on some argument of the elements:: 

 

sage: def add(left, right): 

....: return (left[0], ''.join(sorted(left[1] + right[1]))) 

sage: A = MP(key=lambda k: k[0], merge=add) 

sage: A.add((3, 'a')) 

sage: A 

poset((3, 'a')) 

sage: A.add((3, 'b')) 

sage: A 

poset((3, 'ab')) 

 

We can also deal with cancellations. If the return value of 

our hook-function is ``None``, then the element is removed out of 

the poset:: 

 

sage: def add_None(left, right): 

....: s = left[1] + right[1] 

....: if s == 0: 

....: return None 

....: return (left[0], s) 

sage: B = MP(key=lambda k: k[0], 

....: merge=add_None) 

sage: B.add((7, 42)) 

sage: B.add((7, -42)) 

sage: B 

poset() 

 

.. SEEALSO:: 

 

:meth:`discard`, 

:meth:`pop`, 

:meth:`remove`. 

 

TESTS:: 

 

sage: R = MP([(1, 1, 42), (1, 3, 42), (2, 1, 7), 

....: (4, 4, 42), (1, 2, 7), (2, 2, 7)], 

....: key=lambda k: T(k[2:3])) 

sage: print(R.repr_full(reverse=True)) 

poset((1, 1, 42), (2, 1, 7)) 

+-- oo 

| +-- no successors 

| +-- predecessors: (1, 1, 42) 

+-- (1, 1, 42) 

| +-- successors: oo 

| +-- predecessors: (2, 1, 7) 

+-- (2, 1, 7) 

| +-- successors: (1, 1, 42) 

| +-- predecessors: null 

+-- null 

| +-- successors: (2, 1, 7) 

| +-- no predecessors 

 

:: 

 

sage: P = MP() 

sage: P.add(None) 

Traceback (most recent call last): 

... 

ValueError: None is not an allowed element. 

""" 

if element is None: 

raise ValueError('None is not an allowed element.') 

key = self.get_key(element) 

 

if key in self._shells_: 

if self._merge_ is not None: 

self.shell(key).merge(element, delete=False) 

return 

 

new = MutablePosetShell(self, element) 

new._predecessors_ = self.null.lower_covers(new) 

new._successors_ = self.oo.upper_covers(new) 

 

for s in new.predecessors(): 

for l in s.successors().intersection(new.successors()): 

l.predecessors().remove(s) 

s.successors().remove(l) 

s.successors().add(new) 

for l in new.successors(): 

l.predecessors().add(new) 

 

self._shells_[key] = new 

 

def remove(self, key, raise_key_error=True): 

r""" 

Remove the given object from the poset. 

 

INPUT: 

 

- ``key`` -- the key of an object. 

 

- ``raise_key_error`` -- (default: ``True``) switch raising 

``KeyError`` on and off. 

 

OUTPUT: 

 

Nothing. 

 

If the element is not a member and ``raise_key_error`` is set 

(default), raise a ``KeyError``. 

 

.. NOTE:: 

 

As with Python's ``set``, the methods :meth:`remove` 

and :meth:`discard` only differ in their behavior when an 

element is not contained in the poset: :meth:`remove` 

raises a ``KeyError`` whereas :meth:`discard` does not 

raise any exception. 

 

This default behavior can be overridden with the 

``raise_key_error`` parameter. 

 

EXAMPLES:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: class T(tuple): 

....: def __le__(left, right): 

....: return all(l <= r for l, r in zip(left, right)) 

sage: P = MP([T((1, 1)), T((1, 3)), T((2, 1)), 

....: T((4, 4)), T((1, 2)), T((2, 2))]) 

sage: print(P.repr_full(reverse=True)) 

poset((4, 4), (1, 3), (2, 2), (1, 2), (2, 1), (1, 1)) 

+-- oo 

| +-- no successors 

| +-- predecessors: (4, 4) 

+-- (4, 4) 

| +-- successors: oo 

| +-- predecessors: (1, 3), (2, 2) 

+-- (1, 3) 

| +-- successors: (4, 4) 

| +-- predecessors: (1, 2) 

+-- (2, 2) 

| +-- successors: (4, 4) 

| +-- predecessors: (1, 2), (2, 1) 

+-- (1, 2) 

| +-- successors: (1, 3), (2, 2) 

| +-- predecessors: (1, 1) 

+-- (2, 1) 

| +-- successors: (2, 2) 

| +-- predecessors: (1, 1) 

+-- (1, 1) 

| +-- successors: (1, 2), (2, 1) 

| +-- predecessors: null 

+-- null 

| +-- successors: (1, 1) 

| +-- no predecessors 

sage: P.remove(T((1, 2))) 

sage: print(P.repr_full(reverse=True)) 

poset((4, 4), (1, 3), (2, 2), (2, 1), (1, 1)) 

+-- oo 

| +-- no successors 

| +-- predecessors: (4, 4) 

+-- (4, 4) 

| +-- successors: oo 

| +-- predecessors: (1, 3), (2, 2) 

+-- (1, 3) 

| +-- successors: (4, 4) 

| +-- predecessors: (1, 1) 

+-- (2, 2) 

| +-- successors: (4, 4) 

| +-- predecessors: (2, 1) 

+-- (2, 1) 

| +-- successors: (2, 2) 

| +-- predecessors: (1, 1) 

+-- (1, 1) 

| +-- successors: (1, 3), (2, 1) 

| +-- predecessors: null 

+-- null 

| +-- successors: (1, 1) 

| +-- no predecessors 

 

.. SEEALSO:: 

 

:meth:`add`, 

:meth:`clear`, 

:meth:`discard`, 

:meth:`pop`. 

 

TESTS:: 

 

sage: Q = MP([(1, 1, 42), (1, 3, 42), (2, 1, 7), 

....: (4, 4, 42), (1, 2, 7), (2, 2, 7)], 

....: key=lambda k: T(k[0:2])) 

sage: print(Q.repr_full(reverse=True)) 

poset((4, 4, 42), (1, 3, 42), (2, 2, 7), 

(1, 2, 7), (2, 1, 7), (1, 1, 42)) 

+-- oo 

| +-- no successors 

| +-- predecessors: (4, 4, 42) 

+-- (4, 4, 42) 

| +-- successors: oo 

| +-- predecessors: (1, 3, 42), (2, 2, 7) 

+-- (1, 3, 42) 

| +-- successors: (4, 4, 42) 

| +-- predecessors: (1, 2, 7) 

+-- (2, 2, 7) 

| +-- successors: (4, 4, 42) 

| +-- predecessors: (1, 2, 7), (2, 1, 7) 

+-- (1, 2, 7) 

| +-- successors: (1, 3, 42), (2, 2, 7) 

| +-- predecessors: (1, 1, 42) 

+-- (2, 1, 7) 

| +-- successors: (2, 2, 7) 

| +-- predecessors: (1, 1, 42) 

+-- (1, 1, 42) 

| +-- successors: (1, 2, 7), (2, 1, 7) 

| +-- predecessors: null 

+-- null 

| +-- successors: (1, 1, 42) 

| +-- no predecessors 

sage: Q.remove((1,1)) 

sage: print(Q.repr_full(reverse=True)) 

poset((4, 4, 42), (1, 3, 42), (2, 2, 7), (1, 2, 7), (2, 1, 7)) 

+-- oo 

| +-- no successors 

| +-- predecessors: (4, 4, 42) 

+-- (4, 4, 42) 

| +-- successors: oo 

| +-- predecessors: (1, 3, 42), (2, 2, 7) 

+-- (1, 3, 42) 

| +-- successors: (4, 4, 42) 

| +-- predecessors: (1, 2, 7) 

+-- (2, 2, 7) 

| +-- successors: (4, 4, 42) 

| +-- predecessors: (1, 2, 7), (2, 1, 7) 

+-- (1, 2, 7) 

| +-- successors: (1, 3, 42), (2, 2, 7) 

| +-- predecessors: null 

+-- (2, 1, 7) 

| +-- successors: (2, 2, 7) 

| +-- predecessors: null 

+-- null 

| +-- successors: (1, 2, 7), (2, 1, 7) 

| +-- no predecessors 

 

:: 

 

sage: P = MP() 

sage: P.remove(None) 

Traceback (most recent call last): 

... 

ValueError: None is not an allowed key. 

""" 

if key is None: 

raise ValueError('None is not an allowed key.') 

 

try: 

shell = self._shells_[key] 

except KeyError: 

if not raise_key_error: 

return 

raise KeyError('Key %s is not contained in this poset.' % (key,)) 

 

for reverse in (False, True): 

for p in shell.predecessors(reverse): 

S = p.successors(reverse) 

S.remove(shell) 

D = set(s for s in p.iter_depth_first(reverse) 

if s in shell.successors(reverse)) 

S.update(shell.successors(reverse)) 

S.difference_update(D) 

del self._shells_[key] 

 

def discard(self, key, raise_key_error=False): 

r""" 

Remove the given object from the poset. 

 

INPUT: 

 

- ``key`` -- the key of an object. 

 

- ``raise_key_error`` -- (default: ``False``) switch raising 

``KeyError`` on and off. 

 

OUTPUT: 

 

Nothing. 

 

If the element is not a member and ``raise_key_error`` is set 

(not default), raise a ``KeyError``. 

 

.. NOTE:: 

 

As with Python's ``set``, the methods :meth:`remove` 

and :meth:`discard` only differ in their behavior when an 

element is not contained in the poset: :meth:`remove` 

raises a ``KeyError`` whereas :meth:`discard` does not 

raise any exception. 

 

This default behavior can be overridden with the 

``raise_key_error`` parameter. 

 

EXAMPLES:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: class T(tuple): 

....: def __le__(left, right): 

....: return all(l <= r for l, r in zip(left, right)) 

sage: P = MP([T((1, 1)), T((1, 3)), T((2, 1)), 

....: T((4, 4)), T((1, 2)), T((2, 2))]) 

sage: P.discard(T((1, 2))) 

sage: P.remove(T((1, 2))) 

Traceback (most recent call last): 

... 

KeyError: 'Key (1, 2) is not contained in this poset.' 

sage: P.discard(T((1, 2))) 

 

.. SEEALSO:: 

 

:meth:`add`, 

:meth:`clear`, 

:meth:`remove`, 

:meth:`pop`. 

""" 

return self.remove(key, raise_key_error) 

 

def pop(self, **kwargs): 

r""" 

Remove and return an arbitrary poset element. 

 

INPUT: 

 

- ``kwargs`` -- arguments are passed to :meth:`shells_topological`. 

 

OUTPUT: 

 

An object. 

 

.. NOTE:: 

 

The special elements ``'null'`` and ``'oo'`` cannot be popped. 

 

EXAMPLES:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: P = MP() 

sage: P.add(3) 

sage: P 

poset(3) 

sage: P.pop() 

3 

sage: P 

poset() 

sage: P.pop() 

Traceback (most recent call last): 

... 

KeyError: 'pop from an empty poset' 

 

.. SEEALSO:: 

 

:meth:`add`, 

:meth:`clear`, 

:meth:`discard`, 

:meth:`remove`. 

""" 

kwargs['include_special'] = False 

 

try: 

shell = next(self.shells_topological(**kwargs)) 

except StopIteration: 

raise KeyError('pop from an empty poset') 

self.remove(shell.key) 

return shell.element 

 

def union(self, *other): 

r""" 

Return the union of the given posets as a new poset 

 

INPUT: 

 

- ``other`` -- a poset or an iterable. In the latter case the 

iterated objects are seen as elements of a poset. 

It is possible to specify more than one ``other`` as 

variadic arguments (arbitrary argument lists). 

 

OUTPUT: 

 

A poset. 

 

.. NOTE:: 

 

The key of an element is used for comparison. Thus elements with 

the same key are considered as equal. 

 

Due to keys and a ``merge`` function (see :class:`MutablePoset`) 

this operation might not be commutative. 

 

.. TODO:: 

 

Use the already existing information in the other poset to speed 

up this function. (At the moment each element of the other poset 

is inserted one by one and without using this information.) 

 

EXAMPLES:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: P = MP([3, 42, 7]); P 

poset(3, 7, 42) 

sage: Q = MP([4, 8, 42]); Q 

poset(4, 8, 42) 

sage: P.union(Q) 

poset(3, 4, 7, 8, 42) 

 

.. SEEALSO:: 

 

:meth:`union_update`, 

:meth:`difference`, :meth:`difference_update`, 

:meth:`intersection`, :meth:`intersection_update`, 

:meth:`symmetric_difference`, :meth:`symmetric_difference_update`, 

:meth:`is_disjoint`, 

:meth:`is_subset`, 

:meth:`is_superset`. 

 

TESTS:: 

 

sage: P.union(P, Q, Q, P) 

poset(3, 4, 7, 8, 42) 

""" 

new = self.copy() 

new.update(*other) 

return new 

 

def union_update(self, *other): 

r""" 

Update this poset with the union of itself and another poset. 

 

INPUT: 

 

- ``other`` -- a poset or an iterable. In the latter case the 

iterated objects are seen as elements of a poset. 

It is possible to specify more than one ``other`` as 

variadic arguments (arbitrary argument lists). 

 

OUTPUT: 

 

Nothing. 

 

.. NOTE:: 

 

The key of an element is used for comparison. Thus elements with 

the same key are considered as equal; 

``A.union_update(B)`` and ``B.union_update(A)`` might 

result in different posets. 

 

.. TODO:: 

 

Use the already existing information in the other poset to speed 

up this function. (At the moment each element of the other poset 

is inserted one by one and without using this information.) 

 

EXAMPLES:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: P = MP([3, 42, 7]); P 

poset(3, 7, 42) 

sage: Q = MP([4, 8, 42]); Q 

poset(4, 8, 42) 

sage: P.union_update(Q) 

sage: P 

poset(3, 4, 7, 8, 42) 

 

.. SEEALSO:: 

 

:meth:`union`, 

:meth:`difference`, :meth:`difference_update`, 

:meth:`intersection`, :meth:`intersection_update`, 

:meth:`symmetric_difference`, :meth:`symmetric_difference_update`, 

:meth:`is_disjoint`, 

:meth:`is_subset`, 

:meth:`is_superset`. 

 

TESTS:: 

 

sage: Q.update(P) 

sage: Q 

poset(3, 4, 7, 8, 42) 

""" 

for o in other: 

try: 

it = o.elements() 

except AttributeError: 

it = iter(o) 

for element in it: 

self.add(element) 

 

update = union_update # as in a Python set 

r""" 

Alias of :meth:`union_update`. 

""" 

 

def difference(self, *other): 

r""" 

Return a new poset where all elements of this poset, which are 

contained in one of the other given posets, are removed. 

 

INPUT: 

 

- ``other`` -- a poset or an iterable. In the latter case the 

iterated objects are seen as elements of a poset. 

It is possible to specify more than one ``other`` as 

variadic arguments (arbitrary argument lists). 

 

OUTPUT: 

 

A poset. 

 

.. NOTE:: 

 

The key of an element is used for comparison. Thus elements with 

the same key are considered as equal. 

 

EXAMPLES:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: P = MP([3, 42, 7]); P 

poset(3, 7, 42) 

sage: Q = MP([4, 8, 42]); Q 

poset(4, 8, 42) 

sage: P.difference(Q) 

poset(3, 7) 

 

.. SEEALSO:: 

 

:meth:`union`, :meth:`union_update`, 

:meth:`difference_update`, 

:meth:`intersection`, :meth:`intersection_update`, 

:meth:`symmetric_difference`, :meth:`symmetric_difference_update`, 

:meth:`is_disjoint`, 

:meth:`is_subset`, 

:meth:`is_superset`. 

 

TESTS:: 

 

sage: P.difference(Q, Q) 

poset(3, 7) 

sage: P.difference(P) 

poset() 

sage: P.difference(Q, P) 

poset() 

""" 

new = self.copy() 

new.difference_update(*other) 

return new 

 

def difference_update(self, *other): 

r""" 

Remove all elements of another poset from this poset. 

 

INPUT: 

 

- ``other`` -- a poset or an iterable. In the latter case the 

iterated objects are seen as elements of a poset. 

It is possible to specify more than one ``other`` as 

variadic arguments (arbitrary argument lists). 

 

OUTPUT: 

 

Nothing. 

 

.. NOTE:: 

 

The key of an element is used for comparison. Thus elements with 

the same key are considered as equal. 

 

EXAMPLES:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: P = MP([3, 42, 7]); P 

poset(3, 7, 42) 

sage: Q = MP([4, 8, 42]); Q 

poset(4, 8, 42) 

sage: P.difference_update(Q) 

sage: P 

poset(3, 7) 

 

.. SEEALSO:: 

 

:meth:`union`, :meth:`union_update`, 

:meth:`difference`, 

:meth:`intersection`, :meth:`intersection_update`, 

:meth:`symmetric_difference`, :meth:`symmetric_difference_update`, 

:meth:`is_disjoint`, 

:meth:`is_subset`, 

:meth:`is_superset`. 

""" 

for o in other: 

try: 

it = o.keys() 

except AttributeError: 

it = iter(o) 

for key in it: 

self.discard(key) 

 

def intersection(self, *other): 

r""" 

Return the intersection of the given posets as a new poset 

 

INPUT: 

 

- ``other`` -- a poset or an iterable. In the latter case the 

iterated objects are seen as elements of a poset. 

It is possible to specify more than one ``other`` as 

variadic arguments (arbitrary argument lists). 

 

OUTPUT: 

 

A poset. 

 

.. NOTE:: 

 

The key of an element is used for comparison. Thus elements with 

the same key are considered as equal. 

 

EXAMPLES:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: P = MP([3, 42, 7]); P 

poset(3, 7, 42) 

sage: Q = MP([4, 8, 42]); Q 

poset(4, 8, 42) 

sage: P.intersection(Q) 

poset(42) 

 

.. SEEALSO:: 

 

:meth:`union`, :meth:`union_update`, 

:meth:`difference`, :meth:`difference_update`, 

:meth:`intersection_update`, 

:meth:`symmetric_difference`, :meth:`symmetric_difference_update`, 

:meth:`is_disjoint`, 

:meth:`is_subset`, 

:meth:`is_superset`. 

 

TESTS:: 

 

sage: P.intersection(P, Q, Q, P) 

poset(42) 

""" 

new = self.copy() 

new.intersection_update(*other) 

return new 

 

def intersection_update(self, *other): 

r""" 

Update this poset with the intersection of itself and another poset. 

 

INPUT: 

 

- ``other`` -- a poset or an iterable. In the latter case the 

iterated objects are seen as elements of a poset. 

It is possible to specify more than one ``other`` as 

variadic arguments (arbitrary argument lists). 

 

OUTPUT: 

 

Nothing. 

 

.. NOTE:: 

 

The key of an element is used for comparison. Thus elements with 

the same key are considered as equal; 

``A.intersection_update(B)`` and ``B.intersection_update(A)`` might 

result in different posets. 

 

EXAMPLES:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: P = MP([3, 42, 7]); P 

poset(3, 7, 42) 

sage: Q = MP([4, 8, 42]); Q 

poset(4, 8, 42) 

sage: P.intersection_update(Q) 

sage: P 

poset(42) 

 

.. SEEALSO:: 

 

:meth:`union`, :meth:`union_update`, 

:meth:`difference`, :meth:`difference_update`, 

:meth:`intersection`, 

:meth:`symmetric_difference`, :meth:`symmetric_difference_update`, 

:meth:`is_disjoint`, 

:meth:`is_subset`, 

:meth:`is_superset`. 

""" 

keys = tuple(self.keys()) 

for key in keys: 

if any(key not in o for o in other): 

self.discard(key) 

 

def symmetric_difference(self, other): 

r""" 

Return the symmetric difference of two posets as a new poset. 

 

INPUT: 

 

- ``other`` -- a poset. 

 

OUTPUT: 

 

A poset. 

 

.. NOTE:: 

 

The key of an element is used for comparison. Thus elements with 

the same key are considered as equal. 

 

EXAMPLES:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: P = MP([3, 42, 7]); P 

poset(3, 7, 42) 

sage: Q = MP([4, 8, 42]); Q 

poset(4, 8, 42) 

sage: P.symmetric_difference(Q) 

poset(3, 4, 7, 8) 

 

.. SEEALSO:: 

 

:meth:`union`, :meth:`union_update`, 

:meth:`difference`, :meth:`difference_update`, 

:meth:`intersection`, :meth:`intersection_update`, 

:meth:`symmetric_difference_update`, 

:meth:`is_disjoint`, 

:meth:`is_subset`, 

:meth:`is_superset`. 

""" 

new = self.copy() 

new.symmetric_difference_update(other) 

return new 

 

def symmetric_difference_update(self, other): 

r""" 

Update this poset with the symmetric difference of itself and 

another poset. 

 

INPUT: 

 

- ``other`` -- a poset. 

 

OUTPUT: 

 

Nothing. 

 

.. NOTE:: 

 

The key of an element is used for comparison. Thus elements with 

the same key are considered as equal; 

``A.symmetric_difference_update(B)`` and 

``B.symmetric_difference_update(A)`` might 

result in different posets. 

 

EXAMPLES:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: P = MP([3, 42, 7]); P 

poset(3, 7, 42) 

sage: Q = MP([4, 8, 42]); Q 

poset(4, 8, 42) 

sage: P.symmetric_difference_update(Q) 

sage: P 

poset(3, 4, 7, 8) 

 

.. SEEALSO:: 

 

:meth:`union`, :meth:`union_update`, 

:meth:`difference`, :meth:`difference_update`, 

:meth:`intersection`, :meth:`intersection_update`, 

:meth:`symmetric_difference`, 

:meth:`is_disjoint`, 

:meth:`is_subset`, 

:meth:`is_superset`. 

""" 

T = other.difference(self) 

self.difference_update(other) 

self.union_update(T) 

 

def is_disjoint(self, other): 

r""" 

Return whether another poset is disjoint to this poset. 

 

INPUT: 

 

- ``other`` -- a poset or an iterable. In the latter case the 

iterated objects are seen as elements of a poset. 

 

OUTPUT: 

 

Nothing. 

 

.. NOTE:: 

 

If this poset uses a ``key``-function, then all 

comparisons are performed on the keys of the elements (and 

not on the elements themselves). 

 

EXAMPLES:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: P = MP([3, 42, 7]); P 

poset(3, 7, 42) 

sage: Q = MP([4, 8, 42]); Q 

poset(4, 8, 42) 

sage: P.is_disjoint(Q) 

False 

sage: P.is_disjoint(Q.difference(P)) 

True 

 

.. SEEALSO:: 

 

:meth:`is_subset`, 

:meth:`is_superset`, 

:meth:`union`, :meth:`union_update`, 

:meth:`difference`, :meth:`difference_update`, 

:meth:`intersection`, :meth:`intersection_update`, 

:meth:`symmetric_difference`, :meth:`symmetric_difference_update`. 

""" 

return all(key not in other for key in self.keys()) 

 

isdisjoint = is_disjoint # as in a Python set 

r""" 

Alias of :meth:`is_disjoint`. 

""" 

 

def is_subset(self, other): 

r""" 

Return whether another poset contains this poset, i.e., whether this poset 

is a subset of the other poset. 

 

INPUT: 

 

- ``other`` -- a poset or an iterable. In the latter case the 

iterated objects are seen as elements of a poset. 

 

OUTPUT: 

 

Nothing. 

 

.. NOTE:: 

 

If this poset uses a ``key``-function, then all 

comparisons are performed on the keys of the elements (and 

not on the elements themselves). 

 

EXAMPLES:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: P = MP([3, 42, 7]); P 

poset(3, 7, 42) 

sage: Q = MP([4, 8, 42]); Q 

poset(4, 8, 42) 

sage: P.is_subset(Q) 

False 

sage: Q.is_subset(P) 

False 

sage: P.is_subset(P) 

True 

sage: P.is_subset(P.union(Q)) 

True 

 

.. SEEALSO:: 

 

:meth:`is_disjoint`, 

:meth:`is_superset`, 

:meth:`union`, :meth:`union_update`, 

:meth:`difference`, :meth:`difference_update`, 

:meth:`intersection`, :meth:`intersection_update`, 

:meth:`symmetric_difference`, :meth:`symmetric_difference_update`. 

""" 

return all(key in other for key in self.keys()) 

 

issubset = is_subset # as in a Python set 

r""" 

Alias of :meth:`is_subset`. 

""" 

 

def is_superset(self, other): 

r""" 

Return whether this poset contains another poset, i.e., whether this poset 

is a superset of the other poset. 

 

INPUT: 

 

- ``other`` -- a poset or an iterable. In the latter case the 

iterated objects are seen as elements of a poset. 

 

OUTPUT: 

 

Nothing. 

 

.. NOTE:: 

 

If this poset uses a ``key``-function, then all 

comparisons are performed on the keys of the elements (and 

not on the elements themselves). 

 

EXAMPLES:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: P = MP([3, 42, 7]); P 

poset(3, 7, 42) 

sage: Q = MP([4, 8, 42]); Q 

poset(4, 8, 42) 

sage: P.is_superset(Q) 

False 

sage: Q.is_superset(P) 

False 

sage: P.is_superset(P) 

True 

sage: P.union(Q).is_superset(P) 

True 

 

.. SEEALSO:: 

 

:meth:`is_disjoint`, 

:meth:`is_subset`, 

:meth:`union`, :meth:`union_update`, 

:meth:`difference`, :meth:`difference_update`, 

:meth:`intersection`, :meth:`intersection_update`, 

:meth:`symmetric_difference`, :meth:`symmetric_difference_update`. 

""" 

try: 

it = other.keys() 

except AttributeError: 

it = iter(other) 

return all(key in self for key in it) 

 

issuperset = is_superset # as in a Python set 

r""" 

Alias of :meth:`is_superset`. 

""" 

 

def merge(self, key=None, reverse=False): 

r""" 

Merge the given element with its successors/predecessors. 

 

INPUT: 

 

- ``key`` -- the key specifying an element or ``None`` 

(default), in which case this method is called on each 

element in this poset. 

 

- ``reverse`` -- (default: ``False``) specifies which 

direction to go first: 

``False`` searches towards ``'oo'`` and 

``True`` searches towards ``'null'``. 

When ``key=None``, then this also 

specifies which elements are merged first. 

 

OUTPUT: 

 

Nothing. 

 

This method tests all (not necessarily direct) successors and 

predecessors of the given element whether they can be merged with 

the element itself. This is done by the ``can_merge``-function 

of :class:`MutablePoset`. If this merge is possible, then it 

is performed by calling :class:`MutablePoset`'s 

``merge``-function and the corresponding successor/predecessor 

is removed from the poset. 

 

.. NOTE:: 

 

``can_merge`` is applied in the sense of the condition of 

depth first iteration, i.e., once ``can_merge`` fails, 

the successors/predecessors are no longer tested. 

 

.. NOTE:: 

 

The motivation for such a merge behavior comes from 

asymptotic expansions: `O(n^3)` merges with, for 

example, `3n^2` or `O(n)` to `O(n^3)` (as `n` tends to 

`\infty`; see :wikipedia:`Big_O_notation`). 

 

EXAMPLES:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: class T(tuple): 

....: def __le__(left, right): 

....: return all(l <= r for l, r in zip(left, right)) 

sage: key = lambda t: T(t[0:2]) 

sage: def add(left, right): 

....: return (left[0], left[1], 

....: ''.join(sorted(left[2] + right[2]))) 

sage: def can_add(left, right): 

....: return key(left) >= key(right) 

sage: P = MP([(1, 1, 'a'), (1, 3, 'b'), (2, 1, 'c'), 

....: (4, 4, 'd'), (1, 2, 'e'), (2, 2, 'f')], 

....: key=key, merge=add, can_merge=can_add) 

sage: Q = copy(P) 

sage: Q.merge(T((1, 3))) 

sage: print(Q.repr_full(reverse=True)) 

poset((4, 4, 'd'), (1, 3, 'abe'), (2, 2, 'f'), (2, 1, 'c')) 

+-- oo 

| +-- no successors 

| +-- predecessors: (4, 4, 'd') 

+-- (4, 4, 'd') 

| +-- successors: oo 

| +-- predecessors: (1, 3, 'abe'), (2, 2, 'f') 

+-- (1, 3, 'abe') 

| +-- successors: (4, 4, 'd') 

| +-- predecessors: null 

+-- (2, 2, 'f') 

| +-- successors: (4, 4, 'd') 

| +-- predecessors: (2, 1, 'c') 

+-- (2, 1, 'c') 

| +-- successors: (2, 2, 'f') 

| +-- predecessors: null 

+-- null 

| +-- successors: (1, 3, 'abe'), (2, 1, 'c') 

| +-- no predecessors 

sage: for k in P.keys(): 

....: Q = copy(P) 

....: Q.merge(k) 

....: print('merging %s: %s' % (k, Q)) 

merging (1, 2): poset((1, 2, 'ae'), (1, 3, 'b'), 

(2, 1, 'c'), (2, 2, 'f'), (4, 4, 'd')) 

merging (1, 3): poset((1, 3, 'abe'), (2, 1, 'c'), 

(2, 2, 'f'), (4, 4, 'd')) 

merging (4, 4): poset((4, 4, 'abcdef')) 

merging (2, 1): poset((1, 2, 'e'), (1, 3, 'b'), 

(2, 1, 'ac'), (2, 2, 'f'), (4, 4, 'd')) 

merging (2, 2): poset((1, 3, 'b'), (2, 2, 'acef'), (4, 4, 'd')) 

merging (1, 1): poset((1, 1, 'a'), (1, 2, 'e'), (1, 3, 'b'), 

(2, 1, 'c'), (2, 2, 'f'), (4, 4, 'd')) 

sage: Q = copy(P) 

sage: Q.merge(); Q 

poset((4, 4, 'abcdef')) 

 

.. SEEALSO:: 

 

:meth:`MutablePosetShell.merge` 

 

TESTS:: 

 

sage: copy(P).merge(reverse=False) == copy(P).merge(reverse=True) 

True 

 

:: 

 

sage: P = MP(srange(4), 

....: merge=lambda l, r: l, can_merge=lambda l, r: l >= r); P 

poset(0, 1, 2, 3) 

sage: Q = P.copy() 

sage: Q.merge(reverse=True); Q 

poset(3) 

sage: R = P.mapped(lambda x: x+1) 

sage: R.merge(reverse=True); R 

poset(4) 

 

:: 

 

sage: P = MP(srange(4), 

....: merge=lambda l, r: r, can_merge=lambda l, r: l < r) 

sage: P.merge() 

Traceback (most recent call last): 

... 

RuntimeError: Stopping merge before started; 

the can_merge-function is not reflexive. 

""" 

if key is None: 

for shell in tuple(self.shells_topological(reverse=reverse)): 

if shell.key in self._shells_: 

self.merge(key=shell.key) 

return 

 

shell = self.shell(key) 

 

def can_merge(other): 

return self._can_merge_(shell.element, other.element) 

for rev in (reverse, not reverse): 

to_merge = shell.iter_depth_first( 

reverse=rev, condition=can_merge) 

try: 

next(to_merge) 

except StopIteration: 

raise RuntimeError('Stopping merge before started; the ' 

'can_merge-function is not reflexive.') 

for m in tuple(to_merge): 

if m.is_special(): 

continue 

shell.merge(m.element, check=False, delete=True) 

 

def maximal_elements(self): 

r""" 

Return an iterator over the maximal elements of this poset. 

 

INPUT: 

 

Nothing. 

 

OUTPUT: 

 

An iterator. 

 

EXAMPLES:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: class T(tuple): 

....: def __le__(left, right): 

....: return all(l <= r for l, r in zip(left, right)) 

sage: P = MP([T((1, 1)), T((1, 3)), T((2, 1)), 

....: T((1, 2)), T((2, 2))]) 

sage: list(P.maximal_elements()) 

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

 

.. SEEALSO:: 

 

:meth:`minimal_elements` 

""" 

return iter(shell.element 

for shell in self.oo.predecessors() 

if not shell.is_special()) 

 

def minimal_elements(self): 

r""" 

Return an iterator over the minimal elements of this poset. 

 

INPUT: 

 

Nothing. 

 

OUTPUT: 

 

An iterator. 

 

EXAMPLES:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: class T(tuple): 

....: def __le__(left, right): 

....: return all(l <= r for l, r in zip(left, right)) 

sage: P = MP([T((1, 3)), T((2, 1)), 

....: T((4, 4)), T((1, 2)), T((2, 2))]) 

sage: list(P.minimal_elements()) 

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

 

.. SEEALSO:: 

 

:meth:`maximal_elements` 

""" 

return iter(shell.element 

for shell in self.null.successors() 

if not shell.is_special()) 

 

def map(self, function, topological=False, reverse=False): 

r""" 

Apply the given ``function`` to each element of this poset. 

 

INPUT: 

 

- ``function`` -- a function mapping an existing element to 

a new element. 

 

- ``topological`` -- (default: ``False``) if set, then the 

mapping is done in topological order, otherwise unordered. 

 

- ``reverse`` -- is passed on to topological ordering. 

 

OUTPUT: 

 

Nothing. 

 

.. NOTE:: 

 

Since this method works inplace, it is not allowed that 

``function`` alters the key of an element. 

 

.. NOTE:: 

 

If ``function`` returns ``None``, then the element is 

removed. 

 

EXAMPLES:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: class T(tuple): 

....: def __le__(left, right): 

....: return all(l <= r for l, r in zip(left, right)) 

sage: P = MP([T((1, 3)), T((2, 1)), 

....: T((4, 4)), T((1, 2)), T((2, 2))], 

....: key=lambda e: e[:2]) 

sage: P.map(lambda e: e + (sum(e),)) 

sage: P 

poset((1, 2, 3), (1, 3, 4), (2, 1, 3), (2, 2, 4), (4, 4, 8)) 

 

TESTS:: 

 

sage: P.map(lambda e: e if e[2] != 4 else None); P 

poset((1, 2, 3), (2, 1, 3), (4, 4, 8)) 

 

.. SEEALSO:: 

 

:meth:`copy`, 

:meth:`mapped`. 

""" 

shells = self.shells_topological(reverse=reverse) \ 

if topological else self.shells() 

remove = [] 

for shell in shells: 

shell._element_ = function(shell._element_) 

if shell._element_ is None: 

remove.append(shell.key) 

for key in remove: 

self.remove(key) 

 

def mapped(self, function): 

r""" 

Return a poset where on each element the given ``function`` 

was applied. 

 

INPUT: 

 

- ``function`` -- a function mapping an existing element to 

a new element. 

 

- ``topological`` -- (default: ``False``) if set, then the 

mapping is done in topological order, otherwise unordered. 

 

- ``reverse`` -- is passed on to topological ordering. 

 

OUTPUT: 

 

A :class:`MutablePoset`. 

 

.. NOTE:: 

 

``function`` is not allowed to change the order of the keys, 

but changing the keys themselves is allowed (in contrast 

to :meth:`map`). 

 

EXAMPLES:: 

 

sage: from sage.data_structures.mutable_poset import MutablePoset as MP 

sage: class T(tuple): 

....: def __le__(left, right): 

....: return all(l <= r for l, r in zip(left, right)) 

sage: P = MP([T((1, 3)), T((2, 1)), 

....: T((4, 4)), T((1, 2)), T((2, 2))]) 

sage: P.mapped(lambda e: str(e)) 

poset('(1, 2)', '(1, 3)', '(2, 1)', '(2, 2)', '(4, 4)') 

 

.. SEEALSO:: 

 

:meth:`copy`, 

:meth:`map`. 

""" 

return self.copy(mapping=function) 

 

 

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