Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

645

646

647

648

649

650

651

652

653

654

655

656

657

658

659

660

661

662

663

664

665

666

667

668

669

670

671

672

673

674

675

676

677

678

679

680

681

682

683

684

685

686

687

688

689

690

691

692

693

694

695

696

697

698

699

700

701

702

703

704

705

706

707

708

709

710

711

712

713

714

715

716

717

718

719

720

721

722

723

724

725

726

727

728

729

730

731

732

733

734

735

736

737

738

739

740

741

742

743

744

745

746

747

748

749

750

751

752

753

754

755

756

757

758

759

760

761

762

763

764

765

766

767

768

769

770

771

772

773

774

775

776

777

778

779

780

781

782

783

784

785

786

787

788

789

790

791

792

793

794

795

796

797

798

799

800

801

802

803

804

805

806

807

808

809

810

811

812

813

814

815

816

817

818

819

820

821

822

823

824

825

826

827

828

829

830

831

832

833

834

835

836

837

838

839

840

841

842

843

844

845

846

847

848

849

850

851

852

853

854

855

856

857

858

859

860

861

862

863

864

865

866

867

868

869

870

871

872

873

874

875

876

877

878

879

880

881

882

883

884

885

886

887

888

889

890

891

892

893

894

895

896

897

898

899

900

901

902

903

904

905

906

907

908

909

910

911

912

913

914

915

916

917

918

919

920

921

922

923

924

925

926

927

928

929

930

931

932

933

934

935

936

937

938

939

940

941

942

943

944

945

946

947

948

949

950

951

952

953

954

955

956

957

958

959

960

961

962

963

964

965

966

967

968

969

970

971

972

973

974

975

976

977

978

979

980

981

982

983

984

985

986

987

988

989

990

991

992

993

994

995

996

997

998

999

1000

1001

1002

1003

1004

1005

1006

1007

1008

1009

1010

1011

1012

1013

1014

1015

1016

1017

1018

1019

1020

1021

1022

1023

1024

1025

1026

1027

1028

1029

1030

1031

1032

1033

1034

1035

1036

1037

1038

1039

1040

1041

1042

1043

1044

1045

1046

1047

1048

1049

1050

1051

1052

1053

1054

1055

1056

1057

1058

1059

1060

1061

1062

1063

1064

1065

1066

1067

1068

1069

1070

1071

1072

1073

1074

1075

1076

1077

1078

1079

1080

1081

1082

1083

1084

1085

1086

1087

1088

1089

1090

1091

1092

1093

1094

1095

1096

1097

1098

1099

1100

1101

1102

1103

1104

1105

1106

1107

1108

1109

1110

1111

1112

1113

1114

1115

1116

1117

1118

1119

1120

1121

1122

1123

1124

1125

1126

1127

1128

1129

1130

1131

1132

1133

1134

1135

1136

1137

1138

1139

1140

1141

1142

1143

1144

1145

1146

1147

1148

1149

1150

1151

1152

1153

1154

1155

1156

1157

1158

1159

1160

1161

1162

1163

1164

1165

1166

1167

1168

1169

1170

1171

1172

1173

1174

1175

1176

1177

1178

1179

1180

1181

1182

1183

1184

1185

1186

1187

1188

1189

1190

1191

1192

1193

1194

1195

1196

1197

1198

1199

1200

1201

1202

1203

1204

1205

1206

1207

1208

1209

1210

1211

1212

1213

1214

1215

1216

1217

1218

1219

1220

1221

1222

1223

1224

1225

1226

1227

1228

1229

1230

1231

1232

1233

1234

1235

1236

1237

1238

1239

1240

1241

1242

1243

1244

1245

1246

1247

1248

1249

1250

1251

1252

1253

1254

1255

1256

1257

1258

1259

1260

1261

1262

1263

1264

1265

1266

1267

1268

1269

1270

1271

1272

1273

1274

1275

1276

1277

1278

1279

1280

1281

1282

1283

1284

1285

1286

1287

1288

1289

1290

1291

1292

1293

1294

1295

1296

1297

1298

1299

1300

1301

1302

1303

1304

1305

1306

1307

1308

1309

1310

1311

1312

1313

1314

1315

1316

1317

1318

1319

1320

1321

1322

1323

1324

1325

1326

1327

1328

1329

1330

1331

1332

1333

1334

1335

1336

1337

1338

1339

1340

1341

1342

1343

1344

1345

1346

1347

1348

1349

1350

1351

1352

1353

1354

1355

1356

1357

1358

1359

1360

1361

1362

1363

1364

1365

1366

1367

1368

1369

1370

1371

1372

1373

1374

1375

1376

1377

1378

1379

1380

1381

1382

1383

1384

1385

1386

1387

1388

1389

1390

1391

1392

1393

1394

1395

1396

1397

1398

1399

1400

1401

1402

1403

1404

1405

1406

1407

1408

1409

1410

1411

1412

1413

1414

1415

1416

1417

1418

1419

1420

1421

1422

1423

1424

1425

1426

1427

1428

1429

1430

1431

1432

1433

1434

1435

1436

1437

1438

1439

1440

1441

1442

1443

1444

1445

1446

1447

1448

1449

1450

1451

1452

1453

1454

1455

1456

1457

1458

1459

1460

1461

1462

1463

1464

1465

1466

1467

1468

1469

1470

1471

1472

1473

1474

1475

1476

1477

1478

1479

1480

1481

1482

1483

1484

1485

1486

1487

1488

1489

1490

1491

1492

1493

1494

1495

1496

1497

1498

1499

1500

1501

1502

1503

1504

1505

1506

1507

1508

1509

1510

1511

1512

1513

1514

1515

1516

1517

1518

1519

1520

1521

1522

1523

1524

1525

1526

1527

1528

1529

1530

1531

1532

1533

1534

1535

1536

1537

1538

1539

1540

1541

1542

1543

1544

1545

1546

1547

1548

1549

1550

1551

1552

1553

1554

1555

1556

1557

1558

1559

1560

1561

1562

1563

1564

1565

1566

1567

1568

1569

1570

1571

1572

1573

1574

1575

1576

1577

1578

1579

1580

1581

1582

1583

1584

1585

1586

1587

1588

1589

1590

1591

1592

1593

1594

1595

1596

1597

1598

1599

1600

1601

1602

1603

1604

1605

1606

1607

1608

1609

1610

1611

1612

1613

1614

1615

1616

1617

1618

1619

1620

1621

1622

1623

1624

1625

1626

1627

1628

1629

1630

1631

1632

1633

1634

1635

1636

1637

1638

1639

1640

1641

1642

1643

1644

1645

1646

1647

1648

1649

1650

1651

1652

1653

1654

1655

1656

1657

1658

1659

1660

1661

1662

1663

1664

1665

1666

1667

1668

1669

1670

1671

1672

1673

1674

1675

1676

1677

1678

1679

1680

1681

1682

1683

1684

1685

1686

1687

1688

1689

1690

1691

1692

1693

1694

1695

1696

1697

1698

1699

1700

1701

1702

1703

1704

1705

1706

1707

1708

1709

1710

1711

1712

1713

1714

1715

1716

1717

1718

1719

1720

1721

1722

1723

1724

1725

1726

1727

1728

1729

1730

1731

1732

1733

1734

1735

1736

1737

1738

1739

1740

1741

1742

1743

1744

1745

1746

1747

1748

1749

1750

1751

1752

1753

1754

1755

1756

1757

1758

1759

1760

1761

1762

1763

1764

1765

1766

1767

1768

1769

1770

1771

1772

1773

1774

1775

1776

1777

1778

1779

1780

1781

1782

1783

1784

1785

1786

1787

1788

1789

1790

1791

1792

1793

1794

1795

1796

1797

1798

1799

1800

1801

1802

1803

1804

1805

1806

1807

1808

1809

1810

1811

1812

1813

1814

1815

1816

1817

1818

1819

1820

1821

1822

1823

1824

1825

1826

1827

1828

1829

1830

1831

1832

1833

1834

1835

1836

1837

1838

1839

1840

1841

1842

1843

1844

1845

1846

1847

1848

1849

1850

1851

1852

1853

1854

1855

1856

1857

1858

1859

1860

1861

1862

1863

1864

1865

1866

1867

1868

1869

1870

1871

1872

1873

1874

1875

1876

1877

1878

1879

1880

1881

1882

1883

1884

1885

1886

1887

1888

1889

1890

1891

1892

1893

1894

1895

1896

1897

1898

1899

1900

1901

1902

1903

1904

1905

1906

1907

1908

1909

1910

1911

1912

1913

1914

1915

1916

1917

1918

1919

1920

1921

1922

1923

1924

1925

1926

1927

1928

1929

1930

1931

1932

1933

1934

1935

1936

1937

1938

1939

1940

1941

1942

1943

1944

1945

1946

1947

1948

1949

1950

1951

1952

1953

1954

1955

1956

1957

1958

1959

1960

1961

1962

1963

1964

1965

1966

1967

1968

1969

1970

1971

1972

1973

1974

1975

1976

1977

1978

1979

1980

1981

1982

1983

1984

1985

1986

1987

1988

1989

1990

1991

1992

1993

1994

1995

1996

1997

1998

1999

2000

2001

2002

2003

2004

2005

2006

2007

2008

2009

2010

2011

2012

2013

2014

2015

2016

2017

2018

2019

2020

2021

2022

2023

2024

2025

2026

2027

2028

2029

2030

2031

2032

2033

2034

2035

2036

2037

2038

2039

2040

2041

2042

2043

2044

2045

2046

2047

2048

2049

2050

2051

2052

2053

2054

2055

2056

2057

2058

2059

2060

2061

2062

2063

2064

2065

2066

2067

2068

2069

2070

2071

2072

2073

2074

2075

2076

2077

2078

2079

2080

2081

2082

2083

2084

2085

2086

2087

2088

2089

2090

2091

2092

2093

2094

2095

2096

2097

2098

2099

2100

2101

2102

2103

2104

2105

2106

2107

2108

2109

2110

2111

2112

2113

2114

2115

2116

2117

2118

2119

2120

2121

2122

2123

2124

2125

2126

2127

2128

2129

2130

2131

2132

2133

2134

2135

2136

2137

2138

2139

2140

2141

2142

2143

2144

2145

2146

2147

2148

2149

2150

2151

2152

2153

2154

2155

2156

2157

2158

2159

2160

2161

2162

2163

2164

2165

2166

2167

2168

2169

2170

2171

2172

2173

2174

2175

2176

2177

2178

2179

2180

2181

2182

2183

2184

2185

2186

2187

2188

2189

2190

2191

2192

2193

2194

2195

2196

2197

2198

2199

2200

2201

2202

2203

2204

2205

2206

2207

2208

2209

2210

2211

2212

2213

2214

2215

2216

2217

2218

2219

2220

2221

2222

2223

2224

2225

2226

2227

2228

2229

2230

2231

2232

2233

2234

2235

2236

2237

2238

2239

2240

2241

2242

2243

2244

2245

2246

2247

2248

2249

2250

2251

2252

2253

2254

2255

2256

2257

2258

2259

2260

2261

2262

2263

2264

2265

2266

2267

2268

2269

2270

2271

2272

2273

2274

2275

2276

2277

2278

2279

2280

2281

2282

2283

2284

2285

2286

2287

2288

2289

2290

2291

2292

2293

2294

2295

2296

2297

2298

2299

2300

2301

2302

2303

2304

2305

2306

2307

2308

2309

2310

2311

2312

2313

2314

2315

2316

2317

2318

2319

2320

2321

2322

2323

2324

2325

2326

2327

2328

2329

2330

2331

2332

2333

2334

2335

2336

2337

2338

2339

2340

2341

2342

2343

2344

2345

2346

2347

2348

2349

2350

2351

2352

2353

2354

2355

2356

2357

2358

2359

2360

2361

2362

2363

2364

2365

2366

2367

2368

2369

2370

2371

2372

2373

2374

2375

2376

2377

2378

2379

2380

2381

2382

2383

2384

2385

2386

2387

2388

2389

2390

2391

2392

2393

2394

2395

2396

2397

2398

2399

2400

2401

2402

2403

2404

2405

2406

2407

2408

2409

2410

2411

2412

2413

2414

2415

2416

2417

2418

2419

2420

2421

2422

2423

2424

2425

2426

2427

2428

2429

2430

2431

2432

2433

2434

2435

2436

2437

2438

2439

2440

2441

2442

2443

2444

2445

2446

2447

2448

2449

2450

2451

2452

2453

2454

2455

2456

2457

2458

2459

2460

2461

2462

2463

2464

2465

2466

2467

2468

2469

2470

2471

2472

2473

2474

2475

2476

2477

2478

2479

2480

2481

2482

2483

2484

2485

2486

2487

2488

2489

2490

2491

2492

2493

2494

2495

2496

2497

2498

2499

2500

2501

2502

2503

2504

2505

2506

2507

2508

2509

2510

2511

2512

2513

2514

2515

2516

2517

2518

2519

2520

2521

2522

2523

2524

2525

2526

2527

2528

2529

2530

2531

2532

2533

2534

2535

2536

2537

2538

2539

2540

2541

2542

2543

2544

2545

2546

2547

2548

2549

2550

2551

2552

2553

2554

2555

2556

2557

2558

2559

2560

2561

2562

2563

2564

2565

2566

2567

2568

2569

2570

2571

2572

2573

2574

2575

2576

2577

2578

2579

2580

2581

2582

2583

2584

2585

2586

2587

2588

2589

2590

2591

2592

2593

2594

2595

2596

2597

2598

2599

2600

2601

2602

2603

2604

2605

2606

2607

2608

2609

2610

2611

2612

2613

2614

2615

2616

2617

2618

2619

2620

2621

2622

2623

2624

2625

2626

2627

2628

2629

2630

2631

2632

2633

2634

2635

2636

2637

2638

2639

2640

2641

2642

2643

2644

2645

2646

2647

2648

2649

2650

2651

2652

2653

2654

2655

2656

2657

2658

2659

2660

2661

2662

2663

2664

2665

2666

2667

2668

2669

2670

2671

2672

2673

2674

2675

2676

2677

2678

2679

2680

2681

2682

2683

2684

2685

2686

2687

2688

2689

2690

2691

2692

2693

2694

2695

2696

2697

2698

2699

2700

2701

2702

2703

2704

2705

2706

2707

2708

2709

2710

2711

2712

2713

2714

2715

2716

2717

2718

2719

2720

2721

2722

2723

2724

2725

2726

2727

2728

2729

2730

2731

2732

2733

2734

2735

2736

2737

2738

2739

2740

2741

2742

2743

2744

2745

2746

2747

2748

2749

2750

2751

2752

2753

2754

2755

2756

2757

2758

2759

2760

2761

2762

2763

2764

2765

2766

2767

2768

2769

2770

2771

2772

2773

2774

2775

2776

2777

2778

2779

2780

2781

2782

2783

2784

2785

2786

2787

2788

2789

2790

2791

2792

2793

2794

2795

2796

2797

2798

2799

2800

2801

2802

2803

2804

2805

2806

2807

2808

2809

2810

2811

2812

2813

2814

2815

2816

2817

2818

2819

2820

2821

2822

2823

2824

2825

2826

2827

2828

2829

2830

2831

2832

2833

2834

2835

2836

2837

2838

2839

2840

2841

2842

2843

2844

2845

2846

2847

2848

2849

2850

2851

2852

2853

2854

2855

2856

2857

2858

2859

2860

2861

2862

2863

2864

2865

2866

2867

2868

2869

2870

2871

2872

2873

2874

2875

2876

2877

2878

2879

2880

2881

2882

2883

2884

2885

2886

2887

2888

2889

2890

2891

2892

2893

2894

2895

2896

2897

2898

2899

2900

2901

2902

2903

2904

2905

2906

2907

2908

2909

2910

2911

2912

2913

2914

2915

2916

2917

2918

2919

2920

2921

2922

2923

2924

2925

2926

2927

2928

2929

2930

2931

2932

2933

2934

2935

2936

2937

2938

2939

2940

2941

2942

2943

2944

2945

2946

2947

2948

2949

2950

2951

2952

2953

2954

2955

2956

2957

2958

2959

2960

2961

2962

2963

2964

2965

2966

2967

2968

2969

2970

2971

2972

2973

2974

2975

2976

2977

2978

2979

2980

2981

2982

2983

2984

2985

2986

2987

2988

2989

2990

2991

2992

2993

2994

2995

2996

2997

2998

2999

3000

3001

3002

3003

3004

3005

3006

3007

3008

3009

3010

3011

3012

3013

3014

3015

3016

3017

3018

3019

3020

3021

3022

3023

3024

3025

3026

3027

3028

3029

3030

3031

3032

3033

3034

3035

3036

3037

3038

3039

3040

3041

3042

3043

3044

3045

3046

3047

3048

3049

3050

3051

3052

3053

3054

3055

3056

3057

3058

3059

3060

3061

3062

3063

3064

3065

3066

3067

3068

3069

3070

3071

3072

3073

3074

3075

3076

3077

3078

3079

3080

3081

3082

3083

3084

3085

3086

3087

3088

3089

3090

3091

3092

3093

3094

3095

3096

3097

3098

3099

3100

3101

3102

3103

3104

3105

3106

3107

3108

3109

3110

3111

3112

3113

3114

3115

3116

3117

3118

3119

3120

3121

3122

3123

3124

3125

3126

3127

3128

3129

3130

3131

3132

3133

3134

3135

3136

3137

3138

3139

3140

3141

3142

3143

3144

3145

3146

3147

3148

3149

3150

3151

3152

3153

3154

3155

3156

3157

3158

3159

3160

3161

3162

3163

3164

3165

3166

3167

3168

3169

3170

3171

3172

3173

3174

3175

3176

3177

3178

3179

3180

3181

3182

3183

3184

3185

3186

3187

3188

3189

3190

3191

3192

3193

3194

3195

3196

3197

3198

3199

3200

3201

3202

3203

3204

3205

3206

3207

3208

3209

3210

3211

3212

3213

3214

3215

3216

3217

3218

3219

3220

3221

3222

3223

3224

3225

3226

3227

3228

3229

3230

3231

3232

3233

3234

3235

3236

3237

3238

3239

3240

3241

3242

3243

3244

3245

3246

3247

3248

3249

3250

3251

3252

3253

3254

3255

3256

3257

3258

3259

3260

3261

3262

3263

3264

3265

3266

3267

3268

3269

3270

3271

3272

3273

3274

3275

3276

3277

3278

3279

3280

3281

3282

3283

3284

3285

3286

3287

3288

3289

3290

3291

3292

3293

3294

3295

3296

3297

3298

3299

3300

3301

3302

3303

3304

3305

3306

3307

3308

3309

3310

3311

3312

3313

3314

3315

3316

3317

3318

3319

3320

3321

3322

3323

3324

3325

3326

3327

3328

3329

3330

3331

3332

3333

3334

3335

3336

3337

3338

3339

3340

3341

3342

3343

3344

3345

3346

3347

3348

3349

3350

3351

3352

3353

3354

3355

3356

3357

3358

3359

3360

3361

3362

3363

3364

3365

3366

3367

3368

3369

3370

3371

3372

3373

3374

3375

3376

3377

3378

3379

3380

3381

3382

3383

3384

3385

3386

3387

3388

3389

3390

3391

3392

3393

3394

3395

3396

3397

3398

3399

3400

3401

3402

3403

3404

3405

3406

3407

3408

3409

3410

3411

3412

3413

3414

3415

3416

3417

3418

3419

3420

3421

3422

3423

3424

3425

3426

3427

3428

3429

3430

3431

3432

3433

3434

3435

3436

3437

3438

3439

3440

3441

3442

3443

3444

3445

3446

3447

3448

3449

3450

3451

3452

3453

3454

3455

3456

3457

3458

3459

3460

3461

3462

3463

3464

3465

3466

3467

3468

3469

3470

3471

3472

3473

3474

3475

3476

3477

3478

3479

3480

3481

3482

3483

3484

3485

3486

3487

3488

3489

3490

3491

3492

3493

3494

3495

3496

3497

3498

3499

3500

3501

3502

3503

3504

3505

3506

3507

3508

3509

3510

3511

3512

3513

3514

3515

3516

3517

3518

3519

3520

3521

3522

3523

3524

3525

3526

3527

3528

3529

3530

3531

3532

3533

3534

3535

3536

3537

3538

3539

3540

3541

3542

3543

3544

3545

3546

3547

3548

3549

3550

3551

3552

3553

3554

3555

3556

3557

3558

3559

3560

3561

3562

3563

3564

3565

3566

3567

3568

3569

3570

3571

3572

3573

3574

3575

3576

3577

3578

3579

3580

3581

3582

3583

3584

3585

3586

3587

3588

3589

3590

3591

3592

3593

3594

3595

3596

3597

3598

3599

3600

3601

3602

3603

3604

3605

3606

3607

3608

3609

3610

3611

3612

3613

3614

3615

3616

3617

3618

3619

3620

3621

3622

3623

3624

3625

3626

3627

3628

3629

3630

3631

3632

3633

3634

3635

3636

3637

3638

3639

3640

3641

3642

3643

3644

3645

3646

3647

3648

3649

3650

3651

3652

3653

3654

3655

3656

3657

3658

3659

3660

3661

3662

3663

3664

3665

3666

3667

3668

3669

3670

3671

3672

3673

3674

3675

3676

3677

3678

3679

3680

3681

3682

3683

3684

3685

3686

3687

3688

3689

3690

3691

3692

3693

3694

3695

3696

3697

3698

3699

3700

3701

3702

3703

3704

3705

3706

3707

3708

3709

3710

3711

3712

3713

3714

3715

3716

3717

3718

3719

3720

3721

3722

3723

3724

3725

3726

3727

3728

3729

3730

3731

3732

3733

3734

3735

3736

3737

3738

3739

3740

3741

3742

3743

3744

3745

3746

3747

3748

3749

3750

3751

3752

3753

3754

3755

3756

3757

3758

3759

3760

3761

3762

3763

3764

3765

3766

3767

3768

3769

3770

3771

3772

3773

3774

3775

3776

3777

3778

3779

3780

3781

3782

3783

3784

3785

3786

3787

3788

3789

3790

3791

3792

3793

3794

3795

3796

3797

3798

3799

3800

3801

3802

3803

3804

3805

3806

3807

3808

3809

3810

3811

3812

3813

3814

3815

3816

3817

3818

3819

3820

3821

3822

3823

3824

3825

3826

3827

3828

3829

3830

3831

3832

3833

3834

3835

3836

3837

3838

3839

3840

3841

3842

3843

3844

3845

3846

3847

3848

3849

3850

3851

3852

3853

3854

3855

3856

3857

3858

3859

3860

3861

3862

3863

3864

3865

3866

3867

3868

3869

3870

3871

3872

3873

3874

3875

3876

3877

3878

3879

3880

3881

3882

3883

3884

3885

3886

3887

3888

3889

3890

3891

3892

3893

3894

3895

3896

3897

3898

3899

3900

3901

3902

3903

3904

3905

3906

3907

3908

3909

3910

3911

3912

3913

3914

3915

3916

3917

3918

3919

3920

3921

3922

3923

3924

3925

3926

3927

3928

3929

3930

3931

3932

3933

3934

3935

3936

3937

3938

3939

3940

3941

3942

3943

3944

3945

3946

3947

3948

3949

3950

3951

3952

3953

3954

3955

3956

3957

3958

3959

3960

3961

3962

3963

3964

3965

3966

3967

3968

3969

3970

3971

3972

3973

3974

3975

3976

3977

3978

3979

3980

3981

3982

3983

3984

3985

3986

3987

3988

3989

3990

3991

3992

3993

3994

3995

3996

3997

3998

3999

4000

4001

4002

4003

4004

4005

4006

4007

4008

4009

4010

4011

4012

4013

4014

4015

4016

4017

4018

4019

4020

4021

4022

4023

4024

4025

4026

4027

4028

4029

4030

4031

4032

4033

4034

4035

4036

4037

4038

4039

4040

4041

4042

4043

4044

4045

4046

4047

4048

4049

4050

4051

4052

4053

4054

4055

4056

4057

4058

4059

4060

4061

4062

4063

4064

4065

4066

4067

4068

4069

4070

4071

4072

4073

4074

4075

4076

4077

4078

4079

4080

4081

4082

4083

4084

4085

4086

4087

4088

4089

4090

4091

4092

4093

4094

4095

4096

4097

4098

4099

4100

4101

4102

4103

4104

4105

4106

4107

4108

4109

4110

4111

4112

4113

4114

4115

4116

4117

4118

4119

4120

4121

4122

4123

4124

4125

4126

4127

4128

4129

4130

4131

4132

4133

4134

4135

4136

4137

4138

4139

4140

4141

4142

4143

4144

4145

4146

4147

4148

4149

4150

4151

4152

4153

4154

4155

4156

4157

4158

4159

4160

4161

4162

4163

4164

4165

4166

4167

4168

4169

4170

4171

4172

4173

4174

4175

4176

4177

4178

4179

4180

4181

4182

4183

4184

4185

4186

4187

4188

4189

4190

4191

4192

4193

4194

4195

4196

4197

4198

4199

4200

4201

4202

4203

4204

4205

4206

4207

4208

4209

4210

4211

4212

4213

4214

4215

4216

4217

4218

4219

4220

4221

4222

4223

4224

4225

4226

4227

4228

4229

4230

4231

4232

4233

4234

4235

4236

4237

4238

4239

4240

4241

4242

4243

4244

4245

4246

4247

4248

4249

4250

4251

4252

4253

4254

4255

4256

4257

4258

4259

4260

4261

4262

4263

4264

4265

4266

4267

4268

4269

4270

4271

4272

4273

4274

4275

4276

4277

4278

4279

4280

4281

4282

4283

4284

4285

4286

4287

4288

4289

4290

4291

4292

4293

4294

4295

4296

4297

4298

4299

4300

4301

4302

4303

4304

4305

4306

4307

4308

4309

4310

4311

4312

4313

4314

4315

4316

4317

4318

4319

4320

4321

4322

4323

4324

4325

4326

4327

4328

4329

4330

4331

4332

4333

4334

4335

4336

4337

4338

4339

4340

4341

4342

4343

4344

4345

4346

4347

4348

4349

4350

4351

4352

4353

4354

4355

4356

4357

4358

4359

4360

4361

4362

4363

4364

4365

4366

4367

4368

4369

4370

4371

4372

4373

4374

4375

4376

4377

4378

4379

4380

4381

4382

4383

4384

4385

4386

4387

4388

4389

4390

4391

4392

4393

4394

4395

4396

4397

4398

4399

4400

4401

4402

4403

4404

4405

4406

4407

4408

4409

4410

4411

4412

4413

4414

4415

4416

4417

4418

4419

4420

4421

4422

4423

4424

4425

4426

4427

4428

4429

4430

4431

4432

4433

4434

4435

4436

4437

4438

4439

4440

4441

4442

4443

4444

4445

4446

4447

4448

4449

4450

4451

4452

4453

4454

4455

4456

4457

4458

4459

4460

4461

4462

4463

4464

4465

4466

4467

4468

4469

4470

4471

4472

4473

4474

4475

4476

4477

4478

4479

4480

4481

4482

4483

4484

4485

4486

4487

4488

4489

4490

4491

4492

4493

4494

4495

4496

4497

4498

4499

4500

4501

4502

4503

4504

4505

4506

4507

4508

4509

4510

4511

4512

4513

4514

4515

4516

4517

4518

4519

4520

4521

4522

4523

4524

4525

4526

4527

4528

4529

4530

4531

4532

4533

4534

4535

4536

4537

4538

4539

4540

4541

4542

4543

4544

4545

4546

4547

4548

4549

4550

4551

4552

4553

4554

4555

4556

4557

4558

4559

4560

4561

4562

4563

4564

4565

4566

4567

4568

4569

4570

4571

4572

4573

4574

4575

4576

4577

4578

4579

4580

4581

4582

4583

4584

4585

4586

4587

4588

4589

4590

4591

4592

4593

4594

4595

4596

4597

4598

4599

4600

4601

4602

4603

4604

4605

4606

4607

4608

4609

4610

4611

4612

4613

4614

4615

4616

4617

4618

4619

4620

4621

4622

4623

4624

4625

4626

4627

4628

4629

4630

4631

4632

4633

4634

4635

4636

4637

4638

4639

4640

4641

4642

4643

4644

4645

4646

4647

4648

4649

4650

4651

4652

4653

4654

4655

4656

4657

4658

4659

4660

4661

4662

4663

4664

4665

4666

4667

4668

4669

4670

4671

4672

4673

4674

4675

4676

4677

4678

4679

4680

4681

4682

4683

4684

4685

4686

4687

4688

4689

4690

4691

4692

4693

4694

4695

4696

4697

4698

4699

4700

4701

4702

4703

4704

4705

4706

4707

4708

4709

4710

4711

4712

4713

4714

4715

4716

4717

4718

4719

4720

4721

4722

4723

4724

4725

4726

4727

4728

4729

4730

4731

4732

4733

4734

4735

4736

4737

4738

4739

4740

4741

4742

4743

4744

4745

4746

4747

4748

4749

4750

4751

4752

4753

4754

4755

4756

4757

4758

4759

4760

4761

4762

4763

4764

4765

4766

4767

4768

4769

4770

4771

4772

4773

4774

4775

4776

4777

4778

4779

4780

4781

4782

4783

4784

4785

4786

4787

4788

4789

4790

4791

4792

4793

4794

4795

4796

4797

4798

4799

4800

4801

4802

4803

4804

4805

4806

4807

4808

4809

4810

4811

4812

4813

4814

4815

4816

4817

4818

4819

4820

4821

4822

4823

4824

4825

4826

4827

4828

4829

4830

4831

4832

4833

4834

4835

4836

4837

4838

4839

4840

4841

4842

4843

4844

4845

4846

4847

4848

4849

4850

4851

4852

4853

4854

4855

4856

4857

4858

4859

4860

4861

4862

4863

4864

4865

4866

4867

4868

4869

4870

4871

4872

4873

4874

4875

4876

4877

4878

4879

4880

4881

4882

4883

4884

4885

4886

4887

4888

4889

4890

4891

4892

4893

4894

4895

4896

4897

4898

4899

4900

4901

4902

4903

4904

4905

4906

4907

4908

4909

4910

4911

4912

4913

4914

4915

4916

4917

4918

4919

4920

4921

4922

4923

4924

4925

4926

4927

4928

4929

4930

4931

4932

4933

4934

4935

4936

4937

4938

4939

4940

4941

4942

4943

4944

4945

4946

4947

4948

4949

4950

4951

4952

4953

4954

4955

4956

4957

4958

4959

4960

4961

4962

4963

4964

4965

4966

4967

4968

4969

4970

4971

4972

4973

4974

4975

4976

4977

4978

4979

4980

4981

4982

4983

4984

4985

4986

4987

4988

4989

4990

4991

4992

4993

4994

4995

4996

4997

4998

4999

5000

5001

5002

5003

5004

5005

5006

5007

5008

5009

5010

5011

5012

5013

5014

5015

5016

5017

5018

5019

5020

5021

5022

5023

5024

5025

5026

5027

5028

5029

5030

5031

5032

5033

5034

5035

5036

5037

5038

5039

5040

5041

5042

5043

5044

5045

5046

5047

5048

5049

5050

5051

5052

5053

5054

5055

5056

5057

5058

5059

5060

5061

5062

5063

5064

5065

5066

5067

5068

5069

5070

5071

5072

5073

5074

5075

5076

5077

5078

5079

5080

5081

5082

5083

5084

5085

5086

5087

5088

5089

5090

5091

5092

5093

5094

5095

5096

5097

5098

5099

5100

5101

5102

5103

5104

5105

5106

5107

5108

5109

5110

5111

5112

5113

5114

5115

5116

5117

5118

5119

5120

5121

5122

5123

5124

5125

5126

5127

5128

5129

5130

5131

5132

5133

5134

5135

5136

5137

5138

5139

5140

5141

5142

5143

5144

5145

5146

5147

5148

5149

5150

5151

5152

5153

5154

5155

5156

5157

5158

5159

5160

5161

5162

5163

5164

5165

5166

5167

5168

5169

5170

5171

5172

5173

5174

5175

5176

5177

5178

5179

5180

5181

5182

5183

5184

5185

5186

5187

5188

5189

5190

5191

5192

5193

5194

5195

5196

5197

5198

5199

5200

5201

5202

5203

5204

5205

5206

5207

5208

5209

5210

5211

5212

5213

5214

5215

5216

5217

5218

5219

5220

5221

5222

5223

5224

5225

5226

5227

5228

5229

5230

5231

5232

5233

5234

5235

5236

5237

5238

5239

5240

5241

5242

5243

5244

5245

5246

5247

5248

5249

5250

5251

5252

5253

5254

5255

5256

5257

5258

5259

5260

5261

5262

5263

5264

5265

5266

5267

5268

5269

5270

5271

5272

5273

5274

5275

5276

5277

5278

5279

5280

5281

5282

5283

5284

5285

5286

5287

5288

5289

5290

5291

5292

5293

5294

5295

5296

5297

5298

5299

5300

5301

5302

5303

5304

5305

5306

5307

5308

5309

5310

5311

5312

5313

5314

5315

5316

5317

5318

5319

5320

5321

5322

5323

5324

5325

5326

5327

5328

5329

5330

5331

5332

5333

5334

5335

5336

5337

5338

5339

5340

5341

5342

5343

5344

5345

5346

5347

5348

5349

5350

5351

5352

5353

5354

5355

5356

5357

5358

5359

5360

5361

5362

5363

5364

5365

5366

5367

5368

5369

5370

5371

5372

5373

5374

5375

5376

5377

5378

5379

5380

5381

5382

5383

5384

5385

5386

5387

5388

5389

5390

5391

5392

5393

5394

5395

5396

5397

5398

5399

5400

5401

5402

5403

5404

5405

5406

5407

5408

5409

5410

5411

5412

5413

5414

5415

5416

5417

5418

5419

5420

5421

5422

5423

5424

5425

5426

5427

5428

5429

5430

5431

5432

5433

5434

5435

5436

5437

5438

5439

5440

5441

5442

5443

5444

5445

5446

5447

5448

5449

5450

5451

5452

5453

5454

5455

5456

5457

5458

5459

5460

5461

5462

5463

5464

5465

5466

5467

5468

5469

5470

5471

5472

5473

5474

5475

5476

5477

5478

5479

5480

