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

r""" 

Lattice precision for the parents ``ZpLC``/``QpLC`` and ``ZpLF``/``QpLF`` 

 

AUTHOR: 

 

- Xavier Caruso (2018-02): initial version 

 

TESTS:: 

 

sage: R = ZpLC(2) 

doctest:...: FutureWarning: This class/method/function is marked as experimental. It, its functionality or its interface might change without a formal deprecation. 

See http://trac.sagemath.org/23505 for details. 

sage: prec = R.precision() 

sage: prec 

Precision lattice on 0 objects 

 

sage: S = ZpLF(2) 

sage: prec = S.precision() 

sage: prec 

Precision module on 0 objects 

""" 

 

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

# Copyright (C) 2018 Xavier Caruso <xavier.caruso@normalesup.org> 

# 

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

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

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

# (at your option) any later version. 

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

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

 

 

from sage.misc.misc import walltime 

 

from sage.structure.sage_object import SageObject 

from sage.structure.unique_representation import UniqueRepresentation 

from sage.misc.abstract_method import abstract_method 

from sage.misc.cachefunc import cached_method 

 

from sage.rings.integer_ring import ZZ 

from sage.rings.rational_field import QQ 

from sage.rings.infinity import Infinity 

 

from sage.rings.padics.precision_error import PrecisionError 

 

 

# The default minimal size after which re-echelonization is not performed, 

# i.e., when a variable is not referenced anymore and could be deleted but its 

# corresponding column is further than this threshold from the right end of the 

# matrix representing the precision lattice, then the column is not removed 

# from the matrix because the re-echelonization would be too costly. 

DEFAULT_THRESHOLD_DELETION = 50 

 

# The number of additional digits used for internal computations 

STARTING_ADDITIONAL_PREC = 5 

 

class pRational: 

r""" 

This class implements rational numbers viewed as elements of ``Qp``. 

In particular, it provides additional methods which are specific to 

``p``-adics (as ``p``-adic valuation). 

 

Only for internal use. 

 

INPUT: 

 

- ``p`` -- a prime number 

 

- ``x`` -- a rational number 

 

- ``exponent`` -- an integer (default: 0) 

 

- ``valuation`` -- an integer or None (default: ``None``), 

the ``p``-adic valuation of this element 

 

If not ``None``, this method trusts the given value to the 

attribute ``valuation``. 

 

TESTS:: 

 

sage: from sage.rings.padics.lattice_precision import pRational 

sage: x = pRational(2, 5); x 

5 

sage: y = pRational(2, 5/3, 2); y 

2^2 * 5/3 

 

sage: x + y 

35/3 

sage: x - y 

-5/3 

sage: x * y 

2^2 * 25/3 

sage: x / y 

2^-2 * 3 

 

sage: x.valuation() 

0 

sage: y.valuation() 

2 

 

sage: z = pRational(2, 1024, valuation=4) 

sage: z 

1024 

sage: z.valuation() 

4 

""" 

def __init__(self, p, x, exponent=0, valuation=None): 

r""" 

Construct the element ``x * p^exponent`` 

 

TESTS:: 

 

sage: from sage.rings.padics.lattice_precision import pRational 

sage: pRational(2, 5) 

5 

sage: pRational(2, 5/3, 2) 

2^2 * 5/3 

""" 

self.p = p 

if x in ZZ: 

self.x = ZZ(x) 

else: 

self.x = x 

self.exponent = exponent 

self._valuation = valuation 

 

def __repr__(self): 

r""" 

Return a string representation of this element. 

 

TESTS:: 

 

sage: from sage.rings.padics.lattice_precision import pRational 

sage: pRational(2, 5, 2) # indirect doctest 

2^2 * 5 

""" 

if self.exponent == 0: 

return repr(self.x) 

else: 

return "%s^%s * %s" % (self.p, self.exponent, self.x) 

 

def reduce(self, prec): 

r""" 

Return this element reduced modulo ``p^prec``. 

 

INPUT: 

 

- ``prec`` -- an integer 

 

TESTS:: 

 

sage: from sage.rings.padics.lattice_precision import pRational 

sage: x = pRational(2, 1234567); x 

1234567 

sage: x.reduce(12) 

1671 

 

sage: x = pRational(2, 1234/567); x 

1234/567 

sage: x.reduce(12) 

190 

""" 

if prec is Infinity: 

return self 

x = self.x 

exp = self.exponent 

if x.parent() is ZZ: 

if prec > exp: 

x = x % (self.p ** (prec-exp)) 

else: 

x = 0 

elif x.parent() is QQ: 

num = x.numerator() 

denom = x.denominator() 

valdenom = denom.valuation(self.p) 

denom //= self.p ** valdenom 

exp -= valdenom 

if prec > exp: 

modulo = self.p ** (prec - exp) 

# probably we should use Newton iteration instead  

# (but it is actually slower for now - Python implementation) 

_, inv, _ = denom.xgcd(modulo) 

x = (num*inv) % modulo 

else: 

x = 0 

if self.x == 0: 

val = Infinity 

else: 

val = self._valuation 

return self.__class__(self.p, x, exp, valuation=val) 

 

def reduce_relative(self, prec): 

r""" 

Return this element reduced modulo ``p^n`` where ``n = prec + val(x)``. 

 

INPUT: 

 

- ``prec`` -- a nonnegative integer 

 

TESTS:: 

 

sage: from sage.rings.padics.lattice_precision import pRational 

sage: x = pRational(2, 1234567); x 

1234567 

sage: x.reduce_relative(12) 

1671 

 

sage: x = pRational(2, 1234/567); x 

1234/567 

sage: x.reduce_relative(12) 

190 

""" 

v = self.valuation() 

if v is Infinity: 

return self 

return self.reduce(prec+v) 

 

def normalize(self): 

r""" 

Normalize this element, i.e. write it as ``p^v * u`` where  

``u`` is coprime to `p`. 

 

TESTS:: 

 

sage: from sage.rings.padics.lattice_precision import pRational 

sage: x = pRational(2, 123456, 7); x 

2^7 * 123456 

sage: x.normalize(); x 

2^13 * 1929 

""" 

if self.x == 0: 

self.exponent = 0 

else: 

val = self.valuation() 

exp = self.exponent 

self.x /= self.p ** (val-exp) 

if self.x in ZZ: 

self.x = ZZ(self.x) 

self.exponent = val 

 

def valuation(self): 

r""" 

Return the `p`-adic valuation of this element. 

 

TESTS:: 

 

sage: from sage.rings.padics.lattice_precision import pRational 

sage: x = pRational(2, 123456, 7); x 

2^7 * 123456 

sage: x.valuation() 

13 

""" 

if self._valuation is None: 

valx = self.x.valuation(self.p) 

self._valuation = self.exponent + valx 

return self._valuation 

 

def is_p_power(self): 

r""" 

Return true if this element is a power of `p`. 

 

TESTS:: 

 

sage: from sage.rings.padics.lattice_precision import pRational 

sage: x = pRational(2, 1024, 2); x 

2^2 * 1024 

sage: x.is_p_power() 

True 

 

sage: y = pRational(2, 123456, 7); y 

2^7 * 123456 

sage: y.is_p_power() 

False 

""" 

self.normalize() 

return self.x == 1 

 

def is_zero(self): 

r""" 

Return true if this element vanishes. 

 

TESTS:: 

 

sage: from sage.rings.padics.lattice_precision import pRational 

sage: x = pRational(2, 123456, 7); x 

2^7 * 123456 

sage: x.is_zero() 

False 

 

sage: (x-x).is_zero() 

True 

""" 

return self.x == 0 

 

def __add__(self, other): 

r""" 

Return the sum of ``self`` and ``other``. 

 

TESTS:: 

 

sage: from sage.rings.padics.lattice_precision import pRational 

sage: x = pRational(2, 123456, 7); x 

2^7 * 123456 

sage: y = pRational(2, 891011, 12); y 

2^12 * 891011 

sage: x + y 

2^7 * 28635808 

""" 

p = self.p 

sexp = self.exponent 

oexp = other.exponent 

if sexp is Infinity: return other 

if oexp is Infinity: return self 

if self._valuation is None or other._valuation is None: 

val = None 

elif self._valuation < other._valuation: 

val = self._valuation 

elif self._valuation > other._valuation: 

val = other._valuation 

else: 

val = None 

if sexp < oexp: 

return self.__class__(p, self.x + other.x * p**(oexp-sexp), sexp, valuation=val) 

else: 

return self.__class__(p, self.x * p**(sexp-oexp) + other.x, oexp, valuation=val) 

 

def __sub__(self, other): 

r""" 

Return the subtraction of ``self`` by ``other``. 

 

TESTS:: 

 

sage: from sage.rings.padics.lattice_precision import pRational 

sage: x = pRational(2, 123456, 7); x 

2^7 * 123456 

sage: y = pRational(2, 891011, 12); y 

2^12 * 891011 

sage: x - y 

2^7 * -28388896 

""" 

return self + (-other) 

 

def __neg__(self): 

r""" 

Return the opposite of this element. 

 

TESTS:: 

 

sage: from sage.rings.padics.lattice_precision import pRational 

sage: x = pRational(2, 123456, 7); x 

2^7 * 123456 

sage: -x 

2^7 * -123456 

""" 

return self.__class__(self.p, -self.x, self.exponent, valuation=self._valuation) 

 

def __mul__(self, other): 

r""" 

Return the product of ``self`` and ``other``. 

 

TESTS:: 

 

sage: from sage.rings.padics.lattice_precision import pRational 

sage: x = pRational(2, 123456, 7); x 

2^7 * 123456 

sage: y = pRational(2, 891011, 12); y 

2^12 * 891011 

sage: x * y 

2^19 * 110000654016 

""" 

if self._valuation is None or other._valuation is None: 

val = None 

else: 

val = self._valuation + other._valuation 

return self.__class__(self.p, self.x * other.x, self.exponent + other.exponent, valuation=val) 

 

def __div__(self, other): 

r""" 

Return the quotient of ``self`` by ``other``. 

 

TESTS:: 

 

sage: from sage.rings.padics.lattice_precision import pRational 

sage: x = pRational(2, 123456, 7); x 

2^7 * 123456 

sage: y = pRational(2, 891011, 12); y 

2^12 * 891011 

sage: x / y 

2^-5 * 123456/891011 

""" 

if self._valuation is None or other._valuation is None: 

val = None 

else: 

val = self._valuation - other._valuation 

return self.__class__(self.p, self.x / other.x, self.exponent - other.exponent, valuation=val) 

 

