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

""" 

Capped relative template for complete discrete valuation rings and their fraction fields. 

  

In order to use this template you need to write a linkage file and gluing file. 

For an example see mpz_linkage.pxi (linkage file) and padic_capped_relative_element.pyx (gluing file). 

  

The linkage file implements a common API that is then used in the class CRElement defined here. 

See the documentation of mpz_linkage.pxi for the functions needed. 

  

The gluing file does the following: 

  

- ctypedef's celement to be the appropriate type (e.g. mpz_t) 

- includes the linkage file 

- includes this template 

- defines a concrete class inheriting from ``CRElement``, and implements 

any desired extra methods 

  

AUTHORS: 

  

- David Roe (2012-3-1) -- initial version 

""" 

  

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

# Copyright (C) 2007-2012 David Roe <roed.math@gmail.com> 

# William Stein <wstein@gmail.com> 

# 

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

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

  

# This file implements common functionality among template elements 

include "padic_template_element.pxi" 

  

from sage.structure.element cimport Element 

from sage.rings.padics.common_conversion cimport comb_prec, _process_args_and_kwds 

from sage.rings.integer_ring import ZZ 

from sage.rings.rational_field import QQ 

from sage.categories.sets_cat import Sets 

from sage.categories.sets_with_partial_maps import SetsWithPartialMaps 

from sage.categories.homset import Hom 

  

cdef inline bint exactzero(long ordp): 

""" 

Whether a given valuation represents an exact zero. 

""" 

return ordp >= maxordp 

  

cdef inline int check_ordp_mpz(mpz_t ordp) except -1: 

""" 

Checks for overflow after addition or subtraction of valuations. 

  

There is another variant, :meth:`check_ordp`, for long input. 

  

If overflow is detected, raises an ``OverflowError``. 

""" 

if mpz_fits_slong_p(ordp) == 0 or mpz_cmp_si(ordp, maxordp) > 0 or mpz_cmp_si(ordp, minusmaxordp) < 0: 

raise OverflowError("valuation overflow") 

  

cdef inline int assert_nonzero(CRElement x) except -1: 

""" 

Checks that ``x`` is distinguishable from zero. 

  

Used in division and floor division. 

""" 

if exactzero(x.ordp): 

raise ZeroDivisionError("cannot divide by zero") 

if x.relprec == 0: 

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

  

cdef class CRElement(pAdicTemplateElement): 

cdef int _set(self, x, long val, long xprec, absprec, relprec) except -1: 

""" 

Sets the value of this element from given defining data. 

  

This function is intended for use in conversion, and should 

not be called on an element created with :meth:`_new_c`. 

  

INPUT: 

  

- ``x`` -- data defining a `p`-adic element: int, long, 

Integer, Rational, other `p`-adic element... 

  

- ``val`` -- the valuation of the resulting element 

  

- ``xprec -- an inherent precision of ``x`` 

  

- ``absprec`` -- an absolute precision cap for this element 

  

- ``relprec`` -- a relative precision cap for this element 

  

TESTS:: 

  

sage: R = Zp(5) 

sage: R(15) #indirect doctest 

3*5 + O(5^21) 

sage: R(15, absprec=5) 

3*5 + O(5^5) 

sage: R(15, relprec=5) 

3*5 + O(5^6) 

sage: R(75, absprec = 10, relprec = 9) #indirect doctest 

3*5^2 + O(5^10) 

sage: R(25/9, relprec = 5) #indirect doctest 

4*5^2 + 2*5^3 + 5^5 + 2*5^6 + O(5^7) 

sage: R(25/9, relprec = 4, absprec = 5) #indirect doctest 

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

  

sage: R = Zp(5,5) 

sage: R(25/9) #indirect doctest 

4*5^2 + 2*5^3 + 5^5 + 2*5^6 + O(5^7) 

sage: R(25/9, absprec = 5) 

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

sage: R(25/9, relprec = 4) 

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

  

sage: R = Zp(5); S = Zp(5, 6) 

sage: S(R(17)) # indirect doctest 

2 + 3*5 + O(5^6) 

sage: S(R(17),4) # indirect doctest 

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

sage: T = Qp(5); a = T(1/5) - T(1/5) 

sage: R(a) 

O(5^19) 

sage: S(a) 

O(5^19) 

sage: S(a, 17) 

O(5^17) 

  

sage: R = Zp(5); S = ZpCA(5) 

sage: R(S(17, 5)) #indirect doctest 

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

""" 

cconstruct(self.unit, self.prime_pow) 

cdef long rprec = comb_prec(relprec, self.prime_pow.prec_cap) 

cdef long aprec = comb_prec(absprec, xprec) 

if aprec <= val: # this may also hit an exact zero, if aprec == val == maxordp 

self._set_inexact_zero(aprec) 

elif exactzero(val): 

self._set_exact_zero() 

else: 

self.relprec = min(rprec, aprec - val) 

self.ordp = val 

if isinstance(x,CRElement) and x.parent() is self.parent(): 

cshift(self.unit, (<CRElement>x).unit, 0, self.relprec, self.prime_pow, True) 

else: 

cconv(self.unit, x, self.relprec, val, self.prime_pow) 

  

cdef int _set_exact_zero(self) except -1: 

""" 

Sets ``self`` as an exact zero. 

  

TESTS:: 

  

sage: R = Zp(5); R(0) #indirect doctest 

0 

""" 

csetzero(self.unit, self.prime_pow) 

self.ordp = maxordp 

self.relprec = 0 

  

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

""" 

Sets ``self`` as an inexact zero with precision ``absprec``. 

  

TESTS:: 

  

sage: R = Zp(5); R(0, 5) #indirect doctest 

O(5^5) 

""" 

csetzero(self.unit, self.prime_pow) 

self.ordp = absprec 

self.relprec = 0 

  

cdef CRElement _new_c(self): 

""" 

Creates a new element with the same basic info. 

  

TESTS:: 

  

sage: R = Zp(5) 

sage: R(6,5) * R(7,8) #indirect doctest 

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

""" 

cdef type t = type(self) 

cdef CRElement ans = t.__new__(t) 

ans._parent = self._parent 

ans.prime_pow = self.prime_pow 

cconstruct(ans.unit, ans.prime_pow) 

return ans 

  

cdef pAdicTemplateElement _new_with_value(self, celement value, long absprec): 

""" 

Creates a new element with a given value and absolute precision. 

  

Used by code that doesn't know the precision type. 

""" 

cdef CRElement ans = self._new_c() 

ans.relprec = absprec 

ans.ordp = 0 

ccopy(ans.unit, value, ans.prime_pow) 

ans._normalize() 

return ans 

  

cdef int _get_unit(self, celement value) except -1: 

""" 

Sets ``value`` to the unit of this p-adic element. 

""" 

ccopy(value, self.unit, self.prime_pow) 

  

cdef int check_preccap(self) except -1: 

""" 

Checks that this element doesn't have precision higher than 

allowed by the precision cap. 

  

TESTS:: 

  

sage: Zp(5)(1).lift_to_precision(30) 

Traceback (most recent call last): 

... 

PrecisionError: Precision higher than allowed by the precision cap. 

""" 

if self.relprec > self.prime_pow.prec_cap: 

raise PrecisionError("Precision higher than allowed by the precision cap.") 

  

def __copy__(self): 

""" 

Return a copy of this element. 

  

EXAMPLES:: 

  

sage: a = Zp(5,6)(17); b = copy(a) 

sage: a == b 

True 

sage: a is b 

False 

""" 

cdef CRElement ans = self._new_c() 

ans.relprec = self.relprec 

ans.ordp = self.ordp 

ccopy(ans.unit, self.unit, ans.prime_pow) 

return ans 

  

cdef int _normalize(self) except -1: 

""" 

Normalizes this element, so that ``self.ordp`` is correct. 

  

TESTS:: 

  

sage: R = Zp(5) 

sage: R(6) + R(4) #indirect doctest 

2*5 + O(5^20) 

""" 

cdef long diff 

cdef bint is_zero 

if not exactzero(self.ordp): 

is_zero = creduce(self.unit, self.unit, self.relprec, self.prime_pow) 

if is_zero: 

self._set_inexact_zero(self.ordp + self.relprec) 

else: 

diff = cremove(self.unit, self.unit, self.relprec, self.prime_pow) 

# diff is less than self.relprec since the reduction didn't yield zero 

self.ordp += diff 

check_ordp(self.ordp) 

self.relprec -= diff 

  

def __dealloc__(self): 

""" 

Deallocate the underlying data structure. 

  

TESTS:: 

  

sage: R = Zp(5) 

sage: a = R(17) 

sage: del(a) 

""" 

cdestruct(self.unit, self.prime_pow) 

  

def __reduce__(self): 

""" 

Return a tuple of a function and data that can be used to unpickle this 

element. 

  

TESTS:: 

  

sage: a = ZpCR(5)(-3) 

sage: type(a) 

<type 'sage.rings.padics.padic_capped_relative_element.pAdicCappedRelativeElement'> 

sage: loads(dumps(a)) == a # indirect doctest 

True 

""" 

return unpickle_cre_v2, (self.__class__, self.parent(), cpickle(self.unit, self.prime_pow), self.ordp, self.relprec) 

  

cpdef _neg_(self): 

""" 

Return the additive inverse of this element. 

  

EXAMPLES:: 

  

sage: R = Zp(5, 20, 'capped-rel', 'val-unit') 

sage: R(5) + (-R(5)) # indirect doctest 

O(5^21) 

sage: -R(1) 

95367431640624 + O(5^20) 

sage: -R(5) 

5 * 95367431640624 + O(5^21) 

sage: -R(0) 

0 

""" 

cdef CRElement ans = self._new_c() 

ans.relprec = self.relprec 

ans.ordp = self.ordp 

if ans.relprec != 0: 

