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

""" 

`p`-Adic Base Leaves 

 

Implementations of `\mathbb{Z}_p` and `\mathbb{Q}_p` 

 

AUTHORS: 

 

- David Roe 

- Genya Zaytman: documentation 

- David Harvey: doctests 

- William Stein: doctest updates 

 

EXAMPLES: 

 

`p`-Adic rings and fields are examples of inexact structures, as the 

reals are. That means that elements cannot generally be stored 

exactly: to do so would take an infinite amount of storage. Instead, 

we store an approximation to the elements with varying precision. 

 

There are two types of precision for a `p`-adic element. The first is 

relative precision, which gives the number of known `p`-adic digits:: 

 

sage: R = Qp(5, 20, 'capped-rel', 'series'); a = R(675); a 

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

sage: a.precision_relative() 

20 

 

The second type of precision is absolute precision, which gives the 

power of `p` that this element is stored modulo:: 

 

sage: a.precision_absolute() 

22 

 

The number of times that `p` divides the element is called the 

valuation, and can be accessed with the functions ``valuation()`` and 

``ordp()``: 

 

sage: a.valuation() 

2 

 

The following relationship holds: 

 

``self.valuation() + self.precision_relative() == self.precision_absolute().`` 

 

sage: a.valuation() + a.precision_relative() == a.precision_absolute() 

True 

 

In the capped relative case, the relative precision of an element 

is restricted to be at most a certain value, specified at the 

creation of the field. Individual elements also store their own 

precision, so the effect of various arithmetic operations on 

precision is tracked. When you cast an exact element into a 

capped relative field, it truncates it to the precision cap of the 

field.:: 

 

sage: R = Qp(5, 5); a = R(4006); a 

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

sage: b = R(17/3); b 

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

sage: c = R(4025); c 

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

sage: a + b 

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

sage: a + b + c 

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

 

:: 

 

sage: R = Zp(5, 5, 'capped-rel', 'series'); a = R(4006); a 

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

sage: b = R(17/3); b 

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

sage: c = R(4025); c 

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

sage: a + b 

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

sage: a + b + c 

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

 

In the capped absolute type, instead of having a cap on the 

relative precision of an element there is instead a cap on the 

absolute precision. Elements still store their own precisions, 

and as with the capped relative case, exact elements are truncated 

when cast into the ring.:: 

 

sage: R = ZpCA(5, 5); a = R(4005); a 

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

sage: b = R(4025); b 

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

sage: a * b 

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

sage: (a * b) // 5^3 

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

sage: type((a * b) // 5^3) 

<type 'sage.rings.padics.padic_capped_absolute_element.pAdicCappedAbsoluteElement'> 

sage: (a * b) / 5^3 

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

sage: type((a * b) / 5^3) 

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

 

The fixed modulus type is the leanest of the p-adic rings: it is 

basically just a wrapper around `\mathbb{Z} / p^n \mathbb{Z}` 

providing a unified interface with the rest of the `p`-adics. This is 

the type you should use if your primary interest is in speed (though 

it's not all that much faster than other `p`-adic types). It does not 

track precision of elements.:: 

 

sage: R = ZpFM(5, 5); a = R(4005); a 

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

sage: a // 5 

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

 

`p`-Adic rings and fields should be created using the creation 

functions ``Zp`` and ``Qp`` as above. This will ensure that there is 

only one instance of `\mathbb{Z}_p` and `\mathbb{Q}_p` of a given 

type, `p`, print mode and precision. It also saves typing very long 

class names.:: 

 

sage: Qp(17,10) 

17-adic Field with capped relative precision 10 

sage: R = Qp(7, prec = 20, print_mode = 'val-unit'); S = Qp(7, prec = 20, print_mode = 'val-unit'); R is S 

True 

sage: Qp(2) 

2-adic Field with capped relative precision 20 

 

Once one has a `p`-Adic ring or field, one can cast elements into it 

in the standard way. Integers, ints, longs, Rationals, other `p`-Adic 

types, pari `p`-adics and elements of `\mathbb{Z} / p^n \mathbb{Z}` 

can all be cast into a `p`-Adic field.:: 

 

sage: R = Qp(5, 5, 'capped-rel','series'); a = R(16); a 

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

sage: b = R(23/15); b 

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

sage: S = Zp(5, 5, 'fixed-mod','val-unit'); c = S(Mod(75,125)); c 

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

sage: R(c) 

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

 

In the previous example, since fixed-mod elements don't keep track 

of their precision, we assume that it has the full precision of 

the ring. This is why you have to cast manually here. 

 

While you can cast explicitly as above, the chains of automatic 

coercion are more restricted. As always in Sage, the following 

arrows are transitive and the diagram is commutative.:: 

 

int -> long -> Integer -> Zp capped-rel -> Zp capped_abs -> IntegerMod 

Integer -> Zp fixed-mod -> IntegerMod 

Integer -> Zp capped-abs -> Qp capped-rel 

 

In addition, there are arrows within each type. For capped relative 

and capped absolute rings and fields, these arrows go from lower 

precision cap to higher precision cap. This works since elements 

track their own precision: choosing the parent with higher precision 

cap means that precision is less likely to be truncated unnecessarily. 

For fixed modulus parents, the arrow goes from higher precision cap to 

lower. The fact that elements do not track precision necessitates 

this choice in order to not produce incorrect results. 

 

TESTS:: 

 

sage: R = Qp(5, 15, print_mode='bars', print_sep='&') 

sage: repr(R(2777))[3:] 

'0&0&0&0&0&0&0&0&0&0&4&2&1&0&2' 

sage: TestSuite(R).run() 

 

sage: R = Zp(5, 15, print_mode='bars', print_sep='&') 

sage: repr(R(2777))[3:] 

'0&0&0&0&0&0&0&0&0&0&4&2&1&0&2' 

sage: TestSuite(R).run() 

 

sage: R = ZpCA(5, 15, print_mode='bars', print_sep='&') 

sage: repr(R(2777))[3:] 

'0&0&0&0&0&0&0&0&0&0&4&2&1&0&2' 

sage: TestSuite(R).run() 

 

""" 

