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

r""" 

Fast Expression Evaluation 

  

For many applications such as numerical integration, differential 

equation approximation, plotting a 3d surface, optimization problems, 

Monte-Carlo simulations, etc., one wishes to pass around and evaluate 

a single algebraic expression many, many times at various floating 

point values. Other applications may need to evaluate an expression 

many times in interval arithmetic, or in a finite field. Doing this 

via recursive calls over a python representation of the object (even 

if Maxima or other outside packages are not involved) is extremely 

inefficient. 

  

This module provides a function, :func:`fast_callable`, to 

transform such expressions into a form where they can be evaluated 

quickly:: 

  

sage: f = sin(x) + 3*x^2 

sage: ff = fast_callable(f, vars=[x]) 

sage: ff(3.5) 

36.3992167723104 

sage: ff(RIF(3.5)) 

36.39921677231038? 

  

By default, :func:`fast_callable` only removes some interpretive 

overhead from the evaluation, but all of the individual arithmetic 

operations are done using standard Sage arithmetic. This is still a 

huge win over sage.calculus, which evidently has a lot of overhead. 

Compare the cost of evaluating Wilkinson's polynomial (in unexpanded 

form) at x=30:: 

  

sage: wilk = prod((x-i) for i in [1 .. 20]); wilk 

(x - 1)*(x - 2)*(x - 3)*(x - 4)*(x - 5)*(x - 6)*(x - 7)*(x - 8)*(x - 9)*(x - 10)*(x - 11)*(x - 12)*(x - 13)*(x - 14)*(x - 15)*(x - 16)*(x - 17)*(x - 18)*(x - 19)*(x - 20) 

sage: timeit('wilk.subs(x=30)') # random, long time 

625 loops, best of 3: 1.43 ms per loop 

sage: fc_wilk = fast_callable(wilk, vars=[x]) 

sage: timeit('fc_wilk(30)') # random, long time 

625 loops, best of 3: 9.72 us per loop 

  

You can specify a particular domain for the evaluation using 

``domain=``:: 

  

sage: fc_wilk_zz = fast_callable(wilk, vars=[x], domain=ZZ) 

  

The meaning of domain=D is that each intermediate and final result 

is converted to type D. For instance, the previous example of 

``sin(x) + 3*x^2`` with domain=D would be equivalent to 

``D(D(sin(D(x))) + D(D(3)*D(D(x)^2)))``. (This example also 

demonstrates the one exception to the general rule: if an exponent is an 

integral constant, then it is not wrapped with D().) 

  

At first glance, this seems like a very bad idea if you want to 

compute quickly. And it is a bad idea, for types where we don't 

have a special interpreter. It's not too bad of a slowdown, though. 

To mitigate the costs, we check whether the value already has 

the correct parent before we call D. 

  

We don't yet have a special interpreter with domain ZZ, so we can see 

how that compares to the generic fc_wilk example above:: 

  

sage: timeit('fc_wilk_zz(30)') # random, long time 

625 loops, best of 3: 15.4 us per loop 

  

However, for other types, using domain=D will get a large speedup, 

because we have special-purpose interpreters for those types. One 

example is RDF. Since with domain=RDF we know that every single 

operation will be floating-point, we can just execute the 

floating-point operations directly and skip all the Python object 

creations that you would get from actually using RDF objects:: 

  

sage: fc_wilk_rdf = fast_callable(wilk, vars=[x], domain=RDF) 

sage: timeit('fc_wilk_rdf(30.0)') # random, long time 

625 loops, best of 3: 7 us per loop 

  

The domain does not need to be a Sage type; for instance, domain=float 

also works. (We actually use the same fast interpreter for domain=float 

and domain=RDF; the only difference is that when domain=RDF is used, 

the return value is an RDF element, and when domain=float is used, 

the return value is a Python float.) :: 

  

sage: fc_wilk_float = fast_callable(wilk, vars=[x], domain=float) 

sage: timeit('fc_wilk_float(30.0)') # random, long time 

625 loops, best of 3: 5.04 us per loop 

  

We also have support for ``RR``:: 

  

sage: fc_wilk_rr = fast_callable(wilk, vars=[x], domain=RR) 

sage: timeit('fc_wilk_rr(30.0)') # random, long time 

625 loops, best of 3: 13 us per loop 

  

For ``CC``:: 

  

sage: fc_wilk_cc = fast_callable(wilk, vars=[x], domain=CC) 

sage: timeit('fc_wilk_cc(30.0)') # random, long time 

625 loops, best of 3: 23 us per loop 

  

  

And support for ``CDF``:: 

  

sage: fc_wilk_cdf = fast_callable(wilk, vars=[x], domain=CDF) 

sage: timeit('fc_wilk_cdf(30.0)') # random, long time 

625 loops, best of 3: 10.2 us per loop 

  

Currently, :func:`fast_callable` can accept two kinds of objects: 

polynomials (univariate and multivariate) and symbolic expressions 

(elements of the Symbolic Ring). (This list is likely to grow 

significantly in the near future.) For polynomials, you can omit the 

'vars' argument; the variables will default to the ring generators (in 

the order used when creating the ring). :: 

  

sage: K.<x,y,z> = QQ[] 

sage: p = 10*y + 100*z + x 

sage: fp = fast_callable(p) 

sage: fp(1,2,3) 

321 

  

But you can also specify the variable names to override the default 

ordering (you can include extra variable names here, too). :: 

  

sage: fp = fast_callable(p, vars=('x','w','z','y')) 

  

For symbolic expressions, you need to specify the variable names, so 

that :func:`fast_callable` knows what order to use. :: 

  

sage: var('y,z,x') 

(y, z, x) 

sage: f = 10*y + 100*z + x 

sage: ff = fast_callable(f, vars=(x,y,z)) 

sage: ff(1,2,3) 

321 

  

You can also specify extra variable names:: 

  

sage: ff = fast_callable(f, vars=('x','w','z','y')) 

sage: ff(1,2,3,4) 

341 

  

This should be enough for normal use of :func:`fast_callable`; let's 

discuss some more advanced topics. 

  

Sometimes it may be useful to create a fast version of an expression 

without going through symbolic expressions or polynomials; perhaps 

because you want to describe to :func:`fast_callable` an expression 

with common subexpressions. 

  

Internally, :func:`fast_callable` works in two stages: it constructs 

an expression tree from its argument, and then it builds a 

fast evaluator from that expression tree. You can bypass the first phase 

by building your own expression tree and passing that directly to 

:func:`fast_callable`, using an :class:`ExpressionTreeBuilder`. :: 

  

sage: from sage.ext.fast_callable import ExpressionTreeBuilder 

sage: etb = ExpressionTreeBuilder(vars=('x','y','z')) 

  

An :class:`ExpressionTreeBuilder` has three interesting methods: 

:meth:`constant`, :meth:`var`, and :meth:`call`. 

All of these methods return :class:`ExpressionTree` objects. 

  

The :meth:`var` method takes a string, and returns an expression tree 

for the corresponding variable. :: 

  

sage: x = etb.var('x') 

sage: y = etb.var('y') 

sage: z = etb.var('y') 

  

Expression trees support Python's numeric operators, so you can easily 

build expression trees representing arithmetic expressions. :: 

  

sage: v1 = (x+y)*(y+z) + (y//z) 

  

The :meth:`constant` method takes a Sage value, and returns an 

expression tree representing that value. :: 

  

sage: v2 = etb.constant(3.14159) * x + etb.constant(1729) * y 

  

The :meth:`call` method takes a sage/Python function and zero or more 

expression trees, and returns an expression tree representing 

the function call. :: 

  

sage: v3 = etb.call(sin, v1+v2) 

sage: v3 

sin(add(add(mul(add(v_0, v_1), add(v_1, v_1)), floordiv(v_1, v_1)), add(mul(3.14159000000000, v_0), mul(1729, v_1)))) 

  

Many sage/Python built-in functions are specially handled; for instance, 

when evaluating an expression involving :func:`sin` over ``RDF``, 

the C math library function :func:`sin` is called. Arbitrary functions 

are allowed, but will be much slower since they will call back to 

Python code on every call; for example, the following will work. :: 

  

sage: def my_sqrt(x): return pow(x, 0.5) 

sage: e = etb.call(my_sqrt, v1); e 

{my_sqrt}(add(mul(add(v_0, v_1), add(v_1, v_1)), floordiv(v_1, v_1))) 

sage: fast_callable(e)(1, 2, 3) 

3.60555127546399 

  

To provide :func:`fast_callable` for your own class (so that 

``fast_callable(x)`` works when ``x`` is an instance of your 

class), implement a method ``_fast_callable_(self, etb)`` for your class. 

This method takes an :class:`ExpressionTreeBuilder`, and returns an 

expression tree built up using the methods described above. 

  

EXAMPLES:: 

  

sage: var('x') 

x 

sage: f = fast_callable(sqrt(x^7+1), vars=[x], domain=float) 

  

:: 

  

sage: f(1) 

1.4142135623730951 

sage: f.op_list() 

[('load_arg', 0), ('ipow', 7), ('load_const', 1.0), 'add', 'sqrt', 'return'] 

  

To interpret that last line, we load argument 0 ('x' in this case) onto 

the stack, push the constant 7.0 onto the stack, call the pow function 

(which takes 2 arguments from the stack), push the constant 1.0, add the 

top two arguments of the stack, and then call sqrt. 

  

Here we take sin of the first argument and add it to f:: 

  

sage: from sage.ext.fast_callable import ExpressionTreeBuilder 

sage: etb = ExpressionTreeBuilder('x') 

sage: x = etb.var('x') 

sage: f = etb.call(sqrt, x^7 + 1) 

sage: g = etb.call(sin, x) 

sage: fast_callable(f+g).op_list() 

[('load_arg', 0), ('ipow', 7), ('load_const', 1), 'add', ('py_call', <function sqrt at ...>, 1), ('load_arg', 0), ('py_call', sin, 1), 'add', 'return'] 

  

  

AUTHOR: 

  

- Carl Witty (2009-02): initial version (heavily inspired by 

Robert Bradshaw's fast_eval.pyx) 

  

.. TODO:: 

  

The following bits of text were written for the module docstring. 

They are not true yet, but I hope they will be true someday, at 

which point I will move them into the main text. 

  

The final interesting method of :class:`ExpressionTreeBuilder` is 

:meth:`choice`. This produces conditional expressions, like the C 

``COND ? T : F`` expression or the Python ``T if COND else F``. 

This lets you define piecewise functions using :func:`fast_callable`. :: 

  

sage: v4 = etb.choice(v3 >= etb.constant(0), v1, v2) 

  

The arguments are ``(COND, T, F)`` (the same order as in C), so the 

above means that if ``v3`` evaluates to a nonnegative number, 

then ``v4`` will evaluate to the result of ``v1``; 

otherwise, ``v4`` will evaluate to the result of ``v2``. 

  

Let's see an example where we see that :func:`fast_callable` does not 

evaluate common subexpressions more than once. We'll make a 

:func:`fast_callable` expression that gives the result 

of 16 iterations of the Mandelbrot function. :: 

  

sage: etb = ExpressionTreeBuilder('c') 

sage: z = etb.constant(0) 

sage: c = etb.var('c') 

sage: for i in range(16): 

....: z = z*z + c 

sage: mand = fast_callable(z, domain=CDF) 

  

Now ``ff`` does 32 complex arithmetic operations on each call 

(16 additions and 16 multiplications). However, if ``z*z`` produced 

code that evaluated ``z`` twice, then this would do many 

thousands of arithmetic operations instead. 

  

Note that the handling for common subexpressions only checks whether 

expression trees are the same Python object; for instance, the following 

code will evaluate ``x+1`` twice:: 

  

sage: etb = ExpressionTreeBuilder('x') 

sage: x = etb.var('x') 

sage: (x+1)*(x+1) 

mul(add(v_0, 1), add(v_0, 1)) 

  

but this code will only evaluate ``x+1`` once:: 

  

sage: v = x+1; v*v 

mul(add(v_0, 1), add(v_0, 1)) 

""" 

  

  

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