cneg(ans.unit, self.unit, ans.relprec, ans.prime_pow) 

creduce(ans.unit, ans.unit, ans.relprec, ans.prime_pow) 

return ans 

  

cpdef _add_(self, _right): 

""" 

Return the sum of this element and ``_right``. 

  

EXAMPLES:: 

  

sage: R = Zp(19, 5, 'capped-rel','series') 

sage: a = R(-1); a 

18 + 18*19 + 18*19^2 + 18*19^3 + 18*19^4 + O(19^5) 

sage: b=R(-5/2); b 

7 + 9*19 + 9*19^2 + 9*19^3 + 9*19^4 + O(19^5) 

sage: a+b #indirect doctest 

6 + 9*19 + 9*19^2 + 9*19^3 + 9*19^4 + O(19^5) 

""" 

cdef CRElement ans 

cdef CRElement right = _right 

cdef long tmpL 

if self.ordp == right.ordp: 

ans = self._new_c() 

# The relative precision of the sum is the minimum of the relative precisions in this case, 

# possibly decreasing if we got cancellation 

ans.ordp = self.ordp 

ans.relprec = min(self.relprec, right.relprec) 

if ans.relprec != 0: 

cadd(ans.unit, self.unit, right.unit, ans.relprec, ans.prime_pow) 

ans._normalize() 

else: 

if self.ordp > right.ordp: 

# Addition is commutative, swap so self.ordp < right.ordp 

ans = right; right = self; self = ans 

tmpL = right.ordp - self.ordp 

if tmpL > self.relprec: 

return self 

ans = self._new_c() 

ans.ordp = self.ordp 

ans.relprec = min(self.relprec, tmpL + right.relprec) 

if ans.relprec != 0: 

cshift(ans.unit, right.unit, tmpL, ans.relprec, ans.prime_pow, False) 

cadd(ans.unit, ans.unit, self.unit, ans.relprec, ans.prime_pow) 

creduce(ans.unit, ans.unit, ans.relprec, ans.prime_pow) 

return ans 

  

cpdef _sub_(self, _right): 

""" 

Return the difference of this element and ``_right``. 

  

EXAMPLES:: 

  

sage: R = Zp(13, 4) 

sage: R(10) - R(10) #indirect doctest 

O(13^4) 

sage: R(10) - R(11) 

12 + 12*13 + 12*13^2 + 12*13^3 + O(13^4) 

""" 

cdef CRElement ans 

cdef CRElement right = _right 

cdef long tmpL 

if self.ordp == right.ordp: 

ans = self._new_c() 

# The relative precision of the difference is the minimum of the relative precisions in this case, 

# possibly decreasing if we got cancellation 

ans.ordp = self.ordp 

ans.relprec = min(self.relprec, right.relprec) 

if ans.relprec != 0: 

csub(ans.unit, self.unit, right.unit, ans.relprec, ans.prime_pow) 

ans._normalize() 

elif self.ordp < right.ordp: 

tmpL = right.ordp - self.ordp 

if tmpL > self.relprec: 

return self 

ans = self._new_c() 

ans.ordp = self.ordp 

ans.relprec = min(self.relprec, tmpL + right.relprec) 

if ans.relprec != 0: 

cshift(ans.unit, right.unit, tmpL, ans.relprec, ans.prime_pow, False) 

csub(ans.unit, self.unit, ans.unit, ans.relprec, ans.prime_pow) 

creduce(ans.unit, ans.unit, ans.relprec, ans.prime_pow) 

else: 

tmpL = self.ordp - right.ordp 

if tmpL > right.relprec: 

return right._neg_() 

ans = self._new_c() 

ans.ordp = right.ordp 

ans.relprec = min(right.relprec, tmpL + self.relprec) 

if ans.relprec != 0: 

cshift(ans.unit, self.unit, tmpL, ans.relprec, ans.prime_pow, False) 

csub(ans.unit, ans.unit, right.unit, ans.relprec, ans.prime_pow) 

creduce(ans.unit, ans.unit, ans.relprec, ans.prime_pow) 

return ans 

  

def __invert__(self): 

r""" 

Returns the multiplicative inverse of this element. 

  

.. NOTE:: 

  

The result of inversion always lives in the fraction 

field, even if the element to be inverted is a unit. 

  

EXAMPLES:: 

  

sage: R = Qp(7,4,'capped-rel','series'); a = R(3); a 

3 + O(7^4) 

sage: ~a # indirect doctest 

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

""" 

assert_nonzero(self) 

cdef CRElement ans = self._new_c() 

if ans.prime_pow.in_field == 0: 

ans._parent = self._parent.fraction_field() 

ans.prime_pow = ans._parent.prime_pow 

ans.ordp = -self.ordp 

ans.relprec = self.relprec 

cinvert(ans.unit, self.unit, ans.relprec, ans.prime_pow) 

return ans 

  

cpdef _mul_(self, _right): 

r""" 

Return the product of this element and ``_right``. 

  

EXAMPLES:: 

  

sage: R = Zp(5) 

sage: a = R(2385,11); a 

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

sage: b = R(2387625, 16); b 

5^3 + 4*5^5 + 2*5^6 + 5^8 + 5^9 + O(5^16) 

sage: a * b # indirect doctest 

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

""" 

cdef CRElement ans 

cdef CRElement right = _right 

if exactzero(self.ordp): 

return self 

if exactzero(right.ordp): 

return right 

ans = self._new_c() 

ans.relprec = min(self.relprec, right.relprec) 

if ans.relprec == 0: 

ans._set_inexact_zero(self.ordp + right.ordp) 

else: 

ans.ordp = self.ordp + right.ordp 

cmul(ans.unit, self.unit, right.unit, ans.relprec, ans.prime_pow) 

creduce(ans.unit, ans.unit, ans.relprec, ans.prime_pow) 

check_ordp(ans.ordp) 

return ans 

  

cpdef _div_(self, _right): 

""" 

Return the quotient of this element and ``right``. 

  

.. NOTE:: 

  

The result of division always lives in the fraction field, 

even if the element to be inverted is a unit. 

  

EXAMPLES:: 

  

sage: R = Zp(5,6) 

sage: R(17) / R(21) #indirect doctest 

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

sage: a = R(50) / R(5); a 

2*5 + O(5^7) 

sage: R(5) / R(50) 

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

sage: ~a 

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

sage: 1 / a 

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

""" 

cdef CRElement ans 

cdef CRElement right = _right 

assert_nonzero(right) 

ans = self._new_c() 

if ans.prime_pow.in_field == 0: 

ans._parent = self._parent.fraction_field() 

ans.prime_pow = ans._parent.prime_pow 

if exactzero(self.ordp): 

ans._set_exact_zero() 

return ans 

ans.relprec = min(self.relprec, right.relprec) 

if ans.relprec == 0: 

ans._set_inexact_zero(self.ordp - right.ordp) 

else: 

ans.ordp = self.ordp - right.ordp 

cdivunit(ans.unit, self.unit, right.unit, ans.relprec, ans.prime_pow) 

creduce(ans.unit, ans.unit, ans.relprec, ans.prime_pow) 

check_ordp(ans.ordp) 

return ans 

  

def __pow__(CRElement self, _right, dummy): 

r""" 

Exponentiation. 

  

When ``right`` is divisible by `p` then one can get more 

precision than expected. 

  

Lemma 2.1 [SP]_: 

  

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``. 

  

For `\alpha` in `\ZZ_p` we can simplify the result a bit. In 

this case, the `p`-th power of `1 + \alpha p^{\lambda}` 

satisfies 

  

.. MATH:: 

  

(1 + \alpha p^{\lambda})^p \equiv 1 + \alpha p^{\lambda + 1} mod p^{\lambda + 2} 

  

unless `\lambda = 1` and `p = 2`, in which case 

  

.. MATH:: 

  

(1 + 2 \alpha)^2 \equiv 1 + 4(\alpha^2 + \alpha) mod 8 

  

So for `p \ne 2`, if right is divisible by `p^k` then we add 

`k` to the relative precision of the answer. 

  

For `p = 2`, if we start with something of relative precision 

1 (ie `2^m + O(2^{m+1})`), `\alpha^2 + \alpha \equiv 0 \mod 

2`, so the precision of the result is `k + 2`: 

  

.. MATH:: 

  

(2^m + O(2^{m+1}))^{2^k} = 2^{m 2^k} + O(2^{m 2^k + k + 2}) 

  

For `p`-adic exponents, we define `\alpha^\beta` as 

`\exp(\beta \log(\alpha))`. The precision of the result is 

determined using the power series expansions for the 

exponential and logarithm maps, together with the notes above. 

  

.. NOTE:: 

  

For `p`-adic exponents we always need that `a` is a unit. 

For unramified extensions `a^b` will converge as long as 

`b` is integral (though it may converge for non-integral 

`b` as well depending on the value of `a`). However, in 

highly ramified extensions some bases may be sufficiently 

close to `1` that `exp(b log(a))` does not converge even 

though `b` is integral. 

  

.. WARNING:: 

  

If `\alpha` is a unit, but not congruent to `1` modulo 

`\pi_K`, the result will not be the limit over integers 

`b` converging to `\beta` since this limit does not exist. 

Rather, the logarithm kills torsion in `\ZZ_p^\times`, and 

`\alpha^\beta` will equal `(\alpha')^\beta`, where 

`\alpha'` is the quotient of `\alpha` by the Teichmuller 

representative congruent to `\alpha` modulo `\pi_K`. Thus 

the result will always be congruent to `1` modulo `\pi_K`. 

  

REFERENCES: 

  

.. [SP] *Constructing Class Fields over Local Fields*. Sebastian Pauli. 

  

INPUT: 

  

- ``_right`` -- currently integers and `p`-adic exponents are 

supported. 

  

- ``dummy`` -- not used (Python's ``__pow__`` signature 

includes it) 

  

EXAMPLES:: 

  

sage: R = Zp(19, 5, 'capped-rel','series') 

sage: a = R(-1); a 

18 + 18*19 + 18*19^2 + 18*19^3 + 18*19^4 + O(19^5) 

sage: a^2 # indirect doctest 

1 + O(19^5) 

sage: a^3 

18 + 18*19 + 18*19^2 + 18*19^3 + 18*19^4 + O(19^5) 

sage: R(5)^30 

11 + 14*19 + 19^2 + 7*19^3 + O(19^5) 

sage: K = Qp(19, 5, 'capped-rel','series') 

sage: a = K(-1); a 

18 + 18*19 + 18*19^2 + 18*19^3 + 18*19^4 + O(19^5) 

sage: a^2 

1 + O(19^5) 

sage: a^3 

18 + 18*19 + 18*19^2 + 18*19^3 + 18*19^4 + O(19^5) 

sage: K(5)^30 

11 + 14*19 + 19^2 + 7*19^3 + O(19^5) 

sage: K(5, 3)^19 #indirect doctest 

5 + 3*19 + 11*19^3 + O(19^4) 

  

`p`-adic exponents are also supported:: 

  

sage: a = K(8/5,4); a 

13 + 7*19 + 11*19^2 + 7*19^3 + O(19^4) 

sage: a^(K(19/7)) 

1 + 14*19^2 + 11*19^3 + 13*19^4 + O(19^5) 

sage: (a // K.teichmuller(13))^(K(19/7)) 

1 + 14*19^2 + 11*19^3 + 13*19^4 + O(19^5) 

sage: (a.log() * 19/7).exp() 

1 + 14*19^2 + 11*19^3 + 13*19^4 + O(19^5) 

""" 

