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

""" 

`p`-Adic ``ZZ_pX`` CR Element 

  

This file implements elements of Eisenstein and unramified extensions 

of `\mathbb{Z}_p` and `\mathbb{Q}_p` with capped relative precision. 

  

For the parent class see padic_extension_leaves.pyx. 

  

The underlying implementation is through NTL's ``ZZ_pX`` class. Each 

element contains the following data: 

  

- ``ordp`` (``long``) -- A power of the uniformizer to scale the unit 

by. For unramified extensions this uniformizer is `p`, for Eisenstein 

extensions it is not. A value equal to the maximum value of a long 

indicates that the element is an exact zero. 

  

- ``relprec`` (``long``) -- A signed integer giving the precision to 

which this element is defined. For nonzero ``relprec``, the 

absolute value gives the power of the uniformizer modulo which the 

unit is defined. A positive value indicates that the element is 

normalized (ie ``unit`` is actually a unit: in the case of 

Eisenstein extensions the constant term is not divisible by `p`, in 

the case of unramified extensions that there is at least one 

coefficient that is not divisible by `p`). A negative value 

indicates that the element may or may not be normalized. A zero 

value indicates that the element is zero to some precision. If so, 

``ordp`` gives the absolute precision of the element. If ``ordp`` 

is greater than ``maxordp``, then the element is an exact zero. 

  

- ``unit`` (``ZZ_pX_c``) -- An ntl ``ZZ_pX`` storing the unit part. 

The variable `x` is the uniformizer in the case of Eisenstein 

extensions. If the element is not normalized, the ``unit`` may or 

may not actually be a unit. This ``ZZ_pX`` is created with global 

ntl modulus determined by the absolute value of ``relprec``. If 

``relprec`` is 0, ``unit`` **is not initialized**, or destructed if 

normalized and found to be zero. Otherwise, let `r` be relprec and 

`e` be the ramification index over `\mathbb{Q}_p` or `\mathbb{Z}_p`. 

Then the modulus of unit is given by `p^{ceil(r/e)}`. Note that all 

kinds of problems arise if you try to mix moduli. 

``ZZ_pX_conv_modulus`` gives a semi-safe way to convert between 

different moduli without having to pass through ``ZZX``. 

  

- ``prime_pow`` (some subclass of ``PowComputer_ZZ_pX``) -- a class, 

identical among all elements with the same parent, holding common 

data. 

  

+ ``prime_pow.deg`` -- The degree of the extension 

  

+ ``prime_pow.e`` -- The ramification index 

  

+ ``prime_pow.f`` -- The inertia degree 

  

+ ``prime_pow.prec_cap`` -- the unramified precision cap. For 

Eisenstein extensions this is the smallest power of p that is 

zero. 

  

+ ``prime_pow.ram_prec_cap`` -- the ramified precision cap. For 

Eisenstein extensions this will be the smallest power of `x` that 

is indistinguishable from zero. 

  

+ ``prime_pow.pow_ZZ_tmp``, prime_pow.pow_mpz_t_tmp``, 

``prime_pow.pow_Integer`` -- functions for accessing powers of 

`p`. The first two return pointers. See 

``sage/rings/padics/pow_computer_ext`` for examples and important 

warnings. 

  

+ ``prime_pow.get_context``, ``prime_pow.get_context_capdiv``, 

``prime_pow.get_top_context`` -- obtain an 

``ntl_ZZ_pContext_class`` corresponding to `p^n`. The capdiv 

version divides by ``prime_pow.e`` as appropriate. 

``top_context`` corresponds to `p^{prec_cap}`. 

  

+ ``prime_pow.restore_context``, 

``prime_pow.restore_context_capdiv``, 

``prime_pow.restore_top_context`` -- restores the given context. 

  

+ ``prime_pow.get_modulus``, ``get_modulus_capdiv``, 

``get_top_modulus`` -- Returns a ``ZZ_pX_Modulus_c*`` pointing to 

a polynomial modulus defined modulo `p^n` (appropriately divided 

by ``prime_pow.e`` in the capdiv case). 

  

EXAMPLES: 

  

An Eisenstein extension:: 

  

sage: R = Zp(5,5) 

sage: S.<x> = R[] 

sage: f = x^5 + 75*x^3 - 15*x^2 +125*x - 5 

sage: W.<w> = R.ext(f); W 

Eisenstein Extension in w defined by x^5 + 75*x^3 - 15*x^2 + 125*x - 5 with capped relative precision 25 over 5-adic Ring 

sage: z = (1+w)^5; z 

1 + w^5 + w^6 + 2*w^7 + 4*w^8 + 3*w^10 + w^12 + 4*w^13 + 4*w^14 + 4*w^15 + 4*w^16 + 4*w^17 + 4*w^20 + w^21 + 4*w^24 + O(w^25) 

sage: y = z >> 1; y 

w^4 + w^5 + 2*w^6 + 4*w^7 + 3*w^9 + w^11 + 4*w^12 + 4*w^13 + 4*w^14 + 4*w^15 + 4*w^16 + 4*w^19 + w^20 + 4*w^23 + O(w^24) 

sage: y.valuation() 

4 

sage: y.precision_relative() 

20 

sage: y.precision_absolute() 

24 

sage: z - (y << 1) 

1 + O(w^25) 

sage: (1/w)^12+w 

w^-12 + w + O(w^13) 

sage: (1/w).parent() 

Eisenstein Extension in w defined by x^5 + 75*x^3 - 15*x^2 + 125*x - 5 with capped relative precision 25 over 5-adic Field 

  

Unramified extensions:: 

  

sage: g = x^3 + 3*x + 3 

sage: A.<a> = R.ext(g) 

sage: z = (1+a)^5; z 

(2*a^2 + 4*a) + (3*a^2 + 3*a + 1)*5 + (4*a^2 + 3*a + 4)*5^2 + (4*a^2 + 4*a + 4)*5^3 + (4*a^2 + 4*a + 4)*5^4 + O(5^5) 

sage: z - 1 - 5*a - 10*a^2 - 10*a^3 - 5*a^4 - a^5 

O(5^5) 

sage: y = z >> 1; y 

(3*a^2 + 3*a + 1) + (4*a^2 + 3*a + 4)*5 + (4*a^2 + 4*a + 4)*5^2 + (4*a^2 + 4*a + 4)*5^3 + O(5^4) 

sage: 1/a 

(3*a^2 + 4) + (a^2 + 4)*5 + (3*a^2 + 4)*5^2 + (a^2 + 4)*5^3 + (3*a^2 + 4)*5^4 + O(5^5) 

sage: FFp = R.residue_field() 

sage: R(FFp(3)) 

3 + O(5) 

sage: QQq.<zz> = Qq(25,4) 

sage: QQq(FFp(3)) 

3 + O(5) 

sage: FFq = QQq.residue_field(); QQq(FFq(3)) 

3 + O(5) 

sage: zz0 = FFq.gen(); QQq(zz0^2) 

(zz + 3) + O(5) 

  

Different printing modes:: 

  

sage: R = Zp(5, print_mode='digits'); S.<x> = R[]; f = x^5 + 75*x^3 - 15*x^2 + 125*x -5; W.<w> = R.ext(f) 

sage: z = (1+w)^5; repr(z) 

'...4110403113210310442221311242000111011201102002023303214332011214403232013144001400444441030421100001' 

sage: R = Zp(5, print_mode='bars'); S.<x> = R[]; g = x^3 + 3*x + 3; A.<a> = R.ext(g) 

sage: z = (1+a)^5; repr(z) 

'...[4, 4, 4]|[4, 4, 4]|[4, 4, 4]|[4, 4, 4]|[4, 4, 4]|[4, 4, 4]|[4, 4, 4]|[4, 4, 4]|[4, 4, 4]|[4, 4, 4]|[4, 4, 4]|[4, 4, 4]|[4, 4, 4]|[4, 4, 4]|[4, 4, 4]|[4, 4, 4]|[4, 4, 4]|[4, 3, 4]|[1, 3, 3]|[0, 4, 2]' 

sage: R = Zp(5, print_mode='terse'); S.<x> = R[]; f = x^5 + 75*x^3 - 15*x^2 + 125*x -5; W.<w> = R.ext(f) 

sage: z = (1+w)^5; z 

6 + 95367431640505*w + 25*w^2 + 95367431640560*w^3 + 5*w^4 + O(w^100) 

sage: R = Zp(5, print_mode='val-unit'); S.<x> = R[]; f = x^5 + 75*x^3 - 15*x^2 + 125*x -5; W.<w> = R.ext(f) 

sage: y = (1+w)^5 - 1; y 

w^5 * (2090041 + 19073486126901*w + 1258902*w^2 + 674*w^3 + 16785*w^4) + O(w^100) 

  

You can get at the underlying ntl unit:: 

  

sage: z._ntl_rep() 

[6 95367431640505 25 95367431640560 5] 

sage: y._ntl_rep() 

[2090041 19073486126901 1258902 674 16785] 

sage: y._ntl_rep_abs() 

([5 95367431640505 25 95367431640560 5], 0) 

  

.. NOTE:: 

  

If you get an error ``internal error: can't grow this _ntl_gbigint,`` 

it indicates that moduli are being mixed inappropriately somewhere. 

  

For example, when calling a function with a ``ZZ_pX_c`` as an 

argument, it copies. If the modulus is not 

set to the modulus of the ``ZZ_pX_c``, you can get errors. 

  

AUTHORS: 

  

- David Roe (2008-01-01): initial version 

  

- Robert Harron (2011-09): fixes/enhancements 

  

- Julian Rueth (2014-05-09): enable caching through ``_cache_key`` 

  

""" 

  

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

# Copyright (C) 2008 David Roe <roed.math@gmail.com> 

# William Stein <wstein@gmail.com> 

# 2014 Julian Rueth <julian.rueth@fsfe.org> 

# 

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

# as published by the Free Software Foundation; either version 2 of 

# the License, or (at your option) any later version. 

# 

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

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

from __future__ import print_function 

  

from cysignals.signals cimport sig_on, sig_off 

from sage.ext.stdsage cimport PY_NEW 

include "sage/libs/ntl/decl.pxi" 

  

from sage.rings.integer cimport Integer 

from sage.rings.rational cimport Rational 

from sage.libs.gmp.mpz cimport * 

from sage.libs.gmp.mpq cimport * 

from sage.libs.ntl.ntl_ZZX cimport ntl_ZZX 

from sage.libs.ntl.ntl_ZZ cimport ntl_ZZ 

from sage.libs.ntl.ntl_ZZ_p cimport ntl_ZZ_p 

from sage.libs.ntl.ntl_ZZ_pContext cimport ntl_ZZ_pContext_class 

from sage.libs.ntl.ntl_ZZ_pContext import ntl_ZZ_pContext 

from sage.rings.padics.padic_generic_element cimport pAdicGenericElement 

from sage.libs.pari.all import pari_gen 

from sage.interfaces.gp import GpElement 

from sage.rings.finite_rings.integer_mod import is_IntegerMod 

from sage.rings.padics.padic_ext_element cimport pAdicExtElement 

from sage.rings.padics.precision_error import PrecisionError 

  

from sage.rings.padics.pow_computer_ext cimport PowComputer_ZZ_pX 

from sage.rings.padics.pow_computer_ext cimport PowComputer_ZZ_pX_small_Eis 

from sage.rings.padics.pow_computer_ext cimport PowComputer_ZZ_pX_big_Eis 

from sage.rings.finite_rings.integer_mod_ring import IntegerModRing 

from sage.rings.padics.unramified_extension_generic import UnramifiedExtensionGeneric 

from sage.misc.superseded import deprecated_function_alias, deprecation 

  

from sage.rings.real_double cimport RealDoubleElement 

  

cdef object infinity 

from sage.rings.infinity import infinity 

  

cdef long maxordp = (1L << (sizeof(long) * 8 - 2)) -1 

cdef long minusmaxordp = -maxordp 

  

cdef inline int check_ordp(long a) except -1: 

if a > maxordp or a < minusmaxordp: 

raise ValueError("valuation overflow") 

  

  

cdef class pAdicZZpXCRElement(pAdicZZpXElement): 

def __init__(self, parent, x, absprec = infinity, relprec = infinity, empty = False): 

""" 

Creates an element of a capped relative precision, unramified 

or Eisenstein extension of `\mathbb{Z}_p` or `\mathbb{Q}_p`. 

  

INPUT: 

  

- ``parent`` -- either an ``EisensteinRingCappedRelative`` or 

``UnramifiedRingCappedRelative`` 

  

- ``x`` -- an integer, rational, `p`-adic element, polynomial, 

list, integer_mod, pari int/frac/poly_t/pol_mod, an 

``ntl_ZZ_pX``, an ``ntl_ZZ``, an ``ntl_ZZ_p``, an 

``ntl_ZZX``, or something convertible into parent.residue_field() 

  

- ``absprec`` -- an upper bound on the absolute precision of the 

element created 

  

- ``relprec`` -- an upper bound on the relative precision of 

the element created 

  

- ``empty`` -- whether to return after initializing to zero 

(without setting the valuation). 

  

EXAMPLES:: 

  

sage: R = Zp(5,5) 

sage: S.<x> = R[] 

sage: f = x^5 + 75*x^3 - 15*x^2 +125*x - 5 

sage: W.<w> = R.ext(f) 

sage: z = (1+w)^5; z # indirect doctest 

1 + w^5 + w^6 + 2*w^7 + 4*w^8 + 3*w^10 + w^12 + 4*w^13 + 4*w^14 + 4*w^15 + 4*w^16 + 4*w^17 + 4*w^20 + w^21 + 4*w^24 + O(w^25) 

sage: W(pari('3 + O(5^3)')) 

3 + O(w^15) 

sage: W(R(3,3)) 

3 + O(w^15) 

sage: W.<w> = R.ext(x^625 + 915*x^17 - 95) 

sage: W(3) 

3 + O(w^3125) 

sage: W(w, 14) 

w + O(w^14) 

  

TESTS: 

  

Check that :trac:`3865` is fixed:: 

  

sage: W(gp('3 + O(5^10)')) 

3 + O(w^3125) 

  

  

Check that :trac:`13612` has been fixed:: 

  

sage: R = Zp(3) 

sage: S.<a> = R[] 

sage: W.<a> = R.extension(a^2+1) 

sage: W(W.residue_field().zero()) 

O(3) 

  

sage: K = Qp(3) 

sage: S.<a> = K[] 

sage: L.<a> = K.extension(a^2+1) 

sage: L(L.residue_field().zero()) 

O(3) 

  

""" 

pAdicZZpXElement.__init__(self, parent) 

self.relprec = 0 

if empty: 

return 

cdef long aprec, rprec, ctx_prec, ltmp 

if relprec is not infinity and not isinstance(relprec, Integer): 

relprec = Integer(relprec) 

if (relprec is infinity) or (relprec > parent.precision_cap()): 

rprec = self.prime_pow.ram_prec_cap 

else: 

rprec = mpz_get_si((<Integer>relprec).value) 

if rprec < 0: 

rprec = 0 

if absprec is not infinity: 

if not isinstance(absprec, Integer): 

absprec = Integer(absprec) 

if mpz_fits_slong_p((<Integer>absprec).value) == 0: 

absprec = infinity 

else: 

aprec = mpz_get_si((<Integer>absprec).value) 

cdef mpz_t tmp 

cdef ZZ_c tmp_z 

cdef Py_ssize_t i 

cdef Integer tmp_Int 

cdef Rational xlift 

if isinstance(x, pAdicGenericElement): 

if self.prime_pow.in_field == 0 and x.valuation() < 0: 

raise ValueError("element has negative valuation") 

if x._is_base_elt(self.prime_pow.prime): 

xlift = Rational(x.lift()) 

if mpq_sgn(xlift.value) == 0: 

if (<pAdicGenericElement>x)._is_exact_zero(): 

if absprec is infinity: 

self._set_exact_zero() 

else: 

self._set_inexact_zero(aprec) 

return 

ltmp = mpz_get_si((<Integer>x.precision_absolute()).value) * self.prime_pow.e 

if absprec is infinity or ltmp < aprec: 

aprec = ltmp 

self._set_from_mpq_both(xlift.value, aprec, rprec) 

return 

if isinstance(x, GpElement): 

x = x.__pari__() 

if isinstance(x, pari_gen): 

if x.type() == "t_PADIC": 

if x.variable() != self.prime_pow.prime: 

raise TypeError("Cannot coerce a pari p-adic with the wrong prime.") 

ltmp = x.padicprec(self.prime_pow.prime) * self.prime_pow.e 

if absprec is infinity or ltmp < aprec: 

aprec = ltmp 

absprec = 0 # absprec just has to be non-infinite: everything else uses aprec 

x = x.lift() 

if x.type() == 't_INT': 

x = Integer(x) 

elif x.type() == 't_FRAC': 

x = Rational(x) 

elif x.type() == 't_POLMOD' or x.type == 't_POL': 

# This code doesn't check to see if the primes are the same. 

L = [] 

x = x.lift().lift() 

for i from 0 <= i <= x.poldegree(): 

L.append(Integer(x.polcoeff(i))) 

x = L 

else: 

raise TypeError("unsupported coercion from pari: only p-adics, integers, rationals, polynomials and pol_mods allowed") 

elif is_IntegerMod(x): 

mpz_init(tmp) 

ctx_prec = mpz_remove(tmp, (<Integer>x.modulus()).value, self.prime_pow.prime.value) 

if mpz_cmp_ui(tmp, 1) == 0: 

mpz_clear(tmp) 

x = x.lift() 

if absprec is infinity or ctx_prec < aprec: 

aprec = ctx_prec 

absprec = 0 # absprec just has to be non-infinite: everything else uses aprec 

else: 

mpz_clear(tmp) 

raise TypeError("cannot coerce from the given integer mod ring (not a power of the same prime)") 