def __lshift__(self, n): 

r""" 

Return the product of this element by ``p^n``. 

 

INPUT: 

 

- ``n`` -- a relative integer 

 

TESTS:: 

 

sage: from sage.rings.padics.lattice_precision import pRational 

sage: x = pRational(2, 123456, 7); x 

2^7 * 123456 

sage: x << 10 

2^17 * 123456 

""" 

if self._valuation is None: 

val = None 

else: 

val = self._valuation + n 

return self.__class__(self.p, self.x, self.exponent + n, valuation=val) 

 

def __rshift__(self, n): 

r""" 

Return the quotient of this element by ``p^n``. 

 

INPUT: 

 

- ``n`` -- a relative integer 

 

TESTS:: 

 

sage: from sage.rings.padics.lattice_precision import pRational 

sage: x = pRational(2, 123456, 7); x 

2^7 * 123456 

sage: x >> 10 

2^-3 * 123456 

""" 

return self << (-n) 

 

def unit_part(self): 

r""" 

Return the unit part of this element, that is the part ``u`` 

in the writing ``u * p^v`` with ``u`` coprime to `p`. 

 

TESTS:: 

 

sage: from sage.rings.padics.lattice_precision import pRational 

sage: x = pRational(2, 123456, 7); x 

2^7 * 123456 

sage: x.unit_part() 

1929 

""" 

if self.is_zero(): 

raise ValueError("the unit part of zero is not defined") 

p = self.p 

val = self.valuation() 

x = self.x / (p ** (val-self.exponent)) 

return self.__class__(p, x, 0, valuation=0) 

 

def xgcd(self, other): 

r""" 

Return the gcd of ``self`` and ``other`` together with two 

element ``u`` and ``v`` such that ``u*self + v*other = gcd``. 

 

The ``gcd`` is normalized so that it is a power of `p`. 

 

TESTS:: 

 

sage: from sage.rings.padics.lattice_precision import pRational 

sage: x = pRational(2, 123456, 7); x 

2^7 * 123456 

sage: y = pRational(2, 891011, 12); y 

2^12 * 891011 

 

sage: d, u, v = x.xgcd(y) 

sage: d 

2^7 * 32 

sage: d.normalize(); d 

2^12 * 1 

 

sage: u*x + v*y 

2^7 * 32 

""" 

p = self.p 

sexp = self.exponent 

oexp = other.exponent 

if sexp < oexp: 

a = ZZ(self.x) 

b = ZZ(other.x * (p ** (oexp-sexp))) 

exp = sexp 

else: 

a = ZZ(self.x * (p ** (sexp-oexp))) 

b = ZZ(other.x) 

exp = oexp 

d, u, v = a.xgcd(b) 

if self._valuation is None or other._valuation is None: 

val = None 

else: 

val = min(self._valuation, other._valuation) 

d = self.__class__(p, d, exp, valuation=val) 

u = self.__class__(p, u) 

v = self.__class__(p, v) 

return d, u, v 

 

def value(self): 

r""" 

Return this element as a rational number. 

 

TESTS:: 

 

sage: from sage.rings.padics.lattice_precision import pRational 

sage: x = pRational(2, 123456, 7); x 

2^7 * 123456 

sage: x.value() 

15802368 

""" 

return (self.p ** self.exponent) * self.x 

 

def list(self, prec): 

r""" 

Return the list of the digits of this element (written in radix  

`p`) up to position ``prec``. 

 

The first zeros are omitted. 

 

TESTS:: 

 

sage: from sage.rings.padics.lattice_precision import pRational 

sage: x = pRational(2, 123456); x 

123456 

sage: x.list(5) 

[] 

sage: x.list(20) 

[1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0] 

 

sage: y = pRational(2, 123/456); y 

41/152 

sage: y.list(10) 

[1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1] 

 

sage: z = pRational(2, 0) 

sage: z.list(10) 

[] 

sage: z.list(100) 

[] 

""" 

if self.x not in ZZ: 

self = self.reduce(prec) 

val = self.valuation() 

if val is Infinity: 

return [] 

p = self.p 

x = ZZ(self.x * p**(self.exponent - val)) 

l = [ ] 

for _ in range(val, prec): 

x, digit = x.quo_rem(p) 

l.append(digit) 

return l 

 

 

class DifferentialPrecisionGeneric(SageObject): 

r""" 

A generic class for precision objects obtained by automatic 

differentiation. 

 

INPUT: 

 

- ``p`` -- a prime number 

 

- ``label`` -- a string, the label of the parents to which the elements 

belong that are tracked by this precision module 

 

.. NOTE:: 

 

This object is used internally by the parent ring. You should not 

create instances of this class on your own. 

 

EXAMPLES:: 

 

sage: R = ZpLC(2, label='init') 

sage: R.precision() 

Precision lattice on 0 objects (label: init) 

""" 

def __init__(self, p, label): 

r""" 

TESTS:: 

 

sage: prec = ZpLC(2, label='init').precision() 

sage: from sage.rings.padics.lattice_precision import DifferentialPrecisionGeneric 

sage: isinstance(prec, DifferentialPrecisionGeneric) 

True 

 

""" 

self._p = p 

self._label = label 

self._elements = [ ] 

self._matrix = { } # A dictionary whose keys are weak references to tracked elements 

# and values corresponding columns in the matrix 

# representing the precision lattice 

self._collected_references = [ ] 

self._marked_for_deletion = [ ] 

self._approx_zero = pRational(p, ZZ(0)) 

self._threshold_deletion = DEFAULT_THRESHOLD_DELETION 

self._history_init = None 

self._history = None 

 

def __reduce__(self): 

r""" 

TESTS:: 

 

sage: R = ZpLF(2) 

sage: prec = R.precision() 

sage: dumps(prec) 

Traceback (most recent call last): 

... 

NotImplementedError: pickling/unpickling precision modules is not implemented yet 

""" 

raise NotImplementedError("pickling/unpickling precision modules is not implemented yet") 

 

def _repr_(self): 

r""" 

Return a string representation of this precision object. 

 

EXAMPLES:: 

 

sage: R = ZpLC(2) 

sage: R.precision() 

Precision lattice on ... objects 

 

If a label has been specified, it is included in the representation:: 

 

sage: R = ZpLC(2, label="mylabel") 

sage: R.precision() 

Precision lattice on 0 objects (label: mylabel) 

""" 

label = "" if self._label is None else " (label: %s)"%(self._label,) 

count = "1 object" if len(self._elements) == 1 else "%s objects"%len(self._elements) 

return "%s on %s%s"%(self._repr_type, count, label) 

 

def threshold_deletion(self, threshold=None): 

r""" 

Return (and set) the threshold for column deletion. 

 

When a variable dies, i.e., goes out of scope, the ambient space in 

which the precision module lives can be reduced (by projection onto the 

hyperplane defined by the dead variable). 

This reduction has a cost because it leads to re-echelonization 

of a part of the matrix that encodes the precision. The size of this 

part is roughly measured by the number of columns between the last 

column and the one corresponding to the dead variable. 

 

This threshold returned by this method is the maximal distance until 

which a column of a dead variable is removed and the matrix 

re-echelonized. Beyond the threshold, the column of the dead variable 

is kept in this matrix as if the variable were not destroyed. 

 

INPUT: 

 

- ``threshold`` -- a non-negative integer, ``Infinity`` or ``None`` 

(default: ``None``): if not ``None`` set the threshold to the given 

value. 

 

.. NOTE:: 

 

Setting the threshold to ``0`` disables the dimension reduction. 

 

Setting the threshold to ``Infinity`` forces the dimension reduction 

after each deletion. 

 

EXAMPLES:: 

 

sage: R = ZpLC(2, label='threshold_deletion') 

sage: prec = R.precision() 

sage: prec.threshold_deletion() 

50 

 

sage: prec.threshold_deletion(20) 

20 

sage: prec.threshold_deletion() 

20 

 

sage: prec.threshold_deletion(-2) 

Traceback (most recent call last): 

... 

ValueError: The threshold must be a nonnegative integer or Infinity 

""" 

if threshold is not None: 

if threshold is Infinity or (threshold in ZZ and threshold >= 0): 

self._threshold_deletion = threshold 

else: 

raise ValueError("The threshold must be a nonnegative integer or Infinity") 

return self._threshold_deletion 

 

def prime(self): 

r""" 

Return the underlying prime number attached to this precision lattice. 

 

EXAMPLES:: 

 

sage: R = ZpLC(2, label="mylabel") 

sage: R.precision().prime() 

2 

""" 

return self._p 

 

def _index(self, ref): 

r""" 

Return the index of the column in the precision matrix that  

corresponds to ``ref``. 

 

Only for internal use. 

 

TESTS:: 

 

sage: from sage.rings.padics.lattice_precision import pAdicLatticeElementWeakProxy 

sage: R = ZpLC(2, label="index") 

sage: prec = R.precision() 

sage: x = R(1, 10) 

sage: y = R(1, 5) 

 

sage: prec._index(pAdicLatticeElementWeakProxy(x)) 

0 

sage: prec._index(pAdicLatticeElementWeakProxy(y)) 

1 

 

sage: del x 

sage: prec.del_elements() 

sage: prec._index(pAdicLatticeElementWeakProxy(y)) 

0 

""" 

return self._elements.index(ref) 

 

def ambient_dimension(self): 

r""" 

Return the dimension of the vector space in which the precision 

module/lattice lives. 

 

EXAMPLES: 

 

sage: R = ZpLC(2, label='ambient_dim') 

sage: prec = R.precision() 

 

sage: x, y = R(1, 10), R(1, 5) 

sage: prec.ambient_dimension() 

2 

sage: prec.dimension() 

2 

 

sage: u = x + y 

sage: prec.ambient_dimension() 

3 

sage: prec.dimension() 

3 

 

In the case of ``ZpLC`` (lattice-cap precision), it is always 

equal to the dimension of the lattice. 

 

In the case of ``ZpLF`` (lattice-float precision), the precision 

object is not necessarily a lattice and then may have smaller 

dimension:: 

 

sage: R = ZpLF(2, label='ambient_dim') 

sage: prec = R.precision() 

 

sage: x, y = R(1, 10), R(1, 5) 

sage: prec.ambient_dimension() 

2 

sage: prec.dimension() 

2 

 

sage: u = x + y 

sage: prec.ambient_dimension() 

3 

sage: prec.dimension() 

2 

""" 

return len(self._matrix) 

 

@abstract_method 

def dimension(self): 

