Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

645

646

647

648

649

650

651

652

653

654

655

656

657

658

659

660

661

662

663

664

665

666

667

668

669

670

671

672

673

674

675

676

677

678

679

680

681

682

683

684

685

686

687

688

689

690

691

692

693

694

695

696

697

698

699

700

701

702

703

704

705

706

707

708

709

710

711

712

713

714

715

716

717

718

719

720

721

722

723

724

725

726

727

728

729

730

731

732

733

734

735

736

737

738

739

740

741

742

743

744

745

746

747

748

749

750

751

752

753

754

755

756

757

758

759

760

761

762

763

764

765

766

767

768

769

770

771

772

773

774

775

776

777

778

779

780

781

782

783

784

785

786

787

788

789

790

791

792

793

794

795

796

797

798

799

800

801

802

803

804

805

806

807

808

809

810

811

812

813

814

815

816

817

818

819

820

821

822

823

824

825

826

827

828

829

830

831

832

833

834

835

836

837

838

839

840

841

842

843

844

845

846

847

848

849

850

851

852

853

854

855

856

857

858

859

860

861

862

863

864

865

866

867

868

869

870

871

872

873

874

875

876

877

878

879

880

881

882

883

884

885

886

887

888

889

890

891

892

893

894

895

896

897

898

899

900

901

902

903

904

905

906

907

908

909

910

911

912

913

914

915

916

917

918

919

920

921

922

923

924

925

926

927

928

929

930

931

932

933

934

935

936

937

938

939

940

941

942

943

944

945

946

947

948

949

950

951

952

953

954

955

956

957

958

959

960

961

962

963

964

965

966

967

968

969

970

971

972

973

974

975

976

977

978

979

980

981

982

983

984

985

986

987

988

989

990

991

992

993

994

995

996

997

998

999

1000

1001

1002

1003

1004

1005

1006

1007

1008

1009

1010

1011

1012

1013

1014

1015

1016

1017

1018

1019

1020

1021

1022

1023

1024

1025

1026

1027

1028

1029

1030

1031

1032

1033

1034

1035

1036

1037

1038

1039

1040

1041

1042

1043

1044

1045

1046

1047

1048

1049

1050

1051

1052

1053

1054

1055

1056

1057

1058

1059

1060

1061

1062

1063

1064

1065

1066

1067

1068

1069

1070

1071

1072

1073

1074

1075

1076

1077

1078

1079

1080

1081

1082

1083

1084

1085

1086

1087

1088

1089

1090

1091

1092

1093

1094

1095

1096

1097

1098

1099

1100

1101

1102

1103

1104

1105

1106

1107

1108

1109

1110

1111

1112

1113

1114

1115

1116

1117

1118

1119

1120

1121

1122

1123

1124

1125

1126

1127

1128

1129

1130

1131

1132

1133

1134

1135

1136

1137

1138

1139

1140

1141

1142

1143

1144

1145

1146

1147

1148

1149

1150

1151

1152

1153

1154

1155

1156

1157

1158

1159

1160

1161

1162

1163

1164

1165

1166

1167

1168

1169

1170

1171

1172

1173

1174

1175

1176

1177

1178

1179

1180

1181

1182

1183

1184

1185

1186

1187

1188

1189

1190

1191

1192

1193

1194

1195

1196

1197

1198

1199

1200

1201

1202

1203

1204

1205

1206

1207

1208

1209

1210

1211

1212

1213

1214

1215

1216

1217

1218

1219

1220

1221

1222

1223

1224

1225

1226

1227

1228

1229

1230

1231

1232

1233

1234

1235

1236

1237

1238

1239

1240

1241

1242

1243

1244

1245

1246

1247

1248

1249

1250

1251

1252

1253

1254

1255

1256

1257

1258

1259

1260

1261

1262

1263

1264

1265

1266

1267

1268

1269

1270

1271

1272

1273

1274

1275

1276

1277

1278

1279

1280

1281

1282

1283

1284

1285

1286

1287

1288

1289

1290

1291

1292

1293

1294

1295

1296

1297

1298

1299

1300

1301

1302

1303

1304

1305

1306

1307

1308

1309

1310

1311

1312

1313

1314

1315

1316

1317

1318

1319

1320

1321

1322

1323

1324

1325

1326

1327

1328

1329

1330

1331

1332

1333

1334

1335

1336

1337

1338

1339

1340

1341

1342

1343

1344

1345

1346

1347

1348

1349

1350

1351

1352

1353

1354

1355

1356

1357

1358

1359

1360

1361

1362

1363

1364

1365

1366

1367

1368

1369

1370

1371

1372

1373

1374

1375

1376

1377

1378

1379

1380

1381

1382

1383

1384

1385

1386

1387

1388

1389

1390

1391

1392

1393

1394

1395

1396

1397

1398

1399

1400

1401

1402

1403

1404

1405

1406

1407

1408

1409

1410

1411

1412

1413

1414

1415

1416

1417

1418

1419

1420

1421

1422

1423

1424

1425

1426

1427

1428

1429

1430

1431

1432

1433

1434

1435

1436

1437

1438

1439

1440

1441

1442

1443

1444

1445

1446

1447

1448

1449

1450

1451

1452

1453

1454

1455

1456

1457

1458

1459

1460

1461

1462

1463

1464

1465

1466

1467

1468

1469

1470

1471

1472

1473

1474

1475

1476

1477

1478

1479

1480

1481

1482

1483

1484

1485

1486

1487

1488

1489

1490

1491

1492

1493

1494

1495

1496

1497

1498

1499

1500

1501

1502

1503

1504

1505

1506

1507

1508

1509

1510

1511

1512

1513

1514

1515

1516

1517

1518

1519

1520

1521

1522

1523

1524

1525

1526

1527

1528

1529

1530

1531

1532

1533

1534

1535

1536

1537

1538

1539

1540

1541

1542

1543

1544

1545

1546

1547

1548

1549

1550

1551

1552

1553

1554

1555

1556

1557

1558

1559

1560

1561

1562

1563

1564

1565

1566

1567

1568

1569

1570

1571

1572

1573

1574

1575

1576

1577

1578

1579

1580

1581

1582

1583

1584

1585

1586

1587

1588

1589

1590

1591

1592

1593

1594

1595

1596

1597

1598

1599

1600

1601

1602

1603

1604

1605

1606

1607

1608

1609

1610

1611

1612

1613

1614

1615

1616

1617

1618

1619

1620

1621

1622

1623

1624

1625

1626

1627

1628

1629

1630

1631

1632

1633

1634

1635

1636

1637

1638

1639

1640

1641

1642

1643

1644

1645

1646

1647

1648

1649

1650

1651

1652

1653

1654

1655

1656

1657

1658

1659

1660

1661

1662

1663

1664

1665

1666

1667

1668

1669

1670

1671

1672

1673

1674

1675

1676

1677

1678

1679

1680

1681

1682

1683

1684

1685

1686

1687

1688

1689

1690

1691

1692

1693

1694

1695

1696

1697

1698

1699

1700

1701

1702

1703

1704

1705

1706

1707

1708

1709

1710

1711

1712

1713

1714

1715

1716

1717

1718

1719

1720

1721

1722

1723

1724

1725

1726

1727

1728

1729

1730

1731

1732

1733

1734

1735

1736

1737

1738

1739

1740

1741

1742

1743

1744

1745

1746

1747

1748

1749

1750

1751

1752

1753

1754

1755

1756

1757

1758

1759

1760

1761

1762

1763

1764

1765

1766

1767

1768

1769

1770

1771

1772

1773

1774

1775

1776

1777

1778

1779

1780

1781

1782

1783

1784

1785

1786

1787

1788

1789

1790

1791

1792

1793

1794

1795

1796

1797

1798

1799

1800

1801

1802

1803

1804

1805

1806

1807

1808

1809

1810

1811

1812

1813

1814

1815

1816

1817

1818

1819

1820

1821

1822

1823

1824

1825

1826

1827

1828

1829

1830

1831

1832

1833

1834

1835

1836

1837

1838

1839

1840

1841

1842

1843

1844

1845

1846

1847

1848

1849

1850

1851

1852

1853

1854

1855

1856

1857

1858

1859

1860

1861

1862

1863

1864

1865

1866

1867

1868

1869

1870

1871

1872

1873

1874

1875

1876

1877

1878

1879

1880

1881

1882

1883

1884

1885

1886

1887

1888

1889

1890

1891

1892

1893

1894

1895

1896

1897

1898

1899

1900

1901

1902

1903

1904

1905

1906

1907

1908

1909

1910

1911

1912

1913

1914

1915

1916

1917

1918

1919

1920

1921

1922

1923

1924

1925

1926

1927

1928

1929

1930

1931

1932

1933

1934

1935

1936

1937

1938

1939

1940

1941

1942

1943

1944

1945

1946

1947

1948

1949

1950

1951

1952

1953

1954

1955

1956

1957

1958

1959

1960

1961

1962

1963

1964

1965

1966

1967

1968

1969

1970

1971

1972

1973

1974

1975

1976

1977

1978

1979

1980

1981

1982

1983

1984

1985

1986

1987

1988

1989

1990

1991

1992

1993

1994

1995

1996

1997

1998

1999

2000

2001

2002

2003

2004

2005

2006

2007

2008

2009

2010

2011

2012

2013

2014

2015

2016

2017

2018

2019

2020

2021

2022

2023

2024

2025

2026

2027

2028

2029

2030

2031

2032

2033

2034

2035

2036

2037

2038

2039

2040

2041

2042

2043

2044

2045

2046

2047

2048

2049

2050

2051

2052

2053

2054

2055

2056

2057

2058

2059

2060

2061

2062

2063

2064

2065

2066

2067

2068

2069

2070

2071

2072

2073

2074

2075

2076

2077

2078

2079

2080

2081

2082

2083

2084

2085

2086

2087

2088

2089

2090

2091

2092

2093

2094

2095

2096

2097

2098

2099

2100

2101

2102

2103

2104

2105

2106

2107

2108

2109

2110

2111

2112

2113

2114

2115

2116

2117

2118

2119

2120

2121

2122

2123

2124

2125

2126

2127

2128

2129

2130

2131

2132

2133

2134

2135

2136

2137

2138

2139

2140

2141

2142

2143

2144

2145

2146

2147

2148

2149

2150

2151

2152

2153

2154

2155

2156

2157

2158

2159

2160

2161

2162

2163

2164

2165

2166

2167

2168

2169

2170

2171

2172

2173

2174

2175

2176

2177

2178

2179

2180

2181

2182

2183

2184

2185

2186

2187

2188

2189

2190

2191

2192

2193

2194

2195

2196

2197

2198

2199

2200

2201

2202

2203

2204

2205

2206

2207

2208

2209

2210

2211

2212

2213

2214

2215

2216

2217

2218

2219

2220

2221

2222

2223

2224

2225

2226

2227

2228

2229

2230

2231

2232

2233

2234

2235

2236

2237

2238

2239

2240

2241

2242

2243

2244

2245

2246

2247

2248

2249

2250

2251

2252

2253

2254

2255

2256

2257

2258

2259

2260

2261

2262

2263

2264

2265

2266

2267

2268

2269

2270

2271

2272

2273

2274

2275

2276

2277

2278

2279

2280

2281

2282

2283

2284

2285

2286

2287

2288

2289

2290

2291

2292

2293

2294

2295

2296

2297

2298

2299

2300

2301

2302

2303

2304

2305

2306

2307

2308

2309

2310

2311

2312

2313

2314

2315

2316

2317

2318

2319

2320

2321

2322

2323

2324

2325

2326

2327

2328

2329

2330

2331

2332

2333

2334

2335

2336

2337

2338

2339

2340

2341

2342

2343

2344

2345

2346

2347

2348

2349

2350

2351

2352

2353

2354

2355

2356

2357

2358

2359

2360

2361

2362

2363

2364

2365

2366

2367

2368

2369

2370

2371

2372

2373

2374

2375

2376

2377

2378

2379

2380

2381

2382

2383

2384

2385

2386

2387

2388

2389

2390

2391

2392

2393

2394

2395

2396

2397

2398

2399

2400

2401

2402

2403

2404

2405

2406

2407

2408

2409

2410

2411

2412

2413

2414

2415

2416

2417

2418

2419

2420

2421

2422

2423

2424

2425

2426

2427

2428

2429

2430

2431

2432

2433

2434

2435

2436

2437

2438

2439

2440

2441

2442

2443

2444

2445

2446

2447

2448

2449

2450

2451

2452

2453

2454

2455

2456

2457

2458

2459

2460

2461

2462

2463

2464

2465

2466

2467

2468

2469

2470

2471

2472

2473

2474

2475

2476

2477

2478

2479

2480

2481

2482

2483

2484

2485

2486

2487

2488

2489

2490

2491

2492

2493

2494

2495

2496

2497

2498

2499

2500

2501

2502

2503

2504

2505

2506

2507

2508

2509

2510

2511

2512

2513

2514

2515

2516

2517

2518

2519

2520

2521

2522

2523

2524

2525

2526

2527

2528

2529

2530

2531

2532

2533

2534

2535

2536

2537

2538

2539

2540

2541

2542

2543

2544

2545

2546

2547

2548

2549

2550

2551

2552

2553

2554

2555

2556

2557

2558

2559

2560

2561

2562

2563

2564

2565

2566

2567

2568

2569

2570

2571

2572

2573

2574

2575

2576

2577

2578

2579

2580

2581

2582

2583

2584

2585

2586

2587

2588

2589

2590

2591

2592

2593

2594

2595

2596

2597

2598

2599

2600

2601

2602

2603

2604

2605

2606

2607

2608

2609

2610

2611

2612

2613

2614

2615

2616

2617

2618

2619

2620

2621

2622

2623

2624

2625

2626

2627

2628

2629

2630

2631

2632

2633

2634

2635

2636

2637

2638

2639

2640

2641

2642

2643

2644

2645

2646

2647

2648

2649

2650

2651

2652

2653

2654

2655

2656

2657

2658

2659

2660

2661

2662

2663

2664

2665

2666

2667

2668

2669

2670

2671

2672

2673

2674

2675

2676

2677

2678

2679

2680

2681

2682

2683

2684

2685

2686

2687

2688

2689

2690

2691

2692

2693

2694

2695

2696

2697

2698

2699

2700

2701

2702

2703

2704

2705

2706

2707

2708

2709

2710

2711

2712

2713

2714

2715

2716

2717

2718

2719

2720

2721

2722

2723

2724

2725

2726

2727

2728

2729

2730

2731

2732

2733

2734

2735

2736

2737

2738

2739

2740

2741

2742

2743

2744

2745

2746

2747

2748

2749

2750

2751

2752

2753

2754

2755

2756

2757

2758

2759

2760

2761

2762

2763

2764

2765

2766

2767

2768

2769

2770

2771

2772

2773

2774

2775

2776

2777

2778

2779

2780

2781

2782

2783

2784

2785

2786

2787

2788

2789

2790

2791

2792

2793

2794

2795

2796

2797

2798

2799

2800

2801

2802

2803

2804

2805

2806

2807

2808

2809

2810

2811

2812

2813

2814

2815

2816

2817

2818

2819

2820

2821

2822

2823

2824

2825

2826

2827

2828

2829

2830

2831

2832

2833

2834

2835

2836

2837

2838

2839

2840

2841

2842

2843

2844

2845

2846

2847

2848

2849

2850

2851

2852

2853

2854

2855

2856

2857

2858

2859

2860

2861

2862

2863

2864

2865

2866

2867

2868

2869

2870

2871

2872

2873

2874

2875

2876

2877

2878

2879

2880

2881

2882

2883

2884

2885

2886

2887

2888

2889

2890

2891

2892

2893

2894

2895

2896

2897

2898

2899

2900

2901

2902

2903

2904

2905

2906

2907

2908

2909

2910

2911

2912

2913

2914

2915

2916

2917

2918

2919

2920

2921

2922

2923

2924

2925

2926

2927

2928

2929

2930

2931

2932

2933

2934

2935

2936

2937

2938

2939

2940

2941

2942

2943

2944

2945

2946

2947

2948

2949

2950

2951

2952

2953

2954

2955

2956

2957

2958

2959

2960

2961

2962

2963

2964

2965

2966

2967

2968

2969

2970

2971

2972

2973

2974

2975

2976

2977

2978

2979

2980

2981

2982

2983

2984

2985

2986

2987

2988

2989

2990

2991

2992

2993

2994

2995

2996

2997

2998

2999

3000

3001

3002

3003

3004

3005

3006

3007

3008

3009

3010

3011

3012

3013

3014

3015

3016

3017

3018

3019

3020

3021

3022

3023

3024

3025

3026

3027

3028

3029

3030

3031

3032

3033

3034

3035

3036

3037

3038

3039

3040

3041

3042

3043

3044

3045

3046

3047

3048

3049

3050

3051

3052

3053

3054

3055

3056

3057

3058

3059

3060

3061

3062

3063

3064

3065

3066

3067

3068

3069

3070

3071

3072

3073

3074

3075

3076

3077

3078

3079

3080

3081

3082

3083

3084

3085

3086

3087

3088

3089

3090

3091

3092

3093

3094

3095

3096

3097

3098

3099

3100

3101

3102

3103

3104

3105

3106

3107

3108

3109

3110

3111

3112

3113

3114

3115

3116

3117

3118

3119

3120

3121

3122

3123

3124

3125

3126

3127

3128

3129

3130

3131

3132

3133

3134

3135

3136

3137

3138

3139

3140

3141

3142

3143

3144

3145

3146

3147

3148

3149

3150

3151

3152

3153

3154

3155

3156

3157

3158

3159

3160

3161

3162

3163

3164

3165

3166

3167

3168

3169

3170

3171

3172

3173

3174

3175

3176

3177

3178

3179

3180

3181

3182

3183

3184

3185

3186

3187

3188

3189

3190

3191

3192

3193

3194

3195

3196

3197

3198

3199

3200

3201

3202

3203

3204

3205

3206

3207

3208

3209

3210

3211

3212

3213

3214

3215

3216

3217

3218

3219

3220

3221

3222

3223

3224

3225

3226

3227

3228

3229

3230

3231

3232

3233

3234

3235

3236

3237

3238

3239

3240

3241

3242

3243

3244

3245

3246

3247

3248

3249

3250

3251

3252

3253

3254

3255

3256

3257

3258

3259

3260

3261

3262

3263

3264

3265

3266

3267

3268

3269

3270

3271

3272

3273

3274

3275

3276

3277

3278

3279

3280

3281

3282

3283

3284

3285

3286

3287

3288

3289

3290

3291

3292

3293

3294

3295

3296

3297

3298

3299

3300

3301

3302

3303

3304

3305

3306

3307

3308

3309

3310

3311

3312

3313

3314

3315

3316

3317

3318

3319

3320

3321

3322

3323

3324

3325

3326

3327

3328

3329

3330

3331

3332

3333

3334

3335

3336

3337

3338

3339

3340

3341

3342

3343

3344

3345

3346

3347

3348

3349

3350

3351

3352

3353

3354

3355

3356

3357

3358

3359

3360

3361

3362

3363

3364

3365

3366

3367

3368

3369

3370

3371

3372

3373

3374

3375

3376

3377

3378

3379

3380

3381

3382

3383

3384

3385

3386

3387

3388

3389

3390

3391

3392

3393

3394

3395

3396

3397

3398

3399

3400

3401

3402

3403

3404

3405

3406

3407

3408

3409

3410

3411

3412

3413

3414

3415

3416

3417

3418

3419

3420

3421

3422

3423

3424

3425

3426

3427

3428

3429

3430

3431

3432

3433

3434

3435

3436

3437

3438

3439

3440

3441

3442

3443

3444

3445

3446

3447

3448

3449

3450

3451

3452

3453

3454

3455

3456

3457

3458

3459

3460

3461

3462

3463

3464

3465

3466

3467

3468

3469

3470

3471

3472

3473

3474

3475

3476

3477

3478

3479

3480

3481

3482

3483

3484

3485

3486

3487

3488

3489

3490

3491

3492

3493

3494

3495

3496

3497

3498

3499

3500

3501

3502

3503

3504

3505

3506

3507

3508

3509

3510

3511

3512

3513

3514

3515

3516

3517

3518

3519

3520

3521

3522

3523

3524

3525

3526

3527

3528

3529

3530

3531

3532

3533

3534

3535

3536

3537

3538

3539

3540

3541

3542

3543

3544

3545

3546

3547

3548

3549

3550

3551

3552

3553

3554

3555

3556

3557

3558

3559

3560

3561

3562

3563

3564

3565

3566

3567

3568

3569

3570

3571

3572

3573

3574

3575

3576

3577

3578

3579

3580

3581

3582

3583

3584

3585

3586

3587

3588

3589

3590

3591

3592

3593

3594

3595

3596

3597

3598

3599

3600

3601

3602

3603

3604

3605

3606

3607

3608

3609

3610

3611

3612

3613

3614

3615

3616

3617

3618

3619

3620

3621

3622

3623

3624

3625

3626

3627

3628

3629

3630

3631

3632

3633

3634

3635

3636

3637

3638

3639

3640

3641

3642

3643

3644

3645

3646

3647

3648

3649

3650

3651

3652

3653

3654

3655

3656

3657

3658

3659

3660

3661

3662

3663

3664

3665

3666

3667

3668

3669

3670

3671

3672

3673

3674

3675

3676

3677

3678

3679

3680

3681

3682

3683

3684

3685

3686

3687

3688

3689

3690

3691

3692

3693

3694

3695

3696

3697

3698

3699

3700

3701

3702

3703

3704

3705

3706

3707

3708

3709

3710

3711

3712

3713

3714

3715

3716

3717

3718

3719

3720

3721

3722

3723

3724

3725

3726

3727

3728

3729

3730

3731

3732

3733

3734

3735

3736

3737

3738

3739

3740

3741

3742

3743

3744

3745

3746

3747

3748

3749

3750

3751

3752

3753

3754

3755

3756

3757

3758

3759

3760

3761

3762

3763

3764

3765

3766

3767

3768

3769

3770

3771

3772

3773

3774

3775

3776

3777

3778

3779

3780

3781

3782

3783

3784

3785

3786

3787

3788

3789

3790

3791

3792

3793

3794

3795

3796

3797

3798

3799

3800

3801

3802

3803

3804

3805

3806

3807

3808

3809

3810

3811

3812

3813

3814

3815

3816

3817

3818

3819

3820

3821

3822

3823

3824

3825

3826

3827

3828

3829

3830

3831

3832

3833

3834

3835

3836

3837

3838

3839

3840

3841

3842

3843

3844

3845

3846

3847

3848

3849

3850

3851

3852

3853

3854

3855

3856

3857

3858

3859

3860

3861

3862

3863

3864

3865

3866

3867

3868

3869

3870

3871

3872

3873

3874

3875

3876

3877

3878

3879

3880

3881

3882

3883

3884

3885

3886

3887

3888

3889

3890

3891

3892

3893

3894

3895

3896

3897

3898

3899

3900