elif isinstance(x, ntl_ZZ_p): 

ctx_prec = ZZ_remove(tmp_z, (<ntl_ZZ>x.modulus()).x, self.prime_pow.pow_ZZ_tmp(1)[0]) 

if ZZ_IsOne(tmp_z): 

x = x.lift() 

tmp_Int = PY_NEW(Integer) 

ZZ_to_mpz(tmp_Int.value, &(<ntl_ZZ>x).x) 

x = tmp_Int 

if absprec is infinity or ctx_prec < aprec: 

aprec = ctx_prec 

absprec = 0 # absprec just has to be non-infinite: everything else uses aprec 

else: 

raise TypeError("cannot coerce the given ntl_ZZ_p (modulus not a power of the same prime)") 

elif isinstance(x, ntl_ZZ): 

tmp_Int = PY_NEW(Integer) 

ZZ_to_mpz(tmp_Int.value, &(<ntl_ZZ>x).x) 

x = tmp_Int 

elif isinstance(x, (int, long)): 

x = Integer(x) 

elif x in parent.residue_field() and x.parent().is_finite(): 

# Should only reach here if x is not in F_p 

z = parent.gen() 

poly = x.polynomial().list() 

x = sum([poly[i].lift() * (z ** i) for i in range(len(poly))], parent.zero()) 

if absprec is infinity or 1 < aprec: 

aprec = 1 

absprec = 0 # absprec just has to be non-infinite: everything else uses aprec 

cdef pAdicZZpXCRElement _x 

if isinstance(x, Integer): 

if absprec is infinity: 

self._set_from_mpz_rel((<Integer>x).value, rprec) 

else: 

self._set_from_mpz_both((<Integer>x).value, aprec, rprec) 

elif isinstance(x, Rational): 

if absprec is infinity: 

self._set_from_mpq_rel((<Rational>x).value, rprec) 

else: 

self._set_from_mpq_both((<Rational>x).value, aprec, rprec) 

elif isinstance(x, ntl_ZZ_pX): 

if absprec is infinity: 

self._set_from_ZZ_pX_rel(&(<ntl_ZZ_pX>x).x, (<ntl_ZZ_pX>x).c, rprec) 

else: 

self._set_from_ZZ_pX_both(&(<ntl_ZZ_pX>x).x, (<ntl_ZZ_pX>x).c, aprec, rprec) 

elif isinstance(x, ntl_ZZX): 

if absprec is infinity: 

self._set_from_ZZX_rel((<ntl_ZZX>x).x, rprec) 

else: 

self._set_from_ZZX_both((<ntl_ZZX>x).x, aprec, rprec) 

elif isinstance(x, pAdicExtElement): 

if x.parent() is parent: 

_x = <pAdicZZpXCRElement>x 

if _x.relprec == 0: 

if absprec is infinity or aprec > _x.ordp: 

self._set_inexact_zero(_x.ordp) # this works for exact zeros too. 

else: 

self._set_inexact_zero(aprec) 

elif _x.relprec < 0: 

if -_x.relprec < rprec: 

rprec = _x.relprec 

else: 

rprec = -rprec 

if absprec is infinity or aprec > _x.ordp - rprec: 

self._set(&_x.unit, _x.ordp, rprec) 

elif aprec > _x.ordp: 

self._set(&_x.unit, _x.ordp, _x.ordp - aprec) #negating relprec to indicate non-normalized. 

else: 

self._set_inexact_zero(aprec) 

else: 

if _x.relprec < rprec: 

rprec = _x.relprec 

if absprec is infinity or aprec > _x.ordp + rprec: 

self._set(&_x.unit, _x.ordp, rprec) 

elif aprec > _x.ordp: 

self._set(&_x.unit, _x.ordp, aprec - _x.ordp) 

else: 

self._set_inexact_zero(aprec) 

elif x.parent().fraction_field() is parent: 

if isinstance(x, pAdicZZpXCRElement): 

_x = <pAdicZZpXCRElement>x 

if _x.relprec < 0: 

_x._normalize() 

if _x._is_exact_zero(): 

self._set_exact_zero() 

elif _x._is_inexact_zero(): 

self._set_inexact_zero(_x.ordp) 

else: 

if _x.relprec < rprec: 

rprec = _x.relprec 

self._set(&_x.unit, _x.ordp, rprec) 

else: 

# x is a pAdicZZpXCAElement 

xordp = x.valuation() 

xprec = x.precision_absolute() 

if xordp == xprec: 

self._set_inexact_zero(mpz_get_si((<Integer>xordp).value)) 

else: 

poly = x._ntl_rep_abs()[0] 

if absprec is infinity: 

self._set_from_ZZ_pX_rel(&(<ntl_ZZ_pX>poly).x,(<ntl_ZZ_pX>poly).c, rprec) 

else: 

self._set_from_ZZ_pX_both(&(<ntl_ZZ_pX>poly).x,(<ntl_ZZ_pX>poly).c, aprec, rprec) 

elif x.parent() is parent.fraction_field(): 

_x = <pAdicZZpXCRElement>x 

if _x.relprec < 0: 

_x._normalize() 

if _x._is_exact_zero(): 

self._set_exact_zero() 

elif _x._is_inexact_zero(): 

self._set_inexact_zero(_x.ordp) 

else: 

if _x.relprec < rprec: 

rprec = _x.relprec 

self._set(&_x.unit, _x.ordp, rprec) 

else: 

raise NotImplementedError("Conversion from different p-adic extensions not yet supported") 

else: 

try: 

x = list(x) 

except TypeError: 

try: 

x = x.list() 

except AttributeError: 

raise TypeError("cannot convert x to a p-adic element") 

if absprec is infinity: 

self._set_from_list_rel(x, rprec) 

else: 

self._set_from_list_both(x, aprec, rprec) 

  

def _cache_key(self): 

r""" 

Return a hashable key which identifies this element. 

  

This makes it possible to use this element in caches such as 

functions or methods decorated with ``@cached_function`` or 

``@cached_method`` respectively. 

  

EXAMPLES: 

  

In the following example, ``a`` and ``b`` compare equal. They cannot 

have a meaningful hash value since then their hash value would have to 

be the same:: 

  

sage: K.<a> = Qq(9) 

sage: b = a + O(3) 

sage: a == b 

True 

sage: hash(a) 

Traceback (most recent call last): 

... 

TypeError: unhashable type: 'sage.rings.padics.qadic_flint_CR.qAdicCappedRelativeElement' 

  

However, we want to cache computations which depend on them. Therefore 

they define a ``_cache_key`` which is hashable and uniquely identifies 

them:: 

  

sage: a._cache_key() 

(..., ((0, 1),), 0, 20) 

sage: b._cache_key() 

(..., ((0, 1),), 0, 1) 

  

TESTS: 

  

Check that zero values are handled correctly:: 

  

sage: K.zero()._cache_key() 

(..., 0) 

sage: K(0,1)._cache_key() 

(..., 1, 0) 

  

""" 

if self._is_exact_zero(): 

return (self.parent(), 0) 

elif self._is_inexact_zero(): 

return (self.parent(), 0, self.valuation()) 

else: 

return (self.parent(), 

tuple(tuple(c) if isinstance(c, list) else c 

for c in self.unit_part().expansion()), 

self.valuation(), self.precision_relative()) 

  

cdef int _set_inexact_zero(self, long absprec) except -1: 

""" 

Sets ``self`` to be zero with valuation absprec. 

  

EXAMPLES:: 

  

sage: R = Zp(5,5) 

sage: S.<x> = R[] 

sage: f = x^5 + 75*x^3 - 15*x^2 +125*x - 5 

sage: W.<w> = R.ext(f) 

sage: z = W(0,6); z # indirect doctest 

O(w^6) 

sage: z.valuation() 

6 

sage: z.precision_absolute() 

6 

sage: z.precision_relative() 

0 

  

TESTS:: 

  

sage: R = Zp(17, 3) 

sage: S.<x> = R[] 

sage: W.<w> = R.ext(x^34 - 289*x^5 + 17) 

sage: z = W(0, 6); z 

O(w^6) 

sage: z.valuation() 

6 

sage: z.precision_absolute() 

6 

sage: z.precision_relative() 

0 

""" 

self.ordp = absprec 

self.relprec = 0 

  

cdef int _set_exact_zero(self) except -1: 

""" 

Sets ``self`` to be an exact zero. 

  

EXAMPLES:: 

  

sage: R = Zp(5,5) 

sage: S.<x> = R[] 

sage: f = x^5 + 75*x^3 - 15*x^2 +125*x - 5 

sage: W.<w> = R.ext(f) 

sage: z = R(0); z # indirect doctest 

0 

sage: z.valuation() 

+Infinity 

sage: z.precision_absolute() 

+Infinity 

sage: z.precision_relative() 

0 

  

TESTS:: 

  

sage: R = Zp(89, 3) 

sage: S.<x> = R[] 

sage: W.<w> = R.ext(x^34 - 2*89*x^5 + 89) 

sage: z = R(0); z # indirect doctest 

0 

sage: z.valuation() 

+Infinity 

sage: z.precision_absolute() 

+Infinity 

sage: z.precision_relative() 

0 

""" 

self.ordp = maxordp 

self.relprec = 0 

  

cpdef bint _is_exact_zero(self) except -1: 

""" 

Tests if ``self`` is an exact zero. 

  

EXAMPLES:: 

  

sage: R = Qp(3,5) 

sage: S.<x> = R[] 

sage: f = x^5 + 75*x^3 - 15*x^2 +125*x - 5 

sage: W.<w> = R.ext(f) 

sage: z = W(0) 

sage: z._is_exact_zero() 

True 

sage: z = W(0,6) 

sage: z._is_exact_zero() 

False 

  

TESTS:: 

  

sage: R = Qp(53, 3) 

sage: S.<x> = R[] 

sage: W.<w> = R.ext(x^34 - 2*53^5*x^9 + 53) 

sage: z = W(0) 

sage: z._is_exact_zero() 

True 

sage: z = W(0,6) 

sage: z._is_exact_zero() 

False 

  

""" 

if self.ordp == maxordp: 

return 1 

else: 

return 0 

  

cpdef bint _is_inexact_zero(self) except -1: 

""" 

Tests if ``self`` is an inexact zero. 

  

EXAMPLES:: 

  

sage: R = Zp(7,5) 

sage: S.<x> = R[] 

sage: f = x^5 + 75*x^3 - 15*x^2 +125*x - 5 

sage: W.<w> = R.ext(f) 

sage: z = W(0) 

sage: z._is_inexact_zero() 

False 

sage: z = W(0,6) 

sage: z._is_inexact_zero() 

True 

  

TESTS:: 

  

sage: R = Qp(29, 3) 

sage: S.<x> = R[] 

sage: W.<w> = R.ext(x^29 - 2*29^5*x - 29) 

sage: z = W(0) 

sage: z._is_inexact_zero() 

False 

sage: z = W(0,6) 

sage: z._is_inexact_zero() 

True 

""" 

self._normalize() 

if self.relprec == 0: 

return not self._is_exact_zero() 

else: 

return False 

  

cdef int _set(self, ZZ_pX_c* unit, long ordp, long relprec) except -1: 

""" 

Sets ``unit``, ``ordp`` and ``relprec`` directly. 

  

EXAMPLES:: 

  

sage: R = Zp(5,5) 

sage: S.<x> = R[] 

sage: f = x^5 + 75*x^3 - 15*x^2 +125*x - 5 

sage: W.<w> = R.ext(f) 

sage: F = W.fraction_field() 

sage: z = F(1+w); z # indirect doctest 

1 + w + O(w^25) 

  

TESTS:: 

  

sage: R = Zp(17,30) 

sage: S.<x> = R[] 

sage: f = x^51 - 34 

sage: W.<w> = R.ext(f) 

sage: F = W.fraction_field() 

sage: z = F(1+w); z # indirect doctest 

1 + w + O(w^1530) 

sage: z = F(w+w^2,relprec=0); z 

O(w) 

""" 

self.ordp = ordp 

self._set_prec_rel(relprec) 

if self.relprec != 0: 

ZZ_pX_conv_modulus(self.unit, unit[0], self.prime_pow.get_context_capdiv(relprec).x) 

  

cdef int _set_from_mpz_rel(self, mpz_t x, long relprec) except -1: 

""" 

Sets ``self`` from an ``mpz_t`` with relative precision bounded by ``relprec``. 

  

EXAMPLES:: 

  

sage: R = Zp(5,5) 

sage: S.<x> = R[] 

sage: f = x^5 + 75*x^3 - 15*x^2 +125*x - 5 

sage: W.<w> = R.ext(f) 

sage: W(70, relprec = 8) # indirect doctest 

4*w^5 + 3*w^7 + w^9 + 2*w^10 + 2*w^11 + O(w^13) 

sage: W(70, relprec = 0) 

O(w^5) 

  

TESTS:: 

  

sage: R = Qp(13,50) 

sage: S.<x> = R[] 

sage: f = x^169 - 13 

sage: W.<w> = R.ext(f) 

sage: a = W(65, relprec = 8); a.valuation() # indirect doctest 

169 

sage: W(65, relprec = 0) 

O(w^169) 

""" 

if mpz_sgn(x) == 0: 

self._set_exact_zero() 

return 0 

cdef mpz_t tmp_m 

cdef ZZ_c tmp_z 

cdef long shift 

mpz_init(tmp_m) 

sig_on() 

shift = mpz_remove(tmp_m, x, self.prime_pow.prime.value) 

sig_off() 

self._set_prec_rel(relprec) 

mpz_to_ZZ(&tmp_z, tmp_m) 

mpz_clear(tmp_m) 

if self.relprec != 0: 

ZZ_pX_SetCoeff(self.unit, 0, ZZ_to_ZZ_p(tmp_z)) 

self.ordp = 0 

self._pshift_self(shift) 

else: 

self.ordp = shift * self.prime_pow.e 

  

cdef int _set_from_mpz_both(self, mpz_t x, long absprec, long relprec) except -1: 

""" 

Sets ``self`` from an ``mpz_t`` with relative precision bounded by ``relprec`` 

and absolute precision bounded by ``absprec``. 

  

EXAMPLES:: 

  

sage: R = Zp(5,5) 

sage: S.<x> = R[] 

sage: f = x^5 + 75*x^3 - 15*x^2 +125*x - 5 

sage: W.<w> = R.ext(f) 

sage: W(70, 8) # indirect doctest 

4*w^5 + 3*w^7 + O(w^8) 

sage: W(70, absprec = 4) 

O(w^4) 

  

TESTS:: 

  

sage: R = Zp(7,3) 

sage: S.<x> = R[] 

sage: f = x^49 + 7*x^21 - 14 

sage: W.<w> = R.ext(f) 

sage: W(70, 100) # indirect doctest 

5*w^49 + 6*w^70 + 3*w^91 + O(w^100) 

sage: W(70, absprec = 4) 

O(w^4) 

""" 

if mpz_sgn(x) == 0: 

self._set_inexact_zero(absprec) 

return 0 

cdef mpz_t tmp_m 

cdef ZZ_c tmp_z 

cdef long shift 

mpz_init(tmp_m) 

sig_on() 

shift = mpz_remove(tmp_m, x, self.prime_pow.prime.value) 

sig_off() 

self.ordp = shift * self.prime_pow.e 

if self._set_prec_both(absprec, relprec) == 1: 

# This indicates that self._set_inexact_zero was called 

mpz_clear(tmp_m) 

return 0 

mpz_to_ZZ(&tmp_z, tmp_m) 

mpz_clear(tmp_m) 

if self.relprec != 0: 

ZZ_pX_SetCoeff(self.unit, 0, ZZ_to_ZZ_p(tmp_z)) 

self.ordp = 0 

self._pshift_self(shift) 

  

cdef int _set_from_mpq_rel(self, mpq_t x, long relprec) except -1: 

""" 

Sets ``self`` from an ``mpq_t`` with relative precision 

bounded by ``relprec``. 

  

EXAMPLES:: 

  

sage: R = Zp(5,5) 

sage: S.<x> = R[] 

sage: f = x^5 + 75*x^3 - 15*x^2 +125*x - 5 

sage: W.<w> = R.ext(f) 

sage: z = W(70/3, relprec = 9); z # indirect doctest 

3*w^5 + w^7 + 2*w^9 + 2*w^10 + 4*w^11 + w^12 + 2*w^13 + O(w^14) 

sage: z * 3 

4*w^5 + 3*w^7 + w^9 + 2*w^10 + 2*w^11 + w^13 + O(w^14) 

sage: W(70) 

4*w^5 + 3*w^7 + w^9 + 2*w^10 + 2*w^11 + w^13 + 3*w^16 + w^17 + w^18 + 4*w^20 + 4*w^21 + w^22 + 2*w^23 + 3*w^25 + w^27 + O(w^30) 

sage: F = W.fraction_field() 

sage: y = F(3/700); y 

w^-10 + w^-8 + 4*w^-6 + w^-3 + 4*w^-2 + 3*w^-1 + 3 + 4*w + w^3 + 4*w^4 + w^5 + 4*w^6 + 2*w^7 + 3*w^8 + 4*w^9 + 3*w^10 + 4*w^11 + w^12 + O(w^15) 

sage: y * 700 

3 + O(w^25) 

sage: W(70/3, relprec = 0) 

O(w^5) 

sage: c = F(5^-1 + O(5^2)); c 

w^-5 + 3*w^-3 + 2*w^3 + 4*w^5 + 4*w^6 + 3*w^7 + w^9 + O(w^10) 

sage: c * 5 

1 + O(w^15) 

  

TESTS:: 

  

sage: R = Zp(11, 8, print_mode='digits') 

sage: S.<x> = R[] 

sage: f = x^3 + 1331 * x^2 - 11 * x + 11 

sage: W.<w> = R.ext(f) 

sage: z = W(77/3, relprec = 11); repr(z)[3:] 

'304107A2555000' 

sage: repr(z*3)[3:] 

'56698765444000' 

sage: repr(W(77))[3:] 

'5800A6604678856698765444000' 

sage: F = W.fraction_field() 

sage: y = F(3/847); repr(y)[3:] 

'5563A4105291255628.148272' 

sage: repr(y*847)[3:] 

'000000000000000000000003' 

sage: repr(W(77/3, relprec=0)) 

'0' 

sage: c = F(11^-1 + O(11^2)); repr(c)[3:] 

'011111.01A' 

sage: repr(c * 11)[3:] 

'000000001' 

""" 