5481

5482

5483

5484

5485

5486

5487

5488

5489

5490

5491

5492

5493

5494

5495

5496

5497

5498

5499

5500

5501

5502

5503

5504

5505

5506

5507

5508

5509

5510

5511

5512

5513

5514

5515

5516

5517

5518

5519

5520

5521

5522

5523

5524

5525

5526

5527

5528

5529

5530

5531

5532

5533

5534

5535

5536

5537

5538

5539

5540

5541

5542

5543

5544

5545

5546

5547

5548

5549

5550

5551

5552

5553

5554

5555

5556

5557

5558

5559

5560

5561

5562

5563

5564

5565

5566

5567

5568

5569

5570

5571

5572

5573

5574

5575

5576

5577

5578

5579

5580

5581

5582

5583

5584

5585

5586

5587

5588

5589

5590

5591

5592

5593

5594

5595

5596

5597

5598

5599

5600

5601

5602

5603

5604

5605

5606

5607

5608

5609

5610

5611

5612

5613

5614

5615

5616

5617

5618

5619

5620

5621

5622

5623

5624

5625

5626

5627

5628

5629

5630

5631

5632

5633

5634

5635

5636

5637

5638

5639

5640

5641

5642

5643

5644

5645

5646

5647

5648

5649

5650

5651

5652

5653

5654

5655

5656

5657

5658

5659

5660

5661

5662

5663

5664

5665

5666

5667

5668

5669

5670

5671

5672

5673

5674

5675

5676

5677

5678

5679

5680

5681

5682

5683

5684

5685

5686

5687

5688

5689

5690

5691

5692

5693

5694

5695

5696

5697

5698

5699

5700

5701

5702

5703

5704

5705

5706

5707

5708

5709

5710

5711

5712

5713

5714

5715

5716

5717

5718

5719

5720

5721

5722

5723

5724

5725

5726

5727

5728

5729

5730

5731

5732

5733

5734

5735

5736

5737

5738

5739

5740

5741

5742

5743

5744

5745

5746

5747

5748

5749

5750

5751

5752

5753

5754

5755

5756

5757

5758

5759

5760

5761

5762

5763

5764

5765

5766

5767

5768

5769

5770

5771

5772

5773

5774

5775

5776

5777

5778

5779

5780

5781

5782

5783

5784

5785

5786

5787

5788

5789

5790

5791

5792

5793

5794

5795

5796

5797

5798

5799

5800

5801

5802

5803

5804

5805

5806

5807

5808

5809

5810

5811

5812

5813

5814

5815

5816

5817

5818

5819

5820

5821

5822

5823

5824

5825

5826

5827

5828

5829

5830

5831

5832

5833

5834

5835

5836

5837

5838

5839

5840

5841

5842

5843

5844

5845

5846

5847

5848

5849

5850

5851

5852

5853

5854

5855

5856

5857

5858

5859

5860

5861

5862

5863

5864

5865

5866

5867

5868

5869

5870

5871

5872

5873

5874

5875

5876

5877

5878

5879

5880

5881

5882

5883

5884

5885

5886

5887

5888

5889

5890

5891

5892

5893

5894

5895

5896

5897

5898

5899

5900

5901

5902

5903

5904

5905

5906

5907

5908

5909

5910

5911

5912

5913

5914

5915

5916

5917

5918

5919

5920

5921

5922

5923

5924

5925

5926

5927

5928

5929

5930

5931

5932

5933

5934

5935

5936

5937

5938

5939

5940

5941

5942

5943

5944

5945

5946

5947

5948

5949

5950

5951

5952

5953

5954

5955

5956

5957

5958

5959

5960

5961

5962

5963

5964

5965

5966

5967

5968

5969

5970

5971

5972

5973

5974

5975

5976

5977

5978

5979

5980

5981

5982

5983

5984

5985

5986

5987

5988

5989

5990

5991

5992

5993

5994

5995

5996

5997

5998

5999

6000

6001

6002

6003

6004

6005

6006

6007

6008

6009

6010

6011

6012

6013

6014

6015

6016

6017

6018

6019

6020

6021

6022

6023

6024

6025

6026

6027

6028

6029

6030

6031

6032

6033

6034

6035

6036

6037

6038

6039

6040

6041

6042

6043

6044

6045

6046

6047

6048

6049

6050

6051

6052

6053

6054

6055

6056

6057

6058

6059

6060

6061

6062

6063

6064

6065

6066

6067

6068

6069

6070

6071

6072

6073

6074

6075

6076

6077

6078

6079

6080

6081

6082

6083

6084

6085

6086

6087

6088

6089

6090

6091

6092

6093

6094

6095

6096

6097

6098

6099

6100

6101

6102

6103

6104

6105

6106

6107

6108

6109

6110

6111

6112

6113

6114

6115

6116

6117

6118

6119

6120

6121

6122

6123

6124

6125

6126

6127

6128

6129

6130

6131

6132

6133

6134

6135

6136

6137

6138

6139

6140

6141

6142

6143

6144

6145

6146

6147

6148

6149

6150

6151

6152

6153

6154

6155

6156

6157

6158

6159

6160

6161

6162

6163

6164

6165

6166

6167

6168

6169

6170

6171

6172

6173

6174

6175

6176

6177

6178

6179

6180

6181

6182

6183

6184

6185

6186

6187

6188

6189

6190

6191

6192

6193

6194

6195

6196

6197

6198

6199

6200

6201

6202

6203

6204

6205

6206

6207

6208

6209

6210

6211

6212

6213

6214

6215

6216

6217

6218

6219

6220

6221

6222

6223

6224

6225

6226

6227

6228

6229

6230

6231

6232

6233

6234

6235

6236

6237

6238

6239

6240

6241

6242

6243

6244

6245

6246

6247

6248

6249

6250

6251

6252

6253

6254

6255

6256

6257

6258

6259

6260

6261

6262

6263

6264

6265

6266

6267

6268

6269

6270

6271

6272

6273

6274

6275

6276

6277

6278

6279

6280

6281

6282

6283

6284

6285

6286

6287

6288

6289

6290

6291

6292

6293

6294

6295

6296

6297

6298

6299

6300

6301

6302

6303

6304

6305

6306

6307

6308

6309

6310

6311

6312

6313

6314

6315

6316

6317

6318

6319

6320

6321

6322

6323

6324

6325

6326

6327

6328

6329

6330

6331

6332

6333

6334

6335

6336

6337

6338

6339

6340

6341

6342

6343

6344

6345

6346

6347

6348

6349

6350

6351

6352

6353

6354

6355

6356

6357

6358

6359

6360

6361

6362

6363

6364

6365

6366

6367

6368

6369

6370

6371

6372

6373

6374

6375

6376

6377

6378

6379

6380

6381

6382

6383

6384

6385

6386

6387

6388

6389

6390

6391

6392

6393

6394

6395

6396

6397

6398

6399

6400

6401

6402

6403

6404

6405

6406

6407

6408

6409

6410

6411

6412

6413

6414

6415

6416

6417

6418

6419

6420

6421

6422

6423

6424

6425

6426

6427

6428

6429

6430

6431

6432

6433

6434

6435

6436

6437

6438

6439

6440

6441

6442

6443

6444

6445

6446

6447

6448

6449

6450

6451

6452

6453

6454

6455

6456

6457

6458

6459

6460

6461

6462

6463

6464

6465

6466

6467

6468

6469

6470

6471

6472

r""" 

Base class for polyhedra 

""" 

 

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

# Copyright (C) 2008 Marshall Hampton <hamptonio@gmail.com> 

# Copyright (C) 2011 Volker Braun <vbraun.name@gmail.com> 

# Copyright (C) 2015 Jean-Philippe Labbe <labbe at math.huji.ac.il> 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License 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 division, print_function, absolute_import 

 

import itertools 

import six 

from sage.structure.element import Element, coerce_binop, is_Vector 

from sage.structure.richcmp import rich_to_bool, op_NE 

 

from sage.misc.all import cached_method, prod 

from sage.misc.package import is_package_installed, PackageNotFoundError 

from sage.misc.randstate import current_randstate 

 

from sage.rings.all import QQ, ZZ, AA 

from sage.rings.real_double import RDF 

from sage.modules.free_module_element import vector 

from sage.modules.vector_space_morphism import linear_transformation 

from sage.matrix.constructor import matrix 

from sage.functions.other import sqrt, floor, ceil, binomial 

from sage.groups.matrix_gps.finitely_generated import MatrixGroup 

from sage.graphs.graph import Graph 

from sage.graphs.digraph import DiGraph 

 

from .constructor import Polyhedron 

 

from sage.misc.superseded import deprecated_function_alias 

 

from sage.categories.sets_cat import EmptySetError 

 

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

# Notes if you want to implement your own backend: 

# 

# * derive from Polyhedron_base 

# 

# * you must implement _init_from_Vrepresentation and 

# _init_from_Vrepresentationa 

# 

# * You might want to override _init_empty_polyhedron, 

# _init_facet_adjacency_matrix, _init_vertex_adjacency_matrix, and 

# _make_polyhedron_face. 

# 

# * You can of course also override any other method for which you 

# have a faster implementation. 

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

 

 

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

def is_Polyhedron(X): 

""" 

Test whether ``X`` is a Polyhedron. 

 

INPUT: 

 

- ``X`` -- anything. 

 

OUTPUT: 

 

Boolean. 

 

EXAMPLES:: 

 

sage: p = polytopes.hypercube(2) 

sage: from sage.geometry.polyhedron.base import is_Polyhedron 

sage: is_Polyhedron(p) 

True 

sage: is_Polyhedron(123456) 

False 

""" 

return isinstance(X, Polyhedron_base) 

 

 

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

class Polyhedron_base(Element): 

""" 

Base class for Polyhedron objects 

 

INPUT: 

 

- ``parent`` -- the parent, an instance of 

:class:`~sage.geometry.polyhedron.parent.Polyhedra`. 

 

- ``Vrep`` -- a list ``[vertices, rays, lines]`` or ``None``. The 

V-representation of the polyhedron. If ``None``, the polyhedron 

is determined by the H-representation. 

 

- ``Hrep`` -- a list ``[ieqs, eqns]`` or ``None``. The 

H-representation of the polyhedron. If ``None``, the polyhedron 

is determined by the V-representation. 

 

Only one of ``Vrep`` or ``Hrep`` can be different from ``None``. 

 

TESTS:: 

 

sage: p = Polyhedron() 

sage: TestSuite(p).run() 

""" 

 

def __init__(self, parent, Vrep, Hrep, **kwds): 

""" 

Initializes the polyhedron. 

 

See :class:`Polyhedron_base` for a description of the input 

data. 

 

TESTS:: 

 

sage: p = Polyhedron() # indirect doctests 

""" 

Element.__init__(self, parent=parent) 

if Vrep is not None: 

vertices, rays, lines = Vrep 

self._init_from_Vrepresentation(vertices, rays, lines, **kwds) 

elif Hrep is not None: 

ieqs, eqns = Hrep 

self._init_from_Hrepresentation(ieqs, eqns, **kwds) 

else: 

self._init_empty_polyhedron() 

 

def __hash__(self): 

r""" 

TESTS:: 

 

sage: K.<a> = QuadraticField(2) 

sage: p = Polyhedron(vertices=[(0,1,a),(3,a,5)], 

....: rays=[(a,2,3), (0,0,1)], 

....: base_ring=K) 

sage: q = Polyhedron(vertices=[(3,a,5),(0,1,a)], 

....: rays=[(0,0,1), (a,2,3)], 

....: base_ring=K) 

sage: hash(p) == hash(q) 

True 

""" 

# TODO: find something better *but* fast 

return hash((self.dim(), 

self.ambient_dim(), 

self.n_Hrepresentation(), 

self.n_Vrepresentation(), 

self.n_equations(), 

self.n_facets(), 

self.n_inequalities(), 

self.n_lines(), 

self.n_rays(), 

self.n_vertices())) 

 

def _sage_input_(self, sib, coerced): 

""" 

Return Sage command to reconstruct ``self``. 

 

See :mod:`sage.misc.sage_input` for details. 

 

.. TODO:: 

 

Add the option `preparse` to the method. 

 

EXAMPLES:: 

 

sage: P = Polyhedron(vertices = [[1, 0], [0, 1]], rays = [[1, 1]], backend='ppl') 

sage: sage_input(P) 

Polyhedron(backend='ppl', base_ring=ZZ, rays=[(1, 1)], vertices=[(0, 1), (1, 0)]) 

sage: P = Polyhedron(vertices = [[1, 0], [0, 1]], rays = [[1, 1]], backend='normaliz') # optional - pynormaliz 

sage: sage_input(P) # optional - pynormaliz 

Polyhedron(backend='normaliz', base_ring=ZZ, rays=[(1, 1)], vertices=[(0, 1), (1, 0)]) 

sage: P = Polyhedron(vertices = [[1, 0], [0, 1]], rays = [[1, 1]], backend='polymake') # optional - polymake 

sage: sage_input(P) # optional - polymake 

Polyhedron(backend='polymake', base_ring=QQ, rays=[(QQ(1), QQ(1))], vertices=[(QQ(1), QQ(0)), (QQ(0), QQ(1))]) 

""" 

kwds = dict() 

kwds['base_ring'] = sib(self.base_ring()) 

kwds['backend'] = sib(self.backend()) 

if self.n_vertices() > 0: 

kwds['vertices'] = [sib(tuple(v)) for v in self.vertices()] 

if self.n_rays() > 0: 

kwds['rays'] = [sib(tuple(r)) for r in self.rays()] 

if self.n_lines() > 0: 

kwds['lines'] = [sib(tuple(l)) for l in self.lines()] 

return sib.name('Polyhedron')(**kwds) 

 

def _init_from_Vrepresentation(self, vertices, rays, lines, **kwds): 

""" 

Construct polyhedron from V-representation data. 

 

INPUT: 

 

- ``vertices`` -- list of point. Each point can be specified 

as any iterable container of 

:meth:`~sage.geometry.polyhedron.base.base_ring` elements. 

 

- ``rays`` -- list of rays. Each ray can be specified as any 

iterable container of 

:meth:`~sage.geometry.polyhedron.base.base_ring` elements. 

 

- ``lines`` -- list of lines. Each line can be specified as 

any iterable container of 

:meth:`~sage.geometry.polyhedron.base.base_ring` elements. 

 

EXAMPLES:: 

 

sage: p = Polyhedron() 

sage: from sage.geometry.polyhedron.base import Polyhedron_base 

sage: Polyhedron_base._init_from_Vrepresentation(p, [], [], []) 

Traceback (most recent call last): 

... 

NotImplementedError: A derived class must implement this method. 

""" 

raise NotImplementedError('A derived class must implement this method.') 

 

def _init_from_Hrepresentation(self, ieqs, eqns, **kwds): 

""" 

Construct polyhedron from H-representation data. 

 

INPUT: 

 

- ``ieqs`` -- list of inequalities. Each line can be specified 

as any iterable container of 

:meth:`~sage.geometry.polyhedron.base.base_ring` elements. 

 

- ``eqns`` -- list of equalities. Each line can be specified 

as any iterable container of 

:meth:`~sage.geometry.polyhedron.base.base_ring` elements. 

 

EXAMPLES:: 

 

sage: p = Polyhedron() 

sage: from sage.geometry.polyhedron.base import Polyhedron_base 

sage: Polyhedron_base._init_from_Hrepresentation(p, [], []) 

Traceback (most recent call last): 

... 

NotImplementedError: A derived class must implement this method. 

""" 

raise NotImplementedError('A derived class must implement this method.') 

 

def _init_empty_polyhedron(self): 

""" 

Initializes an empty polyhedron. 

 

TESTS:: 

 

sage: empty = Polyhedron(); empty 

The empty polyhedron in ZZ^0 

sage: empty.Vrepresentation() 

() 

sage: empty.Hrepresentation() 

(An equation -1 == 0,) 

sage: Polyhedron(vertices = []) 

The empty polyhedron in ZZ^0 

sage: Polyhedron(vertices = [])._init_empty_polyhedron() 

sage: from sage.geometry.polyhedron.parent import Polyhedra 

sage: Polyhedra(QQ,7)() 

A 0-dimensional polyhedron in QQ^7 defined as the convex hull of 1 vertex 

""" 

self._Vrepresentation = [] 

self._Hrepresentation = [] 

self.parent()._make_Equation(self, [-1] + [0]*self.ambient_dim()) 

self._Vrepresentation = tuple(self._Vrepresentation) 

self._Hrepresentation = tuple(self._Hrepresentation) 

 

V_matrix = matrix(ZZ, 0, 0, 0) 

V_matrix.set_immutable() 

self.vertex_adjacency_matrix.set_cache(V_matrix) 

 

H_matrix = matrix(ZZ, 1, 1, 0) 

H_matrix.set_immutable() 

self.facet_adjacency_matrix.set_cache(H_matrix) 

 

def _facet_adjacency_matrix(self): 

""" 

Compute the facet adjacency matrix in case it has not been 

computed during initialization. 

 

EXAMPLES:: 

 

sage: p = Polyhedron(vertices=[(0,0),(1,0),(0,1)]) 

sage: p._facet_adjacency_matrix() 

[0 1 1] 

[1 0 1] 

[1 1 0] 

 

Checks that :trac:`22455` is fixed:: 

 

sage: s = polytopes.simplex(2) 

sage: s._facet_adjacency_matrix() 

[0 1 1] 

[1 0 1] 

[1 1 0] 

 

""" 

# TODO: This implementation computes the whole face lattice, 

# which is much more information than necessary. 

M = matrix(ZZ, self.n_facets(), self.n_facets(), 0) 

codim = self.ambient_dim()-self.dim() 

 

def set_adjacent(h1, h2): 

if h1 is h2: 

return 

i = h1.index() - codim 

j = h2.index() - codim 

M[i, j] = 1 

M[j, i] = 1 

 

for face in self.faces(self.dim()-2): 

Hrep = face.ambient_Hrepresentation() 

assert(len(Hrep) == codim+2) 

set_adjacent(Hrep[-2], Hrep[-1]) 

return M 

 

def _vertex_adjacency_matrix(self): 

""" 

Compute the vertex adjacency matrix in case it has not been 

computed during initialization. 

 

EXAMPLES:: 

 

sage: p = Polyhedron(vertices=[(0,0),(1,0),(0,1)]) 

sage: p._vertex_adjacency_matrix() 

[0 1 1] 

[1 0 1] 

[1 1 0] 

""" 

# TODO: This implementation computes the whole face lattice, 

# which is much more information than necessary. 

M = matrix(ZZ, self.n_Vrepresentation(), self.n_Vrepresentation(), 0) 

 

def set_adjacent(v1, v2): 

if v1 is v2: 

return 

i = v1.index() 

j = v2.index() 

M[i, j] = 1 

M[j, i] = 1 

 

face_lattice = self.face_lattice() 

for face in face_lattice: 

Vrep = face.ambient_Vrepresentation() 

if len(Vrep) == 2: 

set_adjacent(Vrep[0], Vrep[1]) 

return M 

 

def _delete(self): 

""" 

Delete this polyhedron. 

 

This speeds up creation of new polyhedra by reusing 

objects. After recycling a polyhedron object, it is not in a 

consistent state any more and neither the polyhedron nor its 

H/V-representation objects may be used any more. 

 

.. SEEALSO:: :meth:`~sage.geometry.polyhedron.parent.Polyhedra_base.recycle` 

 

EXAMPLES:: 

 

sage: p = Polyhedron([(0,0),(1,0),(0,1)]) 

sage: p._delete() 

 

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

sage: def loop_polyhedra(): 

....: for i in range(100): 

....: p = Polyhedron(vertices) 

 

sage: timeit('loop_polyhedra()') # not tested - random 

5 loops, best of 3: 79.5 ms per loop 

 

sage: def loop_polyhedra_with_recycling(): 

....: for i in range(100): 

....: p = Polyhedron(vertices) 

....: p._delete() 

 

sage: timeit('loop_polyhedra_with_recycling()') # not tested - random 

5 loops, best of 3: 57.3 ms per loop 

""" 

self.parent().recycle(self) 

 

def base_extend(self, base_ring, backend=None): 

""" 

Return a new polyhedron over a larger base ring. 

 

INPUT: 

 

- ``base_ring`` -- the new base ring. 

 

- ``backend`` -- the new backend, see 

:func:`~sage.geometry.polyhedron.constructor.Polyhedron`. 

 

OUTPUT: 

 

The same polyhedron, but over a larger base ring. 

 

EXAMPLES:: 

 

sage: P = Polyhedron(vertices=[(1,0), (0,1)], rays=[(1,1)], base_ring=ZZ); P 

A 2-dimensional polyhedron in ZZ^2 defined as the convex hull of 2 vertices and 1 ray 

sage: P.base_extend(QQ) 

A 2-dimensional polyhedron in QQ^2 defined as the convex hull of 2 vertices and 1 ray 

sage: P.base_extend(QQ) == P 

True 

""" 

new_parent = self.parent().base_extend(base_ring, backend) 

return new_parent(self) 

 

def _richcmp_(self, other, op): 

""" 

Compare ``self`` and ``other``. 

 

INPUT: 

 

- ``other`` -- a polyhedron 

 

OUTPUT: 

 

If ``other`` is a polyhedron, then the comparison 

operator "less or equal than" means "is contained in", and 

"less than" means "is strictly contained in". 

 

EXAMPLES:: 

 

sage: P = Polyhedron(vertices=[(1,0), (0,1)], rays=[(1,1)]) 

sage: Q = Polyhedron(vertices=[(1,0), (0,1)]) 

sage: P >= Q 

True 

sage: Q <= P 

True 

sage: P == P 

True 

 

The polytope ``Q`` is strictly contained in ``P``:: 

 

sage: P > Q 

True 

sage: P < Q 

False 

sage: P == Q 

False 

""" 

if self._Vrepresentation is None or other._Vrepresentation is None: 

raise RuntimeError('some V representation is missing') 

# make sure deleted polyhedra are not used in cache 

 

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

return op == op_NE 

 

c0 = self._is_subpolyhedron(other) 

c1 = other._is_subpolyhedron(self) 

if c0 and c1: 

return rich_to_bool(op, 0) 

if c0: 

return rich_to_bool(op, -1) 

else: 

return rich_to_bool(op, 1) 

 

@coerce_binop 

def _is_subpolyhedron(self, other): 

""" 

Test whether ``self`` is a (not necessarily strict) 

sub-polyhedron of ``other``. 

 

INPUT: 

 

- ``other`` -- a :class:`Polyhedron`. 

 

OUTPUT: 

 

Boolean. 

 

EXAMPLES:: 

 

sage: P = Polyhedron(vertices=[(1,0), (0,1)], rays=[(1,1)]) 

sage: Q = Polyhedron(vertices=[(1,0), (0,1)]) 

sage: P._is_subpolyhedron(Q) 

False 

sage: Q._is_subpolyhedron(P) 

True 

""" 

return all(other_H.contains(self_V) 

for other_H in other.Hrepresentation() 

for self_V in self.Vrepresentation()) 

 

@cached_method 

def vertex_facet_graph(self, labels=True): 

r""" 

Return the vertex-facet graph. 

 

This function constructs a directed bipartite graph. 

The nodes of the graph correspond to the vertices of the polyhedron 

and the facets of the polyhedron. There is an directed edge 

from a vertex to a face if and only if the vertex is incident to the face. 

 

INPUT: 

 

- ``labels`` -- boolean (default: ``True``); decide how the nodes 

of the graph are labelled. Either with the original vertices/facets 

of the Polyhedron or with integers. 

 

OUTPUT: 

 

- a bipartite DiGraph. If ``labels`` is ``True``, then the nodes 

of the graph will actually be the vertices and facets of ``self``, 

otherwise they will be integers. 

 

.. SEEALSO:: 

 

:meth:`combinatorial_automorphism_group`, 

:meth:`is_combinatorially_isomorphic`. 

 

EXAMPLES:: 

 

sage: P = polytopes.cube() 

sage: G = P.vertex_facet_graph(); G 

Digraph on 14 vertices 

sage: G.vertices(key = lambda v: str(v)) 

[A vertex at (-1, -1, -1), 

A vertex at (-1, -1, 1), 

A vertex at (-1, 1, -1), 

A vertex at (-1, 1, 1), 

A vertex at (1, -1, -1), 

A vertex at (1, -1, 1), 

A vertex at (1, 1, -1), 

A vertex at (1, 1, 1), 

An inequality (-1, 0, 0) x + 1 >= 0, 

An inequality (0, -1, 0) x + 1 >= 0, 

An inequality (0, 0, -1) x + 1 >= 0, 

An inequality (0, 0, 1) x + 1 >= 0, 

An inequality (0, 1, 0) x + 1 >= 0, 

An inequality (1, 0, 0) x + 1 >= 0] 

sage: G.automorphism_group().is_isomorphic(P.face_lattice().hasse_diagram().automorphism_group()) 

True 

sage: O = polytopes.octahedron(); O 

A 3-dimensional polyhedron in ZZ^3 defined as the convex hull of 6 vertices 

sage: O.vertex_facet_graph() 

Digraph on 14 vertices 

sage: H = O.vertex_facet_graph() 

sage: G.is_isomorphic(H) 

False 

sage: G.reverse_edges(G.edges()) 

sage: G.is_isomorphic(H) 

True 

 

""" 

 

# We construct the edges and remove the columns that have all 1s; 

# those correspond to faces, that contain all vertices (which happens 

# if the polyhedron is not full-dimensional) 

G = DiGraph() 

if labels: 

edges = [[v, f] for f in self.Hrep_generator() 

if any(not(f.is_incident(v)) for v in self.Vrep_generator()) 

for v in self.vertices() if f.is_incident(v)] 

else: 

# here we obtain this incidence information from the incidence matrix 

M = self.incidence_matrix() 

edges = [[i, M.ncols()+j] for i, column in enumerate(M.columns()) 

if any(entry != 1 for entry in column) 

for j in range(M.nrows()) if M[j, i] == 1] 

G.add_edges(edges) 

return G 

 

def plot(self, 

point=None, line=None, polygon=None, # None means unspecified by the user 

wireframe='blue', fill='green', 

projection_direction=None, 

**kwds): 

""" 

Return a graphical representation. 

 

INPUT: 

 

- ``point``, ``line``, ``polygon`` -- Parameters to pass to 

point (0d), line (1d), and polygon (2d) plot commands. 

Allowed values are: 

 

* A Python dictionary to be passed as keywords to the plot 

commands. 

 

* A string or triple of numbers: The color. This is 

equivalent to passing the dictionary ``{'color':...}``. 

 

* ``False``: Switches off the drawing of the corresponding 

graphics object 

 

- ``wireframe``, ``fill`` -- Similar to ``point``, ``line``, 

and ``polygon``, but ``fill`` is used for the graphics 

objects in the dimension of the polytope (or of dimension 2 

for higher dimensional polytopes) and ``wireframe`` is used 

for all lower-dimensional graphics objects 

(default: 'green' for ``fill`` and 'blue' for ``wireframe``) 

 

- ``projection_direction`` -- coordinate list/tuple/iterable 

or ``None`` (default). The direction to use for the 

:meth:`schlegel_projection` of the polytope. If not 

specified, no projection is used in dimensions `< 4` and 

parallel projection is used in dimension `4`. 

 

- ``**kwds`` -- optional keyword parameters that are passed to 

all graphics objects. 

 

OUTPUT: 

 

A (multipart) graphics object. 

 

EXAMPLES:: 

 

sage: square = polytopes.hypercube(2) 

sage: point = Polyhedron([[1,1]]) 

sage: line = Polyhedron([[1,1],[2,1]]) 

sage: cube = polytopes.hypercube(3) 

sage: hypercube = polytopes.hypercube(4) 

 

By default, the wireframe is rendered in blue and the fill in green:: 

 

sage: square.plot() 

Graphics object consisting of 6 graphics primitives 

sage: point.plot() 

Graphics object consisting of 1 graphics primitive 

sage: line.plot() 

Graphics object consisting of 2 graphics primitives 

sage: cube.plot() 

Graphics3d Object 

sage: hypercube.plot() 

Graphics3d Object 

 

Draw the lines in red and nothing else:: 

 

sage: square.plot(point=False, line='red', polygon=False) 

Graphics object consisting of 4 graphics primitives 

sage: point.plot(point=False, line='red', polygon=False) 

Graphics object consisting of 0 graphics primitives 

sage: line.plot(point=False, line='red', polygon=False) 

Graphics object consisting of 1 graphics primitive 

sage: cube.plot(point=False, line='red', polygon=False) 

Graphics3d Object 

sage: hypercube.plot(point=False, line='red', polygon=False) 

Graphics3d Object 

 

Draw points in red, no lines, and a blue polygon:: 

 

sage: square.plot(point={'color':'red'}, line=False, polygon=(0,0,1)) 

Graphics object consisting of 2 graphics primitives 

sage: point.plot(point={'color':'red'}, line=False, polygon=(0,0,1)) 

Graphics object consisting of 1 graphics primitive 

sage: line.plot(point={'color':'red'}, line=False, polygon=(0,0,1)) 

Graphics object consisting of 1 graphics primitive 

sage: cube.plot(point={'color':'red'}, line=False, polygon=(0,0,1)) 

Graphics3d Object 

sage: hypercube.plot(point={'color':'red'}, line=False, polygon=(0,0,1)) 

Graphics3d Object 

 

If we instead use the ``fill`` and ``wireframe`` options, the 

coloring depends on the dimension of the object:: 

 

sage: square.plot(fill='green', wireframe='red') 

Graphics object consisting of 6 graphics primitives 

sage: point.plot(fill='green', wireframe='red') 

Graphics object consisting of 1 graphics primitive 

sage: line.plot(fill='green', wireframe='red') 

Graphics object consisting of 2 graphics primitives 

sage: cube.plot(fill='green', wireframe='red') 

Graphics3d Object 

sage: hypercube.plot(fill='green', wireframe='red') 

Graphics3d Object 

 

TESTS:: 

 

sage: for p in square.plot(): 

....: print("{} {}".format(p.options()['rgbcolor'], p)) 

blue Point set defined by 4 point(s) 

blue Line defined by 2 points 

blue Line defined by 2 points 

blue Line defined by 2 points 

blue Line defined by 2 points 

green Polygon defined by 4 points 

 

sage: for p in line.plot(): 

....: print("{} {}".format(p.options()['rgbcolor'], p)) 

blue Point set defined by 2 point(s) 

green Line defined by 2 points 

 

sage: for p in point.plot(): 

....: print("{} {}".format(p.options()['rgbcolor'], p)) 

green Point set defined by 1 point(s) 

 

Draw the lines in red and nothing else:: 

 

sage: for p in square.plot(point=False, line='red', polygon=False): 

....: print("{} {}".format(p.options()['rgbcolor'], p)) 

red Line defined by 2 points 

red Line defined by 2 points 

red Line defined by 2 points 

red Line defined by 2 points 

 

Draw vertices in red, no lines, and a blue polygon:: 

 

sage: for p in square.plot(point={'color':'red'}, line=False, polygon=(0,0,1)): 

....: print("{} {}".format(p.options()['rgbcolor'], p)) 

red Point set defined by 4 point(s) 

(0, 0, 1) Polygon defined by 4 points 

 

sage: for p in line.plot(point={'color':'red'}, line=False, polygon=(0,0,1)): 

....: print("{} {}".format(p.options()['rgbcolor'], p)) 

red Point set defined by 2 point(s) 

 

sage: for p in point.plot(point={'color':'red'}, line=False, polygon=(0,0,1)): 

....: print("{} {}".format(p.options()['rgbcolor'], p)) 

red Point set defined by 1 point(s) 

 

Draw in red without wireframe:: 

 

sage: for p in square.plot(wireframe=False, fill="red"): 

....: print("{} {}".format(p.options()['rgbcolor'], p)) 

red Polygon defined by 4 points 

 

sage: for p in line.plot(wireframe=False, fill="red"): 

....: print("{} {}".format(p.options()['rgbcolor'], p)) 

red Line defined by 2 points 

 

sage: for p in point.plot(wireframe=False, fill="red"): 

....: print("{} {}".format(p.options()['rgbcolor'], p)) 

red Point set defined by 1 point(s) 

 

The ``projection_direction`` option:: 

 

sage: line3d = Polyhedron([(-1,-1,-1), (1,1,1)]) 

sage: print(line3d.plot(projection_direction=[2,3,4]).description()) 

Line defined by 2 points: [(-0.00..., 0.126...), (0.131..., -1.93...)] 

Point set defined by 2 point(s): [(-0.00..., 0.126...), (0.131..., -1.93...)] 

 

We try to draw the polytope in 2 or 3 dimensions:: 

 

sage: type(Polyhedron(ieqs=[(1,)]).plot()) 

<class 'sage.plot.graphics.Graphics'> 

sage: type(polytopes.hypercube(1).plot()) 

<class 'sage.plot.graphics.Graphics'> 

sage: type(polytopes.hypercube(2).plot()) 

<class 'sage.plot.graphics.Graphics'> 

sage: type(polytopes.hypercube(3).plot()) 

<class 'sage.plot.plot3d.base.Graphics3dGroup'> 

 

In 4d a projection to 3d is used:: 

 

sage: type(polytopes.hypercube(4).plot()) 

<class 'sage.plot.plot3d.base.Graphics3dGroup'> 

sage: type(polytopes.hypercube(5).plot()) 

Traceback (most recent call last): 

... 

NotImplementedError: plotting of 5-dimensional polyhedra not implemented 

 

If the polyhedron is not full-dimensional, the :meth:`affine_hull` is used if necessary:: 

 

sage: type(Polyhedron([(0,), (1,)]).plot()) 

<class 'sage.plot.graphics.Graphics'> 

sage: type(Polyhedron([(0,0), (1,1)]).plot()) 

<class 'sage.plot.graphics.Graphics'> 

sage: type(Polyhedron([(0,0,0), (1,1,1)]).plot()) 

<class 'sage.plot.plot3d.base.Graphics3dGroup'> 

sage: type(Polyhedron([(0,0,0,0), (1,1,1,1)]).plot()) 

<class 'sage.plot.plot3d.base.Graphics3dGroup'> 

sage: type(Polyhedron([(0,0,0,0,0), (1,1,1,1,1)]).plot()) 

<class 'sage.plot.graphics.Graphics'> 

""" 

def merge_options(*opts): 

merged = dict() 

for i in range(len(opts)): 

opt = opts[i] 