3901

3902

3903

3904

3905

3906

3907

3908

3909

3910

3911

3912

3913

3914

3915

3916

3917

3918

3919

3920

3921

3922

3923

3924

3925

3926

3927

3928

3929

3930

3931

3932

3933

3934

3935

3936

3937

3938

3939

3940

3941

3942

3943

3944

3945

3946

3947

3948

3949

3950

3951

3952

3953

3954

3955

3956

3957

3958

3959

3960

3961

3962

3963

3964

3965

3966

3967

3968

3969

3970

3971

3972

3973

3974

3975

3976

3977

3978

3979

3980

3981

3982

3983

3984

3985

3986

3987

3988

3989

3990

3991

3992

3993

3994

3995

3996

3997

3998

3999

4000

4001

4002

4003

4004

4005

4006

4007

4008

4009

4010

4011

4012

4013

4014

4015

4016

4017

4018

4019

4020

4021

4022

4023

4024

4025

4026

4027

4028

4029

4030

4031

4032

4033

4034

4035

4036

4037

4038

4039

4040

4041

4042

4043

4044

4045

4046

4047

4048

4049

4050

4051

4052

4053

4054

4055

4056

4057

4058

4059

4060

4061

4062

4063

4064

4065

4066

4067

4068

4069

4070

4071

4072

4073

4074

4075

4076

4077

4078

4079

4080

4081

4082

4083

4084

4085

4086

4087

4088

4089

4090

4091

4092

4093

4094

4095

4096

4097

4098

4099

4100

4101

4102

4103

4104

4105

4106

4107

4108

4109

4110

4111

4112

4113

4114

4115

4116

4117

4118

4119

4120

4121

4122

4123

4124

4125

4126

4127

4128

4129

4130

4131

4132

4133

4134

4135

4136

4137

4138

4139

4140

4141

4142

4143

4144

4145

4146

4147

4148

4149

4150

4151

4152

4153

4154

4155

4156

4157

4158

4159

4160

4161

4162

4163

4164

4165

4166

4167

4168

4169

4170

4171

4172

4173

4174

4175

4176

4177

4178

4179

4180

4181

4182

4183

4184

4185

4186

4187

4188

4189

4190

4191

4192

4193

4194

4195

4196

4197

4198

4199

4200

4201

4202

4203

4204

4205

4206

4207

4208

4209

4210

4211

4212

4213

4214

4215

4216

4217

4218

4219

4220

4221

4222

4223

4224

4225

4226

4227

4228

4229

4230

4231

4232

4233

4234

4235

4236

4237

4238

4239

4240

4241

4242

4243

4244

4245

4246

4247

4248

4249

4250

4251

4252

4253

4254

4255

4256

4257

4258

4259

4260

4261

4262

4263

4264

4265

4266

4267

4268

4269

4270

4271

4272

4273

4274

4275

4276

4277

4278

4279

4280

4281

4282

4283

4284

4285

4286

4287

4288

4289

4290

4291

4292

4293

4294

4295

4296

4297

4298

4299

4300

4301

4302

4303

4304

4305

4306

4307

4308

4309

4310

4311

4312

4313

4314

4315

4316

4317

4318

4319

4320

4321

4322

4323

4324

4325

4326

4327

4328

4329

4330

4331

4332

4333

4334

4335

4336

4337

4338

4339

4340

4341

4342

4343

4344

4345

4346

4347

4348

4349

4350

4351

4352

4353

4354

4355

4356

4357

4358

4359

4360

4361

4362

4363

4364

4365

4366

4367

4368

4369

4370

4371

4372

4373

4374

4375

4376

4377

4378

4379

4380

4381

4382

4383

4384

4385

4386

4387

4388

4389

4390

4391

4392

4393

4394

4395

4396

4397

4398

4399

4400

4401

4402

4403

4404

4405

4406

4407

4408

4409

4410

4411

4412

4413

4414

4415

4416

4417

4418

4419

4420

4421

4422

4423

4424

4425

4426

4427

4428

4429

4430

4431

4432

4433

4434

4435

4436

4437

4438

4439

4440

4441

4442

4443

4444

4445

4446

4447

4448

4449

4450

4451

4452

4453

4454

4455

4456

4457

4458

4459

4460

4461

4462

4463

4464

4465

4466

4467

4468

4469

4470

4471

4472

4473

4474

4475

4476

4477

4478

4479

4480

4481

4482

4483

4484

4485

4486

4487

4488

4489

4490

4491

4492

4493

4494

4495

4496

4497

4498

4499

4500

4501

4502

4503

4504

4505

4506

4507

4508

4509

4510

4511

4512

4513

4514

4515

4516

4517

4518

4519

4520

4521

4522

4523

4524

4525

4526

4527

4528

4529

4530

4531

4532

4533

4534

4535

4536

4537

4538

4539

4540

4541

4542

4543

4544

4545

4546

4547

4548

4549

4550

4551

4552

4553

4554

4555

4556

4557

4558

4559

4560

4561

4562

4563

4564

4565

4566

4567

4568

4569

4570

4571

4572

4573

4574

4575

4576

4577

4578

4579

4580

4581

4582

4583

4584

4585

4586

4587

4588

4589

4590

4591

4592

4593

4594

4595

4596

4597

4598

4599

4600

4601

4602

4603

4604

4605

4606

4607

4608

4609

4610

4611

4612

4613

4614

4615

4616

4617

4618

4619

4620

4621

4622

4623

4624

4625

4626

4627

4628

4629

4630

4631

4632

4633

4634

4635

4636

4637

4638

4639

4640

4641

4642

4643

4644

4645

4646

4647

4648

4649

4650

4651

4652

4653

4654

4655

4656

4657

4658

4659

4660

4661

4662

4663

4664

4665

4666

4667

4668

4669

4670

4671

4672

4673

4674

4675

4676

4677

4678

4679

4680

4681

4682

4683

4684

4685

4686

4687

4688

4689

4690

4691

4692

4693

4694

4695

4696

4697

4698

4699

4700

4701

4702

4703

4704

4705

4706

r""" 

Asymptotic Ring 

 

This module provides a ring (called :class:`AsymptoticRing`) for 

computations with :wikipedia:`asymptotic expansions <Asymptotic_expansion>`. 

 

 

.. _asymptotic_ring_definition: 

 

(Informal) Definition 

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

 

An asymptotic expansion is a sum such as 

 

.. MATH:: 

 

5z^3 + 4z^2 + O(z) 

 

as `z \to \infty` or 

 

.. MATH:: 

 

3x^{42}y^2 + 7x^3y^3 + O(x^2) + O(y) 

 

as `x` and `y` tend to `\infty`. It is a truncated series (after a 

finite number of terms), which approximates a function. 

 

The summands of the asymptotic expansions are partially ordered. In 

this module these summands are the following: 

 

- Exact terms `c\cdot g` with a coefficient `c` and an element `g` of 

a growth group (:ref:`see below <asymptotic_ring_growth>`). 

 

- `O`-terms `O(g)` (see :wikipedia:`Big O notation <Big_O_notation>`; 

also called *Bachmann--Landau notation*) for a growth group 

element `g` (:ref:`again see below <asymptotic_ring_growth>`). 

 

See 

:wikipedia:`the Wikipedia article on asymptotic expansions <Asymptotic_expansion>` 

for more details. 

Further examples of such elements can be found :ref:`here <asymptotic_ring_intro>`. 

 

 

.. _asymptotic_ring_growth: 

 

Growth Groups and Elements 

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

 

The elements of a :doc:`growth group <growth_group>` are equipped with 

a partial order and usually contain a variable. Examples---the order 

is described below these examples---are 

 

- elements of the form `z^q` for some integer or rational `q` 

(growth groups with :ref:`description strings <growth_group_description>` 

``z^ZZ`` or ``z^QQ``), 

 

- elements of the form `\log(z)^q` for some integer or rational `q` 

(growth groups ``log(z)^ZZ`` or ``log(z)^QQ``), 

 

- elements of the form `a^z` for some 

rational `a` (growth group ``QQ^z``), or 

 

- more sophisticated constructions like products 

`x^r \cdot \log(x)^s \cdot a^y \cdot y^q` 

(this corresponds to an element of the growth group 

``x^QQ * log(x)^ZZ * QQ^y * y^QQ``). 

 

The order in all these examples is induced by the magnitude of the 

elements as `x`, `y`, or `z` (independently) tend to `\infty`. For 

elements only using the variable `z` this means that `g_1 \leq g_2` if 

 

.. MATH:: 

 

\lim_{z\to\infty} \frac{g_1}{g_2} \leq 1. 

 

.. NOTE:: 

 

Asymptotic rings where the variable tend to some value distinct from 

`\infty` are not yet implemented. 

 

To find out more about 

 

- growth groups, 

 

- on how they are created and 

 

- about the above used *descriptions strings* 

 

see the top of the module :doc:`growth group <growth_group>`. 

 

 

.. _asymptotic_ring_intro: 

 

Introductory Examples 

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

 

We start this series of examples by defining two asymptotic rings. 

 

 

Two Rings 

--------- 

 

A Univariate Asymptotic Ring 

^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 

 

First, we construct the following (very simple) asymptotic ring in the variable `z`:: 

 

sage: A.<z> = AsymptoticRing(growth_group='z^QQ', coefficient_ring=ZZ); A 

Asymptotic Ring <z^QQ> over Integer Ring 

 

A typical element of this ring is 

:: 

 

sage: A.an_element() 

z^(3/2) + O(z^(1/2)) 

 

This element consists of two summands: the exact term with coefficient 

`1` and growth `z^{3/2}` and the `O`-term `O(z^{1/2})`. Note that the 

growth of `z^{3/2}` is larger than the growth of `z^{1/2}` as 

`z\to\infty`, thus this expansion cannot be simplified (which would 

be done automatically, see below). 

 

Elements can be constructed via the generator `z` and the function 

:func:`~sage.rings.big_oh.O`, for example 

 

:: 

 

sage: 4*z^2 + O(z) 

4*z^2 + O(z) 

 

A Multivariate Asymptotic Ring 

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 

 

Next, we construct a more sophisticated asymptotic ring in the 

variables `x` and `y` by 

:: 

 

sage: B.<x, y> = AsymptoticRing(growth_group='x^QQ * log(x)^ZZ * QQ^y * y^QQ', coefficient_ring=QQ); B 

Asymptotic Ring <x^QQ * log(x)^ZZ * QQ^y * y^QQ> over Rational Field 

 

Again, we can look at a typical (nontrivial) element:: 

 

sage: B.an_element() 

1/8*x^(3/2)*log(x)^3*(1/8)^y*y^(3/2) + O(x^(1/2)*log(x)*(1/2)^y*y^(1/2)) 

 

Again, elements can be created using the generators `x` and `y`, as well as 

the function :func:`~sage.rings.big_oh.O`:: 

 

sage: log(x)*y/42 + O(1/2^y) 

1/42*log(x)*y + O((1/2)^y) 

 

Arithmetical Operations 

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

 

In this section we explain how to perform various arithmetical 

operations with the elements of the asymptotic rings constructed 

above. 

 

 

The Ring Operations Plus and Times 

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 

 

We start our calculations in the ring 

:: 

 

sage: A 

Asymptotic Ring <z^QQ> over Integer Ring 

 

Of course, we can perform the usual ring operations `+` and `*`:: 

 

sage: z^2 + 3*z*(1-z) 

-2*z^2 + 3*z 

sage: (3*z + 2)^3 

27*z^3 + 54*z^2 + 36*z + 8 

 

In addition to that, special powers---our growth group ``z^QQ`` allows 

the exponents to be out of `\QQ`---can also be computed:: 

 

sage: (z^(5/2)+z^(1/7)) * z^(-1/5) 

z^(23/10) + z^(-2/35) 

 

The central concepts of computations with asymptotic expansions is 

that the `O`-notation can be used. For example, we have 

:: 

 

sage: z^3 + z^2 + z + O(z^2) 

z^3 + O(z^2) 

 

where the result is simplified automatically. A more sophisticated example is 

:: 

 

sage: (z+2*z^2+3*z^3+4*z^4) * (O(z)+z^2) 

4*z^6 + O(z^5) 

 

 

Division 

^^^^^^^^ 

 

The asymptotic expansions support division. For example, we can 

expand `1/(z-1)` to a geometric series:: 

 

sage: 1 / (z-1) 

z^(-1) + z^(-2) + z^(-3) + z^(-4) + ... + z^(-20) + O(z^(-21)) 

 

A default precision (parameter ``default_prec`` of 

:class:`AsymptoticRing`) is predefined. Thus, only the first `20` 

summands are calculated. However, if we only want the first `5` exact 

terms, we cut of the rest by using 

:: 

 

sage: (1 / (z-1)).truncate(5) 

z^(-1) + z^(-2) + z^(-3) + z^(-4) + z^(-5) + O(z^(-6)) 

 

or 

 

:: 

 

sage: 1 / (z-1) + O(z^(-6)) 

z^(-1) + z^(-2) + z^(-3) + z^(-4) + z^(-5) + O(z^(-6)) 

 

Of course, we can work with more complicated expansions as well:: 

 

sage: (4*z+1) / (z^3+z^2+z+O(z^0)) 

4*z^(-2) - 3*z^(-3) - z^(-4) + O(z^(-5)) 

 

Not all elements are invertible, for instance, 

 

:: 

 

sage: 1 / O(z) 

Traceback (most recent call last): 

... 

ZeroDivisionError: Cannot invert O(z). 

 

is not invertible, since it includes `0`. 

 

 

Powers, Expontials and Logarithms 

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 

 

It works as simple as it can be; just use the usual operators ``^``, 

``exp`` and ``log``. For example, we obtain the usual series expansion 

of the logarithm 

:: 

 

sage: -log(1-1/z) 

z^(-1) + 1/2*z^(-2) + 1/3*z^(-3) + ... + O(z^(-21)) 

 

as `z \to \infty`. 

 

Similarly, we can apply the exponential function of an asymptotic expansion:: 

 

sage: exp(1/z) 

1 + z^(-1) + 1/2*z^(-2) + 1/6*z^(-3) + 1/24*z^(-4) + ... + O(z^(-20)) 

 

Arbitrary powers work as well; for example, we have 

:: 

 

sage: (1 + 1/z + O(1/z^5))^(1 + 1/z) 

1 + z^(-1) + z^(-2) + 1/2*z^(-3) + 1/3*z^(-4) + O(z^(-5)) 

 

.. NOTE:: 

 

In the asymptotic ring 

:: 

 

sage: M.<n> = AsymptoticRing(growth_group='QQ^n * n^QQ', coefficient_ring=ZZ) 

 

the operation 

:: 

 

sage: (1/2)^n 

Traceback (most recent call last): 

... 

ValueError: 1/2 is not in Exact Term Monoid QQ^n * n^QQ 

with coefficients in Integer Ring. ... 

 

fails, since the rational `1/2` is not contained in `M`. You can use 

:: 

 

sage: n.rpow(1/2) 

(1/2)^n 

 

instead. (See also the examples in 

:meth:`ExactTerm.rpow() <sage.rings.asymptotic.term_monoid.ExactTerm.rpow>` 

for a detailed explanation.) 

Another way is to use a larger coefficient ring:: 

 

sage: M_QQ.<n> = AsymptoticRing(growth_group='QQ^n * n^QQ', coefficient_ring=QQ) 

sage: (1/2)^n 

(1/2)^n 

 

Multivariate Arithmetic 

^^^^^^^^^^^^^^^^^^^^^^^ 

 

Now let us move on to arithmetic in the multivariate ring 

 

:: 

 

sage: B 

Asymptotic Ring <x^QQ * log(x)^ZZ * QQ^y * y^QQ> over Rational Field 

 

.. TODO:: 

 

write this part 

 

 

More Examples 

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

 

 

The mathematical constant e as a limit 

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

 

The base of the natural logarithm `e` satisfies the equation 

 

.. MATH:: 

 

e = \lim_{n\to\infty} \left(1+\frac{1}{n}\right)^n 

 

By using asymptotic expansions, we obtain the more precise result 

:: 

 

sage: E.<n> = AsymptoticRing(growth_group='n^ZZ', coefficient_ring=SR, default_prec=5); E 

Asymptotic Ring <n^ZZ> over Symbolic Ring 

sage: (1 + 1/n)^n 

e - 1/2*e*n^(-1) + 11/24*e*n^(-2) - 7/16*e*n^(-3) + 2447/5760*e*n^(-4) + O(n^(-5)) 

 

 

Selected Technical Details 

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

 

 

Coercions and Functorial Constructions 

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

 

The :class:`AsymptoticRing` fully supports 

`coercion <../../../../coercion/index.html>`_. For example, the coefficient ring is automatically extended when needed:: 

 

sage: A 

Asymptotic Ring <z^QQ> over Integer Ring 

sage: (z + 1/2).parent() 

Asymptotic Ring <z^QQ> over Rational Field 

 

Here, the coefficient ring was extended to allow `1/2` as a 

coefficient. Another example is 

:: 

 

sage: C.<c> = AsymptoticRing(growth_group='c^ZZ', coefficient_ring=ZZ['e']) 

sage: C.an_element() 

e^3*c^3 + O(c) 

sage: C.an_element() / 7 

1/7*e^3*c^3 + O(c) 

 

Here the result's coefficient ring is the newly found 

:: 

 

sage: (C.an_element() / 7).parent() 

Asymptotic Ring <c^ZZ> over 

Univariate Polynomial Ring in e over Rational Field 

 

Not only the coefficient ring can be extended, but the growth group as 

well. For example, we can add/multiply elements of the asymptotic 

rings ``A`` and ``C`` to get an expansion of new asymptotic ring:: 

 

sage: r = c*z + c/2 + O(z); r 

c*z + 1/2*c + O(z) 

sage: r.parent() 

Asymptotic Ring <c^ZZ * z^QQ> over 

Univariate Polynomial Ring in e over Rational Field 

 

 

Data Structures 

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

 

The summands of an 

:class:`asymptotic expansion <AsymptoticExpansion>` are wrapped 

:doc:`growth group elements <growth_group>`. 

This wrapping is done by the 

:doc:`term monoid module <term_monoid>`. 

However, inside an 

:class:`asymptotic expansion <AsymptoticExpansion>` these summands 

(terms) are stored together with their growth-relationship, i.e., each 

summand knows its direct predecessors and successors. As a data 

structure a special poset (namely a 

:mod:`mutable poset <sage.data_structures.mutable_poset>`) 

is used. We can have a look at this:: 

 

sage: b = x^3*y + x^2*y + x*y^2 + O(x) + O(y) 

sage: print(b.summands.repr_full(reverse=True)) 

poset(x*y^2, x^3*y, x^2*y, O(x), O(y)) 

+-- oo 

| +-- no successors 

| +-- predecessors: x*y^2, x^3*y 

+-- x*y^2 

| +-- successors: oo 

| +-- predecessors: O(x), O(y) 

+-- x^3*y 

| +-- successors: oo 

| +-- predecessors: x^2*y 

+-- x^2*y 

| +-- successors: x^3*y 

| +-- predecessors: O(x), O(y) 

+-- O(x) 

| +-- successors: x*y^2, x^2*y 

| +-- predecessors: null 

+-- O(y) 

| +-- successors: x*y^2, x^2*y 

| +-- predecessors: null 

+-- null 

| +-- successors: O(x), O(y) 

| +-- no predecessors 

 

 

Various 

======= 

 

AUTHORS: 

 

- Benjamin Hackl (2015) 

- Daniel Krenn (2015) 

- Clemens Heuberger (2016) 

 

ACKNOWLEDGEMENT: 

 

- Benjamin Hackl, Clemens Heuberger and Daniel Krenn are supported by the 

Austrian Science Fund (FWF): P 24644-N26. 

 

- Benjamin Hackl is supported by the Google Summer of Code 2015. 

 

 

Classes and Methods 

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

""" 

 

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

# Copyright (C) 2015 Benjamin Hackl <benjamin.hackl@aau.at> 

# 2015 Daniel Krenn <dev@danielkrenn.at> 

# 

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

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 2 of the License, or 

# (at your option) any later version. 

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

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

from __future__ import print_function 

from __future__ import absolute_import 

 

from six import iteritems 

 

from sage.rings.ring import Algebra 

from sage.structure.element import CommutativeAlgebraElement 

from sage.structure.unique_representation import UniqueRepresentation 

from sage.misc.defaults import series_precision 

from sage.rings.all import RIF 

 

 

class NoConvergenceError(RuntimeError): 

r""" 

A special :python:`RuntimeError<library/exceptions.html#exceptions.RuntimeError>` 

which is raised when an algorithm does not converge/stop. 

""" 

pass 

 

 

class AsymptoticExpansion(CommutativeAlgebraElement): 

r""" 

Class for asymptotic expansions, i.e., the elements of an 

:class:`AsymptoticRing`. 

 

INPUT: 

 

- ``parent`` -- the parent of the asymptotic expansion. 

 

- ``summands`` -- the summands as a 

:class:`~sage.data_structures.mutable_poset.MutablePoset`, which 

represents the underlying structure. 

 

- ``simplify`` -- a boolean (default: ``True``). It controls 

automatic simplification (absorption) of the asymptotic expansion. 

 

- ``convert`` -- a boolean (default: ``True``). If set, then the 

``summands`` are converted to the asymptotic ring (the parent of this 

expansion). If not, then the summands are taken as they are. In 

that case, the caller must ensure that the parent of the terms is 

set correctly. 

 

EXAMPLES: 

 

There are several ways to create asymptotic expansions; usually 

this is done by using the corresponding :class:`asymptotic rings <AsymptoticRing>`:: 

 

sage: R_x.<x> = AsymptoticRing(growth_group='x^QQ', coefficient_ring=QQ); R_x 

Asymptotic Ring <x^QQ> over Rational Field 

sage: R_y.<y> = AsymptoticRing(growth_group='y^ZZ', coefficient_ring=ZZ); R_y 

Asymptotic Ring <y^ZZ> over Integer Ring 

 

At this point, `x` and `y` are already asymptotic expansions:: 

 

sage: type(x) 

<class 'sage.rings.asymptotic.asymptotic_ring.AsymptoticRing_with_category.element_class'> 

 

The usual ring operations, but allowing rational exponents (growth 

group ``x^QQ``) can be performed:: 

 

sage: x^2 + 3*(x - x^(2/5)) 

x^2 + 3*x - 3*x^(2/5) 

sage: (3*x^(1/3) + 2)^3 

27*x + 54*x^(2/3) + 36*x^(1/3) + 8 

 

One of the central ideas behind computing with asymptotic 

expansions is that the `O`-notation (see 

:wikipedia:`Big_O_notation`) can be used. For example, we have:: 

 

sage: (x+2*x^2+3*x^3+4*x^4) * (O(x)+x^2) 

4*x^6 + O(x^5) 

 

In particular, :func:`~sage.rings.big_oh.O` can be used to 

construct the asymptotic expansions. With the help of the 

:meth:`summands`, we can also have a look at the inner structure 

of an asymptotic expansion:: 

 

sage: expr1 = x + 2*x^2 + 3*x^3 + 4*x^4; expr2 = O(x) + x^2 

sage: print(expr1.summands.repr_full()) 

poset(x, 2*x^2, 3*x^3, 4*x^4) 

+-- null 

| +-- no predecessors 

| +-- successors: x 

+-- x 

| +-- predecessors: null 

| +-- successors: 2*x^2 

+-- 2*x^2 

| +-- predecessors: x 

| +-- successors: 3*x^3 

+-- 3*x^3 

| +-- predecessors: 2*x^2 

| +-- successors: 4*x^4 

+-- 4*x^4 

| +-- predecessors: 3*x^3 

| +-- successors: oo 

+-- oo 

| +-- predecessors: 4*x^4 

| +-- no successors 

sage: print(expr2.summands.repr_full()) 

poset(O(x), x^2) 

+-- null 

| +-- no predecessors 

| +-- successors: O(x) 

+-- O(x) 

| +-- predecessors: null 

| +-- successors: x^2 

+-- x^2 

| +-- predecessors: O(x) 

| +-- successors: oo 

+-- oo 

| +-- predecessors: x^2 

| +-- no successors 

sage: print((expr1 * expr2).summands.repr_full()) 

poset(O(x^5), 4*x^6) 

+-- null 

| +-- no predecessors 

| +-- successors: O(x^5) 

+-- O(x^5) 

| +-- predecessors: null 

| +-- successors: 4*x^6 

+-- 4*x^6 

| +-- predecessors: O(x^5) 

| +-- successors: oo 

+-- oo 

| +-- predecessors: 4*x^6 

| +-- no successors 

 

In addition to the monomial growth elements from above, we can 

also compute with logarithmic terms (simply by constructing the 

appropriate growth group):: 

 

sage: R_log = AsymptoticRing(growth_group='log(x)^QQ', coefficient_ring=QQ) 

sage: lx = R_log(log(SR.var('x'))) 

sage: (O(lx) + lx^3)^4 

log(x)^12 + O(log(x)^10) 

 

.. SEEALSO:: 

 

:doc:`growth_group`, 

:doc:`term_monoid`, 

:mod:`~sage.data_structures.mutable_poset`. 

""" 