if mpq_sgn(x) == 0: 

self._set_exact_zero() 

return 0 

cdef mpz_t num_unit, den_unit 

self._set_from_mpq_part1(num_unit, den_unit, x) 

self._set_prec_rel(relprec) 

self._set_from_mpq_part2(num_unit, den_unit) 

  

cdef int _set_from_mpq_both(self, mpq_t x, long absprec, long relprec) except -1: 

""" 

Sets ``self`` from an ``mpq_t`` with relative precision 

bounded by ``relprec`` and absolute precision bounded by 

``absprec``. 

  

EXAMPLES:: 

  

sage: R = Zp(5,5) 

sage: S.<x> = R[] 

sage: f = x^5 + 75*x^3 - 15*x^2 +125*x - 5 

sage: W.<w> = R.ext(f) 

sage: z = W(70/3, 14); z # indirect doctest 

3*w^5 + w^7 + 2*w^9 + 2*w^10 + 4*w^11 + w^12 + 2*w^13 + O(w^14) 

sage: z * 3 

4*w^5 + 3*w^7 + w^9 + 2*w^10 + 2*w^11 + w^13 + O(w^14) 

sage: W(70) 

4*w^5 + 3*w^7 + w^9 + 2*w^10 + 2*w^11 + w^13 + 3*w^16 + w^17 + w^18 + 4*w^20 + 4*w^21 + w^22 + 2*w^23 + 3*w^25 + w^27 + O(w^30) 

sage: F = W.fraction_field() 

sage: y = F(3/700,-2); y 

w^-10 + w^-8 + 4*w^-6 + w^-3 + O(w^-2) 

sage: y * 700 

3 + O(w^8) 

sage: W(70/3, absprec = 4) 

O(w^4) 

""" 

if mpq_sgn(x) == 0: 

self._set_inexact_zero(absprec) 

return 0 

cdef mpz_t num_unit, den_unit 

self._set_from_mpq_part1(num_unit, den_unit, x) 

if self._set_prec_both(absprec, relprec) == 1: 

# indicates an inexact zero 

mpz_clear(num_unit) 

mpz_clear(den_unit) 

return 0 

self._set_from_mpq_part2(num_unit, den_unit) 

  

cdef int _set_from_mpq_part1(self, mpz_t num_unit, mpz_t den_unit, mpq_t x) except -1: 

""" 

Sets ``num_unit`` to be the unit of the numerator, ``den_unit`` to be the unit of the denominator and sets ``self.ordp`` correctly. 

  

TESTS:: 

  

sage: R = Zp(5,5) 

sage: S.<x> = R[] 

sage: f = x^5 + 75*x^3 - 15*x^2 +125*x - 5 

sage: W.<w> = R.ext(f) 

sage: z = W(7000/3, 23); z # indirect doctest 

2*w^15 + 2*w^17 + 3*w^19 + w^22 + O(w^23) 

""" 

cdef long num_ordp, den_ordp 

sig_on() 

mpz_init(num_unit) 

mpz_init(den_unit) 

num_ordp = mpz_remove(num_unit, mpq_numref(x), self.prime_pow.prime.value) 

den_ordp = mpz_remove(den_unit, mpq_denref(x), self.prime_pow.prime.value) 

sig_off() 

self.ordp = (num_ordp - den_ordp) * self.prime_pow.e 

if self.ordp < 0 and self.prime_pow.in_field == 0: 

mpz_clear(num_unit) 

mpz_clear(den_unit) 

raise ValueError("p divides the denominator") 

  

cdef int _set_from_mpq_part2(self, mpz_t num_unit, mpz_t den_unit) except -1: 

""" 

Given that ``self.ordp`` and ``self.relprec`` have been set, takes 

``num_unit`` and ``den_unit`` and sets ``self.unit``. 

  

TESTS:: 

  

sage: R = Zp(5,5) 

sage: S.<x> = R[] 

sage: f = x^5 + 75*x^3 - 15*x^2 +125*x - 5 

sage: W.<w> = R.ext(f) 

sage: W(QQ(0), 23) # indirect doctest 

O(w^23) 

sage: W(QQ(0)) 

0 

""" 

cdef ZZ_c num_zz, den_zz 

cdef ZZ_p_c tmp_zp 

cdef long val = self.ordp / self.prime_pow.e 

cdef mpz_t tmp_m 

if self.relprec != 0: 

mpz_init(tmp_m) 

mpz_set(tmp_m, num_unit) 

mpz_to_ZZ(&num_zz, tmp_m) 

mpz_set(tmp_m, den_unit) 

mpz_to_ZZ(&den_zz, tmp_m) 

mpz_clear(tmp_m) 

#The context has been restored in setting self.relprec 

ZZ_p_div(tmp_zp, ZZ_to_ZZ_p(num_zz), ZZ_to_ZZ_p(den_zz)) 

ZZ_pX_SetCoeff(self.unit, 0, tmp_zp) 

self.ordp = 0 

self._pshift_self(val) 

  

cdef int _set_from_ZZX_rel(self, ZZX_c poly, long relprec) except -1: 

""" 

Sets ``self`` from a ``ZZX`` with relative precision bounded by 

``relprec``. 

  

EXAMPLES:: 

  

sage: R = Zp(5,5) 

sage: S.<x> = R[] 

sage: f = x^5 + 75*x^3 - 15*x^2 +125*x - 5 

sage: W.<w> = R.ext(f) 

sage: z = W(ntl.ZZX([4,1,16]), relprec = 14); z # indirect doctest 

4 + w + w^2 + 3*w^7 + w^9 + 2*w^11 + 4*w^13 + O(w^14) 

sage: z._ntl_rep() 

[4 1 16] 

sage: z = W(ntl.ZZX([5^40,5^42,3*5^41]), relprec = 14); z 

w^200 + 4*w^207 + 4*w^209 + w^210 + 2*w^211 + 2*w^213 + O(w^214) 

sage: W(5)^40 + w*W(5)^42 + w^2 * W(3) * W(5)^41 

w^200 + 4*w^207 + 4*w^209 + w^210 + 2*w^211 + 2*w^213 + 2*w^215 + w^217 + 2*w^218 + w^220 + w^221 + w^222 + 3*w^224 + O(w^225) 

sage: z = W(ntl.ZZX([5^40,5^42,3*5^41]), relprec = 0); z 

O(w^200) 

""" 

if ZZX_IsZero(poly): 

self._set_exact_zero() 

return 0 

if ZZX_deg(poly) >= self.prime_pow.deg: 

raise NotImplementedError 

# the -1 in the next line signals that there is no absprec specified 

self._set_from_ZZX_part1(poly, -1, relprec) 

# context was restored in _set_from_ZZX_part1 

if relprec == 0: 

self._set_prec_rel(relprec) 

return 0 

if self.relprec + self.ordp != 0: 

self.prime_pow.restore_context_capdiv(self.relprec + self.ordp) 

ZZX_to_ZZ_pX(self.unit, poly) 

self._internal_lshift(-self.ordp) 

  

cdef int _set_from_ZZX_both(self, ZZX_c poly, long absprec, long relprec) except -1: 

""" 

Sets ``self`` from a ``ZZX`` with relative precision bounded by 

``relprec`` and absolute precision bounded by ``absprec``. 

  

EXAMPLES:: 

  

sage: R = Zp(5,5) 

sage: S.<x> = R[] 

sage: f = x^5 + 75*x^3 - 15*x^2 +125*x - 5 

sage: W.<w> = R.ext(f) 

sage: z = W(ntl.ZZX([4,1,16]), 12); z # indirect doctest 

4 + w + w^2 + 3*w^7 + w^9 + 2*w^11 + O(w^12) 

sage: z._ntl_rep() 

[4 1 16] 

sage: z = W(ntl.ZZX([5^40,5^42,3*5^41]), 212); z 

w^200 + 4*w^207 + 4*w^209 + w^210 + 2*w^211 + O(w^212) 

sage: z = W(ntl.ZZX([5^40,5^42,3*5^41]), 197); z 

O(w^197) 

""" 

if ZZX_IsZero(poly) or absprec <= 0: 

self._set_inexact_zero(absprec) 

return 0 

if ZZX_deg(poly) >= self.prime_pow.deg: 

raise NotImplementedError 

if self._set_from_ZZX_part1(poly, absprec, relprec) == -2: 

# indicates _set_inexact_zero was called 

return 0 

# context was restored in _set_from_ZZX_part1 

if self.relprec + self.ordp != 0: 

self.prime_pow.restore_context_capdiv(self.relprec + self.ordp) 

ZZX_to_ZZ_pX(self.unit, poly) 

self._internal_lshift(-self.ordp) 

  

cdef int _set_from_ZZX_part1(self, ZZX_c poly, long absprec, long relprec) except -1: 

""" 

Sets ``self.ordp`` from ``poly`` and restores the context. ``poly`` must 

have degree less than ``self.prime_pow.deg`` 

  

TESTS:: 

  

sage: R = Zp(5,5) 

sage: S.<x> = R[] 

sage: f = x^5 + 75*x^3 - 15*x^2 +125*x - 5 

sage: W.<w> = R.ext(f) 

sage: z = W(ntl.ZZX([4,1,16]), 12); z # indirect doctest 

4 + w + w^2 + 3*w^7 + w^9 + 2*w^11 + O(w^12) 

""" 

cdef long i = 0 

cdef long deg = ZZX_deg(poly) 

cdef long mini = -1 

cdef long minval 

cdef long curval 

cdef ZZ_c tmp_z 

while mini == -1: 

if not ZZ_IsZero(ZZX_coeff(poly,i)): 

minval = ZZ_remove(tmp_z, ZZX_coeff(poly, i), self.prime_pow.pow_ZZ_tmp(1)[0]) 

mini = i 

i += 1 

while i <= deg: 

if not ZZ_IsZero(ZZX_coeff(poly,i)): 

curval = ZZ_remove(tmp_z, ZZX_coeff(poly, i), self.prime_pow.pow_ZZ_tmp(1)[0]) 

if curval < minval: 

minval = curval 

mini = i 

i += 1 

if self.prime_pow.e == 1: 

self.ordp = minval 

else: 

self.ordp = minval * self.prime_pow.e + mini 

if absprec == -1: # indicates that _set_from_ZZX_rel is calling 

self._set_prec_rel(relprec) 

elif self._set_prec_both(absprec, relprec): 

# indicates self._set_inexact_zero was called 

return -2 

# _set_prec_rel or both has restored the context so that part2 works. 

  

cdef int _set_from_ZZ_pX_rel(self, ZZ_pX_c* poly, ntl_ZZ_pContext_class ctx, long relprec) except -1: 

""" 

Sets ``self`` from a ``ZZ_pX`` with relative precision bounded by 

``relprec``. 

  

If ``ctx`` is ``None`` and ``poly`` is 0 this function will raise an error 

(a ``ZZ_pX`` cannot represent something with infinite absolute 

precision). 

  

EXAMPLES:: 

  

sage: R = Zp(5,5) 

sage: S.<x> = R[] 

sage: f = x^5 + 75*x^3 - 15*x^2 +125*x - 5 

sage: W.<w> = R.ext(f) 

sage: z = W(ntl.ZZ_pX([4,1,16],5^2)); z # indirect doctest 

4 + w + w^2 + 3*w^7 + w^9 + O(w^10) 

sage: z._ntl_rep() 

[4 1 16] 

sage: z = W(ntl.ZZ_pX([5^40,5^42,3*5^41], 5^44)); z 

w^200 + 4*w^207 + 4*w^209 + w^210 + 2*w^211 + 2*w^213 + 2*w^215 + w^217 + 2*w^218 + O(w^220) 

sage: z = W(ntl.ZZ_pX([5^40,5^42,3*5^41], 5^44), relprec = 0); z 

O(w^200) 

""" 

cdef long ctx_prec = -1 

if ctx is not None: 

ctx_prec = self._check_ZZ_pContext(ctx) * self.prime_pow.e 

if ZZ_pX_IsZero(poly[0]): 

if ctx_prec == -1: 

raise ValueError("must specify either a context or an absolute precision bound") 

else: 

self._set_inexact_zero(ctx_prec) 

return 0 

self._set_from_ZZ_pX_part1(poly) 

if relprec == 0: 

self._set_prec_rel(relprec) 

return 0 

if ctx_prec == -1: 

self._set_prec_rel(self.ordp + relprec) 

else: 

self._set_prec_rel(min(ctx_prec, self.ordp + relprec)) 

self._set_from_ZZ_pX_part2(poly) 

  

cdef int _set_from_ZZ_pX_both(self, ZZ_pX_c* poly, ntl_ZZ_pContext_class ctx, long absprec, long relprec) except -1: 

""" 

Sets ``self`` from a ``ZZ_pX`` with relative precision bounded by 

``relprec`` and absolute precision bounded by ``absprec``. 

  

EXAMPLES:: 

  

sage: R = Zp(5,5) 

sage: S.<x> = R[] 

sage: f = x^5 + 75*x^3 - 15*x^2 +125*x - 5 

sage: W.<w> = R.ext(f) 

sage: z = W(ntl.ZZ_pX([4,1,16],5^2), absprec = 8, relprec = 12); z # indirect doctest 

4 + w + w^2 + 3*w^7 + O(w^8) 

sage: z._ntl_rep() 

[4 1 16] 

sage: z = W(ntl.ZZ_pX([5^40,5^42,3*5^41], 5^50), 220); z 

w^200 + 4*w^207 + 4*w^209 + w^210 + 2*w^211 + 2*w^213 + 2*w^215 + w^217 + 2*w^218 + O(w^220) 

sage: z = W(ntl.ZZ_pX([5^40,5^42,3*5^41], 5^44), absprec = 77); z 

O(w^77) 

""" 

cdef long ctx_prec 

if ctx is not None: 

ctx_prec = self._check_ZZ_pContext(ctx) 

if ctx_prec * self.prime_pow.e < absprec: 

absprec = ctx_prec * self.prime_pow.e 

if ZZ_pX_IsZero(poly[0]): 

self._set_inexact_zero(absprec) 

return 0 

self._set_from_ZZ_pX_part1(poly) 

if absprec <= self.ordp: 

self._set_inexact_zero(absprec) 

else: 

self._set_prec_rel(min(absprec, self.ordp + relprec)) 

self._set_from_ZZ_pX_part2(poly) 

  

cdef int _set_from_ZZ_pX_part1(self, ZZ_pX_c* poly) except -1: 

""" 

Sets ``self.ordp`` based on ``poly``. ``poly`` must not be 0. 

  

TESTS:: 

  

sage: R = Zp(5,5) 

sage: S.<x> = R[] 

sage: f = x^5 + 75*x^3 - 15*x^2 +125*x - 5 

sage: W.<w> = R.ext(f) 

sage: z = W(ntl.ZZ_pX([4,1,16],5^2), absprec = 8, relprec = 12); z # indirect doctest 

4 + w + w^2 + 3*w^7 + O(w^8) 

""" 

cdef long val, index 

ZZ_pX_min_val_coeff(val, index, poly[0], self.prime_pow.pow_ZZ_tmp(1)[0]) 

if self.prime_pow.e == 1: 

self.ordp = val 

else: 

self.ordp = val * self.prime_pow.e + index 

  

cdef int _set_from_ZZ_pX_part2(self, ZZ_pX_c* poly) except -1: 

""" 

Assuming that ``self.ordp`` and ``self.relprec`` have been set, sets 

``self.unit`` to ``poly`` and then normalizes. 

  

TESTS:: 

  

sage: R = Zp(5,5) 

sage: S.<x> = R[] 

sage: f = x^5 + 75*x^3 - 15*x^2 +125*x - 5 

sage: W.<w> = R.ext(f) 

sage: z = W(ntl.ZZ_pX([4,1,16],5^2), absprec = 8, relprec = 12); z # indirect doctest 

4 + w + w^2 + 3*w^7 + O(w^8) 

""" 

# We've set self.relprec to what is actually the absolute precision. 

if self.relprec != 0: 

ZZ_pX_conv_modulus(self.unit, poly[0], self.prime_pow.get_context_capdiv(self.relprec).x) 

self.relprec -= self.ordp 

self._internal_lshift(-self.ordp) 

  

cdef bint _set_prec_rel(self, long relprec) except -1: 

""" 

Safely sets the relative precision of ``self`` to be the absolute 

value of ``relprec``. 

  

Returns ``True`` iff ``self.relprec`` was reset. 

  

Note that this will wipe out anything in ``self.unit``. Be 

careful resetting ``self.unit`` directly: if you set it to a 

different modulus, NTL may have problems. The safest way to 

reset ``self.unit`` to a different modulus is:: 

  

self.prime_pow.restore_context_capdiv(self.relprec) 

cdef ZZ_pX_c tmp = self.unit 

self._set_prec_rel(new_rel_prec) 

ZZ_pX_conv_modulus(self.unit, tmp, self.prime_pow.get_context_capdiv(self.relprec).x) 

  

You may be able to just set ``self.relprec`` and 

``ZZ_pX_conv_modulus`` if you're decreasing precision. I'm 

not sure. 

  

TESTS:: 

  

sage: R = Zp(5,5) 

sage: S.<x> = R[] 

sage: f = x^5 + 75*x^3 - 15*x^2 +125*x - 5 

sage: W.<w> = R.ext(f) 

sage: W(70, relprec = 8) # indirect doctest 

4*w^5 + 3*w^7 + w^9 + 2*w^10 + 2*w^11 + O(w^13) 

""" 