# Copyright (C) 2008 Robert Bradshaw <robertwb@math.washington.edu> 

# Copyright (C) 2009 Carl Witty <Carl.Witty@gmail.com> 

# 

# 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 absolute_import 

  

import operator 

from copy import copy 

from sage.rings.real_mpfr cimport RealField_class, RealNumber 

from sage.rings.complex_field import ComplexField_class 

from sage.rings.all import RDF, CDF 

from sage.rings.integer import Integer 

from sage.rings.integer_ring import ZZ 

from sage.structure.element cimport parent 

  

  

def fast_callable(x, domain=None, vars=None, 

_autocompute_vars_for_backward_compatibility_with_deprecated_fast_float_functionality=False, 

expect_one_var=False): 

r""" 

Given an expression x, compiles it into a form that can be quickly 

evaluated, given values for the variables in x. 

  

Currently, x can be an expression object, an element of SR, or a 

(univariate or multivariate) polynomial; this list will probably 

be extended soon. 

  

By default, x is evaluated the same way that a Python function 

would evaluate it -- addition maps to PyNumber_Add, etc. However, 

you can specify domain=D where D is some Sage parent or Python 

type; in this case, all arithmetic is done in that domain. If we 

have a special-purpose interpreter for that parent (like RDF or float), 

domain=... will trigger the use of that interpreter. 

  

If vars is None and x is a polynomial, then we will use the 

generators of parent(x) as the variables; otherwise, vars must be 

specified (unless x is a symbolic expression with only one variable, 

and expect_one_var is True, in which case we will use that variable). 

  

EXAMPLES:: 

  

sage: var('x') 

x 

sage: expr = sin(x) + 3*x^2 

sage: f = fast_callable(expr, vars=[x]) 

sage: f(2) 

sin(2) + 12 

sage: f(2.0) 

12.9092974268257 

  

We have special fast interpreters for domain=float and domain=RDF. 

(Actually it's the same interpreter; only the return type varies.) 

Note that the float interpreter is not actually more accurate than 

the RDF interpreter; elements of RDF just don't display all 

their digits. We have special fast interpreter for domain=CDF:: 

  

sage: f_float = fast_callable(expr, vars=[x], domain=float) 

sage: f_float(2) 

12.909297426825681 

sage: f_rdf = fast_callable(expr, vars=[x], domain=RDF) 

sage: f_rdf(2) 

12.909297426825681 

sage: f_cdf = fast_callable(expr, vars=[x], domain=CDF) 

sage: f_cdf(2) 

12.909297426825681 

sage: f_cdf(2+I) 

10.40311925062204 + 11.510943740958707*I 

sage: f = fast_callable(expr, vars=('z','x','y')) 

sage: f(1, 2, 3) 

sin(2) + 12 

sage: K.<x> = QQ[] 

sage: p = K.random_element(6); p 

-x^6 - 12*x^5 + 1/2*x^4 - 1/95*x^3 - 1/2*x^2 - 4 

sage: fp = fast_callable(p, domain=RDF) 

sage: fp.op_list() 

[('load_arg', 0), ('load_const', -1.0), 'mul', ('load_const', -12.0), 'add', ('load_arg', 0), 'mul', ('load_const', 0.5), 'add', ('load_arg', 0), 'mul', ('load_const', -0.010526315789473684), 'add', ('load_arg', 0), 'mul', ('load_const', -0.5), 'add', ('load_arg', 0), 'mul', ('load_arg', 0), 'mul', ('load_const', -4.0), 'add', 'return'] 

sage: fp(3.14159) 

-4594.161823640176 

sage: K.<x,y,z> = QQ[] 

sage: p = K.random_element(degree=3, terms=5); p 

-x*y^2 - x*z^2 - 6*x^2 - y^2 - 3*x*z 

sage: fp = fast_callable(p, domain=RDF) 

sage: fp.op_list() 

[('load_const', 0.0), ('load_const', -3.0), ('load_arg', 0), ('ipow', 1), ('load_arg', 2), ('ipow', 1), 'mul', 'mul', 'add', ('load_const', -1.0), ('load_arg', 0), ('ipow', 1), ('load_arg', 1), ('ipow', 2), 'mul', 'mul', 'add', ('load_const', -6.0), ('load_arg', 0), ('ipow', 2), 'mul', 'add', ('load_const', -1.0), ('load_arg', 1), ('ipow', 2), 'mul', 'add', ('load_const', -1.0), ('load_arg', 0), ('ipow', 1), ('load_arg', 2), ('ipow', 2), 'mul', 'mul', 'add', 'return'] 

sage: fp(e, pi, sqrt(2)) # abs tol 3e-14 

-98.00156403362932 

sage: symbolic_result = p(e, pi, sqrt(2)); symbolic_result 

-pi^2*e - pi^2 - 3*sqrt(2)*e - 6*e^2 - 2*e 

sage: n(symbolic_result) 

-98.0015640336293 

  

:: 

  

sage: from sage.ext.fast_callable import ExpressionTreeBuilder 

sage: etb = ExpressionTreeBuilder(vars=('x','y'), domain=float) 

sage: x = etb.var('x') 

sage: y = etb.var('y') 

sage: expr = etb.call(sin, x^2 + y); expr 

sin(add(ipow(v_0, 2), v_1)) 

sage: fc = fast_callable(expr, domain=float) 

sage: fc(5, 7) 

0.5514266812416906 

  

Check that fast_callable also works for symbolic functions with evaluation 

functions:: 

  

sage: def evalf_func(self, x, y, parent): return parent(x*y) if parent is not None else x*y 

sage: x,y = var('x,y') 

sage: f = function('f', evalf_func=evalf_func) 

sage: fc = fast_callable(f(x, y), vars=[x, y]) 

sage: fc(3, 4) 

f(3, 4) 

  

And also when there are complex values involved:: 

  

sage: def evalf_func(self, x, y, parent): return parent(I*x*y) if parent is not None else I*x*y 

sage: g = function('g', evalf_func=evalf_func) 

sage: fc = fast_callable(g(x, y), vars=[x, y]) 

sage: fc(3, 4) 

g(3, 4) 

sage: fc2 = fast_callable(g(x, y), domain=complex, vars=[x, y]) 

sage: fc2(3, 4) 

12j 

sage: fc3 = fast_callable(g(x, y), domain=float, vars=[x, y]) 

sage: fc3(3, 4) 

Traceback (most recent call last): 

... 

TypeError: unable to simplify to float approximation 

  

Check :trac:`24805`--if a fast_callable expression involves division 

on a Python object, it will always prefer Python 3 semantics (e.g. 

``x / y`` will try ``x.__truediv__`` instead of ``x.__div__``, as if 

``from __future__ import division`` is in effect). However, for 

classes that implement ``__div__`` but not ``__truediv__`` it will still 

fall back on ``__div__`` for backwards-compatibility, but reliance on 

this functionality is deprecated:: 

  

sage: from sage.ext.fast_callable import ExpressionTreeBuilder 

sage: etb = ExpressionTreeBuilder('x') 

sage: x = etb.var('x') 

sage: class One(object): 

....: def __div__(self, other): 

....: if not isinstance(other, Integer): 

....: return NotImplemented 

....: return 1 / other 

sage: expr = One() / x 

sage: f = fast_callable(expr, vars=[x]) 

sage: f(2) # py2 

doctest:warning...: 

DeprecationWarning: use of __truediv__ should be preferred over __div__ 

See https://trac.sagemath.org/24805 for details. 

1/2 

sage: class ModernOne(One): 

....: def __truediv__(self, other): 

....: if not isinstance(other, Integer): 

....: return NotImplemented 

....: return 1 / other 

sage: expr = ModernOne() / x 

sage: f = fast_callable(expr, vars=[x]) 

sage: f(2) 

1/2 

""" 

cdef Expression et 

if isinstance(x, Expression): 

et = x 

vars = et._etb._vars 

else: 

if vars is None or len(vars) == 0: 

from sage.symbolic.ring import SR 

from sage.symbolic.callable import is_CallableSymbolicExpressionRing 

from sage.symbolic.expression import is_Expression 

  

# XXX This is pretty gross... there should be a "callable_variables" 

# method that does all this. 

vars = x.variables() 

if x.parent() is SR and x.number_of_arguments() > len(vars): 

vars = list(vars) + ['EXTRA_VAR%d' % n for n in range(len(vars), x.number_of_arguments())] 

  

# Failing to specify the variables is deprecated for any 

# symbolic expression, except for PrimitiveFunction and 

# CallableSymbolicExpression. 

if is_Expression(x) and not is_CallableSymbolicExpressionRing(x.parent()): 

if expect_one_var and len(vars) <= 1: 

if len(vars) == 0: 

vars = ['EXTRA_VAR0'] 

else: 

if _autocompute_vars_for_backward_compatibility_with_deprecated_fast_float_functionality: 

from sage.misc.superseded import deprecation 

deprecation(5413, "Substitution using function-call syntax and unnamed arguments is deprecated and will be removed from a future release of Sage; you can use named arguments instead, like EXPR(x=..., y=...)") 

else: 

raise ValueError("List of variables must be specified for symbolic expressions") 

from sage.rings.polynomial.polynomial_ring import is_PolynomialRing 

from sage.rings.polynomial.multi_polynomial_ring import is_MPolynomialRing 

if is_PolynomialRing(x.parent()) or is_MPolynomialRing(x.parent()): 

vars = x.parent().variable_names() 

  

etb = ExpressionTreeBuilder(vars=vars, domain=domain) 

et = x._fast_callable_(etb) 

  

if isinstance(domain, RealField_class): 

import sage.ext.interpreters.wrapper_rr 

builder = sage.ext.interpreters.wrapper_rr.Wrapper_rr 

  

str = InstructionStream(sage.ext.interpreters.wrapper_rr.metadata, 

len(vars), 

domain) 

  