cdef long base_level, exp_prec 

cdef mpz_t tmp 

cdef Integer right 

cdef CRElement base, pright, ans 

cdef bint exact_exp 

if (isinstance(_right, Integer) or isinstance(_right, (int, long)) or isinstance(_right, Rational)): 

if _right < 0: 

base = ~self 

return base.__pow__(-_right, dummy) 

exact_exp = True 

elif self.parent() is _right.parent(): 

## For extension elements, we need to switch to the 

## fraction field sometimes in highly ramified extensions. 

exact_exp = False 

pright = _right 

else: 

self, _right = canonical_coercion(self, _right) 

return self.__pow__(_right, dummy) 

if exact_exp and _right == 0: 

# return 1 to maximum precision 

ans = self._new_c() 

ans.ordp = 0 

ans.relprec = self.prime_pow.ram_prec_cap 

csetone(ans.unit, ans.prime_pow) 

return ans 

if exactzero(self.ordp): 

if exact_exp: 

# We may assume from above that right > 0 

return self 

else: 

# log(0) is not defined 

raise ValueError("0^x is not defined for p-adic x: log(0) does not converge") 

ans = self._new_c() 

if self.relprec == 0: 

# If a positive 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): 

right = <Integer>_right 

mpz_init(tmp) 

mpz_mul_si(tmp, (<Integer>_right).value, self.ordp) 

check_ordp_mpz(tmp) 

ans._set_inexact_zero(mpz_get_si(tmp)) 

mpz_clear(tmp) 

else: 

raise PrecisionError 

elif exact_exp: 

# exact_pow_helper is defined in padic_template_element.pxi 

right = exact_pow_helper(&ans.relprec, self.relprec, _right, self.prime_pow) 

if ans.relprec > self.prime_pow.ram_prec_cap: 

ans.relprec = self.prime_pow.ram_prec_cap 

mpz_init(tmp) 

mpz_mul_si(tmp, right.value, self.ordp) 

check_ordp_mpz(tmp) 

ans.ordp = mpz_get_si(tmp) 

mpz_clear(tmp) 

cpow(ans.unit, self.unit, right.value, ans.relprec, ans.prime_pow) 

else: 

# padic_pow_helper is defined in padic_template_element.pxi 

ans.relprec = padic_pow_helper(ans.unit, self.unit, self.ordp, self.relprec, 

pright.unit, pright.ordp, pright.relprec, self.prime_pow) 

ans.ordp = 0 

return ans 

  

cdef pAdicTemplateElement _lshift_c(self, long shift): 

""" 

Multiplies by `\pi^{\mbox{shift}}`. 

  

Negative shifts may truncate the result if the parent is not a 

field. 

  

TESTS:: 

  

sage: a = Zp(5)(17); a 

2 + 3*5 + O(5^20) 

sage: a << 2 #indirect doctest 

2*5^2 + 3*5^3 + O(5^22) 

sage: a << -2 

O(5^18) 

sage: a << 0 == a 

True 

sage: Zp(5)(0) << -4000 

0 

""" 

if exactzero(self.ordp): 

return self 

if self.prime_pow.in_field == 0 and shift < 0 and -shift > self.ordp: 

return self._rshift_c(-shift) 

cdef CRElement ans = self._new_c() 

ans.relprec = self.relprec 

ans.ordp = self.ordp + shift 

check_ordp(ans.ordp) 

ccopy(ans.unit, self.unit, ans.prime_pow) 

return ans 

  

cdef pAdicTemplateElement _rshift_c(self, long shift): 

""" 

Divides by ``\pi^{\mbox{shift}}``. 

  

Positive shifts may truncate the result if the parent is not a 

field. 

  

TESTS:: 

  

sage: R = Zp(5); K = Qp(5) 

sage: R(17) >> 1 

3 + O(5^19) 

sage: K(17) >> 1 

2*5^-1 + 3 + O(5^19) 

sage: R(17) >> 40 

O(5^0) 

sage: K(17) >> -5 

2*5^5 + 3*5^6 + O(5^25) 

""" 

if exactzero(self.ordp): 

return self 

cdef CRElement ans = self._new_c() 

cdef long diff 

if self.prime_pow.in_field == 1 or shift <= self.ordp: 

ans.relprec = self.relprec 

ans.ordp = self.ordp - shift 

check_ordp(ans.ordp) 

ccopy(ans.unit, self.unit, ans.prime_pow) 

else: 

diff = shift - self.ordp 

if diff >= self.relprec: 

ans._set_inexact_zero(0) 

else: 

ans.relprec = self.relprec - diff 

cshift(ans.unit, self.unit, -diff, ans.relprec, ans.prime_pow, False) 

ans.ordp = 0 

ans._normalize() 

return ans 

  

cpdef _floordiv_(self, _right): 

""" 

Floor division. 

  

TESTS:: 

  

sage: r = Zp(19) 

sage: a = r(1+19+17*19^3+5*19^4); b = r(19^3); a/b 

19^-3 + 19^-2 + 17 + 5*19 + O(19^17) 

sage: a//b # indirect doctest 

17 + 5*19 + O(19^17) 

  

sage: R = Zp(19, 5, 'capped-rel','series') 

sage: a = R(-1); a 

18 + 18*19 + 18*19^2 + 18*19^3 + 18*19^4 + O(19^5) 

sage: b=R(-2*19^3); b 

17*19^3 + 18*19^4 + 18*19^5 + 18*19^6 + 18*19^7 + O(19^8) 

sage: a//b # indirect doctest 

9 + 9*19 + O(19^2) 

  

sage: R = Zp(5,5) 

sage: R(28937) // R(75) # indirect doctest 

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

  

sage: R(0,12) // R(175,3) 

O(5^10) 

""" 

if exactzero(self.ordp): 

return self 

cdef CRElement right = _right 

assert_nonzero(right) 

cdef CRElement ans = self._new_c() 

cdef long diff = self.ordp - right.ordp 

if self.relprec == 0: 

ans.ordp = diff 

ans.relprec = 0 

csetzero(ans.unit, ans.prime_pow) 

elif diff >= 0 or self.prime_pow.in_field: 

ans.ordp = diff 

ans.relprec = min(self.relprec, right.relprec) 

cdivunit(ans.unit, self.unit, right.unit, ans.relprec, ans.prime_pow) 

creduce(ans.unit, ans.unit, ans.relprec, ans.prime_pow) 

else: 

ans.ordp = 0 

ans.relprec = min(self.relprec, right.relprec) + diff 

if ans.relprec < 0: 

ans.relprec = 0 

csetzero(ans.unit, ans.prime_pow) 

else: 

cdivunit(ans.unit, self.unit, right.unit, ans.relprec - diff, ans.prime_pow) 

cshift(ans.unit, ans.unit, diff, ans.relprec, ans.prime_pow, False) 

ans._normalize() 

return ans 

  

def add_bigoh(self, absprec): 

""" 

Returns a new element with absolute precision decreased to 

``absprec``. 

  

INPUT: 

  

- ``absprec`` -- an integer or infinity 

  

OUTPUT: 

  

an equal element with precision set to the minimum of ``self's`` 

precision and ``absprec`` 

  

EXAMPLES:: 

  

sage: R = Zp(7,4,'capped-rel','series'); a = R(8); a.add_bigoh(1) 

1 + O(7) 

sage: b = R(0); b.add_bigoh(3) 

O(7^3) 

sage: R = Qp(7,4); a = R(8); a.add_bigoh(1) 

1 + O(7) 

sage: b = R(0); b.add_bigoh(3) 

O(7^3) 

  

The precision never increases:: 

  

sage: R(4).add_bigoh(2).add_bigoh(4) 

4 + O(7^2) 

  

Another example that illustrates that the precision does 

not increase:: 

  

sage: k = Qp(3,5) 

sage: a = k(1234123412/3^70); a 

2*3^-70 + 3^-69 + 3^-68 + 3^-67 + O(3^-65) 

sage: a.add_bigoh(2) 

2*3^-70 + 3^-69 + 3^-68 + 3^-67 + O(3^-65) 

  

sage: k = Qp(5,10) 

sage: a = k(1/5^3 + 5^2); a 

5^-3 + 5^2 + O(5^7) 

sage: a.add_bigoh(2) 

5^-3 + O(5^2) 

sage: a.add_bigoh(-1) 

5^-3 + O(5^-1) 

""" 