from __future__ import absolute_import 

 

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

# Copyright (C) 2008 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/ 

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

from sage.structure.richcmp import op_LE 

 

from .generic_nodes import pAdicFieldBaseGeneric, \ 

pAdicCappedRelativeFieldGeneric, \ 

pAdicRingBaseGeneric, \ 

pAdicCappedRelativeRingGeneric, \ 

pAdicFixedModRingGeneric, \ 

pAdicCappedAbsoluteRingGeneric, \ 

pAdicFloatingPointRingGeneric, \ 

pAdicFloatingPointFieldGeneric, \ 

pAdicGeneric, \ 

pAdicLatticeGeneric 

from .padic_capped_relative_element import pAdicCappedRelativeElement 

from .padic_capped_absolute_element import pAdicCappedAbsoluteElement 

from .padic_fixed_mod_element import pAdicFixedModElement 

from .padic_floating_point_element import pAdicFloatingPointElement 

 

from sage.rings.integer_ring import ZZ 

from sage.rings.rational_field import QQ 

 

 

class pAdicRingCappedRelative(pAdicRingBaseGeneric, pAdicCappedRelativeRingGeneric): 

r""" 

An implementation of the `p`-adic integers with capped relative 

precision. 

""" 

def __init__(self, p, prec, print_mode, names): 

""" 

Initialization. 

 

INPUT: 

 

- ``p`` -- prime 

- ``prec`` -- precision cap 

- ``print_mode`` -- dictionary with print options. 

- ``names`` -- how to print the prime. 

 

EXAMPLES:: 

 

sage: R = ZpCR(next_prime(10^60)) #indirect doctest 

sage: type(R) 

<class 'sage.rings.padics.padic_base_leaves.pAdicRingCappedRelative_with_category'> 

 

TESTS:: 

 

sage: R = ZpCR(2) 

sage: TestSuite(R).run() 

sage: TestSuite(R).run(elements = [R.random_element() for i in range(2^10)], max_runs = 2^12, skip='_test_metric') # long time 

sage: R._test_metric(elements = [R.random_element() for i in range(2^3)]) # long time 

 

sage: R = ZpCR(3, 1) 

sage: TestSuite(R).run(elements = [R.random_element() for i in range(3^3)]) 

 

sage: R = ZpCR(3, 2) 

sage: TestSuite(R).run(elements = [R.random_element() for i in range(3^6)], skip='_test_metric') # long time 

sage: R._test_metric(elements = [R.random_element() for i in range(2^3)]) # long time 

 

sage: R = ZpCR(next_prime(10^60)) 

sage: TestSuite(R).run(elements = [R.random_element() for i in range(2^3)], max_runs = 2^5, skip='_test_log') # long time 

sage: R._test_log(max_runs=2, elements=[R.random_element() for i in range(4)]) # long time 

""" 

pAdicRingBaseGeneric.__init__(self, p, prec, print_mode, names, pAdicCappedRelativeElement) 

 

def _coerce_map_from_(self, R): 

""" 

Return ``True`` if there is a coerce map from ``R`` to ``self``. 

 

EXAMPLES:: 

 

sage: K = Zp(17) 

sage: K(1) + 1 #indirect doctest 

2 + O(17^20) 

sage: K.has_coerce_map_from(ZZ) 

True 

sage: K.has_coerce_map_from(int) 

True 

sage: K.has_coerce_map_from(QQ) 

False 

sage: K.has_coerce_map_from(RR) 

False 

sage: K.has_coerce_map_from(Qp(7)) 

False 

sage: K.has_coerce_map_from(Zp(17,40)) 

False 

sage: K.has_coerce_map_from(Zp(17,10)) 

True 

sage: K.has_coerce_map_from(ZpCA(17,40)) 

False 

""" 

#if isistance(R, pAdicRingLazy) and R.prime() == self.prime(): 

# return True 