if self.relprec == relprec: 

return False 

if relprec != 0: 

self.prime_pow.restore_context_capdiv(relprec) 

self.unit = ZZ_pX_c() 

self.relprec = relprec 

return True 

  

cdef bint _set_prec_both(self, long absprec, long relprec) except -1: 

""" 

Assuming ``self.ordp`` is set, sets the relative precision of ``self`` 

to the minimum of ``abs(relprec)`` and ``absprec-self.ordp``. 

  

If ``relprec`` is negative, will set ``self.relprec`` to be negative 

(indicating unnormalized unit) 

  

Returns`` True`` iff ``self.relprec = 0``, ie ``self`` was set to an 

inexact zero. 

  

Note that this will wipe out anything in ``self.unit``. Be 

careful resetting ``self.unit`` directly: if you set it to a 

different modulus, NTL may have problems. The safest way to 

reset ``self.unit`` to a different modulus is: 

  

self.prime_pow.restore_context_capdiv(self.relprec) 

cdef ZZ_pX_c tmp = self.unit 

self._set_prec_rel(new_rel_prec) 

ZZ_pX_conv_modulus(self.unit, tmp, self.prime_pow.get_context_capdiv(self.relprec).x) 

  

You may be able to just set ``self.relprec`` and 

``ZZ_pX_conv_modulus`` if you're decreasing precision. I'm 

not sure. 

  

TESTS:: 

  

sage: R = Zp(5,5) 

sage: S.<x> = R[] 

sage: f = x^5 + 75*x^3 - 15*x^2 +125*x - 5 

sage: W.<w> = R.ext(f) 

sage: W(70, 8) # indirect doctest 

4*w^5 + 3*w^7 + O(w^8) 

""" 

self.relprec = absprec - self.ordp 

cdef long arelprec 

if relprec < 0: 

arelprec = -relprec 

else: 

arelprec = relprec 

if self.relprec <= 0: 

self._set_inexact_zero(absprec) 

else: 

if arelprec < self.relprec: 

self.relprec = arelprec 

if self.relprec != 0: 

self.prime_pow.restore_context_capdiv(self.relprec) 

self.unit = ZZ_pX_c() 

if relprec < 0: 

self.relprec = -self.relprec 

return self.relprec == 0 

  

cdef int _normalize(self) except -1: 

""" 

Normalizes ``self``, adjusting ``self.ordp``, ``self.relprec``, and 

``self.unit`` so that ``self.unit`` actually represents a unit. 

  

EXAMPLES:: 

  

sage: R = Zp(5,5) 

sage: S.<x> = R[] 

sage: f = x^5 + 75*x^3 - 15*x^2 +125*x - 5 

sage: W.<w> = R.ext(f) 

sage: z = (1+w)^5 

sage: y = z - 1 

sage: y._ntl_rep_unnormalized() 

[5 3005 25 3060 5] 

sage: y # indirect doctest 

w^5 + w^6 + 2*w^7 + 4*w^8 + 3*w^10 + w^12 + 4*w^13 + 4*w^14 + 4*w^15 + 4*w^16 + 4*w^17 + 4*w^20 + w^21 + 4*w^24 + O(w^25) 

sage: y._ntl_rep_unnormalized() 

[41 26 152 49 535] 

""" 

cdef long minval, mini, shift 

if self.relprec < 0: 

if ZZ_pX_IsZero(self.unit): 

self.ordp -= self.relprec # note that self.relprec < 0 

self.relprec = 0 

else: 

ZZ_pX_min_val_coeff(minval, mini, self.unit, self.prime_pow.pow_ZZ_tmp(1)[0]) 

if self.prime_pow.e == 1: 

shift = minval 

else: 

shift = minval * self.prime_pow.e + mini 

if shift >= -self.relprec: 

self.ordp -= self.relprec # note that self.relprec < 0 

self.relprec = 0 

elif shift > 0: 

self.relprec = -self.relprec - shift 

self.ordp += shift 

self._internal_lshift(-shift) 

else: 

self.relprec = -self.relprec 

  

def _is_normalized(self): 

""" 

Returns whether this element is currently normalized. 

  

EXAMPLES:: 

  

sage: R.<a> = ZqCR(125,implementation="NTL"); b = 5*a + 4; c = 10*a^2 + 6; d = b + c 

sage: d._is_normalized() 

False 

sage: d.valuation() 

1 

sage: d._is_normalized() 

True 

""" 

return self.relprec >= 0 

  

cdef int _internal_lshift(self, long shift) except -1: 

""" 

Multiplies ``self.unit`` by ``x^shift``. 

  

Note that ``self.relprec`` must be set before calling this 

function and should not be 0, and self.unit must be defined to 

precision ``self.relprec - shift`` 

  

This function does not alter ``self.ordp`` even though it WILL 

change the valuation of ``self.unit`` 

  

Also note that if you call this function you should usually 

manually set ``self.relprec = -self.relprec`` since this function 

will usually unnormalize ``self``. 

  

TESTS:: 

  

sage: R = Zp(5,5) 

sage: S.<x> = R[] 

sage: f = x^5 + 75*x^3 - 15*x^2 +125*x - 5 

sage: W.<w> = R.ext(f) 

sage: z = (1+w)^5 

sage: y = z - 1 

sage: y # indirect doctest 

w^5 + w^6 + 2*w^7 + 4*w^8 + 3*w^10 + w^12 + 4*w^13 + 4*w^14 + 4*w^15 + 4*w^16 + 4*w^17 + 4*w^20 + w^21 + 4*w^24 + O(w^25) 

""" 

if self.relprec == 0: 

raise ValueError("p-adic Internal l-shift called with relative precision 0") 

cdef ZZ_pX_c tmpP 

cdef ZZ_pX_Modulus_c* mod 

if self.prime_pow.e == 1: 

if shift > 0: 

ZZ_pX_left_pshift(self.unit, self.unit, self.prime_pow.pow_ZZ_tmp(shift)[0], self.prime_pow.get_context(self.relprec).x) 

else: 

ZZ_pX_right_pshift(self.unit, self.unit, self.prime_pow.pow_ZZ_tmp(-shift)[0], self.prime_pow.get_context(self.relprec).x) 

else: 

if shift > 0: 

self.prime_pow.restore_context_capdiv(self.relprec) 

mod = self.prime_pow.get_modulus_capdiv(self.relprec) 

ZZ_pX_PowerXMod_long_pre(tmpP, shift, mod[0]) 

ZZ_pX_MulMod_pre(self.unit, self.unit, tmpP, mod[0]) 

elif shift < 0: 

self.prime_pow.eis_shift_capdiv(&self.unit, &self.unit, -shift, self.relprec) 

  

cdef int _pshift_self(self, long shift) except -1: 

""" 

Multiplies ``self`` by ``p^shift``. 

  

This function assumes that ``self.relprec``, ``self.ordp`` and 

``self.unit`` are already set (in the case ``self.prime_pow.e 

!= 1``), and is more reasonable to call externally than 

``_internal_lshift`` 

  

EXAMPLES:: 

  

sage: R = Qp(5,5) 

sage: S.<x> = R[] 

sage: f = x^5 + 75*x^3 - 15*x^2 +125*x - 5 

sage: W.<w> = R.ext(f) 

sage: z = W(3/25, relprec = 6); z 

3*w^-10 + 3*w^-8 + 2*w^-6 + O(w^-4) 

sage: z * 25 

3 + O(w^6) 

""" 

cdef ZZ_pX_c high_shifter, high_shifter2 

cdef ZZ_pX_Modulus_c *modulus 

cdef ZZ_pX_Modulus_c modulus_up 

cdef ntl_ZZ_pContext_class c 

cdef PowComputer_ZZ_pX_small_Eis sm 

cdef PowComputer_ZZ_pX_big_Eis big 

cdef ntl_ZZ_pX printer 

cdef ZZ_pX_c* high_array 

cdef long i, high_length 

if self.prime_pow.e == 1: 

self.ordp += shift 

else: 

self.ordp += shift * self.prime_pow.e 

if shift < 0: 

shift = -shift 

c = self.prime_pow.get_context_capdiv(self.relprec) 

c.restore_c() 

modulus = self.prime_pow.get_modulus_capdiv(self.relprec) 

if isinstance(self.prime_pow, PowComputer_ZZ_pX_big_Eis): 

high_array = (<PowComputer_ZZ_pX_big_Eis>self.prime_pow).high_shifter 

elif isinstance(self.prime_pow, PowComputer_ZZ_pX_small_Eis): 

high_array = (<PowComputer_ZZ_pX_small_Eis>self.prime_pow).high_shifter 

else: 

raise TypeError("unrecognized PowComputer type") 

ZZ_pX_conv_modulus(high_shifter, high_array[0], c.x) 

ZZ_pX_InvMod_newton_ram(high_shifter, high_shifter, modulus[0], c.x) 

ZZ_pX_PowerMod_long_pre(high_shifter, high_shifter, shift, modulus[0]) 

ZZ_pX_MulMod_pre(self.unit, self.unit, high_shifter, modulus[0]) 

elif shift > 0: 

i = 0 

c = self.prime_pow.get_context_capdiv(self.relprec) 

c.restore_c() 

modulus = self.prime_pow.get_modulus_capdiv(self.relprec) 

if isinstance(self.prime_pow, PowComputer_ZZ_pX_big_Eis): 

high_array = (<PowComputer_ZZ_pX_big_Eis>self.prime_pow).high_shifter 

high_length = (<PowComputer_ZZ_pX_big_Eis>self.prime_pow).high_length 

elif isinstance(self.prime_pow, PowComputer_ZZ_pX_small_Eis): 

high_array = (<PowComputer_ZZ_pX_small_Eis>self.prime_pow).high_shifter 

high_length = (<PowComputer_ZZ_pX_small_Eis>self.prime_pow).high_length 

else: 

raise TypeError("unrecognized PowComputer type") 

if shift >= self.prime_pow.prec_cap: 

# high_shifter = p^(2^(high_length - 1))/x^(e*2^(high_length - 1)) 

ZZ_pX_conv_modulus(high_shifter, high_array[high_length-1], c.x) 

# if shift = r + s * 2^(high_length - 1) 

# then high_shifter = p^(s*2^(high_length - 1))/x^(e*s*2^(high_length - 1)) 

ZZ_pX_PowerMod_long_pre(high_shifter, high_shifter, (shift / (1L << (high_length - 1))), modulus[0]) 

ZZ_pX_MulMod_pre(self.unit, self.unit, high_shifter, modulus[0]) 

# Now we only need to multiply self.unit by p^r/x^(e*r) where r < 2^(high_length - 1), which is tractable. 

shift = shift % (1L << (high_length - 1)) 

while shift > 0: 

if shift & 1: 

ZZ_pX_conv_modulus(high_shifter, high_array[i], c.x) 

ZZ_pX_MulMod_pre(self.unit, self.unit, high_shifter, modulus[0]) 

shift = shift >> 1 

i += 1 

  

cdef pAdicZZpXCRElement _new_c(self, long relprec): 

""" 

Returns a new element with the same parent as ``self`` and 

relative precision ``relprec`` 

  

Note that if ``relprec`` is non-positive, the convention is that 

``relprec = 0`` indicates an exact or inexact zero, ``relprec < 0`` 

indicates an unnormalized element. 

  

EXAMPLES:: 

  

sage: R = Qp(5,5) 

sage: S.<x> = R[] 

sage: f = x^5 + 75*x^3 - 15*x^2 +125*x - 5 

sage: W.<w> = R.ext(f) 

sage: w^5 + 1 # indirect doctest 

1 + w^5 + O(w^25) 

""" 

cdef pAdicZZpXCRElement ans = pAdicZZpXCRElement.__new__(pAdicZZpXCRElement) 

ans._parent = self._parent 

ans.prime_pow = self.prime_pow 

if relprec > 0: 

self.prime_pow.restore_context_capdiv(relprec) 

ans.relprec = relprec 

elif relprec == 0: 

ans._set_exact_zero() 

else: 

self.prime_pow.restore_context_capdiv(-relprec) 

ans.relprec = relprec 

return ans 

  

def __reduce__(self): 

""" 

Pickles ``self``. 

  

EXAMPLES:: 

  

sage: R = Qp(5,5) 

sage: S.<x> = R[] 

sage: f = x^5 + 75*x^3 - 15*x^2 +125*x - 5 

sage: W.<w> = R.ext(f) 

sage: z = (1 + w)^5 - 1 

sage: loads(dumps(z)) == z 

True 

""" 

cdef Integer relprec, ordp 

relprec = PY_NEW(Integer) 

ordp = PY_NEW(Integer) 

mpz_set_si(relprec.value, self.relprec) 

mpz_set_si(ordp.value, self.ordp) 

if self.relprec == 0: 

return make_ZZpXCRElement, (self.parent(), None, ordp, relprec, 0) 

self.prime_pow.restore_context_capdiv(self.relprec) 

cdef ntl_ZZ_pX holder = ntl_ZZ_pX.__new__(ntl_ZZ_pX) 

holder.c = self.prime_pow.get_context_capdiv(self.relprec) 

holder.x = self.unit 

return make_ZZpXCRElement, (self.parent(), holder, ordp, relprec, 0) 

  

cdef int _cmp_units(left, pAdicGenericElement right) except -2: 

""" 

For units ``left`` and ``right``, returns 0 if they are equal up to 

the lesser of the two precisions, or 1 if they are not. 

  

EXAMPLES:: 

  

sage: R = Qp(5,5) 

sage: S.<x> = R[] 

sage: f = x^5 + 75*x^3 - 15*x^2 +125*x - 5 

sage: W.<w> = R.ext(f) 

sage: w == 1 # indirect doctest 

False 

sage: y = 1 + w + O(w^7) 

sage: z = 1 + w + w^10 + O(w^13) 

sage: y == z 

True 

""" 

# This function needs improvement. In particular, there are a lot of 

# speed improvements to be had, and it should be changed so that it 

# returns 1 only half the time (and -1 the other half) when left and 

# right are not equal. 

cdef pAdicZZpXCRElement diff = <pAdicZZpXCRElement> (left - right) 

diff._normalize() 

if diff.relprec == 0: 

return 0 

# for now, just return 1 

return 1 

  

def __invert__(self): 

""" 

Returns the inverse of ``self``. 

  

EXAMPLES:: 

  

sage: R = Zp(5,5) 

sage: S.<x> = R[] 

sage: f = x^5 + 75*x^3 - 15*x^2 +125*x - 5 

sage: W.<w> = R.ext(f) 

sage: z = (1 + w)^5 

sage: y = ~z; y # indirect doctest 

1 + 4*w^5 + 4*w^6 + 3*w^7 + w^8 + 2*w^10 + w^11 + w^12 + 2*w^14 + 3*w^16 + 3*w^17 + 4*w^18 + 4*w^19 + 2*w^20 + 2*w^21 + 4*w^22 + 3*w^23 + 3*w^24 + O(w^25) 

sage: y.parent() 

Eisenstein Extension in w defined by x^5 + 75*x^3 - 15*x^2 + 125*x - 5 with capped relative precision 25 over 5-adic Field 

sage: z = z - 1 

sage: ~z 

w^-5 + 4*w^-4 + 4*w^-3 + 4*w^-2 + 2*w^-1 + 1 + w + 4*w^2 + 4*w^3 + 4*w^4 + w^5 + w^6 + w^7 + 4*w^8 + 4*w^9 + 2*w^10 + w^11 + 2*w^12 + 4*w^13 + 4*w^14 + O(w^15) 

sage: ~z * z 

1 + O(w^20) 

""" 

if self._is_exact_zero(): 

raise ZeroDivisionError("cannot divide by zero") 

if self._is_inexact_zero(): # this calls _normalize 

raise PrecisionError("cannot divide by something indistinguishable from zero") 

cdef pAdicZZpXCRElement ans = self._new_c(self.relprec) 

if not ans.prime_pow.in_field: 

ans._parent = self._parent.fraction_field() 

ans.prime_pow = ans._parent.prime_pow 

ans.ordp = -self.ordp 

sig_on() 

if self.prime_pow.e == 1: 

ZZ_pX_InvMod_newton_unram(ans.unit, self.unit, self.prime_pow.get_modulus(ans.relprec)[0], self.prime_pow.get_context(ans.relprec).x, self.prime_pow.get_context(1).x) 

else: 

ZZ_pX_InvMod_newton_ram(ans.unit, self.unit, self.prime_pow.get_modulus_capdiv(ans.relprec)[0], self.prime_pow.get_context_capdiv(ans.relprec).x) 

sig_off() 

return ans 

  

cdef pAdicZZpXCRElement _lshift_c(self, long n): 

""" 

Multiplies ``self`` by the uniformizer raised to the power ``n``. If 

``n`` is negative, right shifts by ``-n``. 

  

EXAMPLES:: 

  

sage: R = Zp(5,5) 

sage: S.<x> = R[] 

sage: f = x^5 + 75*x^3 - 15*x^2 +125*x - 5 

sage: W.<w> = R.ext(f) 

sage: z = (1 + w)^5 

sage: z 

1 + w^5 + w^6 + 2*w^7 + 4*w^8 + 3*w^10 + w^12 + 4*w^13 + 4*w^14 + 4*w^15 + 4*w^16 + 4*w^17 + 4*w^20 + w^21 + 4*w^24 + O(w^25) 

sage: z << 17 # indirect doctest 

w^17 + w^22 + w^23 + 2*w^24 + 4*w^25 + 3*w^27 + w^29 + 4*w^30 + 4*w^31 + 4*w^32 + 4*w^33 + 4*w^34 + 4*w^37 + w^38 + 4*w^41 + O(w^42) 

sage: z << (-1) 

w^4 + w^5 + 2*w^6 + 4*w^7 + 3*w^9 + w^11 + 4*w^12 + 4*w^13 + 4*w^14 + 4*w^15 + 4*w^16 + 4*w^19 + w^20 + 4*w^23 + O(w^24) 

""" 