def __init__(self, parent, summands, simplify=True, convert=True): 

r""" 

See :class:`AsymptoticExpansion` for more information. 

 

TESTS:: 

 

sage: R_x.<x> = AsymptoticRing(growth_group='x^ZZ', coefficient_ring=ZZ) 

sage: R_y.<y> = AsymptoticRing(growth_group='y^ZZ', coefficient_ring=ZZ) 

sage: R_x is R_y 

False 

sage: ex1 = x + 2*x^2 + 3*x^3 + 4*x^4 + 5*x^5 

sage: ex2 = x + O(R_x(1)) 

sage: ex1 * ex2 

5*x^6 + O(x^5) 

 

:: 

 

sage: from sage.rings.asymptotic.growth_group import GrowthGroup 

sage: from sage.rings.asymptotic.term_monoid import TermMonoid 

sage: G = GrowthGroup('x^ZZ'); x = G.gen() 

sage: OT = TermMonoid('O', G, ZZ); ET = TermMonoid('exact', G, ZZ) 

sage: R = AsymptoticRing(G, ZZ) 

sage: lst = [ET(x, 1), ET(x^2, 2), OT(x^3), ET(x^4, 4)] 

sage: expr = R(lst, simplify=False); expr # indirect doctest 

4*x^4 + O(x^3) + 2*x^2 + x 

sage: print(expr.summands.repr_full()) 

poset(x, 2*x^2, O(x^3), 4*x^4) 

+-- null 

| +-- no predecessors 

| +-- successors: x 

+-- x 

| +-- predecessors: null 

| +-- successors: 2*x^2 

+-- 2*x^2 

| +-- predecessors: x 

| +-- successors: O(x^3) 

+-- O(x^3) 

| +-- predecessors: 2*x^2 

| +-- successors: 4*x^4 

+-- 4*x^4 

| +-- predecessors: O(x^3) 

| +-- successors: oo 

+-- oo 

| +-- predecessors: 4*x^4 

| +-- no successors 

sage: expr._simplify_(); expr 

4*x^4 + O(x^3) 

sage: print(expr.summands.repr_full()) 

poset(O(x^3), 4*x^4) 

+-- null 

| +-- no predecessors 

| +-- successors: O(x^3) 

+-- O(x^3) 

| +-- predecessors: null 

| +-- successors: 4*x^4 

+-- 4*x^4 

| +-- predecessors: O(x^3) 

| +-- successors: oo 

+-- oo 

| +-- predecessors: 4*x^4 

| +-- no successors 

sage: R(lst, simplify=True) # indirect doctest 

4*x^4 + O(x^3) 

 

:: 

 

sage: R.<x> = AsymptoticRing(growth_group='x^QQ', coefficient_ring=QQ) 

sage: e = R(x^2 + O(x)) 

sage: from sage.rings.asymptotic.asymptotic_ring import AsymptoticExpansion 

sage: S = AsymptoticRing(growth_group='x^QQ', coefficient_ring=ZZ) 

sage: for s in AsymptoticExpansion(S, e.summands).summands.elements_topological(): 

....: print(s.parent()) 

O-Term Monoid x^QQ with implicit coefficients in Integer Ring 

Exact Term Monoid x^QQ with coefficients in Integer Ring 

sage: for s in AsymptoticExpansion(S, e.summands, 

....: convert=False).summands.elements_topological(): 

....: print(s.parent()) 

O-Term Monoid x^QQ with implicit coefficients in Rational Field 

Exact Term Monoid x^QQ with coefficients in Rational Field 

 

:: 

 

sage: AsymptoticExpansion(S, R(1/2).summands) 

Traceback (most recent call last): 

... 

ValueError: Cannot include 1/2 with parent 

Exact Term Monoid x^QQ with coefficients in Rational Field in 

Asymptotic Ring <x^QQ> over Integer Ring 

> *previous* ValueError: 1/2 is not a coefficient in 

Exact Term Monoid x^QQ with coefficients in Integer Ring. 

 

Check :trac:`19921`:: 

 

sage: CR.<Z> = QQ['Z'] 

sage: CR_mod = CR.quotient((Z^2 - 1)*CR) 

sage: R.<x> = AsymptoticRing(growth_group='x^NN', coefficient_ring=CR) 

sage: R_mod = R.change_parameter(coefficient_ring=CR_mod) 

sage: e = 1 + x*(Z^2-1) 

sage: R_mod(e) 

1 

 

Check that :trac:`19999` is resolved:: 

 

sage: A.<x> = AsymptoticRing('QQ^x * x^QQ', QQ) 

sage: 1 + (-1)^x + 2^x + (-2)^x 

(-2)^x + 2^x + (-1)^x + 1 

""" 

super(AsymptoticExpansion, self).__init__(parent=parent) 

 

from sage.data_structures.mutable_poset import MutablePoset 

if not isinstance(summands, MutablePoset): 

raise TypeError('Summands %s are not in a mutable poset as expected ' 

'when creating an element of %s.' % (summands, parent)) 

 

if convert: 

from .misc import combine_exceptions 

from .term_monoid import TermMonoid, ZeroCoefficientError 

def convert_terms(element): 

T = TermMonoid(term_monoid=element.parent(), asymptotic_ring=parent) 

try: 

return T(element) 

except ZeroCoefficientError: 

return None 

except (ValueError, TypeError) as e: 

raise combine_exceptions( 

ValueError('Cannot include %s with parent %s in %s' % 

(element, element.parent(), parent)), e) 

new_summands = summands.copy() 

new_summands.map(convert_terms, topological=True, reverse=True) 

self._summands_ = new_summands 

else: 

self._summands_ = summands 

 

if simplify: 

self._simplify_() 

 

 

@property 

def summands(self): 

r""" 

The summands of this asymptotic expansion stored in the 

underlying data structure (a 

:class:`~sage.data_structures.mutable_poset.MutablePoset`). 

 

EXAMPLES:: 

 

sage: R.<x> = AsymptoticRing(growth_group='x^ZZ', coefficient_ring=ZZ) 

sage: expr = 7*x^12 + x^5 + O(x^3) 

sage: expr.summands 

poset(O(x^3), x^5, 7*x^12) 

 

.. SEEALSO:: 

 

:class:`sage.data_structures.mutable_poset.MutablePoset` 

""" 

return self._summands_ 

 

 

def __hash__(self): 

r""" 

A hash value for this element. 

 

.. WARNING:: 

 

This hash value uses the string representation and might not be 

always right. 

 

TESTS:: 

 

sage: R_log = AsymptoticRing(growth_group='log(x)^QQ', coefficient_ring=QQ) 

sage: lx = R_log(log(SR.var('x'))) 

sage: elt = (O(lx) + lx^3)^4 

sage: hash(elt) # random 

-4395085054568712393 

""" 

return hash(str(self)) 

 

 

def __bool__(self): 

r""" 

Return whether this asymptotic expansion is not identically zero. 

 

INPUT: 

 

Nothing. 

 

OUTPUT: 

 

A boolean. 

 

TESTS:: 

 

sage: R.<x> = AsymptoticRing(growth_group='x^ZZ', coefficient_ring=ZZ) 

sage: bool(R(0)) # indirect doctest 

False 

sage: bool(x) # indirect doctest 

True 

sage: bool(7*x^12 + x^5 + O(x^3)) # indirect doctest 

True 

""" 

return bool(self._summands_) 

 

__nonzero__ = __bool__ 

 

def __eq__(self, other): 

r""" 

Return whether this asymptotic expansion is equal to ``other``. 

 

INPUT: 

 

- ``other`` -- an object. 

 

OUTPUT: 

 

A boolean. 

 

.. NOTE:: 

 

This function uses the coercion model to find a common 

parent for the two operands. 

 

EXAMPLES:: 

 

sage: R.<x> = AsymptoticRing('x^ZZ', QQ) 

sage: (1 + 2*x + 3*x^2) == (3*x^2 + 2*x + 1) # indirect doctest 

True 

sage: O(x) == O(x) 

False 

 

TESTS:: 

 

sage: x == None 

False 

 

:: 

 

sage: x == 'x' 

False 

""" 

if other is None: 

return False 

try: 

return not bool(self - other) 

except (TypeError, ValueError): 

return False 

 

 

def __ne__(self, other): 

r""" 

Return whether this asymptotic expansion is not equal to ``other``. 

 

INPUT: 

 

- ``other`` -- an object. 

 

OUTPUT: 

 

A boolean. 

 

.. NOTE:: 

 

This function uses the coercion model to find a common 

parent for the two operands. 

 

EXAMPLES:: 

 

sage: R.<x> = AsymptoticRing('x^ZZ', QQ) 

sage: (1 + 2*x + 3*x^2) != (3*x^2 + 2*x + 1) # indirect doctest 

False 

sage: O(x) != O(x) 

True 

 

TESTS:: 

 

sage: x != None 

True 

""" 

return not self == other 

 

 

def has_same_summands(self, other): 

r""" 

Return whether this asymptotic expansion and ``other`` have the 

same summands. 

 

INPUT: 

 

- ``other`` -- an asymptotic expansion. 

 

OUTPUT: 

 

A boolean. 

 

.. NOTE:: 

 

While for example ``O(x) == O(x)`` yields ``False``, 

these expansions *do* have the same summands and this method 

returns ``True``. 

 

Moreover, this method uses the coercion model in order to 

find a common parent for this asymptotic expansion and 

``other``. 

 

EXAMPLES:: 

 

sage: R_ZZ.<x_ZZ> = AsymptoticRing('x^ZZ', ZZ) 

sage: R_QQ.<x_QQ> = AsymptoticRing('x^ZZ', QQ) 

sage: sum(x_ZZ^k for k in range(5)) == sum(x_QQ^k for k in range(5)) # indirect doctest 

True 

sage: O(x_ZZ) == O(x_QQ) 

False 

 

TESTS:: 

 

sage: x_ZZ.has_same_summands(None) 

False 

""" 

if other is None: 

return False 

from sage.structure.element import have_same_parent 

if have_same_parent(self, other): 

return self._has_same_summands_(other) 

 

from sage.structure.element import get_coercion_model 

return get_coercion_model().bin_op(self, other, 

lambda self, other: 

self._has_same_summands_(other)) 

 

 

def _has_same_summands_(self, other): 

r""" 

Return whether this :class:`AsymptoticExpansion` has the same 

summands as ``other``. 

 

INPUT: 

 

- ``other`` -- an :class:`AsymptoticExpansion`. 

 

OUTPUT: 

 

A boolean. 

 

.. NOTE:: 

 

This method compares two :class:`AsymptoticExpansion` 

with the same parent. 

 

EXAMPLES:: 

 

sage: R.<x> = AsymptoticRing('x^ZZ', QQ) 

sage: O(x).has_same_summands(O(x)) 

True 

sage: (1 + x + 2*x^2).has_same_summands(2*x^2 + O(x)) # indirect doctest 

False 

""" 

if len(self.summands) != len(other.summands): 

return False 

from builtins import zip 

return all(s == o for s, o in 

zip(self.summands.elements_topological(), 

other.summands.elements_topological())) 

 

def _simplify_(self): 

r""" 

Simplify this asymptotic expansion. 

 

INPUT: 

 

Nothing. 

 

OUTPUT: 

 

Nothing, but modifies this asymptotic expansion. 

 

.. NOTE:: 

 

This method is usually called during initialization of 

this asymptotic expansion. 

 

.. NOTE:: 

 

This asymptotic expansion is simplified by letting 

`O`-terms that are included in this expansion absorb all 

terms with smaller growth. 

 

TESTS:: 

 

sage: from sage.rings.asymptotic.growth_group import GrowthGroup 

sage: from sage.rings.asymptotic.term_monoid import TermMonoid 

sage: G = GrowthGroup('x^ZZ') 

sage: OT = TermMonoid('O', G, ZZ); ET = TermMonoid('exact', G, ZZ) 

sage: R = AsymptoticRing(G, ZZ) 

sage: lst = [ET(x, 1), ET(x^2, 2), OT(x^3), ET(x^4, 4)] 

sage: expr = R(lst, simplify=False); expr # indirect doctest 

4*x^4 + O(x^3) + 2*x^2 + x 

sage: expr._simplify_(); expr 

4*x^4 + O(x^3) 

sage: R(lst) # indirect doctest 

4*x^4 + O(x^3) 

""" 

self._summands_.merge(reverse=True) 

 

 

def _repr_(self, latex=False): 

r""" 

A representation string for this asymptotic expansion. 

 

INPUT: 

 

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

LaTeX-output is returned. 

 

OUTPUT: 

 

A string. 

 

EXAMPLES:: 

 

sage: R.<x> = AsymptoticRing(growth_group='x^ZZ', coefficient_ring=ZZ) 

sage: (5*x^2+12*x) * (x^3+O(x)) # indirect doctest 

5*x^5 + 12*x^4 + O(x^3) 

sage: (5*x^2-12*x) * (x^3+O(x)) # indirect doctest 

5*x^5 - 12*x^4 + O(x^3) 

""" 

if latex: 

from sage.misc.latex import latex as latex_repr 

f = latex_repr 

else: 

f = repr 

s = ' + '.join(f(elem) for elem in 

self.summands.elements_topological(reverse=True)) 

s = s.replace('+ -', '- ') 

if not s: 

return '0' 

return s 

 

 

def _latex_(self): 

r""" 

A LaTeX-representation string for this asymptotic expansion. 

 

OUTPUT: 

 

A string. 

 

TESTS:: 

 

sage: R.<x> = AsymptoticRing(growth_group='x^ZZ', coefficient_ring=ZZ) 

sage: latex((5*x^2+12*x) * (x^3+O(x))) # indirect doctest 

5 x^{5} + 12 x^{4} + O\!\left(x^{3}\right) 

sage: latex((5*x^2-12*x) * (x^3+O(x))) # indirect doctest 

5 x^{5} - 12 x^{4} + O\!\left(x^{3}\right) 

""" 

return self._repr_(latex=True) 

 

 

def show(self): 

r""" 

Pretty-print this asymptotic expansion. 

 

OUTPUT: 

 

Nothing, the representation is printed directly on the 

screen. 

 

EXAMPLES:: 

 

sage: A.<x> = AsymptoticRing('QQ^x * x^QQ * log(x)^QQ', SR.subring(no_variables=True)) 

sage: (pi/2 * 5^x * x^(42/17) - sqrt(euler_gamma) * log(x)^(-7/8)).show() 

<html><script type="math/tex">\newcommand{\Bold}[1]{\mathbf{#1}}\frac{1}{2} \, \pi 

5^{x} x^{\frac{42}{17}} - \sqrt{\gamma_E} \log\left(x\right)^{-\frac{7}{8}}</script></html> 

 

TESTS:: 

 

sage: A.<x> = AsymptoticRing('(e^x)^QQ * x^QQ', SR.subring(no_variables=True)) 

sage: (zeta(3) * (e^x)^(-1/2) * x^42).show() 

<html><script type="math/tex">\newcommand{\Bold}[1]{\mathbf{#1}}\zeta(3) 

\left(e^{x}\right)^{-\frac{1}{2}} x^{42}</script></html> 

""" 

from sage.repl.rich_output.pretty_print import pretty_print 

pretty_print(self) 

 

def monomial_coefficient(self, monomial): 

r""" 

Return the coefficient in the base ring of the given monomial 

in this expansion. 

 

INPUT: 

 

- ``monomial`` -- a monomial element which can be converted 

into the asymptotic ring of this element 

 

OUTPUT: 

 

An element of the coefficient ring. 

 

EXAMPLES:: 

 

sage: R.<m, n> = AsymptoticRing("m^QQ*n^QQ", QQ) 

sage: ae = 13 + 42/n + 2/n/m + O(n^-2) 

sage: ae.monomial_coefficient(1/n) 

42 

sage: ae.monomial_coefficient(1/n^3) 

0 

sage: R.<n> = AsymptoticRing("n^QQ", ZZ) 

sage: ae.monomial_coefficient(1/n) 

42 

sage: ae.monomial_coefficient(1) 

13 

 

TESTS: 

 

Conversion of ``monomial`` the parent of this element must be 

possible:: 

 

sage: R.<m> = AsymptoticRing("m^QQ", QQ) 

sage: S.<n> = AsymptoticRing("n^QQ", QQ) 

sage: m.monomial_coefficient(n) 

Traceback (most recent call last): 

... 

ValueError: Cannot include n with parent Exact Term Monoid 

n^QQ with coefficients in Rational Field in Asymptotic Ring 

<m^QQ> over Rational Field 

> *previous* ValueError: n is not in Growth Group m^QQ 

 

Only monomials are allowed:: 

 

sage: R.<n> = AsymptoticRing("n^QQ", QQ) 

sage: (n + 4).monomial_coefficient(n + 5) 

Traceback (most recent call last): 

... 

ValueError: n + 5 not a monomial 

sage: n.monomial_coefficient(0) 

Traceback (most recent call last): 

... 

ValueError: 0 not a monomial 

 

Cannot extract the coefficient of an O term:: 

 

sage: O(n).monomial_coefficient(n) 

Traceback (most recent call last): 

... 

AttributeError: 'OTermMonoid_with_category.element_class' 

object has no attribute 'coefficient' 

 

The ``monomial`` must be exact:: 

 

sage: n.monomial_coefficient(O(n)) 

Traceback (most recent call last): 

... 

ValueError: non-exact monomial O(n) 

 

""" 

monomial = self.parent()(monomial) 

if not monomial.is_exact(): 

raise ValueError("non-exact monomial {}".format(monomial)) 

 

if len(monomial.summands) != 1: 

raise ValueError("{} not a monomial".format(monomial)) 

 

monomial_growth = next(monomial.summands.elements()).growth 

try: 

return self.summands.element(monomial_growth).coefficient 

except KeyError: 

return self.parent().coefficient_ring(0) 

 

def _add_(self, other): 

r""" 

Add ``other`` to this asymptotic expansion. 

 

INPUT: 

 

- ``other`` -- an :class:`AsymptoticExpansion`. 

 

OUTPUT: 

 

The sum as an :class:`AsymptoticExpansion`. 

 

EXAMPLES:: 

 

sage: R.<x> = AsymptoticRing(growth_group='x^ZZ', coefficient_ring=ZZ) 

sage: expr1 = x^123; expr2 = x^321 

sage: expr1._add_(expr2) 

x^321 + x^123 

sage: expr1 + expr2 # indirect doctest 

x^321 + x^123 

 

If an `O`-term is added to an asymptotic expansion, then 

the `O`-term absorbs everything it can:: 

 

sage: x^123 + x^321 + O(x^555) # indirect doctest 

O(x^555) 

 

TESTS:: 

 

sage: x + O(x) 

O(x) 

sage: O(x) + x 

O(x) 

""" 

return self.parent()(self.summands.union(other.summands), 

simplify=True, convert=False) 

 

 

def _sub_(self, other): 

r""" 

Subtract ``other`` from this asymptotic expansion. 

 

INPUT: 

 

- ``other`` -- an :class:`AsymptoticExpansion`. 

 

OUTPUT: 

 

The difference as an :class:`AsymptoticExpansion`. 

 

.. NOTE:: 

 

Subtraction of two asymptotic expansions is implemented 

by means of addition: `e_1 - e_2 = e_1 + (-1)\cdot e_2`. 

 

EXAMPLES:: 

 

sage: R.<x> = AsymptoticRing(growth_group='x^ZZ', coefficient_ring=ZZ) 

sage: expr1 = x^123; expr2 = x^321 

sage: expr1 - expr2 # indirect doctest 

-x^321 + x^123 

sage: O(x) - O(x) 

O(x) 

""" 

return self + self.parent().coefficient_ring(-1)*other 

 

 

def _mul_term_(self, term): 

r""" 

Helper method: multiply this asymptotic expansion by the 

asymptotic term ``term``. 

 

INPUT: 

 

- ``term`` -- an asymptotic term (see 

:doc:`term_monoid`). 

 

OUTPUT: 

 

The product as an :class:`AsymptoticExpansion`. 

 

TESTS:: 

 

sage: from sage.rings.asymptotic.term_monoid import OTermMonoid 

sage: R.<x> = AsymptoticRing(growth_group='x^ZZ', coefficient_ring=ZZ) 

sage: T = OTermMonoid(R.growth_group, ZZ) 

sage: expr = 10*x^2 + O(x) 

sage: t = T(R.growth_group.gen()) 

sage: expr._mul_term_(t) 

O(x^3) 

""" 

simplify = not term.is_exact() 

return self.parent()(self.summands.mapped(lambda element: term * element), 

simplify=simplify, convert=False) 

 

 

def _mul_(self, other): 