if isinstance(R, pAdicRingCappedRelative) and R.prime() == self.prime(): 

if R.precision_cap() < self.precision_cap(): 

return True 

elif (R.precision_cap() == self.precision_cap() and 

self._printer.richcmp_modes(R._printer, op_LE)): 

return True 

 

def _convert_map_from_(self, R): 

""" 

Finds conversion maps from R to this ring. 

 

EXAMPLES:: 

 

sage: Zp(7).convert_map_from(Zmod(343)) 

Lifting morphism: 

From: Ring of integers modulo 343 

To: 7-adic Ring with capped relative precision 20 

""" 

from sage.rings.finite_rings.integer_mod_ring import IntegerModRing_generic 

if isinstance(R, IntegerModRing_generic): 

N = R.cardinality() 

p = self.prime() 

n = N.exact_log(p) 

if N == p**n: 

from sage.rings.padics.padic_generic import ResidueLiftingMap 

return ResidueLiftingMap._create_(R, self) 

 

class pAdicRingCappedAbsolute(pAdicRingBaseGeneric, pAdicCappedAbsoluteRingGeneric): 

r""" 

An implementation of the `p`-adic integers with capped absolute precision. 

""" 

def __init__(self, p, prec, print_mode, names): 

""" 

Initialization. 

 

INPUT: 

 

- ``p`` -- prime 

- ``prec`` -- precision cap 

- ``print_mode`` -- dictionary with print options. 

- ``names`` -- how to print the prime. 

 

EXAMPLES:: 

 

sage: R = ZpCA(next_prime(10^60)) #indirect doctest 

sage: type(R) 

<class 'sage.rings.padics.padic_base_leaves.pAdicRingCappedAbsolute_with_category'> 

 

TESTS:: 

 

sage: R = ZpCA(2) 

sage: TestSuite(R).run() 

sage: TestSuite(R).run(elements = [R.random_element() for i in range(2^10)], max_runs = 2^12, skip='_test_metric') # long time 

sage: R._test_metric(elements = [R.random_element() for i in range(2^3)]) # long time 

 

sage: R = ZpCA(3, 1) 

sage: TestSuite(R).run(elements = [R.random_element() for i in range(3^3)]) 

 

sage: R = ZpCA(3, 2) 

sage: TestSuite(R).run(elements = [R.random_element() for i in range(3^6)], skip='_test_metric') # long time 

sage: R._test_metric(elements = [R.random_element() for i in range(2^3)]) # long time 

 

sage: R = ZpCA(next_prime(10^60)) 

sage: TestSuite(R).run(elements = [R.random_element() for i in range(2^3)], max_runs = 2^5, skip='_test_log') # long time 

sage: R._test_log(max_runs=2, elements=[R.random_element() for i in range(4)]) 

""" 

pAdicRingBaseGeneric.__init__(self, p, prec, print_mode, names, pAdicCappedAbsoluteElement) 

 

def _coerce_map_from_(self, R): 

""" 

Returns ``True`` if there is a coerce map from ``R`` to ``self``. 

 

EXAMPLES:: 

 

sage: K = ZpCA(17) 

sage: K(1) + 1 #indirect doctest 

2 + O(17^20) 

sage: K.has_coerce_map_from(ZZ) 

True 

sage: K.has_coerce_map_from(int) 

True 

sage: K.has_coerce_map_from(QQ) 

False 

sage: K.has_coerce_map_from(RR) 

False 

sage: K.has_coerce_map_from(Qp(7)) 

False 

sage: K.has_coerce_map_from(ZpCA(17,40)) 

False 

sage: K.has_coerce_map_from(ZpCA(17,10)) 

True 

sage: K.has_coerce_map_from(Zp(17,40)) 

True 

""" 

#if isistance(R, pAdicRingLazy) and R.prime() == self.prime(): 

# return True 

if isinstance(R, pAdicRingCappedRelative) and R.prime() == self.prime(): 

return True 

if isinstance(R, pAdicRingCappedAbsolute) and R.prime() == self.prime(): 

if R.precision_cap() < self.precision_cap(): 

return True 

elif (R.precision_cap() == self.precision_cap() and 

self._printer.richcmp_modes(R._printer, op_LE)): 

return True 

 

def _convert_map_from_(self, R): 

""" 

Finds conversion maps from R to this ring. 

 

EXAMPLES:: 

 

sage: ZpCA(7).convert_map_from(Zmod(343)) 

Lifting morphism: 

From: Ring of integers modulo 343 

To: 7-adic Ring with capped absolute precision 20 

""" 

from sage.rings.finite_rings.integer_mod_ring import IntegerModRing_generic 

if isinstance(R, IntegerModRing_generic): 

N = R.cardinality() 

p = self.prime() 

n = N.exact_log(p) 

if N == p**n: 

from sage.rings.padics.padic_generic import ResidueLiftingMap 

return ResidueLiftingMap._create_(R, self) 

 

class pAdicRingFloatingPoint(pAdicRingBaseGeneric, pAdicFloatingPointRingGeneric): 

