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

r""" 

Interface to polymake 

 

""" 

 

 

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

# Copyright (C) 2017 Simon King <simon.king@uni-jena.de> 

# 

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

# 

# This code is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 

# General Public License for more details. 

# 

# The full text of the GPL is available at: 

# 

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

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

from __future__ import print_function 

from __future__ import absolute_import 

 

import os 

import re 

import sys 

import six 

 

from sage.structure.parent import Parent 

from .expect import console, Expect, ExpectElement, ExpectFunction, FunctionElement 

 

from sage.env import SAGE_EXTCODE, DOT_SAGE 

from sage.misc.misc import get_verbose 

from sage.misc.cachefunc import cached_method 

from sage.interfaces.tab_completion import ExtraTabCompletion 

 

import pexpect 

from random import randrange 

 

from time import sleep 

from six.moves import range 

from six import reraise as raise_ 

import warnings 

 

_name_pattern = re.compile('SAGE[0-9]+') 

 

_available_polymake_answers = { 

0: "returns prompt", 

1: "returns continuation prompt", 

2: "requests interactive input", 

3: "kills computation", 

4: "raises error", 

5: "issues warning", 

6: "shows additional information", 

7: "lost connection", 

8: "fails to respond timely" 

} 

 

class PolymakeError(RuntimeError): 

""" 

Raised if polymake yields an error message. 

 

TESTS:: 

 

sage: polymake.eval('print foo;') # optional polymake 

Traceback (most recent call last): 

... 

PolymakeError: Unquoted string "foo" may clash with future reserved word... 

 

""" 

pass 

 

def polymake_console(command=''): 

""" 

Spawn a new polymake command-line session. 

 

EXAMPLES:: 

 

sage: from sage.interfaces.polymake import polymake_console 

sage: polymake_console() # not tested 

Welcome to polymake version ... 

... 

Ewgenij Gawrilow, Michael Joswig (TU Berlin) 

http://www.polymake.org 

 

This is free software licensed under GPL; see the source for copying conditions. 

There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 

 

Press F1 or enter 'help;' for basic instructions. 

 

Application polytope currently uses following third-party software packages: 

4ti2, bliss, cdd, latte, libnormaliz, lrs, permlib, ppl, sketch, sympol, threejs, tikz, topcom, tosimplex 

For more details: show_credits; 

polytope > 

 

""" 

from sage.repl.rich_output.display_manager import get_display_manager 

if not get_display_manager().is_in_terminal(): 

raise RuntimeError('Can use the console only in the terminal. Try %%polymake magics instead.') 

os.system(command or os.getenv('SAGE_POLYMAKE_COMMAND') or 'polymake') 

 

class Polymake(ExtraTabCompletion, Expect): 

r""" 

Interface to the polymake interpreter. 

 

In order to use this interface, you need to either install the 

optional polymake package for Sage, or install polymake system-wide 

on your computer. 

 

Type ``polymake.[tab]`` for a list of most functions 

available from your polymake install. Type 

``polymake.Function?`` for polymake's help about a given ``Function`` 

Type ``polymake(...)`` to create a new Magma 

object, and ``polymake.eval(...)`` to run a string using 

polymake and get the result back as a string. 

 

EXAMPLES:: 

 

sage: p = polymake.rand_sphere(4, 20, seed=5) # optional - polymake 

sage: p # optional - polymake 

Random spherical polytope of dimension 4; seed=5... 

sage: set_verbose(3) 

sage: p.H_VECTOR # optional - polymake 

used package ppl 

The Parma Polyhedra Library (PPL): A C++ library for convex polyhedra 

and other numerical abstractions. 

http://www.cs.unipr.it/ppl/ 

1 16 47 16 1 

sage: set_verbose(0) 

sage: p.F_VECTOR # optional - polymake 

20 101 162 81 

sage: print(p.F_VECTOR._sage_doc_()) # optional - polymake # random 

property_types/Algebraic Types/Vector: 

A type for vectors with entries of type Element. 

 

You can perform algebraic operations such as addition or scalar multiplication. 

 

You can create a new Vector by entering its elements, e.g.: 

$v = new Vector<Int>(1,2,3); 

or 

$v = new Vector<Int>([1,2,3]); 

 

.. automethod:: _eval_line 

""" 

def __init__(self, script_subdirectory=None, 

logfile=None, server=None,server_tmpdir=None, 

seed=None, command=None): 

""" 

TESTS:: 

 

sage: from sage.interfaces.polymake import Polymake 

sage: Polymake() 

Polymake 

sage: Polymake().is_running() 

False 

 

""" 

if command is None: 

command = "env TERM=dumb {}".format(os.getenv('SAGE_POLYMAKE_COMMAND') or 'polymake') 

Expect.__init__(self, 

name="polymake", 

command=command, 

prompt="polytope > ", 

server=server, 

server_tmpdir=server_tmpdir, 

script_subdirectory=script_subdirectory, 

restart_on_ctrlc=False, 

logfile=logfile, 

eval_using_file_cutoff=1024) # > 1024 causes hangs 

 

self._seed = seed 

self.__tab_completion = {} 

 

@cached_method 

def version(self): 

""" 

Version of the polymake installation. 

 

EXAMPLES:: 

 

sage: polymake.version() # optional - polymake 

'3...' 

 

TESTS:: 

 

sage: from sage.interfaces.polymake import Polymake 

sage: Polymake(command='foobar').version() 

Traceback (most recent call last): 

... 

RuntimeError: unable to start polymake because the command 'foobar' failed: 

The command was not found or was not executable: foobar. 

Please install the optional polymake package for sage (but read its SPKG.txt first!) 

or install polymake system-wide 

 

""" 

return self.get('$Polymake::Version') 

 

# Pickling etc 

 

def __reduce__(self): 

""" 

EXAMPLES:: 

 

sage: loads(dumps(polymake)) is polymake 

True 

 

""" 

return reduce_load_Polymake, tuple([]) 

 

def _object_class(self): 

""" 

Return the class by which elements in this interface are implemented. 

 

TESTS:: 

 

sage: C = polymake('cube(3)') # indirect doctest # optional - polymake 

sage: C # optional - polymake 

cube of dimension 3 

sage: type(C) # optional - polymake 

<class 'sage.interfaces.polymake.PolymakeElement'> 

 

""" 

return PolymakeElement 

 

def _function_element_class(self): 

""" 

Return the class by which member functions of this interface are implemented. 

 

TESTS: 

 

We use ellipses in the tests, to make it more robust against future 

changes in polymake:: 

 

sage: p = polymake.rand_sphere(4, 20, seed=5) # optional - polymake 

sage: p.get_schedule # optional - polymake # indirect doctest 

Member function 'get_schedule' of Polymake::polytope::Polytope__Rational object 

sage: p.get_schedule('F_VECTOR') # optional - polymake # random 

CONE_DIM : RAYS | INPUT_RAYS 

precondition : BOUNDED ( POINTED : ) 

POINTED : 

N_INPUT_RAYS : INPUT_RAYS 

precondition : N_RAYS | N_INPUT_RAYS ( ppl.convex_hull.primal: FACETS, LINEAR_SPAN : RAYS | INPUT_RAYS ) 

sensitivity check for FacetPerm 

ppl.convex_hull.primal: FACETS, LINEAR_SPAN : RAYS | INPUT_RAYS 

INPUT_RAYS_IN_FACETS : INPUT_RAYS, FACETS 

sensitivity check for VertexPerm 

RAYS_IN_FACETS, RAYS, LINEALITY_SPACE : INPUT_RAYS_IN_FACETS, INPUT_RAYS 

GRAPH.ADJACENCY : RAYS_IN_FACETS 

DUAL_GRAPH.ADJACENCY : RAYS_IN_FACETS 

N_EDGES : ADJACENCY ( applied to GRAPH ) 

N_EDGES : ADJACENCY ( applied to DUAL_GRAPH ) 

precondition : POINTED ( LINEALITY_DIM, LINEALITY_SPACE : ) 

LINEALITY_DIM, LINEALITY_SPACE : 

COMBINATORIAL_DIM : CONE_DIM, LINEALITY_DIM 

N_RAYS : RAYS 

N_FACETS : FACETS 

precondition : COMBINATORIAL_DIM ( F_VECTOR : N_FACETS, N_RAYS, GRAPH.N_EDGES, DUAL_GRAPH.N_EDGES, COMBINATORIAL_DIM ) 

F_VECTOR : N_FACETS, N_RAYS, GRAPH.N_EDGES, DUAL_GRAPH.N_EDGES, COMBINATORIAL_DIM 

 

""" 

return PolymakeFunctionElement 

 

def function_call(self, function, args=None, kwds=None): 

""" 

EXAMPLES:: 

 

sage: polymake.rand_sphere(4, 30, seed=15) # optional - polymake # indirect doctest 

Random spherical polytope of dimension 4; seed=15... 

 

""" 

args, kwds = self._convert_args_kwds(args, kwds) 

self._check_valid_function_name(function) 

s = self._function_call_string(function, 

[s.name() for s in args], 

['%s=>%s'%(key,value.name()) for key, value in kwds.items()]) 