cdef CRElement ans 

cdef long aprec, newprec 

if absprec is infinity: 

return self 

elif isinstance(absprec, int): 

aprec = absprec 

else: 

if not isinstance(absprec, Integer): 

absprec = Integer(absprec) 

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

if mpz_sgn((<Integer>absprec).value) == -1: 

raise ValueError("absprec must fit into a signed long") 

else: 

aprec = self.prime_pow.ram_prec_cap 

else: 

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

if aprec < 0 and not self.parent().is_field(): 

return self.parent().fraction_field()(self).add_bigoh(absprec) 

if aprec < self.ordp: 

ans = self._new_c() 

ans._set_inexact_zero(aprec) 

elif aprec >= self.ordp + self.relprec: 

ans = self 

else: 

ans = self._new_c() 

ans.ordp = self.ordp 

ans.relprec = aprec - self.ordp 

creduce(ans.unit, self.unit, ans.relprec, ans.prime_pow) 

return ans 

  

cpdef bint _is_exact_zero(self) except -1: 

""" 

Returns true if this element is exactly zero. 

  

EXAMPLES:: 

  

sage: R = Zp(5) 

sage: R(0)._is_exact_zero() 

True 

sage: R(0,5)._is_exact_zero() 

False 

sage: R(17)._is_exact_zero() 

False 

""" 

return exactzero(self.ordp) 

  

cpdef bint _is_inexact_zero(self) except -1: 

""" 

Returns True if this element is indistinguishable from zero 

but has finite precision. 

  

EXAMPLES:: 

  

sage: R = Zp(5) 

sage: R(0)._is_inexact_zero() 

False 

sage: R(0,5)._is_inexact_zero() 

True 

sage: R(17)._is_inexact_zero() 

False 

""" 

return self.relprec == 0 and not exactzero(self.ordp) 

  

def is_zero(self, absprec = None): 

r""" 

Determines whether this element is zero modulo 

`\pi^{\mbox{absprec}}`. 

  

If ``absprec is None``, returns ``True`` if this element is 

indistinguishable from zero. 

  

INPUT: 

  

- ``absprec`` -- an integer, infinity, or ``None`` 

  

EXAMPLES:: 

  

sage: R = Zp(5); a = R(0); b = R(0,5); c = R(75) 

sage: a.is_zero(), a.is_zero(6) 

(True, True) 

sage: b.is_zero(), b.is_zero(5) 

(True, True) 

sage: c.is_zero(), c.is_zero(2), c.is_zero(3) 

(False, True, False) 

sage: b.is_zero(6) 

Traceback (most recent call last): 

... 

PrecisionError: Not enough precision to determine if element is zero 

""" 

if absprec is None: 

return self.relprec == 0 

if exactzero(self.ordp): 

return True 

if absprec is infinity: 

return False 

if isinstance(absprec, int): 

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

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

return self.ordp >= absprec 

if not isinstance(absprec, Integer): 

absprec = Integer(absprec) 

if self.relprec == 0: 

if mpz_cmp_si((<Integer>absprec).value, self.ordp) > 0: 

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

else: 

return True 

return mpz_cmp_si((<Integer>absprec).value, self.ordp) <= 0 

  

def __nonzero__(self): 

""" 

Returns True if self is distinguishable from zero. 

  

For most applications, explicitly specifying the power of p 

modulo which the element is supposed to be nonzero is 

preferable. 

  

EXAMPLES:: 

  

sage: R = Zp(5); a = R(0); b = R(0,5); c = R(75) 

sage: bool(a), bool(b), bool(c) 

(False, False, True) 

""" 

return self.relprec != 0 

  

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

r""" 

Returns whether self is equal to right modulo 

`\pi^{\mbox{absprec}}`. 

  

If ``absprec is None``, returns True if self and right are 

equal to the minimum of their precisions. 

  

INPUT: 

  

- ``right`` -- a `p`-adic element 

- ``absprec`` -- an integer, infinity, or ``None`` 

  

EXAMPLES:: 

  

sage: R = Zp(5, 10); a = R(0); b = R(0, 3); c = R(75, 5) 

sage: aa = a + 625; bb = b + 625; cc = c + 625 

sage: a.is_equal_to(aa), a.is_equal_to(aa, 4), a.is_equal_to(aa, 5) 

(False, True, False) 

sage: a.is_equal_to(aa, 15) 

Traceback (most recent call last): 

... 

PrecisionError: Elements not known to enough precision 

  

sage: a.is_equal_to(a, 50000) 

True 

  

sage: a.is_equal_to(b), a.is_equal_to(b, 2) 

(True, True) 

sage: a.is_equal_to(b, 5) 

Traceback (most recent call last): 

... 

PrecisionError: Elements not known to enough precision 

  

sage: b.is_equal_to(b, 5) 

Traceback (most recent call last): 

... 

PrecisionError: Elements not known to enough precision 

  

sage: b.is_equal_to(bb, 3) 

True 

sage: b.is_equal_to(bb, 4) 

Traceback (most recent call last): 

... 

PrecisionError: Elements not known to enough precision 

  

sage: c.is_equal_to(b, 2), c.is_equal_to(b, 3) 

(True, False) 

sage: c.is_equal_to(b, 4) 

Traceback (most recent call last): 

... 

PrecisionError: Elements not known to enough precision 

  

sage: c.is_equal_to(cc, 2), c.is_equal_to(cc, 4), c.is_equal_to(cc, 5) 

(True, True, False) 

  

TESTS:: 

  

sage: aa.is_equal_to(a), aa.is_equal_to(a, 4), aa.is_equal_to(a, 5) 

(False, True, False) 

sage: aa.is_equal_to(a, 15) 

Traceback (most recent call last): 

... 

PrecisionError: Elements not known to enough precision 

  

sage: b.is_equal_to(a), b.is_equal_to(a, 2) 

(True, True) 

sage: b.is_equal_to(a, 5) 

Traceback (most recent call last): 

... 

PrecisionError: Elements not known to enough precision 

  

sage: bb.is_equal_to(b, 3) 

True 

sage: bb.is_equal_to(b, 4) 

Traceback (most recent call last): 

... 

PrecisionError: Elements not known to enough precision 

  

sage: b.is_equal_to(c, 2), b.is_equal_to(c, 3) 

(True, False) 

sage: b.is_equal_to(c, 4) 

Traceback (most recent call last): 

... 

PrecisionError: Elements not known to enough precision 

  

sage: cc.is_equal_to(c, 2), cc.is_equal_to(c, 4), cc.is_equal_to(c, 5) 

(True, True, False) 

""" 

cdef CRElement right 

cdef long aprec, rprec 

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

right = _right 

else: 

right = self.parent().coerce(_right) 

if exactzero(self.ordp) and exactzero(right.ordp): 

return True 

elif absprec is infinity: 

raise PrecisionError("Elements not known to enough precision") 

if absprec is None: 

aprec = min(self.ordp + self.relprec, right.ordp + right.relprec) 

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 or \ 

exactzero(self.ordp) and exactzero(right.ordp): 

return True 

else: 

raise PrecisionError("Elements not known to enough precision") 

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

if aprec > self.ordp + self.relprec or aprec > right.ordp + right.relprec: 

raise PrecisionError("Elements not known to enough precision") 

if self.ordp >= aprec and right.ordp >= aprec: 

return True 

elif self.ordp != right.ordp: 

return False 

rprec = aprec - self.ordp 

return ccmp(self.unit, right.unit, rprec, rprec < self.relprec, rprec < right.relprec, self.prime_pow) == 0 

  

cdef int _cmp_units(self, pAdicGenericElement _right) except -2: 

""" 

Comparison of units, used in equality testing. 

  

EXAMPLES:: 

  

sage: R = Zp(5) 

sage: a = R(17); b = R(0,3); c = R(85,7); d = R(2, 1) 

sage: any([a == b, a == c, b == c, b == d, c == d]) 

False 

sage: all([a == a, b == b, c == c, d == d, a == d]) 

True 

  

sage: sorted([a, b, c, d]) 

[2 + 3*5 + O(5^20), 2 + O(5), 2*5 + 3*5^2 + O(5^7), O(5^3)] 

""" 

cdef CRElement right = _right 

cdef long rprec = min(self.relprec, right.relprec) 

if rprec == 0: 

return 0 

return ccmp(self.unit, right.unit, rprec, rprec < self.relprec, rprec < right.relprec, self.prime_pow) 

  

cdef pAdicTemplateElement lift_to_precision_c(self, long absprec): 

""" 

Lifts this element to another with precision at least ``absprec``. 

  

TESTS:: 

  

sage: R = Zp(5); a = R(0); b = R(0,5); c = R(17,3) 

sage: a.lift_to_precision(5) 

0 

sage: b.lift_to_precision(4) 

O(5^5) 

sage: b.lift_to_precision(8) 

O(5^8) 

sage: b.lift_to_precision(40) 

O(5^40) 

sage: c.lift_to_precision(1) 

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

sage: c.lift_to_precision(8) 

2 + 3*5 + O(5^8) 

sage: c.lift_to_precision(40) 

Traceback (most recent call last): 

... 

PrecisionError: Precision higher than allowed by the precision cap. 

""" 

cpdef CRElement ans 

if absprec == maxordp: 

if self.relprec == 0: 

ans = self._new_c() 

ans._set_exact_zero() 

return ans 

else: 

absprec = self.ordp + self.prime_pow.prec_cap 

cdef long relprec = absprec - self.ordp 

if relprec <= self.relprec: 

return self 

ans = self._new_c() 