r""" 

Return the dimension of this precision module. 

 

EXAMPLES:: 

 

sage: R = ZpLC(5, label='dim') 

sage: prec = R.precision() 

sage: prec.dimension() 

0 

 

sage: x = R(1, 10) 

sage: prec.dimension() 

1 

""" 

pass 

 

@abstract_method 

def _new_element(self, *args, **kwargs): 

r""" 

Insert a new element in this precision module. 

 

TESTS:: 

 

sage: R = ZpLC(2) 

sage: x = R.random_element() 

sage: y = R.random_element() 

sage: z = x*y # indirect doctest 

""" 

pass 

 

def _record_collected_element(self, ref): 

r""" 

Record that the element with weak reference ``ref`` 

has been collected by the garbage collector. 

 

INPUT: 

 

- ``ref`` -- the weak reference of the collected element 

 

TESTS:: 

 

sage: R = ZpLC(2, label='gc') 

sage: prec = R.precision() 

 

sage: x = R.random_element() 

sage: prec._collected_references 

[] 

 

sage: del x 

sage: prec._collected_references 

[WeakProxy#...] 

""" 

self._collected_references.append(ref) 

 

@abstract_method 

def del_elements(self, threshold=None): 

r""" 

Delete (or mark for future deletion) the columns of precision  

matrix corresponding to elements that were collected by the  

garbage collector. 

 

INPUT: 

 

- ``threshold`` -- an integer or ``None`` (default: ``None``): 

a column whose distance to the right is greater than the 

threshold is not erased but marked for deletion;  

if ``None``, always erase (never mark for deletion). 

 

EXAMPLES:: 

 

sage: R = ZpLC(2, label='del_elements') 

sage: prec = R.precision() 

 

sage: x = R(1, 10) 

sage: prec 

Precision lattice on 1 object (label: del_elements) 

sage: prec.precision_lattice() 

[1024] 

 

sage: del x 

sage: prec 

Precision lattice on 1 object (label: del_elements) 

sage: prec.precision_lattice() 

[1024] 

 

sage: prec.del_elements() 

sage: prec 

Precision lattice on 0 objects (label: del_elements) 

sage: prec.precision_lattice() 

[] 

""" 

pass 

 

@abstract_method 

def _precision_absolute(self, x): 

r""" 

Return the absolute precision of the given element. 

 

INPUT: 

 

- ``x`` -- the element whose absolute precision is requested 

 

.. NOTE:: 

 

The absolute precision is obtained by projecting the precision 

lattice onto the line of coordinate ``dx``. 

 

This function is not meant to be called directly. Call 

``x.precision_absolute()`` instead. 

 

EXAMPLES:: 

 

sage: R = ZpLC(2) 

sage: x = R(1, 10); x 

1 + O(2^10) 

sage: y = R(1, 5); y 

1 + O(2^5) 

sage: z = x + y; z 

2 + O(2^5) 

sage: z.precision_absolute() # indirect doctest 

5 

""" 

pass 

 

@abstract_method 

def precision_lattice(self, elements=None): 

r""" 

Return a lattice modeling the precision on the given set of elements 

or, if not given, on the whole set of elements tracked by the precision 

module. 

 

INPUT: 

 

- ``elements`` -- a list of elements or ``None`` (default: ``None``) 

 

EXAMPLES:: 

 

sage: R = ZpLC(2, label='precision_lattice') 

sage: prec = R.precision() 

sage: x = R(1, 10); y = R(1, 5) 

sage: u = x + y 

sage: v = x - y 

sage: prec.precision_lattice() 

[ 1024 0 1024 1024] 

[ 0 32 32 1099511627744] 

[ 0 0 2097152 0] 

[ 0 0 0 1099511627776] 

sage: prec.precision_lattice([u, v]) 

[ 32 2016] 

[ 0 2048] 

 

If the precision module does not project to a lattice, 

an error is raised. 

 

sage: R = ZpLF(2, label='precision_lattice') 

sage: prec = R.precision() 

sage: x = R(1, 10); y = R(1, 5) 

sage: u = x + y 

sage: v = x - y 

sage: prec.precision_lattice([x,y,u,v]) 

Traceback (most recent call last): 

... 

PrecisionError: the differential is not surjective 

""" 

pass 

 

def diffused_digits(self, elements=None): 

r""" 

Return the number of diffused digits of precision within a  

subset of elements. 

 

A diffused digit of precision is a known digit which is not 

located on a single variable but only appears on a suitable 

linear combination of variables. 

 

The number of diffused digits of precision quantifies the 

quality of the approximation of the lattice precision by a 

jagged precision (that is a precision which is split over  

all variables). 

 

We refer to [CRV2018]_ for a detail exposition of the notion of 

diffused digits. 

 

EXAMPLES:: 

 

sage: R = ZpLC(2) 

sage: prec = R.precision() 

sage: x = R(1, 10); y = R(1, 5) 

sage: u = x + y 

sage: v = x - y 

 

sage: prec.diffused_digits([x, y]) 

0 

sage: prec.diffused_digits([u, v]) 

6 

 

The elements `u` and `v` are known at absolute precision `O(2^5)`. 

However, the sum `u + v = 2x` is known at precision `O(2^11)`, that 

is with `6` more digits. 

That is where the `6` diffused digits of precision comes from. 

 

Here is another example with matrices:: 

 

sage: M = matrix(R, 2, 2, [R(3, 5), R(7, 5), R(1, 5), R(11, 1)]) 

sage: N = M^10 

 

The next syntax provides as easy way to select an interesting 

subset of variables (the selected subset consists of the four 

entries of the matrix ``N``):: 

 

sage: prec.diffused_digits(N) 

17 

 

Note that, in some cases, the number of diffused digits can be 

infinite:: 

 

sage: R = ZpLF(2) 

sage: prec = R.precision() 

sage: x = R(1, 10) 

sage: y = x 

sage: prec.diffused_digits([x, y]) 

+Infinity 

""" 

try: 

M = self.precision_lattice(elements) 

except PrecisionError: 

return Infinity 

n = M.nrows() 

p = self._p 

return sum(M[i, i].valuation(p) - min(M[j, i].valuation(p) for j in range(i + 1)) for i in range(n)) 

 

def tracked_elements(self, values=True, dead=True): 

r""" 

Return the list of tracked elements. 

 

INPUT: 

 

- ``values`` -- a boolean (default: ``True``); if false, 

the method returns a list of weak references on tracked 

elements instead 

 

- ``dead`` -- a boolean (default: ``True``); whether dead 

elements for which the corresponding column is still not 

erased should be listed or not 

 

EXAMPLES:: 

 

sage: R = ZpLC(2, label='tracked') 

sage: prec = R.precision() 

sage: x = R(1, 10); y = R(1, 5) 

sage: prec.tracked_elements() 

[1 + O(2^10), 1 + O(2^5)] 

sage: prec.tracked_elements(values=False) 

[WeakProxy#..., 

WeakProxy#..., 

WeakProxy#...] 

sage: prec.tracked_elements(values=False, dead=False) 

[WeakProxy#..., 

WeakProxy#...] 

 

sage: u = x + y 

sage: v = x - y 

sage: prec.tracked_elements() 

[1 + O(2^10), 1 + O(2^5), 2 + O(2^5), O(2^5)] 

sage: prec.tracked_elements(values=False) 

[WeakProxy#..., 

WeakProxy#..., 

WeakProxy#..., 

WeakProxy#..., 

WeakProxy#...] 

 

sage: del x; del y 

sage: prec.tracked_elements() 

[None, None, 2 + O(2^5), O(2^5), None] 

sage: prec.tracked_elements(values=False) 

[WeakProxy#..., 

WeakProxy#..., 

WeakProxy#...] 

""" 

ret = [ ref for ref in self._elements if dead or ref() is not None] 

if values: 

ret = [ ref() for ref in ret ] 

return ret 

 

# History 

 

def history_enable(self): 

r""" 

Enable history. 

 

We refer to the documentation of the method :meth:`history` for  

a complete documentation (including examples) about history. 

 

TESTS:: 

 

sage: R = ZpLC(2, label='history_en') 

sage: prec = R.precision() 

 

sage: print(prec.history()) # history is disabled by default 

Traceback (most recent call last): 

... 

ValueError: History is not tracked 

 

sage: prec.history_enable() 

sage: print(prec.history()) 

Timings 

---  

 

.. SEEALSO:: 

 

:meth:`history`, :meth:`history_disable`, :meth:`history_clear` 

""" 

if self._history is None: 

self._history_init = ( len(self._elements), list(self._marked_for_deletion) ) 

self._history = [ ] 

 

def history_disable(self): 

r""" 

Disable history. 

 

We refer to the documentation of the method :meth:`history` for  

a complete documentation (including examples) about history. 

 

TESTS:: 

 

sage: R = ZpLC(2, label='history_dis') 

sage: prec = R.precision() 

 

sage: print(prec.history()) # history is disabled by default 

Traceback (most recent call last): 

... 

ValueError: History is not tracked 

 

sage: prec.history_enable() 

sage: print(prec.history()) 

Timings 

---  

 

sage: prec.history_disable() 

sage: print(prec.history()) 

Traceback (most recent call last): 

... 

ValueError: History is not tracked 

 

.. SEEALSO:: 

 

:meth:`history`, :meth:`history_enable`, :meth:`history_clear` 

""" 

self._history = self._history_init = None 

 

def history_clear(self): 

r""" 

Clear history. 

 

We refer to the documentation of the method :meth:`history` for  

a complete documentation (including examples) about history. 

 

TESTS:: 

 

sage: R = ZpLC(2, label='history_clear') 

sage: prec = R.precision() 

sage: prec.history_enable() 

 

sage: x = R(1, 10); y = R(1, 5) 

sage: x, y = x+y, x-y 

sage: print(prec.history()) # somewhat random 

Timings 

0.000213s oooo 

 

When we clear history, only the last line is kept:: 

 

sage: prec.history_clear() 

sage: print(prec.history()) 

Timings oooo 

--- oooo 

 

sage: prec.del_elements() 

 

sage: print(prec.history()) # somewhat random 

Timings oooo 

0.000005s ~~oo 

0.000285s oo 

 

.. SEEALSO:: 

 

:meth:`history`, :meth:`history_enable`, :meth:`history_disable` 

""" 

if self._history is None: 

raise ValueError("History is not tracked") 

self._history_init = ( len(self._elements), list(self._marked_for_deletion) ) 

self._history = [ ] 

 

def _format_history(self, time, status, timings): 