return self(s) 

 

def _function_call_string(self, function, args, kwds): 

""" 

Returns the string used to make function calls. 

 

EXAMPLES:: 

 

sage: polymake._function_call_string('cube', ['2','7','3'], ['group=>1']) # optional - polymake 

'cube(2,7,3, group=>1);' 

sage: c = polymake('cube(2,7,3, group=>1)') # optional - polymake 

sage: c.VERTICES # optional - polymake 

1 3 3 

1 7 3 

1 3 7 

1 7 7 

sage: c.GROUP # optional - polymake 

full combinatorial group on facets... 

 

""" 

if kwds: 

if args: 

return "%s(%s, %s);"%(function, ",".join(list(args)), ",".join(list(kwds))) 

return "%s(%s);"%(function, ",".join(list(kwds))) 

return "%s(%s);"%(function, ",".join(list(args))) 

 

def console(self): 

""" 

Raise an error, pointing to :meth:`~sage.interfaces.interface.Interface.interact` and :func:`polymake_console`. 

 

EXAMPLES:: 

 

sage: polymake.console() 

Traceback (most recent call last): 

... 

NotImplementedError: Please use polymake_console() function or the .interact() method 

 

""" 

raise NotImplementedError("Please use polymake_console() function or the .interact() method") 

 

# Methods concerning interface communication 

 

def _install_hints(self): 

""" 

TESTS:: 

 

sage: print(polymake._install_hints()) 

Please install the optional polymake package for sage (but read its SPKG.txt first!) 

or install polymake system-wide 

 

""" 

return "Please install the optional polymake package for sage (but read its SPKG.txt first!)"+os.linesep+"or install polymake system-wide" 

 

def _start(self, alt_message=None): 

""" 

Start the polymake interface in the application "polytope". 

 

NOTE: 

 

There should be no need to call this explicitly. 

 

TESTS:: 

 

sage: polymake.application('fan') # optional - polymake 

sage: 'normal_fan' in dir(polymake) # optional - polymake 

True 

sage: polymake.quit() # optional - polymake 

sage: polymake._start() # optional - polymake 

 

Since 'normal_fan' is not defined in the polymake application 'polytope', 

we now get 

:: 

 

sage: 'normal_fan' in dir(polymake) # optional - polymake 

False 

 

""" 

if not self.is_running(): 

self._change_prompt("polytope > ") 

Expect._start(self, alt_message=None) 

self.application("polytope") 

self.eval('use Scalar::Util qw(reftype);') 

self.eval('use Scalar::Util qw(blessed);') 

self.eval('use File::Slurp;') 

 

def _quit_string(self): 

""" 

TESTS:: 

 

sage: polymake._quit_string() 

'exit;' 

""" 

return "exit;" 

 

def _assign_symbol(self): 

""" 

TESTS:: 

 

sage: polymake._assign_symbol() 

'=' 

""" 

return "=" 

 

def _equality_symbol(self): 

""" 

TESTS:: 

 

sage: polymake._equality_symbol() 

'==' 

""" 

return "==" 

 

def _read_in_file_command(self, filename): 

""" 

TESTS:: 

 

sage: polymake._read_in_file_command('foobar') 

'eval read_file "foobar";\n' 

 

Force use of file:: 

 

sage: L = polymake([42] * 400) # optional - polymake 

sage: len(L) # optional - polymake 

400 

 

Just below standard file cutoff of 1024:: 

 

sage: L = polymake([42] * 84) # optional - polymake 

sage: len(L) # optional - polymake 

84 

 

""" 

return 'eval read_file "{}";\n'.format(filename) 

 

def _keyboard_interrupt(self): 

""" 

Interrupt a computation with <Ctrl-c> 

 

TESTS: 

 

For reasons that are not clear to the author, the following test 

is very flaky. Therefore, this test is marked as "not tested". 

 

sage: c = polymake.cube(15) # optional - polymake 

sage: alarm(1) # not tested 

sage: try: # not tested # indirect doctest 

....: c.F_VECTOR 

....: except KeyboardInterrupt: 

....: pass 

Interrupting Polymake... 

doctest:warning 

... 

RuntimeWarning: We ignore that Polymake issues warning during keyboard interrupt 

doctest:warning 

... 

RuntimeWarning: We ignore that Polymake raises error during keyboard interrupt 

 

Afterwards, the interface should still be running. :: 

 

sage: c.N_FACETS # optional - polymake 

30 

 

""" 

if not self.is_running(): 

raise KeyboardInterrupt 

print("Interrupting %s..." % self) 

while True: 

try: 

self._expect.send(chr(3)) 

except pexpect.ExceptionPexpect as msg: 

raise pexpect.ExceptionPexpect("THIS IS A BUG -- PLEASE REPORT. This should never happen.\n" + msg) 

sleep(0.1) 

i = self._expect.expect_list(self._prompt, timeout=1) 

if i==0: 

break 

elif i==7: # EOF 

warnings.warn("Polymake {} during keyboard interrupt".format(_available_polymake_answers[i]), RuntimeWarning) 

self._crash_msg() 

self.quit() 

elif i==8: # Timeout 

self.quit() 

raise RuntimeError("{} interface is not responding. We closed it".format(self)) 

elif i!=3: # Anything but a "computation killed" 

warnings.warn("We ignore that {} {} during keyboard interrupt".format(self, _available_polymake_answers[i]), RuntimeWarning) 

raise KeyboardInterrupt("Ctrl-c pressed while running %s"%self) 

 

def _synchronize(self): 

""" 

TESTS:: 

 

sage: Q = polymake.cube(4) # optional - polymake 

sage: polymake('"ok"') # optional - polymake 

ok 

sage: polymake._expect.sendline() # optional - polymake 

1 

 

Now the interface is badly out of sync:: 

 

sage: polymake('"foobar"') # optional - polymake 

<repr(<sage.interfaces.polymake.PolymakeElement at ...>) failed: 

PolymakeError: Can't locate object method "description" via package "1" 

(perhaps you forgot to load "1"?)...> 

sage: Q.typeof() # optional - polymake 

('foobar...', 'Polymake::polytope::Polytope__Rational') 

sage: Q.typeof.clear_cache() # optional - polymake 

 

After synchronisation, things work again as expected:: 

 

sage: polymake._synchronize() # optional - polymake 

doctest:warning 

... 

UserWarning: Polymake seems out of sync: 

The expected output did not appear before reaching the next prompt. 

sage: polymake('"back to normal"') # optional - polymake 

back to normal 

sage: Q.typeof() # optional - polymake 

('Polymake::polytope::Polytope__Rational', 'ARRAY') 

 

""" 

if not self.is_running(): 

return 

rnd = randrange(2147483647) 

res = str(rnd+1) 

cmd='print 1+{};'+self._expect.linesep 

self._sendstr(cmd.format(rnd)) 

pat = self._expect.expect(self._prompt,timeout=0.5) 

# 0: normal prompt 

# 1: continuation prompt 

# 2: user input expected when requestion "help" 

# 3: what we are looking for when interrupting a computation 

# 4: error 

# 5: warning 

# 6: anything but an error or warning, thus, an information 

# 7: unexpected end of the stream 

# 8: (expected) timeout 

if pat == 8: # timeout 

warnings.warn("{} unexpectedly {} during synchronisation.".format(self, _available_polymake_answers[pat]), RuntimeWarning) 

self.interrupt() 

# ... but we continue, as that probably means we currently are at the end of the buffer 

elif pat == 7: # EOF 

self._crash_msg() 

self.quit() 

elif pat == 0: 

# We got the right prompt, but perhaps in a wrong position in the stream 

# The result of the addition should appear *before* our prompt 

if not res in self._expect.before: 

try: 

warnings.warn("{} seems out of sync: The expected output did not appear before reaching the next prompt.".format(self)) 

while True: 

i = self._expect.expect_list(self._prompt, timeout=0.1) 

if i==8: # This time, we do expect a timeout 

return 

elif i>0: 

raise RuntimeError("Polymake unexpectedly {}".format(_available_polymake_answers[i])) 

except pexpect.TIMEOUT: 

warnings.warn("A timeout has occured when synchronising {}.".format(self), RuntimeWarning) 

self._interrupt() 

except pexpect.EOF: 

self._crash_msg() 

self.quit() 

else: 

return 

else: 

raise RuntimeError("Polymake unexpectedly {}".format(_available_polymake_answers[pat])) 

 

def _next_var_name(self): 

r""" 

Returns the next unused variable name. 

 

TESTS:: 

 

sage: print(polymake._next_var_name()) 

SAGE... 

 

""" 

if len(self._available_vars) != 0: 

return self._available_vars.pop(0) 

try: 

self.__seq += 1 

except AttributeError: 

self.__seq = 0 

return r'SAGE%s'%self.__seq 

 

def clear(self, var): 

""" 

Clear the variable named var. 

 

NOTE: 

 

This is implicitly done when deleting an element in the interface. 

 

TESTS:: 

 

sage: c = polymake.cube(15) # optional - polymake 

sage: polymake._available_vars = [] # optional - polymake 

sage: old = c._name # optional - polymake 

sage: del c # optional - polymake # indirect doctest 

sage: len(polymake._available_vars) # optional - polymake 

1 

sage: polymake._next_var_name() in old # optional - polymake 

True 

 

""" 