if not self.prime_pow.in_field and n < -self.ordp: 

return self._rshift_c(-n) 

check_ordp(n) 

cdef pAdicZZpXCRElement ans 

if self._is_exact_zero() or n == 0: 

return self 

elif self._is_inexact_zero(): 

ans = self._new_c(0) 

else: 

ans = self._new_c(self.relprec) 

ans.unit = self.unit 

ans.ordp = self.ordp + n 

check_ordp(ans.ordp) 

return ans 

  

def __lshift__(pAdicZZpXCRElement self, shift): 

""" 

Multiplies ``self`` by the uniformizer raised to the power ``n``. If 

``n`` is negative, right shifts by ``-n``. 

  

EXAMPLES:: 

  

sage: R = Zp(5,5) 

sage: S.<x> = R[] 

sage: f = x^5 + 75*x^3 - 15*x^2 +125*x - 5 

sage: W.<w> = R.ext(f) 

sage: z = (1 + w)^5 

sage: z 

1 + w^5 + w^6 + 2*w^7 + 4*w^8 + 3*w^10 + w^12 + 4*w^13 + 4*w^14 + 4*w^15 + 4*w^16 + 4*w^17 + 4*w^20 + w^21 + 4*w^24 + O(w^25) 

sage: z << 17 # indirect doctest 

w^17 + w^22 + w^23 + 2*w^24 + 4*w^25 + 3*w^27 + w^29 + 4*w^30 + 4*w^31 + 4*w^32 + 4*w^33 + 4*w^34 + 4*w^37 + w^38 + 4*w^41 + O(w^42) 

sage: z << (-1) 

w^4 + w^5 + 2*w^6 + 4*w^7 + 3*w^9 + w^11 + 4*w^12 + 4*w^13 + 4*w^14 + 4*w^15 + 4*w^16 + 4*w^19 + w^20 + 4*w^23 + O(w^24) 

""" 

cdef pAdicZZpXCRElement ans 

if not isinstance(shift, Integer): 

shift = Integer(shift) 

if mpz_fits_slong_p((<Integer>shift).value) == 0: 

if self._is_exact_zero(): 

return self 

if self.prime_pow.in_field or mpz_sgn((<Integer>shift).value) > 0: 

raise ValueError("Shift does not fit in long") 

else: 

ans = self._new_c(0) 

ans.ordp = 0 

return ans 

return self._lshift_c(mpz_get_si((<Integer>shift).value)) 

  

cdef pAdicZZpXCRElement _rshift_c(self, long n): 

""" 

Divides self by the uniformizer raised to the power ``n``. If 

parent is not a field, throws away the non-positive part of 

the series expansion. If ``n`` is negative, left shifts by ``-n``. 

  

EXAMPLES:: 

  

sage: R = Zp(5,5,print_mode='digits') 

sage: S.<x> = R[] 

sage: f = x^5 + 75*x^3 - 15*x^2 +125*x - 5 

sage: W.<w> = R.ext(f) 

sage: z = (1 + w)^5 

sage: for m in range(26): repr(z >> m) # indirect doctest 

'...4001400444441030421100001' 

'...400140044444103042110000' 

'...40014004444410304211000' 

'...4001400444441030421100' 

'...400140044444103042110' 

'...40014004444410304211' 

'...4001400444441030421' 

'...400140044444103042' 

'...40014004444410304' 

'...4001400444441030' 

'...400140044444103' 

'...40014004444410' 

'...4001400444441' 

'...400140044444' 

'...40014004444' 

'...4001400444' 

'...400140044' 

'...40014004' 

'...4001400' 

'...400140' 

'...40014' 

'...4001' 

'...400' 

'...40' 

'...4' 

'...' 

sage: repr(z >> (-4)) 

'...40014004444410304211000010000' 

""" 

if self.prime_pow.in_field or n <= self.ordp: 

return self._lshift_c(-n) 

if self._is_exact_zero() or n == 0: 

return self 

cdef long arelprec 

if self.relprec < 0: 

arelprec = -self.relprec 

else: 

arelprec = self.relprec 

cdef pAdicZZpXCRElement ans 

if arelprec > n - self.ordp: 

ans = self._new_c(arelprec - (n - self.ordp)) 

if self.prime_pow.e == 1: 

ZZ_pX_right_pshift(ans.unit, self.unit, self.prime_pow.pow_ZZ_tmp(n - self.ordp)[0], self.prime_pow.get_context(ans.relprec).x) 

else: 

self.prime_pow.eis_shift_capdiv(&ans.unit, &self.unit, n - self.ordp, ans.relprec) 

else: 

ans = self._new_c(0) 

ans.ordp = 0 

ans.relprec = -ans.relprec 

return ans 

  

def __rshift__(pAdicZZpXCRElement self, shift): 

""" 

Divides self by the uniformizer raised to the power ``n``. If 

parent is not a field, throws away the non-positive part of 

the series expansion. If ``n`` is negative, left shifts by ``-n``. 

  

EXAMPLES:: 

  

sage: R = Zp(5,5) 

sage: S.<x> = R[] 

sage: f = x^5 + 75*x^3 - 15*x^2 +125*x - 5 

sage: W.<w> = R.ext(f) 

sage: z = (1 + w)^5 

sage: z 

1 + w^5 + w^6 + 2*w^7 + 4*w^8 + 3*w^10 + w^12 + 4*w^13 + 4*w^14 + 4*w^15 + 4*w^16 + 4*w^17 + 4*w^20 + w^21 + 4*w^24 + O(w^25) 

sage: z >> (6) # indirect doctest 

1 + 2*w + 4*w^2 + 3*w^4 + w^6 + 4*w^7 + 4*w^8 + 4*w^9 + 4*w^10 + 4*w^11 + 4*w^14 + w^15 + 4*w^18 + O(w^19) 

sage: z >> (-4) 

w^4 + w^9 + w^10 + 2*w^11 + 4*w^12 + 3*w^14 + w^16 + 4*w^17 + 4*w^18 + 4*w^19 + 4*w^20 + 4*w^21 + 4*w^24 + w^25 + 4*w^28 + O(w^29) 

sage: F = W.fraction_field() 

sage: z = F(z) 

sage: z >> 7 

w^-7 + w^-2 + w^-1 + 2 + 4*w + 3*w^3 + w^5 + 4*w^6 + 4*w^7 + 4*w^8 + 4*w^9 + 4*w^10 + 4*w^13 + w^14 + 4*w^17 + O(w^18) 

""" 

cdef pAdicZZpXCRElement ans 

if not isinstance(shift, Integer): 

shift = Integer(shift) 

if mpz_fits_slong_p((<Integer>shift).value) == 0: 

if self._is_exact_zero(): 

return self 

if self.prime_pow.in_field or mpz_sgn((<Integer>shift).value) < 0: 

raise ValueError("valuation overflow") 

else: 

ans = self._new_c(0) 

ans.ordp = 0 

return ans 

return self._rshift_c(mpz_get_si((<Integer>shift).value)) 

  

cpdef _neg_(self): 

""" 

Returns ``-self``. 

  

EXAMPLES:: 

  

sage: R = Zp(5,5) 

sage: S.<x> = R[] 

sage: f = x^5 + 75*x^3 - 15*x^2 +125*x - 5 

sage: W.<w> = R.ext(f) 

sage: z = (1 + w)^5; z 

1 + w^5 + w^6 + 2*w^7 + 4*w^8 + 3*w^10 + w^12 + 4*w^13 + 4*w^14 + 4*w^15 + 4*w^16 + 4*w^17 + 4*w^20 + w^21 + 4*w^24 + O(w^25) 

sage: -z # indirect doctest 

4 + 3*w^5 + 4*w^6 + w^7 + w^8 + w^9 + w^10 + w^11 + 2*w^12 + 4*w^13 + 4*w^15 + 3*w^16 + w^17 + 2*w^18 + 3*w^19 + 2*w^21 + 4*w^23 + 4*w^24 + O(w^25) 

sage: y = z + (-z); y 

O(w^25) 

sage: -y 

O(w^25) 

sage: -W(0) 

0 

""" 

cdef pAdicZZpXCRElement ans = self._new_c(self.relprec) 

ans.ordp = self.ordp 

if self.relprec != 0: 

self.prime_pow.restore_context_capdiv(self.relprec) 

ZZ_pX_negate(ans.unit, self.unit) 

return ans 

  

# / 1 + \alpha^p \pi_K^{p \lambda} mod \mathfrak{p}_K^{p \lambda + 1} if 1 \le \lambda < \frac{e_K}{p-1} 

# (1 + \alpha \pi^{\lambda})^p \equiv { 1 + (\alpha^p - \epsilon \alpha) \pi_K^{p \lambda} mod \mathfrak{p}_K^{p \lambda + 1} if \lambda = \frac{e_K}{p-1} 

# \ 1 - \epsilon \alpha \pi_K^{\lambda + e} mod \mathfrak{p}_K^{\lambda + e + 1} if \lambda > \frac{e_K}{p-1} 

  

  

def __pow__(pAdicZZpXCRElement self, _right, m): # m ignored 

r""" 

Computes ``self^right``. 

  

Note: when ``right`` is divisible by `p` then one can get more 

precision than expected. 

  

Lemma 2.1 (Constructing Class Fields over Local Fields, Sebastian Pauli): 

  

Let `\alpha` be in `\mathcal{O}_K`. Let 

  

.. MATH:: 

  

p = -\pi_K^{e_K} \epsilon 

  

be the factorization of `p` where `\epsilon` is a unit. Then 

the `p`-th power of `1 + \alpha \pi_K^{\lambda}` satisfies 

  

.. MATH:: 

  

(1 + \alpha \pi^{\lambda})^p \equiv \left{ \begin{array}{lll} 

1 + \alpha^p \pi_K^{p \lambda} & \mod \mathfrak{p}_K^{p \lambda + 1} & \mbox{if $1 \le \lambda < \frac{e_K}{p-1}$} \\ 

1 + (\alpha^p - \epsilon \alpha) \pi_K^{p \lambda} & \mod \mathfrak{p}_K^{p \lambda + 1} & \mbox{if $\lambda = \frac{e_K}{p-1}$} \\ 

1 - \epsilon \alpha \pi_K^{\lambda + e} & \mod \mathfrak{p}_K^{\lambda + e + 1} & \mbox{if $\lambda > \frac{e_K}{p-1}$} 

\end{array} \right. 

  

  

So if ``right`` is divisible by `p^k` we can multiply the 

relative precision by `p` until we exceed `e/(p-1)`, then add 

`e` until we have done a total of `k` things: the precision of 

the result can therefore be greater than the precision of 

``self``. 

  

There is also the issue of `p`-adic exponents, and determining 

how the precision of the exponent affects the precision of the 

result. 

  

In computing `(a + O(\pi^k))^{b + O(p^m)}`, one needs that the 

reduction of `a` mod `\pi` is in the prime field 

`\mathbb{F}_p` (so that the `p^m` power of the Teichmuller 

part is constant as `m` increases). Given this restriction, 

we can factor out the Teichmuller part and use the above lemma 

to find the first spot where 

  

.. MATH:: 

  

(1 + \alpha \pi^{\lambda})^{p^m} 

  

differs from 1. We compare this with the precision bound 

given by computing `(a + O(\pi^k))^b` and take the lesser of 

the two. 

  

In order to do this we need to compute the valuation of ``(self 

/ self.parent().teichmuller(self)) - 1``. 

  

EXAMPLES:: 

  

sage: R = Zp(5,5) 

sage: S.<x> = R[] 

sage: f = x^5 + 75*x^3 - 15*x^2 +125*x - 5 

sage: W.<w> = R.ext(f) 

sage: (1 + w)^5 # indirect doctest 

1 + w^5 + w^6 + 2*w^7 + 4*w^8 + 3*w^10 + w^12 + 4*w^13 + 4*w^14 + 4*w^15 + 4*w^16 + 4*w^17 + 4*w^20 + w^21 + 4*w^24 + O(w^25) 

sage: (1 + w)^-5 

1 + 4*w^5 + 4*w^6 + 3*w^7 + w^8 + 2*w^10 + w^11 + w^12 + 2*w^14 + 3*w^16 + 3*w^17 + 4*w^18 + 4*w^19 + 2*w^20 + 2*w^21 + 4*w^22 + 3*w^23 + 3*w^24 + O(w^25) 

sage: (1 + w + O(w^19))^5 

1 + w^5 + w^6 + 2*w^7 + 4*w^8 + 3*w^10 + w^12 + 4*w^13 + 4*w^14 + 4*w^15 + 4*w^16 + 4*w^17 + 4*w^20 + w^21 + O(w^24) 

sage: (1 + O(w))^5 

1 + O(w^5) 

sage: (1 + w + O(w^3))^25 

1 + w^10 + w^11 + 4*w^12 + O(w^13) 

sage: (3 + 2*w + w^2 + O(w^6))^(15 + O(125)) 

2 + 4*w^6 + w^7 + 3*w^8 + 3*w^9 + 4*w^10 + O(w^11) 

sage: (3 + 2*w + w^2 + O(w^6))^(15 + O(25)) 

2 + 4*w^6 + w^7 + 3*w^8 + 3*w^9 + O(w^10) 

sage: (3 + w^2 + O(w^6))^(15+O(25)) 

2 + w^5 + 4*w^7 + w^9 + 3*w^10 + O(w^11) 

sage: R = Zp(2, 10) 

sage: S.<x> = R[] 

sage: f = x^34 + 18*x^5 - 72*x^3 + 2 

sage: W.<w> = R.ext(f) 

sage: (1+w+O(w^2))^8 

1 + w^8 + O(w^16) 

sage: (1+w+O(w^2))^16 

1 + w^16 + O(w^32) 

sage: (1+w+O(w^2))^32 

1 + w^32 + w^50 + w^55 + w^60 + O(w^64) 

sage: (1+w+O(w^2))^64 

1 + w^64 + w^66 + w^71 + w^76 + w^81 + w^84 + w^86 + w^91 + w^94 + w^96 + O(w^98) 

  

TESTS: 

  

We define ``0^0`` to be unity, :trac:`13786`:: 

  

sage: R = Zp(5,5) 

sage: S.<x> = R[] 

sage: f = x^5 + 75*x^3 - 15*x^2 +125*x - 5 

sage: W.<w> = R.ext(f) 

sage: type(W(0)) 

<type 'sage.rings.padics.padic_ZZ_pX_CR_element.pAdicZZpXCRElement'> 

sage: W(0)^0 

1 + O(w^25) 

sage: W(0)^0 == W(1) 

True 

  

The value returned from ``0^0`` should belong to our ring:: 

  

sage: R = Zp(5,5) 

sage: S.<x> = R[] 

sage: f = x^5 + 75*x^3 - 15*x^2 +125*x - 5 

sage: W.<w> = R.ext(f) 

sage: type(W(0)^0) == type(W(0)) 

True 

  

""" 

self._normalize() 

cdef Integer right 

cdef bint padic_exp 

cdef long exp_prec 

cdef long exp_val 

cdef long relprec 

cdef long threshold # e / (p-1) 

cdef long prime_long 

cdef mpz_t tmp, tmp2 

if mpz_fits_slong_p(self.prime_pow.prime.value) == 0: 

threshold = 0 

else: 

threshold = self.prime_pow.e / (mpz_get_si(self.prime_pow.prime.value) - 1) 

cdef Integer base_level 

cdef pAdicZZpXCRElement ans 

cdef long i 

if self._is_exact_zero(): 

# Return 0 except for 0^0 error or type error on the exponent. 

if isinstance(_right, Integer) or isinstance(_right, Rational) or (isinstance(_right, pAdicGenericElement) and _right._is_base_elt(self.prime_pow.prime)) or isinstance(_right, (int, long)): 

if _right == 0: 

return self.parent(1) 

return self 

else: 

raise TypeError("exponent must be an integer, rational or base p-adic with the same prime") 

elif self._is_inexact_zero(): 

# If an integer exponent, return an inexact zero of valuation right * self.ordp. Otherwise raise an error. 

if isinstance(_right, (int, long)): 

_right = Integer(_right) 

if isinstance(_right, Integer): 

ans = self._new_c(0) 

mpz_init_set_si(tmp, self.ordp) 

mpz_mul(tmp, tmp, (<Integer>_right).value) 

if mpz_cmp_si(tmp, maxordp) >= 0 or mpz_cmp_si(tmp, -maxordp) <= 0: 

raise ValueError("valuation overflow") 

ans.ordp = mpz_get_si(tmp) 

mpz_clear(tmp) 

return ans 

elif isinstance(_right, Rational) or (isinstance(_right, pAdicGenericElement) and _right._is_base_elt(self.prime_pow.prime)): 

raise ValueError("Need more precision") 

else: 

raise TypeError("exponent must be an integer, rational or base p-adic with the same prime") 

if isinstance(_right, (int, long)): 

_right = Integer(_right) 

if isinstance(_right, Integer): 

right = <Integer> _right 

if right == 0: 

# return 1 to maximum precision 

ans = self._new_c(self.prime_pow.ram_prec_cap) 

ans.ordp = 0 

ZZ_pX_SetCoeff_long(ans.unit, 0, 1) 

return ans 

padic_exp = False 

exp_val = _right.valuation(self.prime_pow.prime) ## 

elif isinstance(_right, pAdicGenericElement) and _right._is_base_elt(self.prime_pow.prime): 

if self.ordp != 0: 

raise ValueError("in order to raise to a p-adic exponent, base must be a unit") 

right = Integer(_right) 

padic_exp = True 

exp_prec = _right.precision_absolute() ## 

exp_val = _right.valuation() ## 

if exp_val < 0: 