r""" 

Multiply this asymptotic expansion by another asymptotic expansion ``other``. 

 

INPUT: 

 

- ``other`` -- an :class:`AsymptoticExpansion`. 

 

OUTPUT: 

 

The product as an :class:`AsymptoticExpansion`. 

 

EXAMPLES:: 

 

sage: R.<x> = AsymptoticRing(growth_group='x^ZZ', coefficient_ring=ZZ) 

sage: ex1 = 5*x^12 

sage: ex2 = x^3 + O(x) 

sage: ex1 * ex2 # indirect doctest 

5*x^15 + O(x^13) 

 

.. TODO:: 

 

The current implementation is the standard long 

multiplication. More efficient variants like Karatsuba 

multiplication, or methods that exploit the structure 

of the underlying poset shall be implemented at a later 

point. 

 

TESTS:: 

 

sage: R(1) * R(0) 

0 

sage: _.parent() 

Asymptotic Ring <x^ZZ> over Integer Ring 

""" 

return sum(iter(self._mul_term_(term_other) for 

term_other in other.summands.elements()), 

self.parent().zero()) 

 

def _lmul_(self, other): 

r""" 

Multiply this asymptotic expansion by an element ``other`` of its 

coefficient ring. 

 

INPUT: 

 

- ``other`` -- an element of the coefficient ring. 

 

OUTPUT: 

 

An :class:`AsymptoticExpansion`. 

 

TESTS:: 

 

sage: A.<a> = AsymptoticRing(growth_group='QQ^a * a^QQ * log(a)^QQ', coefficient_ring=ZZ) 

sage: 2*a # indirect doctest 

2*a 

""" 

if other.is_zero(): 

return self.parent().zero() 

 

from .term_monoid import TermMonoid 

E = TermMonoid('exact', asymptotic_ring=self.parent()) 

e = E(self.parent().growth_group.one(), coefficient=other) 

return self._mul_term_(e) 

 

def _div_(self, other): 

r""" 

Divide this element through ``other``. 

 

INPUT: 

 

- ``other`` -- an asymptotic expansion. 

 

OUTPUT: 

 

An asymptotic expansion. 

 

EXAMPLES:: 

 

sage: R.<x> = AsymptoticRing('x^ZZ', QQ, default_prec=5) 

sage: 1/x^42 

x^(-42) 

sage: (1 + 4*x) / (x + 2*x^2) 

2*x^(-1) - 1/2*x^(-2) + 1/4*x^(-3) - 1/8*x^(-4) + 1/16*x^(-5) + O(x^(-6)) 

sage: x / O(x) 

Traceback (most recent call last): 

... 

ZeroDivisionError: Cannot invert O(x). 

 

TESTS: 

 

See :trac:`19521`:: 

 

sage: A.<n> = AsymptoticRing('n^ZZ', SR.subring(no_variables=True)) 

sage: (A.one() / 1).parent() 

Asymptotic Ring <n^ZZ> over Symbolic Constants Subring 

""" 

return self * ~other 

 

 

def __invert__(self, precision=None): 

r""" 

Return the multiplicative inverse of this element. 

 

INPUT: 

 

- ``precision`` -- the precision used for truncating the 

expansion. If ``None`` (default value) is used, the 

default precision of the parent is used. 

 

OUTPUT: 

 

An asymptotic expansion. 

 

.. WARNING:: 

 

Due to truncation of infinite expansions, the element 

returned by this method might not fulfill 

``el * ~el == 1``. 

 

.. TODO:: 

 

As soon as `L`-terms are implemented, this 

implementation has to be adapted as well in order to 

yield correct results. 

 

EXAMPLES:: 

 

sage: R.<x> = AsymptoticRing(growth_group='x^ZZ', coefficient_ring=QQ, default_prec=4) 

sage: ~x 

x^(-1) 

sage: ~(x^42) 

x^(-42) 

sage: ex = ~(1 + x); ex 

x^(-1) - x^(-2) + x^(-3) - x^(-4) + O(x^(-5)) 

sage: ex * (1+x) 

1 + O(x^(-4)) 

sage: ~(1 + O(1/x)) 

1 + O(x^(-1)) 

 

TESTS:: 

 

sage: A.<a> = AsymptoticRing(growth_group='a^ZZ', coefficient_ring=ZZ) 

sage: (1 / a).parent() 

Asymptotic Ring <a^ZZ> over Rational Field 

sage: (a / 2).parent() 

Asymptotic Ring <a^ZZ> over Rational Field 

 

:: 

 

sage: ~A(0) 

Traceback (most recent call last): 

... 

ZeroDivisionError: Cannot invert 0 in 

Asymptotic Ring <a^ZZ> over Integer Ring. 

 

:: 

 

sage: B.<s, t> = AsymptoticRing(growth_group='s^ZZ * t^ZZ', coefficient_ring=QQ) 

sage: ~(s + t) 

Traceback (most recent call last): 

... 

ValueError: Cannot determine main term of s + t since there 

are several maximal elements s, t. 

""" 

if not self.summands: 

raise ZeroDivisionError( 

'Cannot invert {} in {}.'.format(self, self.parent())) 

 

(imax_elem, x) = self._main_term_relative_error_(return_inverse_main_term=True) 

one = x.parent().one() 

 

if x: 

import itertools 

result = AsymptoticExpansion._power_series_( 

coefficients=itertools.repeat(one), 

start=one, 

ratio=-x, 

ratio_start=one, 

precision=precision) 

else: 

result = one 

 

return result._mul_term_(imax_elem) 

 

 

invert = __invert__ 

 

 

def truncate(self, precision=None): 

r""" 

Truncate this asymptotic expansion. 

 

INPUT: 

 

- ``precision`` -- a positive integer or ``None``. Number of 

summands that are kept. If ``None`` (default value) is 

given, then ``default_prec`` from the parent is used. 

 

OUTPUT: 

 

An asymptotic expansion. 

 

.. NOTE:: 

 

For example, truncating an asymptotic expansion with 

``precision=20`` does not yield an expansion with exactly 20 

summands! Rather than that, it keeps the 20 summands 

with the largest growth, and adds appropriate 

`O`-Terms. 

 

EXAMPLES:: 

 

sage: R.<x> = AsymptoticRing('x^ZZ', QQ) 

sage: ex = sum(x^k for k in range(5)); ex 

x^4 + x^3 + x^2 + x + 1 

sage: ex.truncate(precision=2) 

x^4 + x^3 + O(x^2) 

sage: ex.truncate(precision=0) 

O(x^4) 

sage: ex.truncate() 

x^4 + x^3 + x^2 + x + 1 

""" 

if precision is None: 

precision = self.parent().default_prec 

 

if len(self.summands) <= precision: 

return self 

 

summands = self.summands.copy() 

from .term_monoid import TermMonoid 

def convert_terms(element): 

if convert_terms.count < precision: 

convert_terms.count += 1 

return element 

T = TermMonoid(term_monoid='O', asymptotic_ring=self.parent()) 

return T(element) 

convert_terms.count = 0 

summands.map(convert_terms, topological=True, reverse=True) 

return self.parent()(summands, simplify=True, convert=False) 

 

 

def exact_part(self): 

r""" 

Return the expansion consisting of all exact terms of this 

expansion. 

 

INPUT: 

 

Nothing 

 

OUTPUT: 

 

An asymptotic expansion. 

 

EXAMPLES:: 

 

sage: R.<x> = AsymptoticRing('x^QQ * log(x)^QQ', QQ) 

sage: (x^2 + O(x)).exact_part() 

x^2 

sage: (x + log(x)/2 + O(log(x)/x)).exact_part() 

x + 1/2*log(x) 

 

TESTS:: 

 

sage: R.<x, y> = AsymptoticRing('x^QQ * y^QQ', QQ) 

sage: (x + y + O(1/(x*y))).exact_part() 

x + y 

sage: O(x).exact_part() 

0 

""" 

exact_terms = self.summands.copy() 

for term in self.summands.elements_topological(): 

if not term.is_exact(): 

exact_terms.remove(term.growth) 

 

return self.parent(exact_terms) 

 

 

def __pow__(self, exponent, precision=None): 

r""" 

Calculate the power of this asymptotic expansion to the given ``exponent``. 

 

INPUT: 

 

- ``exponent`` -- an element. 

 

- ``precision`` -- the precision used for truncating the 

expansion. If ``None`` (default value) is used, the 

default precision of the parent is used. 

 

OUTPUT: 

 

An asymptotic expansion. 

 

EXAMPLES:: 

 

sage: Q.<x> = AsymptoticRing(growth_group='x^QQ', coefficient_ring=QQ) 

sage: x^(1/7) 

x^(1/7) 

sage: (x^(1/2) + O(x^0))^15 

x^(15/2) + O(x^7) 

 

:: 

 

sage: Z.<y> = AsymptoticRing(growth_group='y^ZZ', coefficient_ring=ZZ) 

sage: y^(1/7) 

y^(1/7) 

sage: _.parent() 

Asymptotic Ring <y^QQ> over Rational Field 

sage: (y^2 + O(y))^(1/2) 

y + O(1) 

sage: (y^2 + O(y))^(-2) 

y^(-4) + O(y^(-5)) 

sage: (1 + 1/y + O(1/y^3))^pi 

1 + pi*y^(-1) + (1/2*pi*(pi - 1))*y^(-2) + O(y^(-3)) 

 

:: 

 

sage: B.<z> = AsymptoticRing(growth_group='z^QQ * log(z)^QQ', coefficient_ring=QQ) 

sage: (z^2 + O(z))^(1/2) 

z + O(1) 

 

:: 

 

sage: A.<x> = AsymptoticRing('QQ^x * x^SR * log(x)^ZZ', QQ) 

sage: x * 2^x 

2^x*x 

sage: 5^x * 2^x 

10^x 

sage: 2^log(x) 

x^(log(2)) 

sage: 2^(x + 1/x) 

2^x + log(2)*2^x*x^(-1) + 1/2*log(2)^2*2^x*x^(-2) + ... + O(2^x*x^(-20)) 

sage: _.parent() 

Asymptotic Ring <QQ^x * x^SR * log(x)^QQ> over Symbolic Ring 

 

:: 

 

sage: C.<c> = AsymptoticRing(growth_group='QQ^c * c^QQ', coefficient_ring=QQ, default_prec=5) 

sage: (3 + 1/c^2)^c 

3^c + 1/3*3^c*c^(-1) + 1/18*3^c*c^(-2) - 4/81*3^c*c^(-3) 

- 35/1944*3^c*c^(-4) + O(3^c*c^(-5)) 

sage: _.parent() 

Asymptotic Ring <QQ^c * c^QQ> over Rational Field 

sage: (2 + (1/3)^c)^c 

2^c + 1/2*(2/3)^c*c + 1/8*(2/9)^c*c^2 - 1/8*(2/9)^c*c 

+ 1/48*(2/27)^c*c^3 + O((2/27)^c*c^2) 

sage: _.parent() 

Asymptotic Ring <QQ^c * c^QQ> over Rational Field 

 

TESTS: 

 

See :trac:`19110`:: 

 

sage: O(x)^(-1) 

Traceback (most recent call last): 

... 

ZeroDivisionError: Cannot take O(x) to exponent -1. 

> *previous* ZeroDivisionError: rational division by zero 

 

:: 

 

sage: B.<z> = AsymptoticRing(growth_group='z^QQ * log(z)^QQ', coefficient_ring=QQ, default_prec=5) 

sage: z^(1+1/z) 

z + log(z) + 1/2*z^(-1)*log(z)^2 + 1/6*z^(-2)*log(z)^3 + 

1/24*z^(-3)*log(z)^4 + O(z^(-4)*log(z)^5) 

sage: _.parent() 

Asymptotic Ring <z^QQ * log(z)^QQ> over Rational Field 

 

:: 

 

sage: B(0)^(-7) 

Traceback (most recent call last): 

... 

ZeroDivisionError: Cannot take 0 to the negative exponent -7. 

sage: B(0)^SR.var('a') 

Traceback (most recent call last): 

... 

NotImplementedError: Taking 0 to the exponent a not implemented. 

 

:: 

 

sage: C.<s, t> = AsymptoticRing(growth_group='s^QQ * t^QQ', coefficient_ring=QQ) 

sage: (s + t)^s 

Traceback (most recent call last): 

... 

ValueError: Cannot take s + t to the exponent s. 

> *previous* ValueError: Cannot determine main term of s + t 

since there are several maximal elements s, t. 

 

Check that :trac:`19946` is fixed:: 

 

sage: A.<n> = AsymptoticRing('QQ^n * n^QQ', SR) 

sage: e = 2^n; e 

2^n 

sage: e.parent() 

Asymptotic Ring <SR^n * n^SR> over Symbolic Ring 

sage: e = A(e); e 

2^n 

sage: e.parent() 

Asymptotic Ring <QQ^n * n^QQ> over Symbolic Ring 

 

:trac:`22120`:: 

 

sage: A.<w> = AsymptoticRing('w^QQbar', QQ) 

sage: w^QQbar(sqrt(2)) 

w^(1.414213562373095?) 

""" 

if not self.summands: 

if exponent == 0: 

return self.parent().one() 

elif exponent > 0: 

return self.parent().zero() 

elif exponent < 0: 

raise ZeroDivisionError('Cannot take %s to the negative exponent %s.' % 

(self, exponent)) 

else: 

raise NotImplementedError('Taking %s to the exponent %s not implemented.' % 

(self, exponent)) 

 

elif exponent == 0: 

return self.parent().one() 

 

elif exponent == 1: 

return self 

 

elif len(self.summands) == 1: 

element = next(self.summands.elements()) 

if isinstance(exponent, AsymptoticExpansion) and element.is_constant(): 

 

return exponent.rpow(base=element.coefficient, precision=precision) 

try: 

return self.parent()._create_element_in_extension_( 

element ** exponent, element.parent()) 

except (ArithmeticError, TypeError, ValueError): 

if not isinstance(exponent, AsymptoticExpansion): 

raise 

 

from sage.rings.integer_ring import ZZ 

try: 

exponent = ZZ(exponent) 

except (TypeError, ValueError): 

pass 

else: 

return super(AsymptoticExpansion, self).__pow__(exponent) 

 

from sage.rings.rational_field import QQ 

try: 

exponent = QQ(exponent) 

except (TypeError, ValueError): 

pass 

else: 

return self.__pow_number__(exponent, precision=precision) 

 

from sage.symbolic.expression import Expression 

if isinstance(exponent, Expression) and exponent.is_constant(): 

return self.__pow_number__(exponent, precision=precision) 

 

if isinstance(exponent, AsymptoticExpansion) and len(self.summands) != 1: 

try: 

return self.__pow_number__(exponent, precision=precision, 

check_convergence=True) 

except NoConvergenceError: 

pass 

 

try: 

return (exponent * self.log(precision=precision)).exp(precision=precision) 

except (TypeError, ValueError, ZeroDivisionError) as e: 

from .misc import combine_exceptions 

raise combine_exceptions( 

ValueError('Cannot take %s to the exponent %s.' % (self, exponent)), e) 

 

 

pow = __pow__ 

 

 

def __pow_number__(self, exponent, precision=None, check_convergence=False): 

r""" 

Return the power of this asymptotic expansion to some 

number (``exponent``). 

 

Let `m` be the maximal element of this asymptotic expansion 

and `r` the remaining summands. This method calculates 

 

.. MATH:: 

 

(m + r)^{\mathit{exponent}} 

= m^{\mathit{exponent}} \sum_{k=0}^K 

\binom{\mathit{exponent}}{k} (r/m)^k 

 

where `K` is chosen such that adding an additional summand 

does not change the result. 

 

INPUT: 

 

- ``exponent`` -- a numerical value (e.g. integer, rational) 

or other constant. 

 

- ``precision`` -- a non-negative integer. 

 

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

then an additional check on the input is performed to ensure 

that the calculated sum converges. 

 

OUTPUT: 

 

An asymptotic expansion. 

 

.. SEEALSO:: 

 

:meth:`pow` 

 

TESTS:: 

 

sage: R.<x> = AsymptoticRing(growth_group='x^ZZ', coefficient_ring=ZZ) 

sage: (1 + x).__pow_number__(4) 

x^4 + 4*x^3 + 6*x^2 + 4*x + 1 

sage: _.parent() 

Asymptotic Ring <x^ZZ> over Rational Field 

sage: (x + 1).__pow_number__(1/2, precision=5) 

x^(1/2) + 1/2*x^(-1/2) - 1/8*x^(-3/2) + 1/16*x^(-5/2) 

- 5/128*x^(-7/2) + O(x^(-9/2)) 

sage: _.parent() 

Asymptotic Ring <x^QQ> over Rational Field 

sage: (8 + 1/x).__pow_number__(1/3, precision=5) 

2 + 1/12*x^(-1) - 1/288*x^(-2) + 5/20736*x^(-3) 

- 5/248832*x^(-4) + O(x^(-5)) 

sage: _.parent() 

Asymptotic Ring <x^QQ> over Rational Field 

 

:: 

 

sage: R(0).__pow_number__(-3/2) 

Traceback (most recent call last): 

... 

ZeroDivisionError: Cannot take 0 to the negative exponent -3/2. 

sage: R(0).__pow_number__(RIF(-1,1)) 

Traceback (most recent call last): 

... 

ValueError: Possible division by zero, since sign of 

the exponent 0.? cannot be determined. 

sage: R(0)^0 

1 

 

:: 

 

sage: A.<a, b> = AsymptoticRing(growth_group='a^ZZ * b^ZZ', coefficient_ring=QQ) 

sage: (a + b).__pow_number__(3/2) 

Traceback (most recent call last): 

... 

ValueError: Cannot determine main term of a + b since 

there are several maximal elements a, b. 

 

:: 

 

sage: S.<s> = AsymptoticRing(growth_group='QQ^s * s^ZZ', coefficient_ring=QQ) 

sage: (2 + 2/s^2).__pow_number__(s, precision=7) 

2^s + 2^s*s^(-1) + 1/2*2^s*s^(-2) - 1/3*2^s*s^(-3) 

- 11/24*2^s*s^(-4) + 11/120*2^s*s^(-5) 

+ 271/720*2^s*s^(-6) + O(2^s*s^(-7)) 

sage: _.parent() 

Asymptotic Ring <QQ^s * s^QQ> over Rational Field 

""" 

if not self.summands: 

if exponent > 0: 

return self.parent().zero() 

elif exponent.is_zero(): 

return self.parent().one() 

elif exponent < 0: 

raise ZeroDivisionError( 

'Cannot take {} to the negative ' 

'exponent {}.'.format(self, exponent)) 

else: 

raise ValueError( 

'Possible division by zero, since sign of the exponent ' 

'{} cannot be determined.'.format(exponent)) 

 

elif len(self.summands) == 1: 

element = next(self.summands.elements()) 

return self.parent()._create_element_in_extension_( 

element**exponent, element.parent()) 

 

try: 

(max_elem, x) = self._main_term_relative_error_() 

except ValueError: 

if check_convergence: 

raise NoConvergenceError 

raise 

 

if check_convergence: 

if not (x * exponent).is_little_o_of_one(): 

raise NoConvergenceError 

 

pmax = self.parent()(max_elem)**exponent 

 

import itertools 

def binomials(a): 

P = a.parent() 

a = a + 1 

f = P(1) 

for k in itertools.count(1): 

k = P(k) 

b = a - k 

if b == 0: 

return 

f *= b / k 

yield f 

 

one = x.parent().one() 

 

result = AsymptoticExpansion._power_series_( 

coefficients=binomials(exponent), 

start=one, 

ratio=x, 

ratio_start=one, 

precision=precision) 

 

return result * pmax 

 

 

def sqrt(self, precision=None): 

r""" 

Return the square root of this asymptotic expansion. 

 

INPUT: 

 

- ``precision`` -- the precision used for truncating the 

expansion. If ``None`` (default value) is used, the 

default precision of the parent is used. 

 

OUTPUT: 

 

An asymptotic expansion. 

 

EXAMPLES:: 

 

sage: A.<s> = AsymptoticRing(growth_group='s^QQ', coefficient_ring=QQ) 

sage: s.sqrt() 

s^(1/2) 

sage: a = (1 + 1/s).sqrt(precision=6); a 

1 + 1/2*s^(-1) - 1/8*s^(-2) + 1/16*s^(-3) 

- 5/128*s^(-4) + 7/256*s^(-5) + O(s^(-6)) 

 

.. SEEALSO:: 

 

:meth:`pow`, :meth:`rpow`, :meth:`exp`. 

 

TESTS:: 

 

sage: P.<p> = PowerSeriesRing(QQ, default_prec=6) 

sage: bool(SR(a.exact_part()).subs(s=1/x) - 

....: SR((1+p).sqrt().polynomial()).subs(p=x) == 0) 

True 

""" 

from sage.rings.rational_field import QQ 

return self.pow(QQ(1)/QQ(2), precision=precision) 

 

 

def O(self): 

r""" 

Convert all terms in this asymptotic expansion to `O`-terms. 

 

INPUT: 

 

Nothing. 

 

OUTPUT: 

 

An asymptotic expansion. 

 

EXAMPLES:: 

 

sage: AR.<x> = AsymptoticRing(growth_group='x^ZZ', coefficient_ring=ZZ) 

sage: O(x) 

O(x) 

sage: type(O(x)) 

<class 'sage.rings.asymptotic.asymptotic_ring.AsymptoticRing_with_category.element_class'> 

sage: expr = 42*x^42 + x^10 + O(x^2); expr 

42*x^42 + x^10 + O(x^2) 

sage: expr.O() 

O(x^42) 

sage: (2*x).O() 

O(x) 

 

.. SEEALSO:: 

 

:func:`sage.rings.power_series_ring.PowerSeriesRing`, 

:func:`sage.rings.laurent_series_ring.LaurentSeriesRing`. 

 

TESTS:: 

 

sage: AR(0).O() 

Traceback (most recent call last): 

... 

NotImplementedOZero: The error term in the result is O(0) 

which means 0 for sufficiently large x. 

""" 

if not self.summands: 

from .misc import NotImplementedOZero 

raise NotImplementedOZero(self.parent()) 

return sum(self.parent().create_summand('O', growth=element) 

for element in self.summands.maximal_elements()) 

 

 

def log(self, base=None, precision=None): 

r""" 

The logarithm of this asymptotic expansion. 

 

INPUT: 

 

- ``base`` -- the base of the logarithm. If ``None`` 

(default value) is used, the natural logarithm is taken. 

 

- ``precision`` -- the precision used for truncating the 

expansion. If ``None`` (default value) is used, the 

default precision of the parent is used. 

 

OUTPUT: 

 

An asymptotic expansion. 

 

.. NOTE:: 

 

Computing the logarithm of an asymptotic expansion 

is possible if and only if there is exactly one maximal 

summand in the expansion. 

 

ALGORITHM: 

 

If the expansion has more than one summand, 

the asymptotic expansion for `\log(1+t)` as `t` tends to `0` 

is used. 

 

.. TODO:: 

 

As soon as `L`-terms are implemented, this 

implementation has to be adapted as well in order to 

yield correct results. 

 

EXAMPLES:: 

 

sage: R.<x> = AsymptoticRing(growth_group='x^ZZ * log(x)^ZZ', coefficient_ring=QQ) 

sage: log(x) 

log(x) 

sage: log(x^2) 

2*log(x) 

sage: log(x-1) 

log(x) - x^(-1) - 1/2*x^(-2) - 1/3*x^(-3) - ... + O(x^(-21)) 

 

TESTS:: 

 

sage: log(R(1)) 

0 

sage: log(R(0)) 

Traceback (most recent call last): 

... 

ArithmeticError: Cannot compute log(0) in 

Asymptotic Ring <x^ZZ * log(x)^ZZ> over Rational Field. 

sage: C.<s, t> = AsymptoticRing(growth_group='s^ZZ * t^ZZ', coefficient_ring=QQ) 

sage: log(s + t) 

Traceback (most recent call last): 

... 

ValueError: Cannot determine main term of s + t since 

there are several maximal elements s, t. 

""" 