self._available_vars.append(_name_pattern.search(var).group()) 

 

def _create(self, value, name=None): 

""" 

Assign a value to a name in the polymake interface. 

 

INPUT: 

 

- ``value``, a string: Polymake command (or value) whose result 

is to be assigned to a variable 

- ``name``, optional string: If given, the new variable has this name. 

Otherwise, the name is automatically generated. 

 

RETURN: 

 

The command by which the assigned value can now be retrieved. 

 

NOTE: 

 

In order to overcome problems with the perl programming language, 

we store *all* data as arrays. If the given value is an array 

of length different from one, then the new variable contains that 

array. Otherwise, the new variable is an array of length one whose 

only entry is the given value, which has to be a scalar (which 

also includes Perl references). In other words, perl hashes 

are not suitable. 

 

EXAMPLES:: 

 

sage: polymake._create("('foo', 'bar')", name="my_array") # optional - polymake 

'@my_array' 

sage: print(polymake.eval('print join(", ", @my_array);')) # optional - polymake 

foo, bar 

sage: polymake._create("'foobar'", name="my_string") # optional - polymake 

'$my_string[0]' 

sage: print(polymake.eval('print $my_string[0];')) # optional - polymake 

foobar 

 

""" 

name = self._next_var_name() if name is None else name 

self.set(name, value) 

# If value is a list, then @name is now equal to that list. 

# Otherwise, value is obtained by $name[0]. So, we modify 

# the name returned by _create so that it can be used to 

# access the wrapped value. 

if self.eval('print scalar @{};'.format(name)).strip() == '1': 

return '$'+name+'[0]' 

return '@'+name 

 

def set(self, var, value): 

""" 

Set the variable var to the given value. 

 

Eventually, ``var`` is a reference to ``value``. 

 

.. WARNING:: 

 

This method, although it doesn't start with an underscore, is 

an internal method and not part of the interface. So, please do 

not try to call it explicitly. Instead, use the polymake interface 

as shown in the examples. 

 

REMARK: 

 

Polymake's user language is Perl. In Perl, if one wants to assign 

the return value of a function to a variable, the syntax to do so 

depends on the type of the return value. While this is fine in 

compiled code, it seems quite awkward in user interaction. 

 

To make this polymake pexpect interface a bit more user friendly, 

we treat *all* variables as arrays. A scalar value (most typically 

a reference) is thus interpreted as the only item in an array of 

length one. It is, of course, possible to use the interface without 

knowing these details. 

 

EXAMPLES:: 

 

sage: c = polymake('cube(3)') # optional - polymake # indirect doctest 

sage: d = polymake.cube(3) # optional - polymake 

 

Equality is, for "big" objects such as polytopes, comparison by 

identity:: 

 

sage: c == d # optional - polymake 

False 

 

However, the list of vertices is equal:: 

 

sage: c.VERTICES == d.VERTICES # optional - polymake 

True 

 

TESTS: 

 

The following shows how polymake variables are wrapped in our interface. 

It should, however, **never** be needed to do the following 

*explicitly*:: 

 

sage: polymake.set('myvar', 'cube(3)') # optional - polymake 

sage: polymake.get('$myvar[0]') # optional - polymake 

'Polymake::polytope::Polytope__Rational=ARRAY(...)' 

 

The following tests against :trac:`22658`:: 

 

sage: P = polymake.new_object("Polytope", FACETS=[[12, -2, -3, -5, -8, -13, -21, -34, -55], # optional - polymake 

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

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

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

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

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

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

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

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

sage: P.VERTICES # optional - polymake 

1 6 0 0 0 0 0 0 0 

1 0 4 0 0 0 0 0 0 

1 0 0 0 0 0 0 0 0 

1 0 0 12/5 0 0 0 0 0 

1 0 0 0 0 0 0 0 12/55 

1 0 0 0 0 0 0 6/17 0 

1 0 0 0 0 0 4/7 0 0 

1 0 0 0 0 12/13 0 0 0 

1 0 0 0 3/2 0 0 0 0 

sage: P.F_VECTOR # optional - polymake 

9 36 84 126 126 84 36 9 

 

""" 

if isinstance(value, six.string_types): 

value = value.strip().rstrip(';').strip() 

cmd = '@%s%s(%s);'%(var,self._assign_symbol(), value) 

self.eval(cmd) 

 

def get(self, cmd): 

""" 

Return the string representation of an object in the polymake interface. 

 

EXAMPLES:: 

 

sage: polymake.get('cube(3)') # optional - polymake 

'Polymake::polytope::Polytope__Rational=ARRAY(...)' 

 

Note that the above string representation is what polymake provides. 

In our interface, we use what polymake calls a "description":: 

 

sage: polymake('cube(3)') # optional - polymake 

cube of dimension 3 

 

 

""" 

return self.eval("print {};".format(cmd)).strip() 

 

def help(self, topic, pager=True): 

""" 

Displays polymake's help on a given topic, as a string. 

 

INPUT: 

 

- ``topic``, a string 

- ``pager``, optional bool, default ``True``: When True, display help, otherwise return as a string. 

 

EXAMPLES:: 

 

sage: print(polymake.help('Polytope', pager=False)) # optional - polymake # random 

objects/Polytope: 

Not necessarily bounded or unbounded polyhedron. 

Nonetheless, the name "Polytope" is used for two reasons: 

Firstly, combinatorially we always deal with polytopes; see the description of VERTICES_IN_FACETS for details. 

The second reason is historical. 

We use homogeneous coordinates, which is why Polytope is derived from Cone. 

Note that a pointed polyhedron is projectively equivalent to a polytope. 

Scalar is the numeric data type used for the coordinates. 

 

In some cases, polymake expects user interaction to choose from 

different available help topics. In these cases, a warning is given, 

and the available help topics are displayed resp. printed, without 

user interaction:: 

 

sage: polymake.help('TRIANGULATION') # optional - polymake # random 

doctest:warning 

... 

UserWarning: Polymake expects user interaction. We abort and return the options that Polymake provides. 

There are 5 help topics matching 'TRIANGULATION': 

1: objects/Visualization/Visual::Polytope/methods/TRIANGULATION 

2: objects/Visualization/Visual::PointConfiguration/methods/TRIANGULATION 

3: objects/Cone/properties/Triangulation and volume/TRIANGULATION 

4: objects/PointConfiguration/properties/Triangulation and volume/TRIANGULATION 

5: objects/Polytope/properties/Triangulation and volume/TRIANGULATION 

 

If an unknown help topic is requested, a :class:`PolymakeError` 

results:: 

 

sage: polymake.help('Triangulation') # optional - polymake 

Traceback (most recent call last): 

... 

PolymakeError: unknown help topic 'Triangulation' 

""" 

H = self.eval('help("{}");\n'.format(topic)) 

if pager: 

from IPython.core.page import page 

page(H, start = 0) 

else: 

return H 

 

def _eval_line(self, line, allow_use_file=True, wait_for_prompt=True, restart_if_needed=True, **kwds): 