if opt is None: 

continue 

elif opt is False: 

return False 

elif isinstance(opt, (six.string_types, list, tuple)): 

merged['color'] = opt 

else: 

merged.update(opt) 

return merged 

 

d = min(self.dim(), 2) 

opts = [wireframe] * d + [fill] + [False] * (2-d) 

# The point/line/polygon options take precedence over wireframe/fill 

opts = [merge_options(opt1, opt2, kwds) 

for opt1, opt2 in zip(opts, [point, line, polygon])] 

 

def project(polyhedron): 

if projection_direction is not None: 

return polyhedron.schlegel_projection(projection_direction) 

elif polyhedron.ambient_dim() == 4: 

# There is no 4-d screen, we must project down to 3d 

return polyhedron.schlegel_projection() 

else: 

return polyhedron.projection() 

 

projection = project(self) 

try: 

plot_method = projection.plot 

except AttributeError: 

projection = project(self.affine_hull()) 

try: 

plot_method = projection.plot 

except AttributeError: 

raise NotImplementedError('plotting of {0}-dimensional polyhedra not implemented' 

.format(self.ambient_dim())) 

return plot_method(*opts) 

 

def show(self, **kwds): 

""" 

Display graphics immediately 

 

This method attempts to display the graphics immediately, 

without waiting for the currently running code (if any) to 

return to the command line. Be careful, calling it from within 

a loop will potentially launch a large number of external 

viewer programs. 

 

INPUT: 

 

- ``kwds`` -- optional keyword arguments. See :meth:`plot` for 

the description of available options. 

 

OUTPUT: 

 

This method does not return anything. Use :meth:`plot` if you 

want to generate a graphics object that can be saved or 

further transformed. 

 

EXAMPLES:: 

 

sage: square = polytopes.hypercube(2) 

sage: square.show(point='red') 

""" 

self.plot(**kwds).show() 

 

def _repr_(self): 

""" 

Return a description of the polyhedron. 

 

EXAMPLES:: 

 

sage: poly_test = Polyhedron(vertices = [[1,2,3,4],[2,1,3,4],[4,3,2,1]]) 

sage: poly_test._repr_() 

'A 2-dimensional polyhedron in ZZ^4 defined as the convex hull of 3 vertices' 

sage: grammar_test = Polyhedron(vertices = [[1,1,1,1,1,1]]) 

sage: grammar_test._repr_() 

'A 0-dimensional polyhedron in ZZ^6 defined as the convex hull of 1 vertex' 

""" 

desc = '' 

if self.n_vertices() == 0: 

desc += 'The empty polyhedron' 

else: 

desc += 'A ' + repr(self.dim()) + '-dimensional polyhedron' 

desc += ' in ' 

desc += self.parent()._repr_ambient_module() 

 

if self.n_vertices() > 0: 

desc += ' defined as the convex hull of ' 

desc += repr(self.n_vertices()) 

if self.n_vertices() == 1: desc += ' vertex' 

else: desc += ' vertices' 

 

if self.n_rays() > 0: 

if self.n_lines() > 0: desc += ", " 

else: desc += " and " 

desc += repr(self.n_rays()) 

if self.n_rays() == 1: desc += ' ray' 

else: desc += ' rays' 

 

if self.n_lines() > 0: 

if self.n_rays() > 0: desc += ", " 

else: desc += " and " 

desc += repr(self.n_lines()) 

if self.n_lines() == 1: desc += ' line' 

else: desc += ' lines' 

 

return desc 

 

def _rich_repr_(self, display_manager, **kwds): 

r""" 

Rich Output Magic Method 

 

See :mod:`sage.repl.rich_output` for details. 

 

EXAMPLES:: 

 

sage: from sage.repl.rich_output import get_display_manager 

sage: dm = get_display_manager() 

sage: polytopes.hypercube(2)._rich_repr_(dm) 

OutputPlainText container 

 

The ``supplemental_plot`` preference lets us control whether 

this object is shown as text or picture+text:: 

 

sage: dm.preferences.supplemental_plot 

'never' 

sage: del dm.preferences.supplemental_plot 

sage: polytopes.hypercube(3) 

A 3-dimensional polyhedron in ZZ^3 defined as the convex hull of 8 vertices (use the .plot() method to plot) 

sage: dm.preferences.supplemental_plot = 'never' 

""" 

prefs = display_manager.preferences 

is_small = (self.ambient_dim() <= 2) 

can_plot = (prefs.supplemental_plot != 'never') 

plot_graph = can_plot and (prefs.supplemental_plot == 'always' or is_small) 

# Under certain circumstances we display the plot as graphics 

if plot_graph: 

plot_kwds = dict(kwds) 

plot_kwds.setdefault('title', repr(self)) 

output = self.plot(**plot_kwds)._rich_repr_(display_manager) 

if output is not None: 

return output 

# create text for non-graphical output 

if can_plot: 

text = '{0} (use the .plot() method to plot)'.format(repr(self)) 

else: 

text = repr(self) 

# latex() produces huge tikz environment, override 

tp = display_manager.types 

if (prefs.text == 'latex' and tp.OutputLatex in display_manager.supported_output()): 

return tp.OutputLatex(r'\text{{{0}}}'.format(text)) 

return tp.OutputPlainText(text) 

 

def cdd_Hrepresentation(self): 

r""" 

Write the inequalities/equations data of the polyhedron in 

cdd's H-representation format. 

 

.. SEEALSO:: 

 

:meth:`write_cdd_Hrepresentation` -- export the polyhedron as a 

H-representation to a file. 

 

OUTPUT: a string 

 

EXAMPLES:: 

 

sage: p = polytopes.hypercube(2) 

sage: print(p.cdd_Hrepresentation()) 

H-representation 

begin 

4 3 rational 

1 1 0 

1 0 1 

1 -1 0 

1 0 -1 

end 

 

sage: triangle = Polyhedron(vertices = [[1,0],[0,1],[1,1]],base_ring=AA) 

sage: triangle.base_ring() 

Algebraic Real Field 

sage: triangle.cdd_Hrepresentation() 

Traceback (most recent call last): 

... 

TypeError: The base ring must be ZZ, QQ, or RDF 

""" 

from .cdd_file_format import cdd_Hrepresentation 

try: 

cdd_type = self._cdd_type 

except AttributeError: 

if self.base_ring() is ZZ or self.base_ring() is QQ: 

cdd_type = 'rational' 

elif self.base_ring() is RDF: 

cdd_type = 'real' 

else: 

raise TypeError('The base ring must be ZZ, QQ, or RDF') 

return cdd_Hrepresentation(cdd_type, 

list(self.inequality_generator()), 

list(self.equation_generator())) 

 

def write_cdd_Hrepresentation(self, filename): 

r""" 

Export the polyhedron as a H-representation to a file. 

 

INPUT: 

 

- ``filename`` -- the output file. 

 

.. SEEALSO:: 

 

:meth:`cdd_Hrepresentation` -- return the H-representation of the 

polyhedron as a string. 

 

EXAMPLES:: 

 

sage: from sage.misc.temporary_file import tmp_filename 

sage: filename = tmp_filename(ext='.ext') 

sage: polytopes.cube().write_cdd_Hrepresentation(filename) 

""" 

with open(filename, 'w') as f: 

f.write(self.cdd_Hrepresentation()) 

 

def cdd_Vrepresentation(self): 

r""" 

Write the vertices/rays/lines data of the polyhedron in cdd's 

V-representation format. 

 

.. SEEALSO:: 

 

:meth:`write_cdd_Vrepresentation` -- export the polyhedron as a 

V-representation to a file. 

 

OUTPUT: a string 

 

EXAMPLES:: 

 

sage: q = Polyhedron(vertices = [[1,1],[0,0],[1,0],[0,1]]) 

sage: print(q.cdd_Vrepresentation()) 

V-representation 

begin 

4 3 rational 

1 0 0 

1 0 1 

1 1 0 

1 1 1 

end 

""" 

from .cdd_file_format import cdd_Vrepresentation 

try: 

cdd_type = self._cdd_type 

except AttributeError: 

if self.base_ring() is ZZ or self.base_ring() is QQ: 

cdd_type = 'rational' 

elif self.base_ring() is RDF: 

cdd_type = 'real' 

else: 

raise TypeError('The base ring must be ZZ, QQ, or RDF') 

return cdd_Vrepresentation(cdd_type, 

list(self.vertex_generator()), 

list(self.ray_generator()), 

list(self.line_generator())) 

 

def write_cdd_Vrepresentation(self, filename): 

r""" 

Export the polyhedron as a V-representation to a file. 

 

INPUT: 

 

- ``filename`` -- the output file. 

 

.. SEEALSO:: 

 

:meth:`cdd_Vrepresentation` -- return the V-representation of the 

polyhedron as a string. 

 

EXAMPLES:: 

 

sage: from sage.misc.temporary_file import tmp_filename 

sage: filename = tmp_filename(ext='.ext') 

sage: polytopes.cube().write_cdd_Vrepresentation(filename) 

""" 

with open(filename, 'w') as f: 

f.write(self.cdd_Vrepresentation()) 

 

@cached_method 

def n_equations(self): 

""" 

Return the number of equations. The representation will 

always be minimal, so the number of equations is the 

codimension of the polyhedron in the ambient space. 

 

EXAMPLES:: 

 

sage: p = Polyhedron(vertices = [[1,0,0],[0,1,0],[0,0,1]]) 

sage: p.n_equations() 

1 

""" 

return len(self.equations()) 

 

@cached_method 

def n_inequalities(self): 

""" 

Return the number of inequalities. The representation will 

always be minimal, so the number of inequalities is the 

number of facets of the polyhedron in the ambient space. 

 

EXAMPLES:: 

 

sage: p = Polyhedron(vertices = [[1,0,0],[0,1,0],[0,0,1]]) 

sage: p.n_inequalities() 

3 

 

sage: p = Polyhedron(vertices = [[t,t^2,t^3] for t in range(6)]) 

sage: p.n_facets() 

8 

""" 

return len(self.inequalities()) 

 

n_facets = n_inequalities 

 

@cached_method 

def n_vertices(self): 

""" 

Return the number of vertices. The representation will 

always be minimal. 

 

EXAMPLES:: 

 

sage: p = Polyhedron(vertices = [[1,0],[0,1],[1,1]], rays=[[1,1]]) 

sage: p.n_vertices() 

2 

""" 

return len(self.vertices()) 

 

@cached_method 

def n_rays(self): 

""" 

Return the number of rays. The representation will 

always be minimal. 

 

EXAMPLES:: 

 

sage: p = Polyhedron(vertices = [[1,0],[0,1]], rays=[[1,1]]) 

sage: p.n_rays() 

1 

""" 

return len(self.rays()) 

 

@cached_method 

def n_lines(self): 

""" 

Return the number of lines. The representation will 

always be minimal. 

 

EXAMPLES:: 

 

sage: p = Polyhedron(vertices = [[0,0]], rays=[[0,1],[0,-1]]) 

sage: p.n_lines() 

1 

""" 

return len(self.lines()) 

 

def to_linear_program(self, solver=None, return_variable=False, base_ring=None): 

r""" 

Return a linear optimization problem over the polyhedron in the form of 

a :class:`MixedIntegerLinearProgram`. 

 

INPUT: 

 

- ``solver`` -- select a solver (MIP backend). See the documentation 

of for :class:`MixedIntegerLinearProgram`. Set to ``None`` by default. 

 

- ``return_variable`` -- (default: ``False``) If ``True``, return a tuple 

``(p, x)``, where ``p`` is the :class:`MixedIntegerLinearProgram` object 

and ``x`` is the vector-valued MIP variable in this problem, indexed 

from 0. If ``False``, only return ``p``. 

 

- ``base_ring`` -- select a field over which the linear program should be 

set up. Use ``RDF`` to request a fast inexact (floating point) solver 

even if ``self`` is exact. 

 

Note that the :class:`MixedIntegerLinearProgram` object will have the 

null function as an objective to be maximized. 

 

.. SEEALSO:: 

 

:meth:`~MixedIntegerLinearProgram.polyhedron` -- return the 

polyhedron associated with a :class:`MixedIntegerLinearProgram` 

object. 

 

EXAMPLES: 

 

Exact rational linear program:: 

 

sage: p = polytopes.cube() 

sage: p.to_linear_program() 

Linear Program (no objective, 3 variables, 6 constraints) 

sage: lp, x = p.to_linear_program(return_variable=True) 

sage: lp.set_objective(2*x[0] + 1*x[1] + 39*x[2]) 

sage: lp.solve() 

42 

sage: lp.get_values(x[0], x[1], x[2]) 

[1, 1, 1] 

 

Floating-point linear program:: 

 

sage: lp, x = p.to_linear_program(return_variable=True, base_ring=RDF) 

sage: lp.set_objective(2*x[0] + 1*x[1] + 39*x[2]) 

sage: lp.solve() 

42.0 

 

Irrational algebraic linear program over an embedded number field:: 

 

sage: p=polytopes.icosahedron() 

sage: lp, x = p.to_linear_program(return_variable=True) 

sage: lp.set_objective(x[0] + x[1] + x[2]) 

sage: lp.solve() 

1/4*sqrt5 + 3/4 

 

Same example with floating point:: 

 

sage: lp, x = p.to_linear_program(return_variable=True, base_ring=RDF) 

sage: lp.set_objective(x[0] + x[1] + x[2]) 

sage: lp.solve() # tol 1e-5 

1.3090169943749475 

 

Same example with a specific floating point solver:: 

 

sage: lp, x = p.to_linear_program(return_variable=True, solver='GLPK') 

sage: lp.set_objective(x[0] + x[1] + x[2]) 

sage: lp.solve() # tol 1e-8 

1.3090169943749475 

 

Irrational algebraic linear program over `AA`:: 

 

sage: p=polytopes.icosahedron(base_ring=AA) 

sage: lp, x = p.to_linear_program(return_variable=True) 

sage: lp.set_objective(x[0] + x[1] + x[2]) 

sage: lp.solve() # long time 

1.309016994374948? 

 

TESTS:: 

 

sage: p=polytopes.flow_polytope(digraphs.DeBruijn(3,2)); p 

A 19-dimensional polyhedron in QQ^27 defined as the convex hull of 1 vertex and 148 rays 

sage: p.to_linear_program().polyhedron() == p 

True 

sage: p=polytopes.icosahedron() 

sage: p.to_linear_program(solver='PPL') 

Traceback (most recent call last): 

... 

TypeError: The PPL backend only supports rational data. 

 

Test that equations are handled correctly (:trac:`24154`):: 

 

sage: p = Polyhedron(vertices=[[19]]) 

sage: lp, x = p.to_linear_program(return_variable=True) 

sage: lp.set_objective(x[0]) 

sage: lp.solve() 

19 

""" 

if base_ring is None: 

base_ring = self.base_ring() 

base_ring = base_ring.fraction_field() 

from sage.numerical.mip import MixedIntegerLinearProgram 

p = MixedIntegerLinearProgram(solver=solver, base_ring=base_ring) 

x = p.new_variable(real=True, nonnegative=False) 

 

for ineqn in self.inequalities_list(): 

b = -ineqn.pop(0) 

p.add_constraint(p.sum([x[i]*ineqn[i] for i in range(len(ineqn))]) >= b) 

 

for eqn in self.equations_list(): 

b = -eqn.pop(0) 

p.add_constraint(p.sum([x[i]*eqn[i] for i in range(len(eqn))]) == b) 

 

if return_variable: 

return p, x 

else: 

return p 

 

def Hrepresentation(self, index=None): 

""" 

Return the objects of the H-representation. Each entry is 

either an inequality or a equation. 

 

INPUT: 

 

- ``index`` -- either an integer or ``None``. 

 

OUTPUT: 

 

The optional argument is an index running from ``0`` to 

``self.n_Hrepresentation()-1``. If present, the 

H-representation object at the given index will be 

returned. Without an argument, returns the list of all 

H-representation objects. 

 

EXAMPLES:: 

 

sage: p = polytopes.hypercube(3) 

sage: p.Hrepresentation(0) 

An inequality (0, 0, -1) x + 1 >= 0 

sage: p.Hrepresentation(0) == p.Hrepresentation() [0] 

True 

""" 

if index is None: 

return self._Hrepresentation 

else: 

return self._Hrepresentation[index] 

 

def repr_pretty_Hrepresentation(self, separator=', ', **kwds): 

r""" 

Return a pretty representation of the Hrepresentation of this 

polyhedron. 

 

INPUT: 

 

- ``separator`` -- a string 

 

Keyword parameters of 

:meth:`~sage.geometry.polyhedron.representation.Hrepresentation.repr_pretty` 

are passed on: 

 

- ``prefix`` -- a string 

 

- ``indices`` -- a tuple or other iterable 

 

- ``latex`` -- a boolean 

 

OUTPUT: 

 

A string. 

 

EXAMPLES:: 

 

sage: P = Polyhedron(ieqs=[(0, 1, 0, 0), (1, 2, 1, 0)], 

....: eqns=[(1, -1, -1, 1)]) 

sage: P.repr_pretty_Hrepresentation() 

'x0 + x1 == x2 + 1, x0 >= 0, 2*x0 + x1 + 1 >= 0' 

""" 

return separator.join(h.repr_pretty(**kwds) 

for h in self.Hrepresentation()) 

 

def Hrep_generator(self): 

""" 

Return an iterator over the objects of the H-representation 

(inequalities or equations). 

 

EXAMPLES:: 

 

sage: p = polytopes.hypercube(3) 

sage: next(p.Hrep_generator()) 

An inequality (0, 0, -1) x + 1 >= 0 

""" 

for H in self.Hrepresentation(): 

yield H 

 

@cached_method 

def n_Hrepresentation(self): 

""" 

Return the number of objects that make up the 

H-representation of the polyhedron. 

 

OUTPUT: 

 

Integer. 

 

EXAMPLES:: 

 

sage: p = polytopes.cross_polytope(4) 

sage: p.n_Hrepresentation() 

16 

sage: p.n_Hrepresentation() == p.n_inequalities() + p.n_equations() 

True 

""" 

return len(self.Hrepresentation()) 

 

def Vrepresentation(self, index=None): 

""" 

Return the objects of the V-representation. Each entry is 

either a vertex, a ray, or a line. 

 

See :mod:`sage.geometry.polyhedron.constructor` for a 

definition of vertex/ray/line. 

 

INPUT: 

 

- ``index`` -- either an integer or ``None``. 

 

OUTPUT: 

 

The optional argument is an index running from ``0`` to 

``self.n_Vrepresentation()-1``. If present, the 

V-representation object at the given index will be 

returned. Without an argument, returns the list of all 

V-representation objects. 

 

EXAMPLES:: 

 

sage: p = polytopes.simplex(4, project=True) 

sage: p.Vrepresentation(0) 

A vertex at (0.7071067812, 0.4082482905, 0.2886751346, 0.2236067977) 

sage: p.Vrepresentation(0) == p.Vrepresentation() [0] 

True 

""" 

if index is None: 

return self._Vrepresentation 

else: 

return self._Vrepresentation[index] 

 

@cached_method 

def n_Vrepresentation(self): 

""" 

Return the number of objects that make up the 

V-representation of the polyhedron. 

 

OUTPUT: 

 

Integer. 

 

EXAMPLES:: 

 

sage: p = polytopes.simplex(4) 

sage: p.n_Vrepresentation() 

5 

sage: p.n_Vrepresentation() == p.n_vertices() + p.n_rays() + p.n_lines() 

True 

""" 

return len(self.Vrepresentation()) 

 

def Vrep_generator(self): 

""" 

Returns an iterator over the objects of the V-representation 

(vertices, rays, and lines). 

 

EXAMPLES:: 

 

sage: p = polytopes.cyclic_polytope(3,4) 

sage: vg = p.Vrep_generator() 

sage: next(vg) 

A vertex at (0, 0, 0) 

sage: next(vg) 

A vertex at (1, 1, 1) 

""" 

for V in self.Vrepresentation(): 

yield V 

 

def inequality_generator(self): 

""" 

Return a generator for the defining inequalities of the 

polyhedron. 

 

OUTPUT: 

 

A generator of the inequality Hrepresentation objects. 

 

EXAMPLES:: 

 

sage: triangle = Polyhedron(vertices=[[1,0],[0,1],[1,1]]) 

sage: for v in triangle.inequality_generator(): print(v) 

An inequality (1, 1) x - 1 >= 0 

An inequality (0, -1) x + 1 >= 0 

An inequality (-1, 0) x + 1 >= 0 

sage: [ v for v in triangle.inequality_generator() ] 

[An inequality (1, 1) x - 1 >= 0, 

An inequality (0, -1) x + 1 >= 0, 

An inequality (-1, 0) x + 1 >= 0] 

sage: [ [v.A(), v.b()] for v in triangle.inequality_generator() ] 

[[(1, 1), -1], [(0, -1), 1], [(-1, 0), 1]] 

""" 

for H in self.Hrepresentation(): 

if H.is_inequality(): 

yield H 

 

@cached_method 

def inequalities(self): 

""" 

Return all inequalities. 

 

OUTPUT: 

 

A tuple of inequalities. 

 

EXAMPLES:: 

 

sage: p = Polyhedron(vertices = [[0,0,0],[0,0,1],[0,1,0],[1,0,0],[2,2,2]]) 

sage: p.inequalities()[0:3] 

(An inequality (1, 0, 0) x + 0 >= 0, 

An inequality (0, 1, 0) x + 0 >= 0, 

An inequality (0, 0, 1) x + 0 >= 0) 

sage: p3 = Polyhedron(vertices = Permutations([1,2,3,4])) 

sage: ieqs = p3.inequalities() 

sage: ieqs[0] 

An inequality (0, 1, 1, 1) x - 6 >= 0 

sage: list(_) 

[-6, 0, 1, 1, 1] 

""" 

return tuple(self.inequality_generator()) 

 

def inequalities_list(self): 

""" 

Return a list of inequalities as coefficient lists. 

 

.. NOTE:: 

 

It is recommended to use :meth:`inequalities` or 

:meth:`inequality_generator` instead to iterate over the 

list of :class:`Inequality` objects. 

 

EXAMPLES:: 

 

sage: p = Polyhedron(vertices = [[0,0,0],[0,0,1],[0,1,0],[1,0,0],[2,2,2]]) 

sage: p.inequalities_list()[0:3] 

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

sage: p3 = Polyhedron(vertices = Permutations([1,2,3,4])) 

sage: ieqs = p3.inequalities_list() 

sage: ieqs[0] 

[-6, 0, 1, 1, 1] 

sage: ieqs[-1] 

[-3, 0, 1, 0, 1] 

sage: ieqs == [list(x) for x in p3.inequality_generator()] 

True 

""" 

return [list(x) for x in self.inequality_generator()] 

 

def equation_generator(self): 

""" 

Return a generator for the linear equations satisfied by the 

polyhedron. 

 

EXAMPLES:: 

 

sage: p = polytopes.regular_polygon(8,base_ring=RDF) 

sage: p3 = Polyhedron(vertices = [x+[0] for x in p.vertices()], base_ring=RDF) 

sage: next(p3.equation_generator()) 

An equation (0.0, 0.0, 1.0) x + 0.0 == 0 

""" 

for H in self.Hrepresentation(): 

if H.is_equation(): 

yield H 

 

@cached_method 

def equations(self): 

""" 

Return all linear constraints of the polyhedron. 

 

OUTPUT: 

 

A tuple of equations. 

 

EXAMPLES:: 

 

sage: test_p = Polyhedron(vertices = [[1,2,3,4],[2,1,3,4],[4,3,2,1],[3,4,1,2]]) 

sage: test_p.equations() 

(An equation (1, 1, 1, 1) x - 10 == 0,) 

""" 

return tuple(self.equation_generator()) 

 

def equations_list(self): 

""" 

Return the linear constraints of the polyhedron. As with 

inequalities, each constraint is given as [b -a1 -a2 ... an] 

where for variables x1, x2,..., xn, the polyhedron satisfies 

the equation b = a1*x1 + a2*x2 + ... + an*xn. 

 

.. NOTE:: 

 

It is recommended to use :meth:`equations` or 

:meth:`equation_generator()` instead to iterate over the 

list of 

:class:`~sage.geometry.polyhedron.representation.Equation` 

objects. 

 

EXAMPLES:: 

 

sage: test_p = Polyhedron(vertices = [[1,2,3,4],[2,1,3,4],[4,3,2,1],[3,4,1,2]]) 

sage: test_p.equations_list() 

[[-10, 1, 1, 1, 1]] 

""" 

return [list(eq) for eq in self.equation_generator()] 

 

def vertices_list(self): 

""" 

Return a list of vertices of the polyhedron. 

 

.. NOTE:: 

 

It is recommended to use :meth:`vertex_generator` instead to 

iterate over the list of :class:`Vertex` objects. 

 

EXAMPLES:: 

 

sage: triangle = Polyhedron(vertices=[[1,0],[0,1],[1,1]]) 

sage: triangle.vertices_list() 

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

sage: a_simplex = Polyhedron(ieqs = [ 

....: [0,1,0,0,0],[0,0,1,0,0],[0,0,0,1,0],[0,0,0,0,1] 

....: ], eqns = [[1,-1,-1,-1,-1]]) 

sage: a_simplex.vertices_list() 

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

sage: a_simplex.vertices_list() == [list(v) for v in a_simplex.vertex_generator()] 

True 

""" 

return [list(x) for x in self.vertex_generator()] 

 

def vertex_generator(self): 

""" 

Return a generator for the vertices of the polyhedron. 

 

EXAMPLES:: 

 

sage: triangle = Polyhedron(vertices=[[1,0],[0,1],[1,1]]) 

sage: for v in triangle.vertex_generator(): print(v) 

A vertex at (0, 1) 

A vertex at (1, 0) 

A vertex at (1, 1) 

sage: v_gen = triangle.vertex_generator() 

sage: next(v_gen) # the first vertex 

A vertex at (0, 1) 

sage: next(v_gen) # the second vertex 

A vertex at (1, 0) 

sage: next(v_gen) # the third vertex 

A vertex at (1, 1) 

sage: try: next(v_gen) # there are only three vertices 

....: except StopIteration: print("STOP") 

STOP 

sage: type(v_gen) 

<... 'generator'> 

sage: [ v for v in triangle.vertex_generator() ] 

[A vertex at (0, 1), A vertex at (1, 0), A vertex at (1, 1)] 

""" 

for V in self.Vrepresentation(): 

if V.is_vertex(): 

yield V 

 

@cached_method 

def vertices(self): 

""" 

Return all vertices of the polyhedron. 

 

OUTPUT: 

 

A tuple of vertices. 

 

EXAMPLES:: 

 

sage: triangle = Polyhedron(vertices=[[1,0],[0,1],[1,1]]) 

sage: triangle.vertices() 

(A vertex at (0, 1), A vertex at (1, 0), A vertex at (1, 1)) 

sage: a_simplex = Polyhedron(ieqs = [ 

....: [0,1,0,0,0],[0,0,1,0,0],[0,0,0,1,0],[0,0,0,0,1] 

....: ], eqns = [[1,-1,-1,-1,-1]]) 

sage: a_simplex.vertices() 

(A vertex at (1, 0, 0, 0), A vertex at (0, 1, 0, 0), 

A vertex at (0, 0, 1, 0), A vertex at (0, 0, 0, 1)) 

""" 

return tuple(self.vertex_generator()) 

 

@cached_method 

def vertices_matrix(self, base_ring=None): 

""" 

Return the coordinates of the vertices as the columns of a matrix. 

 

INPUT: 

 

- ``base_ring`` -- A ring or ``None`` (default). The base ring 

of the returned matrix. If not specified, the base ring of 

the polyhedron is used. 

 

OUTPUT: 

 

A matrix over ``base_ring`` whose columns are the coordinates 

of the vertices. A ``TypeError`` is raised if the coordinates 

cannot be converted to ``base_ring``. 

 

EXAMPLES:: 

 

sage: triangle = Polyhedron(vertices=[[1,0],[0,1],[1,1]]) 

sage: triangle.vertices_matrix() 

[0 1 1] 

[1 0 1] 

sage: (triangle/2).vertices_matrix() 

[ 0 1/2 1/2] 

[1/2 0 1/2] 

sage: (triangle/2).vertices_matrix(ZZ) 

Traceback (most recent call last): 

... 

TypeError: no conversion of this rational to integer 

""" 

if base_ring is None: 

base_ring = self.base_ring() 

m = matrix(base_ring, self.ambient_dim(), self.n_vertices()) 

for i, v in enumerate(self.vertices()): 

for j in range(self.ambient_dim()): 

m[j, i] = v[j] 

return m 

 

def ray_generator(self): 

""" 

Return a generator for the rays of the polyhedron. 

 

EXAMPLES:: 

 

sage: pi = Polyhedron(ieqs = [[1,1,0],[1,0,1]]) 

sage: pir = pi.ray_generator() 

sage: [x.vector() for x in pir] 

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

""" 

for V in self.Vrepresentation(): 

if V.is_ray(): 

yield V 

 

@cached_method 

def rays(self): 

""" 

Return a list of rays of the polyhedron. 

 

OUTPUT: 

 

A tuple of rays. 

 

EXAMPLES:: 

 

sage: p = Polyhedron(ieqs = [[0,0,0,1],[0,0,1,0],[1,1,0,0]]) 

sage: p.rays() 

(A ray in the direction (1, 0, 0), 

A ray in the direction (0, 1, 0), 

A ray in the direction (0, 0, 1)) 

""" 

return tuple(self.ray_generator()) 

 

def rays_list(self): 

""" 

Return a list of rays as coefficient lists. 

 

.. NOTE:: 

 

It is recommended to use :meth:`rays` or 

:meth:`ray_generator` instead to iterate over the list of 

:class:`Ray` objects. 

 

OUTPUT: 

 

A list of rays as lists of coordinates. 

 

EXAMPLES:: 

 

sage: p = Polyhedron(ieqs = [[0,0,0,1],[0,0,1,0],[1,1,0,0]]) 

sage: p.rays_list() 

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

sage: p.rays_list() == [list(r) for r in p.ray_generator()] 

True 

""" 

return [list(x) for x in self.ray_generator()] 

 

def line_generator(self): 

""" 

Return a generator for the lines of the polyhedron. 

 

EXAMPLES:: 

 

sage: pr = Polyhedron(rays = [[1,0],[-1,0],[0,1]], vertices = [[-1,-1]]) 

sage: next(pr.line_generator()).vector() 

(1, 0) 

""" 

for V in self.Vrepresentation(): 

if V.is_line(): 

yield V 

 

@cached_method 

def lines(self): 

""" 

Return all lines of the polyhedron. 

 

OUTPUT: 

 

A tuple of lines. 

 

EXAMPLES:: 

 

sage: p = Polyhedron(rays = [[1,0],[-1,0],[0,1],[1,1]], vertices = [[-2,-2],[2,3]]) 

sage: p.lines() 

(A line in the direction (1, 0),) 

""" 

return tuple(self.line_generator()) 

 

def lines_list(self): 

""" 

Return a list of lines of the polyhedron. The line data is given 

as a list of coordinates rather than as a Hrepresentation object. 

 

.. NOTE:: 

 

It is recommended to use :meth:`line_generator` instead to 

iterate over the list of :class:`Line` objects. 

 

EXAMPLES:: 

 

sage: p = Polyhedron(rays = [[1,0],[-1,0],[0,1],[1,1]], vertices = [[-2,-2],[2,3]]) 

sage: p.lines_list() 

[[1, 0]] 

sage: p.lines_list() == [list(x) for x in p.line_generator()] 

True 

""" 

return [list(x) for x in self.line_generator()] 

 

def bounded_edges(self): 

""" 

Return the bounded edges (excluding rays and lines). 

 

OUTPUT: 

 

A generator for pairs of vertices, one pair per edge. 

 

EXAMPLES:: 

 

sage: p = Polyhedron(vertices=[[1,0],[0,1]], rays=[[1,0],[0,1]]) 

sage: [ e for e in p.bounded_edges() ] 

[(A vertex at (0, 1), A vertex at (1, 0))] 

sage: for e in p.bounded_edges(): print(e) 

(A vertex at (0, 1), A vertex at (1, 0)) 

""" 

obj = self.Vrepresentation() 

for i in range(len(obj)): 

if not obj[i].is_vertex(): continue 

for j in range(i+1, len(obj)): 

if not obj[j].is_vertex(): continue 

if self.vertex_adjacency_matrix()[i, j] == 0: continue 

yield (obj[i], obj[j]) 

 

def Vrepresentation_space(self): 

r""" 

Return the ambient vector space. 

 

OUTPUT: 

 

A free module over the base ring of dimension :meth:`ambient_dim`. 

 

EXAMPLES:: 

 

sage: poly_test = Polyhedron(vertices = [[1,0,0,0],[0,1,0,0]]) 

sage: poly_test.Vrepresentation_space() 

Ambient free module of rank 4 over the principal ideal domain Integer Ring 

sage: poly_test.ambient_space() is poly_test.Vrepresentation_space() 

True 

""" 

return self.parent().Vrepresentation_space() 

 

ambient_space = Vrepresentation_space 

 

def Hrepresentation_space(self): 

r""" 

Return the linear space containing the H-representation vectors. 

 

OUTPUT: 

 

A free module over the base ring of dimension :meth:`ambient_dim` + 1. 

 

EXAMPLES:: 

 

sage: poly_test = Polyhedron(vertices = [[1,0,0,0],[0,1,0,0]]) 

sage: poly_test.Hrepresentation_space() 

Ambient free module of rank 5 over the principal ideal domain Integer Ring 

""" 

return self.parent().Hrepresentation_space() 

 

def ambient_dim(self): 

r""" 

Return the dimension of the ambient space. 

 

EXAMPLES:: 

 

sage: poly_test = Polyhedron(vertices = [[1,0,0,0],[0,1,0,0]]) 

sage: poly_test.ambient_dim() 

4 

""" 

return self.parent().ambient_dim() 

 

def dim(self): 

""" 

Return the dimension of the polyhedron. 

 

OUTPUT: 

 

-1 if the polyhedron is empty, otherwise a non-negative integer. 

 

EXAMPLES:: 

 

sage: simplex = Polyhedron(vertices = [[1,0,0,0],[0,0,0,1],[0,1,0,0],[0,0,1,0]]) 

sage: simplex.dim() 

3 

sage: simplex.ambient_dim() 

4 

 

The empty set is a special case (:trac:`12193`):: 

 

sage: P1=Polyhedron(vertices=[[1,0,0],[0,1,0],[0,0,1]]) 

sage: P2=Polyhedron(vertices=[[2,0,0],[0,2,0],[0,0,2]]) 

sage: P12 = P1.intersection(P2) 

sage: P12 

The empty polyhedron in ZZ^3 

sage: P12.dim() 

-1 

""" 