elif isinstance(domain, ComplexField_class): 

import sage.ext.interpreters.wrapper_cc 

builder = sage.ext.interpreters.wrapper_cc.Wrapper_cc 

str = InstructionStream(sage.ext.interpreters.wrapper_cc.metadata, 

len(vars), 

domain) 

  

elif domain == RDF or domain is float: 

import sage.ext.interpreters.wrapper_rdf 

builder = sage.ext.interpreters.wrapper_rdf.Wrapper_rdf 

str = InstructionStream(sage.ext.interpreters.wrapper_rdf.metadata, 

len(vars), 

domain) 

elif domain == CDF: 

import sage.ext.interpreters.wrapper_cdf 

builder = sage.ext.interpreters.wrapper_cdf.Wrapper_cdf 

str = InstructionStream(sage.ext.interpreters.wrapper_cdf.metadata, 

len(vars), 

domain) 

elif domain is None: 

import sage.ext.interpreters.wrapper_py 

builder = sage.ext.interpreters.wrapper_py.Wrapper_py 

str = InstructionStream(sage.ext.interpreters.wrapper_py.metadata, 

len(vars)) 

else: 

import sage.ext.interpreters.wrapper_el 

builder = sage.ext.interpreters.wrapper_el.Wrapper_el 

str = InstructionStream(sage.ext.interpreters.wrapper_el.metadata, 

len(vars), 

domain) 

generate_code(et, str) 

str.instr('return') 

return builder(str.get_current()) 

  

def function_name(fn): 

r""" 

Given a function, returns a string giving a name for the function. 

  

For functions we recognize, we use our standard opcode name for the 

function (so operator.add becomes 'add', and sage.all.sin becomes 'sin'). 

  

For functions we don't recognize, we try to come up with a name, 

but the name will be wrapped in braces; this is a signal that 

we'll definitely use a slow Python call to call this function. 

(We may use a slow Python call even for functions we do recognize, 

if we're targeting an interpreter without an opcode for the function.) 

  

Only used when printing Expressions. 

  

EXAMPLES:: 

  

sage: from sage.ext.fast_callable import function_name 

sage: function_name(operator.pow) 

'pow' 

sage: function_name(cos) 

'cos' 

sage: function_name(factorial) 

'{factorial}' 

""" 

from sage.structure.dynamic_class import DynamicMetaclass 

if isinstance(type(fn), DynamicMetaclass): 

return "{%r}" % fn 

builtins = get_builtin_functions() 

if fn in builtins: 

return builtins[fn] 

try: 

return "{%s}" % fn.__name__ 

except AttributeError: 

return "{%r}" % fn 

  

cdef class ExpressionTreeBuilder: 

r""" 

A class with helper methods for building Expressions. 

  

An instance of this class is passed to _fast_callable_ methods; 

you can also instantiate it yourself to create your own expressions 

for fast_callable, bypassing _fast_callable_. 

  

EXAMPLES:: 

  

sage: from sage.ext.fast_callable import ExpressionTreeBuilder 

sage: etb = ExpressionTreeBuilder('x') 

sage: x = etb.var('x') 

sage: (x+3)*5 

mul(add(v_0, 3), 5) 

""" 

  

cdef readonly object _domain 

cdef readonly object _vars 

  

def __init__(self, vars, domain=None): 

r""" 

Initialize an instance of ExpressionTreeBuilder. Takes 

a list or tuple of variable names to use, and also an optional 

domain. If a domain is given, then creating an ExpressionConstant 

node with the __call__, make, or constant methods will convert 

the value into the given domain. 

  

Note that this is the only effect of the domain parameter. It 

is quite possible to use different domains for 

ExpressionTreeBuilder and for fast_callable; in that case, 

constants will be converted twice (once when building the 

Expression, and once when generating code). 

  

EXAMPLES:: 

  

sage: from sage.ext.fast_callable import ExpressionTreeBuilder 

sage: etb = ExpressionTreeBuilder('x') 

sage: etb(3^50) 

717897987691852588770249 

sage: etb = ExpressionTreeBuilder('x', domain=RR) 

sage: etb(3^50) 

7.17897987691853e23 

""" 

  

if isinstance(vars, tuple): 

vars = list(vars) 

elif not isinstance(vars, list): 

vars = [vars] 

  

vars = [self._clean_var(v) for v in vars] 

  

self._domain = domain 

self._vars = vars 

  

def __call__(self, x): 

r""" 

Try to convert the given value to an Expression. If it is already 

an Expression, just return it. If it has a _fast_callable_ 

method, then call the method with self as an argument. Otherwise, 

use self.constant() to turn it into a constant. 

  

EXAMPLES:: 

  

sage: from sage.ext.fast_callable import ExpressionTreeBuilder 

sage: etb = ExpressionTreeBuilder('x') 

sage: v = etb(3); v, type(v) 

(3, <type 'sage.ext.fast_callable.ExpressionConstant'>) 

sage: v = etb(polygen(QQ)); v, type(v) 

(v_0, <type 'sage.ext.fast_callable.ExpressionVariable'>) 

sage: v is etb(v) 

True 

""" 

if isinstance(x, Expression): 

return x 

  

try: 

fc = x._fast_callable_ 

except AttributeError: 

return self.constant(x) 

  

return fc(self) 

  

def _clean_var(self, v): 

r""" 

Give a variable name, given a variable. 

  

EXAMPLES:: 

  

sage: from sage.ext.fast_callable import ExpressionTreeBuilder 

sage: etb = ExpressionTreeBuilder('x') 

sage: var('x') 

x 

sage: etb._clean_var(x) 

'x' 

sage: x = polygen(RR); x 

x 

sage: etb._clean_var(x) 

'x' 

""" 

# There should be a better way to do this. (Maybe there is.) 

if not isinstance(v, str): 

v = str(v) 

if '*' in v: 

v = v[v.index('*')+1:] 

return v 

  

def constant(self, c): 

r""" 

Turn the argument into an ExpressionConstant, converting it to 

our domain if we have one. 

  

EXAMPLES:: 

  

sage: from sage.ext.fast_callable import ExpressionTreeBuilder 

sage: etb = ExpressionTreeBuilder('x') 

sage: etb.constant(pi) 

pi 

sage: etb = ExpressionTreeBuilder('x', domain=RealField(200)) 

sage: etb.constant(pi) 

3.1415926535897932384626433832795028841971693993751058209749 

""" 

if self._domain is not None: 

c = self._domain(c) 

return ExpressionConstant(self, c) 

  

def var(self, v): 

r""" 

Turn the argument into an ExpressionVariable. Looks it up in 

the list of variables. (Variables are matched by name.) 

  

EXAMPLES:: 

  

sage: from sage.ext.fast_callable import ExpressionTreeBuilder 

sage: var('a,b,some_really_long_name') 

(a, b, some_really_long_name) 

sage: x = polygen(QQ) 

sage: etb = ExpressionTreeBuilder(vars=('a','b',some_really_long_name, x)) 

sage: etb.var(some_really_long_name) 

v_2 

sage: etb.var('some_really_long_name') 

v_2 

sage: etb.var(x) 

v_3 

sage: etb.var('y') 

Traceback (most recent call last): 

... 

ValueError: Variable 'y' not found 

""" 

var_name = self._clean_var(v) 

try: 

ind = self._vars.index(var_name) 

except ValueError: 

raise ValueError("Variable '%s' not found" % var_name) 

return ExpressionVariable(self, ind) 

  

def _var_number(self, n): 

r""" 

Given an integer, return the variable with that index. 

  

EXAMPLES:: 

  

sage: from sage.ext.fast_callable import ExpressionTreeBuilder 

sage: etb = ExpressionTreeBuilder(vars=('a','b','c','d')) 

sage: etb._var_number(0) 

v_0 

sage: etb._var_number(3) 

v_3 

sage: etb._var_number(4) 

Traceback (most recent call last): 

... 

ValueError: Variable number 4 out of range 

""" 

if 0 <= n < len(self._vars): 

return ExpressionVariable(self, n) 

raise ValueError("Variable number %d out of range" % n) 

  

def call(self, fn, *args): 

r""" 

Construct a call node, given a function and a list of arguments. 

The arguments will be converted to Expressions using 

ExpressionTreeBuilder.__call__. 

  

As a special case, notices if the function is operator.pow and 

the second argument is integral, and constructs an ExpressionIPow 

instead. 

  

EXAMPLES:: 

  

sage: from sage.ext.fast_callable import ExpressionTreeBuilder 

sage: etb = ExpressionTreeBuilder(vars=(x,)) 

sage: etb.call(cos, x) 

cos(v_0) 

sage: etb.call(sin, 1) 

sin(1) 

sage: etb.call(sin, etb(1)) 

sin(1) 

sage: etb.call(factorial, x+57) 

{factorial}(add(v_0, 57)) 

sage: etb.call(operator.pow, x, 543) 

ipow(v_0, 543) 

""" 

if fn is operator.pow: 

base, exponent = args 

return self(base)**exponent 

else: 

return ExpressionCall(self, fn, [self(a) for a in args]) 

  

def choice(self, cond, iftrue, iffalse): 

r""" 

Construct a choice node (a conditional expression), given the 

condition, and the values for the true and false cases. 

  

(It's possible to create choice nodes, but they don't work yet.) 

  

EXAMPLES:: 

  

sage: from sage.ext.fast_callable import ExpressionTreeBuilder 

sage: etb = ExpressionTreeBuilder(vars=(x,)) 

sage: etb.choice(etb.call(operator.eq, x, 0), 0, 1/x) 

(0 if {eq}(v_0, 0) else div(1, v_0)) 

""" 

return ExpressionChoice(self, 

cond, 

self(iftrue), 

self(iffalse)) 

  

# Cache these values, to make expression building a tiny bit faster 

# (by skipping the hash-table lookup in the operator module). 

cdef op_add = operator.add 

cdef op_sub = operator.sub 

cdef op_mul = operator.mul 

cdef op_div 

try: 

op_div = operator.div 

except AttributeError: 

op_div = object() # Unique object not equal to anything else 

cdef op_truediv = operator.truediv 

cdef op_floordiv = operator.floordiv 

cdef op_pow = operator.pow 

cdef op_neg = operator.neg 

cdef op_abs = operator.abs 

cdef op_inv = operator.inv 

  

cdef class Expression: 

r""" 

Represents an expression for fast_callable. 

  

Supports the standard Python arithmetic operators; if arithmetic 

is attempted between an Expression and a non-Expression, the 

non-Expression is converted to an expression (using the 

__call__ method of the Expression's ExpressionTreeBuilder). 

  

EXAMPLES:: 

  

sage: from sage.ext.fast_callable import ExpressionTreeBuilder 

sage: etb = ExpressionTreeBuilder(vars=(x,)) 

sage: x = etb.var(x) 

sage: etb(x) 

v_0 

sage: etb(3) 

3 

sage: etb.call(sin, x) 

sin(v_0) 

sage: (x+1)/(x-1) 

div(add(v_0, 1), sub(v_0, 1)) 

sage: x//5 

floordiv(v_0, 5) 

sage: -abs(~x) 

neg(abs(inv(v_0))) 

""" 

  