r""" 

Evaluate a command. 

 

INPUT: 

 

- ``line``, a command (string) to be evaluated 

- ``allow_use_file`` (optional bool, default ``True``), whether or not 

to use a file if the line is very long. 

- ``wait_for_prompt`` (optional, default ``True``), whether or not 

to wait before polymake returns a prompt. If it is a string, it is considered 

as alternative prompt to be waited for. 

- ``restart_if_needed`` (optional bool, default ``True``), whether or 

not to restart polymake in case something goes wrong 

- further optional arguments (e.g., timeout) that will be passed to 

:meth:`pexpect.pty_spawn.spawn.expect`. Note that they are ignored 

if the line is too long and thus is evaluated via a file. So, 

if a timeout is defined, it should be accompanied by ``allow_use_file=False``. 

 

Different reaction types of polymake, including warnings, comments, 

errors, request for user interaction, and yielding a continuation prompt, 

are taken into account. 

 

Usually, this method is indirectly called via :meth:`~sage.interfaces.expect.Expect.eval`. 

 

EXAMPLES:: 

 

sage: p = polymake.cube(3) # optional - polymake # indirect doctest 

 

Here we see that remarks printed by polymake are displayed if 

the verbosity is positive:: 

 

sage: set_verbose(1) 

sage: p.N_LATTICE_POINTS # optional - polymake 

used package latte 

LattE (Lattice point Enumeration) is a computer software dedicated to the 

problems of counting lattice points and integration inside convex polytopes. 

Copyright by Matthias Koeppe, Jesus A. De Loera and others. 

http://www.math.ucdavis.edu/~latte/ 

27 

sage: set_verbose(0) 

 

If polymake raises an error, the polymake *interface* raises 

a :class:`PolymakeError`:: 

 

sage: polymake.eval('FOOBAR(3);') # optional - polymake 

Traceback (most recent call last): 

... 

PolymakeError: Undefined subroutine &Polymake::User::FOOBAR called... 

 

If a command is incomplete, then polymake returns a continuation 

prompt. In that case, we raise an error:: 

 

sage: polymake.eval('print 3') # optional - polymake 

Traceback (most recent call last): 

... 

SyntaxError: Incomplete polymake command 'print 3' 

sage: polymake.eval('print 3;') # optional - polymake 

'3' 

 

However, if the command contains line breaks but eventually is complete, 

no error is raised:: 

 

sage: print(polymake.eval('$tmp="abc";\nprint $tmp;')) # optional - polymake 

abc 

 

When requesting help, polymake sometimes expect the user to choose 

from a list. In that situation, we abort with a warning, and show 

the list from which the user can choose; we could demonstrate this using 

the :meth:`help` method, but here we use an explicit code evaluation:: 

 

sage: print(polymake.eval('help "TRIANGULATION";')) # optional - polymake # random 

doctest:warning 

... 

UserWarning: Polymake expects user interaction. We abort and return 

the options that Polymake provides. 

There are 5 help topics matching 'TRIANGULATION': 

1: objects/Cone/properties/Triangulation and volume/TRIANGULATION 

2: objects/Polytope/properties/Triangulation and volume/TRIANGULATION 

3: objects/Visualization/Visual::PointConfiguration/methods/TRIANGULATION 

4: objects/Visualization/Visual::Polytope/methods/TRIANGULATION 

5: objects/PointConfiguration/properties/Triangulation and volume/TRIANGULATION 

 

By default, we just wait until polymake returns a result. However, 

it is possible to explicitly set a timeout. The following usually does 

work in an interactive session and often in doc tests, too. However, 

sometimes it hangs, and therefore we remove it from the tests, for now:: 

 

sage: c = polymake.cube(15) # optional - polymake 

sage: polymake.eval('print {}->F_VECTOR;'.format(c.name()), timeout=1) # optional - polymake # not tested 

Traceback (most recent call last): 

... 

RuntimeError: Polymake fails to respond timely 

 

We verify that after the timeout, polymake is still able to give answers:: 

 

sage: c # optional - polymake 

cube of dimension 15 

sage: c.N_VERTICES # optional - polymake 

32768 

 

Note, however, that the recovery after a timeout is not perfect. 

It may happen that in some situation the interface collapses and 

thus polymake would automatically be restarted, thereby losing all 

data that have been computed before. 

 

""" 

line = line.strip() 

if allow_use_file and wait_for_prompt and self._eval_using_file_cutoff and len(line) > self._eval_using_file_cutoff: 

return self._eval_line_using_file(line) 

try: 

if not self.is_running(): 

self._start() 

E = self._expect 

try: 

if len(line) >= 4096: 

raise RuntimeError("Sending more than 4096 characters with %s on a line may cause a hang and you're sending %s characters"%(self, len(line))) 

E.sendline(line) 

if not wait_for_prompt: 

return '' 

 

except OSError as msg: 

if restart_if_needed: 

# The subprocess most likely crashed. 

# If it's really still alive, we fall through 

# and raise RuntimeError. 

if sys.platform.startswith('sunos'): 

# On (Open)Solaris, we might need to wait a 

# while because the process might not die 

# immediately. See Trac #14371. 

for t in [0.5, 1.0, 2.0]: 

if E.isalive(): 

time.sleep(t) 

else: 

break 

if not E.isalive(): 

try: 

self._synchronize() 

except (TypeError, RuntimeError): 

pass 

return self._eval_line(line,allow_use_file=allow_use_file, wait_for_prompt=wait_for_prompt, restart_if_needed=False, **kwds) 

raise_(RuntimeError, "%s\nError evaluating %s in %s"%(msg, line, self), sys.exc_info()[2]) 

 

p_warnings = [] 

p_errors = [] 

have_warning = False 

have_error = False 

have_log = False 

if len(line)>0: 

first = True 

while True: 

try: 

if isinstance(wait_for_prompt, six.string_types): 

pat = E.expect(wait_for_prompt, **kwds) 

else: 

pat = E.expect_list(self._prompt, **kwds) 

except pexpect.EOF as msg: 

try: 

if self.is_local(): 

tmp_to_use = self._local_tmpfile() 

else: 

tmp_to_use = self._remote_tmpfile() 

if self._read_in_file_command(tmp_to_use) in line: 

raise pexpect.EOF(msg) 

except NotImplementedError: 

pass 

if self._quit_string() in line: 

# we expect to get an EOF if we're quitting. 

return '' 

elif restart_if_needed: # the subprocess might have crashed 

try: 

self._synchronize() 

return self._eval_line(line,allow_use_file=allow_use_file, wait_for_prompt=wait_for_prompt, restart_if_needed=False, **kwds) 

except (TypeError, RuntimeError): 

pass 

raise RuntimeError("%s\n%s crashed executing %s"%(msg,self, line)) 

if self._terminal_echo: 

out = E.before 

else: 

out = E.before.rstrip('\n\r') 

if self._terminal_echo and first: 

i = out.find("\n") 

j = out.rfind("\r") 

out = out[i+1:j].replace('\r\n','\n') 

else: 

out = out.strip().replace('\r\n','\n') 

first = False 

if have_error: 

p_errors.append(out) 

have_error = False 

out = "" 

elif have_warning: 

p_warnings.append(out) 

have_warning = False 

out = "" 

elif have_log: 

if get_verbose() > 0: 

print(out) 

have_log = False 

out = "" 

# 0: normal prompt 

# 1: continuation prompt 

# 2: user input expected when requestion "help" 

# 3: what we are looking for when interrupting a computation 

# 4: error 

# 5: warning 

# 6: anything but an error or warning, thus, an information 

# 7: unexpected end of the stream 

# 8: (expected) timeout 

if pat == 0: 

have_log = False 

have_error = False 

have_warning = False 

if E.buffer: 

if not E.buffer.strip(): 

E.send(chr(3)) 

sleep(0.1) 

pat = E.expect_list(self._prompt) 

if E.buffer or pat: 

raise RuntimeError("Couldn't return to prompt after command '{}'".format(line)) 

break 

elif pat == 1: # unexpected continuation prompt 

# Return to normal prompt 

i = pat 

E.send(chr(3)) 

sleep(0.1) 

i = E.expect_list(self._prompt) 

assert i==0, "Command '{}': Couldn't return to normal prompt after polymake {}. Instead, polymake {}".format(line,_available_polymake_answers[pat],_available_polymake_answers[i]) 

raise SyntaxError("Incomplete polymake command '{}'".format(line)) 

elif pat == 2: # request for user interaction 

# Return to normal prompt 

warnings.warn("{} expects user interaction. We abort and return the options that {} provides.".format(self,self)) 

i = pat 

while i: 

self._expect.send(chr(3)) 

sleep(0.1) 

i = self._expect.expect(self._prompt, timeout=0.1) 

# User interaction is expected to happen when requesting help 

if line.startswith('help'): 

out = os.linesep.join(out.split(os.linesep)[:-1]) 

break 

else: 

RuntimeError("Polymake unexpectedly {}".format(_available_polymake_answers[pat])) 

elif pat == 3: # killed by signal 

i = pat 

while pat != 0: 

E.send(chr(3)) 

sleep(0.1) 

i = E.expect_list(self._prompt) 

RuntimeError("Polymake unexpectedly {}".format(_available_polymake_answers[pat])) 

elif pat == 4: # polymake error 

have_error = True 

elif pat == 5: # polymake warning 

have_warning = True 

elif pat == 6: # apparently polymake prints a comment 

have_log = True 

elif pat == 7: # we have reached the end of the buffer 

warnings.warn("Polymake unexpectedly {}".format(_available_polymake_answers[pat]), RuntimeWarning) 

E.buffer = E.before + E.after + E.buffer 

break 

else: # timeout or some other problem 

# Polymake would still continue with the computation. Thus, we send an interrupt 

E.send(chr(3)) 

sleep(0.1) 

while E.expect_list(self._prompt, timeout=0.1): 

# ... and since a single Ctrl-c just interrupts *one* of polymake's 

# rule chains, we repeat until polymake is running out of rules. 

E.send(chr(3)) 

sleep(0.1) 

raise RuntimeError("Polymake {}".format(_available_polymake_answers[pat])) 

else: 

out = '' 

except KeyboardInterrupt: 

self._keyboard_interrupt() 

raise KeyboardInterrupt("Ctrl-c pressed while running %s"%self) 

for w in p_warnings: 

warnings.warn(w, RuntimeWarning) 

for e in p_errors: 

raise PolymakeError(e) 

return out 

 

def _tab_completion(self): 

""" 

Returns a list of polymake function names. 

 

NOTE: 

 

- The list of functions depends on the current application. The 

result is cached, of course separately for each application. 

- It is generally not the case that all the returned function names 

can actually successfully be called. 

 

TESTS:: 

 

sage: polymake.application('fan') # optional - polymake 

sage: 'normal_fan' in dir(polymake) # optional - polymake # indirect doctest 

True 

sage: polymake.quit() # optional - polymake 

sage: polymake._start() # optional - polymake 

 

Since 'normal_fan' is not defined in the polymake application 'polytope', 

we now get 

:: 

 

sage: 'normal_fan' in dir(polymake) # optional - polymake 

False 

 

""" 