if self.n_Vrepresentation() == 0: 

return -1 # the empty set 

else: 

return self.ambient_dim() - self.n_equations() 

 

dimension = dim 

 

def is_empty(self): 

""" 

Test whether the polyhedron is the empty polyhedron 

 

OUTPUT: 

 

Boolean. 

 

EXAMPLES:: 

 

sage: P = Polyhedron(vertices=[[1,0,0],[0,1,0],[0,0,1]]); P 

A 2-dimensional polyhedron in ZZ^3 defined as the convex hull of 3 vertices 

sage: P.is_empty(), P.is_universe() 

(False, False) 

 

sage: Q = Polyhedron(vertices=()); Q 

The empty polyhedron in ZZ^0 

sage: Q.is_empty(), Q.is_universe() 

(True, False) 

 

sage: R = Polyhedron(lines=[(1,0),(0,1)]); R 

A 2-dimensional polyhedron in ZZ^2 defined as the convex hull of 1 vertex and 2 lines 

sage: R.is_empty(), R.is_universe() 

(False, True) 

""" 

return self.n_Vrepresentation() == 0 

 

def is_universe(self): 

""" 

Test whether the polyhedron is the whole ambient space 

 

OUTPUT: 

 

Boolean. 

 

EXAMPLES:: 

 

sage: P = Polyhedron(vertices=[[1,0,0],[0,1,0],[0,0,1]]); P 

A 2-dimensional polyhedron in ZZ^3 defined as the convex hull of 3 vertices 

sage: P.is_empty(), P.is_universe() 

(False, False) 

 

sage: Q = Polyhedron(vertices=()); Q 

The empty polyhedron in ZZ^0 

sage: Q.is_empty(), Q.is_universe() 

(True, False) 

 

sage: R = Polyhedron(lines=[(1,0),(0,1)]); R 

A 2-dimensional polyhedron in ZZ^2 defined as the convex hull of 1 vertex and 2 lines 

sage: R.is_empty(), R.is_universe() 

(False, True) 

""" 

return self.n_Hrepresentation() == 0 

 

@cached_method 

def vertex_adjacency_matrix(self): 

""" 

Return the binary matrix of vertex adjacencies. 

 

EXAMPLES:: 

 

sage: polytopes.simplex(4).vertex_adjacency_matrix() 

[0 1 1 1 1] 

[1 0 1 1 1] 

[1 1 0 1 1] 

[1 1 1 0 1] 

[1 1 1 1 0] 

 

The rows and columns of the vertex adjacency matrix correspond 

to the :meth:`Vrepresentation` objects: vertices, rays, and 

lines. The `(i,j)` matrix entry equals `1` if the `i`-th and 

`j`-th V-representation object are adjacent. 

 

Two vertices are adjacent if they are the endpoints of an 

edge, that is, a one-dimensional face. For unbounded polyhedra 

this clearly needs to be generalized and we define two 

V-representation objects (see 

:mod:`sage.geometry.polyhedron.constructor`) to be adjacent if 

they together generate a one-face. There are three possible 

combinations: 

 

* Two vertices can bound a finite-length edge. 

 

* A vertex and a ray can generate a half-infinite edge 

starting at the vertex and with the direction given by the 

ray. 

 

* A vertex and a line can generate an infinite edge. The 

position of the vertex on the line is arbitrary in this 

case, only its transverse position matters. The direction of 

the edge is given by the line generator. 

 

For example, take the half-plane:: 

 

sage: half_plane = Polyhedron(ieqs=[(0,1,0)]) 

sage: half_plane.Hrepresentation() 

(An inequality (1, 0) x + 0 >= 0,) 

 

Its (non-unique) V-representation consists of a vertex, a ray, 

and a line. The only edge is spanned by the vertex and the 

line generator, so they are adjacent:: 

 

sage: half_plane.Vrepresentation() 

(A line in the direction (0, 1), A ray in the direction (1, 0), A vertex at (0, 0)) 

sage: half_plane.vertex_adjacency_matrix() 

[0 0 1] 

[0 0 0] 

[1 0 0] 

 

In one dimension higher, that is for a half-space in 3 

dimensions, there is no one-dimensional face. Hence nothing is 

adjacent:: 

 

sage: Polyhedron(ieqs=[(0,1,0,0)]).vertex_adjacency_matrix() 

[0 0 0 0] 

[0 0 0 0] 

[0 0 0 0] 

[0 0 0 0] 

 

EXAMPLES: 

 

In a bounded polygon, every vertex has precisely two adjacent ones:: 

 

sage: P = Polyhedron(vertices=[(0, 1), (1, 0), (3, 0), (4, 1)]) 

sage: for v in P.Vrep_generator(): 

....: print("{} {}".format(P.adjacency_matrix().row(v.index()), v)) 

(0, 1, 0, 1) A vertex at (0, 1) 

(1, 0, 1, 0) A vertex at (1, 0) 

(0, 1, 0, 1) A vertex at (3, 0) 

(1, 0, 1, 0) A vertex at (4, 1) 

 

If the V-representation of the polygon contains vertices and 

one ray, then each V-representation object is adjacent to two 

V-representation objects:: 

 

sage: P = Polyhedron(vertices=[(0, 1), (1, 0), (3, 0), (4, 1)], 

....: rays=[(0,1)]) 

sage: for v in P.Vrep_generator(): 

....: print("{} {}".format(P.adjacency_matrix().row(v.index()), v)) 

(0, 1, 0, 0, 1) A ray in the direction (0, 1) 

(1, 0, 1, 0, 0) A vertex at (0, 1) 

(0, 1, 0, 1, 0) A vertex at (1, 0) 

(0, 0, 1, 0, 1) A vertex at (3, 0) 

(1, 0, 0, 1, 0) A vertex at (4, 1) 

 

If the V-representation of the polygon contains vertices and 

two distinct rays, then each vertex is adjacent to two 

V-representation objects (which can now be vertices or 

rays). The two rays are not adjacent to each other:: 

 

sage: P = Polyhedron(vertices=[(0, 1), (1, 0), (3, 0), (4, 1)], 

....: rays=[(0,1), (1,1)]) 

sage: for v in P.Vrep_generator(): 

....: print("{} {}".format(P.adjacency_matrix().row(v.index()), v)) 

(0, 1, 0, 0, 0) A ray in the direction (0, 1) 

(1, 0, 1, 0, 0) A vertex at (0, 1) 

(0, 1, 0, 0, 1) A vertex at (1, 0) 

(0, 0, 0, 0, 1) A ray in the direction (1, 1) 

(0, 0, 1, 1, 0) A vertex at (3, 0) 

""" 

return self._vertex_adjacency_matrix() 

 

adjacency_matrix = vertex_adjacency_matrix 

 

@cached_method 

def facet_adjacency_matrix(self): 

""" 

Return the adjacency matrix for the facets and hyperplanes. 

 

EXAMPLES:: 

 

sage: s4 = polytopes.simplex(4, project=True) 

sage: s4.facet_adjacency_matrix() 

[0 1 1 1 1] 

[1 0 1 1 1] 

[1 1 0 1 1] 

[1 1 1 0 1] 

[1 1 1 1 0] 

""" 

return self._facet_adjacency_matrix() 

 

@cached_method 

def incidence_matrix(self): 

""" 

Return the incidence matrix. 

 

.. NOTE:: 

 

The columns correspond to inequalities/equations in the 

order :meth:`Hrepresentation`, the rows correspond to 

vertices/rays/lines in the order 

:meth:`Vrepresentation` 

 

EXAMPLES:: 

 

sage: p = polytopes.cuboctahedron() 

sage: p.incidence_matrix() 

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

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

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

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

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

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

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

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

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

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

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

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

sage: v = p.Vrepresentation(0) 

sage: v 

A vertex at (-1, -1, 0) 

sage: h = p.Hrepresentation(2) 

sage: h 

An inequality (1, 1, -1) x + 2 >= 0 

sage: h.eval(v) # evaluation (1, 1, -1) * (-1/2, -1/2, 0) + 1 

0 

sage: h*v # same as h.eval(v) 

0 

sage: p.incidence_matrix() [0,2] # this entry is (v,h) 

1 

sage: h.contains(v) 

True 

sage: p.incidence_matrix() [2,0] # note: not symmetric 

0 

""" 

incidence_matrix = matrix(ZZ, self.n_Vrepresentation(), 

self.n_Hrepresentation(), 0) 

for V in self.Vrep_generator(): 

for H in self.Hrep_generator(): 

if self._is_zero(H*V): 

incidence_matrix[V.index(), H.index()] = 1 

return incidence_matrix 

 

def base_ring(self): 

""" 

Return the base ring. 

 

OUTPUT: 

 

The ring over which the polyhedron is defined. Must be a 

sub-ring of the reals to define a polyhedron, in particular 

comparison must be defined. Popular choices are 

 

* ``ZZ`` (the ring of integers, lattice polytope), 

 

* ``QQ`` (exact arithmetic using gmp), 

 

* ``RDF`` (double precision floating-point arithmetic), or 

 

* ``AA`` (real algebraic field). 

 

EXAMPLES:: 

 

sage: triangle = Polyhedron(vertices = [[1,0],[0,1],[1,1]]) 

sage: triangle.base_ring() == ZZ 

True 

""" 

return self.parent().base_ring() 

 

def backend(self): 

""" 

Return the backend used. 

 

OUTPUT: 

 

The name of the backend used for computations. It will be one of 

the following backends: 

 

* ``ppl`` the Parma Polyhedra Library 

 

* ``cdd`` CDD 

 

* ``normaliz`` normaliz 

 

* ``polymake`` polymake 

 

* ``field`` a generic Sage implementation 

 

EXAMPLES:: 

 

sage: triangle = Polyhedron(vertices = [[1, 0], [0, 1], [1, 1]]) 

sage: triangle.backend() 

'ppl' 

sage: D = polytopes.dodecahedron() 

sage: D.backend() 

'field' 

sage: P = Polyhedron([[1.23]]) 

sage: P.backend() 

'cdd' 

""" 

return self.parent().backend() 

 

field = deprecated_function_alias(22551, base_ring) 

 

@cached_method 

def center(self): 

""" 

Return the average of the vertices. 

 

See also :meth:`representative_point`. 

 

OUTPUT: 

 

The center of the polyhedron. All rays and lines are 

ignored. Raises a ``ZeroDivisionError`` for the empty 

polytope. 

 

EXAMPLES:: 

 

sage: p = polytopes.hypercube(3) 

sage: p = p + vector([1,0,0]) 

sage: p.center() 

(1, 0, 0) 

""" 

vertex_sum = vector(self.base_ring(), [0]*self.ambient_dim()) 

for v in self.vertex_generator(): 

vertex_sum += v.vector() 

vertex_sum.set_immutable() 

return vertex_sum / self.n_vertices() 

 

@cached_method 

def representative_point(self): 

""" 

Return a "generic" point. 

 

See also :meth:`center`. 

 

OUTPUT: 

 

A point as a coordinate vector. The point is chosen to be 

interior as far as possible. If the polyhedron is not 

full-dimensional, the point is in the relative interior. If 

the polyhedron is zero-dimensional, its single point is 

returned. 

 

EXAMPLES:: 

 

sage: p = Polyhedron(vertices=[(3,2)], rays=[(1,-1)]) 

sage: p.representative_point() 

(4, 1) 

sage: p.center() 

(3, 2) 

 

sage: Polyhedron(vertices=[(3,2)]).representative_point() 

(3, 2) 

""" 

accumulator = vector(self.base_ring(), [0]*self.ambient_dim()) 

for v in self.vertex_generator(): 

accumulator += v.vector() 

accumulator /= self.n_vertices() 

for r in self.ray_generator(): 

accumulator += r.vector() 

accumulator.set_immutable() 

return accumulator 

 

@cached_method 

def radius_square(self): 

""" 

Return the square of the maximal distance from the 

:meth:`center` to a vertex. All rays and lines are ignored. 

 

OUTPUT: 

 

The square of the radius, which is in :meth:`base_ring`. 

 

EXAMPLES:: 

 

sage: p = polytopes.permutahedron(4, project = False) 

sage: p.radius_square() 

5 

""" 

vertices = [v.vector() - self.center() for v in self.vertex_generator()] 

return max(v.dot_product(v) for v in vertices) 

 

def radius(self): 

""" 

Return the maximal distance from the center to a vertex. All 

rays and lines are ignored. 

 

OUTPUT: 

 

The radius for a rational polyhedron is, in general, not 

rational. use :meth:`radius_square` if you need a rational 

distance measure. 

 

EXAMPLES:: 

 

sage: p = polytopes.hypercube(4) 

sage: p.radius() 

2 

""" 

return sqrt(self.radius_square()) 

 

def is_inscribed(self, certificate=False): 

""" 

This function tests whether the vertices of the polyhedron are 

inscribed on a sphere. 

 

The polyhedron is expected to be compact and full-dimensional. 

A full-dimensional compact polytope is inscribed if there exists 

a point in space which is equidistant to all its vertices. 

 

ALGORITHM: 

 

The function first computes the circumsphere of a full-dimensional 

simplex with vertices of ``self``. It is found by lifting the points on a 

paraboloid to find the hyperplane on which the circumsphere is lifted. 

Then, it checks if all other vertices are equidistant to the 

circumcenter of that simplex. 

 

INPUT: 

 

- ``certificate`` -- (default: ``False``) boolean; specifies whether to 

return the circumcenter, if found 

 

OUTPUT: 

 

If ``certificate`` is true, returns a tuple containing: 

 

1. Boolean. 

2. The circumcenter of the polytope or None. 

 

If ``certificate`` is false: 

 

- a Boolean. 

 

EXAMPLES:: 

 

sage: q = Polyhedron(vertices = [[1,1,1,1],[-1,-1,1,1],[1,-1,-1,1], 

....: [-1,1,-1,1],[1,1,1,-1],[-1,-1,1,-1], 

....: [1,-1,-1,-1],[-1,1,-1,-1],[0,0,10/13,-24/13], 

....: [0,0,-10/13,-24/13]]) 

sage: q.is_inscribed(certificate=True) 

(True, (0, 0, 0, 0)) 

 

sage: cube = polytopes.cube() 

sage: cube.is_inscribed() 

True 

 

sage: translated_cube = Polyhedron(vertices=[v.vector() + vector([1,2,3]) 

....: for v in cube.vertices()]) 

sage: translated_cube.is_inscribed(certificate=True) 

(True, (1, 2, 3)) 

 

sage: truncated_cube = cube.face_truncation(cube.faces(0)[0]) 

sage: truncated_cube.is_inscribed() 

False 

 

The method is not implemented for non-full-dimensional polytope or 

unbounded polyhedra:: 

 

sage: square = Polyhedron(vertices=[[1,0,0],[0,1,0],[1,1,0],[0,0,0]]) 

sage: square.is_inscribed() 

Traceback (most recent call last): 

... 

NotImplementedError: This function is implemented for full-dimensional polyhedron only. 

 

sage: p = Polyhedron(vertices=[(0,0)],rays=[(1,0),(0,1)]) 

sage: p.is_inscribed() 

Traceback (most recent call last): 

... 

NotImplementedError: This function is not implemented for unbounded polyhedron. 

""" 

 

if not self.is_compact(): 

raise NotImplementedError("This function is not implemented for unbounded polyhedron.") 

 

if not self.is_full_dimensional(): 

raise NotImplementedError("This function is implemented for full-dimensional polyhedron only.") 

 

dimension = self.dimension() 

vertices = self.vertices() 

vertex = vertices[0] 

vertex_neighbors = vertex.neighbors() 

 

# The following simplex is full-dimensional because `self` is assumed 

# to be: every vertex has at least `dimension` neighbors and they form 

# a full simplex with `vertex`. 

simplex_vertices = [vertex] + [next(vertex_neighbors) for i in range(dimension)] 

 

raw_data = [] 

for vertex in simplex_vertices: 

vertex_vector = vertex.vector() 

raw_data += [[sum(i**2 for i in vertex_vector)] + 

[i for i in vertex_vector] + [1]] 

matrix_data = matrix(raw_data) 

 

# The determinant "a" should not be zero because the polytope is full 

# dimensional and also the simplex. 

a = matrix_data.matrix_from_columns(range(1, dimension+2)).determinant() 

 

minors = [(-1)**(i)*matrix_data.matrix_from_columns([j for j in range(dimension+2) if j != i]).determinant() 

for i in range(1, dimension+1)] 

c = (-1)**(dimension+1)*matrix_data.matrix_from_columns(range(dimension+1)).determinant() 

 

circumcenter = vector([minors[i]/(2*a) for i in range(dimension)]) 

squared_circumradius = (sum(m**2 for m in minors) - 4 * a * c) / (4*a**2) 

 

# Checking if the circumcenter has the correct sign 

test_vector = vertex.vector() - circumcenter 

if sum(i**2 for i in test_vector) != squared_circumradius: 

circumcenter = - circumcenter 

 

is_inscribed = all(sum(i**2 for i in v.vector() - circumcenter) == squared_circumradius 

for v in vertices if v not in simplex_vertices) 

 

if certificate: 

if is_inscribed: 

return (True, circumcenter) 

else: 

return (False, None) 

else: 

return is_inscribed 

 

def is_compact(self): 

""" 

Test for boundedness of the polytope. 

 

EXAMPLES:: 

 

sage: p = polytopes.icosahedron() 

sage: p.is_compact() 

True 

sage: p = Polyhedron(ieqs = [[0,1,0,0],[0,0,1,0],[0,0,0,1],[1,-1,0,0]]) 

sage: p.is_compact() 

False 

""" 

return self.n_rays() == 0 and self.n_lines() == 0 

 

def is_simple(self): 

""" 

Test for simplicity of a polytope. 

 

See :wikipedia:`Simple_polytope` 

 

EXAMPLES:: 

 

sage: p = Polyhedron([[0,0,0],[1,0,0],[0,1,0],[0,0,1]]) 

sage: p.is_simple() 

True 

sage: p = Polyhedron([[0,0,0],[4,4,0],[4,0,0],[0,4,0],[2,2,2]]) 

sage: p.is_simple() 

False 

 

""" 

if not self.is_compact(): return False 

 

for v in self.vertex_generator(): 

adj = [a for a in v.neighbors()] 

if len(adj) != self.dim(): 

return False 

return True 

 

def is_simplicial(self): 

""" 

Tests if the polytope is simplicial 

 

A polytope is simplicial if every facet is a simplex. 

 

See :wikipedia:`Simplicial_polytope` 

 

EXAMPLES:: 

 

sage: p = polytopes.hypercube(3) 

sage: p.is_simplicial() 

False 

sage: q = polytopes.simplex(5, project=True) 

sage: q.is_simplicial() 

True 

sage: p = Polyhedron([[0,0,0],[1,0,0],[0,1,0],[0,0,1]]) 

sage: p.is_simplicial() 

True 

sage: q = Polyhedron([[1,1,1],[-1,1,1],[1,-1,1],[-1,-1,1],[1,1,-1]]) 

sage: q.is_simplicial() 

False 

 

The method is not implemented for unbounded polyhedra:: 

 

sage: p = Polyhedron(vertices=[(0,0)],rays=[(1,0),(0,1)]) 

sage: p.is_simplicial() 

Traceback (most recent call last): 

... 

NotImplementedError: This function is implemented for polytopes only. 

""" 

if not(self.is_compact()): 

raise NotImplementedError("This function is implemented for polytopes only.") 

d = self.dim() 

return all(len([vertex for vertex in face.incident()]) == d 

for face in self.Hrepresentation()) 

 

def hyperplane_arrangement(self): 

""" 

Return the hyperplane arrangement defined by the equations and 

inequalities. 

 

OUTPUT: 

 

A :class:`hyperplane arrangement 

<sage.geometry.hyperplane_arrangement.arrangement.HyperplaneArrangementElement>` 

consisting of the hyperplanes defined by the 

:meth:`Hrepresentation`. 

If the polytope is full-dimensional, this is the hyperplane 

arrangement spanned by the facets of the polyhedron. 

 

EXAMPLES:: 

 

sage: p = polytopes.hypercube(2) 

sage: p.hyperplane_arrangement() 

Arrangement <-t0 + 1 | -t1 + 1 | t1 + 1 | t0 + 1> 

""" 

names = tuple('t' + str(i) for i in range(self.ambient_dim())) 

from sage.geometry.hyperplane_arrangement.arrangement import HyperplaneArrangements 

field = self.base_ring().fraction_field() 

H = HyperplaneArrangements(field, names) 

return H(self) 

 

@cached_method 

def gale_transform(self): 

""" 

Return the Gale transform of a polytope as described in the 

reference below. 

 

OUTPUT: 

 

A list of vectors, the Gale transform. The dimension is the 

dimension of the affine dependencies of the vertices of the 

polytope. 

 

EXAMPLES: 

 

This is from the reference, for a triangular prism:: 

 

sage: p = Polyhedron(vertices = [[0,0],[0,1],[1,0]]) 

sage: p2 = p.prism() 

sage: p2.gale_transform() 

[(1, 0), (0, 1), (-1, -1), (-1, 0), (0, -1), (1, 1)] 

 

REFERENCES: 

 

Lectures in Geometric Combinatorics, R.R.Thomas, 2006, AMS Press. 

""" 

if not self.is_compact(): raise ValueError('Not a polytope.') 

 

A = matrix(self.n_vertices(), 

[ [1]+x for x in self.vertex_generator()]) 

A = A.transpose() 

A_ker = A.right_kernel() 

return A_ker.basis_matrix().transpose().rows() 

 

@cached_method 

def normal_fan(self): 

r""" 

Return the normal fan of a compact full-dimensional rational polyhedron. 

 

OUTPUT: 

 

A complete fan of the ambient space as a 

:class:`~sage.geometry.fan.RationalPolyhedralFan`. 

 

.. SEEALSO:: 

 

:meth:`~sage.geometry.polyhedron.base.face_fan`. 

 

EXAMPLES:: 

 

sage: S = Polyhedron(vertices = [[0, 0], [1, 0], [0, 1]]) 

sage: S.normal_fan() 

Rational polyhedral fan in 2-d lattice N 

 

sage: C = polytopes.hypercube(4) 

sage: NF = C.normal_fan(); NF 

Rational polyhedral fan in 4-d lattice N 

 

Currently, it is only possible to get the normal fan of a bounded rational polytope:: 

 

sage: P = Polyhedron(rays = [[1, 0], [0, 1]]) 

sage: P.normal_fan() 

Traceback (most recent call last): 

... 

NotImplementedError: the normal fan is only supported for polytopes (compact polyhedra). 

 

sage: Q = Polyhedron(vertices = [[1, 0, 0], [0, 1, 0], [0, 0, 1]]) 

sage: Q.normal_fan() 

Traceback (most recent call last): 

... 

ValueError: the normal fan is only defined for full-dimensional polytopes 

 

sage: R = Polyhedron(vertices = [[0, 0], [AA(sqrt(2)), 0], [0, AA(sqrt(2))]]) 

sage: R.normal_fan() 

Traceback (most recent call last): 

... 

NotImplementedError: normal fan handles only polytopes over the rationals 

 

REFERENCES: 

 

For more information, see Chapter 7 of [Zie2007]_. 

""" 

from sage.geometry.fan import NormalFan 

 

if not QQ.has_coerce_map_from(self.base_ring()): 

raise NotImplementedError('normal fan handles only polytopes over the rationals') 

 

return NormalFan(self) 

 

@cached_method 

def face_fan(self): 

r""" 

Return the face fan of a compact rational polyhedron. 

 

OUTPUT: 

 

A fan of the ambient space as a 

:class:`~sage.geometry.fan.RationalPolyhedralFan`. 

 

.. SEEALSO:: 

 

:meth:`~sage.geometry.polyhedron.base.normal_fan`. 

 

EXAMPLES:: 

 

sage: T = polytopes.cuboctahedron() 

sage: T.face_fan() 

Rational polyhedral fan in 3-d lattice M 

 

The polytope should contain the origin in the interior:: 

 

sage: P = Polyhedron(vertices = [[1/2, 1], [1, 1/2]]) 

sage: P.face_fan() 

Traceback (most recent call last): 

... 

ValueError: face fans are defined only for polytopes containing the origin as an interior point! 

 

sage: Q = Polyhedron(vertices = [[-1, 1/2], [1, -1/2]]) 

sage: Q.contains([0,0]) 

True 

sage: FF = Q.face_fan(); FF 

Rational polyhedral fan in 2-d lattice M 

 

The polytope has to have rational coordinates:: 

 

sage: S = polytopes.dodecahedron() 

sage: S.face_fan() 

Traceback (most recent call last): 

... 

NotImplementedError: face fan handles only polytopes over the rationals 

 

REFERENCES: 

 

For more information, see Chapter 7 of [Zie2007]_. 

""" 

from sage.geometry.fan import FaceFan 

 

if not QQ.has_coerce_map_from(self.base_ring()): 

raise NotImplementedError('face fan handles only polytopes over the rationals') 

 

return FaceFan(self) 

 

def triangulate(self, engine='auto', connected=True, fine=False, regular=None, star=None): 

r""" 

Returns a triangulation of the polytope. 

 

INPUT: 

 

- ``engine`` -- either 'auto' (default), 'internal', or 

'TOPCOM'. The latter two instruct this package to always 

use its own triangulation algorithms or TOPCOM's algorithms, 

respectively. By default ('auto'), TOPCOM is used if it is 

available and internal routines otherwise. 

 

The remaining keyword parameters are passed through to the 

:class:`~sage.geometry.triangulation.point_configuration.PointConfiguration` 

constructor: 

 

- ``connected`` -- boolean (default: ``True``). Whether the 

triangulations should be connected to the regular 

triangulations via bistellar flips. These are much easier to 

compute than all triangulations. 

 

- ``fine`` -- boolean (default: ``False``). Whether the 

triangulations must be fine, that is, make use of all points 

of the configuration. 

 

- ``regular`` -- boolean or ``None`` (default: 

``None``). Whether the triangulations must be regular. A 

regular triangulation is one that is induced by a 

piecewise-linear convex support function. In other words, 

the shadows of the faces of a polyhedron in one higher 

dimension. 

 

* ``True``: Only regular triangulations. 

 

* ``False``: Only non-regular triangulations. 

 

* ``None`` (default): Both kinds of triangulation. 

 

- ``star`` -- either ``None`` (default) or a point. Whether 

the triangulations must be star. A triangulation is star if 

all maximal simplices contain a common point. The central 

point can be specified by its index (an integer) in the 

given points or by its coordinates (anything iterable.) 

 

OUTPUT: 

 

A triangulation of the convex hull of the vertices as a 

:class:`~sage.geometry.triangulation.point_configuration.Triangulation`. The 

indices in the triangulation correspond to the 

:meth:`Vrepresentation` objects. 

 

EXAMPLES:: 

 

sage: cube = polytopes.hypercube(3) 

sage: triangulation = cube.triangulate( 

....: engine='internal') # to make doctest independent of TOPCOM 

sage: triangulation 

(<0,1,2,7>, <0,1,4,7>, <0,2,4,7>, <1,2,3,7>, <1,4,5,7>, <2,4,6,7>) 

sage: simplex_indices = triangulation[0]; simplex_indices 

(0, 1, 2, 7) 

sage: simplex_vertices = [ cube.Vrepresentation(i) for i in simplex_indices ] 

sage: simplex_vertices 

[A vertex at (-1, -1, -1), A vertex at (-1, -1, 1), 

A vertex at (-1, 1, -1), A vertex at (1, 1, 1)] 

sage: Polyhedron(simplex_vertices) 

A 3-dimensional polyhedron in ZZ^3 defined as the convex hull of 4 vertices 

""" 

if not self.is_compact(): 

raise NotImplementedError('I can only triangulate compact polytopes.') 

from sage.geometry.triangulation.point_configuration import PointConfiguration 

pc = PointConfiguration((v.vector() for v in self.vertex_generator()), 

connected=connected, fine=fine, regular=regular, star=star) 

pc.set_engine(engine) 

return pc.triangulate() 

 

@coerce_binop 

def minkowski_sum(self, other): 

""" 

Return the Minkowski sum. 

 

Minkowski addition of two subsets of a vector space is defined 

as 

 

.. MATH:: 

 

X \oplus Y = 

\cup_{y\in Y} (X+y) = 

\cup_{x\in X, y\in Y} (x+y) 

 

See :meth:`minkowski_difference` for a partial inverse operation. 

 

INPUT: 

 

- ``other`` -- a :class:`Polyhedron_base`. 

 

OUTPUT: 

 

The Minkowski sum of ``self`` and ``other``. 

 

EXAMPLES:: 

 

sage: X = polytopes.hypercube(3) 

sage: Y = Polyhedron(vertices=[(0,0,0), (0,0,1/2), (0,1/2,0), (1/2,0,0)]) 

sage: X+Y 

A 3-dimensional polyhedron in QQ^3 defined as the convex hull of 13 vertices 

 

sage: four_cube = polytopes.hypercube(4) 

sage: four_simplex = Polyhedron(vertices = [[0, 0, 0, 1], [0, 0, 1, 0], [0, 1, 0, 0], [1, 0, 0, 0]]) 

sage: four_cube + four_simplex 

A 4-dimensional polyhedron in ZZ^4 defined as the convex hull of 36 vertices 

sage: four_cube.minkowski_sum(four_simplex) == four_cube + four_simplex 

True 

 

sage: poly_spam = Polyhedron([[3,4,5,2],[1,0,0,1],[0,0,0,0],[0,4,3,2],[-3,-3,-3,-3]], base_ring=ZZ) 

sage: poly_eggs = Polyhedron([[5,4,5,4],[-4,5,-4,5],[4,-5,4,-5],[0,0,0,0]], base_ring=QQ) 

sage: poly_spam + poly_spam + poly_eggs 

A 4-dimensional polyhedron in QQ^4 defined as the convex hull of 12 vertices 

 

TESTS:: 

 

sage: Z = X.Minkowski_sum(X) 

doctest:warning...: 

DeprecationWarning: Minkowski_sum is deprecated. Please use minkowski_sum instead. 

See http://trac.sagemath.org/23685 for details. 

""" 

new_vertices = [] 

for v1 in self.vertex_generator(): 

for v2 in other.vertex_generator(): 

new_vertices.append(list(v1() + v2())) 

if new_vertices != []: 

new_rays = self.rays() + other.rays() 

new_lines = self.lines() + other.lines() 

return self.parent().element_class(self.parent(), [new_vertices, new_rays, new_lines], None) 

else: 

return self.parent().element_class(self.parent(), None, None) 

 

_add_ = minkowski_sum 

 

Minkowski_sum = deprecated_function_alias(23685, minkowski_sum) 

 

@coerce_binop 

def minkowski_difference(self, other): 

""" 

Return the Minkowski difference. 

 

Minkowski subtraction can equivalently be defined via 

Minkowski addition (see :meth:`minkowski_sum`) or as 

set-theoretic intersection via 

 

.. MATH:: 

 

X \ominus Y = 

(X^c \oplus Y)^c = 

\cap_{y\in Y} (X-y) 

 

where superscript-"c" means the complement in the ambient 

vector space. The Minkowski difference of convex sets is 

convex, and the difference of polyhedra is again a 

polyhedron. We only consider the case of polyhedra in the 

following. Note that it is not quite the inverse of 

addition. In fact: 

 

* `(X+Y)-Y = X` for any polyhedra `X`, `Y`. 

 

* `(X-Y)+Y \subseteq X` 

 

* `(X-Y)+Y = X` if and only if Y is a Minkowski summand of X. 

 

INPUT: 

 

- ``other`` -- a :class:`Polyhedron_base`. 

 

OUTPUT: 

 

The Minkowski difference of ``self`` and ``other``. Also known 

as Minkowski subtraction of ``other`` from ``self``. 

 

EXAMPLES:: 

 

sage: X = polytopes.hypercube(3) 

sage: Y = Polyhedron(vertices=[(0,0,0), (0,0,1), (0,1,0), (1,0,0)]) / 2 

sage: (X+Y)-Y == X 

True 

sage: (X-Y)+Y < X 

True 

 

The polyhedra need not be full-dimensional:: 

 

sage: X2 = Polyhedron(vertices=[(-1,-1,0),(1,-1,0),(-1,1,0),(1,1,0)]) 

sage: Y2 = Polyhedron(vertices=[(0,0,0), (0,1,0), (1,0,0)]) / 2 

sage: (X2+Y2)-Y2 == X2 

True 

sage: (X2-Y2)+Y2 < X2 

True 

 

Minus sign is really an alias for :meth:`minkowski_difference` 

:: 

 

sage: four_cube = polytopes.hypercube(4) 

sage: four_simplex = Polyhedron(vertices = [[0, 0, 0, 1], [0, 0, 1, 0], [0, 1, 0, 0], [1, 0, 0, 0]]) 

sage: four_cube - four_simplex 

A 4-dimensional polyhedron in ZZ^4 defined as the convex hull of 16 vertices 

sage: four_cube.minkowski_difference(four_simplex) == four_cube - four_simplex 

True 

 

Coercion of the base ring works:: 

 

sage: poly_spam = Polyhedron([[3,4,5,2],[1,0,0,1],[0,0,0,0],[0,4,3,2],[-3,-3,-3,-3]], base_ring=ZZ) 

sage: poly_eggs = Polyhedron([[5,4,5,4],[-4,5,-4,5],[4,-5,4,-5],[0,0,0,0]], base_ring=QQ) / 100 

sage: poly_spam - poly_eggs 

A 4-dimensional polyhedron in QQ^4 defined as the convex hull of 5 vertices 

 

TESTS:: 

 

sage: X = polytopes.hypercube(2) 

sage: Y = Polyhedron(vertices=[(1,1)]) 

sage: (X-Y).Vrepresentation() 

(A vertex at (0, -2), A vertex at (0, 0), A vertex at (-2, 0), A vertex at (-2, -2)) 

 

sage: Y = Polyhedron(vertices=[(1,1), (0,0)]) 

sage: (X-Y).Vrepresentation() 

(A vertex at (0, -1), A vertex at (0, 0), A vertex at (-1, 0), A vertex at (-1, -1)) 

 

sage: X = X + Y # now Y is a Minkowski summand of X 

sage: (X+Y)-Y == X 

True 

sage: (X-Y)+Y == X 

True 

 

TESTS:: 

 

sage: Z = X.Minkowski_difference(X) 

doctest:warning...: 

DeprecationWarning: Minkowski_difference is deprecated. Please use minkowski_difference instead. 

See http://trac.sagemath.org/23685 for details. 

""" 