cdef ExpressionTreeBuilder _etb 

  

def __init__(self, etb): 

r""" 

Initialize an Expression. Sets the _etb member. 

  

EXAMPLES:: 

  

sage: from sage.ext.fast_callable import ExpressionTreeBuilder 

sage: etb = ExpressionTreeBuilder(vars=(x,)) 

sage: v = etb(3); v # indirect doctest 

3 

sage: v._get_etb() is etb 

True 

""" 

self._etb = etb 

  

def _get_etb(self): 

r""" 

Returns the ExpressionTreeBuilder used to build a given expression. 

  

EXAMPLES:: 

  

sage: from sage.ext.fast_callable import ExpressionTreeBuilder 

sage: etb = ExpressionTreeBuilder(vars=(x,)) 

sage: v = etb(3); v 

3 

sage: v._get_etb() is etb 

True 

""" 

return self._etb 

  

def __add__(s, o): 

r""" 

Compute a sum of two Expressions. 

  

EXAMPLES:: 

  

sage: from sage.ext.fast_callable import ExpressionTreeBuilder 

sage: etb = ExpressionTreeBuilder(vars=(x,)) 

sage: x = etb(x) 

sage: x+x 

add(v_0, v_0) 

sage: x+1 

add(v_0, 1) 

sage: 1+x 

add(1, v_0) 

sage: x.__add__(1) 

add(v_0, 1) 

sage: x.__radd__(1) 

add(1, v_0) 

""" 

return _expression_binop_helper(s, o, op_add) 

  

def __sub__(s, o): 

r""" 

Compute a difference of two Expressions. 

  

EXAMPLES:: 

  

sage: from sage.ext.fast_callable import ExpressionTreeBuilder 

sage: etb = ExpressionTreeBuilder(vars=(x,)) 

sage: x = etb(x) 

sage: x-x 

sub(v_0, v_0) 

sage: x-1 

sub(v_0, 1) 

sage: 1-x 

sub(1, v_0) 

sage: x.__sub__(1) 

sub(v_0, 1) 

sage: x.__rsub__(1) 

sub(1, v_0) 

""" 

return _expression_binop_helper(s, o, op_sub) 

  

def __mul__(s, o): 

r""" 

Compute a product of two Expressions. 

  

EXAMPLES:: 

  

sage: from sage.ext.fast_callable import ExpressionTreeBuilder 

sage: etb = ExpressionTreeBuilder(vars=(x,)) 

sage: x = etb(x) 

sage: x*x 

mul(v_0, v_0) 

sage: x*1 

mul(v_0, 1) 

sage: 1*x 

mul(1, v_0) 

sage: x.__mul__(1) 

mul(v_0, 1) 

sage: x.__rmul__(1) 

mul(1, v_0) 

""" 

return _expression_binop_helper(s, o, op_mul) 

  

def __truediv__(s, o): 

r""" 

Compute a quotient of two Expressions. 

  

EXAMPLES:: 

  

sage: from sage.ext.fast_callable import ExpressionTreeBuilder 

sage: etb = ExpressionTreeBuilder(vars=(x,)) 

sage: x = etb(x) 

sage: x/x 

div(v_0, v_0) 

sage: x/1 

div(v_0, 1) 

sage: 1/x 

div(1, v_0) 

sage: x.__truediv__(1) 

div(v_0, 1) 

sage: x.__rtruediv__(1) 

div(1, v_0) 

""" 

return _expression_binop_helper(s, o, op_truediv) 

  

def __div__(s, o): 

r""" 

Compute a quotient of two Expressions. 

  

EXAMPLES:: 

  

sage: from sage.ext.fast_callable import ExpressionTreeBuilder 

sage: etb = ExpressionTreeBuilder(vars=(x,)) 

sage: x = etb(x) 

sage: x/x 

div(v_0, v_0) 

sage: x/1 

div(v_0, 1) 

sage: 1/x 

div(1, v_0) 

sage: x.__div__(1) # py2 

div(v_0, 1) 

sage: x.__rdiv__(1) # py2 

div(1, v_0) 

""" 

return _expression_binop_helper(s, o, op_div) 

  

def __floordiv__(s, o): 

r""" 

Compute the floordiv (the floor of the quotient) of two Expressions. 

  

EXAMPLES:: 

  

sage: from sage.ext.fast_callable import ExpressionTreeBuilder 

sage: etb = ExpressionTreeBuilder(vars=(x,)) 

sage: x = etb(x) 

sage: x//x 

floordiv(v_0, v_0) 

sage: x//1 

floordiv(v_0, 1) 

sage: 1//x 

floordiv(1, v_0) 

sage: x.__floordiv__(1) 

floordiv(v_0, 1) 

sage: x.__rfloordiv__(1) 

floordiv(1, v_0) 

""" 

return _expression_binop_helper(s, o, op_floordiv) 

  

def __pow__(s, o, dummy): 

r""" 

Compute a power expression from two Expressions. 

  

If the second Expression is a constant integer, then return 

an ExpressionIPow instead of an ExpressionCall. 

  

EXAMPLES:: 

  

sage: from sage.ext.fast_callable import ExpressionTreeBuilder 

sage: etb = ExpressionTreeBuilder(vars=(x,)) 

sage: x = etb(x) 

sage: x^x 

pow(v_0, v_0) 

sage: x^1 

ipow(v_0, 1) 

sage: x.__pow__(1) 

ipow(v_0, 1) 

sage: x.__pow__(1.0) 

pow(v_0, 1.00000000000000) 

sage: x.__rpow__(1) 

pow(1, v_0) 

""" 

# XXX There is a performance regression from the original 

# fast_float here; it would replace small integer powers with 

# multiplication. We can't do this safely until we support 

# common subexpression elimination (or at least the dup instruction). 

# (Plus, we should consider how strict a semantics we want; 

# probably this sort of optimization should be controlled by a 

# flag.) 

  

cdef Expression es 

if isinstance(o, (int, long, Integer)): 

es = s 

return ExpressionIPow(es._etb, s, o) 

else: 

# I really don't like this, but I can't think of a better way 

from sage.symbolic.expression import is_Expression 

if is_Expression(o) and o in ZZ: 

es = s 

return ExpressionIPow(es._etb, s, ZZ(o)) 

else: 

return _expression_binop_helper(s, o, op_pow) 

  

def __neg__(self): 

r""" 

Compute the negation of an Expression. 

  

EXAMPLES:: 

  

sage: from sage.ext.fast_callable import ExpressionTreeBuilder 

sage: etb = ExpressionTreeBuilder(vars=(x,)) 

sage: x = etb(x) 

sage: -x 

neg(v_0) 

sage: x.__neg__() 

neg(v_0) 

""" 

return ExpressionCall(self._etb, op_neg, [self]) 

  

def __abs__(self): 

r""" 

Compute the absolute value of an Expression. 

  

EXAMPLES:: 

  

sage: from sage.ext.fast_callable import ExpressionTreeBuilder 

sage: etb = ExpressionTreeBuilder(vars=(x,)) 

sage: x = etb(x) 

sage: abs(x) 

abs(v_0) 

sage: x.abs() 

abs(v_0) 

sage: x.__abs__() 

abs(v_0) 

""" 

return ExpressionCall(self._etb, op_abs, [self]) 

  

def abs(self): 

r""" 

Compute the absolute value of an Expression. 

  

EXAMPLES:: 

  

sage: from sage.ext.fast_callable import ExpressionTreeBuilder 

sage: etb = ExpressionTreeBuilder(vars=(x,)) 

sage: x = etb(x) 

sage: abs(x) 

abs(v_0) 

sage: x.abs() 

abs(v_0) 

sage: x.__abs__() 

abs(v_0) 

""" 

return ExpressionCall(self._etb, op_abs, [self]) 

  

def __invert__(self): 

r""" 

Compute the inverse of an Expression. 

  

EXAMPLES:: 

  

sage: from sage.ext.fast_callable import ExpressionTreeBuilder 

sage: etb = ExpressionTreeBuilder(vars=(x,)) 

sage: x = etb(x) 

sage: ~x 

inv(v_0) 

sage: x.__invert__() 

inv(v_0) 

""" 

return ExpressionCall(self._etb, op_inv, [self]) 

  

  

cdef class ExpressionConstant(Expression): 

r""" 

An Expression that represents an arbitrary constant. 

  

EXAMPLES: 

sage: from sage.ext.fast_callable import ExpressionTreeBuilder 

sage: etb = ExpressionTreeBuilder(vars=(x,)) 

sage: type(etb(3)) 

<type 'sage.ext.fast_callable.ExpressionConstant'> 

""" 

  

cdef object _value 

  

def __init__(self, etb, c): 

r""" 

Initialize an ExpressionConstant. 

  

EXAMPLES:: 

  

sage: from sage.ext.fast_callable import ExpressionTreeBuilder, ExpressionConstant 

sage: etb = ExpressionTreeBuilder(vars=(x,)) 

sage: etb(3) 

3 

sage: v = ExpressionConstant(etb, 3); v 

3 

sage: v._get_etb() is etb 

True 

sage: v.value() 

3 

sage: v.value() == 3 

True 

""" 

Expression.__init__(self, etb) 

self._value = c 

  

def value(self): 

r""" 

Return the constant value of an ExpressionConstant. 

  

EXAMPLES:: 

  

sage: from sage.ext.fast_callable import ExpressionTreeBuilder 

sage: etb = ExpressionTreeBuilder(vars=(x,)) 

sage: etb(3).value() 

3 

""" 

return self._value 

  

def __repr__(self): 

r""" 

Give a string representing this ExpressionConstant. 

(We use the repr of its value.) 

  

EXAMPLES:: 

  

sage: from sage.ext.fast_callable import ExpressionTreeBuilder 

sage: etb = ExpressionTreeBuilder(vars=(x,)) 

sage: v = etb.constant(pi) 

sage: v 

pi 

sage: repr(v) 

'pi' 

sage: v.__repr__() 

'pi' 

""" 

return repr(self._value) 

  

cdef class ExpressionVariable(Expression): 

r""" 

An Expression that represents a variable. 

  

EXAMPLES: 

sage: from sage.ext.fast_callable import ExpressionTreeBuilder 

sage: etb = ExpressionTreeBuilder(vars=(x,)) 

sage: type(etb.var(x)) 

<type 'sage.ext.fast_callable.ExpressionVariable'> 

""" 

cdef int _variable_index 

  

def __init__(self, etb, int n): 

r""" 

Initialize an ExpressionVariable. 

  

EXAMPLES:: 

  

sage: from sage.ext.fast_callable import ExpressionTreeBuilder, ExpressionVariable 

sage: etb = ExpressionTreeBuilder(vars=(x,)) 

sage: etb(x) 

v_0 

sage: v = ExpressionVariable(etb, 0); v 

v_0 

sage: v._get_etb() is etb 

True 

sage: v.variable_index() 

0 

""" 