raise NotImplementedError("negative valuation exponents not yet supported") 

# checks to see if the residue of self.unit is in the prime field. 

if self.prime_pow.e == 1: 

for i from 1 <= i <= ZZ_pX_deg(self.unit): 

if not ZZ_divide_test(ZZ_p_rep(ZZ_pX_coeff(self.unit, i)), self.prime_pow.pow_ZZ_tmp(1)[0]): 

raise ValueError("in order to raise to a p-adic exponent, base must reduce to an element of F_p mod the uniformizer") 

# compute the "level" 

teich_part = self.parent().teichmuller(self) 

base_level = (self / teich_part - 1).valuation() ## 

elif isinstance(_right, Rational): 

raise NotImplementedError 

else: 

raise TypeError("exponent must be an integer, rational or base p-adic with the same prime") 

# Now we compute the increased relprec due to the exponent having positive p-adic valuation 

if exp_val > 0: 

mpz_init_set_si(tmp, self.relprec) 

while mpz_cmp_si(tmp, threshold) <= 0 and exp_val > 0: 

mpz_mul(tmp, tmp, self.prime_pow.prime.value) 

exp_val -= 1 

if exp_val > 0: 

mpz_init_set_si(tmp2, self.prime_pow.e) 

mpz_addmul_ui(tmp, tmp2, exp_val) 

mpz_clear(tmp2) 

if mpz_cmp_si(tmp, self.prime_pow.ram_prec_cap) > 0: 

relprec = self.prime_pow.ram_prec_cap 

else: 

relprec = mpz_get_si(tmp) 

mpz_clear(tmp) 

else: 

relprec = self.relprec 

# Now we compute the limit on relprec due to a non-infinite precision on the exponent. 

if padic_exp: 

if exp_prec > 0: 

# I can freely change base_level, so I use it in place of tmp above. 

while mpz_cmp_si(base_level.value, threshold) <= 0 and exp_prec > 0: 

mpz_mul(base_level.value, base_level.value, self.prime_pow.prime.value) 

exp_prec -= 1 

if exp_prec > 0: 

mpz_init_set_si(tmp2, self.prime_pow.e) 

mpz_addmul_ui(base_level.value, tmp2, exp_prec) 

mpz_clear(tmp2) 

if mpz_cmp_si(base_level.value, relprec) < 0: 

relprec = mpz_get_si(base_level.value) 

else: 

ans = self._new_c(0) 

ans.ordp = 0 

return ans 

ans = self._new_c(relprec) 

if self.ordp == 0: 

ans.ordp = 0 

else: 

mpz_init_set(tmp, right.value) 

mpz_mul_si(tmp, tmp, self.ordp) 

if mpz_cmp_si(tmp, maxordp) >= 0 or mpz_cmp_si(tmp, -maxordp) <= 0: 

raise ValueError("valuation overflow") 

ans.ordp = mpz_get_si(tmp) 

mpz_clear(tmp) 

cdef ntl_ZZ rZZ = ntl_ZZ.__new__(ntl_ZZ) 

mpz_to_ZZ(&rZZ.x, right.value) 

sig_on() 

if mpz_sgn(right.value) < 0: 

if self.prime_pow.e == 1: 

ZZ_pX_InvMod_newton_unram(ans.unit, self.unit, self.prime_pow.get_modulus(ans.relprec)[0], self.prime_pow.get_context(ans.relprec).x, self.prime_pow.get_context(1).x) 

else: 

ZZ_pX_InvMod_newton_ram(ans.unit, self.unit, self.prime_pow.get_modulus_capdiv(ans.relprec)[0], self.prime_pow.get_context_capdiv(ans.relprec).x) 

ZZ_negate(rZZ.x, rZZ.x) 

ZZ_pX_PowerMod_pre(ans.unit, ans.unit, rZZ.x, self.prime_pow.get_modulus_capdiv(ans.relprec)[0]) 

else: 

ZZ_pX_PowerMod_pre(ans.unit, self.unit, rZZ.x, self.prime_pow.get_modulus_capdiv(ans.relprec)[0]) 

sig_off() 

return ans 

  

cpdef _add_(self, _right): 

""" 

Computes the sum of ``self`` and ``right``. 

  

EXAMPLES:: 

  

sage: R = Zp(5,5) 

sage: S.<x> = R[] 

sage: f = x^5 + 75*x^3 - 15*x^2 +125*x - 5 

sage: W.<w> = R.ext(f) 

sage: (4*w^5 + 3*w^7 + w^9 + 2*w^10 + 2*w^11 + O(w^13)) - 69 # indirect doctest 

1 + O(w^13) 

sage: -69 + (4*w^5 + 3*w^7 + w^9 + 2*w^10 + 2*w^11 + O(w^13)) 

1 + O(w^13) 

sage: y = (4*w^5 + 3*w^7 + w^9 + 2*w^10 + 2*w^11 + O(w^13)) 

sage: y - 70 

O(w^13) 

sage: y + 0 

4*w^5 + 3*w^7 + w^9 + 2*w^10 + 2*w^11 + O(w^13) 

""" 

cdef pAdicZZpXCRElement right = <pAdicZZpXCRElement>_right 

cdef pAdicZZpXCRElement ans 

cdef long tmpL 

cdef ZZ_pX_c tmpP 

if self.relprec == 0: 

if self.ordp >= right.ordp + right.relprec: # or self._is_exact_zero() 

return right 

elif self.ordp <= right.ordp: 

ans = self._new_c(0) 

ans.ordp = self.ordp 

else: 

ans = self._new_c(self.ordp - right.ordp) 

ZZ_pX_conv_modulus(ans.unit, right.unit, self.prime_pow.get_context_capdiv(ans.relprec).x) 

if right.relprec < 0: 

ans.relprec = -ans.relprec 

ans.ordp = right.ordp 

return ans 

if right.relprec == 0: 

if right.ordp >= self.ordp + self.relprec: # or right._is_exact_zero() 

return self 

elif right.ordp <= self.ordp: 

ans = self._new_c(0) 

ans.ordp = right.ordp 

else: 

ans = self._new_c(right.ordp - self.ordp) 

ZZ_pX_conv_modulus(ans.unit, self.unit, self.prime_pow.get_context_capdiv(ans.relprec).x) 

if self.relprec < 0: 

ans.relprec = -ans.relprec 

ans.ordp = self.ordp 

return ans 

cdef long srprec = self.relprec 

if srprec < 0: 

srprec = -srprec 

cdef long rrprec = right.relprec 

if rrprec < 0: 

rrprec = -rrprec 

if self.ordp == right.ordp: 

# The relative precision of the sum is the minimum of the relative precisions in this case, possibly decreasing if we got cancellation 

# Since the valuations are the same, we could just add the units, if they had the same modulus. 

# But they don't necessarily, so we may have to conv_modulus 

if srprec == rrprec: 

ans = self._new_c(-srprec) # -srprec indicates that ans is not normalized 

self.prime_pow.restore_context_capdiv(srprec) 

ZZ_pX_add(ans.unit, self.unit, right.unit) 

elif srprec < rrprec: 

ans = self._new_c(-srprec) 

ZZ_pX_conv_modulus(ans.unit, right.unit, self.prime_pow.get_context_capdiv(srprec).x) 

self.prime_pow.restore_context_capdiv(srprec) 

# conv_modulus should have restored the context, so we don't need to again. 

ZZ_pX_add(ans.unit, ans.unit, self.unit) 

else: 

ans = self._new_c(-rrprec) 

ZZ_pX_conv_modulus(ans.unit, self.unit, self.prime_pow.get_context_capdiv(rrprec).x) 

self.prime_pow.restore_context_capdiv(rrprec) 

# conv_modulus should have restored the context, so we don't need to again. 

ZZ_pX_add(ans.unit, ans.unit, right.unit) 

ans.ordp = self.ordp 

elif self.ordp < right.ordp: 

tmpL = right.ordp - self.ordp 

if tmpL >= srprec: 

return self 

if srprec <= tmpL + rrprec: 

ans = self._new_c(-srprec) 

else: 

ans = self._new_c(-tmpL - rrprec) 

ans.ordp = self.ordp 

ZZ_pX_conv_modulus(ans.unit, right.unit, self.prime_pow.get_context_capdiv(ans.relprec).x) 

ans._internal_lshift(tmpL) 

if srprec <= tmpL + rrprec: 

ZZ_pX_add(ans.unit, ans.unit, self.unit) 

else: 

ZZ_pX_conv_modulus(tmpP, self.unit, self.prime_pow.get_context_capdiv(ans.relprec).x) 

ZZ_pX_add(ans.unit, ans.unit, tmpP) 

# if self is normalized, then the valuations are actually different so the sum will be normalized. 

if self.relprec > 0: 

ans.relprec = -ans.relprec 

else: 

tmpL = self.ordp - right.ordp 

if tmpL >= rrprec: 

return right 

if rrprec <= tmpL + srprec: 

ans = self._new_c(-rrprec) 

else: 

ans = self._new_c(-tmpL - srprec) 

ans.ordp = right.ordp 

ZZ_pX_conv_modulus(ans.unit, self.unit, self.prime_pow.get_context_capdiv(ans.relprec).x) 

ans._internal_lshift(tmpL) 

if rrprec <= tmpL + srprec: 

ZZ_pX_add(ans.unit, ans.unit, right.unit) 

else: 

ZZ_pX_conv_modulus(tmpP, right.unit, self.prime_pow.get_context_capdiv(ans.relprec).x) 

ZZ_pX_add(ans.unit, ans.unit, tmpP) 

# if right is normalized, then the valuations are actually different so the sum will be normalized. 

if right.relprec > 0: 

ans.relprec = -ans.relprec 

return ans 

  

cpdef _sub_(self, right): 

""" 

Returns the difference of ``self`` and ``right``. 

  

EXAMPLES:: 

  

sage: R = Zp(5,5) 

sage: S.<x> = R[] 

sage: f = x^5 + 75*x^3 - 15*x^2 +125*x - 5 

sage: W.<w> = R.ext(f) 

sage: a = W(329) 

sage: b = W(111) 

sage: a - b #indirect doctest 

3 + 3*w^5 + w^7 + 2*w^9 + 3*w^10 + 4*w^11 + 2*w^13 + 2*w^14 + w^15 + 4*w^16 + 2*w^18 + 3*w^19 + 2*w^20 + 3*w^21 + w^22 + w^24 + O(w^25) 

sage: W(218) 

3 + 3*w^5 + w^7 + 2*w^9 + 3*w^10 + 4*w^11 + 2*w^13 + 2*w^14 + w^15 + 4*w^16 + 2*w^18 + 3*w^19 + 2*w^20 + 3*w^21 + w^22 + w^24 + O(w^25) 

sage: a - O(w^14) 

4 + 3*w^10 + 2*w^12 + O(w^14) 

sage: a - 0 

4 + 3*w^10 + 2*w^12 + w^14 + 2*w^15 + w^16 + 3*w^17 + 3*w^18 + w^19 + 2*w^21 + 4*w^22 + w^23 + 4*w^24 + O(w^25) 

sage: O(w^14) - a 

1 + 4*w^5 + 3*w^7 + w^9 + w^10 + 2*w^11 + w^12 + w^13 + O(w^14) 

""" 

# For now, a simple implementation 

return self + (-right) 

  

cpdef _mul_(self, _right): 

""" 

Returns the product of ``self`` and ``right``. 

  

EXAMPLES:: 

  

sage: R = Zp(5,5) 

sage: S.<x> = R[] 

sage: f = x^5 + 75*x^3 - 15*x^2 +125*x - 5 

sage: W.<w> = R.ext(f) 

sage: a = W(329) 

sage: b = W(111) 

sage: a*b #indirect doctest 

4 + 3*w^5 + w^7 + 2*w^9 + 4*w^11 + 3*w^12 + 2*w^13 + w^14 + 2*w^15 + 3*w^16 + 4*w^17 + 4*w^18 + 2*w^19 + 2*w^21 + 4*w^22 + 2*w^23 + w^24 + O(w^25) 

sage: a * 0 

0 

sage: a * O(w^14) 

O(w^14) 

""" 

cdef pAdicZZpXCRElement right = <pAdicZZpXCRElement>_right 

cdef ZZ_pX_c modulus_corrected 

cdef ntl_ZZ_pContext_class ctx 

cdef pAdicZZpXCRElement ans 

if self._is_exact_zero(): 

return self 

if right._is_exact_zero(): 

return right 

self._normalize() 

right._normalize() 

if self.relprec <= right.relprec: 

ans = self._new_c(self.relprec) 

else: 

ans = self._new_c(right.relprec) 

ans.ordp = self.ordp + right.ordp 

check_ordp(ans.ordp) 

if ans.relprec == 0: 

return ans 

if self.relprec == right.relprec: 

self.prime_pow.restore_context_capdiv(ans.relprec) 

sig_on() 

ZZ_pX_MulMod_pre(ans.unit, self.unit, right.unit, self.prime_pow.get_modulus_capdiv(ans.relprec)[0]) 

sig_off() 

elif self.relprec < right.relprec: 

sig_on() 

ZZ_pX_conv_modulus(modulus_corrected, right.unit, self.prime_pow.get_context_capdiv(ans.relprec).x) 

ZZ_pX_MulMod_pre(ans.unit, self.unit, modulus_corrected, self.prime_pow.get_modulus_capdiv(ans.relprec)[0]) 

sig_off() 

else: 

sig_on() 

ZZ_pX_conv_modulus(modulus_corrected, self.unit, self.prime_pow.get_context_capdiv(ans.relprec).x) 

ZZ_pX_MulMod_pre(ans.unit, right.unit, modulus_corrected, self.prime_pow.get_modulus_capdiv(ans.relprec)[0]) 

sig_off() 

return ans 

  

cpdef _div_(self, right): 

""" 

Returns the quotient of ``self`` by ``right``. 

  

EXAMPLES:: 

  

sage: R = Zp(5,5) 

sage: S.<x> = R[] 

sage: f = x^5 + 75*x^3 - 15*x^2 +125*x - 5 

sage: W.<w> = R.ext(f) 

sage: W(14) / W(125) #indirect doctest 

4*w^-15 + w^-13 + 3*w^-11 + 2*w^-10 + 3*w^-9 + 4*w^-8 + 4*w^-7 + 3*w^-6 + 2*w^-5 + 4*w^-4 + 3*w^-3 + 2*w^-2 + 4*w^-1 + 2 + w^2 + w^4 + 4*w^5 + w^6 + w^7 + 3*w^9 + O(w^10) 

sage: 1 / w 

w^-1 + O(w^24) 

sage: W.<w> = R.ext(x^25 - 165*x + 5) 

sage: a = (1 + w)^25 - 1 

sage: b = (1 + w)^5 - 1 

sage: c = (1 + w)^20 + (1 + w)^15 + (1 + w)^10 + (1 + w)^5 + 1 

sage: d = a / b; d == c 

True 

sage: d.precision_absolute() 

120 

sage: c.precision_absolute() 

125 

sage: 1 / a == ~a 

True 

""" 

# for now, a simple implementation 

return self * (~right) 

  

def __copy__(self): 

""" 

Returns a copy of ``self``. 

  

EXAMPLES:: 

  

sage: R = Zp(5,5) 

sage: S.<x> = R[] 

sage: f = x^5 + 75*x^3 - 15*x^2 +125*x - 5 

sage: W.<w> = R.ext(f) 

sage: b = W(45, 17); b 

4*w^5 + 3*w^7 + w^9 + w^10 + 2*w^11 + w^12 + w^13 + 3*w^14 + w^16 + O(w^17) 

sage: c = copy(b); c 

4*w^5 + 3*w^7 + w^9 + w^10 + 2*w^11 + w^12 + w^13 + 3*w^14 + w^16 + O(w^17) 

sage: c is b 

False 

""" 

cdef pAdicZZpXCRElement ans = self._new_c(self.relprec) 

ans.ordp = self.ordp 

ans.unit = self.unit 

return ans 

  

def _integer_(self, Z=None): 

""" 

Returns an integer congruent to this element modulo 

`\pi`^``self.absolute_precision()``, if possible. 

  

EXAMPLES:: 

  

sage: ZZ(ZqCR(125,names='a')(-1)) #indirect doctest 

95367431640624 

sage: R = Zp(5); S.<x> = ZZ[]; f = x^5 + 25*x^3 - 5; W.<w> = R.ext(f) 

sage: ZZ(W(-1)) 

95367431640624 

sage: ZZ(W(0)) 

0 

sage: ZZ(W(0,7)) 

0 

sage: ZZ(w) 

Traceback (most recent call last): 

... 

ValueError: This element not well approximated by an integer. 

sage: ZZ(W(5)) # todo: this should be different... 

381469726562505 

""" 

cdef Integer ans 

cdef ZZ_c tmp_z 

if self._is_exact_zero() or self.relprec == 0: 

ans = PY_NEW(Integer) 

return ans 

if self.ordp < 0: 

self._normalize() 

if self.ordp < 0: 

raise ValueError("This element has negative valuation") 

cdef ntl_ZZ_pX f = <ntl_ZZ_pX>self._ntl_rep_abs()[0] 

if f.degree() > 0: 

raise ValueError("This element not well approximated by an integer.") 

ans = PY_NEW(Integer) 

tmp_z = ZZ_p_rep(ZZ_pX_ConstTerm(f.x)) 

ZZ_to_mpz(ans.value, &tmp_z) 

return ans 

  

def is_zero(self, absprec = None): 

""" 

Returns whether the valuation of ``self`` is at least 

``absprec``. If ``absprec`` is ``None``, returns if ``self`` 

is indistinguishable from zero. 

  

If ``self`` is an inexact zero of valuation less than ``absprec``, 

raises a PrecisionError. 

  

EXAMPLES:: 

  

sage: R = Zp(5,5) 

sage: S.<x> = R[] 

sage: f = x^5 + 75*x^3 - 15*x^2 +125*x - 5 

sage: W.<w> = R.ext(f) 

sage: O(w^189).is_zero() 

True 

sage: W(0).is_zero() 

True 

sage: a = W(675) 

sage: a.is_zero() 

False 

sage: a.is_zero(7) 

True 

sage: a.is_zero(21) 

False 

""" 