P = self.parent() 

 

if not self.summands: 

raise ArithmeticError('Cannot compute log(0) in %s.' % (self.parent(),)) 

 

elif len(self.summands) == 1: 

if self.is_one(): 

return P.zero() 

element = next(self.summands.elements()) 

return sum(P._create_element_in_extension_(l, element.parent()) 

for l in element.log_term(base=base)) 

 

(max_elem, x) = self._main_term_relative_error_() 

geom = -x 

 

from sage.rings.integer_ring import ZZ 

import itertools 

 

result = - AsymptoticExpansion._power_series_( 

coefficients=iter(1 / ZZ(k) 

for k in itertools.count(2)), 

start=geom, 

ratio=geom, 

ratio_start=geom, 

precision=precision) 

 

result += x.parent()(max_elem).log() 

if base: 

from sage.functions.log import log 

result = result / log(base) 

return result 

 

 

def is_exact(self): 

r""" 

Return whether all terms of this expansion are exact. 

 

OUTPUT: 

 

A boolean. 

 

EXAMPLES:: 

 

sage: A.<x> = AsymptoticRing('x^QQ * log(x)^QQ', QQ) 

sage: (x^2 + O(x)).is_exact() 

False 

sage: (x^2 - x).is_exact() 

True 

 

TESTS:: 

 

sage: A(0).is_exact() 

True 

sage: A.one().is_exact() 

True 

""" 

return all(T.is_exact() for T in self.summands) 

 

 

def is_little_o_of_one(self): 

r""" 

Return whether this expansion is of order `o(1)`. 

 

INPUT: 

 

Nothing. 

 

OUTPUT: 

 

A boolean. 

 

EXAMPLES:: 

 

sage: A.<x> = AsymptoticRing('x^ZZ * log(x)^ZZ', QQ) 

sage: (x^4 * log(x)^(-2) + x^(-4) * log(x)^2).is_little_o_of_one() 

False 

sage: (x^(-1) * log(x)^1234 + x^(-2) + O(x^(-3))).is_little_o_of_one() 

True 

sage: (log(x) - log(x-1)).is_little_o_of_one() 

True 

 

:: 

 

sage: A.<x, y> = AsymptoticRing('x^QQ * y^QQ * log(y)^ZZ', QQ) 

sage: (x^(-1/16) * y^32 + x^32 * y^(-1/16)).is_little_o_of_one() 

False 

sage: (x^(-1) * y^(-3) + x^(-3) * y^(-1)).is_little_o_of_one() 

True 

sage: (x^(-1) * y / log(y)).is_little_o_of_one() 

False 

sage: (log(y-1)/log(y) - 1).is_little_o_of_one() 

True 

""" 

return all(term.is_little_o_of_one() for term in self.summands.maximal_elements()) 

 

 

def rpow(self, base, precision=None): 

r""" 

Return the power of ``base`` to this asymptotic expansion. 

 

INPUT: 

 

- ``base`` -- an element or ``'e'``. 

 

- ``precision`` -- the precision used for truncating the 

expansion. If ``None`` (default value) is used, the 

default precision of the parent is used. 

 

OUTPUT: 

 

An asymptotic expansion. 

 

EXAMPLES:: 

 

sage: A.<x> = AsymptoticRing('x^ZZ', QQ) 

sage: (1/x).rpow('e', precision=5) 

1 + x^(-1) + 1/2*x^(-2) + 1/6*x^(-3) + 1/24*x^(-4) + O(x^(-5)) 

 

TESTS:: 

 

sage: x.rpow(SR.var('y')) 

Traceback (most recent call last): 

... 

ArithmeticError: Cannot construct y^x in Growth Group x^ZZ 

> *previous* TypeError: unsupported operand parent(s) for *: 

'Growth Group x^ZZ' and 'Growth Group SR^x' 

 

Check that :trac:`19946` is fixed:: 

 

sage: A.<n> = AsymptoticRing('QQ^n * n^QQ', SR) 

sage: n.rpow(2) 

2^n 

sage: _.parent() 

Asymptotic Ring <QQ^n * n^SR> over Symbolic Ring 

""" 

if isinstance(base, AsymptoticExpansion): 

return base.__pow__(self, precision=precision) 

 

P = self.parent() 

 

# first: remove terms from a copy of this term such that a 

# term in o(1) remains 

 

expr_o = self.summands.copy() 

large_terms = [] 

for term in self.summands.elements_topological(): 

if not term.is_little_o_of_one(): 

large_terms.append(term) 

expr_o.remove(term.growth) 

 

expr_o = P(expr_o) 

 

# next: try to take the exponential function of the large elements 

 

try: 

large_result = P.prod( 

P._create_element_in_extension_(term.rpow(base), 

term.parent()) 

for term in large_terms) 

except (TypeError, ValueError) as e: 

from .misc import combine_exceptions 

raise combine_exceptions( 

ValueError('Cannot construct the power of %s to the ' 

'exponent %s in %s.' % 

(base, self, self.parent())), e) 

 

# then: expand expr_o 

 

if not expr_o: 

return large_result 

 

 

if base == 'e': 

geom = expr_o 

else: 

from sage.functions.log import log 

geom = expr_o * log(base) 

P = geom.parent() 

 

from sage.rings.integer_ring import ZZ 

import itertools 

 

def inverted_factorials(): 

f = ZZ(1) 

for k in itertools.count(1): 

f /= ZZ(k) 

yield f 

 

result = AsymptoticExpansion._power_series_( 

coefficients=inverted_factorials(), 

start=P.one(), 

ratio=geom, 

ratio_start=P.one(), 

precision=precision) 

 

return result * large_result 

 

 

def _main_term_relative_error_(self, return_inverse_main_term=False): 

r""" 

Split this asymptotic expansion into `m(1+x)` with `x=o(1)`. 

 

INPUT: 

 

- ``return_inverse_main_term`` -- (default: ``False``) a boolean. 

If set, then the pair `(m^{-1},x)` is returned instead of `(m,x)`. 

 

OUTPUT: 

 

A pair (``m``, ``x``) consisting of 

a :mod:`term <sage.rings.asymptotic.term_monoid>` ``m`` and 

an :class:`asymptotic expansion <AsymptoticExpansion>` ``x``. 

 

EXAMPLES:: 

 

sage: R.<n> = AsymptoticRing('n^ZZ', QQ) 

sage: ex = 2*n^2 + n + O(1/n) 

sage: (m, x) = ex._main_term_relative_error_() 

sage: m 

2*n^2 

sage: x 

1/2*n^(-1) + O(n^(-3)) 

sage: ex = 2*n^2 + n 

sage: (m, x) = ex._main_term_relative_error_() 

sage: m 

2*n^2 

sage: x 

1/2*n^(-1) 

sage: ex._main_term_relative_error_(return_inverse_main_term=True) 

(1/2*n^(-2), 1/2*n^(-1)) 

sage: R(0)._main_term_relative_error_() 

Traceback (most recent call last): 

... 

ArithmeticError: Cannot determine main term of 0. 

 

TESTS:: 

 

sage: R.<m, n> = AsymptoticRing('n^ZZ*m^ZZ', QQ) 

sage: (m + n)._main_term_relative_error_() 

Traceback (most recent call last): 

... 

ValueError: Cannot determine main term of m + n since 

there are several maximal elements m, n. 

""" 

if not self.summands: 

raise ArithmeticError("Cannot determine main term of 0.") 

 

max_elem = tuple(self.summands.maximal_elements()) 

if len(max_elem) != 1: 

raise ValueError('Cannot determine main term of {} since there ' 

'are several maximal elements {}.'.format( 

self, ', '.join(str(e) for e in 

sorted(max_elem, key=str)))) 

max_elem = max_elem[0] 

 

imax_elem = ~max_elem 

if imax_elem.parent() is max_elem.parent(): 

new_self = self 

else: 

new_self = self.parent()._create_element_in_extension_( 

imax_elem, max_elem.parent()).parent()(self) 

 

one = new_self.parent().one() 

x = - one + new_self._mul_term_(imax_elem) 

 

if return_inverse_main_term: 

return (imax_elem, x) 

else: 

return (max_elem, x) 

 

 

@staticmethod 

def _power_series_(coefficients, start, ratio, ratio_start, precision): 

r""" 

Return a taylor series. 

 

Let `c_k` be determined by the ``coefficients`` and set 

 

.. MATH:: 

 

s_k = c_k \cdot \mathit{ratio\_start} \cdot \mathit{ratio}^k. 

 

The result is 

 

.. MATH:: 

 

\mathit{start} + \sum_{k=1}^K s_k 

 

where `K` is chosen such that adding `s_{K+1}` does not change 

the result. 

 

INPUT: 

 

- ``coefficients`` -- an iterator. 

 

- ``start`` -- an asymptotic expansion. 

 

- ``ratio`` -- an asymptotic expansion. 

 

- ``ratio_start`` -- an asymptotic expansion. 

 

- ``precision`` -- a non-negative integer. All intermediate 

results are truncated to this precision. 

 

OUTPUT: 

 

An asymptotic expansion. 

 

TESTS:: 

 

sage: from sage.rings.asymptotic.asymptotic_ring import AsymptoticExpansion 

sage: from itertools import count 

sage: A.<g> = AsymptoticRing('g^ZZ', QQ) 

sage: AsymptoticExpansion._power_series_( 

....: coefficients=iter(ZZ(k) for k in count(1)), 

....: start=A(42), 

....: ratio=1/g, 

....: ratio_start=A(5), 

....: precision=4) 

42 + 5*g^(-1) + 10*g^(-2) + 15*g^(-3) + O(g^(-4)) 

sage: AsymptoticExpansion._power_series_( 

....: coefficients=iter(ZZ(k) for k in count(1)), 

....: start=A(42), 

....: ratio=1/g+O(1/g^2), 

....: ratio_start=A(5), 

....: precision=4) 

42 + 5*g^(-1) + O(g^(-2)) 

sage: AsymptoticExpansion._power_series_( 

....: coefficients=iter(ZZ(k) for k in count(1)), 

....: start=A(42), 

....: ratio=1/g+O(1/g^2), 

....: ratio_start=A(5), 

....: precision=1000000) 

42 + 5*g^(-1) + O(g^(-2)) 

""" 

result = start 

g = ratio_start 

for c in coefficients: 

g *= ratio 

new_result = (result + c*g).truncate(precision=precision) 

if new_result.has_same_summands(result): 

break 

result = new_result 

return result 

 

 

def exp(self, precision=None): 

r""" 

Return the exponential of (i.e., the power of `e` to) this asymptotic expansion. 

 

INPUT: 

 

- ``precision`` -- the precision used for truncating the 

expansion. If ``None`` (default value) is used, the 

default precision of the parent is used. 

 

OUTPUT: 

 

An asymptotic expansion. 

 

.. NOTE:: 

 

The exponential function of this expansion can only be 

computed exactly if the respective growth element can be 

constructed in the underlying growth group. 

 

ALGORITHM: 

 

If the corresponding growth can be constructed, return 

the exact exponential function. Otherwise, if this term 

is `o(1)`, try to expand the series and truncate 

according to the given precision. 

 

.. TODO:: 

 

As soon as `L`-terms are implemented, this 

implementation has to be adapted as well in order to 

yield correct results. 

 

EXAMPLES:: 

 

sage: A.<x> = AsymptoticRing('(e^x)^ZZ * x^ZZ * log(x)^ZZ', SR) 

sage: exp(x) 

e^x 

sage: exp(2*x) 

(e^x)^2 

sage: exp(x + log(x)) 

e^x*x 

 

:: 

 

sage: (x^(-1)).exp(precision=7) 

1 + x^(-1) + 1/2*x^(-2) + 1/6*x^(-3) + ... + O(x^(-7)) 

 

TESTS:: 

 

sage: A.<x> = AsymptoticRing('(e^x)^ZZ * x^QQ * log(x)^QQ', SR) 

sage: exp(log(x)) 

x 

sage: log(exp(x)) 

x 

 

:: 

 

sage: exp(x+1) 

e*e^x 

 

See :trac:`19521`:: 

 

sage: A.<n> = AsymptoticRing('n^ZZ', SR.subring(no_variables=True)) 

sage: exp(O(n^(-3))).parent() 

Asymptotic Ring <n^ZZ> over Symbolic Constants Subring 

""" 

return self.rpow('e', precision=precision) 

 

 

def substitute(self, rules=None, domain=None, **kwds): 

r""" 

Substitute the given ``rules`` in this asymptotic expansion. 

 

INPUT: 

 

- ``rules`` -- a dictionary. 

 

- ``kwds`` -- keyword arguments will be added to the 

substitution ``rules``. 

 

- ``domain`` -- (default: ``None``) a parent. The neutral 

elements `0` and `1` (rules for the keys ``'_zero_'`` and 

``'_one_'``, see note box below) are taken out of this 

domain. If ``None``, then this is determined automatically. 

 

OUTPUT: 

 

An object. 

 

.. NOTE:: 

 

The neutral element of the asymptotic ring is replaced by 

the value to the key ``'_zero_'``; the neutral element of 

the growth group is replaced by the value to the key 

``'_one_'``. 

 

EXAMPLES:: 

 

sage: A.<x> = AsymptoticRing(growth_group='(e^x)^QQ * x^ZZ * log(x)^ZZ', coefficient_ring=QQ, default_prec=5) 

 

:: 

 

sage: (e^x * x^2 + log(x)).subs(x=SR('s')) 

s^2*e^s + log(s) 

sage: _.parent() 

Symbolic Ring 

 

:: 

 

sage: (x^3 + x + log(x)).subs(x=x+5).truncate(5) 

x^3 + 15*x^2 + 76*x + log(x) + 130 + O(x^(-1)) 

sage: _.parent() 

Asymptotic Ring <(e^x)^QQ * x^ZZ * log(x)^ZZ> over Rational Field 

 

:: 

 

sage: (e^x * x^2 + log(x)).subs(x=2*x) 

4*(e^x)^2*x^2 + log(x) + log(2) 

sage: _.parent() 

Asymptotic Ring <(e^x)^QQ * x^QQ * log(x)^QQ> over Symbolic Ring 

 

:: 

 

sage: (x^2 + log(x)).subs(x=4*x+2).truncate(5) 

16*x^2 + 16*x + log(x) + 2*log(2) + 4 + 1/2*x^(-1) + O(x^(-2)) 

sage: _.parent() 

Asymptotic Ring <(e^x)^QQ * x^ZZ * log(x)^ZZ> over Symbolic Ring 

 

:: 

 

sage: (e^x * x^2 + log(x)).subs(x=RIF(pi)) 

229.534211738584? 

sage: _.parent() 

Real Interval Field with 53 bits of precision 

 

.. SEEALSO:: 

 

:meth:`sage.symbolic.expression.Expression.subs` 

 

TESTS:: 

 

sage: x.subs({'y': -1}) 

Traceback (most recent call last): 

... 

ValueError: Cannot substitute y in x since it is not a generator of 

Asymptotic Ring <(e^x)^QQ * x^ZZ * log(x)^ZZ> over Rational Field. 

sage: B.<u, v, w> = AsymptoticRing(growth_group='u^QQ * v^QQ * w^QQ', coefficient_ring=QQ) 

sage: (1/u).subs({'u': 0}) 

Traceback (most recent call last): 

... 

TypeError: Cannot apply the substitution rules {u: 0} on u^(-1) in 

Asymptotic Ring <u^QQ * v^QQ * w^QQ> over Rational Field. 

> *previous* ZeroDivisionError: Cannot substitute in u^(-1) in 

Asymptotic Ring <u^QQ * v^QQ * w^QQ> over Rational Field. 

>> *previous* ZeroDivisionError: Cannot substitute in u^(-1) in 

Exact Term Monoid u^QQ * v^QQ * w^QQ with coefficients in Rational Field. 

>...> *previous* ZeroDivisionError: Cannot substitute in u^(-1) in 

Growth Group u^QQ * v^QQ * w^QQ. 

>...> *previous* ZeroDivisionError: Cannot substitute in u^(-1) in 

Growth Group u^QQ. 

>...> *previous* ZeroDivisionError: rational division by zero 

sage: (1/u).subs({'u': 0, 'v': SR.var('v')}) 

Traceback (most recent call last): 

... 

TypeError: Cannot apply the substitution rules {u: 0, v: v} on u^(-1) in 

Asymptotic Ring <u^QQ * v^QQ * w^QQ> over Rational Field. 

> *previous* ZeroDivisionError: Cannot substitute in u^(-1) in 

Asymptotic Ring <u^QQ * v^QQ * w^QQ> over Rational Field. 

>> *previous* ZeroDivisionError: Cannot substitute in u^(-1) in 

Exact Term Monoid u^QQ * v^QQ * w^QQ with coefficients in Rational Field. 

>...> *previous* ZeroDivisionError: Cannot substitute in u^(-1) in 

Growth Group u^QQ * v^QQ * w^QQ. 

>...> *previous* ZeroDivisionError: Cannot substitute in u^(-1) in 

Growth Group u^QQ. 

>...> *previous* ZeroDivisionError: rational division by zero 

 

:: 

 

sage: u.subs({u: 0, 'v': SR.var('v')}) 

0 

sage: v.subs({u: 0, 'v': SR.var('v')}) 

v 

sage: _.parent() 

Symbolic Ring 

 

:: 

 

sage: u.subs({SR.var('u'): -1}) 

Traceback (most recent call last): 

... 

TypeError: Cannot substitute u in u since it is neither an 

asymptotic expansion nor a string 

(but a <type 'sage.symbolic.expression.Expression'>). 

 

:: 

 

sage: u.subs({u: 1, 'u': 1}) 

1 

sage: u.subs({u: 1}, u=1) 

1 

sage: u.subs({u: 1, 'u': 2}) 

Traceback (most recent call last): 

... 

ValueError: Cannot substitute in u: duplicate key u. 

sage: u.subs({u: 1}, u=3) 

Traceback (most recent call last): 

... 

ValueError: Cannot substitute in u: duplicate key u. 

""" 

# check if nothing to do 

if not rules and not kwds: 

return self 

 

# init and process keyword arguments 

gens = self.parent().gens() 

locals = kwds or dict() 

 

# update with rules 

if isinstance(rules, dict): 

for k, v in iteritems(rules): 

if not isinstance(k, str) and k not in gens: 

raise TypeError('Cannot substitute %s in %s ' 

'since it is neither an ' 

'asymptotic expansion ' 

'nor a string (but a %s).' % 

(k, self, type(k))) 

k = str(k) 

if k in locals and locals[k] != v: 

raise ValueError('Cannot substitute in %s: ' 

'duplicate key %s.' % (self, k)) 

locals[k] = v 

elif rules is not None: 

raise TypeError('Substitution rules %s have to be a dictionary.' % 

(rules,)) 

 

# fill up missing rules 

for g in gens: 

locals.setdefault(str(g), g) 

 

# check if all keys are generators 

gens_str = tuple(str(g) for g in gens) 

for k in locals: 

if str(k) not in gens_str: 

raise ValueError('Cannot substitute %s in %s ' 

'since it is not a generator of %s.' % 

(k, self, self.parent())) 

 

# determine 0 and 1 

if domain is None and \ 

('_zero_' not in locals or '_one_' not in locals): 

P = self.parent() 

for g in gens: 

G = locals[str(g)].parent() 

if G is not P: 

domain = G 

break 

else: 

domain = P 

locals.setdefault('_zero_', domain.zero()) 

locals.setdefault('_one_', domain.one()) 

 

# do the actual substitution 

try: 

return self._substitute_(locals) 

except (ArithmeticError, TypeError, ValueError) as e: 

from .misc import combine_exceptions 

rules = '{' + ', '.join( 

'%s: %s' % (k, v) 

for k, v in sorted(iteritems(locals), 

key=lambda k: str(k[0])) 

if not k.startswith('_') and 

not any(k == str(g) and v is g for g in gens)) + '}' 

raise combine_exceptions( 

TypeError('Cannot apply the substitution rules %s on %s ' 

'in %s.' % (rules, self, self.parent())), e) 

 

subs = substitute 

 

def _substitute_(self, rules): 

r""" 

Substitute the given ``rules`` in this asymptotic expansion. 

 

INPUT: 

 

- ``rules`` -- a dictionary. 

The neutral element of the asymptotic ring is replaced by the value 

to key ``'_zero_'``. 

 

OUTPUT: 

 

An object. 

 

TESTS:: 

 

sage: A.<z> = AsymptoticRing(growth_group='z^QQ', coefficient_ring=QQ) 

sage: z._substitute_({'z': SR.var('a')}) 

a 

sage: _.parent() 

Symbolic Ring 

sage: A(0)._substitute_({'_zero_': 'zero'}) 

'zero' 

sage: (1/z)._substitute_({'z': 4}) 

1/4 

sage: _.parent() 

Rational Field 

sage: (1/z)._substitute_({'z': 0}) 

Traceback (most recent call last): 

... 

ZeroDivisionError: Cannot substitute in z^(-1) in 

Asymptotic Ring <z^QQ> over Rational Field. 

> *previous* ZeroDivisionError: Cannot substitute in z^(-1) in 

Exact Term Monoid z^QQ with coefficients in Rational Field. 

>> *previous* ZeroDivisionError: Cannot substitute in z^(-1) in 

Growth Group z^QQ. 

>...> *previous* ZeroDivisionError: rational division by zero 

""" 

if not self.summands: 

return rules['_zero_'] 

from sage.symbolic.operators import add_vararg 

try: 

return add_vararg( 

*tuple(s._substitute_(rules) 

for s in self.summands.elements_topological())) 

except (ArithmeticError, TypeError, ValueError) as e: 

from .misc import substitute_raise_exception 

substitute_raise_exception(self, e) 

 

 

def compare_with_values(self, variable, function, values, 

rescaled=True, ring=RIF): 