Expression.__init__(self, etb) 

self._variable_index = n 

  

def variable_index(self): 

r""" 

Return the variable index of an ExpressionVariable. 

  

EXAMPLES:: 

  

sage: from sage.ext.fast_callable import ExpressionTreeBuilder 

sage: etb = ExpressionTreeBuilder(vars=(x,)) 

sage: etb(x).variable_index() 

0 

""" 

return self._variable_index 

  

def __repr__(self): 

r""" 

Give a string representing this ExpressionVariable. 

  

EXAMPLES:: 

  

sage: from sage.ext.fast_callable import ExpressionTreeBuilder 

sage: etb = ExpressionTreeBuilder(vars=(x,)) 

sage: v = etb._var_number(0) 

sage: v 

v_0 

sage: repr(v) 

'v_0' 

sage: v.__repr__() 

'v_0' 

""" 

# Should we look up the variable name in self._etb, instead? 

# I think not.. I like the emphasis that we're totally removed 

# from the original expression when we have an Expression. 

return "v_%d" % self._variable_index 

  

cdef class ExpressionCall(Expression): 

r""" 

An Expression that represents a function call. 

  

EXAMPLES: 

sage: from sage.ext.fast_callable import ExpressionTreeBuilder 

sage: etb = ExpressionTreeBuilder(vars=(x,)) 

sage: type(etb.call(sin, x)) 

<type 'sage.ext.fast_callable.ExpressionCall'> 

""" 

cdef object _function 

cdef object _arguments 

  

def __init__(self, etb, fn, args): 

r""" 

Initialize an ExpressionCall. 

  

EXAMPLES:: 

  

sage: from sage.ext.fast_callable import ExpressionTreeBuilder, ExpressionCall 

sage: etb = ExpressionTreeBuilder(vars=(x,)) 

sage: x = etb(x) 

sage: etb.call(factorial, x) 

{factorial}(v_0) 

sage: v = ExpressionCall(etb, factorial, [x]); v 

{factorial}(v_0) 

sage: v._get_etb() is etb 

True 

sage: v.function() 

factorial 

sage: v.arguments() 

[v_0] 

""" 

Expression.__init__(self, etb) 

self._function = fn 

self._arguments = args 

  

def function(self): 

r""" 

Return the function from this ExpressionCall. 

  

EXAMPLES:: 

  

sage: from sage.ext.fast_callable import ExpressionTreeBuilder 

sage: etb = ExpressionTreeBuilder(vars=(x,)) 

sage: etb.call(sin, x).function() 

sin 

""" 

return self._function 

  

def arguments(self): 

r""" 

Return the arguments from this ExpressionCall. 

  

EXAMPLES:: 

  

sage: from sage.ext.fast_callable import ExpressionTreeBuilder 

sage: etb = ExpressionTreeBuilder(vars=(x,)) 

sage: etb.call(sin, x).arguments() 

[v_0] 

""" 

return copy(self._arguments) 

  

def __repr__(self): 

r""" 

Give a string representing this ExpressionCall. 

  

EXAMPLES:: 

  

sage: from sage.ext.fast_callable import ExpressionTreeBuilder 

sage: etb = ExpressionTreeBuilder(vars=(x,)) 

sage: x = etb.var(x) 

sage: etb.call(operator.add, x, 1) 

add(v_0, 1) 

sage: etb.call(factorial, x) 

{factorial}(v_0) 

sage: v = etb.call(sin, x) 

sage: v 

sin(v_0) 

sage: repr(v) 

'sin(v_0)' 

sage: v.__repr__() 

'sin(v_0)' 

""" 

fn = function_name(self._function) 

return '%s(%s)' % (fn, ', '.join(repr(a) for a in self._arguments)) 

  

  

cdef class ExpressionIPow(Expression): 

r""" 

A power Expression with an integer exponent. 

  

EXAMPLES: 

sage: from sage.ext.fast_callable import ExpressionTreeBuilder 

sage: etb = ExpressionTreeBuilder(vars=(x,)) 

sage: type(etb.var('x')^17) 

<type 'sage.ext.fast_callable.ExpressionIPow'> 

""" 

cdef object _base 

cdef object _exponent 

  

def __init__(self, etb, base, exponent): 

r""" 

Initialize an ExpressionIPow. 

  

EXAMPLES:: 

  

sage: from sage.ext.fast_callable import ExpressionTreeBuilder, ExpressionIPow 

sage: etb = ExpressionTreeBuilder(vars=(x,)) 

sage: x = etb(x) 

sage: x^(-12) 

ipow(v_0, -12) 

sage: v = ExpressionIPow(etb, x, 55); v 

ipow(v_0, 55) 

sage: v._get_etb() is etb 

True 

sage: v.base() 

v_0 

sage: v.exponent() 

55 

""" 

Expression.__init__(self, etb) 

self._base = base 

self._exponent = exponent 

  

def base(self): 

r""" 

Return the base from this ExpressionIPow. 

  

EXAMPLES:: 

  

sage: from sage.ext.fast_callable import ExpressionTreeBuilder 

sage: etb = ExpressionTreeBuilder(vars=(x,)) 

sage: (etb(33)^42).base() 

33 

""" 

return self._base 

  

def exponent(self): 

r""" 

Return the exponent from this ExpressionIPow. 

  

EXAMPLES:: 

  

sage: from sage.ext.fast_callable import ExpressionTreeBuilder 

sage: etb = ExpressionTreeBuilder(vars=(x,)) 

sage: (etb(x)^(-1)).exponent() 

-1 

""" 

return self._exponent 

  

def __repr__(self): 

r""" 

Give a string representing this ExpressionIPow. 

  

EXAMPLES:: 

  

sage: from sage.ext.fast_callable import ExpressionTreeBuilder 

sage: etb = ExpressionTreeBuilder(vars=(x,)) 

sage: x = etb.var(x) 

sage: x^3 

ipow(v_0, 3) 

sage: x^(-2) 

ipow(v_0, -2) 

sage: v = (x+1)^3 

sage: v 

ipow(add(v_0, 1), 3) 

sage: repr(v) 

'ipow(add(v_0, 1), 3)' 

sage: v.__repr__() 

'ipow(add(v_0, 1), 3)' 

""" 

return 'ipow(%s, %d)' % (repr(self._base), self._exponent) 

  

cdef class ExpressionChoice(Expression): 

r""" 

A conditional expression. 

  

(It's possible to create choice nodes, but they don't work yet.) 

  

EXAMPLES:: 

  

sage: from sage.ext.fast_callable import ExpressionTreeBuilder 

sage: etb = ExpressionTreeBuilder(vars=(x,)) 

sage: etb.choice(etb.call(operator.eq, x, 0), 0, 1/x) 

(0 if {eq}(v_0, 0) else div(1, v_0)) 

""" 

  

cdef object _cond 

cdef object _iftrue 

cdef object _iffalse 

  

def __init__(self, etb, cond, iftrue, iffalse): 

r""" 

Initialize an ExpressionChoice. 

  

EXAMPLES:: 

  

sage: from sage.ext.fast_callable import ExpressionTreeBuilder, ExpressionChoice 

sage: etb = ExpressionTreeBuilder(vars=(x,)) 

sage: x = etb(x) 

sage: etb.choice(x, ~x, 0) 

(inv(v_0) if v_0 else 0) 

sage: v = ExpressionChoice(etb, x, ~x, etb(0)); v 

(inv(v_0) if v_0 else 0) 

sage: v._get_etb() is etb 

True 

sage: v.condition() 

v_0 

sage: v.if_true() 

inv(v_0) 

sage: v.if_false() 

0 

""" 

Expression.__init__(self, etb) 

self._cond = cond 

self._iftrue = iftrue 

self._iffalse = iffalse 

  

def condition(self): 

r""" 

Return the condition of an ExpressionChoice. 

  

EXAMPLES:: 

  

sage: from sage.ext.fast_callable import ExpressionTreeBuilder 

sage: etb = ExpressionTreeBuilder(vars=(x,)) 

sage: x = etb(x) 

sage: etb.choice(x, ~x, 0).condition() 

v_0 

""" 

return self._cond 

  

def if_true(self): 

r""" 

Return the true branch of an ExpressionChoice. 

  

EXAMPLES:: 

  

sage: from sage.ext.fast_callable import ExpressionTreeBuilder 

sage: etb = ExpressionTreeBuilder(vars=(x,)) 

sage: x = etb(x) 

sage: etb.choice(x, ~x, 0).if_true() 

inv(v_0) 

""" 

return self._iftrue 

  

def if_false(self): 

r""" 

Return the false branch of an ExpressionChoice. 

  

EXAMPLES:: 

  

sage: from sage.ext.fast_callable import ExpressionTreeBuilder 

sage: etb = ExpressionTreeBuilder(vars=(x,)) 

sage: x = etb(x) 

sage: etb.choice(x, ~x, 0).if_false() 

0 

""" 

return self._iffalse 

  

def __repr__(self): 

r""" 

Give a string representation for this ExpressionChoice. 

(Based on the syntax for Python conditional expressions.) 

  

EXAMPLES:: 

  

sage: from sage.ext.fast_callable import ExpressionTreeBuilder 

sage: etb = ExpressionTreeBuilder(vars=(x,)) 

sage: x = etb(x) 

sage: v = etb.choice(x, ~x, 0) 

sage: v 

(inv(v_0) if v_0 else 0) 

sage: repr(v) 

'(inv(v_0) if v_0 else 0)' 

sage: v.__repr__() 

'(inv(v_0) if v_0 else 0)' 

""" 

return '(%s if %s else %s)' % (repr(self._iftrue), 

repr(self._cond), 

repr(self._iffalse)) 

  

cpdef _expression_binop_helper(s, o, op): 

r""" 

Makes an Expression for (s op o). Either s or o (or both) must already 

be an expression. 

  

EXAMPLES:: 

  

sage: from sage.ext.fast_callable import _expression_binop_helper, ExpressionTreeBuilder 

sage: var('x,y') 

(x, y) 

sage: etb = ExpressionTreeBuilder(vars=(x,y)) 

sage: x = etb(x) 

  

Now x is an Expression, but y is not. Still, all the following 

cases work:: 

  

sage: _expression_binop_helper(x, x, operator.add) 

add(v_0, v_0) 

sage: _expression_binop_helper(x, y, operator.add) 

add(v_0, v_1) 

sage: _expression_binop_helper(y, x, operator.add) 

add(v_1, v_0) 

  

""" 

# The Cython way of handling operator overloading on cdef classes 

# (which is inherited from Python) is quite annoying. Inside the 

# code for a binary operator, you know that either the first or 

# second argument (or both) is a member of your class, but you 

# don't know which. 

  

# If there is an arithmetic operator between an Expression and 

# a non-Expression, I want to convert the non-Expression into 

# an Expression. But to do that, I need the ExpressionTreeBuilder 

# from the Expression. 

  

