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

""" 

`p`-Adic Capped Relative Elements 

  

Elements of `p`-Adic Rings with Capped Relative Precision 

  

AUTHORS: 

  

- David Roe: initial version, rewriting to use templates (2012-3-1) 

- Genya Zaytman: documentation 

- David Harvey: doctests 

""" 

  

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

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

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

  

include "sage/libs/linkages/padics/mpz.pxi" 

include "CR_template.pxi" 

  

from sage.libs.pari import pari 

from sage.libs.pari.convert_gmp cimport new_gen_from_padic 

from sage.rings.finite_rings.integer_mod import Mod 

from sage.rings.padics.pow_computer cimport PowComputer_class 

  

cdef extern from "sage/rings/padics/transcendantal.c": 

cdef void padiclog(mpz_t ans, const mpz_t a, unsigned long p, unsigned long prec, const mpz_t modulo) 

cdef void padicexp(mpz_t ans, const mpz_t a, unsigned long p, unsigned long prec, const mpz_t modulo)  

cdef void padicexp_Newton(mpz_t ans, const mpz_t a, unsigned long p, unsigned long prec, unsigned long precinit, const mpz_t modulo) 

  

  

cdef class PowComputer_(PowComputer_base): 

""" 

A PowComputer for a capped-relative padic ring or field. 

""" 

def __init__(self, Integer prime, long cache_limit, long prec_cap, long ram_prec_cap, bint in_field): 

""" 

Initialization. 

  

EXAMPLES:: 

  

sage: R = ZpCR(5) 

sage: type(R.prime_pow) 

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

sage: R.prime_pow._prec_type 

'capped-rel' 

""" 

self._prec_type = 'capped-rel' 

PowComputer_base.__init__(self, prime, cache_limit, prec_cap, ram_prec_cap, in_field) 

  

cdef class pAdicCappedRelativeElement(CRElement): 

""" 

Constructs new element with given parent and value. 

  

INPUT: 

  

- ``x`` -- value to coerce into a capped relative ring or field 

  

- ``absprec`` -- maximum number of digits of absolute precision 

  

- ``relprec`` -- maximum number of digits of relative precision 

  

EXAMPLES:: 

  

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

  

Construct from integers:: 

  

sage: R(3) 

3 + O(5^10) 

sage: R(75) 

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

sage: R(0) 

0 

sage: R(-1) 

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

sage: R(-5) 

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

sage: R(-7*25) 

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

  

Construct from rationals:: 

  

sage: R(1/2) 

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

sage: R(-7875/874) 

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

sage: R(15/425) 

Traceback (most recent call last): 

... 

ValueError: p divides the denominator 

  

Construct from IntegerMod:: 

  

sage: R(Integers(125)(3)) 

3 + O(5^3) 

sage: R(Integers(5)(3)) 

3 + O(5) 

sage: R(Integers(5^30)(3)) 

3 + O(5^10) 

sage: R(Integers(5^30)(1+5^23)) 

1 + O(5^10) 

sage: R(Integers(49)(3)) 

Traceback (most recent call last): 

... 

TypeError: p does not divide modulus 49 

  

:: 

  

sage: R(Integers(48)(3)) 

Traceback (most recent call last): 

... 

TypeError: p does not divide modulus 48 

  

Some other conversions:: 

  

sage: R(R(5)) 

5 + O(5^11) 

  

Construct from Pari objects:: 

  

sage: R = Zp(5) 

sage: x = pari(123123) ; R(x) 

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

sage: R(pari(R(5252))) 

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

sage: R = Zp(5,prec=5) 

sage: R(pari(-1)) 

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

sage: pari(R(-1)) 

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

sage: pari(R(0)) 

0 

sage: R(pari(R(0,5))) 

O(5^5) 

  

# todo: doctests for converting from other types of p-adic rings 

  

""" 

def lift(self): 

""" 

Return an integer or rational congruent to ``self`` modulo ``self``'s 

precision. If a rational is returned, its denominator will equal 

``p^ordp(self)``. 

  

EXAMPLES:: 

  

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

8 

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

8 

sage: R = Qp(7,4); a = R(8/7); a.lift() 

8/7 

""" 

return self.lift_c() 

  

cdef lift_c(self): 

""" 

Implementation of lift. 

  

TESTS:: 

  

sage: O(5^5).lift() #indirect doctest 

0 

sage: R = Qp(5); R(0).lift() 

0 

sage: R(5/9).lift() 

264909532335070 

sage: R(9/5).lift() 

9/5 

""" 

cdef Integer ans 

cdef Rational ansr 

if self.ordp >= 0: 

ans = PY_NEW(Integer) 