r""" 

Return a formatted output for the history. 

 

This is a helper function for the method :meth:`history`. 

 

TESTS:: 

 

sage: R = ZpLC(2, label='history_en') 

sage: prec = R.precision() 

sage: prec._format_history(1.23456789, ['o', 'o', 'o', 'o', 'o', 'o', '~', 'o', 'o'], true) 

'1.234568s oooooo~oo' 

sage: prec._format_history(1.23456789, ['o', 'o', 'o', 'o', 'o', 'o', '~', 'o', 'o'], false) 

'oooooo~oo' 

 

sage: prec._format_history(12.3456789, ['o', 'o', 'o', 'o', 'o', 'o', '~', 'o', 'o'], true) 

' >= 10s oooooo~oo' 

sage: prec._format_history(10^(-10), ['o', 'o', 'o', 'o', 'o', 'o', '~', 'o', 'o'], true) 

' --- oooooo~oo' 

sage: prec._format_history(-1, ['o', 'o', 'o', 'o', 'o', 'o', '~', 'o', 'o'], true) 

' Timings oooooo~oo' 

""" 

status = ''.join(status) 

if timings: 

if time < 0: 

s = " Timings " 

elif time < 0.000001: 

s = " --- " 

elif time >= 10: 

s = " >= 10s " 

else: 

s = "%.6fs" % time 

return s + " " + status 

else: 

return status 

 

 

def history(self, compact=True, separate_reduce=False, timings=True, output_type='asciiart'): 

r""" 

Show history. 

 

The history records creations and deletions of elements attached  

to this precision lattice, together with many timings. 

 

INPUT: 

 

- ``compact`` -- a boolean (default: ``True``); if true, all  

consecutive operations of the same type appear on a single row 

 

- ``separate_reduce`` -- a boolean (default: ``False``); specify 

whether partial/full Hermite reduction should be displayed 

separatedly 

 

- ``timings`` -- a boolean (default: ``True``); specify whether 

timings should be displayed 

 

- ``output_type`` -- only ``asciiart`` is implemented for now. 

 

IMPORTANT NOTE: 

 

History is disabled by default. 

It should then be enabled (through a call to the method :meth:`history_enable`) 

before use. 

 

EXAMPLES:: 

 

sage: R = ZpLC(2, label='history_en') 

sage: prec = R.precision() 

 

We first enable history:: 

 

sage: prec.history_enable() 

 

At the beginning, the history is of course empty:: 

 

sage: print(prec.history()) 

Timings  

--- 

 

Now we start creating and deleting elements:: 

 

sage: L = [ R.random_element() for _ in range(20) ] 

sage: for p in range(20): 

....: if is_prime(p): L[p] = None 

sage: prec.del_elements() 

 

sage: print(prec.history()) # somewhat random 

Timings 

0.001108s oooooooooooooooooooo 

0.000009s oo~~o~o~ooo~o~ooo~o~ 

0.014250s oooooooooooo 

 

The legend is the following:: 

- the symbol ``o`` represents a tracked element, 

- the symbol ``~`` represents an element which is marked for deletion. 

 

On the history, we see: 

- 1st line: twenty new elements were created 

(this corresponds to the affectation of the list ``L``); 

- 2nd line: elements at prime positions were marked for deletion 

(this corresponds to the ``for`` loop); 

- 3rd line: the above elements are indeed deleted 

(this corresponds to the call of the method :meth:`del_elements`. 

 

Here are some variants:: 

 

sage: print(prec.history(timings=False)) 

oooooooooooooooooooo 

oo~~o~o~ooo~o~ooo~o~ 

oooooooooooo 

 

sage: print(prec.history(separate_reduce=True)) # somewhat random 

Timings 

0.001063s oooooooooooooooooooo 

0.000014s oo~~o~o~ooo~o~ooo~o~ 

0.000798s oo~~o~o~ooo~ooooo 

0.000233s oo~~o~o~ooo~orrrr 

0.000824s oo~~o~o~oooooooo 

0.000375s oo~~o~o~ooorrrrr 

0.001724s oo~~o~ooooooooo 

0.001020s oo~~o~orrrrrrrr 

0.001989s oo~~oooooooooo 

0.001303s oo~~orrrrrrrrr 

0.002352s oo~oooooooooo 

0.001632s oo~rrrrrrrrrr 

0.002265s oooooooooooo 

0.001630s oorrrrrrrrrr 

--- oooooooooooo 

 

The symbol ``r`` represents a column of the precision matrix which is 

currently under partial Hermite reduction. 

 

Timings for automatic reduction do not appear because they are included 

in the timings for deletion. 

 

The symbol ``R`` is used to symbolize a column which is under full  

Hermite reduction. Note that full Hermite reduction are never performed  

automatically but needs to be called by hand:: 

 

sage: prec.reduce() 

sage: print(prec.history(separate_reduce=True)) # somewhat random 

Timings 

0.001063s oooooooooooooooooooo 

0.000014s oo~~o~o~ooo~o~ooo~o~ 

0.000798s oo~~o~o~ooo~ooooo 

0.000233s oo~~o~o~ooo~orrrr 

0.000824s oo~~o~o~oooooooo 

0.000375s oo~~o~o~ooorrrrr 

0.001724s oo~~o~ooooooooo 

0.001020s oo~~o~orrrrrrrr 

0.001989s oo~~oooooooooo 

0.001303s oo~~orrrrrrrrr 

0.002352s oo~oooooooooo 

0.001632s oo~rrrrrrrrrr 

0.002265s oooooooooooo 

0.001630s oorrrrrrrrrr 

0.001486s RRRRRRRRRRRR 

--- oooooooooooo 

 

Here is a more common example with matrices:: 

 

sage: R = ZpLC(3) 

sage: prec = R.precision() 

sage: prec.history_enable() 

sage: M = random_matrix(R, 5) 

sage: d = M.determinant() 

sage: print(prec.history()) # somewhat random 

--- 

0.004212s oooooooooooooooooooooooooooooooooooo 

0.000003s oooooooooooooooooooooooooooooooooo~~ 

0.000010s oooooooooooooooooooooooooooooooooo 

0.001560s ooooooooooooooooooooooooooooooooooooooooo 

0.000004s ooooooooooooooooooooooooooooo~oooo~oooo~o 

0.002168s oooooooooooooooooooooooooooooooooooooo 

0.001787s ooooooooooooooooooooooooooooooooooooooooo 

0.000004s oooooooooooooooooooooooooooooooooooooo~~o 

0.000198s ooooooooooooooooooooooooooooooooooooooo 

0.001152s ooooooooooooooooooooooooooooooooooooooooo 

0.000005s ooooooooooooooooooooooooooooooooo~oooo~~o 

0.000853s oooooooooooooooooooooooooooooooooooooo 

0.000610s ooooooooooooooooooooooooooooooooooooooo 

[...] 

0.003879s ooooooooooooooooooooooooooooooooooooooooooooooooooooooooo 

0.000006s oooooooooooooooooooooooooooooooooooooooooooooooooooo~~~~~ 

0.000036s oooooooooooooooooooooooooooooooooooooooooooooooooooo 

0.006737s oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo 

0.000005s oooooooooooooooooooooooooooooooooooooooooooooooooooo~~~~~ooooo 

0.002637s ooooooooooooooooooooooooooooooooooooooooooooooooooooooooo 

0.007118s ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo 

0.000008s oooooooooooooooooooooooooooooooooooooooooooooooooooo~~~~o~~~~oooo 

0.003504s ooooooooooooooooooooooooooooooooooooooooooooooooooooooooo 

0.005371s ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo 

0.000006s ooooooooooooooooooooooooooooooooooooooooooooooooooooo~~~o~~~ooo 

0.001858s ooooooooooooooooooooooooooooooooooooooooooooooooooooooooo 

0.003584s ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo 

0.000004s oooooooooooooooooooooooooooooooooooooooooooooooooooooo~~o~~oo 

0.000801s ooooooooooooooooooooooooooooooooooooooooooooooooooooooooo 

0.001916s ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo 

0.000022s ooooooooooooooooooooooooooooo~~~~~~~~~~~~~~~~~~~~~~oooo~o~o 

0.014705s ooooooooooooooooooooooooooooooooooo 

0.001292s ooooooooooooooooooooooooooooooooooooo 

 

We observe that deleted variables appear mostly on the right. 

This is the so-called principal of temporal locality. 

 

.. SEEALSO:: 

 

:meth:`history_enable`, :meth:`history_disable`, :meth:`history_clear` 

""" 

if self._history is None: 

raise ValueError("History is not tracked") 

total_time = 0 

if output_type == 'asciiart': 

# Legend: 

# o : tracked element 

# ~ : element marked for deletion 

# r : partial reduction 

# R : full Hermite reduction 

(n, mark) = self._history_init 

status = n*['o'] 

for index in mark: 

status[index] = '~' 

hist = [ self._format_history(-1, status, timings) ] 

oldevent = ''; total_time = 0 

for (event, index, tme) in self._history: 

if event == 'partial reduce' or event == 'full reduce': 

if separate_reduce: 

if total_time > 0: 

hist.append(self._format_history(total_time, status, timings)) 

if event == 'partial reduce': code = 'r' 

else: code = 'R' 

status_red = status[:index] + (len(status) - index) * [code] 

hist.append(self._format_history(tme, status_red, timings)) 

total_time = 0 

oldevent = '' 

else: 

total_time += tme 

continue 

if not compact or event != oldevent: 

if total_time > 0: 

hist.append(self._format_history(total_time, status, timings)) 

total_time = 0 

oldevent = event 

total_time += tme 

if event == 'add': 

if index is None: 

status.append('o') 

else: 

status = status[:index] + ['o'] + status[index:] 

elif event == 'mark': 

status[index] = '~' 

elif event == 'del': 

del status[index] 

if total_time > 0 or oldevent == '': 

hist.append(self._format_history(total_time, status, timings)) 

return '\n'.join(hist) 

else: 

raise NotImplementedError 

 

def timings(self, action=None): 