if other.is_empty(): 

return self.parent().universe() # empty intersection = everything 

if not other.is_compact(): 

raise NotImplementedError('only subtracting compact polyhedra is implemented') 

new_eqns = [] 

for eq in self.equations(): 

values = [ eq.A() * v.vector() for v in other.vertices() ] 

eq = list(eq) 

eq[0] += min(values) # shift constant term 

new_eqns.append(eq) 

P = self.parent() 

new_ieqs = [] 

for ieq in self.inequalities(): 

values = [ ieq.A() * v.vector() for v in other.vertices() ] 

ieq = list(ieq) 

ieq[0] += min(values) # shift constant term 

new_ieqs.append(ieq) 

P = self.parent() 

return P.element_class(P, None, [new_ieqs, new_eqns]) 

 

Minkowski_difference = deprecated_function_alias(23685, 

minkowski_difference) 

 

def __sub__(self, other): 

r""" 

Implement minus binary operation 

 

Polyhedra are not a ring with respect to dilatation and 

Minkowski sum, for example `X\oplus(-1)*Y \not= X\ominus Y`. 

 

INPUT: 

 

- ``other`` -- a translation vector or a polyhedron. 

 

OUTPUT: 

 

Either translation by the negative of the given vector or 

Minkowski subtraction by the given polyhedron. 

 

EXAMPLES:: 

 

sage: X = polytopes.hypercube(2) 

sage: v = vector([1,1]) 

sage: (X - v/2).Vrepresentation() 

(A vertex at (-3/2, -3/2), A vertex at (-3/2, 1/2), 

A vertex at (1/2, -3/2), A vertex at (1/2, 1/2)) 

sage: (X-v)+v == X 

True 

 

sage: Y = Polyhedron(vertices=[(1/2,0),(0,1/2)]) 

sage: (X-Y).Vrepresentation() 

(A vertex at (1/2, -1), A vertex at (1/2, 1/2), 

A vertex at (-1, 1/2), A vertex at (-1, -1)) 

sage: (X+Y)-Y == X 

True 

""" 

if is_Polyhedron(other): 

return self.minkowski_difference(other) 

return self + (-other) 

 

def is_minkowski_summand(self, Y): 

""" 

Test whether ``Y`` is a Minkowski summand. 

 

See :meth:`minkowski_sum`. 

 

OUTPUT: 

 

Boolean. Whether there exists another polyhedron `Z` such that 

``self`` can be written as `Y\oplus Z`. 

 

EXAMPLES:: 

 

sage: A = polytopes.hypercube(2) 

sage: B = Polyhedron(vertices=[(0,1), (1/2,1)]) 

sage: C = Polyhedron(vertices=[(1,1)]) 

sage: A.is_minkowski_summand(B) 

True 

sage: A.is_minkowski_summand(C) 

True 

sage: B.is_minkowski_summand(C) 

True 

sage: B.is_minkowski_summand(A) 

False 

sage: C.is_minkowski_summand(A) 

False 

sage: C.is_minkowski_summand(B) 

False 

 

TESTS:: 

 

sage: b = C.is_Minkowski_summand(B) 

doctest:warning...: 

DeprecationWarning: is_Minkowski_summand is deprecated. Please use is_minkowski_summand instead. 

See http://trac.sagemath.org/23685 for details. 

""" 

return self.minkowski_difference(Y).minkowski_sum(Y) == self 

 

is_Minkowski_summand = deprecated_function_alias(23685, 

is_minkowski_summand) 

 

def translation(self, displacement): 

""" 

Return the translated polyhedron. 

 

INPUT: 

 

- ``displacement`` -- a displacement vector or a list/tuple of 

coordinates that determines a displacement vector. 

 

OUTPUT: 

 

The translated polyhedron. 

 

EXAMPLES:: 

 

sage: P = Polyhedron([[0,0],[1,0],[0,1]], base_ring=ZZ) 

sage: P.translation([2,1]) 

A 2-dimensional polyhedron in ZZ^2 defined as the convex hull of 3 vertices 

sage: P.translation( vector(QQ,[2,1]) ) 

A 2-dimensional polyhedron in QQ^2 defined as the convex hull of 3 vertices 

""" 

displacement = vector(displacement) 

new_vertices = [x.vector()+displacement for x in self.vertex_generator()] 

new_rays = self.rays() 

new_lines = self.lines() 

new_ring = self.parent()._coerce_base_ring(displacement) 

return Polyhedron(vertices=new_vertices, rays=new_rays, lines=new_lines, base_ring=new_ring) 

 

def product(self, other): 

""" 

Return the Cartesian product. 

 

INPUT: 

 

- ``other`` -- a :class:`Polyhedron_base`. 

 

OUTPUT: 

 

The Cartesian product of ``self`` and ``other`` with a 

suitable base ring to encompass the two. 

 

EXAMPLES:: 

 

sage: P1 = Polyhedron([[0],[1]], base_ring=ZZ) 

sage: P2 = Polyhedron([[0],[1]], base_ring=QQ) 

sage: P1.product(P2) 

A 2-dimensional polyhedron in QQ^2 defined as the convex hull of 4 vertices 

 

The Cartesian product is the product in the semiring of polyhedra:: 

 

sage: P1 * P1 

A 2-dimensional polyhedron in ZZ^2 defined as the convex hull of 4 vertices 

sage: P1 * P2 

A 2-dimensional polyhedron in QQ^2 defined as the convex hull of 4 vertices 

sage: P2 * P2 

A 2-dimensional polyhedron in QQ^2 defined as the convex hull of 4 vertices 

sage: 2 * P1 

A 1-dimensional polyhedron in ZZ^1 defined as the convex hull of 2 vertices 

sage: P1 * 2.0 

A 1-dimensional polyhedron in RDF^1 defined as the convex hull of 2 vertices 

 

TESTS: 

 

Check that :trac:`15253` is fixed:: 

 

sage: polytopes.hypercube(1) * polytopes.hypercube(2) 

A 3-dimensional polyhedron in ZZ^3 defined as the convex hull of 8 vertices 

""" 

try: 

new_ring = self.parent()._coerce_base_ring(other) 

except TypeError: 

raise TypeError("no common canonical parent for objects with parents: " + str(self.parent()) \ 

+ " and " + str(other.parent())) 

 

new_vertices = [ list(x)+list(y) 

for x in self.vertex_generator() for y in other.vertex_generator()] 

new_rays = [] 

new_rays.extend( [ r+[0]*other.ambient_dim() 

for r in self.ray_generator() ] ) 

new_rays.extend( [ [0]*self.ambient_dim()+r 

for r in other.ray_generator() ] ) 

new_lines = [] 

new_lines.extend( [ l+[0]*other.ambient_dim() 

for l in self.line_generator() ] ) 

new_lines.extend( [ [0]*self.ambient_dim()+l 

for l in other.line_generator() ] ) 

return Polyhedron(vertices=new_vertices, 

rays=new_rays, lines=new_lines, 

base_ring=new_ring) 

 

_mul_ = product 

 

def dilation(self, scalar): 

""" 

Return the dilated (uniformly stretched) polyhedron. 

 

INPUT: 

 

- ``scalar`` -- A scalar, not necessarily in :meth:`base_ring`. 

 

OUTPUT: 

 

The polyhedron dilated by that scalar, possibly coerced to a 

bigger base ring. 

 

EXAMPLES:: 

 

sage: p = Polyhedron(vertices = [[t,t^2,t^3] for t in srange(2,6)]) 

sage: next(p.vertex_generator()) 

A vertex at (2, 4, 8) 

sage: p2 = p.dilation(2) 

sage: next(p2.vertex_generator()) 

A vertex at (4, 8, 16) 

sage: p.dilation(2) == p * 2 

True 

 

TESTS: 

 

Dilation of empty polyhedra works, see :trac:`14987`:: 

 

sage: p = Polyhedron(ambient_dim=2); p 

The empty polyhedron in ZZ^2 

sage: p.dilation(3) 

The empty polyhedron in ZZ^2 

 

sage: p = Polyhedron(vertices=[(1,1)], rays=[(1,0)], lines=[(0,1)]) 

sage: (-p).rays() 

(A ray in the direction (-1, 0),) 

sage: (-p).lines() 

(A line in the direction (0, 1),) 

 

sage: (0*p).rays() 

() 

sage: (0*p).lines() 

() 

""" 

if scalar > 0: 

new_vertices = [ list(scalar*v.vector()) for v in self.vertex_generator() ] 

new_rays = self.rays() 

new_lines = self.lines() 

elif scalar < 0: 

new_vertices = [ list(scalar*v.vector()) for v in self.vertex_generator() ] 

new_rays = [ list(-r.vector()) for r in self.ray_generator()] 

new_lines = self.lines() 

else: 

new_vertices = [ self.ambient_space().zero() for v in self.vertex_generator() ] 

new_rays = [] 

new_lines = [] 

return Polyhedron(vertices=new_vertices, 

rays=new_rays, lines=new_lines, 

base_ring=self.parent()._coerce_base_ring(scalar), 

ambient_dim=self.ambient_dim()) 

 

def _acted_upon_(self, actor, self_on_left): 

""" 

Implement the multiplicative action by scalars or other polyhedra. 

 

INPUT: 

 

- ``actor`` -- A scalar, not necessarily in :meth:`base_ring`, 

or a :class:`Polyhedron`. 

 

OUTPUT: 

 

Multiplication by another polyhedron returns the product 

polytope. Multiplication by a scalar returns the polytope 

dilated by that scalar, possibly coerced to the bigger base ring. 

 

EXAMPLES:: 

 

sage: p = Polyhedron(vertices = [[t,t^2,t^3] for t in srange(2,6)]) 

sage: p._acted_upon_(2, True) == p.dilation(2) 

True 

sage: p*2 == p.dilation(2) 

True 

sage: p*p == p.product(p) 

True 

sage: p + vector(ZZ,[1,2,3]) == p.translation([1,2,3]) 

True 

""" 

if is_Polyhedron(actor): 

return self.product(actor) 

if is_Vector(actor): 

return self.translation(actor) 

else: 

return self.dilation(actor) 

 

def __neg__(self): 

""" 

Negation of a polytope is defined as inverting the coordinates. 

 

EXAMPLES:: 

 

sage: t = polytopes.simplex(3,project=False); t.vertices() 

(A vertex at (0, 0, 0, 1), A vertex at (0, 0, 1, 0), 

A vertex at (0, 1, 0, 0), A vertex at (1, 0, 0, 0)) 

sage: neg_ = -t 

sage: neg_.vertices() 

(A vertex at (-1, 0, 0, 0), A vertex at (0, -1, 0, 0), 

A vertex at (0, 0, -1, 0), A vertex at (0, 0, 0, -1)) 

 

TESTS:: 

 

sage: p = Polyhedron(ieqs=[[1,1,0]]) 

sage: p.rays() 

(A ray in the direction (1, 0),) 

sage: pneg = p.__neg__() 

sage: pneg.rays() 

(A ray in the direction (-1, 0),) 

""" 

return self.dilation(-1) 

 

def __truediv__(self, scalar): 

""" 

Divide by a scalar factor. 

 

See :meth:`dilation` for details. 

 

EXAMPLES:: 

 

sage: p = Polyhedron(vertices = [[t,t^2,t^3] for t in srange(2,4)]) 

sage: (p/5).Vrepresentation() 

(A vertex at (2/5, 4/5, 8/5), A vertex at (3/5, 9/5, 27/5)) 

sage: (p/int(5)).Vrepresentation() 

(A vertex at (0.4, 0.8, 1.6), A vertex at (0.6, 1.8, 5.4)) 

""" 

return self.dilation(1/scalar) 

 

__div__ = __truediv__ 

 

@coerce_binop 

def convex_hull(self, other): 

""" 

Return the convex hull of the set-theoretic union of the two 

polyhedra. 

 

INPUT: 

 

- ``other`` -- a :class:`Polyhedron`. 

 

OUTPUT: 

 

The convex hull. 

 

EXAMPLES:: 

 

sage: a_simplex = polytopes.simplex(3, project=True) 

sage: verts = a_simplex.vertices() 

sage: verts = [[x[0]*3/5+x[1]*4/5, -x[0]*4/5+x[1]*3/5, x[2]] for x in verts] 

sage: another_simplex = Polyhedron(vertices = verts) 

sage: simplex_union = a_simplex.convex_hull(another_simplex) 

sage: simplex_union.n_vertices() 

7 

""" 

hull_vertices = self.vertices() + other.vertices() 

hull_rays = self.rays() + other.rays() 

hull_lines = self.lines() + other.lines() 

return self.parent().element_class(self.parent(), [hull_vertices, hull_rays, hull_lines], None) 

 

@coerce_binop 

def intersection(self, other): 

""" 

Return the intersection of one polyhedron with another. 

 

INPUT: 

 

- ``other`` -- a :class:`Polyhedron`. 

 

OUTPUT: 

 

The intersection. 

 

Note that the intersection of two `\ZZ`-polyhedra might not be 

a `\ZZ`-polyhedron. In this case, a `\QQ`-polyhedron is 

returned. 

 

EXAMPLES:: 

 

sage: cube = polytopes.hypercube(3) 

sage: oct = polytopes.cross_polytope(3) 

sage: cube.intersection(oct*2) 

A 3-dimensional polyhedron in ZZ^3 defined as the convex hull of 12 vertices 

 

As a shorthand, one may use:: 

 

sage: cube & oct*2 

A 3-dimensional polyhedron in ZZ^3 defined as the convex hull of 12 vertices 

 

The intersection of two `\ZZ`-polyhedra is not necessarily a `\ZZ`-polyhedron:: 

 

sage: P = Polyhedron([(0,0),(1,1)], base_ring=ZZ) 

sage: P.intersection(P) 

A 1-dimensional polyhedron in ZZ^2 defined as the convex hull of 2 vertices 

sage: Q = Polyhedron([(0,1),(1,0)], base_ring=ZZ) 

sage: P.intersection(Q) 

A 0-dimensional polyhedron in QQ^2 defined as the convex hull of 1 vertex 

sage: _.Vrepresentation() 

(A vertex at (1/2, 1/2),) 

 

TESTS: 

 

Check that :trac:`19012` is fixed:: 

 

sage: K.<a> = QuadraticField(5) 

sage: P = Polyhedron([[0,0],[0,a],[1,1]]) 

sage: Q = Polyhedron(ieqs=[[-1,a,1]]) 

sage: P.intersection(Q) 

A 2-dimensional polyhedron in (Number Field in a with defining 

polynomial x^2 - 5)^2 defined as the convex hull of 4 vertices 

""" 

new_ieqs = self.inequalities() + other.inequalities() 

new_eqns = self.equations() + other.equations() 

parent = self.parent() 

try: 

return parent.element_class(parent, None, [new_ieqs, new_eqns]) 

except TypeError as msg: 

if self.base_ring() is ZZ: 

parent = parent.base_extend(QQ) 

return parent.element_class(parent, None, [new_ieqs, new_eqns]) 

else: 

raise TypeError(msg) 

 

__and__ = intersection 

 

def truncation(self, cut_frac=None): 

r""" 

Return a new polyhedron formed from two points on each edge 

between two vertices. 

 

INPUT: 

 

- ``cut_frac`` -- integer, how deeply to cut into the edge. 

Default is `\frac{1}{3}`. 

 

OUTPUT: 

 

A Polyhedron object, truncated as described above. 

 

EXAMPLES:: 

 

sage: cube = polytopes.hypercube(3) 

sage: trunc_cube = cube.truncation() 

sage: trunc_cube.n_vertices() 

24 

sage: trunc_cube.n_inequalities() 

14 

""" 

if cut_frac is None: 

cut_frac = ZZ.one() / 3 

 

new_vertices = [] 

for e in self.bounded_edges(): 

new_vertices.append((1 - cut_frac) * e[0]() + cut_frac * e[1]()) 

new_vertices.append(cut_frac * e[0]() + (1 - cut_frac) * e[1]()) 

 

new_vertices = [list(v) for v in new_vertices] 

new_rays = self.rays() 

new_lines = self.lines() 

 

return Polyhedron(vertices=new_vertices, rays=new_rays, 

lines=new_lines, 

base_ring=self.parent()._coerce_base_ring(cut_frac)) 

 

edge_truncation = deprecated_function_alias(18128, truncation) 

 

def face_truncation(self, face, linear_coefficients=None, cut_frac=None): 

r""" 

Return a new polyhedron formed by truncating a face by an hyperplane. 

 

By default, the normal vector of the hyperplane used to truncate the 

polyhedron is obtained by taking the barycenter vector of the cone 

corresponding to the truncated face in the normal fan of the 

polyhedron. It is possible to change the direction using the option 

``linear_coefficients``. 

 

To determine how deep the truncation is done, the method uses the 

parameter ``cut_frac``. By default it is equal to `\frac{1}{3}`. Once 

the normal vector of the cutting hyperplane is chosen, the vertices of 

polyhedron are evaluated according to the corresponding linear 

function. The parameter `\frac{1}{3}` means that the cutting 

hyperplane is placed `\frac{1}{3}` of the way from the vertices of the 

truncated face to the next evaluated vertex. 

 

INPUT: 

 

- ``face`` -- a PolyhedronFace. 

- ``linear_coefficients`` -- tuple of integer. Specifies the coefficient 

of the normal vector of the cutting hyperplane used to truncate the 

face. 

The default direction is determined using the normal fan of the 

polyhedron. 

- ``cut_frac`` -- number between 0 and 1. Determines where the 

hyperplane cuts the polyhedron. A value close to 0 cuts very close 

to the face, whereas a value close to 1 cuts very close to the next 

vertex (according to the normal vector of the cutting hyperplane). 

Default is `\frac{1}{3}`. 

 

OUTPUT: 

 

A Polyhedron object, truncated as described above. 

 

EXAMPLES:: 

 

sage: Cube = polytopes.hypercube(3) 

sage: vertex_trunc1 = Cube.face_truncation(Cube.faces(0)[0]) 

sage: vertex_trunc1.f_vector() 

(1, 10, 15, 7, 1) 

sage: vertex_trunc1.faces(2) 

(<0,1,2,3>, 

<2,3,4,5>, 

<1,2,5,6>, 

<0,1,6,7,8>, 

<4,5,6,7,9>, 

<7,8,9>, 

<0,3,4,8,9>) 

sage: vertex_trunc1.vertices() 

(A vertex at (1, -1, -1), 

A vertex at (1, 1, -1), 

A vertex at (1, 1, 1), 

A vertex at (1, -1, 1), 

A vertex at (-1, -1, 1), 

A vertex at (-1, 1, 1), 

A vertex at (-1, 1, -1), 

A vertex at (-1, -1/3, -1), 

A vertex at (-1/3, -1, -1), 

A vertex at (-1, -1, -1/3)) 

sage: vertex_trunc2 = Cube.face_truncation(Cube.faces(0)[0],cut_frac=1/2) 

sage: vertex_trunc2.f_vector() 

(1, 10, 15, 7, 1) 

sage: vertex_trunc2.faces(2) 

(<0,1,2,3>, 

<2,3,4,5>, 

<1,2,5,6>, 

<0,1,6,7,8>, 

<4,5,6,7,9>, 

<7,8,9>, 

<0,3,4,8,9>) 

sage: vertex_trunc2.vertices() 

(A vertex at (1, -1, -1), 

A vertex at (1, 1, -1), 

A vertex at (1, 1, 1), 

A vertex at (1, -1, 1), 

A vertex at (-1, -1, 1), 

A vertex at (-1, 1, 1), 

A vertex at (-1, 1, -1), 

A vertex at (-1, 0, -1), 

A vertex at (0, -1, -1), 

A vertex at (-1, -1, 0)) 

sage: vertex_trunc3 = Cube.face_truncation(Cube.faces(0)[0],cut_frac=0.3) 

sage: vertex_trunc3.vertices() 

(A vertex at (-1.0, -1.0, 1.0), 

A vertex at (-1.0, 1.0, -1.0), 

A vertex at (-1.0, 1.0, 1.0), 

A vertex at (1.0, 1.0, -1.0), 

A vertex at (1.0, 1.0, 1.0), 

A vertex at (1.0, -1.0, 1.0), 

A vertex at (1.0, -1.0, -1.0), 

A vertex at (-0.4, -1.0, -1.0), 

A vertex at (-1.0, -0.4, -1.0), 

A vertex at (-1.0, -1.0, -0.4)) 

sage: edge_trunc = Cube.face_truncation(Cube.faces(1)[0]) 

sage: edge_trunc.f_vector() 

(1, 10, 15, 7, 1) 

sage: edge_trunc.faces(2) 

(<0,1,2,3>, 

<1,2,4,5>, 

<4,5,6,7>, 

<0,1,5,6,8>, 

<2,3,4,7,9>, 

<6,7,8,9>, 

<0,3,8,9>) 

sage: face_trunc = Cube.face_truncation(Cube.faces(2)[0]) 

sage: face_trunc.vertices() 

(A vertex at (1, -1, -1), 

A vertex at (1, 1, -1), 

A vertex at (1, 1, 1), 

A vertex at (1, -1, 1), 

A vertex at (-1/3, -1, 1), 

A vertex at (-1/3, 1, 1), 

A vertex at (-1/3, 1, -1), 

A vertex at (-1/3, -1, -1)) 

sage: face_trunc.face_lattice().is_isomorphic(Cube.face_lattice()) 

True 

""" 

if cut_frac is None: 

cut_frac = ZZ.one() / 3 

 

face_vertices = face.vertices() 

 

normal_vectors = [] 

 

for facet in self.Hrepresentation(): 

if all(facet.contains(x) and not facet.interior_contains(x) for x 

in face_vertices): 

# The facet contains the face 

normal_vectors.append(facet.A()) 

 

if linear_coefficients is not None: 

normal_vector = sum(linear_coefficients[i]*normal_vectors[i] for i 

in range(len(normal_vectors))) 

else: 

normal_vector = sum(normal_vectors) 

 

B = - normal_vector * (face_vertices[0].vector()) 

 

linear_evaluation = set(-normal_vector * (v.vector()) for v in 

self.vertices()) 

 

if B == max(linear_evaluation): 

C = max(linear_evaluation.difference(set([B]))) 

else: 

C = min(linear_evaluation.difference(set([B]))) 

 

cut_height = (1 - cut_frac) * B + cut_frac * C 

ineq_vector = tuple([cut_height]) + tuple(normal_vector) 

 

new_ieqs = self.inequalities_list() + [ineq_vector] 

new_eqns = self.equations_list() 

 

return Polyhedron(ieqs=new_ieqs, eqns=new_eqns, base_ring= 

self.parent()._coerce_base_ring(cut_frac)) 

 

def barycentric_subdivision(self, subdivision_frac=None): 

r""" 

Return the barycentric subdivision of a compact polyhedron. 

 

DEFINITION: 

 

The barycentric subdivision of a compact polyhedron is a standard way 

to triangulate its faces in such a way that maximal faces correspond to 

flags of faces of the starting polyhedron (i.e. a maximal chain in the 

face lattice of the polyhedron). As a simplicial complex, this is known 

as the order complex of the face lattice of the polyhedron. 

 

REFERENCE: 

 

See :wikipedia:`Barycentric_subdivision` 

Section 6.6, Handbook of Convex Geometry, Volume A, edited by P.M. Gruber and J.M. 

Wills. 1993, North-Holland Publishing Co.. 

 

INPUT: 

 

- ``subdivision_frac`` -- number. Gives the proportion how far the new 

vertices are pulled out of the polytope. Default is `\frac{1}{3}` and 

the value should be smaller than `\frac{1}{2}`. The subdivision is 

computed on the polar polyhedron. 

 

OUTPUT: 

 

A Polyhedron object, subdivided as described above. 

 

EXAMPLES:: 

 

sage: P = polytopes.hypercube(3) 

sage: P.barycentric_subdivision() 

A 3-dimensional polyhedron in QQ^3 defined as the convex hull 

of 26 vertices 

sage: P = Polyhedron(vertices=[[0,0,0],[0,1,0],[1,0,0],[0,0,1]]) 

sage: P.barycentric_subdivision() 

A 3-dimensional polyhedron in QQ^3 defined as the convex hull 

of 14 vertices 

sage: P = Polyhedron(vertices=[[0,1,0],[0,0,1],[1,0,0]]) 

sage: P.barycentric_subdivision() 

A 2-dimensional polyhedron in QQ^3 defined as the convex hull 

of 6 vertices 

sage: P = polytopes.regular_polygon(4, base_ring=QQ) 

sage: P.barycentric_subdivision() 

A 2-dimensional polyhedron in QQ^2 defined as the convex hull of 8 

vertices 

 

TESTS:: 

 

sage: P.barycentric_subdivision(1/2) 

Traceback (most recent call last): 

... 

ValueError: The subdivision fraction should be between 0 and 1/2. 

sage: P = Polyhedron(ieqs=[[1,0,1],[0,1,0],[1,0,0],[0,0,1]]) 

sage: P.barycentric_subdivision() 

Traceback (most recent call last): 

... 

ValueError: The polytope has to be compact. 

sage: P = Polyhedron(vertices=[[0,0,0],[0,1,0],[1,0,0],[0,0,1]], backend='field') 

sage: P.barycentric_subdivision() 

A 3-dimensional polyhedron in QQ^3 defined as the convex hull of 14 vertices 

""" 

if subdivision_frac is None: 

subdivision_frac = ZZ.one() / 3 

 

if not self.is_compact(): 

raise ValueError("The polytope has to be compact.") 

if not (0 < subdivision_frac < ZZ.one() / 2): 

raise ValueError("The subdivision fraction should be " 

"between 0 and 1/2.") 

 

b_ring = self.parent()._coerce_base_ring(subdivision_frac) 

barycenter = self.center() 

 

ambient_dim = self.ambient_dim() 

polytope_dim = self.dimension() 

 

if ambient_dim != polytope_dim: 

start_polar = Polyhedron((self - barycenter).polar().vertices()) 

polar = Polyhedron((self - barycenter).polar().vertices()) 

else: 

start_polar = (self - barycenter).polar() 

polar = (self - barycenter).polar() 

 

for i in range(self.dimension() - 1): 

 

new_ineq = [] 

subdivided_faces = list(start_polar.faces(i)) 

Hrep = polar.Hrepresentation() 

 

for face in subdivided_faces: 

 

face_vertices = face.vertices() 

normal_vectors = [] 

 

for facet in Hrep: 

if all(facet.contains(v) and not facet.interior_contains(v) 

for v in face_vertices): 

# The facet contains the face 

normal_vectors.append(facet.A()) 

 

normal_vector = sum(normal_vectors) 

B = - normal_vector * (face_vertices[0].vector()) 

linear_evaluation = set([-normal_vector * (v.vector()) 

for v in polar.vertices()]) 

 

if B == max(linear_evaluation): 

C = max(linear_evaluation.difference(set([B]))) 

else: 

C = min(linear_evaluation.difference(set([B]))) 

 

ineq_vector = [(1 - subdivision_frac) * B + subdivision_frac * C] + list(normal_vector) 

new_ineq += [ineq_vector] 

 

new_ieqs = polar.inequalities_list() + new_ineq 

new_eqns = polar.equations_list() 

 

polar = Polyhedron(ieqs=new_ieqs, eqns=new_eqns, 

base_ring=b_ring) 

 

if ambient_dim != polytope_dim: 

return (Polyhedron(polar.polar().vertices())) + barycenter 

else: 

return (polar.polar()) + barycenter 

 

def _make_polyhedron_face(self, Vindices, Hindices): 

""" 

Construct a face of the polyhedron. 

 

INPUT: 

 

- ``Vindices`` -- a tuple of integers. The indices of the 

V-representation objects that span the face. 

 

- ``Hindices`` -- a tuple of integers. The indices of the 

H-representation objects that hold as equalities on the 

face. 

 

OUTPUT: 

 

A new :class:`~sage.geometry.polyhedron.face.PolyhedronFace` instance. It is not checked 

whether the input data actually defines a face. 

 

EXAMPLES:: 

 

sage: square = polytopes.hypercube(2) 

sage: square._make_polyhedron_face((0,2), (1,)) 

<0,2> 

""" 

from sage.geometry.polyhedron.face import PolyhedronFace 

return PolyhedronFace(self, Vindices, Hindices) 

 

@cached_method 

def face_lattice(self): 

""" 

Return the face-lattice poset. 

 

OUTPUT: 

 

A :class:`~sage.combinat.posets.posets.FinitePoset`. Elements 

are given as 

:class:`~sage.geometry.polyhedron.face.PolyhedronFace`. 

 

In the case of a full-dimensional polytope, the faces are 

pairs (vertices, inequalities) of the spanning vertices and 

corresponding saturated inequalities. In general, a face is 

defined by a pair (V-rep. objects, H-rep. objects). The 

V-representation objects span the face, and the corresponding 

H-representation objects are those inequalities and equations 

that are saturated on the face. 

 

The bottom-most element of the face lattice is the "empty 

face". It contains no V-representation object. All 

H-representation objects are incident. 

 

The top-most element is the "full face". It is spanned by all 

V-representation objects. The incident H-representation 

objects are all equations and no inequalities. 

 

In the case of a full-dimensional polytope, the "empty face" 

and the "full face" are the empty set (no vertices, all 

inequalities) and the full polytope (all vertices, no 

inequalities), respectively. 

 

ALGORITHM: 

 

For a full-dimensional polytope, the basic algorithm is 

described in 

:func:`~sage.geometry.hasse_diagram.Hasse_diagram_from_incidences`. 

There are three generalizations of [KP2002]_ necessary to deal 

with more general polytopes, corresponding to the extra 

H/V-representation objects: 

 

* Lines are removed before calling 

:func:`Hasse_diagram_from_incidences`, and then added back 

to each face V-representation except for the "empty face". 

 

* Equations are removed before calling 

:func:`Hasse_diagram_from_incidences`, and then added back 

to each face H-representation. 

 

* Rays: Consider the half line as an example. The 

V-representation objects are a point and a ray, which we can 

think of as a point at infinity. However, the point at 

infinity has no inequality associated to it, so there is 

only one H-representation object alltogether. The face 

lattice does not contain the "face at infinity". This means 

that in :func:`Hasse_diagram_from_incidences`, one needs to 

drop faces with V-representations that have no matching 

H-representation. In addition, one needs to ensure that 

every non-empty face contains at least one vertex. 

 

EXAMPLES:: 

 

sage: square = polytopes.hypercube(2) 

sage: square.face_lattice() 

Finite poset containing 10 elements with distinguished linear extension 

sage: list(_) 

[<>, <0>, <1>, <2>, <3>, <0,1>, <0,2>, <2,3>, <1,3>, <0,1,2,3>] 

sage: poset_element = _[6] 

sage: a_face = poset_element 

sage: a_face 

<0,2> 

sage: a_face.dim() 

1 

sage: set(a_face.ambient_Vrepresentation()) == \ 

....: set([square.Vrepresentation(0), square.Vrepresentation(2)]) 

True 

sage: a_face.ambient_Vrepresentation() 

(A vertex at (-1, -1), A vertex at (1, -1)) 

sage: a_face.ambient_Hrepresentation() 

(An inequality (0, 1) x + 1 >= 0,) 

 

A more complicated example:: 

 

sage: c5_10 = Polyhedron(vertices = [[i,i^2,i^3,i^4,i^5] for i in range(1,11)]) 

sage: c5_10_fl = c5_10.face_lattice() 

sage: [len(x) for x in c5_10_fl.level_sets()] 

[1, 10, 45, 100, 105, 42, 1] 

 

Note that if the polyhedron contains lines then there is a 

dimension gap between the empty face and the first non-empty 

face in the face lattice:: 

 

sage: line = Polyhedron(vertices=[(0,)], lines=[(1,)]) 

sage: [ fl.dim() for fl in line.face_lattice() ] 

[-1, 1] 

 

TESTS:: 

 

sage: c5_20 = Polyhedron(vertices = [[i,i^2,i^3,i^4,i^5] 

....: for i in range(1,21)]) 

sage: c5_20_fl = c5_20.face_lattice() # long time 

sage: [len(x) for x in c5_20_fl.level_sets()] # long time 

[1, 20, 190, 580, 680, 272, 1] 

sage: polytopes.hypercube(2).face_lattice().plot() 

Graphics object consisting of 27 graphics primitives 

sage: level_sets = polytopes.cross_polytope(2).face_lattice().level_sets() 

sage: level_sets[0], level_sets[-1] 

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

 

Various degenerate polyhedra:: 

 

sage: Polyhedron(vertices=[[0,0,0],[1,0,0],[0,1,0]]).face_lattice().level_sets() 

[[<>], [<0>, <1>, <2>], [<0,1>, <0,2>, <1,2>], [<0,1,2>]] 

sage: Polyhedron(vertices=[(1,0,0),(0,1,0)], rays=[(0,0,1)]).face_lattice().level_sets() 

[[<>], [<1>, <2>], [<0,1>, <0,2>, <1,2>], [<0,1,2>]] 

sage: Polyhedron(rays=[(1,0,0),(0,1,0)], vertices=[(0,0,1)]).face_lattice().level_sets() 

[[<>], [<0>], [<0,1>, <0,2>], [<0,1,2>]] 

sage: Polyhedron(rays=[(1,0),(0,1)], vertices=[(0,0)]).face_lattice().level_sets() 

[[<>], [<0>], [<0,1>, <0,2>], [<0,1,2>]] 

sage: Polyhedron(vertices=[(1,),(0,)]).face_lattice().level_sets() 

[[<>], [<0>, <1>], [<0,1>]] 

sage: Polyhedron(vertices=[(1,0,0),(0,1,0)], lines=[(0,0,1)]).face_lattice().level_sets() 

[[<>], [<0,1>, <0,2>], [<0,1,2>]] 

sage: Polyhedron(lines=[(1,0,0)], vertices=[(0,0,1)]).face_lattice().level_sets() 

[[<>], [<0,1>]] 

sage: Polyhedron(lines=[(1,0),(0,1)], vertices=[(0,0)]).face_lattice().level_sets() 

[[<>], [<0,1,2>]] 

sage: Polyhedron(lines=[(1,0)], rays=[(0,1)], vertices=[(0,0)])\ 

....: .face_lattice().level_sets() 

[[<>], [<0,1>], [<0,1,2>]] 

sage: Polyhedron(vertices=[(0,)], lines=[(1,)]).face_lattice().level_sets() 

[[<>], [<0,1>]] 

sage: Polyhedron(lines=[(1,0)], vertices=[(0,0)]).face_lattice().level_sets() 

[[<>], [<0,1>]] 

""" 