if self.relprec == 0: 

ans._set_inexact_zero(absprec) 

else: 

ans.ordp = self.ordp 

ans.relprec = relprec 

ccopy(ans.unit, self.unit, ans.prime_pow) 

return ans 

  

def _cache_key(self): 

r""" 

Return a hashable key which identifies this element for caching. 

  

TESTS:: 

  

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

sage: (9*a)._cache_key() 

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

  

.. SEEALSO:: 

  

:meth:`sage.misc.cachefunc._cache_key` 

""" 

tuple_recursive = lambda l: tuple(tuple_recursive(x) for x in l) if isinstance(l, list) else l 

return (self.parent(), tuple_recursive(trim_zeros(list(self.expansion()))), self.valuation(), self.precision_relative()) 

  

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(17,5); a = R(11) 

sage: a 

11 + O(17^5) 

sage: a._teichmuller_set_unsafe(); a 

11 + 14*17 + 2*17^2 + 12*17^3 + 15*17^4 + O(17^5) 

sage: E = a.expansion(lift_mode='teichmuller'); E 

17-adic expansion of 11 + 14*17 + 2*17^2 + 12*17^3 + 15*17^4 + O(17^5) (teichmuller) 

sage: list(E) 

[11 + 14*17 + 2*17^2 + 12*17^3 + 15*17^4 + O(17^5), 0, 0, 0, 0] 

  

Note that if you set an element which is congruent to 0 you 

get an exact 0. 

  

sage: b = R(17*5); b 

5*17 + O(17^6) 

sage: b._teichmuller_set_unsafe(); b 

0 

""" 

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") 

else: 

cteichmuller(self.unit, self.unit, self.relprec, self.prime_pow) 

  

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: K.<a> = Qq(5^3) 

sage: a.polynomial() 

(1 + O(5^20))*x + (O(5^20)) 

sage: a.polynomial(var='y') 

(1 + O(5^20))*y + (O(5^20)) 

sage: (5*a^2 + K(25, 4)).polynomial() 

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

""" 

R = self.base_ring() 

S = R[var] 

if exactzero(self.ordp): 

return S([]) 

else: 

prec = self.precision_absolute() 

e = self.parent().e() 

L = ccoefficients(self.unit, self.ordp, self.relprec, self.prime_pow) 

if e == 1: 

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

else: 

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

return S(L) 

  

def precision_absolute(self): 

""" 

Returns the absolute precision of this element. 

  

This is the power of the maximal ideal modulo which this 

element is defined. 

  

EXAMPLES:: 

  

sage: R = Zp(7,3,'capped-rel'); a = R(7); a.precision_absolute() 

4 

sage: R = Qp(7,3); a = R(7); a.precision_absolute() 

4 

sage: R(7^-3).precision_absolute() 

0 

  

sage: R(0).precision_absolute() 

+Infinity 

sage: R(0,7).precision_absolute() 

7 

""" 

if exactzero(self.ordp): 

return infinity 

cdef Integer ans = Integer.__new__(Integer) 

mpz_set_si(ans.value, self.ordp + self.relprec) 

return ans 

  

def precision_relative(self): 

""" 

Returns the relative precision of this element. 

  

This is the power of the maximal ideal modulo which the unit 

part of self is defined. 

  

EXAMPLES:: 

  

sage: R = Zp(7,3,'capped-rel'); a = R(7); a.precision_relative() 

3 

sage: R = Qp(7,3); a = R(7); a.precision_relative() 

3 

sage: a = R(7^-2, -1); a.precision_relative() 

1 

sage: a 

7^-2 + O(7^-1) 

  

sage: R(0).precision_relative() 

0 

sage: R(0,7).precision_relative() 

0 

""" 

cdef Integer ans = Integer.__new__(Integer) 

mpz_set_si(ans.value, self.relprec) 

return ans 

  

cpdef pAdicTemplateElement unit_part(self): 

r""" 

Returns `u`, where this element is `\pi^v u`. 

  

EXAMPLES:: 

  

sage: R = Zp(17,4,'capped-rel') 

sage: a = R(18*17) 

sage: a.unit_part() 

1 + 17 + O(17^4) 

sage: type(a) 

<type 'sage.rings.padics.padic_capped_relative_element.pAdicCappedRelativeElement'> 

sage: R = Qp(17,4,'capped-rel') 

sage: a = R(18*17) 

sage: a.unit_part() 

1 + 17 + O(17^4) 

sage: type(a) 

<type 'sage.rings.padics.padic_capped_relative_element.pAdicCappedRelativeElement'> 

sage: a = R(2*17^2); a 

2*17^2 + O(17^6) 

sage: a.unit_part() 

2 + O(17^4) 

sage: b=1/a; b 

9*17^-2 + 8*17^-1 + 8 + 8*17 + O(17^2) 

sage: b.unit_part() 

9 + 8*17 + 8*17^2 + 8*17^3 + O(17^4) 

sage: Zp(5)(75).unit_part() 

3 + O(5^20) 

  

sage: R(0).unit_part() 

Traceback (most recent call last): 

... 

ValueError: unit part of 0 not defined 

sage: R(0,7).unit_part() 

O(17^0) 

""" 

if exactzero(self.ordp): 

raise ValueError("unit part of 0 not defined") 

cdef CRElement ans = (<CRElement>self)._new_c() 

ans.ordp = 0 

ans.relprec = (<CRElement>self).relprec 

ccopy(ans.unit, (<CRElement>self).unit, ans.prime_pow) 

return ans 

  

cdef long valuation_c(self): 

""" 

Returns the valuation of this element. 

  

If self is an exact zero, returns ``maxordp``, which is defined as 

``(1L << (sizeof(long) * 8 - 2))-1``. 

  

EXAMPLES:: 

  

sage: R = Qp(5); a = R(1) 

sage: a.valuation() #indirect doctest 

0 

sage: b = (a << 4); b.valuation() 

4 

sage: b = (a << 1073741822); b.valuation() 

1073741822 

""" 

return self.ordp 

  

cpdef val_unit(self, p=None): 

""" 

Returns a pair ``(self.valuation(), self.unit_part())``. 

  

INPUT: 

  

- ``p`` -- a prime (default: ``None``). If specified, will make sure that p==self.parent().prime() 

  

.. NOTE:: 

  

The optional argument ``p`` is used for consistency with the 

valuation methods on integer and rational. 

  

EXAMPLES:: 

  

sage: R = Zp(5); a = R(75, 20); a 

3*5^2 + O(5^20) 

sage: a.val_unit() 

(2, 3 + O(5^18)) 

sage: R(0).val_unit() 

Traceback (most recent call last): 

... 

ValueError: unit part of 0 not defined 

sage: R(0, 10).val_unit() 

(10, O(5^0)) 

""" 

# Since we keep this element normalized there's not much to do here. 

if p is not None and p != self.parent().prime(): 

raise ValueError('Ring (%s) residue field of the wrong characteristic.'%self.parent()) 

if exactzero((<CRElement>self).ordp): 

raise ValueError("unit part of 0 not defined") 

cdef Integer val = Integer.__new__(Integer) 

mpz_set_si(val.value, (<CRElement>self).ordp) 

cdef CRElement unit = (<CRElement>self)._new_c() 

unit.ordp = 0 

unit.relprec = (<CRElement>self).relprec 

ccopy(unit.unit, (<CRElement>self).unit, unit.prime_pow) 

return val, unit 

  

def __hash__(self): 

""" 

Hashing. 

  

.. WARNING:: 

  

Hashing of `p`-adic elements will likely be deprecated soon. See :trac:`11895`. 

  

EXAMPLES:: 

  

sage: R = Zp(5) 

sage: hash(R(17)) #indirect doctest 

17 

  

sage: hash(R(-1)) 

1977822444 # 32-bit 

95367431640624 # 64-bit 

""" 

if exactzero(self.ordp): 

return 0 

return chash(self.unit, self.ordp, self.relprec, self.prime_pow) ^ self.ordp 

  

cdef class pAdicCoercion_ZZ_CR(RingHomomorphism): 

""" 

The canonical inclusion from the integer ring to a capped relative ring. 

  

EXAMPLES:: 

  

sage: f = Zp(5).coerce_map_from(ZZ); f 

Ring morphism: 

From: Integer Ring 

To: 5-adic Ring with capped relative precision 20 

  

TESTS:: 

  

sage: TestSuite(f).run() 

  

""" 

def __init__(self, R): 

""" 

Initialization. 

  

EXAMPLES:: 

  

sage: f = Zp(5).coerce_map_from(ZZ); type(f) 

<type 'sage.rings.padics.padic_capped_relative_element.pAdicCoercion_ZZ_CR'> 

""" 

RingHomomorphism.__init__(self, ZZ.Hom(R)) 

self._zero = R.element_class(R, 0) 

self._section = pAdicConvert_CR_ZZ(R) 

  

cdef dict _extra_slots(self): 

""" 

Helper for copying and pickling. 

  

EXAMPLES:: 

  

sage: f = Zp(5).coerce_map_from(ZZ) 

sage: g = copy(f) # indirect doctest 

sage: g 

Ring morphism: 

From: Integer Ring 

To: 5-adic Ring with capped relative precision 20 

sage: g == f 

True 

sage: g is f 

False 

sage: g(5) 

5 + O(5^21) 

sage: g(5) == f(5) 

True 

""" 

_slots = RingHomomorphism._extra_slots(self) 

_slots['_zero'] = self._zero 

_slots['_section'] = self.section() # use method since it copies coercion-internal sections. 

return _slots 

  

cdef _update_slots(self, dict _slots): 