cdef Expression self 

cdef Expression other 

  

if not isinstance(o, Expression): 

self = s 

other = self._etb(o) 

elif not isinstance(s, Expression): 

other = o 

self = other._etb(s) 

else: 

self = s 

other = o 

assert self._etb is other._etb 

  

return ExpressionCall(self._etb, op, [self, other]) 

  

class IntegerPowerFunction(object): 

r""" 

This class represents the function x^n for an arbitrary integral 

power n. That is, IntegerPowerFunction(2) is the squaring function; 

IntegerPowerFunction(-1) is the reciprocal function. 

  

EXAMPLES:: 

  

sage: from sage.ext.fast_callable import IntegerPowerFunction 

sage: square = IntegerPowerFunction(2) 

sage: square 

(^2) 

sage: square(pi) 

pi^2 

sage: square(I) 

-1 

sage: square(RIF(-1, 1)).str(style='brackets') 

'[0.00000000000000000 .. 1.0000000000000000]' 

sage: IntegerPowerFunction(-1) 

(^(-1)) 

sage: IntegerPowerFunction(-1)(22/7) 

7/22 

sage: v = Integers(123456789)(54321) 

sage: v^9876543210 

79745229 

sage: IntegerPowerFunction(9876543210)(v) 

79745229 

""" 

  

def __init__(self, n): 

r""" 

Initializes an IntegerPowerFunction. 

  

EXAMPLES:: 

  

sage: from sage.ext.fast_callable import IntegerPowerFunction 

sage: cube = IntegerPowerFunction(3) 

sage: cube 

(^3) 

sage: cube(AA(7)^(1/3)) 

7.000000000000000? 

sage: cube.exponent 

3 

""" 

self.exponent = n 

  

def __repr__(self): 

r""" 

Return a string representing this IntegerPowerFunction. 

  

EXAMPLES:: 

  

sage: from sage.ext.fast_callable import IntegerPowerFunction 

sage: square = IntegerPowerFunction(2) 

sage: square 

(^2) 

sage: repr(square) 

'(^2)' 

sage: square.__repr__() 

'(^2)' 

sage: repr(IntegerPowerFunction(-57)) 

'(^(-57))' 

""" 

if self.exponent >= 0: 

return "(^%s)" % self.exponent 

else: 

return "(^(%s))" % self.exponent 

  

def __call__(self, x): 

r""" 

Call this IntegerPowerFunction, to compute a power of its argument. 

  

EXAMPLES:: 

  

sage: from sage.ext.fast_callable import IntegerPowerFunction 

sage: square = IntegerPowerFunction(2) 

sage: square.__call__(5) 

25 

sage: square(5) 

25 

""" 

return x**self.exponent 

  

cdef dict builtin_functions = None 

cpdef dict get_builtin_functions(): 

r""" 

To handle ExpressionCall, we need to map from Sage and 

Python functions to opcode names. 

  

This returns a dictionary which is that map. 

  

We delay building builtin_functions to break a circular import 

between sage.calculus and this file. 

  

EXAMPLES:: 

  

sage: from sage.ext.fast_callable import get_builtin_functions 

sage: builtins = get_builtin_functions() 

sage: sorted(set(builtins.values())) 

['abs', 'acos', 'acosh', 'add', 'asin', 'asinh', 'atan', 'atanh', 'ceil', 'cos', 'cosh', 'cot', 'csc', 'div', 'exp', 'floor', 'floordiv', 'inv', 'log', 'mul', 'neg', 'pow', 'sec', 'sin', 'sinh', 'sqrt', 'sub', 'tan', 'tanh'] 

sage: builtins[sin] 

'sin' 

sage: builtins[ln] 

'log' 

""" 

# We delay building builtin_functions to break a circular import 

# between sage.functions and this file. 

global builtin_functions 

if builtin_functions is not None: 

return builtin_functions 

builtin_functions = { 

op_add: 'add', 

op_sub: 'sub', 

op_mul: 'mul', 

op_div: 'div', 

op_truediv: 'div', 

op_floordiv: 'floordiv', 

op_abs: 'abs', 

op_neg: 'neg', 

op_inv: 'inv', 

op_pow: 'pow', 

} 

  

# not handled: atan2, log2, log10 

import sage.functions.all as func_all 

for fn in ('sqrt', 'ceil', 'floor', 

'sin', 'cos', 'tan', 'sec', 'csc', 'cot', 

'asin', 'acos', 'atan', 'sinh', 'cosh', 'tanh', 

'asinh', 'acosh', 'atanh', 'exp', 'log'): 

builtin_functions[getattr(func_all, fn)] = fn 

builtin_functions[func_all.abs_symbolic] = 'abs' 

builtin_functions[func_all.ln] = 'log' 

return builtin_functions 

  

cdef class InstructionStream # forward declaration 

  

cpdef generate_code(Expression expr, InstructionStream stream): 

r""" 

Generate code from an Expression tree; write the result into an 

InstructionStream. 

  

In fast_callable, first we create an Expression, either directly 

with an ExpressionTreeBuilder or with _fast_callable_ methods. 

Then we optimize the Expression in tree form. (Unfortunately, 

this step is currently missing -- we do no optimizations.) 

  

Then we linearize the Expression into a sequence of instructions, 

by walking the Expression and sending the corresponding stack 

instructions to an InstructionStream. 

  

EXAMPLES:: 

  

sage: from sage.ext.fast_callable import ExpressionTreeBuilder, generate_code, InstructionStream 

sage: etb = ExpressionTreeBuilder('x') 

sage: x = etb.var('x') 

sage: expr = ((x+pi)*(x+1)) 

sage: from sage.ext.interpreters.wrapper_py import metadata, Wrapper_py 

sage: instr_stream = InstructionStream(metadata, 1) 

sage: generate_code(expr, instr_stream) 

sage: instr_stream.instr('return') 

sage: v = Wrapper_py(instr_stream.get_current()) 

sage: type(v) 

<type 'sage.ext.interpreters.wrapper_py.Wrapper_py'> 

sage: v(7) 

8*pi + 56 

  

TESTS:: 

  

sage: def my_sin(x): return sin(x) 

sage: def my_norm(x, y): return x*x + y*y 

sage: def my_sqrt(x): 

....: if x < 0: raise ValueError("sqrt of negative number") 

....: return sqrt(x, extend=False) 

sage: fc = fast_callable(expr, domain=RealField(130)) 

sage: fc(0) 

3.1415926535897932384626433832795028842 

sage: fc(1) 

8.2831853071795864769252867665590057684 

sage: fc = fast_callable(expr, domain=RDF) 

sage: fc(0) 

3.141592653589793 

sage: fc(1) 

8.283185307179586 

sage: fc.op_list() 

[('load_arg', 0), ('load_const', pi), 'add', ('load_arg', 0), ('load_const', 1), 'add', 'mul', 'return'] 

sage: fc = fast_callable(etb.call(sin, x) + etb.call(sqrt, x), domain=RDF) 

sage: fc(1) 

1.8414709848078965 

sage: fc.op_list() 

[('load_arg', 0), 'sin', ('load_arg', 0), 'sqrt', 'add', 'return'] 

sage: fc = fast_callable(etb.call(sin, x) + etb.call(sqrt, x)) 

sage: fc(1) 

sin(1) + 1 

sage: fc.op_list() 

[('load_arg', 0), ('py_call', sin, 1), ('load_arg', 0), ('py_call', <function sqrt at ...>, 1), 'add', 'return'] 

sage: fc = fast_callable(etb.call(my_sin, x), domain=RDF) 

sage: fc(3) 

0.1411200080598672 

sage: fc = fast_callable(etb.call(my_sin, x), domain=RealField(100)) 

sage: fc(3) 

0.14112000805986722210074480281 

sage: fc.op_list() 

[('load_arg', 0), ('py_call', <function my_sin at 0x...>, 1), 'return'] 

sage: fc = fast_callable(etb.call(my_sqrt, x), domain=RDF) 

sage: fc(3) 

1.7320508075688772 

sage: parent(fc(3)) 

Real Double Field 

sage: fc(-3) 

Traceback (most recent call last): 

... 

ValueError: sqrt of negative number 

sage: fc = fast_callable(etb.call(my_sqrt, x), domain=RR) 

sage: fc(3) 

1.73205080756888 

sage: fc(-3) 

Traceback (most recent call last): 

... 

ValueError: sqrt of negative number 

sage: etb2 = ExpressionTreeBuilder(('y','z')) 

sage: y = etb2.var('y') 

sage: z = etb2.var('z') 

sage: fc = fast_callable(etb2.call(sqrt, etb2.call(my_norm, y, z)), domain=RDF) 

sage: fc(3, 4) 

5.0 

sage: fc.op_list() 

[('load_arg', 0), ('load_arg', 1), ('py_call', <function my_norm at 0x...>, 2), 'sqrt', 'return'] 

sage: fc.python_calls() 

[<function my_norm at 0x...>] 

sage: fc = fast_callable(etb2.call(sqrt, etb2.call(my_norm, y, z)), domain=RR) 

sage: fc(3, 4) 

5.00000000000000 

sage: fc = fast_callable(etb2.call(my_norm, y, z), domain=ZZ) 

sage: fc(3, 4) 

25 

sage: fc.op_list() 

[('load_arg', 0), ('load_arg', 1), ('py_call', <function my_norm at 0x...>, 2), 'return'] 

sage: fc = fast_callable(expr) 

sage: fc(3.0r) 

4.0*pi + 12.0 

sage: fc = fast_callable(x+3, domain=ZZ) 

sage: fc(4) 

7 

sage: fc = fast_callable(x/3, domain=ZZ) 

sage: fc(4) 

Traceback (most recent call last): 

... 

TypeError: no conversion of this rational to integer 

sage: fc(6) 

2 

sage: fc = fast_callable(etb.call(sin, x), domain=ZZ) 

sage: fc(0) 

0 

sage: fc(3) 

Traceback (most recent call last): 

... 

TypeError: unable to convert sin(3) to an integer 

  

:: 

  

sage: fc = fast_callable(etb(x)^100) 

sage: fc(pi) 

pi^100 

sage: fc = fast_callable(etb(x)^100, domain=ZZ) 

sage: fc(2) 

1267650600228229401496703205376 

sage: fc = fast_callable(etb(x)^100, domain=RIF) 

sage: fc(RIF(-2)) 

1.2676506002282295?e30 

sage: fc = fast_callable(etb(x)^100, domain=RDF) 

sage: fc.op_list() 

[('load_arg', 0), ('ipow', 100), 'return'] 

sage: fc(1.1) 

13780.61233982... 

sage: fc = fast_callable(etb(x)^100, domain=RR) 

sage: fc.op_list() 

[('load_arg', 0), ('ipow', 100), 'return'] 

sage: fc(1.1) 

13780.6123398224 

sage: fc = fast_callable(etb(x)^(-100), domain=RDF) 

sage: fc.op_list() 

[('load_arg', 0), ('ipow', -100), 'return'] 

sage: fc(1.1) 

7.25657159014...e-05 

sage: fc = fast_callable(etb(x)^(-100), domain=RR) 

sage: fc(1.1) 

0.0000725657159014814 

sage: expo = 2^32 

sage: base = (1.0).nextabove() 

sage: fc = fast_callable(etb(x)^expo, domain=RDF) 

sage: fc.op_list() 

[('load_arg', 0), ('py_call', (^4294967296), 1), 'return'] 

sage: fc(base) # rel tol 1e-15 

1.0000009536747712 

sage: RDF(base)^expo 

1.0000009536747712 

sage: fc = fast_callable(etb(x)^expo, domain=RR) 

sage: fc.op_list() 

[('load_arg', 0), ('py_call', (^4294967296), 1), 'return'] 

sage: fc(base) 

1.00000095367477 

sage: base^expo 

1.00000095367477 

  

Make sure we do not overflow the stack with highly nested expressions 

(:trac:`11766`):: 

  

sage: R.<x> = CC[] 

sage: f = R(list(range(100000))) 

sage: ff = fast_callable(f) 

sage: f(0.5) 

2.00000000000000 

sage: ff(0.5) 

2.00000000000000 

sage: f(0.9), ff(0.9) 

(90.0000000000000, 90.0000000000000) 

""" 