""" 

Compute the (rescaled) difference between this asymptotic 

expansion and the given values. 

 

INPUT: 

 

- ``variable`` -- an asymptotic expansion or a string. 

 

- ``function`` -- a callable or symbolic expression giving the 

comparison values. 

 

- ``values`` -- a list or iterable of values where the comparison 

shall be carried out. 

 

- ``rescaled`` -- (default: ``True``) determines whether 

the difference is divided by the error term of the asymptotic 

expansion. 

 

- ``ring`` -- (default: ``RIF``) the parent into which the 

difference is converted. 

 

OUTPUT: 

 

A list of pairs containing comparison points and (rescaled) 

difference values. 

 

EXAMPLES:: 

 

sage: A.<n> = AsymptoticRing('QQ^n * n^ZZ', SR) 

sage: catalan = binomial(2*x, x)/(x+1) 

sage: expansion = 4^n*(1/sqrt(pi)*n^(-3/2) 

....: - 9/8/sqrt(pi)*n^(-5/2) 

....: + 145/128/sqrt(pi)*n^(-7/2) + O(n^(-9/2))) 

sage: expansion.compare_with_values(n, catalan, srange(5, 10)) 

[(5, 0.5303924444775?), 

(6, 0.5455279498787?), 

(7, 0.556880411050?), 

(8, 0.565710587724?), 

(9, 0.572775029098?)] 

sage: expansion.compare_with_values(n, catalan, [5, 10, 20], rescaled=False) 

[(5, 0.3886263699387?), (10, 19.1842458318?), (20, 931314.63637?)] 

sage: expansion.compare_with_values(n, catalan, [5, 10, 20], rescaled=False, ring=SR) 

[(5, 168/5*sqrt(5)/sqrt(pi) - 42), 

(10, 1178112/125*sqrt(10)/sqrt(pi) - 16796), 

(20, 650486218752/125*sqrt(5)/sqrt(pi) - 6564120420)] 

 

Instead of a symbolic expression, a callable function can 

be specified as well:: 

 

sage: A.<n> = AsymptoticRing('n^ZZ * log(n)^ZZ', SR) 

sage: def H(n): 

....: return sum(1/k for k in srange(1, n+1)) 

sage: H_expansion = (log(n) + euler_gamma + 1/(2*n) 

....: - 1/(12*n^2) + O(n^-4)) 

sage: H_expansion.compare_with_values(n, H, srange(25, 30)) # rel tol 1e-6 

[(25, -0.008326995?), 

(26, -0.008327472?), 

(27, -0.008327898?), 

(28, -0.00832828?), 

(29, -0.00832862?)] 

 

.. SEEALSO:: 

 

:meth:`plot_comparison` 

 

TESTS:: 

 

sage: A.<x, y> = AsymptoticRing('x^ZZ*y^ZZ', QQ) 

sage: expansion = x^2 + O(x) + O(y) 

sage: expansion.compare_with_values(y, lambda z: z^2, srange(20, 30)) 

Traceback (most recent call last): 

.... 

NotImplementedError: exactly one error term required 

sage: expansion = x^2 

sage: expansion.compare_with_values(y, lambda z: z^2, srange(20, 30)) 

Traceback (most recent call last): 

.... 

NotImplementedError: exactly one error term required 

sage: expansion = x^2 + O(x) 

sage: expansion.compare_with_values(y, lambda z: z^2, srange(20, 30)) 

Traceback (most recent call last): 

.... 

NameError: name 'x' is not defined 

sage: expansion.compare_with_values(x, lambda z: z^2, srange(20, 30)) 

[(20, 0), (21, 0), ..., (29, 0)] 

sage: expansion.compare_with_values(x, SR('x*y'), srange(20, 30)) 

Traceback (most recent call last): 

.... 

NotImplementedError: expression x*y has more than one variable 

""" 

from .term_monoid import OTerm 

from sage.rings.integer_ring import ZZ 

 

main = self.exact_part() 

error = self - main 

error_terms = list(error.summands) 

if len(error_terms) != 1: 

raise NotImplementedError("exactly one error term required") 

if not isinstance(error_terms[0], OTerm): 

raise NotImplementedError("{} is not an O term".format(error)) 

error_growth = error_terms[0].growth 

 

if hasattr(function, 'variables'): 

expr = function 

vars = expr.variables() 

if len(vars) > 1: 

raise NotImplementedError("expression {} has more than one " 

"variable".format(expr)) 

elif len(vars) == 1: 

v = vars[0] 

def function(arg): 

return expr.subs({v: arg}) 

else: 

def function(arg): 

return expr 

 

if rescaled: 

points = list( 

(k, ring((main.subs({variable: k}) - function(k)) / 

error_growth._substitute_({str(variable): k, 

'_one_': ZZ(1)}))) 

for k in values) 

else: 

points = list( 

(k, ring(main.subs({variable: k}) - function(k))) 

for k in values) 

 

return points 

 

 

def plot_comparison(self, variable, function, values, rescaled=True, 

ring=RIF, relative_tolerance=0.025, **kwargs): 

r""" 

Plot the (rescaled) difference between this asymptotic 

expansion and the given values. 

 

INPUT: 

 

- ``variable`` -- an asymptotic expansion or a string. 

 

- ``function`` -- a callable or symbolic expression giving the 

comparison values. 

 

- ``values`` -- a list or iterable of values where the comparison 

shall be carried out. 

 

- ``rescaled`` -- (default: ``True``) determines whether 

the difference is divided by the error term of the asymptotic 

expansion. 

 

- ``ring`` -- (default: ``RIF``) the parent into which the 

difference is converted. 

 

- ``relative_tolerance`` -- (default: ``0.025``). Raise error 

when relative error exceeds this tolerance. 

 

Other keyword arguments are passed to :func:`list_plot`. 

 

OUTPUT: 

 

A graphics object. 

 

.. NOTE:: 

 

If rescaled (i.e. divided by the error term), the output 

should be bounded. 

 

This method is mainly meant to have an easily usable 

plausability check for asymptotic expansion created in 

some way. 

 

EXAMPLES: 

 

We want to check the quality of the asymptotic expansion of 

the harmonic numbers:: 

 

sage: A.<n> = AsymptoticRing('n^ZZ * log(n)^ZZ', SR) 

sage: def H(n): 

....: return sum(1/k for k in srange(1, n+1)) 

sage: H_expansion = (log(n) + euler_gamma + 1/(2*n) 

....: - 1/(12*n^2) + O(n^-4)) 

sage: H_expansion.plot_comparison(n, H, srange(1, 30)) 

Graphics object consisting of 1 graphics primitive 

 

Alternatively, the unscaled (absolute) difference can be 

plotted as well:: 

 

sage: H_expansion.plot_comparison(n, H, srange(1, 30), 

....: rescaled=False) 

Graphics object consisting of 1 graphics primitive 

 

Additional keywords are passed to :func:`list_plot`:: 

 

sage: H_expansion.plot_comparison(n, H, srange(1, 30), 

....: plotjoined=True, marker='o', 

....: color='green') 

Graphics object consisting of 1 graphics primitive 

 

.. SEEALSO:: 

 

:meth:`compare_with_values` 

 

TESTS:: 

 

sage: H_expansion.plot_comparison(n, H, [600]) 

Traceback (most recent call last): 

... 

ValueError: Numerical noise is too high, the comparison is inaccurate 

sage: H_expansion.plot_comparison(n, H, [600], relative_tolerance=2) 

Graphics object consisting of 1 graphics primitive 

""" 

from sage.plot.plot import list_plot 

points = self.compare_with_values(variable, function, 

values, rescaled=rescaled, ring=ring) 

 

from sage.rings.real_mpfi import RealIntervalField_class 

if isinstance(ring, RealIntervalField_class): 

if not all(p[1].relative_diameter() <= relative_tolerance for p in points): 

raise ValueError('Numerical noise is too high, the ' 

'comparison is inaccurate') 

 

# RIFs cannot be plotted, they need to be converted to RR 

# (see #15011). 

points = [(p[0], p[1].center()) for p in points] 

 

return list_plot(points, **kwargs) 

 

 

def symbolic_expression(self, R=None): 

r""" 

Return this asymptotic expansion as a symbolic expression. 

 

INPUT: 

 

- ``R`` -- (a subring of) the symbolic ring or ``None``. 

The output is will be an element of ``R``. If ``None``, 

then the symbolic ring is used. 

 

OUTPUT: 

 

A symbolic expression. 

 

EXAMPLES:: 

 

sage: A.<x, y, z> = AsymptoticRing(growth_group='x^ZZ * y^QQ * log(y)^QQ * QQ^z * z^QQ', coefficient_ring=QQ) 

sage: SR(A.an_element()) # indirect doctest 

1/8*(1/8)^z*x^3*y^(3/2)*z^(3/2)*log(y)^(3/2) + 

Order((1/2)^z*x*sqrt(y)*sqrt(z)*sqrt(log(y))) 

 

TESTS:: 

 

sage: a = A.an_element(); a 

1/8*x^3*y^(3/2)*log(y)^(3/2)*(1/8)^z*z^(3/2) + 

O(x*y^(1/2)*log(y)^(1/2)*(1/2)^z*z^(1/2)) 

sage: a.symbolic_expression() 

1/8*(1/8)^z*x^3*y^(3/2)*z^(3/2)*log(y)^(3/2) + 

Order((1/2)^z*x*sqrt(y)*sqrt(z)*sqrt(log(y))) 

sage: _.parent() 

Symbolic Ring 

 

:: 

 

sage: from sage.symbolic.ring import SymbolicRing 

sage: class MySymbolicRing(SymbolicRing): 

....: pass 

sage: mySR = MySymbolicRing() 

sage: a.symbolic_expression(mySR).parent() is mySR 

True 

""" 

if R is None: 

from sage.symbolic.ring import SR 

R = SR 

 

return self.substitute(dict((g, R(R.var(str(g)))) 

for g in self.parent().gens()), 

domain=R) 

 

 

_symbolic_ = symbolic_expression # will be used by SR._element_constructor_ 

 

 

def map_coefficients(self, f, new_coefficient_ring=None): 

r""" 

Return the asymptotic expansion obtained by applying ``f`` to 

each coefficient of this asymptotic expansion. 

 

INPUT: 

 

- ``f`` -- a callable. A coefficient `c` will be mapped to `f(c)`. 

 

- ``new_coefficient_ring`` -- (default: ``None``) a ring. 

 

OUTPUT: 

 

An asymptotic expansion. 

 

EXAMPLES:: 

 

sage: A.<n> = AsymptoticRing(growth_group='n^ZZ', coefficient_ring=ZZ) 

sage: a = n^4 + 2*n^3 + 3*n^2 + O(n) 

sage: a.map_coefficients(lambda c: c+1) 

2*n^4 + 3*n^3 + 4*n^2 + O(n) 

sage: a.map_coefficients(lambda c: c-2) 

-n^4 + n^2 + O(n) 

 

TESTS:: 

 

sage: a.map_coefficients(lambda c: 1/c, new_coefficient_ring=QQ) 

n^4 + 1/2*n^3 + 1/3*n^2 + O(n) 

sage: _.parent() 

Asymptotic Ring <n^ZZ> over Rational Field 

sage: a.map_coefficients(lambda c: 1/c) 

Traceback (most recent call last): 

... 

ValueError: ... is not a coefficient in 

Exact Term Monoid n^ZZ with coefficients in Integer Ring. 

""" 

def mapping(term): 

T = term.parent().change_parameter( 

coefficient_ring=new_coefficient_ring) 

if hasattr(term, 'coefficient'): 

c = f(term.coefficient) 

if c.is_zero(): 

return None 

return T(term.growth, c) 

else: 

return T(term.growth) 

 

P = self.parent().change_parameter(coefficient_ring=new_coefficient_ring) 

S = self.summands.copy() 

S.map(mapping) 

return P(S, simplify=False, convert=False) 

 

 

def factorial(self): 

r""" 

Return the factorial of this asymptotic expansion. 

 

OUTPUT: 

 

An asymptotic expansion. 

 

EXAMPLES:: 

 

sage: A.<n> = AsymptoticRing(growth_group='n^ZZ * log(n)^ZZ', coefficient_ring=ZZ, default_prec=5) 

sage: n.factorial() 

sqrt(2)*sqrt(pi)*e^(n*log(n))*(e^n)^(-1)*n^(1/2) 

+ 1/12*sqrt(2)*sqrt(pi)*e^(n*log(n))*(e^n)^(-1)*n^(-1/2) 

+ 1/288*sqrt(2)*sqrt(pi)*e^(n*log(n))*(e^n)^(-1)*n^(-3/2) 

+ O(e^(n*log(n))*(e^n)^(-1)*n^(-5/2)) 

sage: _.parent() 

Asymptotic Ring <(e^(n*log(n)))^(Symbolic Constants Subring) * 

(e^n)^(Symbolic Constants Subring) * 

n^(Symbolic Constants Subring) * 

log(n)^(Symbolic Constants Subring)> 

over Symbolic Constants Subring 

 

:wikipedia:`Catalan numbers <Catalan_number>` 

`\frac{1}{n+1}\binom{2n}{n}`:: 

 

sage: (2*n).factorial() / n.factorial()^2 / (n+1) # long time 

1/sqrt(pi)*(e^n)^(2*log(2))*n^(-3/2) 

- 9/8/sqrt(pi)*(e^n)^(2*log(2))*n^(-5/2) 

+ 145/128/sqrt(pi)*(e^n)^(2*log(2))*n^(-7/2) 

+ O((e^n)^(2*log(2))*n^(-9/2)) 

 

Note that this method substitutes the asymptotic expansion into 

Stirling's formula. This substitution has to be possible which is 

not always guaranteed:: 

 

sage: S.<s> = AsymptoticRing(growth_group='s^QQ * log(s)^QQ', coefficient_ring=QQ, default_prec=4) 

sage: log(s).factorial() 

Traceback (most recent call last): 

... 

TypeError: Cannot apply the substitution rules {s: log(s)} on 

sqrt(2)*sqrt(pi)*e^(s*log(s))*(e^s)^(-1)*s^(1/2) 

+ O(e^(s*log(s))*(e^s)^(-1)*s^(-1/2)) in 

Asymptotic Ring <(e^(s*log(s)))^QQ * (e^s)^QQ * s^QQ * log(s)^QQ> 

over Symbolic Constants Subring. 

... 

 

.. SEEALSO:: 

 

:meth:`~sage.rings.asymptotic.asymptotic_expansion_generators.AsymptoticExpansionGenerators.Stirling` 

 

TESTS:: 

 

sage: A.<m> = AsymptoticRing(growth_group='m^ZZ * log(m)^ZZ', coefficient_ring=QQ, default_prec=5) 

sage: m.factorial() 

sqrt(2)*sqrt(pi)*e^(m*log(m))*(e^m)^(-1)*m^(1/2) 

+ 1/12*sqrt(2)*sqrt(pi)*e^(m*log(m))*(e^m)^(-1)*m^(-1/2) 

+ 1/288*sqrt(2)*sqrt(pi)*e^(m*log(m))*(e^m)^(-1)*m^(-3/2) 

+ O(e^(m*log(m))*(e^m)^(-1)*m^(-5/2)) 

 

:: 

 

sage: A(1/2).factorial() 

1/2*sqrt(pi) 

sage: _.parent() 

Asymptotic Ring <m^ZZ * log(m)^ZZ> over Symbolic Ring 

 

:: 

 

sage: B.<a, b> = AsymptoticRing('a^ZZ * b^ZZ', QQ, default_prec=3) 

sage: b.factorial() 

O(e^(b*log(b))*(e^b)^(-1)*b^(1/2)) 

sage: (a*b).factorial() 

Traceback (most recent call last): 

... 

ValueError: Cannot build the factorial of a*b 

since it is not univariate. 

""" 

vars = self.variable_names() 

 

if len(vars) == 0: 

if self.is_zero(): 

return self.parent().one() 

assert len(self.summands) == 1 

element = next(self.summands.elements()) 

return self.parent()._create_element_in_extension_( 

element._factorial_(), element.parent()) 

 

if len(vars) == 1: 

from .asymptotic_expansion_generators import \ 

asymptotic_expansions 

var = vars[0] 

S = asymptotic_expansions.Stirling( 

var, precision=self.parent().default_prec) 

from sage.structure.element import get_coercion_model 

cm = get_coercion_model() 

P = cm.common_parent(self, S) 

return S.subs({var: P.coerce(self)}) 

 

else: 

raise ValueError( 

'Cannot build the factorial of {} since it is not ' 

'univariate.'.format(self)) 

 

 

def variable_names(self): 

r""" 

Return the names of the variables of this asymptotic expansion. 

 

OUTPUT: 

 

A tuple of strings. 

 

EXAMPLES:: 

 

sage: A.<m, n> = AsymptoticRing('QQ^m * m^QQ * n^ZZ * log(n)^ZZ', QQ) 

sage: (4*2^m*m^4*log(n)).variable_names() 

('m', 'n') 

sage: (4*2^m*m^4).variable_names() 

('m',) 

sage: (4*log(n)).variable_names() 

('n',) 

sage: (4*m^3).variable_names() 

('m',) 

sage: (4*m^0).variable_names() 

() 

sage: (4*2^m*m^4 + log(n)).variable_names() 

('m', 'n') 

sage: (2^m + m^4 + log(n)).variable_names() 

('m', 'n') 

sage: (2^m + m^4).variable_names() 

('m',) 

""" 

vars = sorted(sum(iter(s.variable_names() 

for s in self.summands), 

tuple())) 

from itertools import groupby 

return tuple(v for v, _ in groupby(vars)) 

 

 

def _singularity_analysis_(self, var, zeta, precision=None): 

r""" 

Return the asymptotic growth of the coefficients of some 

generating function having this singular expansion around `\zeta`. 

 

INPUT: 

 

- ``var`` -- a string, the variable for the growth of the coefficients, 

or the generator of an asymptotic ring. 

 

- ``zeta`` -- location of the singularity 

 

- ``precision`` -- (default: ``None``) an integer. If ``None``, then 

the default precision of the parent of this expansion is used. 

 

OUTPUT: 

 

An asymptotic expansion in ``var``. 

 

EXAMPLES:: 

 

sage: C.<T> = AsymptoticRing('T^QQ', QQ) 

sage: ex = 2 - 2*T^(-1/2) + 2*T^(-1) - 2*T^(-3/2) + O(T^(-2)) 

sage: ex._singularity_analysis_('n', 1/4, precision=2) 

1/sqrt(pi)*4^n*n^(-3/2) - 9/8/sqrt(pi)*4^n*n^(-5/2) + O(4^n*n^(-3)) 

 

The parameter ``var`` can also be the generator of an asymptotic 

ring:: 

 

sage: A.<n> = AsymptoticRing('n^QQ', QQ) 

sage: ex._singularity_analysis_(n, 1/4, precision=2) 

1/sqrt(pi)*4^n*n^(-3/2) - 9/8/sqrt(pi)*4^n*n^(-5/2) + O(4^n*n^(-3)) 

 

If the parameter ``precision`` is omitted, the default precision 

of the parent of this expansion is used. :: 

 

sage: C.<T> = AsymptoticRing('T^QQ', QQ, default_prec=1) 

sage: ex = 2 - 2*T^(-1/2) + 2*T^(-1) - 2*T^(-3/2) + O(T^(-2)) 

sage: ex._singularity_analysis_('n', 1/4) 

1/sqrt(pi)*4^n*n^(-3/2) + O(4^n*n^(-5/2)) 

 

.. SEEALSO:: 

 

:meth:`AsymptoticRing.coefficients_of_generating_function` 

 

.. WARNING:: 

 

Once singular expansions around points other than infinity 

are implemented (:trac:`20050`), this method will be 

renamed to ``singularity_analysis``, the parameter 

``zeta`` will be dropped (as it will be part of the 

singular expansion) and expansions around infinity will no 

longer be accepted. 

 

TESTS:: 

 

sage: C.<T> = AsymptoticRing('T^QQ', QQ) 

sage: (1/T)._singularity_analysis_('n', 1) 

Traceback (most recent call last): 

... 

NotImplementedOZero: T^(-1) 

""" 

from .misc import NotImplementedOZero 

OZeroEncountered = False 

 

if precision is None: 

precision = self.parent().default_prec 

 

result = 0 

for s in self.summands: 

try: 

contribution = s._singularity_analysis_( 

var=var, zeta=zeta, 

precision=precision) 

except NotImplementedOZero: 

OZeroEncountered = True 

else: 

result += contribution 

 

if OZeroEncountered and (isinstance(result, int) and result == 0 

or result.is_exact()): 

raise NotImplementedOZero(self) 

return result 

 

 

class AsymptoticRing(Algebra, UniqueRepresentation): 

r""" 

A ring consisting of :class:`asymptotic expansions <AsymptoticExpansion>`. 

 

INPUT: 

 

- ``growth_group`` -- either a partially ordered group (see 

:doc:`growth_group`) or a string 

describing such a growth group (see 

:class:`~sage.rings.asymptotic.growth_group.GrowthGroupFactory`). 

 

- ``coefficient_ring`` -- the ring which contains the 

coefficients of the expansions. 

 

- ``default_prec`` -- a positive integer. This is the number of 

summands that are kept before truncating an infinite series. 

 

- ``category`` -- the category of the parent can be specified 

in order to broaden the base structure. It has to be a 

subcategory of ``Category of rings``. This is also the default 

category if ``None`` is specified. 

 

EXAMPLES: 

 

We begin with the construction of an asymptotic ring in various 

ways. First, we simply pass a string specifying the underlying 

growth group:: 

 

sage: R1_x.<x> = AsymptoticRing(growth_group='x^QQ', coefficient_ring=QQ); R1_x 

Asymptotic Ring <x^QQ> over Rational Field 

sage: x 

x 

 

This is equivalent to the following code, which explicitly 

specifies the underlying growth group:: 

 

sage: from sage.rings.asymptotic.growth_group import GrowthGroup 

sage: G_QQ = GrowthGroup('x^QQ') 

sage: R2_x.<x> = AsymptoticRing(growth_group=G_QQ, coefficient_ring=QQ); R2_x 

Asymptotic Ring <x^QQ> over Rational Field 

 

Of course, the coefficient ring of the asymptotic ring and the 

base ring of the underlying growth group do not need to 

coincide:: 

 

sage: R_ZZ_x.<x> = AsymptoticRing(growth_group='x^QQ', coefficient_ring=ZZ); R_ZZ_x 

Asymptotic Ring <x^QQ> over Integer Ring 

 

Note, we can also create and use logarithmic growth groups:: 

 

sage: R_log = AsymptoticRing(growth_group='log(x)^ZZ', coefficient_ring=QQ); R_log 

Asymptotic Ring <log(x)^ZZ> over Rational Field 

 

Other growth groups are available. See :doc:`asymptotic_ring` for 

more examples. 

 

Below there are some technical details. 

 

According to the conventions for parents, uniqueness is ensured:: 

 

sage: R1_x is R2_x 

True 

 

Furthermore, the coercion framework is also involved. Coercion 

between two asymptotic rings is possible (given that the 

underlying growth groups and coefficient rings are chosen 

appropriately):: 

 

sage: R1_x.has_coerce_map_from(R_ZZ_x) 

True 

 

Additionally, for the sake of convenience, the coefficient ring 

also coerces into the asymptotic ring (representing constant 

quantities):: 

 

sage: R1_x.has_coerce_map_from(QQ) 

True 

 

TESTS:: 

 

sage: from sage.rings.asymptotic.asymptotic_ring import AsymptoticRing as AR_class 

sage: class AR(AR_class): 

....: class Element(AR_class.Element): 

....: __eq__ = AR_class.Element.has_same_summands 

sage: A = AR(growth_group='z^QQ', coefficient_ring=QQ) 

sage: from itertools import islice 

sage: TestSuite(A).run( # not tested # long time # see #19424 

....: verbose=True, 

....: elements=tuple(islice(A.some_elements(), 10)), 

....: skip=('_test_some_elements', # to many elements 

....: '_test_distributivity')) # due to cancellations: O(z) != O(z^2) 

""" 

 