""" 

Helper for copying and pickling. 

  

EXAMPLES:: 

  

sage: f = Zp(5).coerce_map_from(ZZ) 

sage: g = copy(f) # indirect doctest 

sage: g 

Ring morphism: 

From: Integer Ring 

To: 5-adic Ring with capped relative precision 20 

sage: g == f 

True 

sage: g is f 

False 

sage: g(5) 

5 + O(5^21) 

sage: g(5) == f(5) 

True 

  

""" 

self._zero = _slots['_zero'] 

self._section = _slots['_section'] 

RingHomomorphism._update_slots(self, _slots) 

  

cpdef Element _call_(self, x): 

""" 

Evaluation. 

  

EXAMPLES:: 

  

sage: f = Zp(5).coerce_map_from(ZZ) 

sage: f(0).parent() 

5-adic Ring with capped relative precision 20 

sage: f(5) 

5 + O(5^21) 

""" 

if mpz_sgn((<Integer>x).value) == 0: 

return self._zero 

cdef CRElement ans = self._zero._new_c() 

ans.relprec = ans.prime_pow.prec_cap 

ans.ordp = cconv_mpz_t(ans.unit, (<Integer>x).value, ans.relprec, False, ans.prime_pow) 

return ans 

  

cpdef Element _call_with_args(self, x, args=(), kwds={}): 

""" 

This function is used when some precision cap is passed in 

(relative or absolute or both), or an empty element is 

desired. 

  

See the documentation for 

:meth:`pAdicCappedRelativeElement.__init__` for more details. 

  

EXAMPLES:: 

  

sage: R = Zp(5,4) 

sage: type(R(10,2)) 

<type 'sage.rings.padics.padic_capped_relative_element.pAdicCappedRelativeElement'> 

sage: R(10,2) 

2*5 + O(5^2) 

sage: R(10,3,1) 

2*5 + O(5^2) 

sage: R(10,absprec=2) 

2*5 + O(5^2) 

sage: R(10,relprec=2) 

2*5 + O(5^3) 

sage: R(10,absprec=1) 

O(5) 

sage: R(10,empty=True) 

O(5^0) 

""" 

cdef long val, aprec, rprec 

cdef CRElement ans 

_process_args_and_kwds(&aprec, &rprec, args, kwds, False, self._zero.prime_pow) 

if mpz_sgn((<Integer>x).value) == 0: 

if exactzero(aprec): 

return self._zero 

ans = self._zero._new_c() 

ans._set_inexact_zero(aprec) 

else: 

val = get_ordp(x, self._zero.prime_pow) 

ans = self._zero._new_c() 

if aprec <= val: 

ans._set_inexact_zero(aprec) 

else: 

ans.relprec = min(rprec, aprec - val) 

ans.ordp = cconv_mpz_t(ans.unit, (<Integer>x).value, ans.relprec, False, self._zero.prime_pow) 

return ans 

  

def section(self): 

""" 

Returns a map back to the ring of integers that approximates an element 

by an integer. 

  

EXAMPLES:: 

  

sage: f = Zp(5).coerce_map_from(ZZ).section() 

sage: f(Zp(5)(-1)) - 5^20 

-1 

""" 

from sage.misc.constant_function import ConstantFunction 

if not isinstance(self._section.domain, ConstantFunction): 

import copy 

self._section = copy.copy(self._section) 

return self._section 

  

cdef class pAdicConvert_CR_ZZ(RingMap): 

""" 

The map from a capped relative ring back to the ring of integers that 

returns the smallest non-negative integer approximation to its input 

which is accurate up to the precision. 

  

Raises a ``ValueError``, if the input is not in the closure of the image of 

the integers. 

  

EXAMPLES:: 

  

sage: f = Zp(5).coerce_map_from(ZZ).section(); f 

Set-theoretic ring morphism: 

From: 5-adic Ring with capped relative precision 20 

To: Integer Ring 

""" 

def __init__(self, R): 

""" 

Initialization. 

  

EXAMPLES:: 

  

sage: f = Qp(5).coerce_map_from(ZZ).section(); type(f) 

<type 'sage.rings.padics.padic_capped_relative_element.pAdicConvert_CR_ZZ'> 

sage: f.category() 

Category of homsets of sets with partial maps 

sage: Zp(5).coerce_map_from(ZZ).section().category() 

Category of homsets of sets 

""" 

if R.is_field() or R.degree() > 1 or R.characteristic() != 0 or R.residue_characteristic() == 0: 

RingMap.__init__(self, Hom(R, ZZ, SetsWithPartialMaps())) 

else: 

RingMap.__init__(self, Hom(R, ZZ, Sets())) 

  

cpdef Element _call_(self, _x): 

""" 

Evaluation. 

  

EXAMPLES:: 

  

sage: f = Qp(5).coerce_map_from(ZZ).section() 

sage: f(Qp(5)(-1)) - 5^20 

-1 

sage: f(Qp(5)(0)) 

0 

sage: f(Qp(5)(1/5)) 

Traceback (most recent call last): 

... 

ValueError: negative valuation 

""" 

cdef Integer ans = Integer.__new__(Integer) 

cdef CRElement x = _x 

if x.relprec != 0: 

cconv_mpz_t_out(ans.value, x.unit, x.ordp, x.relprec, x.prime_pow) 

return ans 

  

cdef class pAdicCoercion_QQ_CR(RingHomomorphism): 

""" 

The canonical inclusion from the rationals to a capped relative field. 

  

EXAMPLES:: 

  

sage: f = Qp(5).coerce_map_from(QQ); f 

Ring morphism: 

From: Rational Field 

To: 5-adic Field with capped relative precision 20 

  

TESTS:: 

  

sage: TestSuite(f).run() 

  

""" 

def __init__(self, R): 

""" 

Initialization. 

  

EXAMPLES:: 

  

sage: f = Qp(5).coerce_map_from(QQ); type(f) 

<type 'sage.rings.padics.padic_capped_relative_element.pAdicCoercion_QQ_CR'> 

""" 

RingHomomorphism.__init__(self, QQ.Hom(R)) 

self._zero = R.element_class(R, 0) 

self._section = pAdicConvert_CR_QQ(R) 

  

cdef dict _extra_slots(self): 

""" 

Helper for copying and pickling. 

  

EXAMPLES:: 

  

sage: f = Qp(5).coerce_map_from(QQ) 

sage: g = copy(f) # indirect doctest 

sage: g 

Ring morphism: 

From: Rational Field 

To: 5-adic Field with capped relative precision 20 

sage: g == f 

True 

sage: g is f 

False 

sage: g(6) 

1 + 5 + O(5^20) 

sage: g(6) == f(6) 

True 

""" 

_slots = RingHomomorphism._extra_slots(self) 

_slots['_zero'] = self._zero 

_slots['_section'] = self.section() # use method since it copies coercion-internal sections. 

return _slots 

  

cdef _update_slots(self, dict _slots): 

""" 

Helper for copying and pickling. 

  

EXAMPLES:: 

  

sage: f = Qp(5).coerce_map_from(QQ) 

sage: g = copy(f) # indirect doctest 

sage: g 

Ring morphism: 

From: Rational Field 

To: 5-adic Field with capped relative precision 20 

sage: g == f 

True 

sage: g is f 

False 

sage: g(6) 

1 + 5 + O(5^20) 

sage: g(6) == f(6) 

True 

  

""" 

self._zero = _slots['_zero'] 

self._section = _slots['_section'] 

RingHomomorphism._update_slots(self, _slots) 

  

cpdef Element _call_(self, x): 

""" 

Evaluation. 

  

EXAMPLES:: 

  

sage: f = Qp(5).coerce_map_from(QQ) 

sage: f(0).parent() 

5-adic Field with capped relative precision 20 

sage: f(1/5) 

5^-1 + O(5^19) 

sage: f(1/4) 

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

""" 

if mpq_sgn((<Rational>x).value) == 0: 

return self._zero 

cdef CRElement ans = self._zero._new_c() 

ans.relprec = ans.prime_pow.prec_cap 

ans.ordp = cconv_mpq_t(ans.unit, (<Rational>x).value, ans.relprec, False, self._zero.prime_pow) 

return ans 

  

cpdef Element _call_with_args(self, x, args=(), kwds={}): 

""" 

This function is used when some precision cap is passed in 

(relative or absolute or both), or an empty element is 

desired. 

  

See the documentation for 

:meth:`pAdicCappedRelativeElement.__init__` for more details. 

  

EXAMPLES:: 

  

sage: R = Qp(5,4) 

sage: type(R(10/3,2)) 

<type 'sage.rings.padics.padic_capped_relative_element.pAdicCappedRelativeElement'> 

sage: R(10/3,2) 

4*5 + O(5^2) 

sage: R(10/3,3,1) 

4*5 + O(5^2) 

sage: R(10/3,absprec=2) 

4*5 + O(5^2) 

sage: R(10/3,relprec=2) 

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

sage: R(10/3,absprec=1) 

O(5) 

sage: R(10/3,empty=True) 

O(5^0) 

sage: R(3/100,absprec=-1) 

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

""" 

cdef long val, aprec, rprec 

cdef CRElement ans 

_process_args_and_kwds(&aprec, &rprec, args, kwds, False, self._zero.prime_pow) 

if mpq_sgn((<Rational>x).value) == 0: 

if exactzero(aprec): 

return self._zero 

ans = self._zero._new_c() 

ans._set_inexact_zero(aprec) 

else: 

val = get_ordp(x, self._zero.prime_pow) 

ans = self._zero._new_c() 

if aprec <= val: 

ans._set_inexact_zero(aprec) 

else: 

ans.relprec = min(rprec, aprec - val) 

ans.ordp = cconv_mpq_t(ans.unit, (<Rational>x).value, ans.relprec, False, self._zero.prime_pow) 

return ans 

  

def section(self): 

""" 

Returns a map back to the rationals that approximates an element by 

a rational number. 

  

EXAMPLES:: 

  

sage: f = Qp(5).coerce_map_from(QQ).section() 

sage: f(Qp(5)(1/4)) 

1/4 

sage: f(Qp(5)(1/5)) 

1/5 

""" 