cdef ExpressionConstant econst 

cdef ExpressionVariable evar 

cdef ExpressionCall ecall 

cdef ExpressionChoice echoice 

  

# Maintain our own stack to avoid crashing on deeply-nested expressions. 

cdef list todo = [expr] 

do_call = Expression(None) 

while len(todo): 

expr = todo.pop() 

if isinstance(expr, ExpressionConstant): 

econst = expr 

stream.load_const(econst._value) 

elif isinstance(expr, ExpressionVariable): 

evar = expr 

stream.load_arg(evar._variable_index) 

elif isinstance(expr, ExpressionCall): 

ecall = expr 

todo.append(expr) 

todo.append(do_call) 

for arg in reversed(ecall._arguments): 

todo.append(arg) 

continue 

elif expr is do_call: 

# arguments already evaluated, make the call 

ecall = todo.pop() 

fn = ecall._function 

opname = get_builtin_functions().get(fn) 

if opname is not None: 

if stream.has_instr(opname): 

stream.instr0(opname, ()) 

continue 

if stream.has_instr('py_call'): 

stream.instr('py_call', fn, len(ecall._arguments)) 

else: 

raise ValueError("Unhandled function %s in generate_code" % fn) 

elif isinstance(expr, ExpressionIPow): 

base = expr.base() 

exponent = expr.exponent() 

metadata = stream.get_metadata() 

ipow_range = metadata.ipow_range 

if ipow_range is True: 

use_ipow = True 

elif isinstance(ipow_range, tuple): 

a,b = ipow_range 

use_ipow = (a <= exponent <= b) 

else: 

use_ipow = False 

generate_code(base, stream) 

if use_ipow: 

stream.instr('ipow', exponent) 

else: 

stream.instr('py_call', IntegerPowerFunction(exponent), 1) 

else: 

raise ValueError("Unhandled expression kind %s in generate_code" % type(expr)) 

  

cdef class InterpreterMetadata # forward declaration 

  

cdef class InstructionStream: 

r""" 

An InstructionStream takes a sequence of instructions (passed in by 

a series of method calls) and computes the data structures needed 

by the interpreter. This is the stage where we switch from operating 

on Expression trees to a linear representation. If we had a peephole 

optimizer (we don't) it would go here. 

  

Currently, this class is not very general; it only works for 

interpreters with a fixed set of memory chunks (with fixed names). 

Basically, it only works for stack-based expression interpreters. 

It should be generalized, so that the interpreter metadata includes 

a description of the memory chunks involved and the instruction stream 

can handle any interpreter. 

  

Once you're done adding instructions, you call get_current() to retrieve 

the information needed by the interpreter (as a Python dictionary). 

""" 

  

cdef InterpreterMetadata _metadata 

cdef list _instrs 

cdef list _bytecode 

cdef list _constants 

cdef object _constant_locs 

cdef object _py_constants 

cdef object _py_constant_locs 

cdef int _stack_cur_size 

cdef int _stack_max_size 

cdef int _n_args 

cdef object _domain 

  

def __init__(self, metadata, n_args, domain=None): 

r""" 

Initialize an InstructionStream. 

  

INPUT: 

  

- metadata -- The metadata_by_opname from a wrapper module 

  

- n_args -- The number of arguments accessible by the generated code 

(this is just passed to the wrapper class) 

  

- domain -- The domain of interpretation (this is just passed to the 

wrapper class) 

  

EXAMPLES:: 

  

sage: from sage.ext.interpreters.wrapper_rdf import metadata 

sage: from sage.ext.fast_callable import InstructionStream 

sage: instr_stream = InstructionStream(metadata, 1) 

sage: instr_stream.get_current() 

{'args': 1, 

'code': [], 

'constants': [], 

'domain': None, 

'py_constants': [], 

'stack': 0} 

sage: md = instr_stream.get_metadata() 

sage: type(md) 

<type 'sage.ext.fast_callable.InterpreterMetadata'> 

sage: md.by_opname['py_call'] 

(CompilerInstrSpec(0, 1, ['py_constants', 'n_inputs']), 3) 

sage: md.by_opcode[3] 

('py_call', CompilerInstrSpec(0, 1, ['py_constants', 'n_inputs'])) 

""" 

self._metadata = metadata 

self._instrs = [] 

self._bytecode = [] 

self._constants = [] 

self._constant_locs = {} 

self._py_constants = [] 

self._py_constant_locs = {} 

self._stack_cur_size = 0 

self._stack_max_size = 0 

self._domain = domain 

self._n_args = n_args 

  

def load_const(self, c): 

r""" 

Add a 'load_const' instruction to this InstructionStream. 

  

EXAMPLES:: 

  

sage: from sage.ext.interpreters.wrapper_rdf import metadata 

sage: from sage.ext.fast_callable import InstructionStream, op_list 

sage: instr_stream = InstructionStream(metadata, 1) 

sage: instr_stream.load_const(5) 

sage: instr_stream.current_op_list() 

[('load_const', 5)] 

sage: instr_stream.load_const(7) 

sage: instr_stream.load_const(5) 

sage: instr_stream.current_op_list() 

[('load_const', 5), ('load_const', 7), ('load_const', 5)] 

  

Note that constants are shared: even though we load 5 twice, it 

only appears once in the constant table. :: 

  

sage: instr_stream.get_current()['constants'] 

[5, 7] 

""" 

self.instr('load_const', c) 

  

def load_arg(self, n): 

r""" 

Add a 'load_arg' instruction to this InstructionStream. 

  

EXAMPLES:: 

  

sage: from sage.ext.interpreters.wrapper_rdf import metadata 

sage: from sage.ext.fast_callable import InstructionStream 

sage: instr_stream = InstructionStream(metadata, 12) 

sage: instr_stream.load_arg(5) 

sage: instr_stream.current_op_list() 

[('load_arg', 5)] 

sage: instr_stream.load_arg(3) 

sage: instr_stream.current_op_list() 

[('load_arg', 5), ('load_arg', 3)] 

""" 

self.instr('load_arg', n) 

  

cpdef bint has_instr(self, opname): 

r""" 

Check whether this InstructionStream knows how to generate code 

for a given instruction. 

  

EXAMPLES:: 

  

sage: from sage.ext.interpreters.wrapper_rdf import metadata 

sage: from sage.ext.fast_callable import InstructionStream 

sage: instr_stream = InstructionStream(metadata, 1) 

sage: instr_stream.has_instr('return') 

True 

sage: instr_stream.has_instr('factorial') 

False 

sage: instr_stream.has_instr('abs') 

True 

""" 

return (opname in self._metadata.by_opname) 

  

def instr(self, opname, *args): 

r""" 

Generate code in this InstructionStream for the given instruction 

and arguments. 

  

The opname is used to look up a CompilerInstrSpec; the 

CompilerInstrSpec describes how to interpret the arguments. 

(This is documented in the class docstring for CompilerInstrSpec.) 

  

EXAMPLES:: 

  

sage: from sage.ext.interpreters.wrapper_rdf import metadata 

sage: from sage.ext.fast_callable import InstructionStream 

sage: instr_stream = InstructionStream(metadata, 1) 

sage: instr_stream.instr('load_arg', 0) 

sage: instr_stream.instr('sin') 

sage: instr_stream.instr('py_call', math.sin, 1) 

sage: instr_stream.instr('abs') 

sage: instr_stream.instr('factorial') 

Traceback (most recent call last): 

... 

KeyError: 'factorial' 

sage: instr_stream.instr('return') 

sage: instr_stream.current_op_list() 

[('load_arg', 0), 'sin', ('py_call', <built-in function sin>, 1), 'abs', 'return'] 

""" 

self.instr0(opname, args) 

  

cdef instr0(self, opname, tuple args): 

""" 

Cdef version of instr. (Can't cpdef because of star args.) 

""" 

cdef int i 

  

spec, opcode = self._metadata.by_opname[opname] 

assert len(spec.parameters) == len(args) 

  

cdef int n_inputs = spec.n_inputs 

cdef int n_outputs = spec.n_outputs 

  

self._bytecode.append(opcode) 

for i in range(len(args)): 

if spec.parameters[i] == 'constants': 

# XXX bad for strict-mode floating-point constants 

# (doesn't handle signed 0, NaN) 

arg = args[i] 

if (arg,parent(arg)) in self._constant_locs: 

self._bytecode.append(self._constant_locs[(arg,parent(arg))]) 

else: 

loc = len(self._constants) 

self._constants.append(arg) 

self._constant_locs[(arg,parent(arg))] = loc 

self._bytecode.append(loc) 

elif spec.parameters[i] == 'args': 

self._bytecode.append(args[i]) 

elif spec.parameters[i] == 'code': 

self._bytecode.append(args[i]) 

elif spec.parameters[i] == 'n_inputs': 

self._bytecode.append(args[i]) 

n_inputs = args[i] 

elif spec.parameters[i] == 'n_outputs': 

self._bytecode.append(args[i]) 

n_outputs = args[i] 

elif spec.parameters[i] == 'py_constants': 

arg = args[i] 

if arg in self._py_constant_locs: 

self._bytecode.append(self._py_constant_locs[arg]) 

else: 

loc = len(self._py_constants) 

self._py_constants.append(arg) 

self._py_constant_locs[arg] = loc 

self._bytecode.append(loc) 

else: 

raise ValueError 

  

self._stack_cur_size -= n_inputs 

self._stack_cur_size += n_outputs 

self._stack_max_size = max(self._stack_max_size, self._stack_cur_size) 

  

def get_metadata(self): 