if not self.is_running(): 

self._start() 

try: 

return self.__tab_completion[self._application] 

except KeyError: 

pass 

s = self.eval("apropos '';").split(self._expect.linesep) 

out = [] 

for name in s: 

if name.startswith("/function"): 

out.append(name.split("/")[-1]) 

self.__tab_completion[self._application] = sorted(out) 

return self.__tab_completion[self._application] 

 

# Polymake specific methods 

 

def application(self, app): 

""" 

Change to a given polymake application. 

 

INPUT: 

 

- ``app``, a string, one of "common", "fulton", "group", "matroid", "topaz", 

"fan", "graph", "ideal", "polytope", "tropical" 

 

EXAMPLES: 

 

We expose a computation that uses both the 'polytope' and the 'fan' 

application of polymake. Let us start by defining a polytope `q` in 

terms of inequalities. Polymake knows to compute the f- and h-vector 

and finds that the polytope is very ample:: 

 

sage: q = polymake.new_object("Polytope", INEQUALITIES=[[5,-4,0,1],[-3,0,-4,1],[-2,1,0,0],[-4,4,4,-1],[0,0,1,0],[8,0,0,-1],[1,0,-1,0],[3,-1,0,0]]) # optional - polymake 

sage: q.H_VECTOR # optional - polymake 

1 5 5 1 

sage: q.F_VECTOR # optional - polymake 

8 14 8 

sage: q.VERY_AMPLE # optional - polymake 

1 

 

In the application 'fan', polymake can now compute the normal fan 

of `q` and its (primitive) rays:: 

 

sage: polymake.application('fan') # optional - polymake 

sage: g = q.normal_fan() # optional - polymake 

sage: g.RAYS # optional - polymake 

-1 0 1/4 

0 -1 1/4 

1 0 0 

1 1 -1/4 

0 1 0 

0 0 -1 

0 -1 0 

-1 0 0 

sage: g.RAYS.primitive() # optional - polymake 

-4 0 1 

0 -4 1 

1 0 0 

4 4 -1 

0 1 0 

0 0 -1 

0 -1 0 

-1 0 0 

 

Note that the list of functions available by tab completion depends 

on the application. 

 

TESTS: 

 

Since 'tubing_of_graph' is not defined in the polymake application 'polytope' 

but only in 'tropical', the following shows the effect of changing 

the application. :: 

 

sage: polymake.application('polytope') # optional - polymake 

sage: 'tubing_of_graph' in dir(polymake) # optional - polymake 

False 

sage: polymake.application('tropical') # optional - polymake 

sage: 'tubing_of_graph' in dir(polymake) # optional - polymake 

True 

sage: polymake.application('polytope') # optional - polymake 

sage: 'tubing_of_graph' in dir(polymake) # optional - polymake 

False 

 

For completeness, we show what happens when asking for an application 

that doesn't exist:: 

 

sage: polymake.application('killerapp') # optional - polymake 

Traceback (most recent call last): 

... 

ValueError: Unknown polymake application 'killerapp' 

 

Of course, a different error results when we send an explicit 

command in polymake to change to an unknown application:: 

 

sage: polymake.eval('application "killerapp";') # optional - polymake 

Traceback (most recent call last): 

... 

PolymakeError: Unknown application killerapp 

 

""" 

if not self.is_running(): 

self._start() 

if app not in ["common", "fulton", "group", "matroid", "topaz", "fan", "graph", "ideal", "polytope", "tropical"]: 

raise ValueError("Unknown polymake application '{}'".format(app)) 

self._application = app 

patterns = ["{} > ".format(app), # 0: normal prompt 

"{} \([0-9]+\)> ".format(app), # 1: continuation prompt 

"Please choose ".format(app), # 2: user input expected when requesting "help" 

"killed by signal", # 3: what we are looking for when interrupting a computation 

"polymake: +ERROR: +", # 4: error 

"polymake: +WARNING: +", # 5: warning 

"polymake: +", # 6: anything but an error or warning, thus, an information 

pexpect.EOF, # 7: unexpected end of the stream 

pexpect.TIMEOUT] # 8: timeout 

self._change_prompt(self._expect.compile_pattern_list(patterns)) 

self._sendstr('application "{}";{}'.format(app, self._expect.linesep)) 

pat = self._expect.expect_list(self._prompt) 

if pat: 

raise RuntimeError("When changing the application, polymake unexpectedly {}".format(_available_polymake_answers[pat])) 

 

def new_object(self, name, *args, **kwds): 

""" 

Return a new instance of a given polymake type, with given positional or named arguments. 

 

INPUT: 

 

- ``name`` of a polymake class (potentially templated), as string. 

- further positional or named arguments, to be passed to the constructor. 

 

EXAMPLES:: 

 

sage: q = polymake.new_object("Polytope<Rational>", INEQUALITIES=[[4,-4,0,1],[-4,0,-4,1],[-2,1,0,0],[-4,4,4,-1],[0,0,1,0],[8,0,0,-1]]) # optional - polymake 

sage: q.N_VERTICES # optional - polymake 

4 

sage: q.BOUNDED # optional - polymake 

1 

sage: q.VERTICES # optional - polymake 

1 2 0 4 

1 3 0 8 

1 2 1 8 

1 3 1 8 

sage: q.full_typename() # optional - polymake 

'Polytope<Rational>' 

 

""" 

try: 

f = self.__new[name] 

except AttributeError: 

self.__new = {} 

f = self.__new[name] = self._function_class()(self, "new %s"%name) 

except KeyError: 

f = self.__new[name] = self._function_class()(self, "new %s"%name) 

return f(*args, **kwds) 

 

polymake = Polymake() 

 

def reduce_load_Polymake(): 

""" 

Returns the polymake interface object defined in :mod:`sage.interfaces.polymake`. 

 

EXAMPLES:: 

 

sage: from sage.interfaces.polymake import reduce_load_Polymake 

sage: reduce_load_Polymake() 

Polymake 

""" 

return polymake 

 

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

## Elements 

 

from warnings import warn 

 

class PolymakeElement(ExtraTabCompletion, ExpectElement): 

""" 

Elements in the polymake interface. 

 

EXAMPLES: 

 

We support all "big" polymake types, Perl arrays of length 

different from one, and Perl scalars:: 

 

sage: p = polymake.rand_sphere(4, 20, seed=5) # optional - polymake 

sage: p.typename() # optional - polymake 

'Polytope' 

sage: p # optional - polymake 

Random spherical polytope of dimension 4; seed=5... 

 

Now, one can work with that element in Python syntax, for example:: 

 

sage: p.VERTICES[2][2] # optional - polymake 

-3319173990813887/4503599627370496 

 

""" 

def _repr_(self): 

""" 

String representation of polymake elements. 

 

EXAMPLES: 

 

In the case of a "big" object, if polymake provides a description 

of the object that is not longer than single line, it is used for 

printing:: 

 

sage: p = polymake.rand_sphere(3, 12, seed=15) # optional - polymake 

sage: p # optional - polymake 

Random spherical polytope of dimension 3; seed=15... 

sage: c = polymake.cube(4) # optional - polymake 

sage: c # optional - polymake 

cube of dimension 4 

 

We use the print representation of scalars to display scalars:: 

 

sage: p.N_VERTICES # optional - polymake 

12 

 

The items of a Perl arrays are shown separated by commas:: 

 

sage: p.get_member('list_properties') # optional - polymake # random 

POINTS, CONE_AMBIENT_DIM, BOUNDED, FEASIBLE, N_POINTS, POINTED, 

CONE_DIM, FULL_DIM, LINEALITY_DIM, LINEALITY_SPACE, 

COMBINATORIAL_DIM, AFFINE_HULL, VERTICES, N_VERTICES 

 

We chose to print rule chains explicitly, so that the user doesn't 

need to know how to list the rules using polymake commands:: 

 

sage: r = p.get_schedule('"H_VECTOR"') # optional - polymake 

sage: r # optional - polymake # random 

precondition : N_RAYS | N_INPUT_RAYS ( ppl.convex_hull.primal: FACETS, LINEAR_SPAN : RAYS | INPUT_RAYS ) 

sensitivity check for FacetPerm 

ppl.convex_hull.primal: FACETS, LINEAR_SPAN : RAYS | INPUT_RAYS 

RAYS_IN_FACETS : RAYS, FACETS 

SIMPLICIAL : COMBINATORIAL_DIM, RAYS_IN_FACETS 

N_FACETS : FACETS 

precondition : COMBINATORIAL_DIM ( F_VECTOR : N_FACETS, N_RAYS, COMBINATORIAL_DIM ) 

F_VECTOR : N_FACETS, N_RAYS, COMBINATORIAL_DIM 

precondition : SIMPLICIAL ( H_VECTOR : F_VECTOR ) 

H_VECTOR : F_VECTOR 

sage: r.typeof() # optional - polymake 

('Polymake::Core::Scheduler::RuleChain', 'ARRAY') 

 

Similarly, polymake matrices and vectors are explicitly listed:: 

 

sage: c.VERTICES.typename() # optional - polymake 

'Matrix' 

sage: c.VERTICES[0].typename() # optional - polymake 

'Vector' 

sage: c.VERTICES # optional - polymake # random 

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 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 1 

1 1 -1 1 1 

1 -1 1 1 1 

1 1 1 1 1 

sage: c.VERTICES[0] # optional - polymake 

1 -1 -1 -1 -1 

 

For other types, we simply use the print representation offered 

by polymake:: 

 

sage: p.TWO_FACE_SIZES.typename() # optional - polymake 

'Map' 

sage: p.TWO_FACE_SIZES # optional - polymake 

{(3 20)} 

 

""" 