r""" 

Return cumulated timings (grouped by actions) since the last  

time history has been cleared. 

 

INPUT: 

 

- ``action`` -- ``None`` (the default), ``add``, ``mark``, ``del``, 

``partial reduce`` or ``full reduce``; if not None, return the  

cumulated timing corresponding to this action; otherwise, return 

a dictionary 

 

Here are the meanings of the keywords above: 

- ``add``: time spent in adding new colunmns to the precision matrix 

(corresponding to the creation of new elements) 

- ``mark``: time spent in marking elements for deletion 

- ``del``: time spent in deleting columns of the precision matrix 

and re-echelonizing the matrix 

- ``partial reduce``: time spent in partial Hermite reduction 

- ``full reduce``: time spent in full Hermite reduction. 

 

EXAMPLES:: 

 

sage: R = ZpLC(2, label='timings') 

sage: prec = R.precision() 

sage: prec.history_enable() 

sage: M = random_matrix(R, 5, 5) 

sage: N = M^10 

sage: prec.timings() # somewhat random 

{'add': 1.0530245304107666, 

'del': 0.24358701705932617, 

'mark': 0.0013289451599121094, 

'partial reduce': 0.21604204177856445 

'full reduce': 0} 

 

TESTS:: 

 

sage: prec.history_clear() 

sage: prec.timings() 

{'add': 0, 'del': 0, 'full reduce': 0, 'mark': 0, 'partial reduce': 0} 

""" 

if self._history is None: 

raise ValueError("History is not tracked") 

tme_by_event = { 'add': 0, 'del': 0, 'mark': 0, 'partial reduce': 0, 'full reduce': 0 } 

for (event, _, tme) in self._history: 

tme_by_event[event] += tme 

if action is None: 

return tme_by_event 

if tme_by_event.has_key(action): 

return tme_by_event[action] 

else: 

raise ValueError("invalid event") 

 

 

class PrecisionLattice(UniqueRepresentation, DifferentialPrecisionGeneric): 

r""" 

A class for handling precision lattices which are used to 

track precision in the ZpLC model. 

 

The precision lattice is stored as a triangular matrix whose 

rows are generators of the lattice. 

 

INPUT: 

 

- ``p`` -- a prime number 

 

- ``label`` -- a string, the label of the parents to which the elements 

tracked by this lattice belong. 

 

.. NOTE:: 

 

You should not create instances of this class directly. The precision 

lattice is automatically initialized at the creation of the parent. 

 

EXAMPLES:: 

 

sage: R = ZpLC(2, label='init') 

sage: R.precision() 

Precision lattice on 0 objects (label: init) 

""" 

def __init__(self, p, label): 

r""" 

TESTS:: 

 

sage: from sage.rings.padics.lattice_precision import PrecisionLattice 

sage: R = ZpLC(2) 

sage: isinstance(R.precision(), PrecisionLattice) 

True 

 

""" 

DifferentialPrecisionGeneric.__init__(self, p, label) 

self._repr_type = "Precision lattice" 

self._capped = { } 

 

# We need to copy this method. 

# Indeed otherwise it is inherited from UniqueRepresentation 

def __reduce__(self): 

r""" 

TESTS:: 

 

sage: R = ZpLC(2) 

sage: prec = R.precision() 

sage: dumps(prec) 

Traceback (most recent call last): 

... 

NotImplementedError: pickling/unpickling precision modules is not implemented yet 

""" 

raise NotImplementedError("pickling/unpickling precision modules is not implemented yet") 

 

def _index(self, ref): 

r""" 

Return the index of the element whose reference is ``ref``. 

 

TESTS:: 

 

sage: from sage.rings.padics.lattice_precision import pAdicLatticeElementWeakProxy 

sage: R = ZpLC(2, label="index") 

sage: prec = R.precision() 

sage: x = R(1, 10) 

sage: y = R(1, 5) 

 

sage: prec._index(pAdicLatticeElementWeakProxy(x)) 

0 

sage: prec._index(pAdicLatticeElementWeakProxy(y)) 

1 

 

sage: del x 

sage: prec.del_elements() 

sage: prec._index(pAdicLatticeElementWeakProxy(y)) 

0 

""" 

return len(self._matrix[ref]) - 1 

 

def dimension(self): 

r""" 

Return the dimension of this lattice. 

 

EXAMPLES:: 

 

sage: R = ZpLC(5, label='dimension') 

sage: prec = R.precision() 

sage: prec.dimension() 

0 

 

sage: x = R(1, 10) 

sage: prec.dimension() 

1 

""" 

return len(self._matrix) 

 

def reduce(self, index=0, partial=False): 

r""" 

Reduce the size of the entries above the diagonal of the precision matrix. 

 

INPUT: 

 

- ``index`` -- an integer, the starting row for which the reduction 

is performed 

 

- ``partial`` -- a boolean (default: False) specifying whether a 

partial or a full Hermite reduction should be performed 

 

NOTE: 

 

The partial reduction has cost `O(m^2)` where `m` is the number of  

rows that need to be reduced (that is the difference between the  

total number of rows and ``index``). 

 

The full Hermite reduction has cost `O(m^3)`. 

 

.. NOTE:: 

 

The software ensures that the precision lattice is always partially 

reduced. Calling the function manually with the argument 

``partial=True`` should then just do nothing. 

 

TESTS:: 

 

sage: R = ZpLC(2) 

sage: x = R.random_element() 

sage: del x 

sage: R.precision().del_elements() # indirect doctest 

""" 

n = len(self._elements) 

if index >= n-1: 

return 

if partial: 

# Partial reduction 

# Cost: O(m^2) with m = n-index 

tme = walltime() 

diffval = (n-index) * [0] 

for j in range(n-1, index, -1): 

col = self._matrix[self._elements[j]] 

prec = col[j].valuation() - diffval[j-index] 

for i in range(index, j): 

col[i] = col[i].reduce(prec) 

col[i].normalize() 

dval = col[i].valuation() - prec 

if dval < diffval[i-index]: 

diffval[i-index] = dval 

# We update history 

if self._history is not None: 

self._history.append(('partial reduce', index, walltime(tme))) 

else: 

# Full Hermite reduction 

# Cost: O(m^3) with m = n-index 

tme = walltime() 

for j in range(index+1, n): 

# In what follows, we assume that col[j] is a power of p 

col = self._matrix[self._elements[j]] 

valpivot = col[j].valuation() 

for i in range(index, j): 

reduced = col[i].reduce(valpivot) 

scalar = (col[i] - reduced) >> valpivot 

if scalar.is_zero(): continue 

col[i] = reduced 

col[i].normalize() 

for j2 in range(j+1, n): 

col2 = self._matrix[self._elements[j2]] 

col2[i] -= scalar*col2[i] 

col2[i].normalize() 

# We update history 

if self._history is not None: 

self._history.append(('full reduce', index, walltime(tme))) 

 

def _new_element(self, x, dx, bigoh, dx_mode='linear_combination', capped=False): 

r""" 

Update the lattice when a new element is created. 

 

This function is not meant to be called manually. 

It is automatically called by the parent when a new 

element is created. 

 

INPUT: 

 

- ``x`` -- the newly created element 

 

- ``dx`` -- a dictionary representing the differential of ``x`` 

 

- ``bigoh`` -- an integer or ``None`` (default: ``None``): the  

bigoh to be added to the precision of ``x``; if ``None``, the 

default cap is used. 

 

- ``dx_mode`` -- a string, either ``linear_combination`` (the default) 

or ``values`` 

 

- ``capped`` -- a boolean, whether this element has been capped  

according to the parent's cap 

 

If ``dx_mode`` is ``linear_combination``, the dictionary ``dx``  

encodes the expression of the differential of ``x``.  

For example, if ``x`` was defined as ``x = y*z`` then: 

 

.. MATH:: 

 

dx = y dz + z dy 

 

and the corresponding dictionary is ``{z: y, y: z}`` (except 

that the keys are not the elements themselves but weak references 

to them). 

 

If ``dx_mode`` is ``values``, the dictionary ``dx`` directly 

specifies the entries that have to be stored in the precision lattice. 

This mode is only used for multiple conversion between different 

parents (see :meth:`multiple_conversion`). 

 

TESTS:: 

 

sage: R = ZpLC(2) 

sage: x = R.random_element() 

sage: y = R.random_element() 

sage: z = x*y # indirect doctest 

""" 

# First we delete some elements marked for deletion 

self.del_elements(threshold=self._threshold_deletion) 

 

# Then we add the new element 

tme = walltime() 

p = self._p 

n = len(self._elements) 

x_ref = pAdicLatticeElementWeakProxy(x, self._record_collected_element) 

self._elements.append(x_ref) 

col = n * [self._approx_zero] 

if dx_mode == 'linear_combination': 

for elt, scalar in dx: 

ref = pAdicLatticeElementWeakProxy(elt) 

if not isinstance(scalar, pRational): 

scalar = pRational(p, scalar) 

c = self._matrix[ref] 

for i in range(len(c)): 

col[i] += scalar * c[i] 

elif dx_mode == 'values': 

for elt, scalar in dx: 

ref = pAdicLatticeElementWeakProxy(elt) 

if not isinstance(scalar, pRational): 

scalar = pRational(p, scalar) 

i = self._index(ref) 

col[i] = scalar 

else: 

raise ValueError("dx_mode must be either 'linear_combination' or 'values'") 

for i in range(n): 

col[i] = col[i].reduce(bigoh) 

col.append(pRational(p, ZZ(1), bigoh)) 

self._matrix[x_ref] = col 

self._capped[x_ref] = capped 

 

# We update history 

if self._history is not None: 

self._history.append(('add', None, walltime(tme))) 

 

def del_elements(self, threshold=None): 

r""" 

Erase columns of the lattice precision matrix corresponding to 

elements which are marked for deletion and echelonize the matrix 

in order to keep it upper triangular. 

 

INPUT: 

 

- ``threshold`` -- an integer or ``None`` (default: ``None``): 

a column whose distance to the right is greater than the 

threshold is not erased 

 

EXAMPLES:: 

 

sage: R = ZpLC(2, label='delelts') 

sage: prec = R.precision() 

 

sage: x = R(1, 10) 

sage: prec 

Precision lattice on 1 object (label: delelts) 

sage: prec.precision_lattice() 

[1024] 

 

sage: del x 

sage: prec 

Precision lattice on 1 object (label: delelts) 

sage: prec.precision_lattice() 

[1024] 

 

sage: prec.del_elements() 

sage: prec 

Precision lattice on 0 objects (label: delelts) 

sage: prec.precision_lattice() 

[] 

""" 

p = self._p 

n = len(self._elements) 

 

# We mark new collected elements for deletion 

# The list self._collected_references can be updated while 

# the loop runs. 

# However, we do not need to copy it because Python supports 

# iteration over a list to which elements are added. 

count = 0 

for ref in self._collected_references: 

count += 1 

tme = walltime() 

index = self._index(ref) 

self._marked_for_deletion.append(index) 