r""" 

Returns the interpreter metadata being used by the current 

InstructionStream. 

  

The code generator sometimes uses this to decide which code 

to generate. 

  

EXAMPLES:: 

  

sage: from sage.ext.interpreters.wrapper_rdf import metadata 

sage: from sage.ext.fast_callable import InstructionStream 

sage: instr_stream = InstructionStream(metadata, 1) 

sage: md = instr_stream.get_metadata() 

sage: type(md) 

<type 'sage.ext.fast_callable.InterpreterMetadata'> 

""" 

return self._metadata 

  

def current_op_list(self): 

r""" 

Returns the list of instructions that have been added to this 

InstructionStream so far. 

  

It's OK to call this, then add more instructions. 

  

EXAMPLES:: 

  

sage: from sage.ext.interpreters.wrapper_rdf import metadata 

sage: from sage.ext.fast_callable import InstructionStream 

sage: instr_stream = InstructionStream(metadata, 1) 

sage: instr_stream.instr('load_arg', 0) 

sage: instr_stream.instr('py_call', math.sin, 1) 

sage: instr_stream.instr('abs') 

sage: instr_stream.instr('return') 

sage: instr_stream.current_op_list() 

[('load_arg', 0), ('py_call', <built-in function sin>, 1), 'abs', 'return'] 

""" 

return op_list(self.get_current(), self._metadata) 

  

def get_current(self): 

r""" 

Return the current state of the InstructionStream, as a dictionary 

suitable for passing to a wrapper class. 

  

NOTE: The dictionary includes internal data structures of the 

InstructionStream; you must not modify it. 

  

EXAMPLES:: 

  

sage: from sage.ext.interpreters.wrapper_rdf import metadata 

sage: from sage.ext.fast_callable import InstructionStream 

sage: instr_stream = InstructionStream(metadata, 1) 

sage: instr_stream.get_current() 

{'args': 1, 

'code': [], 

'constants': [], 

'domain': None, 

'py_constants': [], 

'stack': 0} 

sage: instr_stream.instr('load_arg', 0) 

sage: instr_stream.instr('py_call', math.sin, 1) 

sage: instr_stream.instr('abs') 

sage: instr_stream.instr('return') 

sage: instr_stream.current_op_list() 

[('load_arg', 0), ('py_call', <built-in function sin>, 1), 'abs', 'return'] 

sage: instr_stream.get_current() 

{'args': 1, 

'code': [0, 0, 3, 0, 1, 12, 2], 

'constants': [], 

'domain': None, 

'py_constants': [<built-in function sin>], 

'stack': 1} 

""" 

d = {'args': self._n_args, 

'constants': self._constants, 

'py_constants': self._py_constants, 

'stack': self._stack_max_size, 

'code': self._bytecode, 

'domain': self._domain} 

return d 

  

cdef class InterpreterMetadata(object): 

r""" 

The interpreter metadata for a fast_callable interpreter. Currently 

consists of a dictionary mapping instruction names to 

(CompilerInstrSpec, opcode) pairs, a list mapping opcodes to 

(instruction name, CompilerInstrSpec) pairs, and a range of exponents 

for which the ipow instruction can be used. This range can be 

False (if the ipow instruction should never be used), a pair of 

two integers (a,b), if ipow should be used for a<=n<=b, or True, 

if ipow should always be used. When ipow cannot be used, then 

we fall back on calling IntegerPowerFunction. 

  

See the class docstring for CompilerInstrSpec for more information. 

  

NOTE: You must not modify the metadata. 

""" 

cdef public dict by_opname 

cdef public list by_opcode 

cdef public ipow_range 

  

def __init__(self, by_opname, by_opcode, ipow_range): 

r""" 

Initialize an InterpreterMetadata object. 

  

EXAMPLES:: 

  

sage: from sage.ext.fast_callable import InterpreterMetadata 

sage: metadata = InterpreterMetadata(by_opname={'opname dict goes here': True}, by_opcode=['opcode list goes here'], ipow_range=(2, 57)) 

sage: metadata.by_opname 

{'opname dict goes here': True} 

sage: metadata.by_opcode 

['opcode list goes here'] 

sage: metadata.ipow_range 

(2, 57) 

""" 

self.by_opname = by_opname 

self.by_opcode = by_opcode 

self.ipow_range = ipow_range 

  

class CompilerInstrSpec(object): 

r""" 

Describes a single instruction to the fast_callable code generator. 

  

An instruction has a number of stack inputs, a number of stack 

outputs, and a parameter list describing extra arguments that 

must be passed to the InstructionStream.instr method (that end up 

as extra words in the code). 

  

The parameter list is a list of strings. Each string is one of 

the following: 

  

- 'args' - The instruction argument refers to an input argument of the 

wrapper class; it is just appended to the code. 

  

- 'constants', 'py_constants' - The instruction argument is a value; the 

value is added to the corresponding list (if it's not already there) and 

the index is appended to the code. 

  

- 'n_inputs', 'n_outputs' - The instruction actually takes a variable 

number of inputs or outputs (the n_inputs and n_outputs attributes of 

this instruction are ignored). The instruction argument specifies the 

number of inputs or outputs (respectively); it is just appended to the 

code. 

""" 

  

def __init__(self, n_inputs, n_outputs, parameters): 

r""" 

Initialize a CompilerInstrSpec. 

  

EXAMPLES:: 

  

sage: from sage.ext.fast_callable import CompilerInstrSpec 

sage: CompilerInstrSpec(0, 1, ['py_constants', 'n_inputs']) 

CompilerInstrSpec(0, 1, ['py_constants', 'n_inputs']) 

""" 

self.n_inputs = n_inputs 

self.n_outputs = n_outputs 

self.parameters = parameters 

  

def __repr__(self): 

r""" 

Give a string representation for this CompilerInstrSpec. 

  

EXAMPLES:: 

  

sage: from sage.ext.fast_callable import CompilerInstrSpec 

sage: v = CompilerInstrSpec(0, 1, ['py_constants', 'n_inputs']) 

sage: v 

CompilerInstrSpec(0, 1, ['py_constants', 'n_inputs']) 

sage: repr(v) 

"CompilerInstrSpec(0, 1, ['py_constants', 'n_inputs'])" 

sage: v.__repr__() 

"CompilerInstrSpec(0, 1, ['py_constants', 'n_inputs'])" 

""" 

return "CompilerInstrSpec(%d, %d, %s)" % (self.n_inputs, self.n_outputs, self.parameters) 

  

def op_list(args, metadata): 

r""" 

Given a dictionary with the result of calling get_current on an 

InstructionStream, and the corresponding interpreter metadata, 

return a list of the instructions, in a simple somewhat 

human-readable format. 

  

For debugging only. (That is, it's probably not a good idea to 

try to programmatically manipulate the result of this function; 

the expected use is just to print the returned list to the 

screen.) 

  

There's probably no reason to call this directly; if you 

have a wrapper object, call op_list on it; if you have an 

InstructionStream object, call current_op_list on it. 

  

EXAMPLES:: 

  

sage: from sage.ext.interpreters.wrapper_rdf import metadata 

sage: from sage.ext.fast_callable import InstructionStream, op_list 

sage: instr_stream = InstructionStream(metadata, 1) 

sage: instr_stream.instr('load_arg', 0) 

sage: instr_stream.instr('abs') 

sage: instr_stream.instr('return') 

sage: instr_stream.current_op_list() 

[('load_arg', 0), 'abs', 'return'] 

sage: op_list(instr_stream.get_current(), metadata) 

[('load_arg', 0), 'abs', 'return'] 

""" 

ops = [] 

code = args['code'] 

while len(code): 

opcode = code[0] 

code = code[1:] 

(opname, instr) = metadata.by_opcode[opcode] 

if len(instr.parameters): 

op = [opname] 

for p in instr.parameters: 

p_loc = code[0] 

code = code[1:] 

if p in ('args', 'code', 'n_inputs', 'n_outputs'): 

op.append(p_loc) 

else: 

op.append(args[p][p_loc]) 

ops.append(tuple(op)) 

else: 

ops.append(opname) 

return ops 

  

  

cdef class Wrapper: 

r""" 

The parent class for all fast_callable wrappers. Implements shared 

behavior (currently only debugging). 

""" 

  

def __init__(self, args, metadata): 

r""" 

Initialize a Wrapper object. 

  

EXAMPLES:: 

  

sage: from sage.ext.fast_callable import ExpressionTreeBuilder, generate_code, InstructionStream 

sage: etb = ExpressionTreeBuilder('x') 

sage: x = etb.var('x') 

sage: expr = ((x+pi)*(x+1)) 

sage: from sage.ext.interpreters.wrapper_py import metadata, Wrapper_py 

sage: instr_stream = InstructionStream(metadata, 1) 

sage: generate_code(expr, instr_stream) 

sage: instr_stream.instr('return') 

sage: v = Wrapper_py(instr_stream.get_current()) 

sage: v.get_orig_args() 

{'args': 1, 

'code': [0, 0, 1, 0, 4, 0, 0, 1, 1, 4, 6, 2], 

'constants': [pi, 1], 

'domain': None, 

'py_constants': [], 

'stack': 3} 

sage: v.op_list() 

[('load_arg', 0), ('load_const', pi), 'add', ('load_arg', 0), ('load_const', 1), 'add', 'mul', 'return'] 

""" 

  

# We only keep the original arguments for debugging (op_list(), etc.); 

# is it worth the memory cost? (Note that we may be holding on to 

# large objects that could otherwise be garbage collected, for 

# instance.) 

self._orig_args = args 

self._metadata = metadata 

  

def get_orig_args(self): 

r""" 

Get the original arguments used when initializing this 

wrapper. 

  

(Probably only useful when writing doctests.) 

  

EXAMPLES:: 

  

sage: fast_callable(sin(x)/x, vars=[x], domain=RDF).get_orig_args() 

{'args': 1, 

'code': [0, 0, 16, 0, 0, 8, 2], 

'constants': [], 

'domain': Real Double Field, 

'py_constants': [], 

'stack': 2} 

""" 

return self._orig_args 

  

def op_list(self): 

r""" 

Return the list of instructions in this wrapper. 

  

EXAMPLES:: 

  

sage: fast_callable(cos(x)*x, vars=[x], domain=RDF).op_list() 

[('load_arg', 0), ('load_arg', 0), 'cos', 'mul', 'return'] 

""" 

return op_list(self._orig_args, self._metadata) 

  

def python_calls(self): 

r""" 

List the Python functions that are called in this wrapper. 

  

(Python function calls are slow, so ideally this list would 

be empty. If it is not empty, then perhaps there is an 

optimization opportunity where a Sage developer could speed 

this up by adding a new instruction to the interpreter.) 

  

EXAMPLES:: 

  

sage: fast_callable(abs(sin(x)), vars=[x], domain=RDF).python_calls() 

[] 

sage: fast_callable(abs(sin(factorial(x))), vars=[x]).python_calls() 

[factorial, sin] 

""" 

ops = self.op_list() 

py_calls = [] 

for op in ops: 

if isinstance(op, tuple) and op[0] == 'py_call': 

py_calls.append(op[1]) 

return py_calls