T1, T2 = self.typeof() 

P = self._check_valid() 

name = self._name 

if T1: 

Temp = self.typename() 

if Temp: 

T1 = Temp 

if T1 in ['Matrix', 'Vector']: 

out = P.get(name).strip() 

elif 'RuleChain' in T1: 

out = os.linesep.join(P.get('join("##",{}->list)'.format(name)).split('##')) 

else: 

try: 

out = P.get('{}->description'.format(name)).strip() 

except PolymakeError: 

out = '' 

if os.linesep in out: 

out = '' 

if not out: 

if "Polytope" == T1: 

out = "{}[{}]".format(P.get("{}->type->full_name".format(name)) or "PolymakeElement", _name_pattern.search(name).group()) 

elif T1=='' and T2=='ARRAY': 

out = P.eval('print join(", ", @{});'.format(name)).strip() 

elif T1=='' and T2=='HASH': 

out = P.get('%{}'.format(name)).strip() 

elif self._name[0] == '@': 

out = P.eval('print join(", ", {});'.format(name)).strip() 

else: 

out = P.get(name).strip() 

return out 

 

def __cmp__(self, other): 

""" 

Comparison of polymake elements. 

 

EXAMPLES: 

 

The default for comparing equality for polytopes is *identity*:: 

 

sage: p1 = polymake.rand_sphere(3, 12, seed=15) # optional - polymake 

sage: p2 = polymake.rand_sphere(3, 12, seed=15) # optional - polymake 

sage: p1 == p2 # optional - polymake 

False 

 

However, other data types are compared by equality, not identity:: 

 

sage: p1.VERTICES == p2.VERTICES # optional - polymake 

True 

 

A computation applied to a polytope can change the available 

properties, and thus we have 

:: 

 

sage: p1.get_member('list_properties') == p2.get_member('list_properties') # optional - polymake 

True 

sage: p1.F_VECTOR # optional - polymake 

12 30 20 

sage: p1.get_member('list_properties') == p2.get_member('list_properties') # optional - polymake 

False 

 

""" 

P = self._check_valid() 

if P.eval("print %s %s %s;"%(self.name(), P._equality_symbol(), other.name())).strip() == P._true_symbol(): 

return 0 

if P.eval("print %s %s %s;"%(self.name(), P._lessthan_symbol(), other.name())).strip() == P._true_symbol(): 

return -1 

if P.eval("print %s %s %s;"%(self.name(), P._greaterthan_symbol(), other.name())).strip() == P._true_symbol(): 

return 1 

return -2 # that's supposed to be an error value. 

 

def bool(self): 

""" 

Return whether this polymake element is equal to ``True``. 

 

EXAMPLES:: 

 

sage: from sage.interfaces.polymake import polymake 

sage: polymake(0).bool() # optional polymake 

False 

sage: polymake(1).bool() # optional polymake 

True 

 

""" 

P = self._check_valid() 

t = P._true_symbol() 

cmd = '%s %s %s;'%(self._name, P._equality_symbol(), t) 

return P.get(cmd) == t 

 

def known_properties(self): 

""" 

List the names of properties that have been computed so far on this element. 

 

NOTE: 

 

This is in many cases equivalent to use polymake's ``list_properties``, 

which returns a blank separated string representation of the list of properties. 

However, on some elements, ``list_properties`` would simply result in 

an error. 

 

EXAMPLES:: 

 

sage: c = polymake.cube(4) # optional - polymake 

sage: c.known_properties() # optional - polymake 

['AFFINE_HULL', 

'BOUNDED', 

'CONE_AMBIENT_DIM', 

'CONE_DIM', 

... 

'VERTICES_IN_FACETS'] 

sage: c.list_properties() # optional - polymake 

CONE_AMBIENT_DIM, CONE_DIM, FACETS, AFFINE_HULL, VERTICES_IN_FACETS, 

BOUNDED... 

 

A computation can change the list of known properties:: 

 

sage: c.F_VECTOR # optional - polymake 

16 32 24 8 

sage: c.known_properties() # optional - polymake 

['AFFINE_HULL', 

'BOUNDED', 

'COMBINATORIAL_DIM', 

'CONE_AMBIENT_DIM', 

'CONE_DIM', 

'DUAL_H_VECTOR', 

'FACETS', 

'FAR_FACE', 

'FEASIBLE', 

'FULL_DIM', 

'F_VECTOR', 

'GRAPH', 

'LINEALITY_DIM', 

'LINEALITY_SPACE', 

'N_FACETS', 

'N_VERTICES', 

'POINTED', 

'SIMPLE', 

'SIMPLICIAL', 

'VERTICES', 

'VERTICES_IN_FACETS'] 

 

""" 

P = self._check_valid() 

try: 

return sorted(P.get('join(", ", {}->list_properties)'.format(self._name)).split(', ')) 

except PolymakeError: 

return [] 

 

@cached_method 

def _member_list(self): 

""" 

The list of properties that polymake knows to compute for this element. 

 

The resulting list is used for tab completion. 

 

TESTS:: 

 

sage: c = polymake.cube(4) # optional - polymake 

sage: c._member_list() # optional - polymake 

['AFFINE_HULL', 

'ALTSHULER_DET', 

'BALANCE', 

'BALANCED', 

... 

'WEAKLY_CENTERED', 

'ZONOTOPE_INPUT_POINTS'] 

 

""" 

### return the members of a "big" object. 

P = self._check_valid() 

try: 

P.eval('$SAGETMP = typeof {+'+self._name+'};') 

except (TypeError, PolymakeError): # this happens for a perl type that isn't a Polymake type 

return [] 

cmd = 'print join(", ", sorted_uniq(sort { $a cmp $b } map { keys %{$_->properties} }$SAGETMP, @{$SAGETMP->super}));' 

try: 

out = P.eval(cmd).split(', ') 

except PolymakeError as msg: 

return [] 

return sorted(out) 

 

def typename(self): 

""" 

The name of the underlying base type of this element in polymake. 

 

EXAMPLES:: 

 

sage: c = polymake.cube(4) # optional - polymake 

sage: c.typename() # optional - polymake 

'Polytope' 

sage: c.VERTICES.typename() # optional - polymake 

'Matrix' 

 

""" 

P = self._check_valid() 

try: 

return P.eval('print {}->type->name;'.format(self._name)) 

except PolymakeError as msg: 

return '' 

 

def full_typename(self): 

""" 

The name of the specialised type of this element. 

 

EXAMPLES:: 

 

sage: c = polymake.cube(4) # optional - polymake 

sage: c.full_typename() # optional - polymake 

'Polytope<Rational>' 

sage: c.VERTICES.full_typename() # optional - polymake 

'Matrix<Rational, NonSymmetric>' 

 

""" 

P = self._check_valid() 

try: 

return P.eval('print {}->type->full_name;'.format(self._name)) 

except PolymakeError as msg: 

return '' 

 

def qualified_typename(self): 

""" 

The qualified name of the type of this element. 

 

EXAMPLES:: 

 

sage: c = polymake.cube(4) # optional - polymake 

sage: c.qualified_typename() # optional - polymake 

'polytope::Polytope<Rational>' 

sage: c.VERTICES.qualified_typename() # optional - polymake 

'common::Matrix<Rational, NonSymmetric>' 

 

""" 

P = self._check_valid() 

try: 

return P.eval('print {}->type->qualified_name;'.format(self._name)) 

except PolymakeError as msg: 

return '' 

 

def _tab_completion(self): 

""" 

Return a list of available function and property names. 

 

NOTE: 

 

This currently returns the names of functions defined in the current 

application, regardless whether they can be applied to this element 

or not, together with the list of properties of this element that 

polymake knows how to compute. It does not contain the list of available 

member functions of this element. This may change in future versions 

of polymake. 

 

EXAMPLES:: 

 

sage: c = polymake.cube(4) # optional - polymake 

sage: c._tab_completion() # optional - polymake 

['AFFINE_HULL', 

'ALTSHULER_DET', 

'BALANCE', 

'BALANCED', 

'BOUNDARY_LATTICE_POINTS', 

... 

'zero_vector', 

'zonotope', 

'zonotope_tiling_lattice', 

'zonotope_vertices_fukuda'] 

 

""" 

return sorted(self._member_list()+self.parent()._tab_completion()) 

 

def __getattr__(self, attrname): 