coatom_to_Hindex = [ h.index() for h in self.inequality_generator() ] 

Hindex_to_coatom = [None] * self.n_Hrepresentation() 

for i in range(len(coatom_to_Hindex)): 

Hindex_to_coatom[ coatom_to_Hindex[i] ] = i 

 

atom_to_Vindex = [ v.index() for v in self.Vrep_generator() if not v.is_line() ] 

Vindex_to_atom = [None] * self.n_Vrepresentation() 

for i in range(len(atom_to_Vindex)): 

Vindex_to_atom[ atom_to_Vindex[i] ] = i 

 

atoms_incidences = [ tuple([ Hindex_to_coatom[h.index()] 

for h in v.incident() if h.is_inequality() ]) 

for v in self.Vrepresentation() if not v.is_line() ] 

 

coatoms_incidences = [ tuple([ Vindex_to_atom[v.index()] 

for v in h.incident() if not v.is_line() ]) 

for h in self.Hrepresentation() if h.is_inequality() ] 

 

atoms_vertices = [ Vindex_to_atom[v.index()] for v in self.vertex_generator() ] 

equations = [ e.index() for e in self.equation_generator() ] 

lines = [ l.index() for l in self.line_generator() ] 

 

def face_constructor(atoms, coatoms): 

if len(atoms) == 0: 

Vindices = () 

else: 

Vindices = tuple(sorted([ atom_to_Vindex[i] for i in atoms ]+lines)) 

Hindices = tuple(sorted([ coatom_to_Hindex[i] for i in coatoms ]+equations)) 

return self._make_polyhedron_face(Vindices, Hindices) 

 

from sage.geometry.hasse_diagram import Hasse_diagram_from_incidences 

return Hasse_diagram_from_incidences\ 

(atoms_incidences, coatoms_incidences, 

face_constructor=face_constructor, required_atoms=atoms_vertices) 

 

def faces(self, face_dimension): 

""" 

Return the faces of given dimension 

 

INPUT: 

 

- ``face_dimension`` -- integer. The dimension of the faces 

whose representation will be returned. 

 

OUTPUT: 

 

A tuple of 

:class:`~sage.geometry.polyhedron.face.PolyhedronFace`. See 

:mod:`~sage.geometry.polyhedron.face` for details. The order 

random but fixed. 

 

EXAMPLES: 

 

Here we find the vertex and face indices of the eight three-dimensional 

facets of the four-dimensional hypercube:: 

 

sage: p = polytopes.hypercube(4) 

sage: p.faces(3) 

(<0,1,2,3,4,5,6,7>, <0,1,2,3,8,9,10,11>, <0,1,4,5,8,9,12,13>, 

<0,2,4,6,8,10,12,14>, <2,3,6,7,10,11,14,15>, <8,9,10,11,12,13,14,15>, 

<4,5,6,7,12,13,14,15>, <1,3,5,7,9,11,13,15>) 

 

sage: face = p.faces(3)[0] 

sage: face.ambient_Hrepresentation() 

(An inequality (1, 0, 0, 0) x + 1 >= 0,) 

sage: face.vertices() 

(A vertex at (-1, -1, -1, -1), A vertex at (-1, -1, -1, 1), 

A vertex at (-1, -1, 1, -1), A vertex at (-1, -1, 1, 1), 

A vertex at (-1, 1, -1, -1), A vertex at (-1, 1, -1, 1), 

A vertex at (-1, 1, 1, -1), A vertex at (-1, 1, 1, 1)) 

 

You can use the 

:meth:`~sage.geometry.polyhedron.representation.PolyhedronRepresentation.index` 

method to enumerate vertices and inequalities:: 

 

sage: def get_idx(rep): return rep.index() 

sage: [get_idx(_) for _ in face.ambient_Hrepresentation()] 

[4] 

sage: [get_idx(_) for _ in face.ambient_Vrepresentation()] 

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

 

sage: [ ([get_idx(_) for _ in face.ambient_Vrepresentation()], 

....: [get_idx(_) for _ in face.ambient_Hrepresentation()]) 

....: for face in p.faces(3) ] 

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

([0, 1, 2, 3, 8, 9, 10, 11], [5]), 

([0, 1, 4, 5, 8, 9, 12, 13], [6]), 

([0, 2, 4, 6, 8, 10, 12, 14], [7]), 

([2, 3, 6, 7, 10, 11, 14, 15], [2]), 

([8, 9, 10, 11, 12, 13, 14, 15], [0]), 

([4, 5, 6, 7, 12, 13, 14, 15], [1]), 

([1, 3, 5, 7, 9, 11, 13, 15], [3])] 

 

TESTS:: 

 

sage: pr = Polyhedron(rays = [[1,0,0],[-1,0,0],[0,1,0]], vertices = [[-1,-1,-1]], lines=[(0,0,1)]) 

sage: pr.faces(4) 

() 

sage: pr.faces(3) 

(<0,1,2,3>,) 

sage: pr.faces(2) 

(<0,1,2>,) 

sage: pr.faces(1) 

() 

sage: pr.faces(0) 

() 

sage: pr.faces(-1) 

() 

""" 

fl = self.face_lattice().level_sets() 

codim = self.dim() - face_dimension 

index = len(fl) - 1 - codim 

if index >= len(fl) or index < 1: 

return tuple() 

return tuple(fl[index]) 

 

@cached_method 

def f_vector(self): 

r""" 

Return the f-vector. 

 

OUTPUT: 

 

Returns a vector whose ``i``-th entry is the number of 

``i``-dimensional faces of the polytope. 

 

EXAMPLES:: 

 

sage: p = Polyhedron(vertices=[[1, 2, 3], [1, 3, 2], 

....: [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1], [0, 0, 0]]) 

sage: p.f_vector() 

(1, 7, 12, 7, 1) 

""" 

return vector(ZZ, [len(x) for x in self.face_lattice().level_sets()]) 

 

def vertex_graph(self): 

""" 

Return a graph in which the vertices correspond to vertices 

of the polyhedron, and edges to edges. 

 

EXAMPLES:: 

 

sage: g3 = polytopes.hypercube(3).vertex_graph(); g3 

Graph on 8 vertices 

sage: g3.automorphism_group().cardinality() 

48 

sage: s4 = polytopes.simplex(4).vertex_graph(); s4 

Graph on 5 vertices 

sage: s4.is_eulerian() 

True 

""" 

from itertools import combinations 

inequalities = self.inequalities() 

vertices = self.vertices() 

 

# Associated to 'v' the inequalities in contact with v 

vertex_ineq_incidence = [frozenset([i for i,ineq in enumerate(inequalities) if self._is_zero(ineq.eval(v))]) 

for i,v in enumerate(vertices)] 

 

# the dual incidence structure 

ineq_vertex_incidence = [set() for _ in range(len(inequalities))] 

for v,ineq_list in enumerate(vertex_ineq_incidence): 

for ineq in ineq_list: 

ineq_vertex_incidence[ineq].add(v) 

 

n = len(vertices) 

 

pairs = [] 

for i,j in combinations(range(n),2): 

common_ineq = vertex_ineq_incidence[i]&vertex_ineq_incidence[j] 

if not common_ineq: # or len(common_ineq) < d-2: 

continue 

 

if len(set.intersection(*[ineq_vertex_incidence[k] for k in common_ineq])) == 2: 

pairs.append((i,j)) 

 

from sage.graphs.graph import Graph 

g = Graph() 

g.add_vertices(vertices) 

g.add_edges((vertices[i], vertices[j]) for i, j in pairs) 

return g 

 

graph = vertex_graph 

 

def vertex_digraph(self, f, increasing=True): 

""" 

Return the directed graph of the polyhedron according to a linear form. 

 

The underlying undirected graph is the graph of vertices and edges. 

 

INPUT: 

 

- ``f`` -- a linear form. The linear form can be provided as: 

 

- a vector space morphism with one-dimensional codomain, (see 

:meth:`sage.modules.vector_space_morphism.linear_transformation` 

and 

:class:`sage.modules.vector_space_morphism.VectorSpaceMorphism`) 

- a vector ; in this case the linear form is obtained by duality 

using the dot product: ``f(v) = v.dot_product(f)``. 

 

- ``increasing`` -- boolean (default ``True``) whether to orient 

edges in the increasing or decreasing direction. 

 

By default, an edge is oriented from `v` to `w` if 

`f(v) \leq f(w)`. 

 

If `f(v)=f(w)`, then two opposite edges are created. 

 

EXAMPLES:: 

 

sage: penta = Polyhedron([[0,0],[1,0],[0,1],[1,2],[3,2]]) 

sage: G = penta.vertex_digraph(vector([1,1])); G 

Digraph on 5 vertices 

sage: G.sinks() 

[A vertex at (3, 2)] 

 

sage: A = matrix(ZZ, [[1], [-1]]) 

sage: f = linear_transformation(A) 

sage: G = penta.vertex_digraph(f) ; G 

Digraph on 5 vertices 

sage: G.is_directed_acyclic() 

False 

 

.. SEEALSO:: 

 

:meth:`vertex_graph` 

""" 

from sage.modules.vector_space_morphism import VectorSpaceMorphism 

if isinstance(f, VectorSpaceMorphism): 

if f.codomain().dimension() == 1: 

orientation_check = lambda v: f(v) >= 0 

else: 

raise TypeError('The linear map f must have ' 

'one-dimensional codomain') 

else: 

try: 

if f.is_vector(): 

orientation_check = lambda v: v.dot_product(f) >= 0 

else: 

raise TypeError('f must be a linear map or a vector') 

except AttributeError: 

raise TypeError('f must be a linear map or a vector') 

if not increasing: 

f = -f 

from sage.graphs.digraph import DiGraph 

dg = DiGraph() 

for j in range(self.n_vertices()): 

vj = self.Vrepresentation(j) 

for vi in vj.neighbors(): 

if orientation_check(vj.vector() - vi.vector()): 

dg.add_edge(vi, vj) 

return dg 

 

def polar(self): 

""" 

Return the polar (dual) polytope. 

 

The original vertices are translated so that their barycenter 

is at the origin, and then the vertices are used as the 

coefficients in the polar inequalities. 

 

EXAMPLES:: 

 

sage: p = Polyhedron(vertices = [[0,0,1],[0,1,0],[1,0,0],[0,0,0],[1,1,1]], base_ring=QQ) 

sage: p 

A 3-dimensional polyhedron in QQ^3 defined as the convex hull of 5 vertices 

sage: p.polar() 

A 3-dimensional polyhedron in QQ^3 defined as the convex hull of 6 vertices 

 

sage: cube = polytopes.hypercube(3) 

sage: octahedron = polytopes.cross_polytope(3) 

sage: cube_dual = cube.polar() 

sage: octahedron == cube_dual 

True 

""" 

assert self.is_compact(), "Not a polytope." 

 

verts = [list(self.center() - v.vector()) for v in self.vertex_generator()] 

base_ring = self.parent()._coerce_base_ring(self.center().parent()) 

return Polyhedron(ieqs=[[1] + list(v) for v in verts], base_ring=base_ring) 

 

def pyramid(self): 

""" 

Returns a polyhedron that is a pyramid over the original. 

 

EXAMPLES:: 

 

sage: square = polytopes.hypercube(2); square 

A 2-dimensional polyhedron in ZZ^2 defined as the convex hull of 4 vertices 

sage: egyptian_pyramid = square.pyramid(); egyptian_pyramid 

A 3-dimensional polyhedron in QQ^3 defined as the convex hull of 5 vertices 

sage: egyptian_pyramid.n_vertices() 

5 

sage: for v in egyptian_pyramid.vertex_generator(): print(v) 

A vertex at (0, -1, -1) 

A vertex at (0, -1, 1) 

A vertex at (0, 1, -1) 

A vertex at (0, 1, 1) 

A vertex at (1, 0, 0) 

""" 

new_verts = \ 

[[0] + x for x in self.Vrep_generator()] + \ 

[[1] + list(self.center())] 

return Polyhedron(vertices=new_verts) 

 

def bipyramid(self): 

""" 

Return a polyhedron that is a bipyramid over the original. 

 

EXAMPLES:: 

 

sage: octahedron = polytopes.cross_polytope(3) 

sage: cross_poly_4d = octahedron.bipyramid() 

sage: cross_poly_4d.n_vertices() 

8 

sage: q = [list(v) for v in cross_poly_4d.vertex_generator()] 

sage: q 

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

[0, -1, 0, 0], 

[0, 0, -1, 0], 

[0, 0, 0, -1], 

[0, 0, 0, 1], 

[0, 0, 1, 0], 

[0, 1, 0, 0], 

[1, 0, 0, 0]] 

 

Now check that bipyramids of cross-polytopes are cross-polytopes:: 

 

sage: q2 = [list(v) for v in polytopes.cross_polytope(4).vertex_generator()] 

sage: [v in q2 for v in q] 

[True, True, True, True, True, True, True, True] 

""" 

new_verts = \ 

[[ 0] + list(x) for x in self.vertex_generator()] + \ 

[[ 1] + list(self.center())] + \ 

[[-1] + list(self.center())] 

new_rays = [[0] + r for r in self.rays()] 

new_lines = [[0] + list(l) for l in self.lines()] 

return Polyhedron(vertices=new_verts, rays=new_rays, lines=new_lines) 

 

def prism(self): 

""" 

Return a prism of the original polyhedron. 

 

EXAMPLES:: 

 

sage: square = polytopes.hypercube(2) 

sage: cube = square.prism() 

sage: cube 

A 3-dimensional polyhedron in ZZ^3 defined as the convex hull of 8 vertices 

sage: hypercube = cube.prism() 

sage: hypercube.n_vertices() 

16 

""" 

new_verts = [] 

new_verts.extend( [ [0] + v for v in self.vertices()] ) 

new_verts.extend( [ [1] + v for v in self.vertices()] ) 

new_rays = [ [0] + r for r in self.rays()] 

new_lines = [ [0] + l for l in self.lines()] 

return Polyhedron(vertices=new_verts, rays=new_rays, lines=new_lines, 

base_ring=self.base_ring()) 

 

def projection(self): 

""" 

Return a projection object. 

 

See also 

:meth:`~sage.geometry.polyhedron.base.Polyhedron_base.schlegel_projection` 

for a more interesting projection. 

 

OUTPUT: 

 

The identity projection. This is useful for plotting 

polyhedra. 

 

EXAMPLES:: 

 

sage: p = polytopes.hypercube(3) 

sage: proj = p.projection() 

sage: proj 

The projection of a polyhedron into 3 dimensions 

""" 

from .plot import Projection 

self.projection = Projection(self) 

return self.projection 

 

def render_solid(self, **kwds): 

""" 

Return a solid rendering of a 2- or 3-d polytope. 

 

EXAMPLES:: 

 

sage: p = polytopes.hypercube(3) 

sage: p_solid = p.render_solid(opacity = .7) 

sage: type(p_solid) 

<type 'sage.plot.plot3d.index_face_set.IndexFaceSet'> 

""" 

proj = self.projection() 

if self.ambient_dim() == 3: 

return proj.render_solid_3d(**kwds) 

if self.ambient_dim() == 2: 

return proj.render_fill_2d(**kwds) 

raise ValueError("render_solid is only defined for 2 and 3 dimensional polyhedra.") 

 

def render_wireframe(self, **kwds): 

""" 

For polytopes in 2 or 3 dimensions, return the edges 

as a list of lines. 

 

EXAMPLES:: 

 

sage: p = Polyhedron([[1,2,],[1,1],[0,0]]) 

sage: p_wireframe = p.render_wireframe() 

sage: p_wireframe._objects 

[Line defined by 2 points, Line defined by 2 points, Line defined by 2 points] 

""" 

proj = self.projection() 

if self.ambient_dim() == 3: 

return proj.render_wireframe_3d(**kwds) 

if self.ambient_dim() == 2: 

return proj.render_outline_2d(**kwds) 

raise ValueError("render_wireframe is only defined for 2 and 3 dimensional polyhedra.") 

 

def schlegel_projection(self, projection_dir=None, height=1.1): 

""" 

Return the Schlegel projection. 

 

* The polyhedron is translated such that its 

:meth:`~sage.geometry.polyhedron.base.Polyhedron_base.center` 

is at the origin. 

 

* The vertices are then normalized to the unit sphere 

 

* The normalized points are stereographically projected from a 

point slightly outside of the sphere. 

 

INPUT: 

 

- ``projection_direction`` -- coordinate list/tuple/iterable 

or ``None`` (default). The direction of the Schlegel 

projection. For a full-dimensional polyhedron, the default 

is the first facet normal; Otherwise, the vector consisting 

of the first n primes is chosen. 

 

- ``height`` -- float (default: `1.1`). How far outside of the 

unit sphere the focal point is. 

 

OUTPUT: 

 

A :class:`~sage.geometry.polyhedron.plot.Projection` object. 

 

EXAMPLES:: 

 

sage: p = polytopes.hypercube(3) 

sage: sch_proj = p.schlegel_projection() 

sage: schlegel_edge_indices = sch_proj.lines 

sage: schlegel_edges = [sch_proj.coordinates_of(x) for x in schlegel_edge_indices] 

sage: len([x for x in schlegel_edges if x[0][0] > 0]) 

4 

""" 

proj = self.projection() 

if projection_dir is None: 

vertices = self.vertices() 

facet = self.Hrepresentation(0) 

f0 = [ v.index() for v in facet.incident() ] 

projection_dir = [sum([vertices[f0[i]][j]/len(f0) for i in range(len(f0))]) 

for j in range(self.ambient_dim())] 

return proj.schlegel(projection_direction=projection_dir, height=height) 

 

def _volume_lrs(self, verbose=False): 

""" 

Computes the volume of a polytope using lrs. 

 

OUTPUT: 

 

The volume, cast to RDF (although lrs seems to output a 

rational value this must be an approximation in some cases). 

 

EXAMPLES:: 

 

sage: polytopes.hypercube(3)._volume_lrs() #optional - lrslib 

8.0 

sage: (polytopes.hypercube(3)*2)._volume_lrs() #optional - lrslib 

64.0 

sage: polytopes.twenty_four_cell()._volume_lrs() #optional - lrslib 

2.0 

 

REFERENCES: 

 

David Avis's lrs program. 

""" 

if not is_package_installed('lrslib'): 

raise PackageNotFoundError('lrslib') 

 

from sage.misc.temporary_file import tmp_filename 

from subprocess import Popen, PIPE 

in_str = self.cdd_Vrepresentation() 

in_str += 'volume' 

in_filename = tmp_filename() 

in_file = open(in_filename, 'w') 

in_file.write(in_str) 

in_file.close() 

if verbose: print(in_str) 

 

lrs_procs = Popen(['lrs', in_filename], 

stdin=PIPE, stdout=PIPE, stderr=PIPE) 

ans, err = lrs_procs.communicate() 

if verbose: 

print(ans) 

# FIXME: check err 

 

for a_line in ans.splitlines(): 

if 'Volume=' in a_line: 

volume = a_line.split('Volume=')[1] 

volume = RDF(QQ(volume)) 

return volume 

 

raise ValueError("lrs did not return a volume") 

 

def _volume_latte(self, verbose=False, algorithm='triangulate', **kwargs): 

""" 

Computes the volume of a polytope using LattE integrale. 

 

INPUT: 

 

- ``arg`` -- a cdd or LattE description string. 

 

- ``algorithm`` -- (default: 'triangulate') the integration method. Use 'triangulate' for 

polytope triangulation or 'cone-decompose' for tangent cone decomposition method. 

 

- ``raw_output`` -- if ``True`` then return directly the output string from LattE. 

 

- ``verbose`` -- if ``True`` then return directly verbose output from LattE. 

 

- For all other options, consult the LattE manual. 

 

OUTPUT: 

 

A rational value, or a string if ``raw_output`` if set to ``True``. 

 

.. NOTE:: 

 

This function depends on LattE (i.e., the ``latte_int`` optional 

package). See the LattE documentation for furthe details. 

 

EXAMPLES:: 

 

sage: polytopes.hypercube(3)._volume_latte() #optional - latte_int 

8 

sage: (polytopes.hypercube(3)*2)._volume_latte() #optional - latte_int 

64 

sage: polytopes.twenty_four_cell()._volume_latte() #optional - latte_int 

2 

sage: polytopes.cuboctahedron()._volume_latte() #optional - latte_int 

20/3 

 

TESTS:: 

 

Testing triangulate algorithm:: 

 

sage: polytopes.cuboctahedron()._volume_latte(algorithm='triangulate') #optional - latte_int 

20/3 

 

Testing cone decomposition algorithm:: 

 

sage: polytopes.cuboctahedron()._volume_latte(algorithm='cone-decompose') #optional - latte_int 

20/3 

 

Testing raw output:: 

 

sage: polytopes.cuboctahedron()._volume_latte(raw_output=True) #optional - latte_int 

'20/3' 

""" 

if is_package_installed('latte_int'): 

from sage.interfaces.latte import integrate 

if self.base_ring() == RDF: 

raise ValueError("LattE integrale cannot be applied over inexact rings.") 

else: 

return integrate(self.cdd_Hrepresentation(), algorithm=algorithm, cdd=True, verbose=verbose, **kwargs) 

 

else: 

raise PackageNotFoundError('latte_int') 

 

@cached_method 

def volume(self, measure='ambient', engine='auto', **kwds): 

""" 

Return the volume of the polytope. 

 

INPUT: 

 

- ``measure`` -- string. The measure to use. Allowed values are: 

 

* ``ambient`` (default): Lebesgue measure of ambient space (volume) 

* ``induced``: Lebesgue measure of the affine hull (relative volume) 

* ``induced_rational``: Scaling of the Lebesgue measure for rational polytopes 

 

- ``engine`` -- string. The backend to use. Allowed values are: 

 

* ``'auto'`` (default): choose engine according to measure 

* ``'internal'``: see :meth:`triangulate`. 

* ``'TOPCOM'``: see :meth:`triangulate`. 

* ``'lrs'``: use David Avis's lrs program (optional). 

* ``'latte'``: use LattE integrale program (optional). 

 

- ``**kwds`` -- keyword arguments that are passed to the 

triangulation engine. 

 

OUTPUT: 

 

The volume of the polytope. 

 

EXAMPLES:: 

 

sage: polytopes.hypercube(3).volume() 

8 

sage: (polytopes.hypercube(3)*2).volume() 

64 

sage: polytopes.twenty_four_cell().volume() 

2 

 

Volume of the same polytopes, using the optional package lrslib 

(which requires a rational polytope). For mysterious historical 

reasons, Sage casts lrs's exact answer to a float:: 

 

sage: I3 = polytopes.hypercube(3) 

sage: I3.volume(engine='lrs') #optional - lrslib 

8.0 

sage: C24 = polytopes.twenty_four_cell() 

sage: C24.volume(engine='lrs') #optional - lrslib 

2.0 

 

If the base ring is exact, the answer is exact:: 

 

sage: P5 = polytopes.regular_polygon(5) 

sage: P5.volume() 

2.377641290737884? 

 

sage: polytopes.icosahedron().volume() 

5/12*sqrt5 + 5/4 

sage: numerical_approx(_) # abs tol 1e9 

2.18169499062491 

 

When considering lower-dimensional polytopes, we can ask for the 

ambient (full-dimensional), the induced measure (of the affine 

hull) or, in the case of lattice polytopes, for the induced rational measure. 

This is controlled by the parameter `measure`. Different engines 

may have different ideas on the definition of volume of a 

lower-dimensional object:: 

 

sage: P = Polyhedron([[0, 0], [1, 1]]) 

sage: P.volume() 

0 

sage: P.volume(measure='induced') 

sqrt(2) 

sage: P.volume(measure='induced_rational') # optional -- latte_int 

1 

 

sage: S = polytopes.regular_polygon(6); S 

A 2-dimensional polyhedron in AA^2 defined as the convex hull of 6 vertices 

sage: edge = S.faces(1)[2].as_polyhedron() 

sage: edge.vertices() 

(A vertex at (0.866025403784439?, 1/2), A vertex at (0, 1)) 

sage: edge.volume() 

0 

sage: edge.volume(measure='induced') 

1 

 

sage: Dexact = polytopes.dodecahedron() 

sage: v = Dexact.faces(2)[0].as_polyhedron().volume(measure='induced', engine='internal'); v 

-80*(55*sqrt(5) - 123)/sqrt(-6368*sqrt(5) + 14240) 

sage: v = Dexact.faces(2)[4].as_polyhedron().volume(measure='induced', engine='internal'); v 

-80*(55*sqrt(5) - 123)/sqrt(-6368*sqrt(5) + 14240) 

sage: RDF(v) # abs tol 1e-9 

1.53406271079044 

 

sage: Dinexact = polytopes.dodecahedron(exact=False) 

sage: w = Dinexact.faces(2)[0].as_polyhedron().volume(measure='induced', engine='internal'); RDF(w) # abs tol 1e-9 

1.534062710738235 

 

sage: [polytopes.simplex(d).volume(measure='induced') for d in range(1,5)] == [sqrt(d+1)/factorial(d) for d in range(1,5)] 

True 

 

sage: I = Polyhedron([[-3, 0], [0, 9]]) 

sage: I.volume(measure='induced') 

3*sqrt(10) 

sage: I.volume(measure='induced_rational') # optional -- latte_int 

3 

 

sage: T = Polyhedron([[3, 0, 0], [0, 4, 0], [0, 0, 5]]) 

sage: T.volume(measure='induced') 

1/2*sqrt(769) 

sage: T.volume(measure='induced_rational') # optional -- latte_int 

1/2 

 

sage: Q = Polyhedron(vertices=[(0, 0, 1, 1), (0, 1, 1, 0), (1, 1, 0, 0)]) 

sage: Q.volume(measure='induced') 

1 

sage: Q.volume(measure='induced_rational') # optional -- latte_int 

1/2 

 

The volume of a full-dimensional unbounded polyhedron is infinity:: 

 

sage: P = Polyhedron(vertices = [[1, 0], [0, 1]], rays = [[1, 1]]) 

sage: P.volume() 

+Infinity 

 

The volume of a non full-dimensional unbounded polyhedron depends on the measure used:: 

 

sage: P = Polyhedron(ieqs = [[1,1,1],[-1,-1,-1],[3,1,0]]); P 

A 1-dimensional polyhedron in QQ^2 defined as the convex hull of 1 vertex and 1 ray 

sage: P.volume() 

0 

sage: P.volume(measure='induced') 

+Infinity 

sage: P.volume(measure='ambient') 

0 

sage: P.volume(measure='induced_rational') 

+Infinity 

""" 

if measure == 'induced_rational' and engine not in ['auto', 'latte']: 

raise TypeError("The induced rational measure can only be computed with the engine set to `auto` or `latte`") 

if engine == 'auto' and measure == 'induced_rational': 

engine = 'latte' 

 

if measure == 'ambient': 

if self.dim() < self.ambient_dim(): 

return self.base_ring().zero() 

if engine == 'lrs': 

return self._volume_lrs(**kwds) 

elif engine == 'latte': 

return self._volume_latte(**kwds) 

# if the polyhedron is unbounded, return infinity 

if not self.is_compact(): 

from sage.rings.infinity import infinity 

return infinity 

triangulation = self.triangulate(engine=engine, **kwds) 

pc = triangulation.point_configuration() 

return sum([pc.volume(simplex) for simplex in triangulation]) / ZZ(self.dim()).factorial() 

elif measure == 'induced': 

# if polyhedron is actually full-dimensional, return volume with ambient measure 

if self.dim() == self.ambient_dim(): 

return self.volume(measure='ambient', engine=engine, **kwds) 

# if the polyhedron is unbounded, return infinity 

if not self.is_compact(): 

from sage.rings.infinity import infinity 

return infinity 

# use an orthogonal transformation, which preserves volume up to a factor provided by the transformation matrix 

A, b = self.affine_hull(orthogonal=True, as_affine_map=True) 

Adet = (A.matrix().transpose() * A.matrix()).det() 

return self.affine_hull(orthogonal=True).volume(measure='ambient', engine=engine, **kwds) / sqrt(Adet) 

elif measure == 'induced_rational': 

if self.dim() < self.ambient_dim() and engine != 'latte': 

raise TypeError("The induced rational measure can only be computed with the engine set to `auto` or `latte`") 

# if the polyhedron is unbounded, return infinity 

if not self.is_compact(): 

from sage.rings.infinity import infinity 

return infinity 

return self._volume_latte(**kwds) 

 

def integrate(self, polynomial, **kwds): 

r""" 

Return the integral of a polynomial over a polytope. 

 

INPUT: 

 

- ``P`` -- Polyhedron. 

 

- ``polynomial`` -- A multivariate polynomial or a valid LattE description string for 

polynomials. 

 

- ``**kwds`` -- additional keyword arguments that are passed to the engine. 

 

OUTPUT: 

 

The integral of the polynomial over the polytope. 

 

.. NOTE:: 

 

The polytope triangulation algorithm is used. This function depends 

on LattE (i.e., the ``latte_int`` optional package). 

 

EXAMPLES:: 

 

sage: P = polytopes.cube() 

sage: x, y, z = polygens(QQ, 'x, y, z') 

sage: P.integrate(x^2*y^2*z^2) # optional - latte_int 

8/27 

 

If the polyhedron has floating point coordinates, an inexact result can 

be obtained if we transform to rational coordinates:: 

 

sage: P = 1.4142*polytopes.cube() 

sage: P_QQ = Polyhedron(vertices = [[QQ(vi) for vi in v] for v in P.vertex_generator()]) 

sage: RDF(P_QQ.integrate(x^2*y^2*z^2)) # optional - latte_int 

6.703841212195228 

 

Integral over a non full-dimensional polytope:: 

 

sage: x, y = polygens(QQ, 'x, y') 

sage: P = Polyhedron(vertices=[[0,0],[1,1]]) 

sage: P.integrate(x*y) # optional - latte_int 

Traceback (most recent call last): 

... 

NotImplementedError: The polytope must be full-dimensional. 

 

TESTS:: 

 

Testing a three-dimensional integral:: 

 

sage: P = polytopes.octahedron() 

sage: x, y, z = polygens(QQ, 'x, y, z') 

sage: P.integrate(2*x^2*y^4*z^6+z^2) # optional - latte_int 

630632/4729725 

 

Testing a polytope with non-rational vertices:: 

 

sage: P = polytopes.icosahedron() 

sage: P.integrate(x^2*y^2*z^2) # optional - latte_int 

Traceback (most recent call last): 

... 

TypeError: The base ring must be ZZ, QQ, or RDF 

 

Testing a univariate polynomial:: 

 

sage: P = Polyhedron(vertices=[[0],[1]]) 

sage: x = polygen(QQ, 'x') 

sage: P.integrate(x) # optional - latte_int 

1/2 

 

Testing a polytope with floating point coordinates:: 

 

sage: P = Polyhedron(vertices = [[0, 0], [1, 0], [1.1, 1.1], [0, 1]]) 

sage: P.integrate('[[1,[2,2]]]') # optional - latte_int 

Traceback (most recent call last): 

... 

TypeError: LattE integrale cannot be applied over inexact rings. 

""" 

if is_package_installed('latte_int'): 

from sage.interfaces.latte import integrate 

if self.base_ring() == RDF: 

raise TypeError("LattE integrale cannot be applied over inexact rings.") 

elif not self.is_full_dimensional(): 

raise NotImplementedError("The polytope must be full-dimensional.") 

else: 

return integrate(self.cdd_Hrepresentation(), polynomial, cdd=True) 

 

else: 

raise PackageNotFoundError('latte_int') 

 

def contains(self, point): 

""" 

Test whether the polyhedron contains the given ``point``. 

 

See also :meth:`interior_contains` and 

:meth:`relative_interior_contains`. 

 

INPUT: 

 

- ``point`` -- coordinates of a point (an iterable). 

 

OUTPUT: 

 

Boolean. 

 

EXAMPLES:: 

 

sage: P = Polyhedron(vertices=[[1,1],[1,-1],[0,0]]) 

sage: P.contains( [1,0] ) 

True 

sage: P.contains( P.center() ) # true for any convex set 

True 

 

As a shorthand, one may use the usual ``in`` operator:: 

 

sage: P.center() in P 

True 

sage: [-1,-1] in P 

False 

 

The point need not have coordinates in the same field as the 

polyhedron:: 

 

sage: ray = Polyhedron(vertices=[(0,0)], rays=[(1,0)], base_ring=QQ) 

sage: ray.contains([sqrt(2)/3,0]) # irrational coordinates are ok 

True 

sage: a = var('a') 

sage: ray.contains([a,0]) # a might be negative! 

False 

sage: assume(a>0) 

sage: ray.contains([a,0]) 

True 

sage: ray.contains(['hello', 'kitty']) # no common ring for coordinates 

False 

 

The empty polyhedron needs extra care, see :trac:`10238`:: 

 

sage: empty = Polyhedron(); empty 

The empty polyhedron in ZZ^0 

sage: empty.contains([]) 

False 

sage: empty.contains([0]) # not a point in QQ^0 

False 

sage: full = Polyhedron(vertices=[()]); full 

A 0-dimensional polyhedron in ZZ^0 defined as the convex hull of 1 vertex 

sage: full.contains([]) 

True 

sage: full.contains([0]) 

False 

""" 

try: 

p = vector(point) 

except TypeError: # point not iterable or no common ring for elements 

if len(point) > 0: 

return False 

else: 

p = vector(self.base_ring(), []) 

 

if len(p) != self.ambient_dim(): 

return False 

 

for H in self.Hrep_generator(): 

if not H.contains(p): 

return False 

return True 

 

__contains__ = contains 

 

def interior_contains(self, point): 