r""" 

An implementation of the `p`-adic integers with floating point 

precision. 

""" 

def __init__(self, p, prec, print_mode, names): 

""" 

Initialization. 

 

INPUT: 

 

- ``p`` -- prime 

- ``prec`` -- precision cap 

- ``print_mode`` -- dictionary with print options. 

- ``names`` -- how to print the prime. 

 

EXAMPLES:: 

 

sage: R = ZpFP(next_prime(10^60)) #indirect doctest 

sage: type(R) 

<class 'sage.rings.padics.padic_base_leaves.pAdicRingFloatingPoint_with_category'> 

 

TESTS:: 

 

sage: R = ZpFP(2) 

sage: TestSuite(R).run() 

sage: TestSuite(R).run(elements = [R.random_element() for i in range(2^10)], max_runs = 2^12, skip='_test_metric') # long time 

sage: R._test_metric(elements = [R.random_element() for i in range(2^3)]) # long time 

 

sage: R = ZpFP(3, 1) 

sage: TestSuite(R).run(elements = [R.random_element() for i in range(3^3)]) 

 

sage: R = ZpFP(3, 2) 

sage: TestSuite(R).run(elements = [R.random_element() for i in range(3^6)], skip='_test_metric') # long time 

sage: R._test_metric(elements = [R.random_element() for i in range(2^3)]) # long time 

 

sage: R = ZpFP(next_prime(10^60)) 

sage: TestSuite(R).run(elements = [R.random_element() for i in range(2^3)], max_runs = 2^5, skip='_test_log') # long time 

sage: R._test_log(max_runs=2, elements=[R.random_element() for i in range(4)]) 

""" 

pAdicRingBaseGeneric.__init__(self, p, prec, print_mode, names, pAdicFloatingPointElement) 

 

def _coerce_map_from_(self, R): 

""" 

Returns ``True`` if there is a coerce map from ``R`` to ``self``. 

 

EXAMPLES:: 

 

sage: K = ZpFP(17) 

sage: K(1) + 1 #indirect doctest 

2 

sage: K.has_coerce_map_from(ZZ) 

True 

sage: K.has_coerce_map_from(int) 

True 

sage: K.has_coerce_map_from(QQ) 

False 

sage: K.has_coerce_map_from(RR) 

False 

sage: K.has_coerce_map_from(Qp(7)) 

False 

sage: K.has_coerce_map_from(Zp(17,40)) 

False 

sage: K.has_coerce_map_from(Zp(17,10)) 

False 

sage: K.has_coerce_map_from(ZpCA(17,40)) 

False 

""" 

if isinstance(R, pAdicRingFloatingPoint) and R.prime() == self.prime(): 

if R.precision_cap() > self.precision_cap(): 

return True 

elif R.precision_cap() == self.precision_cap() and self._printer.richcmp_modes(R._printer, op_LE): 

return True 

 

def _convert_map_from_(self, R): 

""" 

Finds conversion maps from R to this ring. 

 

EXAMPLES:: 

 

sage: ZpFP(7).convert_map_from(Zmod(343)) 

Lifting morphism: 

From: Ring of integers modulo 343 

To: 7-adic Ring with floating precision 20 

""" 

from sage.rings.finite_rings.integer_mod_ring import IntegerModRing_generic 

if isinstance(R, IntegerModRing_generic): 

N = R.cardinality() 

p = self.prime() 

n = N.exact_log(p) 

if N == p**n: 

from sage.rings.padics.padic_generic import ResidueLiftingMap 

return ResidueLiftingMap._create_(R, self) 

 

class pAdicRingFixedMod(pAdicRingBaseGeneric, pAdicFixedModRingGeneric): 

r""" 

An implementation of the `p`-adic integers using fixed modulus. 

""" 

def __init__(self, p, prec, print_mode, names): 

""" 

Initialization 

 

INPUT: 

 

- ``p`` -- prime 

- ``prec`` -- precision cap 

- ``print_mode`` -- dictionary with print options. 

- ``names`` -- how to print the prime. 

 

EXAMPLES:: 

 

sage: R = ZpFM(next_prime(10^60)) #indirect doctest 

sage: type(R) 

<class 'sage.rings.padics.padic_base_leaves.pAdicRingFixedMod_with_category'> 

 

TESTS:: 

 

sage: R = ZpFM(2) 

sage: TestSuite(R).run() 

sage: TestSuite(R).run(elements = [R.random_element() for i in range(2^10)], max_runs = 2^12, skip='_test_metric') # long time 

sage: R._test_metric(elements = [R.random_element() for i in range(2^3)]) # long time 

 

sage: R = ZpFM(3, 1) 

sage: TestSuite(R).run(elements = [R.random_element() for i in range(3^3)]) 

 

sage: R = ZpFM(3, 2) 

sage: TestSuite(R).run(elements = [R.random_element() for i in range(3^6)], skip='_test_metric') # long time 

sage: R._test_metric(elements = [R.random_element() for i in range(2^3)]) # long time 

 

sage: R = ZpFM(next_prime(10^60)) 

sage: TestSuite(R).run(skip='_test_log') 

sage: TestSuite(R).run(elements = [R.random_element() for i in range(2^4)], max_runs = 2^6, skip='_test_log') # long time 

sage: R._test_log(max_runs=2, elements=[R.random_element() for i in range(4)]) 

 

Fraction fields work after :trac:`23510`:: 

 

sage: R = ZpFM(5) 

sage: K = R.fraction_field(); K 

5-adic Field with floating precision 20 

sage: K(R(90)) 

3*5 + 3*5^2 

""" 