""" 

Return a property of this element, or a polymake function with this 

element as first argument, or a member function of this element. 

 

NOTE: 

 

If the attribute name is known as the name of a property, it is 

interpreted as such. Otherwise, if it is known as a function in 

the current application, the function is returned with this 

element inserted as first argument, and potential further arguments, 

when called. Otherwise, it is assumed that it is a member function 

of this element, and treated as such. Note that member functions 

are currently invisible in tab completion, thus, the user has 

to know the name of the member function. 

 

EXAMPLES: 

 

A property:: 

 

sage: c = polymake.cube(3) # optional - polymake 

sage: c.H_VECTOR # optional - polymake 

1 5 5 1 

sage: c.N_VERTICES # optional - polymake 

8 

sage: d = polymake.cross(3) # optional - polymake 

sage: d.N_VERTICES # optional - polymake 

6 

 

A function:: 

 

sage: c.minkowski_sum_fukuda # optional - polymake 

minkowski_sum_fukuda (bound to Polymake::polytope::Polytope__Rational object) 

sage: s = c.minkowski_sum_fukuda(d) # optional - polymake 

sage: s.N_VERTICES # optional - polymake 

24 

sage: s # optional - polymake 

Polytope<Rational>[SAGE...] 

 

A member function:: 

 

sage: c = polymake.cube(2) # optional - polymake 

sage: V = polymake.new_object('Vector', [1,0,0]) # optional - polymake 

sage: V # optional - polymake 

1 0 0 

sage: c.contains # optional - polymake 

Member function 'contains' of Polymake::polytope::Polytope__Rational object 

sage: c.contains(V) # optional - polymake 

1 

 

""" 

P = self._check_valid() 

if attrname[:1] == "_": 

raise AttributeError 

if attrname not in P._tab_completion(): 

if attrname in self._member_list(): 

try: 

return P('{}->{}'.format(self._name, attrname)) 

except (TypeError, PolymakeError): 

raise AttributeError 

else: 

try: 

return P._function_element_class()(self, '{}->{}'.format(self._name, attrname), memberfunction=True) 

except (TypeError, PolymakeError): 

raise AttributeError 

return P._function_element_class()(self, attrname, memberfunction=False) 

 

def get_member_function(self, attrname): 

""" 

Request a member function of this element. 

 

NOTE: 

 

It is not checked whether a member function with the given name 

exists. 

 

EXAMPLES:: 

 

sage: c = polymake.cube(2) # optional - polymake 

sage: c.contains # optional - polymake 

Member function 'contains' of Polymake::polytope::Polytope__Rational object 

sage: V = polymake.new_object('Vector', [1,0,0]) # optional - polymake 

sage: V # optional - polymake 

1 0 0 

sage: c.contains(V) # optional - polymake 

1 

 

Whether a member function of the given name actually exists for that 

object will only be clear when calling it:: 

 

sage: c.get_member_function('foo') # optional - polymake 

Member function 'foo' of Polymake::polytope::Polytope__Rational object 

sage: c.get_member_function('foo')() # optional - polymake 

Traceback (most recent call last): 

... 

TypeError: Can't locate object method "foo" via package "Polymake::polytope::Polytope__Rational" at input line 1. 

 

""" 

P = self._check_valid() 

return P._function_element_class()(self, '{}->{}'.format(self._name, attrname), memberfunction=True) 

 

def get_member(self, attrname): 

""" 

Get a member/property of this element. 

 

NOTE: 

 

Normally, it should be possible to just access the property 

in the usual Python syntax for attribute access. However, if 

that fails, one can request the member explicitly. 

 

EXAMPLES:: 

 

sage: p = polymake.rand_sphere(4, 20, seed=5) # optional - polymake 

 

Normally, a property would be accessed as follows:: 

 

sage: p.F_VECTOR # optional - polymake 

20 101 162 81 

 

However, explicit access is possible as well:: 

 

sage: p.get_member('F_VECTOR') # optional - polymake 

20 101 162 81 

 

In some cases, the explicit access works better:: 

 

sage: p.type # optional - polymake 

Member function 'type' of Polymake::polytope::Polytope__Rational object 

sage: p.get_member('type') # optional - polymake 

Polytope<Rational>[SAGE...] 

sage: p.get_member('type').get_member('name') # optional - polymake 

Polytope 

 

Note that in the last example calling the erroneously constructed 

member function ``type`` still works:: 

 

sage: p.type() # optional - polymake 

Polytope<Rational>[SAGE...] 

 

""" 

P = self._check_valid() 

return P('%s->%s'%(self.name(), attrname)) 

 

def __getitem__(self, key): 

""" 

Indexing and slicing. 

 

Slicing returns a Python list. 

 

EXAMPLES:: 

 

sage: p = polymake.rand_sphere(3, 12, seed=15) # optional - polymake 

sage: p.VERTICES[3] # optional - polymake 

1 -6157731020575175/18014398509481984 4184896164481703/4503599627370496 -2527292586301447/18014398509481984 

sage: p.list_properties()[2] # optional - polymake 

BOUNDED 

 

Slicing:: 

 

sage: p.F_VECTOR[:] # optional - polymake 

[12, 30, 20] 

sage: p.F_VECTOR[0:1] # optional - polymake 

[12] 

sage: p.F_VECTOR[0:3:2] # optional - polymake 

[12, 20] 

""" 

P = self._check_valid() 

if isinstance(key, slice): 

indices = key.indices(len(self)) 

return [ self[i] for i in range(*indices) ] 

_, T = self.typeof() 

if self._name.startswith('@'): 

return P('${}[{}]'.format(self._name[1:], key)) 

if T=='ARRAY': 

return P('{}[{}]'.format(self._name, key)) 

if T=='HASH': 

try: 

if key.parent() is self.parent(): 

key = key._name 

else: 

key = str(key) 

except AttributeError: 

key = str(key) 

return P(name+"{"+key+"}") 

raise NotImplementedError("Cannot get items from Perl type {}".format(T)) 

 

def __iter__(self): 

""" 

Return an iterator for ``self``. 

 

OUTPUT: iterator 

 

EXAMPLES:: 

 

sage: p = polymake.rand_sphere(3, 12, seed=15) # optional - polymake 

sage: [ x for x in p.VERTICES[3] ] # optional - polymake 

[1, -6157731020575175/18014398509481984, 4184896164481703/4503599627370496, -2527292586301447/18014398509481984] 

""" 

for i in range(len(self)): 

yield self[i] 

 

def __len__(self): 

""" 

EXAMPLES:: 

 

sage: p = polymake.rand_sphere(3, 12, seed=15) # optional - polymake 

sage: len(p.FACETS) # optional - polymake 

20 

sage: len(p.list_properties()) # optional - polymake 

13 

 

""" 

P = self._check_valid() 

T1,T2 = self.typeof() 

name = self._name 

if T2=='ARRAY': 

return int(P.eval('print scalar @{+%s};'%name)) 

if T2=='HASH': 

return int(P.eval('print scalar keys %{+%s};'%name)) 

if T1: 

raise TypeError("Don't know how to compute the length of {} object".format(T1)) 

return int(P.eval('print scalar {};'.format(name))) 

 

@cached_method 

def typeof(self): 

""" 

Returns the type of a polymake "big" object, and its underlying Perl type. 

 

NOTE: 

 

This is mainly for internal use. 

 

EXAMPLES:: 

 

sage: p = polymake.rand_sphere(3, 13, seed=12) # optional - polymake 

sage: p.typeof() # optional - polymake 

('Polymake::polytope::Polytope__Rational', 'ARRAY') 

sage: p.VERTICES.typeof() # optional - polymake 

('Polymake::common::Matrix_A_Rational_I_NonSymmetric_Z', 'ARRAY') 

sage: p.get_schedule("F_VECTOR").typeof() # optional - polymake 

('Polymake::Core::Scheduler::RuleChain', 'ARRAY') 

 

On "small" objects, it just returns empty strings:: 

 

sage: p.N_VERTICES.typeof() # optional - polymake 

('', '') 

sage: p.list_properties().typeof() # optional - polymake 

('', '') 

""" 

P = self._check_valid() 

name = self._name 

return P.eval('print ref({});'.format(name)), P.eval('print reftype({});'.format(name)) 

 

def _sage_(self): 

""" 

Convert self to a Sage object. 

 

EXAMPLES:: 

 

sage: a = polymake(1/2); a # optional - polymake 

1/2 

sage: a.sage() # optional - polymake 

1/2 

sage: _.parent() # optional - polymake 

Rational Field 

 

Quadratic extensions:: 

 

sage: K.<sqrt5> = QuadraticField(5) 

sage: polymake(K(0)).sage() # optional - polymake 

0 

sage: _.parent() # optional - polymake 

Rational Field 

sage: polymake(sqrt5).sage() # optional - polymake 

a 

sage: polymake(-sqrt5).sage() # optional - polymake 

-a 

sage: polymake(1/3-1/2*sqrt5).sage() # optional - polymake 

-1/2*a + 1/3 

sage: polymake(-1+sqrt5).sage() # optional - polymake 

a - 1 

 

Vectors:: 

 

sage: PP = polymake.cube(3) # optional - polymake 

sage: PP.F_VECTOR.sage() # optional - polymake 

(8, 12, 6) 

sage: _.parent() # optional - polymake 

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

 

Matrices:: 

 

sage: polymake.unit_matrix(2).sage() # optional - polymake 

[1 0] 

[0 1] 

sage: _.parent() # optional - polymake 

Full MatrixSpace of 2 by 2 dense matrices over Integer Ring 

 

""" 