cdef bint ans 

cdef long aprec 

self._normalize() 

if self._is_exact_zero(): 

ans = True 

elif absprec is None: 

ans = (self.relprec == 0) 

else: 

if not isinstance(absprec, Integer): 

absprec = Integer(absprec) 

if mpz_fits_slong_p((<Integer>absprec).value) == 0: 

if mpz_sgn((<Integer>absprec).value) < 0: 

ans = True 

elif self.relprec == 0: 

raise PrecisionError("Not enough precision to determine if element is zero") 

else: 

ans = False 

else: 

aprec = mpz_get_si((<Integer>absprec).value) 

if self.relprec == 0 and aprec > self.ordp: 

raise PrecisionError("Not enough precision to determine if element is zero") 

else: 

ans = (self.ordp >= aprec) 

return ans 

  

cpdef ntl_ZZ_pX _ntl_rep_unnormalized(self): 

""" 

Returns an ``ntl_ZZ_pX`` holding the current unit part of ``self``. 

  

``self`` is not normalized before this, so the polynomial 

returned may not actually be a unit. 

  

EXAMPLES:: 

  

sage: R = Zp(5,5) 

sage: S.<x> = R[] 

sage: f = x^5 + 75*x^3 - 15*x^2 +125*x - 5 

sage: W.<w> = R.ext(f) 

sage: a = W(566); b = W(209) 

sage: c = a + b; c._ntl_rep_unnormalized() 

[775] 

sage: c 

w^10 + 4*w^12 + 2*w^14 + w^15 + 2*w^16 + 4*w^17 + w^18 + w^20 + 2*w^21 + 3*w^22 + w^23 + w^24 + O(w^25) 

sage: c._ntl_rep_unnormalized() 

[106 60 114 35 112] 

""" 

if self.relprec == 0: 

raise ValueError("self == 0") 

self.prime_pow.restore_context_capdiv(self.relprec) 

cdef ntl_ZZ_pX ans = ntl_ZZ_pX.__new__(ntl_ZZ_pX) 

ans.c = self.prime_pow.get_context_capdiv(self.relprec) 

ans.x = self.unit 

return ans 

  

cpdef ntl_ZZ_pX _ntl_rep(self): 

""" 

Returns an ``ntl_ZZ_pX`` that holds the unit part of ``self``. 

  

EXAMPLES:: 

  

sage: R = Zp(5,5) 

sage: S.<x> = R[] 

sage: f = x^5 + 75*x^3 - 15*x^2 +125*x - 5 

sage: W.<w> = R.ext(f) 

sage: a = W(566); b = W(209) 

sage: c = a + b; c._ntl_rep() 

[106 60 114 35 112] 

sage: c 

w^10 + 4*w^12 + 2*w^14 + w^15 + 2*w^16 + 4*w^17 + w^18 + w^20 + 2*w^21 + 3*w^22 + w^23 + w^24 + O(w^25) 

sage: c._ntl_rep() 

[106 60 114 35 112] 

""" 

self._normalize() 

return self._ntl_rep_unnormalized() 

  

cpdef _ntl_rep_abs(self): 

""" 

Returns a pair ``(f, k)`` where ``f`` is an ``ntl_ZZ_pX`` and ``k`` is a 

non-positive integer such that ``self = f(self.parent.gen())*p^k`` 

  

EXAMPLES:: 

  

sage: R = Zp(5,5) 

sage: S.<x> = R[] 

sage: f = x^5 + 75*x^3 - 15*x^2 +125*x - 5 

sage: W.<w> = R.ext(f) 

sage: a = W(566); b = W(209) 

sage: c = a + b; c._ntl_rep_abs() 

([775], 0) 

sage: c 

w^10 + 4*w^12 + 2*w^14 + w^15 + 2*w^16 + 4*w^17 + w^18 + w^20 + 2*w^21 + 3*w^22 + w^23 + w^24 + O(w^25) 

sage: c._ntl_rep_abs() 

([775], 0) 

sage: (~c)._ntl_rep_abs() 

([121], -2) 

sage: ~c 

w^-10 + w^-8 + 4*w^-6 + 4*w^-5 + 3*w^-3 + 4*w^-2 + 3*w^-1 + 4 + 4*w + 2*w^2 + 4*w^3 + 3*w^4 + O(w^5) 

sage: ~c * 25 

1 + 4*w^5 + 3*w^7 + w^9 + 4*w^10 + 2*w^11 + 3*w^12 + w^13 + 4*w^14 + O(w^15) 

sage: W(121) 

1 + 4*w^5 + 3*w^7 + w^9 + 4*w^10 + 2*w^11 + 3*w^12 + w^13 + 4*w^14 + 2*w^16 + 3*w^17 + 3*w^18 + 4*w^19 + 4*w^20 + 3*w^21 + w^22 + w^23 + 4*w^24 + O(w^25) 

""" 

self._normalize() 

if self.ordp == 0: 

return self._ntl_rep(), Integer(0) 

cdef ntl_ZZ_pContext_class ctx 

cdef long little_shift, ppow 

if self.ordp > 0: 

ctx = self.prime_pow.get_context_capdiv(self.ordp + self.relprec) 

else: 

little_shift = ((-self.ordp) % self.prime_pow.e) 

if little_shift != 0: 

little_shift = self.prime_pow.e - little_shift 

ctx = self.prime_pow.get_context_capdiv(self.relprec + little_shift) 

ctx.restore_c() 

cdef pAdicZZpXCRElement dummy = pAdicZZpXCRElement.__new__(pAdicZZpXCRElement) 

cdef ntl_ZZ_pX ans = ntl_ZZ_pX.__new__(ntl_ZZ_pX) 

cdef Integer ans_k = PY_NEW(Integer) 

dummy.unit = self.unit 

dummy.prime_pow = self.prime_pow 

if self.ordp > 0: 

dummy.relprec = self.ordp + self.relprec 

dummy._internal_lshift(self.ordp) 

ans.x = dummy.unit 

else: 

ppow = (self.ordp - little_shift) / self.prime_pow.e 

mpz_set_si(ans_k.value, ppow) 

dummy.ordp = 0 # _pshift_self wants ordp set 

dummy.relprec = self.relprec + little_shift 

# self = x^(self.prime_pow.e * ppow) * x^(little_shift) * self.unit 

# so we want to _internal_lshift dummy.unit by little_shift 

dummy._internal_lshift(little_shift) 

# and then write 

# self = p^(ppow) * (x^e/p)^(ppow) * dummy.unit 

# so we need to multiply dummy.unit by (p/x^e)^(-ppow) in the Eisenstein case 

# which we can do by _pshift_self 

dummy._pshift_self(-ppow) 

ans.x = dummy.unit 

ans.c = ctx 

return ans, ans_k 

  

def polynomial(self, var='x'): 

""" 

Returns a polynomial over the base ring that yields this element 

when evaluated at the generator of the parent. 

  

INPUT: 

  

- ``var`` -- string, the variable name for the polynomial 

  

EXAMPLES:: 

  

sage: S.<x> = ZZ[] 

sage: W.<w> = Zp(5).extension(x^2 - 5) 

sage: (w + W(5, 7)).polynomial() 

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

""" 

R = self.base_ring() 

S = R[var] 

if self.is_zero(): 

return S([]) 

prec = self.relprec + self.ordp 

e = self.parent().e() 

f, k = self._ntl_rep_abs() 

L = [Integer(c) for c in f.list()] 

if e == 1: 

L = [R(c, prec) >> k for c in L] 

else: 