if self._history is not None: 

self._history.append(('mark', index, walltime(tme))) 

del self._collected_references[:count] 

 

# We erase corresponding columns and echelonize 

self._marked_for_deletion.sort(reverse=True) 

count = 0 

for index in self._marked_for_deletion: 

if threshold is not None and index < n - threshold: break 

n -= 1; count += 1 

 

tme = walltime() 

ref = self._elements[index] 

del self._elements[index] 

del self._matrix[ref] 

capped = self._capped[ref] 

del self._capped[ref] 

 

# Now, we echelonize 

for i in range(index, n): 

ref = self._elements[i] 

col = self._matrix[ref] 

if col[i].valuation() < col[i+1].valuation(): 

self._capped[ref], capped = capped, capped or self._capped[ref] 

else: 

capped = capped or self._capped[ref] 

 

d, u, v = col[i].xgcd(col[i+1]) 

up, vp = col[i+1]/d, col[i]/d 

col[i] = d 

del col[i+1] 

for j in range(i+1, n): 

col = self._matrix[self._elements[j]] 

col[i], col[i+1] = u*col[i] + v*col[i+1], up*col[i] - vp*col[i+1] 

 

# We update history 

if self._history is not None: 

self._history.append(('del', index, walltime(tme))) 

 

# And we reduce a bit 

# (we do not perform a complete reduction because it is costly) 

self.reduce(index, partial=True) 

 

del self._marked_for_deletion[:count] 

 

def _lift_to_precision(self, x, prec): 

r""" 

Lift the specified element to the specified precision. 

 

INPUT: 

 

- ``x`` -- the element whose precision has to be lifted 

 

- ``prec`` -- the new precision 

 

NOTE: 

 

The new precision lattice is computed as the intersection 

of the current precision lattice with the subspace 

 

..MATH:: 

 

p^{prec} \Z_p dx \oplus \bigoplus_{y \neq x} \Q_p dy 

 

This function may change at the same time the precision of  

other elements having the same parent. 

 

.. NOTE:: 

 

This function is not meant to be called directly. Use 

``x.lift_to_precision`` instead. 

 

EXAMPLES:: 

 

sage: R = ZpLC(2) 

sage: x = R(1, 10); x 

1 + O(2^10) 

sage: y = R(1, 5); y 

1 + O(2^5) 

sage: z = x + y; z 

2 + O(2^5) 

 

sage: prec = R.precision() 

sage: prec._lift_to_precision(z, 12) 

sage: z 

2 + O(2^12) 

sage: y 

1 + O(2^10) 

""" 

ref = pAdicLatticeElementWeakProxy(x) 

col = self._matrix[ref] 

n = len(self._elements) 

 

rows_by_val = { } 

for i in range(len(col)): 

v = col[i].valuation() 

if v >= prec: continue 

if rows_by_val.has_key(v): 

rows_by_val[v].append(i) 

else: 

rows_by_val[v] = [i] 

vals = rows_by_val.keys() 

vals.sort() 

vals.append(prec) 

 

for t in range(len(vals)-1): 

v, w = vals[t], vals[t+1] 

rows = rows_by_val[v] 

piv = max(rows) 

for i in rows: 

if i == piv: continue 

# We clear the entry on the i-th row 

scalar = (col[i]/col[piv]).reduce(prec-v) 

for j in range(piv,n): 

col_cur = self._matrix[self._elements[j]] 

col_cur[i] -= scalar*col_cur[piv] 

# We rescale the piv-th row 

for j in range(piv,n): 

col_cur = self._matrix[self._elements[j]] 

col_cur[piv] <<= w - v 

# Now the entry on the piv-th row has valuation w 

# We update the dictionary accordingly 

if w < prec: 

rows_by_val[w].append(piv) 

 

self._precision_absolute_data.clear_cache() 

 

@cached_method(key=lambda self, x: pAdicLatticeElementWeakProxy(x)) 

def _precision_absolute_data(self, x): 

r""" 

Return absolute precision data for ``x``. 

 

.. NOTE:: 

 

Helper method for :meth:`_precision_absolute` and 

:meth:`_is_precision_capped`. 

 

TESTS:: 

 

sage: R = ZpLC(2) 

sage: x = R(1, 10); x 

1 + O(2^10) 

sage: y = R(1, 5); y 

1 + O(2^5) 

sage: z = x + y; z 

2 + O(2^5) 

sage: z.precision_absolute() # indirect doctest 

5 

""" 

ref = pAdicLatticeElementWeakProxy(x) 

col = self._matrix[ref] 

absprec = Infinity 

capped = False 

for i in range(len(col)): 

v = col[i].valuation() 

if v < absprec: 

absprec = v 

capped = self._capped[self._elements[i]] 

elif v == absprec: 

capped = capped and self._capped[self._elements[i]] 

return (absprec, capped) 

 

def _precision_absolute(self, x): 

r""" 

Return the absolute precision of the given element. 

 

INPUT: 

 

- ``x`` -- an element in the parent corresponding to this lattice 

 

.. NOTE:: 

 

The absolute precision is obtained by projecting the precision 

lattice onto the line of coordinate ``dx``. 

 

This function is not meant to be called directly. Call 

``x.precision_absolute()`` instead. 

 

EXAMPLES:: 

 

sage: R = ZpLC(2) 

sage: x = R(1, 10); x 

1 + O(2^10) 

sage: y = R(1, 5); y 

1 + O(2^5) 

sage: z = x + y; z 

2 + O(2^5) 

sage: z.precision_absolute() # indirect doctest 

5 

""" 

return self._precision_absolute_data(x)[0] 

 

def _is_precision_capped(self, x): 

r""" 

Return whether the absolute precision of ``x`` results from a cap 

coming from the parent. 

 

INPUT: 

 

- ``x`` -- an element in the parent corresponding to this lattice 

 

.. NOTE:: 

 

This function is not meant to be called directly. Call 

``x.is_precision_capped`` instead. 

 

EXAMPLES:: 

 

sage: R = ZpLC(2) 

sage: x = R(1, 10); x 

1 + O(2^10) 

sage: x.is_precision_capped() # indirect doctest 

False 

 

sage: y = x-x; y 

O(2^40) 

sage: y.is_precision_capped() # indirect doctest 

True 

""" 

return self._precision_absolute_data(x)[1] 

 

def precision_lattice(self, elements=None): 

r""" 

Return a matrix representing the precision lattice on a 

subset of elements. 

 

INPUT: 

 

- ``elements`` -- a list of elements or ``None`` (default: ``None``) 

 

- ``echelon`` -- a boolean (default: ``True``); whether the result 

should be in echelon form 

 

EXAMPLES:: 

 

sage: R = ZpLC(2, label='preclattice') 

sage: prec = R.precision() 

sage: x = R(1, 10); y = R(1, 5) 

sage: u = x + y 

sage: v = x - y 

sage: prec.precision_lattice() 

[ 1024 0 1024 1024] 

[ 0 32 32 1099511627744] 

[ 0 0 2097152 0] 

[ 0 0 0 1099511627776] 

sage: prec.precision_lattice([u, v]) 

[ 32 2016] 

[ 0 2048] 

 

Here is another example with matrices:: 

 

sage: M = matrix(R, 2, 2, [R(3, 5), R(7, 5), R(1, 5), R(11, 1)]) 

sage: N = M^10 

sage: prec.precision_lattice() 

23 x 23 dense matrix over Integer Ring (use the '.str()' method to see the entries) 

 

The next syntax provides as easy way to select an interesting 

subset of variables (the selected subset consists of the four 

entries of the matrix ``N``):: 

 

sage: prec.precision_lattice(N) 

[ 2048 512 28160 230400] 

[ 0 2048 14336 258048] 

[ 0 0 65536 65536] 

[ 0 0 0 262144] 

 

We can give a list of matrices as well:: 

 

sage: prec.precision_lattice([M, N]) 

[ 32 0 0 0 226115584 96788480 52174848 82804736] 

[ 0 32 0 0 52174848 121765888 11829248 28516352] 

[ 0 0 32 0 96788480 42762240 121765888 199614464] 

[ 0 0 0 2 5175296 12475904 1782272 4045824] 

[ 0 0 0 0 268435456 0 0 0] 

[ 0 0 0 0 0 268435456 0 0] 

[ 0 0 0 0 0 0 268435456 0] 

[ 0 0 0 0 0 0 0 268435456] 

""" 

if elements is None: 

elements = self._elements 

else: 

elements = list_of_padics(elements) 

n = len(self._elements) 

rows = [] 

val = 0 

for ref in elements: 

col = self._matrix[ref] 

row = [ x.value() for x in col ] 

valcol = min([ x.valuation() for x in col ]) 

if valcol < val: val = valcol 

row += (n-len(row)) * [ZZ(0)] 

rows.append(row) 

from sage.matrix.constructor import matrix 

M = matrix(rows).transpose() 

if val < 0: 

M *= self._p ** (-val) 

M = M.change_ring(ZZ) 

M.echelonize() 

n = len(elements) 

M = M.submatrix(0, 0, n, n) 

if val < 0: 

M *= self._p ** val 

return M 

 

 

class PrecisionModule(UniqueRepresentation, DifferentialPrecisionGeneric): 

r""" 

A class for handling precision modules which are used to 

track precision in the ZpLF model. 

 

The precision module (which is not necessarily a lattice) 

is stored as a matrix whose rows are generators. 

""" 

def __init__(self, p, label, prec): 

r""" 

Initialize this precision module. 

 

INPUT: 

 

- ``p`` -- a prime number 

 

- ``label`` -- a string, the label of the parents to which belong 

the elements tracked by this precision module 

 

NOTE: 

 

The precision module is automatically initialized at the  

creation of the parent. 

 

TESTS:: 

 

sage: R = ZpLF(2, label='init') 

sage: R.precision() 

Precision module on 0 objects (label: init) 

""" 

DifferentialPrecisionGeneric.__init__(self, p, label) 

# elements whose valuation are not less than self._zero_cap are assumed to vanish 

self._zero_cap = prec 

self._internal_prec = prec + STARTING_ADDITIONAL_PREC 

self._count = 0 

self._threshold = 1 

self._repr_type = "Precision module" 

 

# We need to copy this method. 

# Indeed otherwise it is inherited from UniqueRepresentation 

def __reduce__(self): 

r""" 

TESTS:: 

 

sage: R = ZpLF(2) 

sage: prec = R.precision() 

sage: dumps(prec) 

Traceback (most recent call last): 

... 

NotImplementedError: pickling/unpickling precision modules is not implemented yet 

""" 