if self.relprec == 0: 

mpz_set_ui(ans.value, 0) 

else: 

mpz_set(ans.value, self.unit) 

mpz_mul(ans.value, ans.value, self.prime_pow.pow_mpz_t_tmp(self.ordp)) 

return ans 

else: 

ansr = Rational.__new__(Rational) 

if self.relprec == 0: 

mpq_set_si(ansr.value, 0, 1) 

return self 

else: 

mpz_set(mpq_numref(ansr.value), self.unit) 

mpz_set(mpq_denref(ansr.value), self.prime_pow.pow_mpz_t_tmp(-self.ordp)) 

return ansr 

  

def __pari__(self): 

""" 

Converts this element to an equivalent pari element. 

  

EXAMPLES:: 

  

sage: R = Zp(17, 10); a = ~R(14); pari(a) #indirect doctest 

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

sage: pari(R(0)) 

0 

sage: pari(R(0,5)) 

O(17^5) 

""" 

return self._to_gen() 

  

cdef pari_gen _to_gen(self): 

""" 

Converts this element to an equivalent pari element. 

  

EXAMPLES:: 

  

sage: R = Zp(5, 10); a = R(17); pari(a) #indirect doctest 

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

sage: pari(R(0)) 

0 

sage: pari(R(0,5)) 

O(5^5) 

sage: pari(R(0,5)).debug() 

[&=...] PADIC(lg=5):... (precp=0,valp=5):... ... ... ... 

p : [&=...] INT(lg=3):... (+,lgefint=3):... ...  

p^l : [&=...] INT(lg=3):... (+,lgefint=3):... ...  

I : gen_0 

""" 

if exactzero(self.ordp): 

return pari.zero() 

else: 

return new_gen_from_padic(self.ordp, self.relprec, 

self.prime_pow.prime.value, 

self.prime_pow.pow_mpz_t_tmp(self.relprec), 

self.unit) 

def _integer_(self, Z=None): 

""" 

Returns an integer congruent to this element modulo 

``p^self.absolute_precision()``. 

  

EXAMPLES:: 

  

sage: R = Zp(5); a = R(-1); a._integer_() 

95367431640624 

""" 

if self.ordp < 0: 

raise ValueError("Cannot form an integer out of a p-adic field element with negative valuation") 

return self.lift_c() 

  

def residue(self, absprec=1, field=None, check_prec=True): 

""" 

Reduces this element modulo `p^{\mathrm{absprec}}`. 

  

INPUT: 

  

- ``absprec`` -- a non-negative integer (default: ``1``) 

  

- ``field`` -- boolean (default ``None``). Whether to return an element of GF(p) or Zmod(p). 

  

- ``check_prec`` -- boolean (default ``True``). Whether to raise an error if this 

element has insufficient precision to determine the reduction. 

  

OUTPUT: 

  

This element reduced modulo `p^\mathrm{absprec}` as an element of 

`\ZZ/p^\mathrm{absprec}\ZZ` 

  

EXAMPLES:: 

  

sage: R = Zp(7,4) 

sage: a = R(8) 

sage: a.residue(1) 

1 

  

This is different from applying ``% p^n`` which returns an element in 

the same ring:: 

  

sage: b = a.residue(2); b 

8 

sage: b.parent() 

Ring of integers modulo 49 

sage: c = a % 7^2; c 

1 + 7 + O(7^4) 

sage: c.parent() 

7-adic Ring with capped relative precision 4 

  

For elements in a field, application of ``% p^n`` always returns 

zero, the remainder of the division by ``p^n``:: 

  

sage: K = Qp(7,4) 

sage: a = K(8) 

sage: a.residue(2) 

8 

sage: a % 7^2 

0 

  

sage: b = K(1/7) 

sage: b.residue() 

Traceback (most recent call last): 

... 

ValueError: element must have non-negative valuation in order to compute residue. 

  

TESTS:: 

  

sage: R = Zp(7,4) 

sage: a = R(8) 

sage: a.residue(0) 

0 

sage: a.residue(-1) 

Traceback (most recent call last): 

... 

ValueError: cannot reduce modulo a negative power of p. 

sage: a.residue(5) 

Traceback (most recent call last): 

... 

PrecisionError: not enough precision known in order to compute residue. 

sage: a.residue(5, check_prec=False) 

8 

  

sage: a.residue(field=True).parent() 

Finite Field of size 7 

  

.. SEEALSO:: 

  

:meth:`_mod_` 

  

""" 

cdef Integer selfvalue, modulus 

cdef long aprec 

if not isinstance(absprec, Integer): 

absprec = Integer(absprec) 

if check_prec and absprec > self.precision_absolute(): 