# enable the category framework for elements 

Element = AsymptoticExpansion 

 

 

@staticmethod 

def __classcall__(cls, growth_group=None, coefficient_ring=None, 

names=None, category=None, default_prec=None): 

r""" 

Normalizes the input in order to ensure a unique 

representation of the parent. 

 

For more information see :class:`AsymptoticRing`. 

 

EXAMPLES: 

 

``__classcall__`` unifies the input to the constructor of 

:class:`AsymptoticRing` such that the instances generated 

are unique. Also, this enables the use of the generation 

framework:: 

 

sage: from sage.rings.asymptotic.growth_group import GrowthGroup 

sage: MG = GrowthGroup('x^ZZ') 

sage: AR1 = AsymptoticRing(growth_group=MG, coefficient_ring=ZZ) 

sage: AR2.<x> = AsymptoticRing(growth_group='x^ZZ', coefficient_ring=ZZ) 

sage: AR1 is AR2 

True 

 

The bracket notation can only be used if the growth group 

has a generator:: 

 

sage: AR.<lx> = AsymptoticRing(growth_group='log(x)^ZZ', coefficient_ring=ZZ) 

Traceback (most recent call last): 

... 

ValueError: Growth Group log(x)^ZZ does not provide any 

generators but name 'lx' given. 

 

The names of the generators have to agree with the names used in 

the growth group except for univariate rings:: 

 

sage: A.<icecream> = AsymptoticRing(growth_group='x^ZZ', coefficient_ring=ZZ); A 

Asymptotic Ring <x^ZZ> over Integer Ring 

sage: icecream 

x 

sage: A.<x, y> = AsymptoticRing(growth_group='x^ZZ * y^ZZ', coefficient_ring=ZZ); A 

Asymptotic Ring <x^ZZ * y^ZZ> over Integer Ring 

sage: A.<y, x> = AsymptoticRing(growth_group='x^ZZ * y^ZZ', coefficient_ring=ZZ) 

Traceback (most recent call last): 

... 

ValueError: Names 'y', 'x' do not coincide with 

generators 'x', 'y' of Growth Group x^ZZ * y^ZZ. 

sage: A.<a, b> = AsymptoticRing(growth_group='x^ZZ * y^ZZ', coefficient_ring=ZZ) 

Traceback (most recent call last): 

... 

ValueError: Names 'a', 'b' do not coincide with 

generators 'x', 'y' of Growth Group x^ZZ * y^ZZ. 

sage: A.<x, b> = AsymptoticRing(growth_group='x^ZZ * y^ZZ', coefficient_ring=ZZ) 

Traceback (most recent call last): 

... 

ValueError: Names 'x', 'b' do not coincide with 

generators 'x', 'y' of Growth Group x^ZZ * y^ZZ. 

sage: A.<x> = AsymptoticRing(growth_group='x^ZZ * y^ZZ', coefficient_ring=ZZ) 

Traceback (most recent call last): 

... 

ValueError: Name 'x' do not coincide with 

generators 'x', 'y' of Growth Group x^ZZ * y^ZZ. 

sage: A.<x, y, z> = AsymptoticRing(growth_group='x^ZZ * y^ZZ', coefficient_ring=ZZ) 

Traceback (most recent call last): 

... 

ValueError: Names 'x', 'y', 'z' do not coincide with 

generators 'x', 'y' of Growth Group x^ZZ * y^ZZ. 

 

TESTS:: 

 

sage: AsymptoticRing(growth_group=None, coefficient_ring=ZZ) 

Traceback (most recent call last): 

... 

ValueError: Growth group not specified. Cannot continue. 

sage: AsymptoticRing(growth_group='x^ZZ', coefficient_ring=None) 

Traceback (most recent call last): 

... 

ValueError: Coefficient ring not specified. Cannot continue. 

sage: AsymptoticRing(growth_group='x^ZZ', coefficient_ring='icecream') 

Traceback (most recent call last): 

... 

ValueError: icecream is not a ring. Cannot continue. 

""" 

from sage.categories.sets_cat import Sets 

from sage.categories.rings import Rings 

 

Sets_parent_class = Sets().parent_class 

while issubclass(cls, Sets_parent_class): 

cls = cls.__base__ 

 

if isinstance(growth_group, str): 

from .growth_group import GrowthGroup 

growth_group = GrowthGroup(growth_group) 

 

if growth_group is None: 

raise ValueError('Growth group not specified. Cannot continue.') 

 

if coefficient_ring is None: 

raise ValueError('Coefficient ring not specified. Cannot continue.') 

if coefficient_ring not in Rings(): 

raise ValueError('%s is not a ring. Cannot continue.' % (coefficient_ring,)) 

 

strgens = tuple(str(g) for g in growth_group.gens_monomial()) 

def format_names(N): 

return ('s ' if len(N) != 1 else ' ') + ', '.join("'%s'" % n for n in N) 

if names and not strgens: 

raise ValueError('%s does not provide any generators but name%s given.' % 

(growth_group, format_names(names))) 

elif names is not None and len(names) == 1 and len(strgens) == 1: 

pass 

elif names is not None and names != strgens: 

raise ValueError('Name%s do not coincide with generator%s of %s.' % 

(format_names(names), format_names(strgens), growth_group)) 

 

if category is None: 

from sage.categories.commutative_algebras import CommutativeAlgebras 

from sage.categories.rings import Rings 

category = CommutativeAlgebras(Rings()) 

 

if default_prec is None: 

default_prec = series_precision() 

 

return super(AsymptoticRing, 

cls).__classcall__(cls, growth_group, coefficient_ring, 

category=category, 

default_prec=default_prec) 

 

def __init__(self, growth_group, coefficient_ring, category, default_prec): 

r""" 

See :class:`AsymptoticRing` for more information. 

 

TESTS:: 

 

sage: R1 = AsymptoticRing(growth_group='x^ZZ', coefficient_ring=ZZ); R1 

Asymptotic Ring <x^ZZ> over Integer Ring 

sage: R2.<x> = AsymptoticRing(growth_group='x^QQ', coefficient_ring=QQ); R2 

Asymptotic Ring <x^QQ> over Rational Field 

sage: R1 is R2 

False 

 

:: 

 

sage: R3 = AsymptoticRing('x^ZZ') 

Traceback (most recent call last): 

... 

ValueError: Coefficient ring not specified. Cannot continue. 

""" 

self._coefficient_ring_ = coefficient_ring 

self._growth_group_ = growth_group 

self._default_prec_ = default_prec 

super(AsymptoticRing, self).__init__(base_ring=coefficient_ring, 

category=category) 

 

 

@property 

def growth_group(self): 

r""" 

The growth group of this asymptotic ring. 

 

EXAMPLES:: 

 

sage: AR = AsymptoticRing(growth_group='x^ZZ', coefficient_ring=ZZ) 

sage: AR.growth_group 

Growth Group x^ZZ 

 

.. SEEALSO:: 

 

:doc:`growth_group` 

""" 

return self._growth_group_ 

 

 

@property 

def coefficient_ring(self): 

r""" 

The coefficient ring of this asymptotic ring. 

 

EXAMPLES:: 

 

sage: AR = AsymptoticRing(growth_group='x^ZZ', coefficient_ring=ZZ) 

sage: AR.coefficient_ring 

Integer Ring 

""" 

return self._coefficient_ring_ 

 

 

@property 

def default_prec(self): 

r""" 

The default precision of this asymptotic ring. 

 

This is the parameter used to determine how many summands 

are kept before truncating an infinite series (which occur 

when inverting asymptotic expansions). 

 

EXAMPLES:: 

 

sage: AR = AsymptoticRing(growth_group='x^ZZ', coefficient_ring=ZZ) 

sage: AR.default_prec 

20 

sage: AR = AsymptoticRing('x^ZZ', ZZ, default_prec=123) 

sage: AR.default_prec 

123 

""" 

return self._default_prec_ 

 

 

def change_parameter(self, **kwds): 

r""" 

Return an asymptotic ring with a change in one or more of the given parameters. 

 

INPUT: 

 

- ``growth_group`` -- (default: ``None``) the new growth group. 

 

- ``coefficient_ring`` -- (default: ``None``) the new coefficient ring. 

 

- ``category`` -- (default: ``None``) the new category. 

 

- ``default_prec`` -- (default: ``None``) the new default precision. 

 

OUTPUT: 

 

An asymptotic ring. 

 

EXAMPLES:: 

 

sage: A = AsymptoticRing(growth_group='x^ZZ', coefficient_ring=ZZ) 

sage: A.change_parameter(coefficient_ring=QQ) 

Asymptotic Ring <x^ZZ> over Rational Field 

 

TESTS:: 

 

sage: A.change_parameter(coefficient_ring=ZZ) is A 

True 

sage: A.change_parameter(coefficient_ring=None) is A 

True 

""" 

parameters = ('growth_group', 'coefficient_ring', 'default_prec') 

values = dict() 

for parameter in parameters: 

default = getattr(self, parameter) 

values[parameter] = kwds.get(parameter, default) 

if values[parameter] is None: 

values[parameter] = default 

values['category'] = self.category() 

if isinstance(values['growth_group'], str): 

from .growth_group import GrowthGroup 

values['growth_group'] = GrowthGroup(values['growth_group']) 

if all(values[parameter] is getattr(self, parameter) 

for parameter in parameters) and values['category'] is self.category(): 

return self 

return self._underlying_class()(**values) 

 

@staticmethod 

def _create_empty_summands_(): 

r""" 

Create an empty data structure suitable for storing and working 

with summands. 

 

INPUT: 

 

Nothing. 

 

OUTPUT: 

 

A :class:`~sage.data_structures.mutable_poset.MutablePoset`. 

 

TESTS:: 

 

sage: AsymptoticRing._create_empty_summands_() 

poset() 

""" 

from sage.data_structures.mutable_poset import MutablePoset 

from .term_monoid import can_absorb, absorption 

return MutablePoset(key=lambda element: element.growth, 

can_merge=can_absorb, 

merge=absorption) 

 

 

def _create_element_in_extension_(self, term, old_term_parent=None): 

r""" 

Create an element in an extension of this asymptotic ring which 

is chosen according to the input. 

 

INPUT: 

 

- ``term`` -- the element data. 

 

- ``old_term_parent`` -- the parent of ``term`` is compared to this 

parent. If both are the same or ``old_parent`` is ``None``, 

then the result is an expansion in this (``self``) asymptotic ring. 

 

OUTPUT: 

 

An element. 

 

EXAMPLES:: 

 

sage: A = AsymptoticRing('z^ZZ', ZZ) 

sage: a = next(A.an_element().summands.elements_topological()) 

sage: B = AsymptoticRing('z^QQ', QQ) 

sage: b = next(B.an_element().summands.elements_topological()) 

sage: c = A._create_element_in_extension_(a, a.parent()) 

sage: next(c.summands.elements_topological()).parent() 

O-Term Monoid z^ZZ with implicit coefficients in Integer Ring 

sage: c = A._create_element_in_extension_(b, a.parent()) 

sage: next(c.summands.elements_topological()).parent() 

O-Term Monoid z^QQ with implicit coefficients in Rational Field 

 

TESTS:: 

 

sage: c = A._create_element_in_extension_(b, None) 

sage: next(c.summands.elements_topological()).parent() 

O-Term Monoid z^QQ with implicit coefficients in Rational Field 

""" 

if old_term_parent is None or term.parent() is old_term_parent: 

parent = self 

else: 

# Insert an 'if' here once terms can have different 

# coefficient rings, as this will be for L-terms. 

parent = self.change_parameter( 

growth_group=term.parent().growth_group, 

coefficient_ring=term.parent().coefficient_ring) 

return parent(term, simplify=False, convert=False) 

 

 

def _element_constructor_(self, data, simplify=True, convert=True): 

r""" 

Convert a given object to this asymptotic ring. 

 

INPUT: 

 

- ``data`` -- an object representing the element to be 

initialized. 

 

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

element is simplified (terms are absorbed) automatically. 

 

- ``convert`` -- (default: ``True``) passed on to the element 

constructor. If set, then the ``summands`` are converted to 

the asymptotic ring (the parent of this expansion). If not, 

then the summands are taken as they are. In that case, the 

caller must ensure that the parent of the terms is set 

correctly. 

 

OUTPUT: 

 

An element of this asymptotic ring. 

 

TESTS:: 

 

sage: AR = AsymptoticRing(growth_group='x^ZZ', coefficient_ring=ZZ) 

sage: AR(5) # indirect doctest 

5 

sage: AR(3*x^2) # indirect doctest 

3*x^2 

sage: x = ZZ['x'].gen(); x.parent() 

Univariate Polynomial Ring in x over Integer Ring 

sage: AR(x) 

x 

sage: y = ZZ['y'].gen(); AR(y) # indirect doctest 

Traceback (most recent call last): 

... 

ValueError: Polynomial y is not in 

Asymptotic Ring <x^ZZ> over Integer Ring 

> *previous* ValueError: Growth y is not in 

Exact Term Monoid x^ZZ with coefficients in Integer Ring. 

>> *previous* ValueError: y is not in Growth Group x^ZZ. 

 

:: 

 

sage: A = AsymptoticRing(growth_group='p^ZZ', coefficient_ring=QQ) 

sage: P.<p> = QQ[] 

sage: A(p) # indirect doctest 

p 

sage: A(p^11) # indirect doctest 

p^11 

sage: A(2*p^11) # indirect doctest 

2*p^11 

sage: A(3*p^4 + 7/3*p - 8) # indirect doctest 

3*p^4 + 7/3*p - 8 

 

:: 

 

sage: S = AsymptoticRing(growth_group='x^ZZ * y^ZZ', coefficient_ring=QQ) 

sage: var('x, y') 

(x, y) 

sage: S(x + y) # indirect doctest 

x + y 

sage: S(2*x - 4*x*y^6) # indirect doctest 

-4*x*y^6 + 2*x 

 

:: 

 

sage: A.<a,b> = AsymptoticRing('a^ZZ * b^ZZ', QQ) 

sage: 1/a 

a^(-1) 

 

:: 

 

sage: P.<a, b, c> = ZZ[] 

sage: A(a + b) 

a + b 

sage: A(a + c) 

Traceback (most recent call last): 

... 

ValueError: Polynomial a + c is not in 

Asymptotic Ring <a^ZZ * b^ZZ> over Rational Field 

> *previous* ValueError: Growth c is not in 

Exact Term Monoid a^ZZ * b^ZZ with coefficients in Rational Field. 

>> *previous* ValueError: c is not in Growth Group a^ZZ * b^ZZ. 

>...> *previous* ValueError: c is not in any of the factors of 

Growth Group a^ZZ * b^ZZ 

 

:: 

 

sage: M = AsymptoticRing('m^ZZ', ZZ) 

sage: N = AsymptoticRing('n^ZZ', QQ) 

sage: N(M.an_element()) # indirect doctest 

Traceback (most recent call last): 

... 

ValueError: Cannot include m^3 with parent 

Exact Term Monoid m^ZZ with coefficients in Integer Ring 

in Asymptotic Ring <n^ZZ> over Rational Field 

> *previous* ValueError: m^3 is not in Growth Group n^ZZ 

 

:: 

 

sage: M([1]) # indirect doctest 

Traceback (most recent call last): 

... 

TypeError: Not all list entries of [1] are asymptotic terms, 

so cannot create an asymptotic expansion in 

Asymptotic Ring <m^ZZ> over Integer Ring. 

sage: M(SR.var('a') + 1) # indirect doctest 

Traceback (most recent call last): 

... 

ValueError: Symbolic expression a + 1 is not in 

Asymptotic Ring <m^ZZ> over Integer Ring. 

> *previous* ValueError: a is not in 

Exact Term Monoid m^ZZ with coefficients in Integer Ring. 

>> *previous* ValueError: Factor a of a is neither a coefficient 

(in Integer Ring) nor growth (in Growth Group m^ZZ). 

""" 

from sage.data_structures.mutable_poset import MutablePoset 

if isinstance(data, MutablePoset): 

return self.element_class(self, data, simplify=simplify, convert=convert) 

 

if type(data) == self.element_class and data.parent() == self: 

return data 

 

if isinstance(data, AsymptoticExpansion): 

return self.element_class(self, data.summands, 

simplify=simplify, convert=convert) 

 

from .term_monoid import GenericTerm 

if isinstance(data, GenericTerm): 

data = (data,) 

 

if isinstance(data, (list, tuple)): 

if not all(isinstance(elem, GenericTerm) for elem in data): 

raise TypeError('Not all list entries of %s ' 

'are asymptotic terms, so cannot create an ' 

'asymptotic expansion in %s.' % (data, self)) 

summands = AsymptoticRing._create_empty_summands_() 

summands.union_update(data) 

return self.element_class(self, summands, 

simplify=simplify, convert=convert) 

 

if not data: 

summands = AsymptoticRing._create_empty_summands_() 

return self.element_class(self, summands, 

simplify=simplify, convert=False) 

 

try: 

P = data.parent() 

except AttributeError: 

return self.create_summand('exact', data) 

 

from .misc import combine_exceptions 

from sage.symbolic.ring import SymbolicRing 

from sage.rings.polynomial.polynomial_ring import is_PolynomialRing 

from sage.rings.polynomial.multi_polynomial_ring_generic import is_MPolynomialRing 

from sage.rings.power_series_ring import is_PowerSeriesRing 

 

if isinstance(P, SymbolicRing): 

from sage.symbolic.operators import add_vararg 

if data.operator() == add_vararg: 

summands = [] 

for summand in data.operands(): 

# TODO: check if summand is an O-Term here 

# (see #19425, #19426) 

try: 

summands.append(self.create_summand('exact', summand)) 

except ValueError as e: 

raise combine_exceptions( 

ValueError('Symbolic expression %s is not in %s.' % 

(data, self)), e) 

return sum(summands, self.zero()) 

 

elif is_PolynomialRing(P): 

p = P.gen() 

try: 

return sum(iter(self.create_summand('exact', growth=p**i, 

coefficient=c) 

for i, c in enumerate(data)), 

self.zero()) 

except ValueError as e: 

raise combine_exceptions( 

ValueError('Polynomial %s is not in %s' % (data, self)), e) 

 

elif is_MPolynomialRing(P): 

try: 

return sum(iter(self.create_summand('exact', growth=g, coefficient=c) 

for c, g in iter(data)), 

self.zero()) 

except ValueError as e: 

raise combine_exceptions( 

ValueError('Polynomial %s is not in %s' % (data, self)), e) 

 

elif is_PowerSeriesRing(P): 

raise NotImplementedError( 

'Cannot convert %s from the %s to an asymptotic expansion ' 

'in %s, since growths at other points than +oo are not yet ' 

'supported.' % (data, P, self)) 

# Delete lines above as soon as we can deal with growths 

# other than the that at going to +oo. 

p = P.gen() 

try: 

result = self(data.polynomial()) 

except ValueError as e: 

raise combine_exceptions( 

ValueError('Powerseries %s is not in %s' % (data, self)), e) 

prec = data.precision_absolute() 

if prec < sage.rings.infinity.PlusInfinity(): 

try: 

result += self.create_summand('O', growth=p**prec) 

except ValueError as e: 

raise combine_exceptions( 

ValueError('Powerseries %s is not in %s' % 

(data, self)), e) 

return result 

 

return self.create_summand('exact', data) 

 

 

def _coerce_map_from_(self, R): 

r""" 

Return whether ``R`` coerces into this asymptotic ring. 

 

INPUT: 

 

- ``R`` -- a parent. 

 

OUTPUT: 

 

A boolean. 

 

.. NOTE:: 

 

There are two possible cases: either ``R`` coerces in the 

:meth:`coefficient_ring` of this asymptotic ring, or ``R`` 

itself is an asymptotic ring, where both the 

:meth:`growth_group` and the :meth:`coefficient_ring` coerce into 

the :meth:`growth_group` and the :meth:`coefficient_ring` of this 

asymptotic ring, respectively. 

 

TESTS:: 

 

sage: AR_ZZ = AsymptoticRing(growth_group='x^ZZ', coefficient_ring=ZZ); AR_ZZ 

Asymptotic Ring <x^ZZ> over Integer Ring 

sage: x_ZZ = AR_ZZ.gen() 

sage: AR_QQ = AsymptoticRing(growth_group='x^QQ', coefficient_ring=QQ); AR_QQ 

Asymptotic Ring <x^QQ> over Rational Field 

sage: x_QQ = AR_QQ.gen() 

sage: AR_QQ.has_coerce_map_from(AR_ZZ) # indirect doctest 

True 

sage: x_ZZ * x_QQ 

x^2 

 

:: 

 

sage: AR_QQ.has_coerce_map_from(QQ) 

True 

sage: AR_QQ.has_coerce_map_from(ZZ) 

True 

""" 

from sage.data_structures.mutable_poset import MutablePoset 

if R == MutablePoset: 

return 

if self.coefficient_ring.has_coerce_map_from(R): 

return True 

if self.growth_group.has_coerce_map_from(R): 

return True 

elif isinstance(R, AsymptoticRing): 

if self.growth_group.has_coerce_map_from(R.growth_group) and \ 

self.coefficient_ring.has_coerce_map_from(R.coefficient_ring): 

return True 

 

 

def _repr_(self): 

r""" 

A representation string of this asymptotic ring. 

 

INPUT: 

 

Nothing. 

 

OUTPUT: 

 

A string. 

 

EXAMPLES:: 

 

sage: AR = AsymptoticRing(growth_group='x^ZZ', 

....: coefficient_ring=ZZ) 

sage: repr(AR) # indirect doctest 

'Asymptotic Ring <x^ZZ> over Integer Ring' 

""" 

try: 

G = '<' + self.growth_group._repr_(condense=True) + '>' 

except TypeError: 

G = repr(self.growth_group) 

return 'Asymptotic Ring %s over %s' % (G, self.coefficient_ring) 

 

 

def _an_element_(self): 