""" 

Test whether the interior of the polyhedron contains the 

given ``point``. 

 

See also :meth:`contains` and 

:meth:`relative_interior_contains`. 

 

INPUT: 

 

- ``point`` -- coordinates of a point. 

 

OUTPUT: 

 

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

 

EXAMPLES:: 

 

sage: P = Polyhedron(vertices=[[0,0],[1,1],[1,-1]]) 

sage: P.contains( [1,0] ) 

True 

sage: P.interior_contains( [1,0] ) 

False 

 

If the polyhedron is of strictly smaller dimension than the 

ambient space, its interior is empty:: 

 

sage: P = Polyhedron(vertices=[[0,1],[0,-1]]) 

sage: P.contains( [0,0] ) 

True 

sage: P.interior_contains( [0,0] ) 

False 

 

The empty polyhedron needs extra care, see :trac:`10238`:: 

 

sage: empty = Polyhedron(); empty 

The empty polyhedron in ZZ^0 

sage: empty.interior_contains([]) 

False 

""" 

try: 

p = vector(point) 

except TypeError: # point not iterable or no common ring for elements 

if len(point) > 0: 

return False 

else: 

p = vector(self.base_ring(), []) 

 

if len(p) != self.ambient_dim(): 

return False 

 

for H in self.Hrep_generator(): 

if not H.interior_contains(p): 

return False 

return True 

 

def relative_interior_contains(self, point): 

""" 

Test whether the relative interior of the polyhedron 

contains the given ``point``. 

 

See also :meth:`contains` and :meth:`interior_contains`. 

 

INPUT: 

 

- ``point`` -- coordinates of a point. 

 

OUTPUT: 

 

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

 

EXAMPLES:: 

 

sage: P = Polyhedron(vertices=[(1,0), (-1,0)]) 

sage: P.contains( (0,0) ) 

True 

sage: P.interior_contains( (0,0) ) 

False 

sage: P.relative_interior_contains( (0,0) ) 

True 

sage: P.relative_interior_contains( (1,0) ) 

False 

 

The empty polyhedron needs extra care, see :trac:`10238`:: 

 

sage: empty = Polyhedron(); empty 

The empty polyhedron in ZZ^0 

sage: empty.relative_interior_contains([]) 

False 

""" 

try: 

p = vector(point) 

except TypeError: # point not iterable or no common ring for elements 

if len(point) > 0: 

return False 

else: 

p = vector(self.base_ring(), []) 

 

if len(p) != self.ambient_dim(): 

return False 

 

for eq in self.equation_generator(): 

if not eq.contains(p): 

return False 

 

for ine in self.inequality_generator(): 

if not ine.interior_contains(p): 

return False 

 

return True 

 

def is_simplex(self): 

r""" 

Return whether the polyhedron is a simplex. 

 

EXAMPLES:: 

 

sage: Polyhedron([(0,0,0), (1,0,0), (0,1,0)]).is_simplex() 

True 

sage: polytopes.simplex(3).is_simplex() 

True 

sage: polytopes.hypercube(3).is_simplex() 

False 

""" 

return self.is_compact() and (self.dim()+1 == self.n_vertices()) 

 

def neighborliness(self): 

r""" 

Returns the largest ``k``, such that the polyhedron is ``k``-neighborly. 

 

In case of the ``d``-dimensional simplex, it returns ``d + 1``. 

 

See :wikipedia:`Neighborly_polytope` 

 

.. SEEALSO:: 

 

:meth:`is_neighborly` 

 

EXAMPLES:: 

 

sage: cube = polytopes.cube() 

sage: cube.neighborliness() 

1 

sage: P = Polyhedron(); P 

The empty polyhedron in ZZ^0 

sage: P.neighborliness() 

0 

sage: P = Polyhedron([[0]]); P 

A 0-dimensional polyhedron in ZZ^1 defined as the convex hull of 1 vertex 

sage: P.neighborliness() 

1 

sage: S = polytopes.simplex(5); S 

A 5-dimensional polyhedron in ZZ^6 defined as the convex hull of 6 vertices 

sage: S.neighborliness() 

6 

sage: C = polytopes.cyclic_polytope(7,10); C 

A 7-dimensional polyhedron in QQ^7 defined as the convex hull of 10 vertices 

sage: C.neighborliness() 

3 

sage: C = polytopes.cyclic_polytope(6,11); C 

A 6-dimensional polyhedron in QQ^6 defined as the convex hull of 11 vertices 

sage: C.neighborliness() 

3 

sage: [polytopes.cyclic_polytope(5,n).neighborliness() for n in range(6,10)] 

[6, 2, 2, 2] 

 

""" 

if self.is_simplex(): 

return self.dim() + 1 

else: 

k = 1 

while len(self.faces(k)) == binomial(self.n_vertices(), k + 1): 

k += 1 

return k 

 

def is_neighborly(self, k=None): 

r""" 

Return whether the polyhedron is neighborly. 

 

If the input ``k`` is provided then return whether the polyhedron is ``k``-neighborly 

 

See :wikipedia:`Neighborly_polytope` 

 

 

INPUT: 

 

- ``k`` -- the dimension up to which to check if every set of ``k`` 

vertices forms a face. If no ``k`` is provided, check up to floor 

of half the dimension of the polyhedron. 

 

OUTPUT: 

 

- ``True`` if the every set of up to ``k`` vertices forms a face, 

- ``False`` otherwise 

 

.. SEEALSO:: 

 

:meth:`neighborliness` 

 

EXAMPLES:: 

 

sage: cube = polytopes.hypercube(3) 

sage: cube.is_neighborly() 

True 

sage: cube = polytopes.hypercube(4) 

sage: cube.is_neighborly() 

False 

 

Cyclic polytopes are neighborly: 

 

:: 

 

sage: all([polytopes.cyclic_polytope(i, i + 1 + j).is_neighborly() for i in range(5) for j in range(3)]) 

True 

 

The neighborliness of a polyhedron equals floor of dimension half 

(or larger in case of a simplex) if and only if the polyhedron 

is neighborly:: 

 

sage: testpolys = [polytopes.cube(), polytopes.cyclic_polytope(6, 9), polytopes.simplex(6)] 

sage: [(P.neighborliness()>=floor(P.dim()/2)) == P.is_neighborly() for P in testpolys] 

[True, True, True] 

 

""" 

if k is None: 

k = floor(self.dim()/2) 

return all(len(self.faces(i)) == binomial(self.n_vertices(), i + 1) for i in range(1, k)) 

 

@cached_method 

def is_lattice_polytope(self): 

r""" 

Return whether the polyhedron is a lattice polytope. 

 

OUTPUT: 

 

``True`` if the polyhedron is compact and has only integral 

vertices, ``False`` otherwise. 

 

EXAMPLES:: 

 

sage: polytopes.cross_polytope(3).is_lattice_polytope() 

True 

sage: polytopes.regular_polygon(5).is_lattice_polytope() 

False 

""" 

if not self.is_compact(): 

return False 

if self.base_ring() is ZZ: 

return True 

return all(v.is_integral() for v in self.vertex_generator()) 

 

@cached_method 

def lattice_polytope(self, envelope=False): 

r""" 

Return an encompassing lattice polytope. 

 

INPUT: 

 

- ``envelope`` -- boolean (default: ``False``). If the 

polyhedron has non-integral vertices, this option decides 

whether to return a strictly larger lattice polytope or 

raise a ``ValueError``. This option has no effect if the 

polyhedron has already integral vertices. 

 

OUTPUT: 

 

A :class:`LatticePolytope 

<sage.geometry.lattice_polytope.LatticePolytopeClass>`. If the 

polyhedron is compact and has integral vertices, the lattice 

polytope equals the polyhedron. If the polyhedron is compact 

but has at least one non-integral vertex, a strictly larger 

lattice polytope is returned. 

 

If the polyhedron is not compact, a ``NotImplementedError`` is 

raised. 

 

If the polyhedron is not integral and ``envelope=False``, a 

``ValueError`` is raised. 

 

ALGORITHM: 

 

For each non-integral vertex, a bounding box of integral 

points is added and the convex hull of these integral points 

is returned. 

 

EXAMPLES: 

 

First, a polyhedron with integral vertices:: 

 

sage: P = Polyhedron( vertices = [(1, 0), (0, 1), (-1, 0), (0, -1)]) 

sage: lp = P.lattice_polytope(); lp 

2-d reflexive polytope #3 in 2-d lattice M 

sage: lp.vertices() 

M(-1, 0), 

M( 0, -1), 

M( 0, 1), 

M( 1, 0) 

in 2-d lattice M 

 

Here is a polyhedron with non-integral vertices:: 

 

sage: P = Polyhedron( vertices = [(1/2, 1/2), (0, 1), (-1, 0), (0, -1)]) 

sage: lp = P.lattice_polytope() 

Traceback (most recent call last): 

... 

ValueError: Some vertices are not integral. You probably want 

to add the argument "envelope=True" to compute an enveloping 

lattice polytope. 

sage: lp = P.lattice_polytope(True); lp 

2-d reflexive polytope #5 in 2-d lattice M 

sage: lp.vertices() 

M(-1, 0), 

M( 0, -1), 

M( 1, 1), 

M( 0, 1), 

M( 1, 0) 

in 2-d lattice M 

""" 

if not self.is_compact(): 

raise NotImplementedError('Only compact lattice polytopes are allowed.') 

 

try: 

vertices = self.vertices_matrix(ZZ).columns() 

except TypeError: 

if not envelope: 

raise ValueError('Some vertices are not integral. ' 

'You probably want to add the argument ' 

'"envelope=True" to compute an enveloping lattice polytope.') 

vertices = [] 

for v in self.vertex_generator(): 

vbox = [ set([floor(x),ceil(x)]) for x in v ] 

vertices.extend( itertools.product(*vbox) ) 

 

# construct the (enveloping) lattice polytope 

from sage.geometry.lattice_polytope import LatticePolytope 

return LatticePolytope(vertices) 

 

def _integral_points_PALP(self): 

r""" 

Return the integral points in the polyhedron using PALP. 

 

This method is for testing purposes and will eventually be removed. 

 

OUTPUT: 

 

The list of integral points in the polyhedron. If the 

polyhedron is not compact, a ``ValueError`` is raised. 

 

EXAMPLES:: 

 

sage: Polyhedron(vertices=[(-1,-1),(1,0),(1,1),(0,1)])._integral_points_PALP() 

[M(-1, -1), M(0, 1), M(1, 0), M(1, 1), M(0, 0)] 

sage: Polyhedron(vertices=[(-1/2,-1/2),(1,0),(1,1),(0,1)]).lattice_polytope(True).points() 

M(-1, -1), 

M(-1, 0), 

M( 0, -1), 

M( 1, 1), 

M( 0, 1), 

M( 1, 0), 

M( 0, 0) 

in 2-d lattice M 

sage: Polyhedron(vertices=[(-1/2,-1/2),(1,0),(1,1),(0,1)])._integral_points_PALP() 

[M(1, 1), M(0, 1), M(1, 0), M(0, 0)] 

""" 

if not self.is_compact(): 

raise ValueError('Can only enumerate points in a compact polyhedron.') 

lp = self.lattice_polytope(True) 

# remove cached values to get accurate timings 

try: 

del lp._points 

del lp._npoints 

except AttributeError: 

pass 

if self.is_lattice_polytope(): 

return list(lp.points()) 

return [p for p in lp.points() if self.contains(p)] 

 

@cached_method 

def bounding_box(self, integral=False, integral_hull=False): 

r""" 

Return the coordinates of a rectangular box containing the non-empty polytope. 

 

INPUT: 

 

- ``integral`` -- Boolean (default: ``False``). Whether to 

only allow integral coordinates in the bounding box. 

 

- ``integral_hull`` -- Boolean (default: ``False``). If ``True``, return a 

box containing the integral points of the polytope, or ``None, None`` if it 

is known that the polytope has no integral points. 

 

OUTPUT: 

 

A pair of tuples ``(box_min, box_max)`` where ``box_min`` are 

the coordinates of a point bounding the coordinates of the 

polytope from below and ``box_max`` bounds the coordinates 

from above. 

 

EXAMPLES:: 

 

sage: Polyhedron([ (1/3,2/3), (2/3, 1/3) ]).bounding_box() 

((1/3, 1/3), (2/3, 2/3)) 

sage: Polyhedron([ (1/3,2/3), (2/3, 1/3) ]).bounding_box(integral=True) 

((0, 0), (1, 1)) 

sage: Polyhedron([ (1/3,2/3), (2/3, 1/3) ]).bounding_box(integral_hull=True) 

(None, None) 

sage: Polyhedron([ (1/3,2/3), (3/3, 4/3) ]).bounding_box(integral_hull=True) 

((1, 1), (1, 1)) 

sage: polytopes.buckyball(exact=False).bounding_box() 

((-0.8090169944, -0.8090169944, -0.8090169944), (0.8090169944, 0.8090169944, 0.8090169944)) 

""" 

box_min = [] 

box_max = [] 

if self.n_vertices == 0: 

raise ValueError('Empty polytope is not allowed') 

if not self.is_compact(): 

raise ValueError('Only polytopes (compact polyhedra) are allowed.') 

for i in range(self.ambient_dim()): 

coords = [ v[i] for v in self.vertex_generator() ] 

max_coord = max(coords) 

min_coord = min(coords) 

if integral_hull: 

a = ceil(min_coord) 

b = floor(max_coord) 

if a > b: 

return None, None 

box_max.append(b) 

box_min.append(a) 

elif integral: 

box_max.append(ceil(max_coord)) 

box_min.append(floor(min_coord)) 

else: 

box_max.append(max_coord) 

box_min.append(min_coord) 

return (tuple(box_min), tuple(box_max)) 

 

def integral_points_count(self, **kwds): 

r""" 

Return the number of integral points in the polyhedron. 

 

This generic version of this method simply calls ``integral_points``. 

 

EXAMPLES:: 

 

sage: P = polytopes.cube() 

sage: P.integral_points_count() 

27 

 

We shrink the polyhedron a little bit:: 

 

sage: Q = P*(8/9) 

sage: Q.integral_points_count() 

1 

 

Same for a polyhedron whose coordinates are not rationals. Note that 

the answer is an integer even though there are no guarantees for 

exactness:: 

 

sage: Q = P*RDF(8/9) 

sage: Q.integral_points_count() 

1 

 

Unbounded polyhedra (with or without lattice points) are not supported:: 

 

sage: P = Polyhedron(vertices=[[1/2, 1/3]], rays=[[1, 1]]) 

sage: P.integral_points_count() 

Traceback (most recent call last): 

... 

NotImplementedError: ... 

sage: P = Polyhedron(vertices=[[1, 1]], rays=[[1, 1]]) 

sage: P.integral_points_count() 

Traceback (most recent call last): 

... 

NotImplementedError: ... 

 

""" 

return len(self.integral_points()) 

 

def integral_points(self, threshold=100000): 

r""" 

Return the integral points in the polyhedron. 

 

Uses either the naive algorithm (iterate over a rectangular 

bounding box) or triangulation + Smith form. 

 

INPUT: 

 

- ``threshold`` -- integer (default: 100000). Use the naive 

algorithm as long as the bounding box is smaller than this. 

 

OUTPUT: 

 

The list of integral points in the polyhedron. If the 

polyhedron is not compact, a ``ValueError`` is raised. 

 

EXAMPLES:: 

 

sage: Polyhedron(vertices=[(-1,-1),(1,0),(1,1),(0,1)]).integral_points() 

((-1, -1), (0, 0), (0, 1), (1, 0), (1, 1)) 

 

sage: simplex = Polyhedron([(1,2,3), (2,3,7), (-2,-3,-11)]) 

sage: simplex.integral_points() 

((-2, -3, -11), (0, 0, -2), (1, 2, 3), (2, 3, 7)) 

 

The polyhedron need not be full-dimensional:: 

 

sage: simplex = Polyhedron([(1,2,3,5), (2,3,7,5), (-2,-3,-11,5)]) 

sage: simplex.integral_points() 

((-2, -3, -11, 5), (0, 0, -2, 5), (1, 2, 3, 5), (2, 3, 7, 5)) 

 

sage: point = Polyhedron([(2,3,7)]) 

sage: point.integral_points() 

((2, 3, 7),) 

 

sage: empty = Polyhedron() 

sage: empty.integral_points() 

() 

 

Here is a simplex where the naive algorithm of running over 

all points in a rectangular bounding box no longer works fast 

enough:: 

 

sage: v = [(1,0,7,-1), (-2,-2,4,-3), (-1,-1,-1,4), (2,9,0,-5), (-2,-1,5,1)] 

sage: simplex = Polyhedron(v); simplex 

A 4-dimensional polyhedron in ZZ^4 defined as the convex hull of 5 vertices 

sage: len(simplex.integral_points()) 

49 

 

A case where rounding in the right direction goes a long way:: 

 

sage: P = 1/10*polytopes.hypercube(14) 

sage: P.integral_points() 

((0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),) 

 

Finally, the 3-d reflexive polytope number 4078:: 

 

sage: v = [(1,0,0), (0,1,0), (0,0,1), (0,0,-1), (0,-2,1), 

....: (-1,2,-1), (-1,2,-2), (-1,1,-2), (-1,-1,2), (-1,-3,2)] 

sage: P = Polyhedron(v) 

sage: pts1 = P.integral_points() # Sage's own code 

sage: all(P.contains(p) for p in pts1) 

True 

sage: pts2 = LatticePolytope(v).points() # PALP 

sage: for p in pts1: p.set_immutable() 

sage: set(pts1) == set(pts2) 

True 

 

sage: timeit('Polyhedron(v).integral_points()') # not tested - random 

625 loops, best of 3: 1.41 ms per loop 

sage: timeit('LatticePolytope(v).points()') # not tested - random 

25 loops, best of 3: 17.2 ms per loop 

 

TESTS: 

 

Test some trivial cases (see :trac:`17937`):: 

 

sage: P = Polyhedron(ambient_dim=1) # empty polyhedron in 1 dimension 

sage: P.integral_points() 

() 

sage: P = Polyhedron(ambient_dim=0) # empty polyhedron in 0 dimensions 

sage: P.integral_points() 

() 

sage: P = Polyhedron([[3]]) # single point in 1 dimension 

sage: P.integral_points() 

((3),) 

sage: P = Polyhedron([[1/2]]) # single non-integral point in 1 dimension 

sage: P.integral_points() 

() 

sage: P = Polyhedron([[]]) # single point in 0 dimensions 

sage: P.integral_points() 

((),) 

""" 

if not self.is_compact(): 

raise ValueError('Can only enumerate points in a compact polyhedron.') 

# Trivial cases: polyhedron with 0 or 1 vertices 

if self.n_vertices() == 0: 

return () 

if self.n_vertices() == 1: 

v = self.vertices_list()[0] 

try: 

return (vector(ZZ, v),) 

except TypeError: # vertex not integral 

return () 

 

# for small bounding boxes, it is faster to naively iterate over the points of the box 

box_min, box_max = self.bounding_box(integral_hull=True) 

if box_min is None: 

return () 

box_points = prod(max_coord-min_coord+1 for min_coord, max_coord in zip(box_min, box_max)) 

if not self.is_lattice_polytope() or \ 

(self.is_simplex() and box_points < 1000) or \ 

box_points<threshold: 

from sage.geometry.integral_points import rectangular_box_points 

return rectangular_box_points(list(box_min), list(box_max), self) 

 

# for more complicate polytopes, triangulate & use smith normal form 

from sage.geometry.integral_points import simplex_points 

if self.is_simplex(): 

return simplex_points(self.Vrepresentation()) 

triangulation = self.triangulate() 

points = set() 

for simplex in triangulation: 

triang_vertices = [ self.Vrepresentation(i) for i in simplex ] 

new_points = simplex_points(triang_vertices) 

for p in new_points: 

p.set_immutable() 

points.update(new_points) 

# assert all(self.contains(p) for p in points) # slow 

return tuple(points) 

 

def get_integral_point(self, index, **kwds): 

r""" 

Return the ``index``-th integral point in this polyhedron. 

 

This is equivalent to ``sorted(self.integral_points())[index]``. 

However, so long as self.integral_points_count() does not need to 

enumerate all integral points, neither does this method. Hence it can 

be significantly faster. If the polyhedron is not compact, a 

``ValueError`` is raised. 

 

INPUT: 

 

- ``index`` -- integer. The index of the integral point to be found. If 

this is not in [0, ``self.integral_point_count()``), an ``IndexError`` 

is raised. 

 

- ``**kwds`` -- optional keyword parameters that are passed to 

:meth:`self.integral_points_count`. 

 

ALGORITHM: 

 

The function computes each of the components of the requested point in 

turn. To compute x_i, the ith component, it bisects the upper and lower 

bounds on x_i given by the bounding box. At each bisection, it uses 

:meth:`integral_points_count` to determine on which side of the 

bisecting hyperplane the requested point lies. 

 

.. SEEALSO:: 

 

:meth:`integral_points_count`. 

 

EXAMPLES:: 

 

sage: P = Polyhedron(vertices=[(-1,-1),(1,0),(1,1),(0,1)]) 

sage: P.get_integral_point(1) 

(0, 0) 

sage: P.get_integral_point(4) 

(1, 1) 

sage: sorted(P.integral_points()) 

[(-1, -1), (0, 0), (0, 1), (1, 0), (1, 1)] 

sage: P.get_integral_point(5) 

Traceback (most recent call last): 

... 

IndexError: ... 

 

sage: Q = Polyhedron([(1,3), (2, 7), (9, 77)]) 

sage: [Q.get_integral_point(i) for i in range(Q.integral_points_count())] == sorted(Q.integral_points()) 

True 

sage: Q.get_integral_point(0, explicit_enumeration_threshold=0, triangulation='cddlib') # optional - latte_int 

(1, 3) 

sage: Q.get_integral_point(0, explicit_enumeration_threshold=0, triangulation='cddlib', foo=True) # optional - latte_int 

Traceback (most recent call last): 

... 

RuntimeError: ... 

 

sage: R = Polyhedron(vertices=[[1/2, 1/3]], rays=[[1, 1]]) 

sage: R.get_integral_point(0) 

Traceback (most recent call last): 

... 

ValueError: ... 

""" 

 

if not self.is_compact(): 

raise ValueError('Can only enumerate points in a compact polyhedron.') 

 

if not 0 <= index < self.integral_points_count(**kwds): 

raise IndexError('polytope index out of range') 

 

D = self.ambient_dim() 

lower_bounds, upper_bounds = self.bounding_box() 

coordinate = [] 

P = self 

S = self.parent() 

for i in range(D): # Now compute x_i, the ith component of coordinate. 

lower, upper = ceil(lower_bounds[i]), floor(upper_bounds[i]) + 1 # So lower <= x_i < upper. 

while lower < upper-1: 

guess = (lower + upper) // 2 # > lower. 

# Build new polyhedron by intersecting P with the halfspace {x_i < guess}. 

P_lt_guess = P.intersection(S(None, ([[guess-1] + [0] * i + [-1] + [0] * (D - i - 1)], []))) 

# Avoid computing P_geq_guess = P.intersection({x_i >= guess}) right now, it might not be needed. 

P_lt_guess_count = P_lt_guess.integral_points_count(**kwds) 

if P_lt_guess_count > index: # Move upper down to guess. 

upper = guess 

index -= 0 

P = P_lt_guess 

else: # P_lt_guess_count <= index: # Move lower up to guess. 

lower = guess 

index -= P_lt_guess_count 

P_geq_guess = P.intersection(S(None, ([[-guess] + [0] * i + [1] + [0] * (D - i - 1)], []))) 

P = P_geq_guess 

coordinate.append(lower) # Record the new component that we have found. 

point = vector(ZZ, coordinate) 

point.set_immutable() 

return point 

 

def random_integral_point(self, **kwds): 

r""" 

Return an integral point in this polyhedron chosen uniformly at random. 

 

INPUT: 

 

- ``**kwds`` -- optional keyword parameters that are passed to 

:meth:`self.get_integral_point`. 

 

OUTPUT: 

 

The integral point in the polyhedron chosen uniformly at random. If the 

polyhedron is not compact, a ``ValueError`` is raised. If the 

polyhedron does not contain any integral points, an ``EmptySetError`` is 

raised. 

 

.. SEEALSO:: 

 

:meth:`get_integral_point`. 

 

EXAMPLES:: 

 

sage: P = Polyhedron(vertices=[(-1,-1),(1,0),(1,1),(0,1)]) 

sage: P.random_integral_point() # random 

(0, 0) 

sage: P.random_integral_point() in P.integral_points() 

True 

sage: P.random_integral_point(explicit_enumeration_threshold=0, triangulation='cddlib') # random, optional - latte_int 

(1, 1) 

sage: P.random_integral_point(explicit_enumeration_threshold=0, triangulation='cddlib', foo=7) # optional - latte_int 

Traceback (most recent call last): 

... 

RuntimeError: ... 

 

sage: Q = Polyhedron(vertices=[(2, 1/3)], rays=[(1, 2)]) 

sage: Q.random_integral_point() 

Traceback (most recent call last): 

... 

ValueError: ... 

 

sage: R = Polyhedron(vertices=[(1/2, 0), (1, 1/2), (0, 1/2)]) 

sage: R.random_integral_point() 

Traceback (most recent call last): 

... 

EmptySetError: ... 

""" 

 

if not self.is_compact(): 

raise ValueError('Can only sample integral points in a compact polyhedron.') 

 

count = self.integral_points_count() 

if count == 0: 

raise EmptySetError('Polyhedron does not contain any integral points.') 

 

return self.get_integral_point(current_randstate().python_random().randint(0, count-1), **kwds) 

 

@cached_method 

def combinatorial_automorphism_group(self, vertex_graph_only=False): 

""" 

Computes the combinatorial automorphism group. 

 

If ``vertex_graph_only`` is ``True``, the automorphism group 

of the vertex-edge graph of the polyhedron is returned. Otherwise 

the automorphism group of the vertex-facet graph, which is 

isomorphic to the automorphism group of the face lattice is returned. 

 

INPUT: 

 

- ``vertex_graph_only`` -- boolean (default: ``False``); whether 

to return the automorphism group of the vertex edges graph or 

of the lattice. 

 

OUTPUT: 

 

A 

:class:`PermutationGroup<sage.groups.perm_gps.permgroup.PermutationGroup_generic_with_category'>` 

that is isomorphic to the combinatorial automorphism group is 

returned. 

 

- if ``vertex_graph_only`` is ``True``: 

The automorphism group of the vertex-edge graph of the polyhedron 

 

- if ``vertex_graph_only`` is ``False`` (default): 

The automorphism group of the vertex-facet graph of the polyhedron, 

see :meth:`vertex_facet_graph`. This group is isomorphic to the 

automorphism group of the face lattice of the polyhedron. 

 

NOTE: 

 

Depending on ``vertex_graph_only``, this method returns groups 

that are not necessarily isomorphic, see the examples below. 

 

.. SEEALSO:: 

 

:meth:`is_combinatorially_isomorphic`, 

:meth:`graph`, 

:meth:`vertex_facet_graph`. 

 

EXAMPLES:: 

 

sage: quadrangle = Polyhedron(vertices=[(0,0),(1,0),(0,1),(2,3)]) 

sage: quadrangle.combinatorial_automorphism_group().is_isomorphic(groups.permutation.Dihedral(4)) 

True 

sage: quadrangle.restricted_automorphism_group() 

Permutation Group with generators [()] 

 

Permutations can only exchange vertices with vertices, rays 

with rays, and lines with lines:: 

 

sage: P = Polyhedron(vertices=[(1,0,0), (1,1,0)], rays=[(1,0,0)], lines=[(0,0,1)]) 

sage: P.combinatorial_automorphism_group(vertex_graph_only=True) 

Permutation Group with generators [(A vertex at (1,0,0),A vertex at (1,1,0))] 

 

This shows an example of two polytopes whose vertex-edge graphs are isomorphic, 

but their face_lattices are not isomorphic:: 

 

sage: Q=Polyhedron([[-123984206864/2768850730773, -101701330976/922950243591, -64154618668/2768850730773, -2748446474675/2768850730773], 

....: [-11083969050/98314591817, -4717557075/98314591817, -32618537490/98314591817, -91960210208/98314591817], 

....: [-9690950/554883199, -73651220/554883199, 1823050/554883199, -549885101/554883199], [-5174928/72012097, 5436288/72012097, -37977984/72012097, 60721345/72012097], 

....: [-19184/902877, 26136/300959, -21472/902877, 899005/902877], [53511524/1167061933, 88410344/1167061933, 621795064/1167061933, 982203941/1167061933], 

....: [4674489456/83665171433, -4026061312/83665171433, 28596876672/83665171433, -78383796375/83665171433], [857794884940/98972360190089, -10910202223200/98972360190089, 2974263671400/98972360190089, -98320463346111/98972360190089]]) 

sage: C = polytopes.cyclic_polytope(4,8) 

sage: C.is_combinatorially_isomorphic(Q) 

False 

sage: C.combinatorial_automorphism_group(vertex_graph_only=True).is_isomorphic(Q.combinatorial_automorphism_group(vertex_graph_only=True)) 

True 

sage: C.combinatorial_automorphism_group(vertex_graph_only=False).is_isomorphic(Q.combinatorial_automorphism_group(vertex_graph_only=False)) 

False 

 

The automorphism group of the face lattice is isomorphic to the combinatorial automorphism group:: 

 

sage: CG = C.face_lattice().hasse_diagram().automorphism_group() 

sage: C.combinatorial_automorphism_group().is_isomorphic(CG) 

True 

sage: QG = Q.face_lattice().hasse_diagram().automorphism_group() 

sage: Q.combinatorial_automorphism_group().is_isomorphic(QG) 

True 

 

""" 

if vertex_graph_only: 

G = self.graph() 

else: 

G = self.vertex_facet_graph() 

group = G.automorphism_group(edge_labels=True) 

self._combinatorial_automorphism_group = group 

 

return self._combinatorial_automorphism_group 

 

@cached_method 

def restricted_automorphism_group(self, output="abstract"): 

r""" 

Return the restricted automorphism group. 

 

First, let the linear automorphism group be the subgroup of 

the affine group `AGL(d,\RR) = GL(d,\RR) \ltimes \RR^d` 

preserving the `d`-dimensional polyhedron. The affine group 

acts in the usual way `\vec{x}\mapsto A\vec{x}+b` on the 

ambient space. 

 

The restricted automorphism group is the subgroup of the linear 

automorphism group generated by permutations of the generators 

of the same type. That is, vertices can only be permuted with 

vertices, ray generators with ray generators, and line 

generators with line generators. 

 

For example, take the first quadrant 

 

.. MATH:: 

 

Q = \Big\{ (x,y) \Big| x\geq 0,\; y\geq0 \Big\} 

\subset \QQ^2 

 

Then the linear automorphism group is 

 

.. MATH:: 

 

\mathrm{Aut}(Q) = 

\left\{ 

\begin{pmatrix} 

a & 0 \\ 0 & b 

\end{pmatrix} 

,~ 

\begin{pmatrix} 

0 & c \\ d & 0 

\end{pmatrix} 

:~ 

a, b, c, d \in \QQ_{>0} 

\right\} 

\subset 

GL(2,\QQ) 

\subset 

E(d) 

 

Note that there are no translations that map the quadrant `Q` 

to itself, so the linear automorphism group is contained in 

the general linear group (the subgroup of transformations 

preserving the origin). The restricted automorphism group is 

 

.. MATH:: 

 

\mathrm{Aut}(Q) = 

\left\{ 

\begin{pmatrix} 

1 & 0 \\ 0 & 1 

\end{pmatrix} 

,~ 

\begin{pmatrix} 

0 & 1 \\ 1 & 0 

\end{pmatrix} 

\right\} 

\simeq \ZZ_2 

 

INPUT: 

 

- ``output`` -- how the group should be represented: 

 

- ``"abstract"`` (default) -- return an abstract permutation 

group without further meaning. 

 

- ``"permutation"`` -- return a permutation group on the 

indices of the polyhedron generators. For example, the 

permutation ``(0,1)`` would correspond to swapping 

``self.Vrepresentation(0)`` and ``self.Vrepresentation(1)``. 

 

- ``"matrix"`` -- return a matrix group representing affine 

transformations. When acting on affine vectors, you should 

append a `1` to every vector. If the polyhedron is not full 

dimensional, the returned matrices act as the identity on 

the orthogonal complement of the affine space spanned by 

the polyhedron. 

 

- ``"matrixlist"`` -- like ``matrix``, but return the list of 

elements of the matrix group. Useful for fields without a 

good implementation of matrix groups or to avoid the 

overhead of creating the group. 

 

OUTPUT: 

 

- For ``output="abstract"`` and ``output="permutation"``: 

a :class:`PermutationGroup<sage.groups.perm_gps.permgroup.PermutationGroup_generic>`. 

 

- For ``output="matrix"``: a :class:`MatrixGroup`. 

 

- For ``output="matrixlist"``: a list of matrices. 

 

REFERENCES: 

 

- [BSS2009]_ 

 

EXAMPLES:: 

 

sage: P = polytopes.cross_polytope(3) 

sage: P.restricted_automorphism_group() 

Permutation Group with generators [(3,4), (2,3)(4,5), (2,5), (1,2)(5,6), (1,6)] 

sage: P.restricted_automorphism_group(output="permutation") 

Permutation Group with generators [(2,3), (1,2)(3,4), (1,4), (0,1)(4,5), (0,5)] 

sage: P.restricted_automorphism_group(output="matrix") 

Matrix group over Rational Field with 5 generators ( 

[ 1 0 0 0] [1 0 0 0] [ 1 0 0 0] [0 1 0 0] [-1 0 0 0] 

[ 0 1 0 0] [0 0 1 0] [ 0 -1 0 0] [1 0 0 0] [ 0 1 0 0] 

[ 0 0 -1 0] [0 1 0 0] [ 0 0 1 0] [0 0 1 0] [ 0 0 1 0] 

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

) 

 

:: 

 

sage: P24 = polytopes.twenty_four_cell() 

sage: AutP24 = P24.restricted_automorphism_group() 

sage: PermutationGroup([ 

....: '(1,20,2,24,5,23)(3,18,10,19,4,14)(6,21,11,22,7,15)(8,12,16,17,13,9)', 

....: '(1,21,8,24,4,17)(2,11,6,15,9,13)(3,20)(5,22)(10,16,12,23,14,19)' 

....: ]) == AutP24 

True 

sage: len(AutP24) 

1152 

 

Here is the quadrant example mentioned in the beginning:: 

 

sage: P = Polyhedron(rays=[(1,0),(0,1)]) 

sage: P.Vrepresentation() 

(A vertex at (0, 0), A ray in the direction (0, 1), A ray in the direction (1, 0)) 

sage: P.restricted_automorphism_group(output="permutation") 

Permutation Group with generators [(1,2)] 

 

Also, the polyhedron need not be full-dimensional:: 

 

sage: P = Polyhedron(vertices=[(1,2,3,4,5),(7,8,9,10,11)]) 

sage: P.restricted_automorphism_group() 

Permutation Group with generators [(1,2)] 

sage: G = P.restricted_automorphism_group(output="matrixlist") 

sage: G 

[ 

[1 0 0 0 0 0] [ -87/55 -82/55 -2/5 38/55 98/55 12/11] 

[0 1 0 0 0 0] [-142/55 -27/55 -2/5 38/55 98/55 12/11] 

[0 0 1 0 0 0] [-142/55 -82/55 3/5 38/55 98/55 12/11] 

[0 0 0 1 0 0] [-142/55 -82/55 -2/5 93/55 98/55 12/11] 

[0 0 0 0 1 0] [-142/55 -82/55 -2/5 38/55 153/55 12/11] 

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

] 

sage: g = AffineGroup(5, QQ)(G[1]) 

sage: g 

[ -87/55 -82/55 -2/5 38/55 98/55] [12/11] 

[-142/55 -27/55 -2/5 38/55 98/55] [12/11] 

x |-> [-142/55 -82/55 3/5 38/55 98/55] x + [12/11] 

[-142/55 -82/55 -2/5 93/55 98/55] [12/11] 

[-142/55 -82/55 -2/5 38/55 153/55] [12/11] 

sage: g^2 

[1 0 0 0 0] [0] 

[0 1 0 0 0] [0] 

x |-> [0 0 1 0 0] x + [0] 

[0 0 0 1 0] [0] 

[0 0 0 0 1] [0] 

sage: g(list(P.vertices()[0])) 

(7, 8, 9, 10, 11) 

sage: g(list(P.vertices()[1])) 

(1, 2, 3, 4, 5) 

 

Affine transformations do not change the restricted automorphism 

group. For example, any non-degenerate triangle has the 

dihedral group with 6 elements, `D_6`, as its automorphism 

group:: 

 

sage: initial_points = [vector([1,0]), vector([0,1]), vector([-2,-1])] 

sage: points = initial_points 

sage: Polyhedron(vertices=points).restricted_automorphism_group() 

Permutation Group with generators [(2,3), (1,2)] 

sage: points = [pt - initial_points[0] for pt in initial_points] 

sage: Polyhedron(vertices=points).restricted_automorphism_group() 

Permutation Group with generators [(2,3), (1,2)] 

sage: points = [pt - initial_points[1] for pt in initial_points] 

sage: Polyhedron(vertices=points).restricted_automorphism_group() 

Permutation Group with generators [(2,3), (1,2)] 

sage: points = [pt - 2*initial_points[1] for pt in initial_points] 

sage: Polyhedron(vertices=points).restricted_automorphism_group() 

Permutation Group with generators [(2,3), (1,2)] 

 

The ``output="matrixlist"`` can be used over fields without a 

complete implementation of matrix groups:: 

 

sage: P = polytopes.dodecahedron(); P 

A 3-dimensional polyhedron in (Number Field in sqrt5 with defining polynomial x^2 - 5)^3 defined as the convex hull of 20 vertices 

sage: G = P.restricted_automorphism_group(output="matrixlist") 

sage: len(G) 

120 

 

Floating-point computations are supported with a simple fuzzy 

zero implementation:: 

 

sage: P = Polyhedron(vertices=[(1/3,0,0,1),(0,1/4,0,1),(0,0,1/5,1)], base_ring=RDF) 

sage: P.restricted_automorphism_group() 

Permutation Group with generators [(2,3), (1,2)] 

sage: len(P.restricted_automorphism_group(output="matrixlist")) 

6 

 

TESTS:: 

 

sage: P = Polyhedron(vertices=[(1,0), (1,1)], rays=[(1,0)]) 

sage: P.restricted_automorphism_group(output="permutation") 

Permutation Group with generators [(1,2)] 

sage: P.restricted_automorphism_group(output="matrix") 

Matrix group over Rational Field with 1 generators ( 

[ 1 0 0] 

[ 0 -1 1] 

[ 0 0 1] 

) 

sage: P.restricted_automorphism_group(output="foobar") 

Traceback (most recent call last): 

... 

ValueError: unknown output 'foobar', valid values are ('abstract', 'permutation', 'matrix', 'matrixlist') 

""" 