pAdicRingBaseGeneric.__init__(self, p, prec, print_mode, names, pAdicFixedModElement) 

 

def _coerce_map_from_(self, R): 

""" 

Returns ``True`` if there is a coerce map from ``R`` to ``self``. 

 

EXAMPLES:: 

 

sage: K = ZpFM(17) 

sage: K(1) + 1 #indirect doctest 

2 + O(17^20) 

sage: K.has_coerce_map_from(ZZ) 

True 

sage: K.has_coerce_map_from(int) 

True 

sage: K.has_coerce_map_from(QQ) 

False 

sage: K.has_coerce_map_from(RR) 

False 

sage: K.has_coerce_map_from(Zp(7)) 

False 

sage: K.has_coerce_map_from(ZpFM(17,40)) 

True 

sage: K.has_coerce_map_from(ZpFM(17,10)) 

False 

sage: K.has_coerce_map_from(Zp(17,40)) 

False 

""" 

#if isistance(R, pAdicRingLazy) and R.prime() == self.prime(): 

# return True 

if isinstance(R, pAdicRingFixedMod) and R.prime() == self.prime(): 

if R.precision_cap() > self.precision_cap(): 

return True 

elif (R.precision_cap() == self.precision_cap() and 

self._printer.richcmp_modes(R._printer, op_LE)): 

return True 

 

def _convert_map_from_(self, R): 

""" 

Finds conversion maps from R to this ring. 

 

EXAMPLES:: 

 

sage: ZpFM(7).convert_map_from(Zmod(343)) 

Lifting morphism: 

From: Ring of integers modulo 343 

To: 7-adic Ring of fixed modulus 7^20 

""" 

from sage.rings.finite_rings.integer_mod_ring import IntegerModRing_generic 

if isinstance(R, IntegerModRing_generic): 

N = R.cardinality() 

p = self.prime() 

n = N.exact_log(p) 

if N == p**n: 

from sage.rings.padics.padic_generic import ResidueLiftingMap 

return ResidueLiftingMap._create_(R, self) 

 

class pAdicFieldCappedRelative(pAdicFieldBaseGeneric, pAdicCappedRelativeFieldGeneric): 

r""" 

An implementation of `p`-adic fields with capped relative precision. 

 

EXAMPLES:: 

 

sage: K = Qp(17, 1000000) #indirect doctest 

sage: K = Qp(101) #indirect doctest 

 

""" 

 

def __init__(self, p, prec, print_mode, names): 

""" 

Initialization. 

 

INPUT: 

 

- ``p`` -- prime 

- ``prec`` -- precision cap 

- ``print_mode`` -- dictionary with print options. 

- ``names`` -- how to print the prime. 

 

EXAMPLES:: 

 

sage: K = Qp(next_prime(10^60)) # indirect doctest 

sage: type(K) 

<class 'sage.rings.padics.padic_base_leaves.pAdicFieldCappedRelative_with_category'> 

 

TESTS:: 

 

sage: R = Qp(2) 

sage: TestSuite(R).run() 

sage: TestSuite(R).run(elements = [R.random_element() for i in range(2^10)], max_runs = 2^12, skip='_test_metric') # long time 

sage: R._test_metric(elements = [R.random_element() for i in range(2^3)]) # long time 

 

sage: R = Qp(3, 1) 

sage: TestSuite(R).run(elements = [R.random_element() for i in range(3^6)], skip='_test_metric') # long time 

sage: R._test_metric(elements = [R.random_element() for i in range(2^3)]) # long time 

 

sage: R = Qp(3, 2) 

sage: TestSuite(R).run(elements=[R.random_element() for i in range(3^9)], skip="_test_metric") # long time 

sage: R._test_metric(elements=[R.random_element() for i in range(3^3)]) 

 

sage: R = Qp(next_prime(10^60)) 

sage: TestSuite(R).run(skip='_test_log') 

sage: TestSuite(R).run(elements = [R.random_element() for i in range(2^3)], max_runs = 2^5, skip='_test_log') # long time 

sage: R._test_log(max_runs=2, elements=[R.random_element() for i in range(4)]) 

""" 

pAdicFieldBaseGeneric.__init__(self, p, prec, print_mode, names, pAdicCappedRelativeElement) 

 

def _coerce_map_from_(self, R): 