from sage.misc.constant_function import ConstantFunction 

if not isinstance(self._section.domain, ConstantFunction): 

import copy 

self._section = copy.copy(self._section) 

return self._section 

  

cdef class pAdicConvert_CR_QQ(RingMap): 

""" 

The map from the capped relative ring back to the rationals that returns a 

rational approximation of its input. 

  

EXAMPLES:: 

  

sage: f = Qp(5).coerce_map_from(QQ).section(); f 

Set-theoretic ring morphism: 

From: 5-adic Field with capped relative precision 20 

To: Rational Field 

""" 

def __init__(self, R): 

""" 

Initialization. 

  

EXAMPLES:: 

  

sage: f = Qp(5).coerce_map_from(QQ).section(); type(f) 

<type 'sage.rings.padics.padic_capped_relative_element.pAdicConvert_CR_QQ'> 

sage: f.category() 

Category of homsets of sets 

""" 

if R.degree() > 1 or R.characteristic() != 0 or R.residue_characteristic() == 0: 

RingMap.__init__(self, Hom(R, QQ, SetsWithPartialMaps())) 

else: 

RingMap.__init__(self, Hom(R, QQ, Sets())) 

  

cpdef Element _call_(self, _x): 

""" 

Evaluation. 

  

EXAMPLES:: 

  

sage: f = Qp(5).coerce_map_from(QQ).section() 

sage: f(Qp(5)(-1)) 

-1 

sage: f(Qp(5)(0)) 

0 

sage: f(Qp(5)(1/5)) 

1/5 

""" 

cdef Rational ans = Rational.__new__(Rational) 

cdef CRElement x = _x 

if x.relprec == 0: 

mpq_set_ui(ans.value, 0, 1) 

else: 

cconv_mpq_t_out(ans.value, x.unit, x.ordp, x.relprec, x.prime_pow) 

return ans 

  

cdef class pAdicConvert_QQ_CR(Morphism): 

""" 

The inclusion map from the rationals to a capped relative ring that is 

defined on all elements with non-negative `p`-adic valuation. 

  

EXAMPLES:: 

  

sage: f = Zp(5).convert_map_from(QQ); f 

Generic morphism: 

From: Rational Field 

To: 5-adic Ring with capped relative precision 20 

""" 

def __init__(self, R): 

""" 

Initialization. 

  

EXAMPLES:: 

  

sage: f = Zp(5).convert_map_from(QQ); type(f) 

<type 'sage.rings.padics.padic_capped_relative_element.pAdicConvert_QQ_CR'> 

""" 

Morphism.__init__(self, Hom(QQ, R, SetsWithPartialMaps())) 

self._zero = R.element_class(R, 0) 

self._section = pAdicConvert_CR_QQ(R) 

  

cdef dict _extra_slots(self): 

""" 

Helper for copying and pickling. 

  

EXAMPLES:: 

  

sage: f = Zp(5).convert_map_from(QQ) 

sage: g = copy(f) # indirect doctest 

sage: g == f 

True 

sage: g(1/6) 

1 + 4*5 + 4*5^3 + 4*5^5 + 4*5^7 + 4*5^9 + 4*5^11 + 4*5^13 + 4*5^15 + 4*5^17 + 4*5^19 + O(5^20) 

sage: g(1/6) == f(1/6) 

True 

""" 

_slots = Morphism._extra_slots(self) 

_slots['_zero'] = self._zero 

_slots['_section'] = self.section() # use method since it copies coercion-internal sections. 

return _slots 

  

cdef _update_slots(self, dict _slots): 

""" 

Helper for copying and pickling. 

  

EXAMPLES:: 

  

sage: f = Zp(5).convert_map_from(QQ) 

sage: g = copy(f) # indirect doctest 

sage: g == f 

True 

sage: g(1/6) 

1 + 4*5 + 4*5^3 + 4*5^5 + 4*5^7 + 4*5^9 + 4*5^11 + 4*5^13 + 4*5^15 + 4*5^17 + 4*5^19 + O(5^20) 

sage: g(1/6) == f(1/6) 

True 

""" 

self._zero = _slots['_zero'] 

self._section = _slots['_section'] 

Morphism._update_slots(self, _slots) 

  

cpdef Element _call_(self, x): 

""" 

Evaluation. 

  

EXAMPLES:: 

  

sage: f = Zp(5,4).convert_map_from(QQ) 

sage: f(1/7) 

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

sage: f(0) 

0 

""" 

if mpq_sgn((<Rational>x).value) == 0: 

return self._zero 

cdef CRElement ans = self._zero._new_c() 

ans.relprec = ans.prime_pow.prec_cap 

ans.ordp = cconv_mpq_t(ans.unit, (<Rational>x).value, ans.relprec, False, self._zero.prime_pow) 

if ans.ordp < 0: 

raise ValueError("p divides the denominator") 

return ans 

  

cpdef Element _call_with_args(self, x, args=(), kwds={}): 

""" 

This function is used when some precision cap is passed in 

(relative or absolute or both), or an empty element is 

desired. 

  

See the documentation for 

:meth:`pAdicCappedRelativeElement.__init__` for more details. 

  

EXAMPLES:: 

  

sage: R = Zp(5,4) 

sage: type(R(10/3,2)) 

<type 'sage.rings.padics.padic_capped_relative_element.pAdicCappedRelativeElement'> 

sage: R(10/3,2) 

4*5 + O(5^2) 

sage: R(10/3,3,1) 

4*5 + O(5^2) 

sage: R(10/3,absprec=2) 

4*5 + O(5^2) 

sage: R(10/3,relprec=2) 

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

sage: R(10/3,absprec=1) 

O(5) 

sage: R(10/3,empty=True) 

O(5^0) 

sage: R(3/100,relprec=3) 

Traceback (most recent call last): 

... 

ValueError: p divides the denominator 

""" 

cdef long val, aprec, rprec 

cdef CRElement ans 

_process_args_and_kwds(&aprec, &rprec, args, kwds, False, self._zero.prime_pow) 

if mpq_sgn((<Rational>x).value) == 0: 

if exactzero(aprec): 

return self._zero 

ans = self._zero._new_c() 

ans._set_inexact_zero(aprec) 

else: 

val = get_ordp(x, self._zero.prime_pow) 

ans = self._zero._new_c() 

if aprec <= val: 

ans._set_inexact_zero(aprec) 

else: 

ans.relprec = min(rprec, aprec - val) 

ans.ordp = cconv_mpq_t(ans.unit, (<Rational>x).value, ans.relprec, False, self._zero.prime_pow) 

if ans.ordp < 0: 

raise ValueError("p divides the denominator") 

return ans 

  

def section(self): 

""" 

Returns the map back to the rationals that returns the smallest 

non-negative integer approximation to its input which is accurate up to 

the precision. 

  

EXAMPLES:: 

  

sage: f = Zp(5,4).convert_map_from(QQ).section() 

sage: f(Zp(5,4)(-1)) 

-1 

""" 

from sage.misc.constant_function import ConstantFunction 

if not isinstance(self._section.domain, ConstantFunction): 

import copy 

self._section = copy.copy(self._section) 

return self._section 

  

cdef class pAdicCoercion_CR_frac_field(RingHomomorphism): 

""" 

The canonical inclusion of Zq into its fraction field. 

  

EXAMPLES:: 

  

sage: R.<a> = ZqCR(27, implementation='FLINT') 

sage: K = R.fraction_field() 

sage: f = K.coerce_map_from(R); f 

Ring morphism: 

From: Unramified Extension in a defined by x^3 + 2*x + 1 with capped relative precision 20 over 3-adic Ring 

To: Unramified Extension in a defined by x^3 + 2*x + 1 with capped relative precision 20 over 3-adic Field 

  

TESTS:: 

  

sage: TestSuite(f).run() 

  

""" 

def __init__(self, R, K): 

""" 

Initialization. 

  

EXAMPLES:: 

  

sage: R.<a> = ZqCR(27, implementation='FLINT') 

sage: K = R.fraction_field() 

sage: f = K.coerce_map_from(R); type(f) 

<type 'sage.rings.padics.qadic_flint_CR.pAdicCoercion_CR_frac_field'> 

""" 

RingHomomorphism.__init__(self, R.Hom(K)) 

self._zero = K(0) 

self._section = pAdicConvert_CR_frac_field(K, R) 

  

cpdef Element _call_(self, _x): 

""" 

Evaluation. 

  

EXAMPLES:: 

  

sage: R.<a> = ZqCR(27, implementation='FLINT') 

sage: K = R.fraction_field() 

sage: f = K.coerce_map_from(R) 

sage: f(a) 

a + O(3^20) 

sage: f(R(0)) 

0 

""" 

cdef CRElement x = _x 

cdef CRElement ans = self._zero._new_c() 

ans.ordp = x.ordp 

ans.relprec = x.relprec 

cshift(ans.unit, x.unit, 0, ans.relprec, x.prime_pow, False) 

return ans 

  

cpdef Element _call_with_args(self, _x, args=(), kwds={}): 

""" 

This function is used when some precision cap is passed in 

(relative or absolute or both). 

  

See the documentation for 

:meth:`pAdicCappedAbsoluteElement.__init__` for more details. 

  

EXAMPLES:: 

  

sage: R.<a> = ZqCR(27, implementation='FLINT') 

sage: K = R.fraction_field() 

sage: f = K.coerce_map_from(R) 

sage: f(a, 3) 

a + O(3^3) 

sage: b = 9*a 

sage: f(b, 3) 

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

sage: f(b, 4, 1) 

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

sage: f(b, 4, 3) 

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

sage: f(b, absprec=4) 

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

sage: f(b, relprec=3) 

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

sage: f(b, absprec=1) 

O(3) 

sage: f(R(0)) 

0 

""" 