raise NotImplementedError("pickling/unpickling precision modules is not implemented yet") 

 

def internal_prec(self): 

r""" 

Return the relative precision at which computations is handled 

internally. 

 

It is slightly greater than the actual precision and increases 

a bit (at a logarithmic rate) when new elements are created  

and/or computed. 

 

EXAMPLES:: 

 

sage: R = ZpLF(5, prec=20, label='internal_prec') 

sage: prec = R.precision() 

 

sage: prec.internal_prec() 

25 

 

sage: L = [ R.random_element() for _ in range(50) ] 

sage: prec.internal_prec() 

28 

""" 

return self._internal_prec 

 

def dimension(self): 

r""" 

Return the dimension of this precision module. 

 

EXAMPLES: 

 

In general, the dimension increases by 1 when a new 

element with a given precision is created:: 

 

sage: R = ZpLF(2, label='dimension') 

sage: prec = R.precision() 

 

sage: prec.dimension() 

0 

sage: x = R.random_element(prec=10) 

sage: prec.dimension() 

1 

sage: y = R.random_element(prec=10) 

sage: prec.dimension() 

2 

 

However in general it does not increase while 

doing computations:: 

 

sage: u = x + y 

sage: v = x^2 + 3*y + x*y + y^3 

sage: prec.dimension() 

2 

 

Of course, it may also decrease when a sufficient  

number of variables are collected:: 

 

sage: del x, y, u 

sage: prec.del_elements() 

sage: prec.dimension() 

1 

 

sage: del v 

sage: prec.del_elements() 

sage: prec.dimension() 

0 

""" 

if len(self._elements) == 0: 

return 0 

return len(self._matrix[self._elements[-1]]) 

 

def is_lattice(self): 

r""" 

Return ``True`` if this precision module is a lattice 

(i.e. has maximal dimension). 

 

EXAMPLES:: 

 

sage: R = ZpLF(2, label='is_lattice') 

sage: prec = R.precision() 

 

sage: x = R(1, 10) 

sage: y = R(1, 5) 

sage: prec.is_lattice() 

True 

 

sage: u = x + y 

sage: prec.is_lattice() 

False 

 

.. SEEALSO:: 

 

:meth:`dimension` 

""" 

return self.dimension() == len(self._elements) 

 

def _new_element(self, x, dx, bigoh, dx_mode='linear_combination'): 

r""" 

Update the lattice when a new element is created. 

 

This function is not meant to be called manually. 

It is automatically called by the parent when a new 

element is created. 

 

INPUT: 

 

- ``x`` -- the newly created element 

 

- ``dx`` -- a dictionary representing the differential of ``x`` 

 

- ``bigoh`` -- an integer or ``None`` (default: ``None``): the  

bigoh to be added to the precision of ``x``; if ``None``, the 

default cap is used. 

 

- ``dx_mode`` -- a string, either ``"linear_combination"`` (the 

default) or ``"values"`` 

 

If ``dx_mode`` is ``"linear_combination"``, the dictionary ``dx`` 

encodes the expression of the differential of ``x``. For example, if 

``x`` was defined as ``x = y*z`` then: 

 

.. MATH:: 

 

dx = y dz + z dy 

 

and the corresponding dictionary is ``{z: y, y: z}`` (except 

that the keys are not the elements themselves but weak references 

to them). 

 

If ``dx_mode`` is ``"values"``, the dictionary ``dx`` directly 

specifies the entries that have to stored in the precision module. 

This mode is only used for multiple conversion between different 

parents (see :meth:`multiple_conversion`). 

 

TESTS:: 

 

sage: R = ZpLF(2) 

sage: x = R.random_element() 

sage: y = R.random_element() 

sage: z = x*y # indirect doctest 

""" 

# First we delete some elements marked for deletion 

if self._marked_for_deletion: 

self.del_elements(threshold=self._threshold_deletion) 

 

# We increase the internal prec 

# The heuristic behind this is the following: when computing 

# with N digits of precision, we except that about N-log_p(c) 

# of them are correct after c elementary operations. 

self._count += 1 

if self._count > self._threshold: 

self._internal_prec += 1 

self._threshold *= self._p 

 

tme = walltime() 

p = self._p 

n = self.dimension() 

x_ref = pAdicLatticeElementWeakProxy(x, self._record_collected_element) 

col = n * [self._approx_zero] 

if dx_mode == 'linear_combination': 

expected_vals = n * [ Infinity ] 

for elt, scalar in dx: 

ref = pAdicLatticeElementWeakProxy(elt) 

if not isinstance(scalar, pRational): 

scalar = pRational(p, scalar) 

c = self._matrix[ref] 

for i in range(len(c)): 

summand = scalar * c[i] 

expected_vals[i] = min(expected_vals[i], summand.valuation()) 

col[i] += summand 

for i in range(n): 

if col[i].valuation() >= expected_vals[i] + self._zero_cap: 

col[i] = self._approx_zero 

elif dx_mode == 'values': 

for elt, scalar in dx: 

ref = pAdicLatticeElementWeakProxy(elt) 

if not isinstance(scalar, pRational): 

scalar = pRational(p, scalar) 

i = self._index(ref) 

col[i] = scalar 

else: 

raise ValueError("dx_mode must be either 'linear_combination' or 'values'") 

 

for i in range(n): 

col[i] = col[i].reduce_relative(self._internal_prec) 

if bigoh is not None: 

col.append(pRational(p, ZZ(1), bigoh)) 

 

self._elements.append(x_ref) 

self._matrix[x_ref] = col 

 

# We update history 

if self._history is not None: 

self._history.append(('add', None, walltime(tme))) 

 

def del_elements(self, threshold=None): 

r""" 

Erase columns of the lattice precision matrix corresponding to 

elements which were collected by the garbage collector. 

Then reduce the matrix in order to keep it in echelon form. 

 

INPUT: 

 

- ``threshold`` -- an integer or ``None`` (default: ``None``): 

a non-pivot column whose distance to the right is greater than  

the threshold is not erased but only marked for future deletion 

 

EXAMPLES:: 

 

sage: R = ZpLF(2, label='delelts') 

sage: prec = R.precision() 

 

sage: x = R(1, 10) 

sage: prec 

Precision module on 1 object (label: delelts) 

sage: prec.precision_lattice() 

[1024] 

 

sage: del x 

sage: prec 

Precision module on 1 object (label: delelts) 

sage: prec.precision_lattice() 

[1024] 

 

sage: prec.del_elements() 

sage: prec 

Precision module on 0 objects (label: delelts) 

sage: prec.precision_lattice() 

[] 

""" 

p = self._p 

 

# We mark new collected elements for deletion 

# The list self._collected_references can be updated while 

# the loop runs. 

# However, we do not need to copy it because Python supports 

# iteration over a list to which elements are added. 

count = 0 

for ref in self._collected_references: 

count += 1 

tme = walltime() 

index = self._index(ref) 

if index == 0: 

length_before = 0 

else: 

length_before = len(self._matrix[self._elements[index-1]]) 

length = len(self._matrix[ref]) 

if length > length_before: 

self._marked_for_deletion.append(index) 

if self._history is not None: 

self._history.append(('mark', index, walltime(tme))) 

else: 

# if the column is not a pivot, we erase it without delay 

# (btw, is it a good idea?) 

del self._elements[index] 

self._marked_for_deletion = [ i if i < index else i-1 for i in self._marked_for_deletion ] 

if self._history is not None: 

self._history.append(('del', index, walltime(tme))) 

del self._collected_references[:count] 

 

# We erase corresponding columns and echelonize 

n = len(self._elements) 

self._marked_for_deletion.sort(reverse=True) 

count = 0 

for index in self._marked_for_deletion: 

if threshold is not None and index < n - threshold: break 

n -= 1; count += 1 

 

tme = walltime() 

 

length = len(self._matrix[self._elements[index]]) 

del self._matrix[self._elements[index]] 

del self._elements[index] 

start = index 

while start < n: 

i = start 

val = Infinity 

end = n 

while i < n: 

col = self._matrix[self._elements[i]] 

if len(col) > length: 

end = i 

break 

v = col[-1].valuation() 

if v < val: 

val = v 

piv = i 

i += 1 

if val < Infinity: 

# another pivot has been found, we place it in front 

self._elements[start], self._elements[piv] = self._elements[piv], self._elements[start] 

break 

 

# No pivot was found. We re-echelonize 

for i in range(start, end): 

del self._matrix[self._elements[i]][-1] 

if end == n: break 

# col is the column of index "end" 

# its size is (length + 1) 

d, u, v = col[length-1].xgcd(col[length]) 

up, vp = col[length]/d, col[length-1]/d 

col[length-1] = d.reduce_relative(self._internal_prec) 

del col[length] 

start = end + 1 

for j in range(start, n): 

col = self._matrix[self._elements[j]] 

a1 = u*col[length-1]; a2 = v*col[length]; a = a1 + a2 

b1 = up*col[length-1]; b2 = vp*col[length]; b = b1 + b2 

if a.valuation() > min(a1.valuation(), a2.valuation()) + self._zero_cap: 

col[length-1] = self._approx_zero 

else: 

col[length-1] = a.reduce_relative(self._internal_prec) 

if b.valuation() > min(b1.valuation(), b2.valuation()) + self._zero_cap: 

col[length] = self._approx_zero 

else: 

col[length] = b.reduce_relative(self._internal_prec) 

length += 1 

 

# We update history 

if self._history is not None: 

self._history.append(('del', index, walltime(tme))) 

 

del self._marked_for_deletion[:count] 

 

def _lift_to_precision(self, x, prec): 

r""" 

Lift the specified element to the specified precision. 

 

INPUT: 

 

- ``x`` -- the element whose precision has to be lifted 

 

- ``prec`` -- the new precision 

 

NOTE: 

 

The new precision lattice is computed as the intersection 

of the current precision lattice with the subspace 

 

..MATH:: 

 

p^{prec} \Z_p dx \oplus \bigoplus_{y \neq x} \Q_p dy 

 

This function may change at the same time the precision of  

other elements having the same parent. 

 

.. NOTE:: 

 

This function is not meant to be called directly. Use 

``x.lift_to_precision`` instead. 

 

EXAMPLES:: 

 

sage: R = ZpLF(2) 

sage: x = R(1, 10); x 

1 + O(2^10) 

sage: y = R(1, 5); y 

1 + O(2^5) 

sage: u = x^2 + x*y 

sage: v = y^2 + x*y 

sage: w = u + v 

 

sage: prec = R.precision() 

sage: prec._lift_to_precision(w, 11) 

sage: w 

2^2 + O(2^11) 

sage: y 

1 + O(2^9) 

""" 