raise PrecisionError("not enough precision known in order to compute residue.") 

elif absprec < 0: 

raise ValueError("cannot reduce modulo a negative power of p.") 

if self.ordp < 0: 

raise ValueError("element must have non-negative valuation in order to compute residue.") 

if field is None: 

field = (absprec == 1) 

elif field and absprec != 1: 

raise ValueError("field keyword may only be set at precision 1") 

modulus = PY_NEW(Integer) 

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

mpz_set(modulus.value, self.prime_pow.pow_mpz_t_tmp(aprec)) 

selfvalue = PY_NEW(Integer) 

if self.relprec == 0: 

mpz_set_ui(selfvalue.value, 0) 

else: 

# Need to do this better. 

mpz_mul(selfvalue.value, self.prime_pow.pow_mpz_t_tmp(self.ordp), self.unit) 

if field: 

from sage.rings.finite_rings.all import GF 

return GF(self.parent().prime())(selfvalue) 

else: 

return Mod(selfvalue, modulus) 

  

def _log_binary_splitting(self, aprec, mina=0): 

r""" 

Return ``\log(self)`` for ``self`` equal to 1 in the residue field 

  

This is a helper method for :meth:`log`. 

It uses a fast binary splitting algorithm. 

  

INPUT: 

  

- ``aprec`` -- an integer, the precision to which the result is 

correct. ``aprec`` must not exceed the precision cap of the ring over 

which this element is defined. 

- ``mina`` -- an integer (default: 0), the series will check `n` up to 

this valuation (and beyond) to see if they can contribute to the 

series. 

  

NOTE:: 

  

The function does not check that its argument ``self`` is 

1 in the residue field. If this assumption is not fullfiled 

the behaviour of the function is not specified. 

  

ALGORITHM: 

  

1. Raise `u` to the power `p^v` for a suitable `v` in order 

to make it closer to 1. (`v` is chosen such that `p^v` is 

close to the precision.) 

  

2. Write 

  

.. MATH:: 

  

u^{p-1} = \prod_{i=1}^\infty (1 - a_i p^{(v+1)*2^i}) 

  

with `0 \leq a_i < p^{(v+1)*2^i}` and compute 

`\log(1 - a_i p^{(v+1)*2^i})` using the standard Taylor expansion 

  

.. MATH:: 

  

\log(1 - x) = -x - 1/2 x^2 - 1/3 x^3 - 1/4 x^4 - 1/5 x^5 - \cdots 

  

together with a binary splitting method. 

  

3. Divide the result by `p^v` 

  

The complexity of this algorithm is quasi-linear. 

  

EXAMPLES:: 

  

sage: r = Qp(5,prec=4)(6) 

sage: r._log_binary_splitting(2) 

5 + O(5^2) 

sage: r._log_binary_splitting(4) 

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

sage: r._log_binary_splitting(100) 

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

  

sage: r = Zp(5,prec=4,type='fixed-mod')(6) 

sage: r._log_binary_splitting(5) 

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

  

""" 

cdef unsigned long p 

cdef unsigned long prec = min(aprec, self.relprec) 

cdef pAdicCappedRelativeElement ans 

  

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

raise NotImplementedError("The prime %s does not fit in a long" % self.prime_pow.prime) 

p = self.prime_pow.prime  

  

ans = self._new_c() 

ans.ordp = 0 

ans.relprec = prec 

sig_on() 

padiclog(ans.unit, self.unit, p, prec, self.prime_pow.pow_mpz_t_tmp(prec)) 

sig_off() 

ans._normalize() 

  

return ans 

  

def _exp_binary_splitting(self, aprec): 

""" 

Compute the exponential power series of this element 

  

This is a helper method for :meth:`exp`. 

  

INPUT: 

  

- ``aprec`` -- an integer, the precision to which to compute the 

exponential 

  

NOTE:: 

  

The function does not check that its argument ``self`` is 

the disk of convergence of ``exp``. If this assumption is not 

fullfiled the behaviour of the function is not specified. 

  

ALGORITHM: 

  

Write 

  

.. MATH:: 

  

self = \sum_{i=1}^\infty a_i p^{2^i} 

  

with `0 \leq a_i < p^{2^i}` and compute 

`\exp(a_i p^{2^i})` using the standard Taylor expansion 

  

.. MATH:: 

  

\exp(x) = 1 + x + x^2/2 + x^3/6 + x^4/24 + \cdots 

  

together with a binary splitting method. 

  

The binary complexity of this algorithm is quasi-linear. 

  

EXAMPLES:: 

  

sage: R = Zp(7,5) 

sage: x = R(7) 

sage: x.exp(algorithm="binary_splitting") # indirect doctest 

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

  

""" 