""" 

Returns ``True`` if there is a coerce map from ``R`` to ``self``. 

 

EXAMPLES:: 

 

sage: K = Qp(17) 

sage: K(1) + 1 #indirect doctest 

2 + O(17^20) 

sage: K.has_coerce_map_from(ZZ) 

True 

sage: K.has_coerce_map_from(int) 

True 

sage: K.has_coerce_map_from(QQ) 

True 

sage: K.has_coerce_map_from(RR) 

False 

sage: K.has_coerce_map_from(Qp(7)) 

False 

sage: K.has_coerce_map_from(Qp(17,40)) 

False 

sage: K.has_coerce_map_from(Qp(17,10)) 

True 

sage: K.has_coerce_map_from(Zp(17,40)) 

True 

 

""" 

#if isinstance(R, pAdicRingLazy) or isinstance(R, pAdicFieldLazy) and R.prime() == self.prime(): 

# return True 

if isinstance(R, (pAdicRingCappedRelative, pAdicRingCappedAbsolute)) and R.prime() == self.prime(): 

return True 

if isinstance(R, pAdicFieldCappedRelative) and R.prime() == self.prime(): 

if R.precision_cap() < self.precision_cap(): 

return True 

elif (R.precision_cap() == self.precision_cap() and 

self._printer.richcmp_modes(R._printer, op_LE)): 

return True 

 

def _convert_map_from_(self, R): 

""" 

Finds conversion maps from R to this ring. 

 

EXAMPLES:: 

 

sage: Qp(7).convert_map_from(Zmod(343)) 

Lifting morphism: 

From: Ring of integers modulo 343 

To: 7-adic Field with capped relative precision 20 

""" 

from sage.rings.finite_rings.integer_mod_ring import IntegerModRing_generic 

if isinstance(R, IntegerModRing_generic): 

N = R.cardinality() 

p = self.prime() 

n = N.exact_log(p) 

if N == p**n: 

from sage.rings.padics.padic_generic import ResidueLiftingMap 

return ResidueLiftingMap._create_(R, self) 

 

def random_element(self, algorithm='default'): 

r""" 

Returns a random element of ``self``, optionally using the ``algorithm`` 

argument to decide how it generates the element. Algorithms currently 

implemented: 

 

- default: Choose an integer `k` using the standard 

distribution on the integers. Then choose an integer `a` 

uniformly in the range `0 \le a < p^N` where `N` is the 

precision cap of ``self``. Return ``self(p^k * a, absprec = 

k + self.precision_cap())``. 

 

EXAMPLES:: 

 

sage: Qp(17,6).random_element() 

15*17^-8 + 10*17^-7 + 3*17^-6 + 2*17^-5 + 11*17^-4 + 6*17^-3 + O(17^-2) 

""" 

if (algorithm == 'default'): 

k = ZZ.random_element() 

a = ZZ.random_element(self.prime()**self.precision_cap()) 

return self(self.prime()**k * a, absprec = k + self.precision_cap()) 

else: 

raise NotImplementedError("Don't know %s algorithm"%algorithm) 

 

class pAdicFieldFloatingPoint(pAdicFieldBaseGeneric, pAdicFloatingPointFieldGeneric): 

r""" 

An implementation of the `p`-adic rationals with floating point 

precision. 

""" 

def __init__(self, p, prec, print_mode, names): 

""" 

Initialization. 

 

INPUT: 

 

- ``p`` -- prime 

- ``prec`` -- precision cap 

- ``print_mode`` -- dictionary with print options. 

- ``names`` -- how to print the prime. 

 

EXAMPLES:: 

 

sage: R = QpFP(next_prime(10^60)) #indirect doctest 

sage: type(R) 

<class 'sage.rings.padics.padic_base_leaves.pAdicFieldFloatingPoint_with_category'> 

 

TESTS:: 

 

sage: R = QpFP(2) 

sage: TestSuite(R).run() 

sage: TestSuite(R).run(elements = [R.random_element() for i in range(2^10)], max_runs = 2^12, skip='_test_metric') # long time 

sage: R._test_metric(elements = [R.random_element() for i in range(2^3)]) # long time 

 

sage: R = QpFP(3, 1) 

sage: TestSuite(R).run(elements = [R.random_element() for i in range(3^3)]) 

 

sage: R = QpFP(3, 2) 

sage: TestSuite(R).run(elements = [R.random_element() for i in range(3^6)], skip='_test_metric') # long time 

sage: R._test_metric(elements = [R.random_element() for i in range(2^3)]) # long time 

 

sage: R = QpFP(next_prime(10^60)) 

sage: TestSuite(R).run(skip='_test_log') 

sage: TestSuite(R).run(elements = [R.random_element() for i in range(2^3)], max_runs = 2^5, skip='_test_log') # long time 

sage: R._test_log(max_runs=2, elements=[R.random_element() for i in range(4)]) 

""" 

pAdicFieldBaseGeneric.__init__(self, p, prec, print_mode, names, pAdicFloatingPointElement) 

 

def _coerce_map_from_(self, R): 