T1, T2 = self.typeof() 

P = self._check_valid() 

if T1: 

Temp = self.typename() 

if Temp: 

T1 = Temp 

if T1 == 'QuadraticExtension': 

# We can't seem to access a, b, r by method calls, so let's parse. 

from re import match 

m = match(r'(-?[0-9/]+)[+]?((-?[0-9/]+)r([0-9/]+))?', repr(self)) 

if m is None: 

raise NotImplementedError("Cannot parse QuadraticExtension element: {}".format(self)) 

a, b, r = m.group(1), m.group(3), m.group(4) 

from sage.rings.rational_field import QQ 

if r is None: 

# Prints like a rational, so we can't know the extension. Coerce to rational. 

return QQ(a) 

else: 

from sage.rings.number_field.number_field import QuadraticField 

K = QuadraticField(r) 

return QQ(a) + QQ(b) * K.gen() 

elif T1 == 'Vector' or T1 == 'SparseVector': 

from sage.modules.free_module_element import vector 

return vector([x.sage() for x in self]) 

elif T1 == 'Matrix' or T1 == 'SparseMatrix': 

from sage.matrix.constructor import matrix 

return matrix([x.sage() for x in self]) 

else: 

return super(PolymakeElement, self)._sage_() 

 

def _sage_doc_(self): 

""" 

EXAMPLES:: 

 

sage: c = polymake.cube(3) # optional - polymake 

sage: print(c._sage_doc_()) # optional - polymake # random 

objects/Polytope: 

Not necessarily bounded or unbounded polyhedron. 

Nonetheless, the name "Polytope" is used for two reasons: 

Firstly, combinatorially we always deal with polytopes; see the description of VERTICES_IN_FACETS for details. 

The second reason is historical. 

We use homogeneous coordinates, which is why Polytope is derived from Cone. 

Note that a pointed polyhedron is projectively equivalent to a polytope. 

Scalar is the numeric data type used for the coordinates. 

<BLANKLINE> 

objects/Polytope/specializations/Polytope<Rational>: 

A rational polyhedron realized in Q^d 

sage: print(c.FACETS._sage_doc_()) # optional - polymake # random 

property_types/Algebraic Types/SparseMatrix: 

A SparseMatrix is a two-dimensional associative array with row and column indices as keys; elements equal to the default value (ElementType(), which is 0 for most numerical types) are not stored, but implicitly encoded by the gaps in the key set. Each row and column is organized as an AVL-tree. 

<BLANKLINE> 

Use dense to convert this into its dense form. 

<BLANKLINE> 

You can create a new SparseMatrix by entering its entries row by row, as a list of SparseVectors e.g.: 

$A = new SparseMatrix<Int>(<< '.'); 

(5) (1 1) 

(5) (4 2) 

(5) 

(5) (0 3) (1 -1) 

. 

 

""" 

P = self._check_valid() 

# according to Julian Pfeifle, the only case in which the fully qualified 

# typename would not provide the doc. 

Tname = self.typename() 

Tqname = self.qualified_typename() 

Tfname = self.full_typename() 

if Tname == 'Polytope': 

try: 

doc = P.eval('help "Polytope";') 

except PolymakeError: 

doc = '' 

else: 

try: 

doc = P.eval('help "{}";'.format(Tname)) 

except PolymakeError: 

doc = '' 

try: 

doc2 = P.eval('help "{}";'.format(Tqname)) 

except PolymakeError: 

doc2 = '' 

if doc: 

if doc2: 

doc = doc+os.linesep+doc2 

else: 

doc = doc2 

try: 

doc3 = P.eval('help "{}";'.format(Tfname)) 

except PolymakeError: 

doc3 = '' 

if doc: 

if doc3: 

doc = doc+os.linesep+doc3 

else: 

doc = doc3 

if doc: 

return doc 

return "Undocumented polymake type '{}'".format(self.full_typename()) 

 

class PolymakeFunctionElement(FunctionElement): 

""" 

A callable (function or member function) bound to a polymake element. 

 

EXAMPLES:: 

 

sage: c = polymake.cube(2) # optional - polymake 

sage: V = polymake.new_object('Vector', [1,0,0]) # optional - polymake 

sage: V # optional - polymake 

1 0 0 

sage: c.contains # optional - polymake 

Member function 'contains' of Polymake::polytope::Polytope__Rational object 

sage: c.contains(V) # optional - polymake 

1 

 

""" 

def __init__(self, obj, name, memberfunction=False): 

""" 

INPUT: 

 

- Polymake object that this function is bound to 

- name (string): It actually says how to call this function in polymake. 

So, if it is a member function, it will look like `"$SAGE123[0]->func_name"`. 

- ``memberfunction`` (bool, default False): Whether this is a member function 

or a plain function applied with this element as first argument. 

 

EXAMPLES:: 

 

sage: p = polymake.rand_sphere(3, 13, seed=12) # optional - polymake 

sage: p.minkowski_sum_fukuda # optional - polymake 

minkowski_sum_fukuda (bound to Polymake::polytope::Polytope__Rational object) 

sage: p.get_schedule # optional - polymake 

Member function 'get_schedule' of Polymake::polytope::Polytope__Rational object 

 

""" 

self._obj = obj 

self._name = name 

self._is_memberfunc = memberfunction 

def _repr_(self): 

""" 

EXAMPLES:: 

 

sage: p = polymake.rand_sphere(3, 13, seed=12) # optional - polymake 

sage: p.minkowski_sum_fukuda # optional - polymake 

minkowski_sum_fukuda (bound to Polymake::polytope::Polytope__Rational object) 

sage: p.contains # optional - polymake 

Member function 'contains' of Polymake::polytope::Polytope__Rational object 

 

""" 

if self._is_memberfunc: 

return "Member function '{}' of {} object".format(self._name.split("->")[-1], self._obj.typeof()[0]) 

return "{} (bound to {} object)".format(self._name, self._obj.typeof()[0]) 

 

def __call__(self, *args, **kwds): 

""" 

EXAMPLES: 

 

We consider both member functions of an element and global functions 

bound to an element:: 

 

sage: p = polymake.rand_sphere(3, 13, seed=12) # optional - polymake 

sage: p.get_schedule('VERTICES') # optional - polymake # random 

sensitivity check for VertexPerm 

cdd.convex_hull.canon: POINTED, RAYS, LINEALITY_SPACE : INPUT_RAYS 

sage: p.minkowski_sum_fukuda(p).F_VECTOR # optional - polymake 

13 33 22 

 

""" 

if self._is_memberfunc: 

return self._obj._check_valid().function_call(self._name, list(args), kwds) 

return self._obj._check_valid().function_call(self._name, [self._obj] + list(args), kwds) 

 

def _sage_doc_(self): 

""" 

Return documentation of this function. 

 

NOTE: 

 

For unclear reasons, accessing documentation with `?` sometimes 

does not include the return value of this method. 

 

EXAMPLES:: 

 

sage: p = polymake.rand_sphere(3, 13, seed=12) # optional - polymake 

sage: print(p.get_schedule._sage_doc_()) # optional - polymake # random 

objects/Core::Object/methods/get_schedule: 

get_schedule(request; ... ) -> Core::RuleChain 

<BLANKLINE> 

Compose an optimal chain of production rules providing all requested properties. 

The returned RuleChain object can be applied to the original object as well as to any other object 

with the same initial set of properties. If no feasible rule chain exists, `undef' is returned. 

<BLANKLINE> 

To watch the rule scheduler at work, e.g. to see announcements about tried preconditions, 

you may temporarily increase the verbosity levels $Verbose::rules and $Verbose::scheduler. 

<BLANKLINE> 

Arguments: 

String request : name of a property with optional alternatives or a property path in dotted notation. 

Several requests may be listed. 

<BLANKLINE> 

Returns Core::RuleChain 

sage: print(p.minkowski_sum_fukuda._sage_doc_()) # optional - polymake # random 

functions/Producing a polytope from polytopes/minkowski_sum_fukuda: 

minkowski_sum_fukuda(summands) -> Polytope<Scalar> 

<BLANKLINE> 

Computes the (VERTICES of the) Minkowski sum of a list of polytopes using the algorithm by Fukuda described in 

Komei Fukuda, From the zonotope construction to the Minkowski addition of convex polytopes, J. Symbolic Comput., 38(4):1261-1272, 2004. 

<BLANKLINE> 

Arguments: 

Array<Polytope<Scalar>> summands 

<BLANKLINE> 

Returns Polytope<Scalar> 

<BLANKLINE> 

Example: 

> $p = minkowski_sum_fukuda([cube(2),simplex(2),cross(2)]); 

> print $p->VERTICES; 

1 -2 -1 

1 -1 -2 

1 3 -1 

1 3 1 

1 2 -2 

1 -2 2 

1 -1 3 

1 1 3 

 

""" 

P = self._obj._check_valid() 

return P.help(self._name.split("->")[-1], pager=False)