r""" 

Return an element of this asymptotic ring. 

 

INPUT: 

 

Nothing. 

 

OUTPUT: 

 

An :class:`AsymptoticExpansion`. 

 

EXAMPLES:: 

 

sage: AsymptoticRing(growth_group='z^QQ', coefficient_ring=ZZ).an_element() 

z^(3/2) + O(z^(1/2)) 

sage: AsymptoticRing(growth_group='z^ZZ', coefficient_ring=QQ).an_element() 

1/8*z^3 + O(z) 

sage: AsymptoticRing(growth_group='z^QQ', coefficient_ring=QQ).an_element() 

1/8*z^(3/2) + O(z^(1/2)) 

""" 

from .term_monoid import TermMonoid 

E = TermMonoid('exact', asymptotic_ring=self) 

O = TermMonoid('O', asymptotic_ring=self) 

return self(E.an_element(), simplify=False, convert=False)**3 + \ 

self(O.an_element(), simplify=False, convert=False) 

 

 

def some_elements(self): 

r""" 

Return some elements of this term monoid. 

 

See :class:`TestSuite` for a typical use case. 

 

INPUT: 

 

Nothing. 

 

OUTPUT: 

 

An iterator. 

 

EXAMPLES:: 

 

sage: from itertools import islice 

sage: A = AsymptoticRing(growth_group='z^QQ', coefficient_ring=ZZ) 

sage: tuple(islice(A.some_elements(), 10)) 

(z^(3/2) + O(z^(1/2)), 

O(z^(1/2)), 

z^(3/2) + O(z^(-1/2)), 

-z^(3/2) + O(z^(1/2)), 

O(z^(-1/2)), 

O(z^2), 

z^6 + O(z^(1/2)), 

-z^(3/2) + O(z^(-1/2)), 

O(z^2), 

z^(3/2) + O(z^(-2))) 

""" 

from sage.misc.mrange import cantor_product 

from .term_monoid import TermMonoid 

E = TermMonoid('exact', asymptotic_ring=self) 

O = TermMonoid('O', asymptotic_ring=self) 

return iter(self(e, simplify=False, convert=False)**3 + 

self(o, simplify=False, convert=False) 

for e, o in cantor_product( 

E.some_elements(), O.some_elements())) 

 

 

def gens(self): 

r""" 

Return a tuple with generators of this asymptotic ring. 

 

INPUT: 

 

Nothing. 

 

OUTPUT: 

 

A tuple of asymptotic expansions. 

 

.. NOTE:: 

 

Generators do not necessarily exist. This depends on the 

underlying growth group. For example, 

:class:`monomial growth groups <sage.rings.asymptotic.growth_group.MonomialGrowthGroup>` 

have a generator, and exponential growth groups 

do not. 

 

EXAMPLES:: 

 

sage: AR.<x> = AsymptoticRing(growth_group='x^ZZ', coefficient_ring=ZZ) 

sage: AR.gens() 

(x,) 

sage: B.<y,z> = AsymptoticRing(growth_group='y^ZZ * z^ZZ', coefficient_ring=QQ) 

sage: B.gens() 

(y, z) 

""" 

return tuple(self.create_summand('exact', 

growth=g, 

coefficient=self.coefficient_ring(1)) 

for g in self.growth_group.gens_monomial()) 

 

 

def gen(self, n=0): 

r""" 

Return the ``n``-th generator of this asymptotic ring. 

 

INPUT: 

 

- ``n`` -- (default: `0`) a non-negative integer. 

 

OUTPUT: 

 

An asymptotic expansion. 

 

EXAMPLES:: 

 

sage: R.<x> = AsymptoticRing(growth_group='x^ZZ', coefficient_ring=ZZ) 

sage: R.gen() 

x 

""" 

return self.gens()[n] 

 

 

def ngens(self): 

r""" 

Return the number of generators of this asymptotic ring. 

 

INPUT: 

 

Nothing. 

 

OUTPUT: 

 

An integer. 

 

EXAMPLES:: 

 

sage: AR.<x> = AsymptoticRing(growth_group='x^ZZ', coefficient_ring=ZZ) 

sage: AR.ngens() 

1 

""" 

return len(self.growth_group.gens_monomial()) 

 

 

def coefficients_of_generating_function(self, function, singularities, precision=None, 

return_singular_expansions=False): 

r""" 

Return the asymptotic growth of the coefficients of some 

generating function by means of Singularity Analysis. 

 

INPUT: 

 

- ``function`` -- a callable function in one variable. 

 

- ``singularities`` -- list of dominant singularities of the function. 

 

- ``precision`` -- (default: ``None``) an integer. If ``None``, then 

the default precision of the asymptotic ring is used. 

 

- ``return_singular_expansions`` -- (default: ``False``) a boolean. 

If set, the singular expansions are also returned. 

 

OUTPUT: 

 

- If ``return_singular_expansions=False``: An asymptotic expansion from 

this ring. 

 

- If ``return_singular_expansions=True``: A named tuple with 

components ``asymptotic_expansion`` and 

``singular_expansions``. The former contains an asymptotic 

expansion from this ring, the latter is a dictionary which 

contains the singular expansions around the singularities. 

 

.. TODO:: 

 

Make this method more usable by implementing the 

processing of symbolic expressions. 

 

EXAMPLES: 

 

Catalan numbers:: 

 

sage: def catalan(z): 

....: return (1-(1-4*z)^(1/2))/(2*z) 

sage: B.<n> = AsymptoticRing('QQ^n * n^QQ', QQ) 

sage: B.coefficients_of_generating_function(catalan, (1/4,), precision=3) 

1/sqrt(pi)*4^n*n^(-3/2) - 9/8/sqrt(pi)*4^n*n^(-5/2) 

+ 145/128/sqrt(pi)*4^n*n^(-7/2) + O(4^n*n^(-4)) 

sage: B.coefficients_of_generating_function(catalan, (1/4,), precision=2, 

....: return_singular_expansions=True) 

SingularityAnalysisResult(asymptotic_expansion=1/sqrt(pi)*4^n*n^(-3/2) 

- 9/8/sqrt(pi)*4^n*n^(-5/2) + O(4^n*n^(-3)), 

singular_expansions={1/4: 2 - 2*T^(-1/2) 

+ 2*T^(-1) - 2*T^(-3/2) + O(T^(-2))}) 

 

Unit fractions:: 

 

sage: def logarithmic(z): 

....: return -log(1-z) 

sage: B.coefficients_of_generating_function(logarithmic, (1,), precision=5) 

n^(-1) + O(n^(-3)) 

 

Harmonic numbers:: 

 

sage: def harmonic(z): 

....: return -log(1-z)/(1-z) 

sage: B.<n> = AsymptoticRing('QQ^n * n^QQ * log(n)^QQ', QQ) 

sage: ex = B.coefficients_of_generating_function(harmonic, (1,), precision=13); ex 

log(n) + euler_gamma + 1/2*n^(-1) - 1/12*n^(-2) + 1/120*n^(-4) 

+ O(n^(-6)) 

sage: ex.has_same_summands(asymptotic_expansions.HarmonicNumber( 

....: 'n', precision=5)) 

True 

 

.. WARNING:: 

 

Once singular expansions around points other than infinity 

are implemented (:trac:`20050`), the output in the case 

``return_singular_expansions`` will change to return singular 

expansions around the singularities. 

 

TESTS:: 

 

sage: def f(z): 

....: return z/(1-z) 

sage: B.coefficients_of_generating_function(f, (1,), precision=3) 

Traceback (most recent call last): 

... 

NotImplementedOZero: The error term in the result is O(0) 

which means 0 for sufficiently large n. 

""" 

from sage.symbolic.ring import SR 

from .misc import NotImplementedOZero 

 

singular_expansions = {} 

 

OZeroEncountered = False 

 

A = AsymptoticRing('T^QQ * log(T)^QQ', coefficient_ring=SR, 

default_prec=precision) 

T = A.gen() 

 

result = A.zero() 

for singularity in singularities: 

singular_expansion = A(function((1-1/T)*singularity)) 

singular_expansions[singularity] = singular_expansion 

 

try: 

contribution = singular_expansion._singularity_analysis_( 

var='Z', zeta=singularity, 

precision=precision).subs(Z=self.gen()) 

except NotImplementedOZero: 

OZeroEncountered = True 

else: 

result += contribution 

 

if OZeroEncountered and result.is_exact(): 

raise NotImplementedOZero(self) 

 

if return_singular_expansions: 

from collections import namedtuple 

SingularityAnalysisResult = namedtuple( 

'SingularityAnalysisResult', 

['asymptotic_expansion', 'singular_expansions']) 

return SingularityAnalysisResult( 

asymptotic_expansion=result, 

singular_expansions=singular_expansions) 

else: 

return result 

 

 

def create_summand(self, type, data=None, **kwds): 

r""" 

Create a simple asymptotic expansion consisting of a single 

summand. 

 

INPUT: 

 

- ``type`` -- 'O' or 'exact'. 

 

- ``data`` -- the element out of which a summand has to be created. 

 

- ``growth`` -- an element of the :meth:`growth_group`. 

 

- ``coefficient`` -- an element of the :meth:`coefficient_ring`. 

 

.. NOTE:: 

 

Either ``growth`` and ``coefficient`` or ``data`` have to 

be specified. 

 

OUTPUT: 

 

An asymptotic expansion. 

 

.. NOTE:: 

 

This method calls the factory :class:`TermMonoid 

<sage.rings.asymptotic.term_monoid.TermMonoidFactory>` 

with the appropriate arguments. 

 

EXAMPLES:: 

 

sage: R = AsymptoticRing(growth_group='x^ZZ', coefficient_ring=ZZ) 

sage: R.create_summand('O', x^2) 

O(x^2) 

sage: R.create_summand('exact', growth=x^456, coefficient=123) 

123*x^456 

sage: R.create_summand('exact', data=12*x^13) 

12*x^13 

 

TESTS:: 

 

sage: R.create_summand('exact', data='12*x^13') 

12*x^13 

sage: R.create_summand('exact', data='x^13 * 12') 

12*x^13 

sage: R.create_summand('exact', data='x^13') 

x^13 

sage: R.create_summand('exact', data='12') 

12 

sage: R.create_summand('exact', data=12) 

12 

 

:: 

 

sage: Z = R.change_parameter(coefficient_ring=Zmod(3)) 

sage: Z.create_summand('exact', data=42) 

0 

 

:: 

 

sage: R.create_summand('O', growth=42*x^2, coefficient=1) 

Traceback (most recent call last): 

... 

ValueError: Growth 42*x^2 is not in O-Term Monoid x^ZZ with implicit coefficients in Integer Ring. 

> *previous* ValueError: 42*x^2 is not in Growth Group x^ZZ. 

 

:: 

 

sage: AR.<z> = AsymptoticRing('z^QQ', QQ) 

sage: AR.create_summand('exact', growth='z^2') 

Traceback (most recent call last): 

... 

TypeError: Cannot create exact term: only 'growth' but 

no 'coefficient' specified. 

""" 

from .term_monoid import TermMonoid, ZeroCoefficientError 

TM = TermMonoid(type, asymptotic_ring=self) 

 

if data is None: 

try: 

data = kwds.pop('growth') 

except KeyError: 

raise TypeError("Neither 'data' nor 'growth' are specified.") 

if type == 'exact' and kwds.get('coefficient') is None: 

raise TypeError("Cannot create exact term: only 'growth' " 

"but no 'coefficient' specified.") 

 

try: 

return self(TM(data, **kwds), simplify=False, convert=False) 

except ZeroCoefficientError: 

return self.zero() 

 

 

def variable_names(self): 

r""" 

Return the names of the variables. 

 

OUTPUT: 

 

A tuple of strings. 

 

EXAMPLES:: 

 

sage: A = AsymptoticRing(growth_group='x^ZZ * QQ^y', coefficient_ring=QQ) 

sage: A.variable_names() 

('x', 'y') 

""" 

return self.growth_group.variable_names() 

 

 

def construction(self): 

r""" 

Return the construction of this asymptotic ring. 

 

OUTPUT: 

 

A pair whose first entry is an 

:class:`asymptotic ring construction functor <AsymptoticRingFunctor>` 

and its second entry the coefficient ring. 

 

EXAMPLES:: 

 

sage: A = AsymptoticRing(growth_group='x^ZZ * QQ^y', coefficient_ring=QQ) 

sage: A.construction() 

(AsymptoticRing<x^ZZ * QQ^y>, Rational Field) 

 

.. SEEALSO:: 

 

:doc:`asymptotic_ring`, 

:class:`AsymptoticRing`, 

:class:`AsymptoticRingFunctor`. 

 

TESTS: 

 

:trac:`22392`:: 

 

sage: from sage.rings.asymptotic.asymptotic_ring import AsymptoticRing 

sage: class MyAsymptoticRing(AsymptoticRing): 

....: pass 

sage: A = MyAsymptoticRing(growth_group='x^ZZ', coefficient_ring=QQ) 

sage: A.construction()[0].cls 

<class '__main__.MyAsymptoticRing'> 

""" 

return (AsymptoticRingFunctor(self.growth_group, 

default_prec=self.default_prec, 

category=self.category(), 

cls=self._underlying_class()), 

self.coefficient_ring) 

 

 

from sage.categories.pushout import ConstructionFunctor 

 

 

class AsymptoticRingFunctor(ConstructionFunctor): 

r""" 

A :class:`construction functor <sage.categories.pushout.ConstructionFunctor>` 

for :class:`asymptotic rings <AsymptoticRing>`. 

 

INPUT: 

 

- ``growth_group`` -- a partially ordered group (see 

:class:`AsymptoticRing` or 

:doc:`growth_group` for details). 

 

- ``default_prec`` -- ``None`` (default) or an integer. 

 

- ``category`` -- ``None`` (default) or a category. 

 

- ``cls`` -- :class:`AsymptoticRing` (default) or a derived class. 

 

EXAMPLES:: 

 

sage: AsymptoticRing(growth_group='x^ZZ', coefficient_ring=QQ).construction() # indirect doctest 

(AsymptoticRing<x^ZZ>, Rational Field) 

 

.. SEEALSO:: 

 

:doc:`asymptotic_ring`, 

:class:`AsymptoticRing`, 

:class:`sage.rings.asymptotic.growth_group.AbstractGrowthGroupFunctor`, 

:class:`sage.rings.asymptotic.growth_group.ExponentialGrowthGroupFunctor`, 

:class:`sage.rings.asymptotic.growth_group.MonomialGrowthGroupFunctor`, 

:class:`sage.categories.pushout.ConstructionFunctor`. 

 

TESTS:: 

 

sage: X = AsymptoticRing(growth_group='x^ZZ', coefficient_ring=QQ) 

sage: Y = AsymptoticRing(growth_group='y^ZZ', coefficient_ring=QQ) 

sage: cm = sage.structure.element.get_coercion_model() 

sage: cm.record_exceptions() 

sage: cm.common_parent(X, Y) 

Asymptotic Ring <x^ZZ * y^ZZ> over Rational Field 

sage: sage.structure.element.coercion_traceback() # not tested 

 

:: 

 

sage: from sage.categories.pushout import pushout 

sage: pushout(AsymptoticRing(growth_group='x^ZZ', coefficient_ring=ZZ), QQ) 

Asymptotic Ring <x^ZZ> over Rational Field 

""" 

 

rank = 13 

 

 

def __init__(self, growth_group, 

default_prec=None, category=None, 

cls=None): 

r""" 

See :class:`AsymptoticRingFunctor` for details. 

 

TESTS:: 

 

sage: from sage.rings.asymptotic.asymptotic_ring import AsymptoticRingFunctor 

sage: from sage.rings.asymptotic.growth_group import GrowthGroup 

sage: AsymptoticRingFunctor(GrowthGroup('x^ZZ')) 

AsymptoticRing<x^ZZ> 

""" 

self.growth_group = growth_group 

if cls is None: 

self.cls = AsymptoticRing 

else: 

self.cls = cls 

self._default_prec_ = default_prec 

self._category_ = category 

 

from sage.categories.rings import Rings 

super(ConstructionFunctor, self).__init__( 

Rings(), Rings()) 

 

 

def _repr_(self): 

r""" 

Return a representation string of this functor. 

 

OUTPUT: 

 

A string. 

 

TESTS:: 

 

sage: AsymptoticRing(growth_group='x^ZZ', coefficient_ring=QQ).construction()[0] # indirect doctest 

AsymptoticRing<x^ZZ> 

 

:trac:`22392`:: 

 

sage: from sage.rings.asymptotic.asymptotic_ring import AsymptoticRing 

sage: class MyAsymptoticRing(AsymptoticRing): 

....: pass 

sage: A = MyAsymptoticRing(growth_group='x^ZZ', coefficient_ring=QQ) 

sage: A.construction() 

(MyAsymptoticRing<x^ZZ>, Rational Field) 

""" 

return '{}<{}>'.format(self.cls.__name__, 

self.growth_group._repr_(condense=True)) 

 

 

def _apply_functor(self, coefficient_ring): 

r""" 

Apply this functor to the given ``coefficient_ring``. 

 

INPUT: 

 

- ``base`` - anything :class:`~sage.rings.asymptotic.growth_group.MonomialGrowthGroup` accepts. 

 

OUTPUT: 

 

An :class:`AsymptoticRing`. 

 

EXAMPLES:: 

 

sage: A = AsymptoticRing(growth_group='x^ZZ', coefficient_ring=QQ) 

sage: F, C = A.construction() 

sage: F(C) # indirect doctest 

Asymptotic Ring <x^ZZ> over Rational Field 

 

TESTS: 

 

:trac:`22392`:: 

 

sage: from sage.rings.asymptotic.asymptotic_ring import AsymptoticRing 

sage: class MyAsymptoticRing(AsymptoticRing): 

....: pass 

sage: A = MyAsymptoticRing(growth_group='x^ZZ', coefficient_ring=QQ) 

sage: type(A.construction()[0](ZZ)) 

<class '__main__.MyAsymptoticRing_with_category'> 

 

 

sage: C = CyclotomicField(3) 

sage: P = C['z'] 

sage: type(P(2) * A.gen()) 

<class '...MyAsymptoticRing_with_category.element_class'> 

 

:trac:`22396`:: 

 

sage: A.<n> = AsymptoticRing('ZZ^n * n^ZZ', ZZ, default_prec=3) 

sage: 1/(QQ(1)+n) 

n^(-1) - n^(-2) + n^(-3) + O(n^(-4)) 

""" 

kwds = {'growth_group': self.growth_group, 

'coefficient_ring': coefficient_ring} 

if self._category_ is not None: 

kwds['category'] = self._category_ 

if self._default_prec_ is not None: 

kwds['default_prec'] = self._default_prec_ 

return self.cls(**kwds) 

 

 

def merge(self, other): 

r""" 

Merge this functor with ``other`` if possible. 

 

INPUT: 

 

- ``other`` -- a functor. 

 

OUTPUT: 

 

A functor or ``None``. 

 

EXAMPLES:: 

 

sage: X = AsymptoticRing(growth_group='x^ZZ', coefficient_ring=QQ) 

sage: Y = AsymptoticRing(growth_group='y^ZZ', coefficient_ring=QQ) 

sage: F_X = X.construction()[0] 

sage: F_Y = Y.construction()[0] 

sage: F_X.merge(F_X) 

AsymptoticRing<x^ZZ> 

sage: F_X.merge(F_Y) 

AsymptoticRing<x^ZZ * y^ZZ> 

 

TESTS: 

 

:trac:`22396`:: 

 

sage: AN = AsymptoticRing(growth_group='y^ZZ', coefficient_ring=QQ) 

sage: F_AN = AN.construction()[0]; F_AN._default_prec_ = None 

sage: A3 = AsymptoticRing(growth_group='x^ZZ', coefficient_ring=QQ, default_prec=3) 

sage: F_A3 = A3.construction()[0] 

sage: A5 = AsymptoticRing(growth_group='x^ZZ', coefficient_ring=QQ, default_prec=5) 

sage: F_A5 = A5.construction()[0] 

 

sage: F_AN.merge(F_AN)(ZZ).default_prec 

20 

sage: F_AN.merge(F_A3)(ZZ).default_prec 

3 

sage: F_AN.merge(F_A5)(ZZ).default_prec 

5 

sage: F_A3.merge(F_AN)(ZZ).default_prec 

3 

sage: F_A3.merge(F_A3)(ZZ).default_prec 

3 

sage: F_A3.merge(F_A5)(ZZ).default_prec 

3 

sage: F_A5.merge(F_AN)(ZZ).default_prec 

5 

sage: F_A5.merge(F_A3)(ZZ).default_prec 

3 

sage: F_A5.merge(F_A5)(ZZ).default_prec 

5 

 

sage: A = AsymptoticRing(growth_group='y^ZZ', coefficient_ring=QQ) 

sage: F1 = A.construction()[0] 

sage: F2 = A.construction()[0]; F2._category_ = Rings() 

sage: F1.merge(F2)._category_ 

Category of rings 

""" 

if self == other: 

return self 

 

if isinstance(other, AsymptoticRingFunctor) and self.cls == other.cls: 

from sage.structure.element import get_coercion_model 

cm = get_coercion_model() 

try: 

G = cm.common_parent(self.growth_group, other.growth_group) 

except TypeError: 

pass 

else: 

if (self._default_prec_ is None 

and other._default_prec_ is None): 

default_prec = None 

elif self._default_prec_ is None: 

default_prec = other._default_prec_ 

elif other._default_prec_ is None: 

default_prec = self._default_prec_ 

else: 

default_prec = min(self._default_prec_, 

other._default_prec_) 

if (self._category_ is None 

and other._category_ is None): 

category = None 

elif self._category_ is None: 

category = other._category_ 

elif other._category_ is None: 

category = self._category_ 

else: 

category = self._category_ | other._category_ 

 

return AsymptoticRingFunctor( 

G, 

default_prec=default_prec, 

category=category, 

cls=self.cls) 

 

 

def __eq__(self, other): 

r""" 

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

 

INPUT: 

 

- ``other`` -- a functor. 

 

OUTPUT: 

 

A boolean. 

 

EXAMPLES:: 

 

sage: X = AsymptoticRing(growth_group='x^ZZ', coefficient_ring=QQ) 

sage: Y = AsymptoticRing(growth_group='y^ZZ', coefficient_ring=QQ) 

sage: F_X = X.construction()[0] 

sage: F_Y = Y.construction()[0] 

sage: F_X == F_X 

True 

sage: F_X == F_Y 

False 

""" 

return (type(self) == type(other) 

and self.growth_group == other.growth_group 

and self._default_prec_ == other._default_prec_ 

and self._category_ == other._category_ 

and self.cls == other.cls) 

 

 

def __ne__(self, other): 

r""" 

Return whether this functor is not equal to ``other``. 

 

INPUT: 

 

- ``other`` -- a functor. 

 

OUTPUT: 

 

A boolean. 

 

EXAMPLES:: 

 

sage: X = AsymptoticRing(growth_group='x^ZZ', coefficient_ring=QQ) 

sage: Y = AsymptoticRing(growth_group='y^ZZ', coefficient_ring=QQ) 

sage: F_X = X.construction()[0] 

sage: F_Y = Y.construction()[0] 

sage: F_X != F_X 

False 

sage: F_X != F_Y 

True 

""" 

return not self == other