""" 

Returns ``True`` if there is a coerce map from ``R`` to ``self``. 

 

EXAMPLES:: 

 

sage: K = QpFP(17) 

sage: K(1) + 1 #indirect doctest 

2 

sage: K.has_coerce_map_from(ZZ) 

True 

sage: K.has_coerce_map_from(int) 

True 

sage: K.has_coerce_map_from(QQ) 

True 

sage: K.has_coerce_map_from(RR) 

False 

sage: K.has_coerce_map_from(Qp(7)) 

False 

sage: K.has_coerce_map_from(Zp(17,40)) 

False 

sage: K.has_coerce_map_from(Qp(17,10)) 

False 

sage: K.has_coerce_map_from(ZpFP(17)) 

True 

sage: K.has_coerce_map_from(ZpCA(17,40)) 

False 

""" 

if isinstance(R, (pAdicRingFixedMod, pAdicRingFloatingPoint, pAdicFieldFloatingPoint)) and R.prime() == self.prime(): 

if R.precision_cap() > self.precision_cap(): 

return True 

elif R.precision_cap() == self.precision_cap() and self._printer.richcmp_modes(R._printer, op_LE): 

return True 

 

def _convert_map_from_(self, R): 

""" 

Finds conversion maps from R to this ring. 

 

EXAMPLES:: 

 

sage: QpFP(7).convert_map_from(Zmod(343)) 

Lifting morphism: 

From: Ring of integers modulo 343 

To: 7-adic Field with floating precision 20 

""" 

from sage.rings.finite_rings.integer_mod_ring import IntegerModRing_generic 

if isinstance(R, IntegerModRing_generic): 

N = R.cardinality() 

p = self.prime() 

n = N.exact_log(p) 

if N == p**n: 

from sage.rings.padics.padic_generic import ResidueLiftingMap 

return ResidueLiftingMap._create_(R, self) 

 

# Lattice precision 

################### 

 

class pAdicRingLattice(pAdicLatticeGeneric, pAdicRingBaseGeneric): 

""" 

An implementation of the `p`-adic integers with lattice precision. 

 

INPUT: 

 

- ``p`` -- prime 

 

- ``prec`` -- precision cap, given as a pair (``relative_cap``, ``absolute_cap``) 

 

- ``subtype`` -- either ``'cap'`` or ``'float'`` 

 

- ``print_mode`` -- dictionary with print options 

 

- ``names`` -- how to print the prime 

 

- ``label`` -- the label of this ring 

 

.. SEEALSO:: 

 

:meth:`label` 

 

EXAMPLES:: 

 

sage: R = ZpLC(next_prime(10^60)) # indirect doctest 

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: type(R) 

<class 'sage.rings.padics.padic_base_leaves.pAdicRingLattice_with_category'> 

 

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

sage: R 

2-adic Ring with lattice-cap precision (label: init) 

""" 

def __init__(self, p, prec, subtype, print_mode, names, label=None): 

""" 

Initialization. 

 

TESTS: 

 

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

sage: TestSuite(R).run(skip='_test_teichmuller') 

""" 

# We need to set the subtype first, so that 

# pAdicRingBaseGeneric.__init__ can work 

self._subtype = subtype 

if isinstance(prec,tuple): 

pAdicRingBaseGeneric.__init__(self, p, prec[1], print_mode, names, None) 

else: 

pAdicRingBaseGeneric.__init__(self, p, prec, print_mode, names, None) 

pAdicLatticeGeneric.__init__(self, p, prec, print_mode, names, label) 

 

def _coerce_map_from_(self, R): 

""" 

Return ``True`` if there is a coerce map from ``R`` to this ring. 

 

EXAMPLES:: 

 

sage: R = ZpLC(2) 

sage: R.has_coerce_map_from(ZZ) 

True 

sage: R.has_coerce_map_from(QQ) 

False 

 

sage: K = R.fraction_field() 

sage: K.has_coerce_map_from(R) 

True 

sage: K.has_coerce_map_from(QQ) 

True 

 

Note that coerce map does not exist between ``p``-adic rings with 

lattice precision and other ``p``-adic rings. 

 

sage: S = Zp(2) 

sage: R.has_coerce_map_from(S) 

False 

sage: S.has_coerce_map_from(R) 

False 

 

Similarly there is no coercion maps between ``p``-adic rings with 

different labels. 

 

sage: R2 = ZpLC(2, label='coerce') 

sage: R.has_coerce_map_from(R2) 

False 

sage: R2.has_coerce_map_from(R) 

False 

""" 

if isinstance(R, pAdicRingLattice) and R.precision() is self.precision(): 

return True 

 

def random_element(self, prec=None): 

""" 

Return a random element of this ring. 

 

INPUT: 

 

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

absolute precision of the generated random element 

 

EXAMPLES:: 

 

sage: R = ZpLC(2) 

sage: R.random_element() # random 

2^3 + 2^4 + 2^5 + 2^6 + 2^7 + 2^10 + 2^11 + 2^14 + 2^15 + 2^16 + 2^17 + 2^18 + 2^19 + 2^21 + O(2^23) 

 

sage: R.random_element(prec=10) # random 

1 + 2^3 + 2^4 + 2^7 + O(2^10) 

""" 