cdef unsigned long p 

cdef unsigned long prec = aprec 

cdef pAdicCappedRelativeElement ans 

cdef Integer selfint = self.lift_c() 

  

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

raise NotImplementedError("The prime %s does not fit in a long" % self.prime_pow.prime) 

p = self.prime_pow.prime  

  

ans = self._new_c() 

ans.ordp = 0 

ans.relprec = prec 

sig_on() 

padicexp(ans.unit, selfint.value, p, prec, self.prime_pow.pow_mpz_t_tmp(prec)) 

sig_off() 

  

return ans 

  

def _exp_newton(self, aprec, log_algorithm=None): 

""" 

Compute the exponential power series of this element 

  

This is a helper method for :meth:`exp`. 

  

INPUT: 

  

- ``aprec`` -- an integer, the precision to which to compute the 

exponential 

  

- ``log_algorithm`` (default: None) -- the algorithm used for 

computing the logarithm. This attribute is passed to the log 

method. See :meth:`log` for more details about the possible 

algorithms. 

  

NOTE:: 

  

The function does not check that its argument ``self`` is 

the disk of convergence of ``exp``. If this assumption is not 

fullfiled the behaviour of the function is not specified. 

  

ALGORITHM: 

  

Solve the equation `\log(x) = self` using the Newton scheme:: 

  

.. MATH:: 

  

x_{i+1} = x_i \cdot (1 + self - \log(x_i)) 

  

The binary complexity of this algorithm is roughly the same 

than that of the computation of the logarithm. 

  

EXAMPLES:: 

  

sage: R.<w> = Zq(7^2,5) 

sage: x = R(7*w) 

sage: x.exp(algorithm="newton") # indirect doctest 

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

""" 

cdef unsigned long p 

cdef unsigned long prec = aprec 

cdef pAdicCappedRelativeElement ans 

cdef Integer selfint = self.lift_c() 

  

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

raise NotImplementedError("The prime %s does not fit in a long" % self.prime_pow.prime) 

p = self.prime_pow.prime  

  

ans = self._new_c() 

ans.ordp = 0 

ans.relprec = prec 

mpz_set_ui(ans.unit, 1) 

sig_on() 

if p == 2: 

padicexp_Newton(ans.unit, selfint.value, p, prec, 2, self.prime_pow.pow_mpz_t_tmp(prec)) 

else: 

padicexp_Newton(ans.unit, selfint.value, p, prec, 1, self.prime_pow.pow_mpz_t_tmp(prec)) 

sig_off() 

  

return ans 

  

def unpickle_pcre_v1(R, unit, ordp, relprec): 

""" 

Unpickles a capped relative element. 

  

EXAMPLES:: 

  

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

sage: R = Zp(5) 

sage: a = unpickle_pcre_v1(R, 17, 2, 5); a 

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

""" 

return unpickle_cre_v2(pAdicCappedRelativeElement, R, unit, ordp, relprec) 

  

def base_p_list(Integer n, bint pos, PowComputer_class prime_pow): 

""" 

Returns a base-`p` list of digits of ``n``. 

  

INPUT: 

  

- ``n`` -- a positive Integer. 

  

- ``pos`` -- a boolean. If True, then returns the standard base `p` expansion. 

Otherwise, the digits lie in the range `-p/2` to `p/2`. 

  

- ``prime_pow`` -- A PowComputer giving the prime. 

  

EXAMPLES:: 

  

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

sage: base_p_list(192837, True, Zp(5).prime_pow) 

[2, 2, 3, 2, 3, 1, 2, 2] 

sage: 2 + 2*5 + 3*5^2 + 2*5^3 + 3*5^4 + 5^5 + 2*5^6 + 2*5^7 

192837 

sage: base_p_list(192837, False, Zp(5).prime_pow) 

[2, 2, -2, -2, -1, 2, 2, 2] 

sage: 2 + 2*5 - 2*5^2 - 2*5^3 - 5^4 + 2*5^5 + 2*5^6 + 2*5^7 

192837 

""" 

if mpz_sgn(n.value) < 0: 

raise ValueError("n must be nonnegative") 

cdef expansion_mode mode = simple_mode if pos else smallest_mode 

# We need a p-adic element to feed to ExpansionIter before resetting its curvalue 

from sage.rings.padics.all import Zp 

p = prime_pow.prime 

dummy = Zp(p)(0) 

cdef ExpansionIter expansion = ExpansionIter(dummy, n.exact_log(p) + 2, mode) 

mpz_set(expansion.curvalue, n.value) 

return trim_zeros(list(expansion))