L = [R(c, (prec - i - 1) // e + 1) >> k for i, c in enumerate(L)] 

return S(L) 

  

cdef ZZ_p_c _const_term(self): 

""" 

Returns the constant term of ``self.unit``. 

  

Note: this may be divisible by `p` if ``self`` is not normalized. 

  

EXAMPLES:: 

  

sage: R = Zp(5,5) 

sage: S.<x> = R[] 

sage: f = x^5 + 75*x^3 - 15*x^2 +125*x - 5 

sage: W.<w> = R.ext(f) 

sage: a = W(566) 

sage: a._const_term_test() #indirect doctest 

566 

""" 

return ZZ_pX_ConstTerm((<pAdicZZpXCRElement>self).unit) 

  

def is_equal_to(self, right, absprec = None): 

""" 

Returns whether ``self`` is equal to ``right`` modulo ``self.uniformizer()^absprec``. 

  

If ``absprec`` is ``None``, returns if ``self`` is equal to ``right`` modulo the lower of their two precisions. 

  

EXAMPLES:: 

  

sage: R = Zp(5,5) 

sage: S.<x> = R[] 

sage: f = x^5 + 75*x^3 - 15*x^2 +125*x - 5 

sage: W.<w> = R.ext(f) 

sage: a = W(47); b = W(47 + 25) 

sage: a.is_equal_to(b) 

False 

sage: a.is_equal_to(b, 7) 

True 

""" 

# Should be sped up later 

return (self - right).is_zero(absprec) 

  

# def lift(self): 

# """ 

# Returns an element of a number field defined by the same polynomial as self's parent that is congruent to self modulo an appropriate ideal. 

  

# Not currently implemented. 

# """ 

# raise NotImplementedError 

  

cpdef pAdicZZpXCRElement lift_to_precision(self, absprec=None): 

""" 

Returns a ``pAdicZZpXCRElement`` congruent to ``self`` but with 

absolute precision at least ``absprec``. 

  

INPUT: 

  

- ``absprec`` -- (default ``None``) the absolute precision of 

the result. If ``None``, lifts to the maximum precision 

allowed. 

  

.. NOTE:: 

  

If setting ``absprec`` that high would violate the 

precision cap, raises a precision error. If self is an 

inexact zero and ``absprec`` is greater than the maximum 

allowed valuation, raises an error. 

  

Note that the new digits will not necessarily be zero. 

  

EXAMPLES:: 

  

sage: R = Zp(5,5) 

sage: S.<x> = R[] 

sage: f = x^5 + 75*x^3 - 15*x^2 +125*x - 5 

sage: W.<w> = R.ext(f) 

sage: a = W(345, 17); a 

4*w^5 + 3*w^7 + w^9 + 3*w^10 + 2*w^11 + 4*w^12 + w^13 + 2*w^14 + 2*w^15 + O(w^17) 

sage: b = a.lift_to_precision(19); b 

4*w^5 + 3*w^7 + w^9 + 3*w^10 + 2*w^11 + 4*w^12 + w^13 + 2*w^14 + 2*w^15 + w^17 + 2*w^18 + O(w^19) 

sage: c = a.lift_to_precision(24); c 

4*w^5 + 3*w^7 + w^9 + 3*w^10 + 2*w^11 + 4*w^12 + w^13 + 2*w^14 + 2*w^15 + w^17 + 2*w^18 + 4*w^19 + 4*w^20 + 2*w^21 + 4*w^23 + O(w^24) 

sage: a._ntl_rep() 

[19 35 118 60 121] 

sage: b._ntl_rep() 

[19 35 118 60 121] 

sage: c._ntl_rep() 

[19 35 118 60 121] 

sage: a.lift_to_precision().precision_relative() == W.precision_cap() 

True 

""" 

cdef pAdicZZpXCRElement ans 

cdef long aprec, rprec 

self._normalize() 

if self._is_exact_zero(): 

return self 

if absprec is not None and not isinstance(absprec, Integer): 

absprec = Integer(absprec) 

if absprec is None: 

if self.relprec == 0: 

# return an exact zero 

ans = self._new_c(0) 

ans._set_exact_zero() 

return ans 

aprec = self.prime_pow.ram_prec_cap + self.ordp 

elif mpz_fits_slong_p((<Integer>absprec).value) == 0: 

if mpz_sgn((<Integer>absprec).value) < 0 or self.relprec == self.prime_pow.ram_prec_cap: 

return self 

else: 

if self.relprec == 0: 

raise ValueError("absprec larger than maximum allowable valuation") 

else: 

raise PrecisionError("Precision higher than allowed by the precision cap.") 

else: 

aprec = mpz_get_si((<Integer>absprec).value) 

if aprec <= self.ordp + self.relprec: 

return self 

if self.relprec == 0: 

if self.ordp >= aprec: 

return self 

elif aprec >= maxordp: 

raise ValueError("absprec larger than maximum allowable valuation") 

else: 

ans = self._new_c(0) 

ans._set_inexact_zero(aprec) 

return ans 

# Now we're done handling all the special cases. 

rprec = aprec - self.ordp 

if rprec > self.prime_pow.ram_prec_cap: 

raise PrecisionError("Precision higher than allowed by the precision cap.") 

ans = self._new_c(rprec) 

ans.ordp = self.ordp 

ZZ_pX_conv_modulus(ans.unit, self.unit, self.prime_pow.get_context_capdiv(rprec).x) 

return ans 

  

def expansion(self, n = None, lift_mode = 'simple'): 

""" 

Returns a list giving a series representation of self. 

  

- If ``lift_mode == 'simple'`` or ``'smallest'``, the returned 

list will consist of integers (in the Eisenstein case) or a 

list of lists of integers (in the unramified case). ``self`` 

can be reconstructed as a sum of elements of the list times 

powers of the uniformiser (in the Eisenstein case), or as a 

sum of powers of the `p` times polynomials in the generator 

(in the unramified case). 

  

+ If ``lift_mode == 'simple'``, all integers will be in the interval 

`[0,p-1]`. 

  

+ If ``lift_mode == 'smallest'`` they will be in the 

interval `[(1-p)/2, p/2]`. 

  

- If ``lift_mode == 'teichmuller'``, returns a list of 

``pAdicZZpXCRElements``, all of which are Teichmuller 

representatives and such that ``self`` is the sum of that list 

times powers of the uniformizer. 

  

Note that zeros are truncated from the returned list if 

``self.parent()`` is a field, so you must use the 

``valuation`` function to fully reconstruct ``self``. 

  

INPUT: 

  

- ``n`` -- integer (default ``None``). If given, returns the corresponding 

entry in the expansion. 

  

EXAMPLES:: 

  

sage: R = Zp(5,5) 

sage: S.<x> = R[] 

sage: f = x^5 + 75*x^3 - 15*x^2 +125*x - 5 

sage: W.<w> = R.ext(f) 

sage: y = W(775, 19); y 

w^10 + 4*w^12 + 2*w^14 + w^15 + 2*w^16 + 4*w^17 + w^18 + O(w^19) 

sage: (y>>9).expansion() 

[0, 1, 0, 4, 0, 2, 1, 2, 4, 1] 

sage: (y>>9).expansion(lift_mode='smallest') 

[0, 1, 0, -1, 0, 2, 1, 2, 0, 1] 

sage: w^10 - w^12 + 2*w^14 + w^15 + 2*w^16 + w^18 + O(w^19) 

w^10 + 4*w^12 + 2*w^14 + w^15 + 2*w^16 + 4*w^17 + w^18 + O(w^19) 

sage: g = x^3 + 3*x + 3 

sage: A.<a> = R.ext(g) 

sage: y = 75 + 45*a + 1200*a^2; y 

4*a*5 + (3*a^2 + a + 3)*5^2 + 4*a^2*5^3 + a^2*5^4 + O(5^6) 

sage: E = y.expansion(); E 

5-adic expansion of 4*a*5 + (3*a^2 + a + 3)*5^2 + 4*a^2*5^3 + a^2*5^4 + O(5^6) 

sage: list(E) 

[[], [0, 4], [3, 1, 3], [0, 0, 4], [0, 0, 1], []] 

sage: list(y.expansion(lift_mode='smallest')) 

[[], [0, -1], [-2, 2, -2], [1], [0, 0, 2], []] 

sage: 5*((-2*5 + 25) + (-1 + 2*5)*a + (-2*5 + 2*125)*a^2) 

4*a*5 + (3*a^2 + a + 3)*5^2 + 4*a^2*5^3 + a^2*5^4 + O(5^6) 

sage: list(W(0).expansion()) 

[] 

sage: list(W(0,4).expansion()) 

[] 

sage: list(A(0,4).expansion()) 

[] 

""" 

if lift_mode == 'teichmuller': 

zero = self.parent()(0) 

elif self.prime_pow.e == 1: 

zero = [] 

else: 

zero = Integer(0) 

if n in ('simple', 'smallest', 'teichmuller'): 

deprecation(14825, "Interface to expansion has changed; first argument now n") 

lift_mode = n 

n = None 

elif isinstance(n, slice): 

return self.slice(n.start, n.stop, n.step) 

elif n is not None: 

if self._is_exact_zero() or n < self.ordp: 

return zero 

elif n >= self.ordp + self.relprec: 

raise PrecisionError 

cdef Integer ordp 

if self.relprec == 0: # cannot have n = None 

return [] 

if lift_mode == 'simple': 

ulist = self.ext_p_list(pos=True) 

elif lift_mode == 'smallest': 

ulist = self.ext_p_list(pos=False) 

elif lift_mode == 'teichmuller': 

if n is None: 

ulist = self.teichmuller_expansion() 

else: 

return self.teichmuller_expansion(n) 

else: 

raise ValueError("lift mode must be one of 'simple', 'smallest' or 'teichmuller'") 

if n is not None: 

try: 

return ulist[n - self.ordp] 

except IndexError: 

return zero 

if self.prime_pow.in_field == 0 and self.ordp > 0: 

ulist = [zero] * self.ordp + ulist 

return ulist 

  

list = deprecated_function_alias(14825, expansion) 

  

def matrix_mod_pn(self): 

""" 

Returns the matrix of right multiplication by the element on 

the power basis `1, x, x^2, \ldots, x^{d-1}` for this 

extension field. Thus the *rows* of this matrix give the 

images of each of the `x^i`. The entries of the matrices are 

IntegerMod elements, defined modulo `p^{N / e}` where `N` is 

the absolute precision of this element (unless this element is 

zero to arbitrary precision; in that case the entries are 

integer zeros.) 

  

Raises an error if this element has negative valuation. 

  

EXAMPLES:: 

  

sage: R = ZpCR(5,5) 

sage: S.<x> = R[] 

sage: f = x^5 + 75*x^3 - 15*x^2 +125*x - 5 

sage: W.<w> = R.ext(f) 

sage: a = (3+w)^7 

sage: a.matrix_mod_pn() 

[2757 333 1068 725 2510] 

[ 50 1507 483 318 725] 

[ 500 50 3007 2358 318] 

[1590 1375 1695 1032 2358] 

[2415 590 2370 2970 1032] 

  

TESTS: 

  

Check that :trac:`13617` has been fixed:: 

  

sage: W.zero().matrix_mod_pn() 

[0 0 0 0 0] 

[0 0 0 0 0] 

[0 0 0 0 0] 

[0 0 0 0 0] 

[0 0 0 0 0] 

  

""" 

if self.valuation_c() < 0: 

raise ValueError("self must be integral") 

n = self.prime_pow.deg 

from sage.matrix.all import matrix 

if self._is_exact_zero(): 

from sage.rings.integer_ring import IntegerRing 

return matrix(IntegerRing(), n, n) 

R = IntegerModRing(self.prime_pow.pow_Integer(self.prime_pow.capdiv(self.ordp + self.relprec))) 

L = [] 

cdef ntl_ZZ_pX cur = <ntl_ZZ_pX>self._ntl_rep_abs()[0] 

cur.c.restore_c() 

cdef ZZ_pX_Modulus_c* m = self.prime_pow.get_modulus_capdiv(self.ordp + self.relprec) 

cdef ZZ_pX_c x 

ZZ_pX_SetX(x) 

cdef Py_ssize_t i, j 

zero = int(0) 

for i from 0 <= i < n: 

curlist = cur.list() 

L.extend(curlist + [zero]*(n - len(curlist))) 

ZZ_pX_MulMod_pre(cur.x, cur.x, x, m[0]) 

return matrix(R, n, n, L) 

  

# def matrix(self, base = None): 

# """ 

# If base is None, return the matrix of right multiplication by 

# the element on the power basis `1, x, x^2, \ldots, x^{d-1}` 

# for this extension field. Thus the \emph{rows} of this matrix 

# give the images of each of the `x^i`. 

  

# If base is not None, then base must be either a field that 

# embeds in the parent of self or a morphism to the parent of 

# self, in which case this function returns the matrix of 

# multiplication by self on the power basis, where we view the 

# parent field as a field over base. 

  

# INPUT: 

# base -- field or morphism 

# """ 

# raise NotImplementedError 

  

# def multiplicative_order(self, prec=None): 

# """ 

# Returns the multiplicative order of self, ie the smallest 

# positive n so that there is an exact p-adic element congruent 

# to self modulo self's precision that is an nth root of unity. 

  

# Note: unlike the case for Qp and Zp, it is possible to have 

# non-teichmuller elements with finite orders. This can happen 

# only if (p-1) divides the ramification index (see the 

# documentation on __pow__). 

  

# INPUT: 

  

# - self -- a p-adic element 

# - prec -- an integer 

  

# OUTPUT: 

  

# - integer -- the multiplicative order of self 

# """ 

# raise NotImplementedError 

  

def teichmuller_expansion(self, n = None): 

r""" 

Returns a list [`a_0`, `a_1`,..., `a_n`] such that 

  

- `a_i^q = a_i` 

- ``self.unit_part()`` = `\sum_{i = 0}^n a_i \pi^i`, where `\pi` is a 

uniformizer of ``self.parent()`` 

- if `a_i \ne 0`, the absolute precision of `a_i` is 

``self.precision_relative() - i`` 

  

INPUT: 

  

- ``n`` -- integer (default ``None``). If given, returns the corresponding 

entry in the expansion. 

  

EXAMPLES:: 

  

sage: R.<a> = ZqCR(5^4,4) 

sage: E = a.teichmuller_expansion(); E 

5-adic expansion of a + O(5^4) (teichmuller) 

sage: list(E) 

[a + (2*a^3 + 2*a^2 + 3*a + 4)*5 + (4*a^3 + 3*a^2 + 3*a + 2)*5^2 + (4*a^2 + 2*a + 2)*5^3 + O(5^4), (3*a^3 + 3*a^2 + 2*a + 1) + (a^3 + 4*a^2 + 1)*5 + (a^2 + 4*a + 4)*5^2 + O(5^3), (4*a^3 + 2*a^2 + a + 1) + (2*a^3 + 2*a^2 + 2*a + 4)*5 + O(5^2), (a^3 + a^2 + a + 4) + O(5)] 

sage: sum([c * 5^i for i, c in enumerate(E)]) 

a + O(5^4) 

sage: all([c^625 == c for c in E]) 

True 

  

sage: S.<x> = ZZ[] 

sage: f = x^3 - 98*x + 7 

sage: W.<w> = ZpCR(7,3).ext(f) 

sage: b = (1+w)^5; L = b.teichmuller_expansion(); L 

[1 + O(w^9), 5 + 5*w^3 + w^6 + 4*w^7 + O(w^8), 3 + 3*w^3 + O(w^7), 3 + 3*w^3 + O(w^6), O(w^5), 4 + 5*w^3 + O(w^4), 3 + O(w^3), 6 + O(w^2), 6 + O(w)] 

sage: sum([w^i*L[i] for i in range(9)]) == b 

True 

sage: all([L[i]^(7^3) == L[i] for i in range(9)]) 

True 

  

sage: L = W(3).teichmuller_expansion(); L 

[3 + 3*w^3 + w^7 + O(w^9), O(w^8), O(w^7), 4 + 5*w^3 + O(w^6), O(w^5), O(w^4), 3 + O(w^3), 6 + O(w^2)] 

sage: sum([w^i*L[i] for i in range(len(L))]) 

3 + O(w^9) 

""" 

cdef long rp = self.relprec 

cdef pAdicZZpXCRElement v 

if n is None: 

L = [] 

if rp == 0: 

return L 

elif self._is_exact_zero() or n < self.ordp: 

return self.parent()(0) 

elif n >= self.ordp + rp: 

raise PrecisionError 

else: 

v = self._new_c(rp) 

cdef pAdicZZpXCRElement u = self.unit_part() 

cdef long goal 

if n is not None: goal = rp - n + self.ordp 

while u.relprec > 0: 

v = self._new_c(rp) 

self.prime_pow.teichmuller_set_c(&v.unit, &u.unit, rp) 

v.ordp = 0 

if n is None: 

L.append(v) 

elif rp == goal: 

return v 

if rp == 1: break 

ZZ_pX_sub(u.unit, u.unit, v.unit) 

u.relprec = -u.relprec 

u._normalize() 

if u.relprec == 0: break 

rp -= 1 

u.ordp -= 1 

while u.ordp > 0: 

if n is None: 

v = self._new_c(0) 

v._set_inexact_zero(rp) 

L.append(v) 

elif rp == goal: 

v = self._new_c(0) 

v._set_inexact_zero(rp) 

return v 

rp -= 1 

u.ordp -= 1 

if n is None: 

return L 

else: 

v = self._new_c(0) 

v._set_inexact_zero(rp) 

return v 

  

teichmuller_list = deprecated_function_alias(14825, teichmuller_expansion) 

  

def _teichmuller_set_unsafe(self): 

""" 

Sets this element to the Teichmuller representative with the 

same residue. 

  

.. WARNING:: 

  

This function modifies the element, which is not safe. 

Elements are supposed to be immutable. 

  

EXAMPLES:: 

  

sage: R = Zp(7,5) 

sage: S.<x> = R[] 

sage: f = x^5 + 77*x^3 - 98*x^2 - 7 

sage: W.<w> = R.ext(f) 

sage: y = W.teichmuller(3, 15); y #indirect doctest 

3 + 4*w^5 + 2*w^8 + 6*w^10 + w^11 + 6*w^12 + 5*w^13 + 4*w^14 + O(w^15) 

  

sage: y^7 == y 

True 

sage: g = x^3 + 3*x^2 + 4 

sage: A.<a> = R.ext(g) 

sage: b = A.teichmuller(1 + 2*a - a^2); b 

(6*a^2 + 2*a + 1) + (5*a + 3)*7 + (5*a + 5)*7^2 + (4*a^2 + 4*a + 2)*7^3 + (2*a + 1)*7^4 + O(7^5) 

sage: b^343 == b 

True 

  

TESTS: 

  

We check that :trac:`8239` is resolved:: 

  

sage: K.<a> = Qq(25) 

sage: K.teichmuller(K(2/5)) 

Traceback (most recent call last): 

... 

ValueError: cannot set negative valuation element to Teichmuller representative. 

""" 

self._normalize() 

if self.ordp > 0: 

self._set_exact_zero() 

elif self.ordp < 0: 

raise ValueError("cannot set negative valuation element to Teichmuller representative.") 

elif self.relprec == 0: 

raise ValueError("not enough precision known") 

else: 

self.prime_pow.teichmuller_set_c(&self.unit, &self.unit, self.relprec) 

  

# def padded_list(self, n, lift_mode = 'simple'): 

# """ 

# Returns a list of coefficients of pi starting with `pi^0` up to 

# `pi^n` exclusive (padded with zeros if needed) 

  

# """ 

# raise NotImplementedError 

  

def precision_absolute(self): 

""" 

Returns the absolute precision of ``self``, ie the power of the 

uniformizer modulo which this element is defined. 

  

EXAMPLES:: 

  

sage: R = Zp(5,5) 

sage: S.<x> = R[] 

sage: f = x^5 + 75*x^3 - 15*x^2 +125*x - 5 

sage: W.<w> = R.ext(f) 

sage: a = W(75, 19); a 

3*w^10 + 2*w^12 + w^14 + w^16 + w^17 + 3*w^18 + O(w^19) 

sage: a.valuation() 

10 

sage: a.precision_absolute() 

19 

sage: a.precision_relative() 

9 

sage: a.unit_part() 

3 + 2*w^2 + w^4 + w^6 + w^7 + 3*w^8 + O(w^9) 

sage: (a.unit_part() - 3).precision_absolute() 

9 

""" 

cdef Integer ans 

if self.ordp == maxordp: 

return infinity 

else: 

ans = PY_NEW(Integer) 

if self.relprec > 0: 

mpz_set_si(ans.value, self.relprec + self.ordp) 

else: 

mpz_set_si(ans.value, -self.relprec + self.ordp) 

return ans 

  

def precision_relative(self): 

""" 

Returns the relative precision of ``self``, ie the power of the 

uniformizer modulo which the unit part of ``self`` is defined. 

  

EXAMPLES:: 

  

sage: R = Zp(5,5) 

sage: S.<x> = R[] 

sage: f = x^5 + 75*x^3 - 15*x^2 +125*x - 5 

sage: W.<w> = R.ext(f) 

sage: a = W(75, 19); a 

3*w^10 + 2*w^12 + w^14 + w^16 + w^17 + 3*w^18 + O(w^19) 

sage: a.valuation() 

10 

sage: a.precision_absolute() 

19 

sage: a.precision_relative() 

9 

sage: a.unit_part() 

3 + 2*w^2 + w^4 + w^6 + w^7 + 3*w^8 + O(w^9) 

""" 

self._normalize() 

cdef Integer ans = PY_NEW(Integer) 

mpz_set_ui(ans.value, self.relprec) 

return ans 

  

# def residue(self, n = 1): 

# """ 

# Reduces this element modulo pi^n. 

# """ 

# raise NotImplementedError 

  

cdef long valuation_c(self): 

""" 

Returns the valuation of ``self``. 

  

EXAMPLES:: 

  

sage: R = Zp(5,5) 

sage: S.<x> = R[] 

sage: f = x^5 + 75*x^3 - 15*x^2 +125*x - 5 

sage: W.<w> = R.ext(f) 

sage: a = W(75, 19); a 

3*w^10 + 2*w^12 + w^14 + w^16 + w^17 + 3*w^18 + O(w^19) 

sage: a.valuation() # indirect doctest 

10 

sage: a.precision_absolute() 

19 

sage: a.precision_relative() 

9 

sage: a.unit_part() 

3 + 2*w^2 + w^4 + w^6 + w^7 + 3*w^8 + O(w^9) 

""" 

self._normalize() 

return self.ordp 

  

cpdef pAdicZZpXCRElement unit_part(self): 

""" 

Returns the unit part of ``self``, ie ``self / uniformizer^(self.valuation())`` 

  

EXAMPLES:: 

  

sage: R = Zp(5,5) 

sage: S.<x> = R[] 

sage: f = x^5 + 75*x^3 - 15*x^2 +125*x - 5 

sage: W.<w> = R.ext(f) 

sage: a = W(75, 19); a 

3*w^10 + 2*w^12 + w^14 + w^16 + w^17 + 3*w^18 + O(w^19) 

sage: a.valuation() 

10 

sage: a.precision_absolute() 

19 

sage: a.precision_relative() 

9 

sage: a.unit_part() 

3 + 2*w^2 + w^4 + w^6 + w^7 + 3*w^8 + O(w^9) 

  

TESTS: 

  

We check that :trac:`13616` is resolved:: 

  

sage: z = (1+w)^5 

sage: y = z - 1 

sage: t=y-y 

sage: t.unit_part() 

O(w^0) 

""" 

self._normalize() 

cdef pAdicZZpXCRElement ans = self._new_c(self.relprec) 

ans.ordp = 0 

if self.relprec != 0: 

ans.unit = self.unit 

return ans 

  

cdef ext_p_list(self, bint pos): 

""" 

Returns a list of integers (in the Eisenstein case) or a list 

of lists of integers (in the unramified case). ``self`` can be 

reconstructed as a sum of elements of the list times powers of 

the uniformiser (in the Eisenstein case), or as a sum of 

powers of `p` times polynomials in the generator (in the 

unramified case). 

  

If ``pos`` is ``True``, all integers will be in the interval `[0,p-1]`, 

otherwise they will be in the interval `[(1-p)/2, p/2]`. 

  

Note that zeros are truncated from the returned list, so you 

must use the ``valuation()`` function to completely recover ``self``. 

  

EXAMPLES:: 

  

sage: R = Zp(5,5) 

sage: S.<x> = R[] 

sage: f = x^5 + 75*x^3 - 15*x^2 +125*x - 5 

sage: W.<w> = R.ext(f) 

sage: y = W(775, 19); y 

w^10 + 4*w^12 + 2*w^14 + w^15 + 2*w^16 + 4*w^17 + w^18 + O(w^19) 

sage: y._ext_p_list(True) 

[1, 0, 4, 0, 2, 1, 2, 4, 1] 

sage: y._ext_p_list(False) 

[1, 0, -1, 0, 2, 1, 2, 0, 1] 

sage: w^10 - w^12 + 2*w^14 + w^15 + 2*w^16 + w^18 + O(w^19) 

w^10 + 4*w^12 + 2*w^14 + w^15 + 2*w^16 + 4*w^17 + w^18 + O(w^19) 

sage: g = x^3 + 3*x + 3 

sage: A.<a> = R.ext(g) 

sage: y = 75 + 45*a + 1200*a^2; y 

4*a*5 + (3*a^2 + a + 3)*5^2 + 4*a^2*5^3 + a^2*5^4 + O(5^6) 

sage: y._ext_p_list(True) 

[[0, 4], [3, 1, 3], [0, 0, 4], [0, 0, 1]] 

sage: y._ext_p_list(False) 

[[0, -1], [-2, 2, -2], [1], [0, 0, 2]] 

sage: 5*((-2*5 + 25) + (-1 + 2*5)*a + (-2*5 + 2*125)*a^2) 

4*a*5 + (3*a^2 + a + 3)*5^2 + 4*a^2*5^3 + a^2*5^4 + O(5^6) 

""" 

self._normalize() 

return self.ext_p_list_precs(pos, self.relprec) 

  

def make_ZZpXCRElement(parent, unit, ordp, relprec, version): 

""" 

Unpickling. 

  

EXAMPLES:: 

  

sage: R = Zp(5,5) 

sage: S.<x> = R[] 

sage: f = x^5 + 75*x^3 - 15*x^2 +125*x - 5 

sage: W.<w> = R.ext(f) 

sage: y = W(775, 19); y 

w^10 + 4*w^12 + 2*w^14 + w^15 + 2*w^16 + 4*w^17 + w^18 + O(w^19) 

sage: loads(dumps(y)) #indirect doctest 

w^10 + 4*w^12 + 2*w^14 + w^15 + 2*w^16 + 4*w^17 + w^18 + O(w^19) 

  

sage: from sage.rings.padics.padic_ZZ_pX_CR_element import make_ZZpXCRElement 

sage: make_ZZpXCRElement(W, y._ntl_rep(), 3, 9, 0) 

w^3 + 4*w^5 + 2*w^7 + w^8 + 2*w^9 + 4*w^10 + w^11 + O(w^12) 

""" 

cdef pAdicZZpXCRElement ans 

cdef ZZ_pX_c poly 

if version == 0: 

ans = pAdicZZpXCRElement(parent, [], empty = True) 

if relprec == 0: 

ans._set_inexact_zero(mpz_get_si((<Integer>ordp).value)) 

else: 

ans.prime_pow.restore_context_capdiv(mpz_get_si((<Integer>relprec).value)) 

poly = (<ntl_ZZ_pX>unit).x 

ans._set(&poly, mpz_get_si((<Integer>ordp).value), mpz_get_si((<Integer>relprec).value)) 

return ans 

else: 

raise ValueError("unknown unpickling version")