p = self.prime() 

if self._subtype == 'cap': 

if prec is None: 

prec = self._prec_cap_absolute 

x = ZZ.random_element(p**prec) 

relcap = x.valuation(p) + self._prec_cap_relative 

if relcap < prec: 

prec = relcap 

return self._element_class(self, x, prec=prec) 

else: 

if prec is None: 

cap = self._prec_cap_relative 

else: 

cap = prec 

x = ZZ.random_element(p**cap) 

v = x.valuation(p) 

if prec is None and v > 0: 

x += p**cap * ZZ.random_element(p**v) 

return self._element_class(self, x, prec=prec) 

 

class pAdicFieldLattice(pAdicLatticeGeneric, pAdicFieldBaseGeneric): 

""" 

An implementation of the `p`-adic numbers with lattice precision. 

 

INPUT: 

 

- ``p`` -- prime 

 

- ``prec`` -- precision cap, given as a pair (``relative_cap``, ``absolute_cap``) 

 

- ``subtype`` -- either ``'cap'`` or ``'float'`` 

 

- ``print_mode`` -- dictionary with print options 

 

- ``names`` -- how to print the prime 

 

- ``label`` -- the label of this ring 

 

.. SEEALSO:: 

 

:meth:`label` 

 

EXAMPLES:: 

 

sage: R = QpLC(next_prime(10^60)) # indirect doctest 

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: type(R) 

<class 'sage.rings.padics.padic_base_leaves.pAdicFieldLattice_with_category'> 

 

sage: R = QpLC(2,label='init') # indirect doctest 

sage: R 

2-adic Field with lattice-cap precision (label: init) 

""" 

def __init__(self, p, prec, subtype, print_mode, names, label=None): 

""" 

Initialization. 

 

TESTS:: 

 

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

sage: TestSuite(R).run(skip='_test_teichmuller') 

""" 

# We need to set the subtype first, so that 

# pAdicFieldBaseGeneric.__init__ can work 

self._subtype = subtype 

if isinstance(prec,tuple): 

pAdicFieldBaseGeneric.__init__(self, p, prec[1], print_mode, names, None) 

else: 

pAdicFieldBaseGeneric.__init__(self, p, prec, print_mode, names, None) 

pAdicLatticeGeneric.__init__(self, p, prec, print_mode, names, label) 

 

def _coerce_map_from_(self, R): 

""" 

Return ``True`` if there is a coerce map from ``R`` to this ring. 

 

EXAMPLES:: 

 

sage: R = ZpLC(2) 

sage: R.has_coerce_map_from(ZZ) 

True 

sage: R.has_coerce_map_from(QQ) 

False 

 

sage: K = R.fraction_field() 

sage: K.has_coerce_map_from(R) 

True 

sage: K.has_coerce_map_from(QQ) 

True 

 

Note that coerce map does not exist between ``p``-adic fields with 

lattice precision and other ``p``-adic rings. 

 

sage: L = Qp(2) 

sage: K.has_coerce_map_from(L) 

False 

sage: L.has_coerce_map_from(K) 

False 

 

Similarly there is no coercion maps between ``p``-adic rings with 

different labels. 

 

sage: K2 = QpLC(2, label='coerce') 

sage: K.has_coerce_map_from(K2) 

False 

sage: K2.has_coerce_map_from(K) 

False 

""" 

if isinstance(R, (pAdicRingLattice, pAdicFieldLattice)) and R.precision() is self.precision(): 

return True 

 

def random_element(self, prec=None, integral=False): 

""" 

Return a random element of this ring. 

 

INPUT: 

 

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

absolute precision of the generated random element 

 

- ``integral`` -- a boolean (default: ``False``); if true 

return an element in the ring of integers 

 

EXAMPLES:: 

 

sage: K = QpLC(2) 

sage: K.random_element() # random 

2^-8 + 2^-7 + 2^-6 + 2^-5 + 2^-3 + 1 + 2^2 + 2^3 + 2^5 + O(2^12) 

sage: K.random_element(integral=True) # random 

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

 

sage: K.random_element(prec=10) # random 

2^(-3) + 1 + 2 + 2^4 + 2^8 + O(2^10) 

 

If the given precision is higher than the internal cap of the 

parent, then the cap is used:: 

 

sage: K.precision_cap_relative() 

20 

sage: K.random_element(prec=100) # random 

2^5 + 2^8 + 2^11 + 2^12 + 2^14 + 2^18 + 2^20 + 2^24 + O(2^25) 

""" 

if integral: 

val = 0 

else: 

val = ZZ.random_element() 

if prec is None: 

prec = self._prec_cap_absolute - val 

p = self.prime() 

x = ZZ.random_element(p**prec) 

relcap = x.valuation(p) + self._prec_cap_relative 

if relcap < prec: 

prec = relcap 

return self._element_class(self, x*(p**val), prec=prec)