# The algorithm works as follows: 

# 

# Let V be the matrix where every column is a homogeneous 

# coordinate of a V-representation object (vertex, ray, line). 

# Let us assume that V has full rank, that the polyhedron is 

# full dimensional. 

# 

# Let Q = V Vt and C = Vt Q^-1 V. The rows and columns of C 

# can be thought of as being indexed by the V-rep objects of the 

# polytope. 

# 

# It turns out that we can identify the restricted automorphism 

# group with the automorphism group of the edge-colored graph 

# on the V-rep objects with colors determined by the symmetric 

# matrix C. 

# 

# An automorphism of this graph is equivalent to a permutation 

# matrix P such that C = Pt C P. If we now define 

# A = V P Vt Q^-1, then one can check that V P = A V. 

# In other words: permuting the generators is the same as 

# applying the affine transformation A on the generators. 

# 

# If the given polyhedron is not fully-dimensional, 

# then Q will be not invertible. In this case, we use a 

# pseudoinverse Q+ instead of Q^-1. The formula for A acting on 

# the space spanned by V then simplifies to A = V P V+ where V+ 

# denotes the pseudoinverse of V, which also equals V+ = Vt Q+. 

# 

# If we are asked to return the (group of) transformation 

# matrices to the user, we also require that those 

# transformations act as the identity on the orthogonal 

# complement of the space spanned by V. This complement is the 

# space spanned by the columns of W = 1 - V V+. One can check 

# that B = (V P V+) + W is the correct matrix: it acts the same 

# as A on V and it satisfies B W = W. 

 

outputs = ("abstract", "permutation", "matrix", "matrixlist") 

if output not in outputs: 

raise ValueError("unknown output {!r}, valid values are {}".format(output, outputs)) 

 

# For backwards compatibility, we treat "abstract" as 

# "permutation", but where we add 1 to the indices of the 

# permutations. 

index0 = 0 

if output == "abstract": 

index0 = 1 

output = "permutation" 

 

if self.base_ring().is_exact(): 

def rational_approximation(c): 

return c 

else: 

c_list = [] 

def rational_approximation(c): 

# Implementation detail: Return unique integer if two 

# c-values are the same up to machine precision. But 

# you can think of it as a uniquely-chosen rational 

# approximation. 

for i, x in enumerate(c_list): 

if self._is_zero(x - c): 

return i 

c_list.append(c) 

return len(c_list) - 1 

 

if self.is_compact(): 

def edge_label(i,j,c_ij): 

return c_ij 

else: 

# In the non-compact case, we also label the edges by the 

# type of the V-representation object. This ensures that 

# vertices, rays, and lines are only permuted amongst 

# themselves. 

def edge_label(i,j,c_ij): 

return (self.Vrepresentation(i).type(), c_ij, self.Vrepresentation(j).type()) 

 

# Homogeneous coordinates for the V-representation objects. 

# Mathematically, V is a matrix. For efficiency however, we 

# represent it as a list of column vectors. 

V = [v.homogeneous_vector() for v in self.Vrepresentation()] 

 

# Pseudoinverse of V Vt 

Qplus = sum(v.column() * v.row() for v in V).pseudoinverse() 

 

# Construct the graph. 

G = Graph() 

for i in range(len(V)): 

for j in range(i+1, len(V)): 

c_ij = rational_approximation(V[i] * Qplus * V[j]) 

G.add_edge(index0+i, index0+j, edge_label(i, j, c_ij)) 

 

permgroup = G.automorphism_group(edge_labels=True) 

if output == "permutation": 

return permgroup 

elif output == "matrix": 

permgroup = permgroup.gens() 

 

# Compute V+ = Vt Q+ as list of row vectors 

Vplus = list(matrix(V) * Qplus) # matrix(V) is Vt 

 

# Compute W = 1 - V V+ 

W = 1 - sum(V[i].column() * Vplus[i].row() for i in range(len(V))) 

 

# Convert the permutation group to a matrix group. 

# If P is a permutation, then we return the matrix 

# B = (V P V+) + W. 

# 

# If output == "matrix", we loop over the generators of the group. 

# Otherwise, we loop over all elements. 

matrices = [] 

for perm in permgroup: 

A = sum(V[perm(i)].column() * Vplus[i].row() for i in range(len(V))) 

matrices.append(A + W) 

 

if output == "matrixlist": 

return matrices 

else: 

return MatrixGroup(matrices) 

 

def is_full_dimensional(self): 

""" 

Return whether the polyhedron is full dimensional. 

 

OUTPUT: 

 

Boolean. Whether the polyhedron is not contained in any strict 

affine subspace. 

 

EXAMPLES:: 

 

sage: polytopes.hypercube(3).is_full_dimensional() 

True 

sage: Polyhedron(vertices=[(1,2,3)], rays=[(1,0,0)]).is_full_dimensional() 

False 

""" 

return self.dim() == self.ambient_dim() 

 

def is_combinatorially_isomorphic(self, other, algorithm='bipartite_graph'): 

r""" 

Return whether the polyhedron is combinatorially isomorphic to another polyhedron. 

 

We only consider bounded polyhedra. By definition, they are 

combinatorially isomorphic if their faces lattices are isomorphic. 

 

INPUT: 

 

- ``other`` -- a polyhedron object. 

- ``algorithm`` (default = ``bipartite_graph``) -- the algorithm to use. 

The other possible value is ``face_lattice``. 

 

OUTPUT: 

 

- ``True`` if the two polyhedra are combinatorially isomorphic 

- ``False`` otherwise 

 

.. SEEALSO:: 

 

:meth:`combinatorial_automorphism_group`, 

:meth:`vertex_facet_graph`. 

 

REFERENCES: 

 

For the equivalence of the two algorithms see [KK1995]_, p. 877-878 

 

EXAMPLES: 

 

The square is combinatorially isomorphic to the 2-dimensional cube:: 

 

sage: polytopes.hypercube(2).is_combinatorially_isomorphic(polytopes.regular_polygon(4)) 

True 

 

All the faces of the 3-dimensional permutahedron are either 

combinatorially isomorphic to a square or a hexagon:: 

 

sage: H = polytopes.regular_polygon(6) 

sage: S = polytopes.hypercube(2) 

sage: P = polytopes.permutahedron(4) 

sage: all(F.as_polyhedron().is_combinatorially_isomorphic(S) or F.as_polyhedron().is_combinatorially_isomorphic(H) for F in P.faces(2)) 

True 

 

Checking that a regular simplex intersected with its reflection 

through the origin is combinatorially isomorphic to the intersection 

of a cube with a hyperplane perpendicular to its long diagonal:: 

 

sage: def simplex_intersection(k): 

....: S1 = Polyhedron([vector(v)-vector(polytopes.simplex(k).center()) for v in polytopes.simplex(k).vertices_list()]) 

....: S2 = Polyhedron([-vector(v) for v in S1.vertices_list()]) 

....: return S1.intersection(S2) 

sage: def cube_intersection(k): 

....: C = polytopes.hypercube(k+1) 

....: H = Polyhedron(eqns=[[0]+[1 for i in range(k+1)]]) 

....: return C.intersection(H) 

sage: [simplex_intersection(k).is_combinatorially_isomorphic(cube_intersection(k)) for k in range(2,5)] 

[True, True, True] 

sage: simplex_intersection(2).is_combinatorially_isomorphic(polytopes.regular_polygon(6)) 

True 

sage: simplex_intersection(3).is_combinatorially_isomorphic(polytopes.octahedron()) 

True 

 

Two polytopes with the same `f`-vector, but different combinatorial types:: 

 

sage: P = Polyhedron([[-605520/1525633, -605520/1525633, -1261500/1525633, -52200/1525633, 11833/1525633],\ 

[-720/1769, -600/1769, 1500/1769, 0, -31/1769], [-216/749, 240/749, -240/749, -432/749, 461/749], \ 

[-50/181, 50/181, 60/181, -100/181, -119/181], [-32/51, -16/51, -4/51, 12/17, 1/17],\ 

[1, 0, 0, 0, 0], [16/129, 128/129, 0, 0, 1/129], [64/267, -128/267, 24/89, -128/267, 57/89],\ 

[1200/3953, -1200/3953, -1440/3953, -360/3953, -3247/3953], [1512/5597, 1512/5597, 588/5597, 4704/5597, 2069/5597]]) 

sage: C = polytopes.cyclic_polytope(5,10) 

sage: C.f_vector() == P.f_vector(); C.f_vector() 

True 

(1, 10, 45, 100, 105, 42, 1) 

sage: C.is_combinatorially_isomorphic(P) 

False 

 

sage: S = polytopes.simplex(3) 

sage: S = S.face_truncation(S.faces(0)[0]) 

sage: S = S.face_truncation(S.faces(0)[0]) 

sage: S = S.face_truncation(S.faces(0)[0]) 

sage: T = polytopes.simplex(3) 

sage: T = T.face_truncation(T.faces(0)[0]) 

sage: T = T.face_truncation(T.faces(0)[0]) 

sage: T = T.face_truncation(T.faces(0)[1]) 

sage: T.is_combinatorially_isomorphic(S) 

False 

sage: T.f_vector(), S.f_vector() 

((1, 10, 15, 7, 1), (1, 10, 15, 7, 1)) 

 

sage: C = polytopes.hypercube(5) 

sage: C.is_combinatorially_isomorphic(C) 

True 

sage: C.is_combinatorially_isomorphic(C, algorithm='magic') 

Traceback (most recent call last): 

... 

AssertionError: `algorithm` must be 'bipartite graph' or 'face_lattice' 

 

sage: G = Graph() 

sage: C.is_combinatorially_isomorphic(G) 

Traceback (most recent call last): 

... 

AssertionError: input `other` must be a polyhedron 

 

sage: H = Polyhedron(eqns=[[0,1,1,1,1]]); H 

A 3-dimensional polyhedron in QQ^4 defined as the convex hull of 1 vertex and 3 lines 

sage: C.is_combinatorially_isomorphic(H) 

Traceback (most recent call last): 

... 

AssertionError: polyhedron `other` must be bounded 

 

""" 

assert isinstance(other, Polyhedron_base), "input `other` must be a polyhedron" 

assert self.is_compact(), "polyhedron `self` must be bounded" 

assert other.is_compact(), "polyhedron `other` must be bounded" 

assert algorithm in ['bipartite_graph', 'face_lattice'], "`algorithm` must be 'bipartite graph' or 'face_lattice'" 

 

# For speed, we check if the polyhedra have the same number of facets and vertices. 

# This is faster then building the bipartite graphs first and 

# then check that they won't be isomorphic. 

if self.n_vertices() != other.n_vertices() or self.n_facets() != other.n_facets(): 

return False 

 

if algorithm == 'bipartite_graph': 

G_self = self.vertex_facet_graph(False) 

G_other = other.vertex_facet_graph(False) 

 

return G_self.is_isomorphic(G_other) 

else: 

return self.face_lattice().is_isomorphic(other.face_lattice()) 

 

def affine_hull(self, as_affine_map=False, orthogonal=False, orthonormal=False, extend=False): 

""" 

Return the affine hull. 

 

Each polyhedron is contained in some smallest affine subspace 

(possibly the entire ambient space). The affine hull is the 

same polyhedron but thought of as a full-dimensional 

polyhedron in this subspace. We provide a projection of the ambient 

space of the polyhedron to Euclidian space of dimension of the 

polyhedron. Then the image of the polyhedron under this 

projection (or, depending on the parameter ``as_affine_map``, 

the projection itself) is returned. 

 

INPUT: 

 

- ``as_affine_map`` (boolean, default = False) -- If ``False``, return 

a polyhedron. If ``True``, return the affine transformation, 

that sends the embedded polytope to a fulldimensional one. 

It is given as a pair ``(A,b)``, where A is a linear transformation 

and ``b`` is a vector, and the affine transformation sends ``v`` to 

``A(v)+b``. 

 

- ``orthogonal`` (boolean, default = False) -- if ``True``, 

provide an orthogonal transformation. 

 

- ``orthonormal`` (boolean, default = False) -- if ``True``, 

provide an orthonormal transformation. If the base ring does not 

provide the neccessary square roots, the extend parameter 

needs to be set to ``True``. 

 

- ``extend`` (boolean, default = False) -- if ``True``, 

allow base ring to be extended if neccessary. This becomes 

relevant when requiering an orthonormal transformation. 

 

OUTPUT: 

 

A full-dimensional polyhedron or a linear transformation, 

depending on the parameter ``as_affine_map``. 

 

 

.. TODO: 

 

- make the parameters ``orthogonal`` and ``orthonormal`` work with unbounded polyhedra. 

- allow to return ``as_affine_map=True`` for default setting 

 

EXAMPLES:: 

 

sage: triangle = Polyhedron([(1,0,0), (0,1,0), (0,0,1)]); triangle 

A 2-dimensional polyhedron in ZZ^3 defined as the convex hull of 3 vertices 

sage: triangle.affine_hull() 

A 2-dimensional polyhedron in ZZ^2 defined as the convex hull of 3 vertices 

 

sage: half3d = Polyhedron(vertices=[(3,2,1)], rays=[(1,0,0)]) 

sage: half3d.affine_hull().Vrepresentation() 

(A ray in the direction (1), A vertex at (3)) 

 

The resulting affine hulls depend on the parameter ``orthogonal`` and ``orthonormal``:: 

 

sage: L = Polyhedron([[1,0],[0,1]]); L 

A 1-dimensional polyhedron in ZZ^2 defined as the convex hull of 2 vertices 

sage: A = L.affine_hull(); A 

A 1-dimensional polyhedron in ZZ^1 defined as the convex hull of 2 vertices 

sage: A.vertices() 

(A vertex at (0), A vertex at (1)) 

sage: A = L.affine_hull(orthogonal=True); A 

A 1-dimensional polyhedron in QQ^1 defined as the convex hull of 2 vertices 

sage: A.vertices() 

(A vertex at (0), A vertex at (2)) 

sage: A = L.affine_hull(orthonormal=True) 

Traceback (most recent call last): 

... 

ValueError: The base ring needs to be extended; try with "extend=True" 

sage: A = L.affine_hull(orthonormal=True, extend=True); A 

A 1-dimensional polyhedron in AA^1 defined as the convex hull of 2 vertices 

sage: A.vertices() 

(A vertex at (0), A vertex at (1.414213562373095?)) 

 

More generally:: 

 

sage: S = polytopes.simplex(); S 

A 3-dimensional polyhedron in ZZ^4 defined as the convex hull of 4 vertices 

sage: A = S.affine_hull(); A 

A 3-dimensional polyhedron in ZZ^3 defined as the convex hull of 4 vertices 

sage: A.vertices() 

(A vertex at (0, 0, 0), 

A vertex at (0, 0, 1), 

A vertex at (0, 1, 0), 

A vertex at (1, 0, 0)) 

sage: A = S.affine_hull(orthogonal=True); A 

A 3-dimensional polyhedron in QQ^3 defined as the convex hull of 4 vertices 

sage: A.vertices() 

(A vertex at (0, 0, 0), 

A vertex at (2, 0, 0), 

A vertex at (1, 3/2, 0), 

A vertex at (1, 1/2, 4/3)) 

sage: A = S.affine_hull(orthonormal=True, extend=True); A 

A 3-dimensional polyhedron in AA^3 defined as the convex hull of 4 vertices 

sage: A.vertices() 

(A vertex at (0, 0, 0), 

A vertex at (1.414213562373095?, 0, 0), 

A vertex at (0.7071067811865475?, 1.224744871391589?, 0), 

A vertex at (0.7071067811865475?, 0.4082482904638630?, 1.154700538379252?)) 

 

More examples with the ``orthonormal`` parameter:: 

 

sage: P = polytopes.permutahedron(3); P 

A 2-dimensional polyhedron in ZZ^3 defined as the convex hull of 6 vertices 

sage: set([F.as_polyhedron().affine_hull(orthonormal=True, extend=True).volume() for F in P.affine_hull().faces(1)]) == {1, sqrt(AA(2))} 

True 

sage: set([F.as_polyhedron().affine_hull(orthonormal=True, extend=True).volume() for F in P.affine_hull(orthonormal=True, extend=True).faces(1)]) == {sqrt(AA(2))} 

True 

sage: D = polytopes.dodecahedron() 

sage: F = D.faces(2)[0].as_polyhedron() 

sage: F.affine_hull(orthogonal=True) 

A 2-dimensional polyhedron in (Number Field in sqrt5 with defining polynomial x^2 - 5)^2 defined as the convex hull of 5 vertices 

sage: F.affine_hull(orthonormal=True, extend=True) 

A 2-dimensional polyhedron in AA^2 defined as the convex hull of 5 vertices 

sage: K.<sqrt2> = QuadraticField(2) 

sage: P = Polyhedron([2*[K.zero()],2*[sqrt2]]) 

sage: K.<sqrt2> = QuadraticField(2) 

sage: P = Polyhedron([2*[K.zero()],2*[sqrt2]]); P 

A 1-dimensional polyhedron in (Number Field in sqrt2 with defining polynomial x^2 - 2)^2 defined as the convex hull of 2 vertices 

sage: P.vertices() 

(A vertex at (0, 0), A vertex at (sqrt2, sqrt2)) 

sage: A = P.affine_hull(orthonormal=True); A 

A 1-dimensional polyhedron in (Number Field in sqrt2 with defining polynomial x^2 - 2)^1 defined as the convex hull of 2 vertices 

sage: A.vertices() 

(A vertex at (0), A vertex at (2)) 

sage: K.<sqrt3> = QuadraticField(3) 

sage: P = Polyhedron([2*[K.zero()],2*[sqrt3]]); P 

A 1-dimensional polyhedron in (Number Field in sqrt3 with defining polynomial x^2 - 3)^2 defined as the convex hull of 2 vertices 

sage: P.vertices() 

(A vertex at (0, 0), A vertex at (sqrt3, sqrt3)) 

sage: A = P.affine_hull(orthonormal=True) 

Traceback (most recent call last): 

... 

ValueError: The base ring needs to be extended; try with "extend=True" 

sage: A = P.affine_hull(orthonormal=True, extend=True); A 

A 1-dimensional polyhedron in AA^1 defined as the convex hull of 2 vertices 

sage: A.vertices() 

(A vertex at (0), A vertex at (2.449489742783178?)) 

sage: sqrt(6).n() 

2.44948974278318 

 

 

 

The affine hull is combinatorially equivalent to the input:: 

 

sage: P.is_combinatorially_isomorphic(P.affine_hull()) 

True 

sage: P.is_combinatorially_isomorphic(P.affine_hull(orthogonal=True)) 

True 

sage: P.is_combinatorially_isomorphic(P.affine_hull(orthonormal=True, extend=True)) 

True 

 

The ``orthonormal=True`` parameter preserves volumes; 

it provides an isometric copy of the polyhedron:: 

 

sage: Pentagon = polytopes.dodecahedron().faces(2)[0].as_polyhedron() 

sage: P = Pentagon.affine_hull(orthonormal=True, extend=True) 

sage: _, c= P.is_inscribed(certificate=True) 

sage: c 

(0.4721359549995794?, 0.6498393924658126?) 

sage: circumradius = (c-vector(P.vertices()[0])).norm() 

sage: p = polytopes.regular_polygon(5) 

sage: p.volume() 

2.377641290737884? 

sage: P.volume() 

1.53406271079097? 

sage: p.volume()*circumradius^2 

1.534062710790965? 

sage: P.volume() == p.volume()*circumradius^2 

True 

 

One can also use ``orthogonal`` parameter to calculate volumes; 

in this case we don't need to switch base rings. One has to divide 

by the square root of the determinant of the linear part of the 

affine transformation times its transpose:: 

 

sage: Pentagon = polytopes.dodecahedron().faces(2)[0].as_polyhedron() 

sage: Pnormal = Pentagon.affine_hull(orthonormal=True, extend=True) 

sage: Pgonal = Pentagon.affine_hull(orthogonal=True) 

sage: A,b = Pentagon.affine_hull(orthogonal = True, as_affine_map=True) 

sage: Adet = (A.matrix().transpose()*A.matrix()).det() 

sage: Pnormal.volume() 

1.53406271079097? 

sage: Pgonal.volume()/sqrt(Adet) 

-80*(55*sqrt(5) - 123)/sqrt(-6368*sqrt(5) + 14240) 

sage: Pgonal.volume()/sqrt(Adet).n(digits=20) 

1.5340627107909646651 

sage: AA(Pgonal.volume()^2) == (Pnormal.volume()^2)*AA(Adet) 

True 

 

An other example with ``as_affine_map=True``:: 

 

sage: P = polytopes.permutahedron(4) 

sage: A,b = P.affine_hull(orthonormal=True, as_affine_map=True, extend=True) 

sage: Q = P.affine_hull(orthonormal=True, extend=True) 

sage: Q.center() 

(0.7071067811865475?, 1.224744871391589?, 1.732050807568878?) 

sage: A(P.center()) + b == Q.center() 

True 

 

 

For unbounded, non full-dimensional polyhedra, the ``orthogonal=True`` and ``orthonormal=True`` 

is not implemented:: 

 

sage: P = Polyhedron(ieqs=[[0, 1, 0], [0, 0, 1], [0, 0, -1]]); P 

A 1-dimensional polyhedron in QQ^2 defined as the convex hull of 1 vertex and 1 ray 

sage: P.is_compact() 

False 

sage: P.is_full_dimensional() 

False 

sage: P.affine_hull(orthogonal=True) 

Traceback (most recent call last): 

... 

NotImplementedError: "orthogonal=True" and "orthonormal=True" work only for compact polyhedra 

sage: P.affine_hull(orthonormal=True) 

Traceback (most recent call last): 

... 

NotImplementedError: "orthogonal=True" and "orthonormal=True" work only for compact polyhedra 

 

Setting ``as_affine_map`` to ``True`` only works in combination 

with ``orthogonal`` or ``orthonormal`` set to ``True``:: 

 

sage: S = polytopes.simplex() 

sage: S.affine_hull(as_affine_map=True) 

Traceback (most recent call last): 

... 

NotImplementedError: "as_affine_map=True" only works with "orthogonal=True" and "orthonormal=True" 

 

If the polyhedron is full-dimensional, it is returned:: 

 

sage: polytopes.cube().affine_hull() 

A 3-dimensional polyhedron in ZZ^3 defined as the convex hull of 8 vertices 

sage: polytopes.cube().affine_hull(as_affine_map=True) 

(Vector space morphism represented by the matrix: 

[1 0 0] 

[0 1 0] 

[0 0 1] 

Domain: Vector space of dimension 3 over Rational Field 

Codomain: Vector space of dimension 3 over Rational Field, (0, 0, 0)) 

 

TESTS:: 

 

Check that :trac:`23355` is fixed:: 

 

sage: P = Polyhedron([[7]]); P 

A 0-dimensional polyhedron in ZZ^1 defined as the convex hull of 1 vertex 

sage: P.affine_hull() 

A 0-dimensional polyhedron in ZZ^0 defined as the convex hull of 1 vertex 

sage: P.affine_hull(orthonormal='True') 

A 0-dimensional polyhedron in QQ^0 defined as the convex hull of 1 vertex 

sage: P.affine_hull(orthogonal='True') 

A 0-dimensional polyhedron in QQ^0 defined as the convex hull of 1 vertex 

 

Check that :trac:`24047` is fixed:: 

 

sage: P1 = Polyhedron(vertices=([[-1, 1], [0, -1], [0, 0], [-1, -1]])) 

sage: P2 = Polyhedron(vertices=[[1, 1], [1, -1], [0, -1], [0, 0]]) 

sage: P = P1.intersection(P2) 

sage: A, b = P.affine_hull(as_affine_map=True, orthonormal=True, extend=True) 

 

sage: Polyhedron([(2,3,4)]).affine_hull() 

A 0-dimensional polyhedron in ZZ^0 defined as the convex hull of 1 vertex 

""" 

# handle trivial full-dimensional case 

if self.ambient_dim() == self.dim(): 

if as_affine_map: 

return linear_transformation(matrix(self.base_ring(), self.dim(), self.dim(), self.base_ring().one())), self.ambient_space().zero() 

return self 

 

if orthogonal or orthonormal: 

# see TODO 

if not self.is_compact(): 

raise NotImplementedError('"orthogonal=True" and "orthonormal=True" work only for compact polyhedra') 

# translate 0th vertex to the origin 

Q = self.translation(-vector(self.vertices()[0])) 

v = next((_ for _ in Q.vertices() if _.vector() == Q.ambient_space().zero()), None) 

# finding the zero in Q; checking that Q actually has a vertex zero 

assert v.vector() == Q.ambient_space().zero() 

# choose as an affine basis the neighbors of the origin vertex in Q 

M = matrix(self.base_ring(), self.dim(), self.ambient_dim(), [list(w) for w in itertools.islice(v.neighbors(), self.dim())]) 

# Switch base_ring to AA if neccessary, 

# since gram_schmidt needs to be able to take square roots. 

# Pick orthonormal basis and transform all vertices accordingly 

# if the orthonormal transform makes it neccessary, change base ring. 

try: 

A = M.gram_schmidt(orthonormal=orthonormal)[0] 

except TypeError: 

if not extend: 

raise ValueError('The base ring needs to be extended; try with "extend=True"') 

M = matrix(AA, M) 

A = M.gram_schmidt(orthonormal=orthonormal)[0] 

if as_affine_map: 

return linear_transformation(A, side='right'), -A*vector(A.base_ring(), self.vertices()[0]) 

return Polyhedron([A*vector(A.base_ring(), v) for v in Q.vertices()], base_ring=A.base_ring()) 

 

# translate one vertex to the origin 

v0 = self.vertices()[0].vector() 

gens = [] 

for v in self.vertices()[1:]: 

gens.append(v.vector() - v0) 

for r in self.rays(): 

gens.append(r.vector()) 

for l in self.lines(): 

gens.append(l.vector()) 

 

# Pick subset of coordinates to coordinatize the affine span 

pivots = matrix(gens).pivots() 

 

def pivot(indexed): 

return [indexed[i] for i in pivots] 

 

vertices = [pivot(_) for _ in self.vertices()] 

rays = [pivot(_) for _ in self.rays()] 

lines = [pivot(_) for _ in self.lines()] 

if as_affine_map: 

raise NotImplementedError('"as_affine_map=True" only works with "orthogonal=True" and "orthonormal=True"') 

return Polyhedron(vertices=vertices, rays=rays, lines=lines, base_ring=self.base_ring()) 

 

def _polymake_init_(self): 

""" 

Return a polymake "Polytope" object corresponding to ``self``. 

 

EXAMPLES:: 

 

sage: P = polytopes.cube() 

sage: PP = polymake(P) # optional - polymake 

sage: PP.N_VERTICES # optional - polymake 

8 

 

Lower-dimensional polyhedron:: 

 

sage: P = Polyhedron(vertices=[[1, 0], [0, 1]]) 

sage: PP = polymake(P) # optional - polymake 

sage: PP.COMBINATORIAL_DIM # optional - polymake 

1 

sage: PP.AFFINE_HULL # optional - polymake 

-1 1 1 

 

Empty polyhedron:: 

 

sage: P = Polyhedron(ambient_dim=2, vertices=[]) 

sage: PP = polymake(P) # optional - polymake 

sage: PP.COMBINATORIAL_DIM # optional - polymake 

-1 

 

Pointed unbounded polyhedron:: 

 

sage: P = Polyhedron(vertices=[[1, 0], [0, 1]], rays=[[1, 0]]) 

sage: PP = polymake(P) # optional - polymake 

sage: PP.VERTICES # optional - polymake 

1 0 1 

1 1 0 

0 1 0 

sage: PP.FACETS # optional - polymake 

1 0 -1 

-1 1 1 

0 0 1 

 

Non-pointed polyhedron:: 

 

sage: P = Polyhedron(vertices=[[1, 0], [0, 1]], lines=[[1, 0]]) 

sage: PP = polymake(P) # optional - polymake 

sage: PP.VERTICES # optional - polymake 

1 0 1 

1 0 0 

sage: PP.FACETS # optional - polymake 

1 0 -1 

0 0 1 

sage: PP.LINEALITY_SPACE # optional - polymake 

0 1 0 

 

Algebraic polyhedron:: 

 

sage: P = polytopes.dodecahedron(); P 

A 3-dimensional polyhedron in (Number Field in sqrt5 with defining polynomial x^2 - 5)^3 defined as the convex hull of 20 vertices 

sage: print("There may be a recompilation warning"); PP = polymake(P); PP # optional - polymake 

There may be a recompilation warning... 

Polytope<QuadraticExtension<Rational>>[...] 

sage: sorted(PP.VERTICES[:], key=repr)[0] # optional - polymake 

1 -1+1r5 -4+2r5 0 

 

Floating-point polyhedron:: 

 

sage: P = polytopes.dodecahedron(exact=False); P 

A 3-dimensional polyhedron in RDF^3 defined as the convex hull of 20 vertices 

sage: print("There may be a recompilation warning"); PP = polymake(P); PP # optional - polymake 

There may be a recompilation warning... 

Polytope<Float>[...] 

sage: sorted(PP.VERTICES[:], key=repr)[0] # optional - polymake 

1 -0.472135955 0 -1.236067978 

 

""" 

from sage.interfaces.polymake import polymake 

polymake_field = polymake(self.base_ring().fraction_field()) 

polymake_class = "Polytope<{}>".format(polymake_field) 

if self.is_empty(): 

# Polymake 3.1 cannot enter an empty polyhedron using 

# FACETS and AFFINE_HULL. Use corresponding input properties instead. 

# https://forum.polymake.org/viewtopic.php?f=8&t=545 

return polymake.new_object(polymake_class, 

INEQUALITIES=self.inequalities_list(), 

EQUATIONS=self.equations_list()) 

else: 

return polymake.new_object(polymake_class, 

FACETS=self.inequalities_list(), 

AFFINE_HULL=self.equations_list(), 

VERTICES= [ [1] + v for v in self.vertices_list() ] \ 

+ [ [0] + r for r in self.rays_list() ], 

LINEALITY_SPACE=[ [0] + l for l in self.lines_list() ])