ref = pAdicLatticeElementWeakProxy(x) 

col = self._matrix[ref] 

n = len(self._elements) 

 

rows_by_val = { } 

for i in range(len(col)): 

v = col[i].valuation() 

if v >= prec: continue 

if rows_by_val.has_key(v): 

rows_by_val[v].append(i) 

else: 

rows_by_val[v] = [i] 

vals = rows_by_val.keys() 

vals.sort() 

vals.append(prec) 

 

for t in range(len(vals)-1): 

v, w = vals[t], vals[t+1] 

rows = rows_by_val[v] 

piv = max(rows) 

for i in rows: 

if i == piv: continue 

# We clear the entry on the i-th row 

scalar = (col[i]/col[piv]).reduce(prec-v) 

for j in range(n): 

col_cur = self._matrix[self._elements[j]] 

if len(col_cur) > piv: 

col_cur[i] -= scalar*col_cur[piv] 

col_cur[i] = col_cur[i].reduce_relative(self._internal_prec) 

# We rescale the piv-th row 

# (if w is Infinity, we delete it) 

for j in range(n): 

col_cur = self._matrix[self._elements[j]] 

if len(col_cur) > piv: 

if w is Infinity: 

del col_cur[piv] 

else: 

col_cur[piv] <<= w - v 

# Now the entry on the piv-th row has valuation w 

# We update the dictionary accordingly 

if w < prec: 

rows_by_val[w].append(piv) 

 

self._precision_absolute.clear_cache() 

 

@cached_method(key=lambda self, x: pAdicLatticeElementWeakProxy(x)) 

def _precision_absolute(self, x): 

r""" 

Return the absolute precision of the given element. 

 

INPUT: 

 

- ``x`` -- the element whose absolute precision is requested 

 

.. NOTE:: 

 

The absolute precision is obtained by projecting the precision 

module onto the line of coordinate ``dx``. 

 

This function is not meant to be called directly. Call 

``x.precision_absolute()`` instead. 

 

EXAMPLES:: 

 

sage: R = ZpLF(2) 

sage: prec = R.precision() 

 

sage: x = R(1, 10); x 

1 + O(2^10) 

sage: y = R(1, 5); y 

1 + O(2^5) 

sage: z = x + y; z 

2 + O(2^5) 

sage: z.precision_absolute() # indirect doctest 

5 

 

In some cases, the absolute precision returned by this function 

may be infinite:: 

 

sage: y = R(1) 

sage: prec._precision_absolute(y) 

+Infinity 

 

However calling the method :meth:`absolute_precision` of the 

element itself reintroduces a cap:: 

 

sage: y.precision_absolute() 

20 

""" 

ref = pAdicLatticeElementWeakProxy(x) 

col = self._matrix[ref] 

if len(col) == 0: 

return Infinity 

else: 

return min( [ c.valuation() for c in col ] ) 

 

def precision_lattice(self, elements=None): 

r""" 

Return a matrix representing the precision lattice on a 

subset of elements. 

 

INPUT: 

 

- ``elements`` -- a list of elements or ``None`` (default: ``None``) 

 

EXAMPLES:: 

 

sage: R = ZpLF(2, label='preclattice') 

sage: prec = R.precision() 

sage: x = R(1, 10); y = R(1, 5) 

sage: prec.precision_lattice() 

[1024 0] 

[ 0 32] 

 

sage: u = x + y 

sage: v = x - y 

sage: prec.precision_lattice([u, v]) 

[ 32 2016] 

[ 0 2048] 

 

If the precision module does not project to a lattice, 

an error is raised. 

 

sage: prec.precision_lattice([x, y, u, v]) 

Traceback (most recent call last): 

... 

PrecisionError: the differential is not surjective 

 

Here is another example with matrices:: 

 

sage: M = matrix(R, 2, 2, [R(3, 5), R(7, 5), R(1, 5), R(11, 1)]) 

sage: N = M^10 

 

The next syntax provides as easy way to select an interesting 

subset of variables (the selected subset consists of the four 

entries of the matrix ``N``):: 

 

sage: prec.precision_lattice(N) 

[ 2048 512 28160 230400] 

[ 0 2048 14336 258048] 

[ 0 0 65536 65536] 

[ 0 0 0 262144] 

""" 

if elements is None: 

elements = self._elements 

else: 

elements = list_of_padics(elements) 

n = len(self._elements) 

rows = [ ]; val = 0 

for ref in elements: 

col = self._matrix[ref] 

row = [ x.value() for x in col ] 

valcol = min([ x.valuation() for x in col ]) 

if valcol < val: val = valcol 

row += (n-len(row)) * [ZZ(0)] 

rows.append(row) 

from sage.matrix.constructor import matrix 

M = matrix(rows).transpose() 

if val < 0: 

M *= self._p ** (-val) 

M = M.change_ring(ZZ) 

M.echelonize() 

n = len(elements) 

if len(M.pivots()) < n: 

raise PrecisionError("the differential is not surjective") 

for i in range(n): 

v = M[i, i].valuation(self._p) 

M[i, i] = self._p ** v 

M.echelonize() 

M = M.submatrix(0, 0, n, n) 

if val < 0: 

M *= self._p ** val 

return M 

 

class pAdicLatticeElementWeakProxy(object): 

r""" 

The implementations of :class:`DifferentialPrecisionGeneric` hold 

weak references to :class:`pAdicLatticeElement`. They are stored in 

dictionaries, e.g., a dictionary that maps an element to the corresponding 

column in the precision lattice matrix. 

However, weak references as implemented by Python are tricky to use as 

dictionary keys. Their equality depends on the equality of the element they 

point to (as long as that element is alive) and then on the equality by 

``id``. This means that statements such as: ``ref in D == ref in D`` could 

be false if the garbage collector kicks in between the two invocations. 

To prevent very subtle and hardly reproducible bugs, we wrap weak 

references in a proxy that gives every lattice element a unique increasing 

id and uses that id for comparisons. 

 

EXAMPLES: 

 

Proxy elements exist only internally and are not usually exposed to the user:: 

 

sage: from sage.rings.padics.lattice_precision import pAdicLatticeElementWeakProxy 

sage: R = ZpLF(2, label='proxy') 

sage: p = R(2) 

sage: prec = R.precision() 

sage: proxy = prec._elements[0] 

sage: isinstance(proxy, pAdicLatticeElementWeakProxy) 

True 

""" 

_next_id = 0 

 

def __init__(self, element, callback=None): 

r""" 

TESTS:: 

 

sage: from sage.rings.padics.lattice_precision import pAdicLatticeElementWeakProxy 

sage: R = ZpLF(2, label='proxy') 

sage: p = R(2) 

sage: pAdicLatticeElementWeakProxy(p) == pAdicLatticeElementWeakProxy(p) 

True 

sage: pAdicLatticeElementWeakProxy(p) is pAdicLatticeElementWeakProxy(p) 

False 

 

""" 

if not hasattr(element, '_proxy_id'): 

element._proxy_id = pAdicLatticeElementWeakProxy._next_id 

pAdicLatticeElementWeakProxy._next_id +=1 

self._id = element._proxy_id 

from weakref import ref 

proxy_callback = callback 

if callback is not None: 

proxy_callback = lambda _: callback(self) 

self._weakref = ref(element, proxy_callback) 

 

def __hash__(self): 

r""" 

Return a hash value for this proxy. 

 

EXAMPLES:: 

 

sage: from sage.rings.padics.lattice_precision import pAdicLatticeElementWeakProxy 

sage: R = ZpLF(2, label='proxy') 

sage: p = R(2) 

sage: hash(pAdicLatticeElementWeakProxy(p)) == hash(pAdicLatticeElementWeakProxy(p)) 

True 

 

""" 

return self._id 

 

def __eq__(self, other): 

r""" 

Return whether this proxy is undistinguishable from ``other``. 

 

EXAMPLES:: 

 

sage: from sage.rings.padics.lattice_precision import pAdicLatticeElementWeakProxy 

sage: R = ZpLF(2, label='proxy') 

sage: p = R(2) 

sage: q = R(2) 

sage: pAdicLatticeElementWeakProxy(p) == pAdicLatticeElementWeakProxy(p) 

True 

sage: pAdicLatticeElementWeakProxy(q) == pAdicLatticeElementWeakProxy(p) 

False 

 

""" 

return isinstance(other, pAdicLatticeElementWeakProxy) and self._id == other._id 

 

def __call__(self): 

r""" 

Return the lattice element this proxy points to, or ``None`` if the 

target has already been finalized. 

 

EXAMPLES:: 

 

sage: from sage.rings.padics.lattice_precision import pAdicLatticeElementWeakProxy 

sage: R = ZpLF(2, label='proxy') 

sage: p = R(2) 

sage: pAdicLatticeElementWeakProxy(p)() 

2 + O(2^21) 

 

""" 

return self._weakref() 

 

def __repr__(self): 

r""" 

Return a printable representation of this proxy. 

 

EXAMPLES:: 

 

sage: from sage.rings.padics.lattice_precision import pAdicLatticeElementWeakProxy 

sage: R = ZpLF(2, label='proxy_repr') 

sage: p = R(2) 

sage: R.precision()._elements # indirect doctest 

[WeakProxy#...] 

 

""" 

return "WeakProxy#%s"%(self._id,) 

 

def list_of_padics(elements): 

r""" 

Convert a list of p-adic composed elements (such as polynomials, matrices) 

to a list of weak references of their p-adic coefficients. 

 

This is a helper function for the method :meth:`precision_lattice`. 

 

TESTS:: 

 

sage: from sage.rings.padics.lattice_precision import list_of_padics 

sage: R = ZpLC(2) 

sage: M = random_matrix(R, 2, 2) 

sage: list_of_padics(M) 

[WeakProxy#..., 

WeakProxy#..., 

WeakProxy#..., 

WeakProxy#...] 

""" 

from sage.rings.padics.padic_lattice_element import pAdicLatticeElement 

if isinstance(elements, pAdicLatticeElement): 

return [ pAdicLatticeElementWeakProxy(elements) ] 

try: 

if elements.parent().is_sparse(): 

elements = elements.coefficients() 

except AttributeError: 

pass 

if not isinstance(elements, list): 

elements = list(elements) 

ans = [ ] 

for x in elements: 

ans += list_of_padics(x) 

return ans