cdef long aprec, rprec 

cdef CRElement x = _x 

cdef CRElement ans = self._zero._new_c() 

cdef bint reduce = False 

_process_args_and_kwds(&aprec, &rprec, args, kwds, False, ans.prime_pow) 

if aprec <= x.ordp: 

csetzero(ans.unit, x.prime_pow) 

ans.relprec = 0 

ans.ordp = aprec 

else: 

if rprec < x.relprec: 

reduce = True 

else: 

rprec = x.relprec 

if aprec < rprec + x.ordp: 

rprec = aprec - x.ordp 

reduce = True 

ans.ordp = x.ordp 

ans.relprec = rprec 

cshift(ans.unit, x.unit, 0, rprec, x.prime_pow, reduce) 

return ans 

  

def section(self): 

""" 

Returns a map back to the ring that converts elements of 

non-negative valuation. 

  

EXAMPLES:: 

  

sage: R.<a> = ZqCR(27, implementation='FLINT') 

sage: K = R.fraction_field() 

sage: f = K.coerce_map_from(R) 

sage: f(K.gen()) 

a + O(3^20) 

""" 

from sage.misc.constant_function import ConstantFunction 

if not isinstance(self._section.domain, ConstantFunction): 

import copy 

self._section = copy.copy(self._section) 

return self._section 

  

cdef dict _extra_slots(self): 

""" 

Helper for copying and pickling. 

  

TESTS:: 

  

sage: R.<a> = ZqCR(27, implementation='FLINT') 

sage: K = R.fraction_field() 

sage: f = K.coerce_map_from(R) 

sage: g = copy(f) # indirect doctest 

sage: g 

Ring morphism: 

From: Unramified Extension in a defined by x^3 + 2*x + 1 with capped relative precision 20 over 3-adic Ring 

To: Unramified Extension in a defined by x^3 + 2*x + 1 with capped relative precision 20 over 3-adic Field 

sage: g == f 

True 

sage: g is f 

False 

sage: g(a) 

a + O(3^20) 

sage: g(a) == f(a) 

True 

  

""" 

_slots = RingHomomorphism._extra_slots(self) 

_slots['_zero'] = self._zero 

_slots['_section'] = self.section() # use method since it copies coercion-internal sections. 

return _slots 

  

cdef _update_slots(self, dict _slots): 

""" 

Helper for copying and pickling. 

  

TESTS:: 

  

sage: R.<a> = ZqCR(9, implementation='FLINT') 

sage: K = R.fraction_field() 

sage: f = K.coerce_map_from(R) 

sage: g = copy(f) # indirect doctest 

sage: g 

Ring morphism: 

From: Unramified Extension in a defined by x^2 + 2*x + 2 with capped relative precision 20 over 3-adic Ring 

To: Unramified Extension in a defined by x^2 + 2*x + 2 with capped relative precision 20 over 3-adic Field 

sage: g == f 

True 

sage: g is f 

False 

sage: g(a) 

a + O(3^20) 

sage: g(a) == f(a) 

True 

  

""" 

self._zero = _slots['_zero'] 

self._section = _slots['_section'] 

RingHomomorphism._update_slots(self, _slots) 

  

def is_injective(self): 

r""" 

Return whether this map is injective. 

  

EXAMPLES:: 

  

sage: R.<a> = ZqCR(9, implementation='FLINT') 

sage: K = R.fraction_field() 

sage: f = K.coerce_map_from(R) 

sage: f.is_injective() 

True 

  

""" 

return True 

  

def is_surjective(self): 

r""" 

Return whether this map is surjective. 

  

EXAMPLES:: 

  

sage: R.<a> = ZqCR(9, implementation='FLINT') 

sage: K = R.fraction_field() 

sage: f = K.coerce_map_from(R) 

sage: f.is_surjective() 

False 

  

""" 

return False 

  

  

cdef class pAdicConvert_CR_frac_field(Morphism): 

""" 

The section of the inclusion from `\ZZ_q`` to its fraction field. 

  

EXAMPLES:: 

  

sage: R.<a> = ZqCR(27, implementation='FLINT') 

sage: K = R.fraction_field() 

sage: f = R.convert_map_from(K); f 

Generic morphism: 

From: Unramified Extension in a defined by x^3 + 2*x + 1 with capped relative precision 20 over 3-adic Field 

To: Unramified Extension in a defined by x^3 + 2*x + 1 with capped relative precision 20 over 3-adic Ring 

""" 

def __init__(self, K, R): 

""" 

Initialization. 

  

EXAMPLES:: 

  

sage: R.<a> = ZqCR(27, implementation='FLINT') 

sage: K = R.fraction_field() 

sage: f = R.convert_map_from(K); type(f) 

<type 'sage.rings.padics.qadic_flint_CR.pAdicConvert_CR_frac_field'> 

""" 

Morphism.__init__(self, Hom(K, R, SetsWithPartialMaps())) 

self._zero = R(0) 

  

cpdef Element _call_(self, _x): 

""" 

Evaluation. 

  

EXAMPLES:: 

  

sage: R.<a> = ZqCR(27, implementation='FLINT') 

sage: K = R.fraction_field() 

sage: f = R.convert_map_from(K) 

sage: f(K.gen()) 

a + O(3^20) 

""" 

cdef CRElement x = _x 

if x.ordp < 0: raise ValueError("negative valuation") 

cdef CRElement ans = self._zero._new_c() 

ans.relprec = x.relprec 

ans.ordp = x.ordp 

cshift(ans.unit, x.unit, 0, ans.relprec, ans.prime_pow, False) 

return ans 

  

cpdef Element _call_with_args(self, _x, args=(), kwds={}): 

""" 

This function is used when some precision cap is passed in 

(relative or absolute or both). 

  

See the documentation for 

:meth:`pAdicCappedAbsoluteElement.__init__` for more details. 

  

EXAMPLES:: 

  

sage: R.<a> = ZqCR(27, implementation='FLINT') 

sage: K = R.fraction_field() 

sage: f = R.convert_map_from(K); a = K(a) 

sage: f(a, 3) 

a + O(3^3) 

sage: b = 9*a 

sage: f(b, 3) 

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

sage: f(b, 4, 1) 

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

sage: f(b, 4, 3) 

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

sage: f(b, absprec=4) 

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

sage: f(b, relprec=3) 

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

sage: f(b, absprec=1) 

O(3) 

sage: f(K(0)) 

0 

""" 

cdef long aprec, rprec 

cdef CRElement x = _x 

if x.ordp < 0: raise ValueError("negative valuation") 

cdef CRElement ans = self._zero._new_c() 

cdef bint reduce = False 

_process_args_and_kwds(&aprec, &rprec, args, kwds, False, ans.prime_pow) 

if aprec <= x.ordp: 

csetzero(ans.unit, x.prime_pow) 

ans.relprec = 0 

ans.ordp = aprec 

else: 

if rprec < x.relprec: 

reduce = True 

else: 

rprec = x.relprec 

if aprec < rprec + x.ordp: 

rprec = aprec - x.ordp 

reduce = True 

ans.ordp = x.ordp 

ans.relprec = rprec 

cshift(ans.unit, x.unit, 0, rprec, x.prime_pow, reduce) 

return ans 

  

cdef dict _extra_slots(self): 

""" 

Helper for copying and pickling. 

  

TESTS:: 

  

sage: R.<a> = ZqCR(27, implementation='FLINT') 

sage: K = R.fraction_field() 

sage: f = R.convert_map_from(K) 

sage: a = K(a) 

sage: g = copy(f) # indirect doctest 

sage: g 

Generic morphism: 

From: Unramified Extension in a defined by x^3 + 2*x + 1 with capped relative precision 20 over 3-adic Field 

To: Unramified Extension in a defined by x^3 + 2*x + 1 with capped relative precision 20 over 3-adic Ring 

sage: g == f 

True 

sage: g is f 

False 

sage: g(a) 

a + O(3^20) 

sage: g(a) == f(a) 

True 

""" 

_slots = Morphism._extra_slots(self) 

_slots['_zero'] = self._zero 

return _slots 

  

cdef _update_slots(self, dict _slots): 

""" 

Helper for copying and pickling. 

  

TESTS:: 

  

sage: R.<a> = ZqCR(9, implementation='FLINT') 

sage: K = R.fraction_field() 

sage: f = R.convert_map_from(K) 

sage: a = K(a) 

sage: g = copy(f) # indirect doctest 

sage: g 

Generic morphism: 

From: Unramified Extension in a defined by x^2 + 2*x + 2 with capped relative precision 20 over 3-adic Field 

To: Unramified Extension in a defined by x^2 + 2*x + 2 with capped relative precision 20 over 3-adic Ring 

sage: g == f 

True 

sage: g is f 

False 

sage: g(a) 

a + O(3^20) 

sage: g(a) == f(a) 

True 

  

""" 

self._zero = _slots['_zero'] 

Morphism._update_slots(self, _slots) 

  

def unpickle_cre_v2(cls, parent, unit, ordp, relprec): 

""" 

Unpickles a capped relative element. 

  

EXAMPLES:: 

  

sage: from sage.rings.padics.padic_capped_relative_element import unpickle_cre_v2 

sage: R = Zp(5); a = R(85,6) 

sage: b = unpickle_cre_v2(a.__class__, R, 17, 1, 5) 

sage: a == b 

True 

sage: a.precision_relative() == b.precision_relative() 

True 

""" 

cdef CRElement ans = cls.__new__(cls) 

ans._parent = parent 

ans.prime_pow = <PowComputer_?>parent.prime_pow 

cconstruct(ans.unit, ans.prime_pow) 

cunpickle(ans.unit, unit, ans.prime_pow) 

ans.ordp = ordp 

ans.relprec = relprec 

return ans