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

""" 

`p`-Adic ``ZZ_pX Element`` 

  

A common superclass implementing features shared by all elements that 

use NTL's ``ZZ_pX`` as the fundamental data type. 

  

AUTHORS: 

  

- David Roe 

""" 

  

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

# 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 __future__ import print_function 

  

from sage.ext.stdsage cimport PY_NEW 

from cpython.list cimport * 

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

  

from sage.libs.gmp.mpz cimport * 

from sage.libs.ntl.ntl_ZZ cimport ntl_ZZ 

from sage.libs.ntl.ntl_ZZ_p cimport ntl_ZZ_p 

from sage.libs.ntl.ntl_ZZ_pX cimport ntl_ZZ_pX 

from sage.libs.ntl.ntl_ZZX cimport ntl_ZZX 

from sage.libs.ntl.ntl_ZZ_pContext cimport ntl_ZZ_pContext_class 

from sage.libs.ntl.ntl_ZZ_pContext import ntl_ZZ_pContext 

from sage.rings.integer cimport Integer 

from sage.rings.rational cimport Rational 

from sage.rings.padics.padic_generic_element cimport pAdicGenericElement 

from sage.rings.finite_rings.integer_mod import is_IntegerMod 

from sage.rings.padics.padic_printing cimport pAdicPrinter_class 

from sage.rings.padics.pow_computer_ext cimport PowComputer_ext 

from sage.rings.rational_field import QQ 

  

zero = Integer(0) 

one = Integer(1) 

two = Integer(2) 

big = two**128 + one 

#this should not fit in a long, since it's supposed to be bigger than any valid absolute precision. 

  

cdef class pAdicZZpXElement(pAdicExtElement): 

def __init__(self, parent): 

""" 

Initialization 

  

EXAMPLES:: 

  

sage: A = Zp(next_prime(50000),10) 

sage: S.<x> = A[] 

sage: B.<t> = A.ext(x^2+next_prime(50000)) #indirect doctest 

""" 

self.prime_pow = <PowComputer_ZZ_pX>parent.prime_pow 

pAdicExtElement.__init__(self, parent) 

  

cdef int _set_from_list(self, L) except -1: 

""" 

Sets ``self`` from a list. 

  

The list can contain integers, ``IntegerMods``, rationals, or 

`p`-adic base elements 

  

INPUT: 

  

- `L` -- a list. 

  

EXAMPLES:: 

  

sage: R = ZpFM(5,5) 

sage: S.<x> = ZZ[] 

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

sage: W([1,2,3,4]) #indirect doctest 

1 + 2*w + 3*w^2 + 4*w^3 + O(w^25) 

sage: W([5,10,15,20]) 

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

""" 

cdef ntl_ZZ_pContext_class ctx 

L, min_val, ctx = preprocess_list(self, L) 

if ctx is None: 

self._set_from_ZZX((<ntl_ZZX>ntl_ZZX(L)).x) 

else: 

self._set_from_ZZ_pX(&(<ntl_ZZ_pX>ntl_ZZ_pX(L, ctx)).x, ctx) 

self._pshift_self(mpz_get_si((<Integer>min_val).value)) 

  

cdef int _set_from_list_rel(self, L, long relprec) except -1: 

""" 

Sets ``self`` from a list. 

  

The list can contain integers, ``IntegerMods``, rationals, or 

`p`-adic base elements 

  

INPUT: 

  

- ``L`` -- a list. 

  

- ``relprec`` -- an integer, capping the relative precision of 

``self``. 

  

EXAMPLES:: 

  

sage: R = Zp(5,5) 

sage: S.<x> = ZZ[] 

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

sage: W([1,2,3,4]) #indirect doctest 

1 + 2*w + 3*w^2 + 4*w^3 + O(w^25) 

sage: W([5,10,15,20], relprec=16) 

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

""" 

cdef ntl_ZZ_pContext_class ctx 

L, min_val, ctx = preprocess_list(self, L) 

if ctx is None: 

self._set_from_ZZX_rel((<ntl_ZZX>ntl_ZZX(L)).x, relprec) 

else: 

self._set_from_ZZ_pX_rel(&(<ntl_ZZ_pX>ntl_ZZ_pX(L, ctx)).x, ctx, relprec) 

self._pshift_self(mpz_get_si((<Integer>min_val).value)) 

  

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

""" 

Sets ``self`` from a list. 

  

The list can contain integers, ``IntegerMods``, rationals, or 

`p`-adic base elements 

  

INPUT: 

  

- ``L`` -- a list. 

  

- ``relprec`` -- an integer, capping the relative precision of 

``self``. 

  

EXAMPLES:: 

  

sage: R = ZpCA(5,5) 

sage: S.<x> = ZZ[] 

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

sage: W([1,2,3,4]) 

1 + 2*w + 3*w^2 + 4*w^3 + O(w^25) 

sage: W([5,10,15,20], absprec=16) #indirect doctest 

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

""" 

cdef ntl_ZZ_pContext_class ctx 

L, min_val, ctx = preprocess_list(self, L) 

if ctx is None: 

self._set_from_ZZX_abs((<ntl_ZZX>ntl_ZZX(L)).x, absprec) 

else: 

self._set_from_ZZ_pX_abs(&(<ntl_ZZ_pX>ntl_ZZ_pX(L, ctx)).x, ctx, absprec) 

self._pshift_self(mpz_get_si((<Integer>min_val).value)) 

  

cdef int _set_from_list_both(self, L, long absprec, long relprec) except -1: 

""" 

Sets ``self`` from a list. 

  

The list can contain integers, ``IntegerMods``, rationals, or 

`p`-adic base elements 

  

INPUT: 

  

- ``L`` -- a list. 

  

- ``absprec`` -- an integer, capping the absolute precision of 

``self``. 

  

- ``relprec`` -- an integer, capping the relative precision of 

``self``. 

  

EXAMPLES:: 

  

sage: R = Zp(5,5) 

sage: S.<x> = ZZ[] 

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

sage: W([1,2,3,4]) 

1 + 2*w + 3*w^2 + 4*w^3 + O(w^25) 

sage: W([5,10,15,20], absprec=16) #indirect doctest 

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

""" 

cdef ntl_ZZ_pContext_class ctx 

L, min_val, ctx = preprocess_list(self, L) 

if ctx is None: 

self._set_from_ZZX_both((<ntl_ZZX>ntl_ZZX(L)).x, absprec, relprec) 

else: 

self._set_from_ZZ_pX_both(&(<ntl_ZZ_pX>ntl_ZZ_pX(L, ctx)).x, ctx, absprec, relprec) 

self._pshift_self(mpz_get_si((<Integer>min_val).value)) 

  

cdef long _check_ZZ_pContext(self, ntl_ZZ_pContext_class ctx) except -1: 

""" 

Checks that the given ``ntl_ZZ_pContext`` is actually a power 

of the relevant prime. If so, returns the exponent. 

  

INPUT: 

  

- ``ctx`` -- An ``ntl_ZZ_pContext_class`` 

  

OUTPUT: 

  

- ``val`` -- If ``ctx`` is a context for `p^n`, returns `n`. 

Otherwise, raises an error. 

  

EXAMPLES:: 

  

sage: R = Zp(5,5) 

sage: S.<x> = R[] 

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

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

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

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

""" 

cdef ZZ_c leftover 

cdef long val = ZZ_remove(leftover, ctx.p.x, self.prime_pow.pow_ZZ_tmp(1)[0]) 

if ZZ_IsOne(leftover): 

return val 

else: 

raise ValueError("context must be a power of the appropriate prime") 

  

cdef ext_p_list_precs(self, bint pos, long prec): 

""" 

Returns a list giving a series representation of ``self``. 

  

- The returned list will consist of: 

  

+ integers (in the Eisenstein case) or 

  

+ lists of integers (in the unramified case). 

  

- ``self`` can be reconstructed as 

  

+ a sum of elements of the list times powers of the 

uniformiser (in the Eisenstein case), or 

  

+ as a sum of powers of the `p` times polynomials in the 

generator (in the unramified case). 

  

Note that zeros are truncated from the returned list, so you 

must use the valuation() function to completely recover self. 

  

INPUT: 

  

- ``pos`` -- ``bint``. If ``True``, all integers will be in 

the range `[0,p-1]`, otherwise they will be in the range 

`[(1-p)/2, p/2]`. 

  

- ``prec`` -- How many terms to return in the list. This is 

important since shifting in the Eisenstein case can 

introduce random high order bits. Thus the process would 

not otherwise necessarily terminate at the right point. 

  

OUTPUT: 

  

- ``L`` -- A list of integers or list of lists giving the 

series expansion of ``self``. 

  

EXAMPLES:: 

  

sage: R = Zp(5,5) 

sage: S.<x> = R[] 

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

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

sage: y = W(775, 19); y 

w^10 + 4*w^12 + 2*w^14 + w^15 + 2*w^16 + 4*w^17 + w^18 + O(w^19) 

sage: y._ext_p_list(True) #indirect doctest 

[1, 0, 4, 0, 2, 1, 2, 4, 1] 

sage: y._ext_p_list(False) 

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

""" 

ans = [] 

cdef ntl_ZZ ZZ_coeff = ntl_ZZ() 

cdef Integer coeff = PY_NEW(Integer) 

cdef Integer zero = Integer(0) 

cdef Integer list_elt 

cdef ZZ_c halfp 

cdef Py_ssize_t i, j 

cdef ZZ_p_c const_term_holder 

self.prime_pow.restore_top_context() 

###ZZ_p_construct(&const_term_holder) 

cdef ntl_ZZ holder = ntl_ZZ() 

cdef ZZ_p_c tmp 

cdef pAdicPrinter_class printer = <pAdicPrinter_class>self.parent()._printer 

cdef ZZ_pX_c shifter = (<ntl_ZZ_pX>self._ntl_rep()).x 

  

#cdef ntl_ZZ_pContext_class cup = self.prime_pow.get_context(self.prime_pow.prec_cap + (<PowComputer_ZZ_pX_FM_Eis>self.prime_pow).low_length) 

#cdef ntl_ZZ_pX printer = ntl_ZZ_pX([],cup) 

#printer.x = ((<PowComputer_ZZ_pX_FM_Eis>self.prime_pow).low_shifter[0]).val() 

#print(printer) 

  

if self.prime_pow.e == 1: 

for j from 0 <= j < self.prime_pow.prec_cap: 

ans.append([]) 

for i from 0 <= i < self.prime_pow.deg: 

ZZ_coeff.x = ZZ_p_rep(ZZ_pX_coeff(shifter, i)) 

ZZ_to_mpz(coeff.value, &ZZ_coeff.x) 

L = printer.base_p_list(coeff, pos) 

for j from 0 <= j < prec: 

if j < len(L): 

ans[j].append(L[j]) 

else: 

ans[j].append(zero) 

for j from 0 <= j < prec: 

while len(ans[j]) > 0: 

if ans[j][-1] == 0: 

ans[j].pop() 

else: 

break 

zerotest = [] 

else: 

halfp = self.prime_pow.pow_ZZ_tmp(1)[0] 

ZZ_DivRem_long(halfp, halfp, 2) 

i = 0 

while True: 

# It's important that one doesn't normalize in between shifting (for capped relative elements): 

# _const_term doesn't normalize and thus we pick up the zeros 

# since we're throwing away leading zeros, it doesn't matter if we start normalized or not. 

for j from 0 <= j < self.prime_pow.e: 

list_elt = PY_NEW(Integer) 

if i + j == prec: 

break 

ZZ_rem(holder.x, ZZ_p_rep(ZZ_pX_coeff(shifter, j)), self.prime_pow.pow_ZZ_tmp(1)[0]) 

if not pos and not ZZ_IsZero(holder.x) and ZZ_compare(holder.x, halfp) > 0: 

ZZ_sub(holder.x, self.prime_pow.pow_ZZ_tmp(1)[0], holder.x) 

ZZ_p_add(tmp, ZZ_to_ZZ_p(holder.x), ZZ_pX_coeff(shifter, j)) 

ZZ_pX_SetCoeff(shifter, j, tmp) 

ZZ_negate(holder.x, holder.x) 

ZZ_to_mpz(list_elt.value, &holder.x) 

ans.append(list_elt) 

i += self.prime_pow.e 

if i >= prec: 

break 

self.prime_pow.eis_shift(&shifter, &shifter, self.prime_pow.e, self.prime_pow.capdiv(prec - i)) 

zerotest = 0 

while len(ans) > 0: 

if ans[-1] == zerotest: 

ans.pop() 

else: 

break 

while len(ans) > 0: 

if ans[0] == zerotest: 

ans.pop(0) 

else: 

break 

return ans 

  

def norm(self, base = None): 

""" 

Return the absolute or relative norm of this element. 

  

NOTE! This is not the `p`-adic absolute value. This is a 

field theoretic norm down to a ground ring. If you want the 

`p`-adic absolute value, use the ``abs()`` function instead. 

  

If ``base`` is given then ``base`` must be a subfield of the 

parent `L` of ``self``, in which case the norm is the relative 

norm from L to ``base``. 

  

In all other cases, the norm is the absolute norm down to 

`\mathbb{Q}_p` or `\mathbb{Z}_p`. 

  

EXAMPLES:: 

  

sage: R = ZpCR(5,5) 

sage: S.<x> = R[] 

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

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

sage: ((1+2*w)^5).norm() 

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

sage: ((1+2*w)).norm()^5 

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

  

TESTS:: 

  

sage: R = ZpCA(5,5) 

sage: S.<x> = ZZ[] 

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

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

sage: ((1+2*w)^5).norm() 

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

sage: ((1+2*w)).norm()^5 

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

sage: R = ZpFM(5,5) 

sage: S.<x> = ZZ[] 

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

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

sage: ((1+2*w)^5).norm() 

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

sage: ((1+2*w)).norm()^5 

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

  

Check that :trac:`11586` has been resolved:: 

  

sage: R.<x> = QQ[] 

sage: f = x^2 + 3*x + 1 

sage: M.<a> = Qp(7).extension(f) 

sage: M(7).norm() 

7^2 + O(7^22) 

sage: b = 7*a + 35 

sage: b.norm() 

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

sage: b*b.frobenius() 

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

""" 

if base is not None: 

if base is self.parent(): 

return self 

else: 

raise NotImplementedError 

if self._is_exact_zero(): 

return self.parent().ground_ring()(0) 

elif self._is_inexact_zero(): 

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

if self.valuation() == 0: 

return self.parent().ground_ring()(self.matrix_mod_pn().det()) 

else: 

if self.parent().e() == 1: 

norm_of_uniformizer = self.parent().ground_ring().uniformizer_pow(self.parent().degree()) 

else: 

norm_of_uniformizer = (-1)**self.parent().degree() * self.parent().defining_polynomial()[0] 

return self.parent().ground_ring()(self.unit_part().matrix_mod_pn().det()) * norm_of_uniformizer**self.valuation() 

  

def trace(self, base = None): 

""" 

Return the absolute or relative trace of this element. 

  

If ``base`` is given then ``base`` must be a subfield of the 

parent `L` of ``self``, in which case the norm is the relative 

norm from `L` to ``base``. 

  

In all other cases, the norm is the absolute norm down to 

`\mathbb{Q}_p` or `\mathbb{Z}_p`. 

  

EXAMPLES:: 

  

sage: R = ZpCR(5,5) 

sage: S.<x> = R[] 

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

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

sage: a = (2+3*w)^7 

sage: b = (6+w^3)^5 

sage: a.trace() 

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

sage: a.trace() + b.trace() 

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

sage: (a+b).trace() 

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

  

TESTS:: 

  

sage: R = ZpCA(5,5) 

sage: S.<x> = ZZ[] 

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

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

sage: a = (2+3*w)^7 

sage: b = (6+w^3)^5 

sage: a.trace() 

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

sage: a.trace() + b.trace() 

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

sage: (a+b).trace() 

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

sage: R = ZpFM(5,5) 

sage: S.<x> = R[] 

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

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

sage: a = (2+3*w)^7 

sage: b = (6+w^3)^5 

sage: a.trace() 

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

sage: a.trace() + b.trace() 

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

sage: (a+b).trace() 

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

""" 

if base is not None: 

if base is self.parent(): 

return self 

else: 

raise NotImplementedError 

if self._is_exact_zero(): 

return self.parent().ground_ring()(0) 

elif self._is_inexact_zero(): 

return self.ground_ring(0, (self.valuation() - 1) // self.parent().e() + 1) 

if self.valuation() >= 0: 

return self.parent().ground_ring()(self.matrix_mod_pn().trace()) 

else: 

shift = -(self.valuation() // self.parent().e()) 

return self.parent().ground_ring()((self * self.parent().prime() ** shift).matrix_mod_pn().trace()) / self.parent().prime()**shift 

  

def _rational_(self): 

""" 

Returns a rational approximation of ``self``. 

  

This does not try to optimize which rational is picked: see 

``algdep`` for another option. 

  

EXAMPLES:: 

  

sage: QQ(Qq(125,names='a')(-1/5)) #indirect doctest 

-1/5 

""" 

if self.valuation() < 0: 

pk = self.parent().prime()**(-self.ordp()).ceil() 

return (self * pk)._integer_() / pk 

else: 

return QQ(self._integer_()) 

  

def _prime_pow(self): 

""" 

Provides access to ``self's`` ``prime_pow``. 

  

EXAMPLES:: 

  

sage: R = ZpCR(5,5) 

sage: S.<x> = R[] 

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

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

sage: w._prime_pow() 

PowComputer_ext for 5, with polynomial [3120 125 3110 75 0 1] 

""" 

return self.prime_pow 

  

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

""" 

Multiplies this element by ``p^shift``. 

  

TESTS: 

  

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

  

sage: K = ZpCA(3) 

sage: R.<u> = K[] 

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

sage: L(R.gen()) 

u + O(3^20) 

  

sage: K = ZpFM(3) 

sage: R.<u> = K[] 

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

sage: L(R.gen()) 

u + O(3^20) 

  

""" 

if shift != 0: 

raise NotImplementedError 

  

def _test_preprocess_list(R, L): 

""" 

Given a list of elements convertible to ``ntl_ZZ_p``s, finds the 

appropriate absolute precision and returns a list of either ``ntl_ZZs`` or ``ntl_ZZ_ps``. 

  

INPUT: 

  

- ``R`` -- a `p`-adic extension ring 

  

- ``L`` -- a list of rationals, integers, ints, longs, 

``ntl_ZZ_ps``, ``ntl_ZZs``, ``IntegerMods`` or `p`-adic base 

elements 

  

OUTPUT: 

  

- ``LL`` -- if all inputs are integral, a list of ``ntl_ZZs``. 

Otherwise, a list of ``ntl_ZZ_ps``, modulo `p^n` which is 

determined by the precision cap of ``R`` and the precisions of 

the elements in ``L``. 

  

- ``min_val`` -- A valuation by which to multiply the elements of 

``LL`` in order to recover the input elements of ``L``. 

  

- ``ctx`` -- An ``ntl_ZZ_p_Context`` giving the power of `p` 

modulo which the elements in ``LL`` are defined. If ``None``, 

then the elements of ``LL`` are ``ntl_ZZs``. 

  

EXAMPLES:: 

  

sage: from sage.rings.padics.padic_ZZ_pX_element import _test_preprocess_list 

sage: from sage.libs.ntl.all import ZZ as ntl_ZZ, ZZ_p as ntl_ZZ_p 

sage: _test_preprocess_list(ZqCA(25,names='a',implementation="NTL"), [1,2,3]) 

([1, 2, 3], 0, None) 

sage: _test_preprocess_list(ZqCA(25,names='a',implementation="NTL"), [10,20,30]) 

([10, 20, 30], 0, None) 

sage: _test_preprocess_list(ZqCA(25,names='a',implementation="NTL"), [1/5,2/5,3]) 

([1, 2, 15], -1, NTL modulus 95367431640625) 

sage: _test_preprocess_list(ZqCA(25,names='a',implementation="NTL"), [1/5,mod(2,625),3]) 

([1, 10, 15], -1, NTL modulus 3125) 

sage: _test_preprocess_list(ZqCA(25,names='a',implementation="NTL"), [1/5,mod(2,625),ntl_ZZ_p(3,25)]) 

([1, 10, 15], -1, NTL modulus 125) 

sage: _test_preprocess_list(ZqCA(25,names='a',implementation="NTL"), [1/5,mod(2,625),Zp(5)(5,3)]) 

([1, 10, 1], -1, NTL modulus 625) 

sage: _test_preprocess_list(ZqCA(25,names='a',implementation="NTL"), [1/5,mod(2,625),Zp(5)(5,3),0]) 

([1, 10, 1, 0], -1, NTL modulus 625) 

sage: _test_preprocess_list(ZqCA(25,names='a',implementation="NTL"), [1/5,mod(2,625),Zp(5)(5,3),mod(0,3125)]) 

([1, 10, 1, 0], -1, NTL modulus 625) 

""" 

return preprocess_list(R(0), L) 

  

cdef preprocess_list(pAdicZZpXElement elt, L): 

""" 

See the documentation for _test_preprocess_list 

""" 

cdef Py_ssize_t i 

cdef ZZ_c tmp 

cdef ntl_ZZ_pContext_class ctx 

cdef ntl_ZZ pshift_z 

cdef Integer pshift_m 

cdef long aprec 

cdef ntl_ZZ py_tmp 

if not isinstance(L, list): 

raise TypeError("L must be a list") 

min_val, min_aprec, total_type = find_val_aprec(elt.prime_pow, L) 

#return "a","b","c" 

if total_type == two: 

# all integers 

return [ntl_ZZ(a) for a in L], zero, None 

if min_val < 0 and not elt.prime_pow.in_field: 

raise ValueError("negative valuation") 

if total_type == one: 

# rationals and integers 

py_tmp = ntl_ZZ.__new__(ntl_ZZ) 

py_tmp.x = elt.prime_pow.pow_ZZ_top()[0] 

ctx = ntl_ZZ_pContext(py_tmp) 

else: 

# integers, rationals and things with finite precision 

# note that min_val will be non-positive since things with finite precision return non-positive valuation from get_val_prec 

py_tmp = ntl_ZZ.__new__(ntl_ZZ) 

py_tmp.x = elt.prime_pow.pow_ZZ_tmp(mpz_get_ui((<Integer>(min_aprec - min_val)).value))[0] 

ctx = ntl_ZZ_pContext(py_tmp) 

if min_val < 0: 

pshift_z = ntl_ZZ.__new__(ntl_ZZ) 

pshift_z.x = elt.prime_pow.pow_ZZ_tmp(-mpz_get_si((<Integer>min_val).value))[0] 

pshift_m = elt.prime_pow.pow_Integer(-mpz_get_si((<Integer>min_val).value)) 

for i from 0 <= i < len(L): 

if isinstance(L[i], ntl_ZZ): 

L[i] = ntl_ZZ_p(L[i]*pshift_z, ctx) 

elif isinstance(L[i], Integer) or isinstance(L[i], Rational) or isinstance(L[i], (int, long)): 

L[i] = ntl_ZZ_p(L[i]*pshift_m, ctx) 

elif isinstance(L[i], pAdicGenericElement) and L[i]._is_base_elt(elt.prime_pow.prime): 

L[i] = ntl_ZZ_p((L[i] << min_val).lift(), ctx) 

elif is_IntegerMod(L[i]): 

L[i] = ntl_ZZ_p(L[i].lift()*pshift_m, ctx) 

elif (L[i].modulus_context() is not ctx) or min_val != zero: 

L[i] = ntl_ZZ_p(L[i].lift()*pshift_z, ctx) 

elif elt.parent().is_capped_relative() and min_val > 0: 

pshift_z = ntl_ZZ.__new__(ntl_ZZ) 

pshift_z.x = elt.prime_pow.pow_ZZ_tmp(mpz_get_ui((<Integer>min_val).value))[0] 

pshift_m = elt.prime_pow.pow_Integer(mpz_get_ui((<Integer>min_val).value)) 

for i from 0 <= i < len(L): 

if isinstance(L[i], ntl_ZZ): 

ZZ_div(tmp, (<ntl_ZZ>L[i]).x, pshift_z.x) 

py_tmp = ntl_ZZ.__new__(ntl_ZZ) 

py_tmp.x = tmp 

L[i] = ntl_ZZ_p(py_tmp, ctx) 

elif isinstance(L[i], Integer) or isinstance(L[i], Rational) or isinstance(L[i], (int, long)): 

L[i] = ntl_ZZ_p(L[i]//pshift_m, ctx) 

elif isinstance(L[i], pAdicGenericElement) and L[i]._is_base_elt(elt.prime_pow.prime): 

L[i] = ntl_ZZ_p((L[i] << min_val).lift(), ctx) 

elif is_IntegerMod(L[i]): 

L[i] = ntl_ZZ_p(L[i].lift()//pshift_m, ctx) 

elif (L[i].modulus_context() is not ctx) or min_val != zero: 

ZZ_div(tmp, (<ntl_ZZ>L[i].lift()).x, pshift_z.x) 

py_tmp = ntl_ZZ.__new__(ntl_ZZ) 

py_tmp.x = tmp 

L[i] = ntl_ZZ_p(py_tmp, ctx) 

else: 

for i from 0 <= i < len(L): 

if isinstance(L[i], ntl_ZZ) or isinstance(L[i], Integer) or isinstance(L[i], Rational) or isinstance(L[i], (int, long)): 

L[i] = ntl_ZZ_p(L[i], ctx) 

elif (isinstance(L[i], pAdicGenericElement) and L[i]._is_base_elt(elt.prime_pow.prime)) or is_IntegerMod(L[i]) or (L[i].modulus_context() is not ctx): 

L[i] = ntl_ZZ_p(L[i].lift(), ctx) 

return L, min_val, ctx 

  

def _find_val_aprec_test(R, L): 

""" 

Given a list ``L``, finds the minimum valuation, minimum absolute 

precision and minimum common type of the elements. 

  

INPUT: 

  

- ``R`` -- a `p`-adic extension 

- ``L`` -- a list of integers, rationals, ``IntegerMods``, etc. 

  

OUTPUT: 

  

- ``min_val`` -- the minimum valuation of any element in the list. 

  

- ``min_aprec`` -- the minimum absolute precision of any element 

in the list. If infinite, a predefined constant ``big`` is 

returned instead. 

  

  

- ``total_type`` -- 

  

+ If all elements are integers or ints, 2. 

  

+ If all elements are rationals or integers, 1. 

  

+ If some elements have finite precision, 0. 

  

EXAMPLES:: 

  

sage: from sage.rings.padics.padic_ZZ_pX_element import _find_val_aprec_test 

sage: from sage.libs.ntl.all import ZZ as ntl_ZZ, ZZ_p as ntl_ZZ_p 

sage: _find_val_aprec_test(Zq(25,names='a',implementation="NTL"), [15, int(75), ntl_ZZ(625)]) 

(1, 340282366920938463463374607431768211457, 2) 

sage: _find_val_aprec_test(Zq(25,names='a',implementation="NTL"), [5, int(25), 7/25]) 

(-2, 340282366920938463463374607431768211457, 1) 

sage: _find_val_aprec_test(Zq(25,names='a',implementation="NTL"), [mod(4,125), Zp(5)(5,5), ntl_ZZ_p(16,625), 4/125]) 

(-3, 3, 0) 

sage: _find_val_aprec_test(Zq(25,names='a',implementation="NTL"), [mod(25,125), Zp(5)(5,5), ntl_ZZ_p(15,625)]) 

(0, 3, 0) 

""" 

return find_val_aprec(R.prime_pow, L) 

  

cdef find_val_aprec(PowComputer_ext pp, L): 

""" 

Given a list ``L``, finds the minimum valuation, minimum absolute 

precision and minimum common type of the elements. 

  

INPUT: 

  

- ``pp`` -- a PowComputer_ext for the element that this list is 

being initialized into. 

  

- ``L`` -- a list of integers, rationals, ``IntegerMods``, etc. 

  

See the documentation for _find_val_aprec_test for more details. 

""" 

cdef Py_ssize_t i 

min_val = big 

min_aprec = big 

total_type = two # we begin by defaulting to the list elements being integers 

for i from 0 <= i < len(L): 

cur_val, cur_aprec, cur_type = get_val_prec(pp, L[i]) 

#return "a","b","c" 

# proc_type == 0 indicates something with finite precision 

# proc_type == 1 indicates a rational, or something that cannot be coerced to an integer 

# proc_type == 2 indicates an integer, or something that can be coerced to an integer 

# but can be coerced to Z/p^n for any n. 

if cur_aprec < min_aprec: 

min_aprec = cur_aprec 

if cur_val < min_val: 

min_val = cur_val 

if cur_type < total_type: 

total_type = cur_type 

return min_val, min_aprec, total_type 

  

def _test_get_val_prec(R, a): 

""" 

Returns valuation, absolute precision and type of an input 

element. 

  

INPUT: 

  

- ``R`` -- A `p`-adic extension ring to provide a ``PowComputer_ext`` 

  

- ``a`` -- A rational, integer, int, long, ``ntl_ZZ_p``, 

``ntl_ZZ``, ``IntegerMod`` or `p`-adic base element. 

  

OUTPUT: 

  

- ``val`` -- if ``a`` is exact, ``a.valuation(p)``, otherwise 

``min(0, a.valuation())`` 

  

- ``aprec`` -- the absolute precision of ``a``. If ``a`` is 

exact, a large predefined constant. 

  

- type -- 

  

+ 2 if ``a`` is an integer, int or long; 

  

+ 1 if ``a`` is a rational. 

  

+ 0 if ``a`` has finite precision. 

  

EXAMPLES:: 

  

sage: from sage.rings.padics.padic_ZZ_pX_element import _test_get_val_prec 

sage: from sage.libs.ntl.all import ZZ as ntl_ZZ, ZZ_p as ntl_ZZ_p 

sage: _test_get_val_prec(Zq(25,names='a',implementation="NTL"), 15) 

(1, 340282366920938463463374607431768211457, 2) 

sage: _test_get_val_prec(Zq(25,names='a',implementation="NTL"), ntl_ZZ(15)) 

(1, 340282366920938463463374607431768211457, 2) 

sage: _test_get_val_prec(Zq(25,names='a',implementation="NTL"), int(15)) 

(1, 340282366920938463463374607431768211457, 2) 

sage: _test_get_val_prec(Zq(25,names='a',implementation="NTL"), 1/15) 

(-1, 340282366920938463463374607431768211457, 1) 

sage: _test_get_val_prec(Zq(25,names='a',implementation="NTL"), Zp(5)(15,4)) 

(0, 4, 0) 

sage: _test_get_val_prec(Zq(25,names='a',implementation="NTL"), Qp(5)(1/15,4)) 

(-1, 4, 0) 

sage: _test_get_val_prec(Zq(25,names='a',implementation="NTL"), mod(15,625)) 

(0, 4, 0) 

sage: _test_get_val_prec(Zq(25,names='a',implementation="NTL"), ntl_ZZ_p(15,625)) 

(0, 4, 0) 

  

TESTS:: 

  

sage: _test_get_val_prec(Zq(25,names='a',implementation="NTL"), 0) #indirect doctest 

(340282366920938463463374607431768211457, 340282366920938463463374607431768211457, 2) 

sage: _test_get_val_prec(Zq(25,names='a',implementation="NTL"), ntl_ZZ(0)) 

(340282366920938463463374607431768211457, 340282366920938463463374607431768211457, 2) 

sage: _test_get_val_prec(Zq(25,names='a',implementation="NTL"), int(0)) 

(340282366920938463463374607431768211457, 340282366920938463463374607431768211457, 2) 

sage: _test_get_val_prec(Zq(25,names='a',implementation="NTL"), 0/1) 

(340282366920938463463374607431768211457, 340282366920938463463374607431768211457, 1) 

sage: _test_get_val_prec(Zq(25,names='a',implementation="NTL"), Zp(5)(25,4)) 

(0, 4, 0) 

sage: _test_get_val_prec(Zq(25,names='a',implementation="NTL"), Qp(5)(1/25,4)) 

(-2, 4, 0) 

sage: _test_get_val_prec(Zq(25,names='a',implementation="NTL"), Zp(5)(0)) 

(340282366920938463463374607431768211457, 340282366920938463463374607431768211457, 1) 

sage: _test_get_val_prec(Zq(25,names='a',implementation="NTL"), mod(0,625)) 

(0, 4, 0) 

sage: _test_get_val_prec(Zq(25,names='a',implementation="NTL"), ntl_ZZ_p(0,625)) 

(0, 4, 0) 

""" 

return get_val_prec(R.prime_pow, a) 

  

cdef get_val_prec(PowComputer_ext pp, a): 

""" 

Returns valuation, absolute precision and type of an input element. 

  

INPUT: 

  

- ``pp`` -- A ``PowComputer_ext`` 

  

- ``a`` -- A rational, integer, int, long, ``ntl_ZZ_p``, 

``ntl_ZZ``, ``IntegerMod`` or `p`-adic base element. 

  

See _test_get_val_prec for more details. 

""" 

cdef ntl_ZZ py_tmp 

if isinstance(a, Integer): 

if a == 0: 

return (big, big, two) 

return (a.valuation(pp.prime), big, two) 

if isinstance(a, ntl_ZZ): 

if ZZ_IsZero((<ntl_ZZ>a).x): 

return (big, big, two) 

py_tmp = ntl_ZZ.__new__(ntl_ZZ) 

py_tmp.x = pp.pow_ZZ_tmp(1)[0] 

return (Integer(a.valuation(py_tmp)), big, two) 

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

if a == 0: 

return (big, big, two) 

return (Integer(a).valuation(pp.prime), big, two) 

if isinstance(a, Rational): 

if a == 0: 

return (big, big, one) 

val = a.valuation(pp.prime) 

return (val, big, one) 

if isinstance(a, pAdicGenericElement) and a._is_base_elt(pp.prime): 

if a.parent().prime() == pp.prime: 

if a._is_exact_zero(): 

return (big, big, one) 

val = a.valuation() 

return (val if val < zero else zero, a.precision_absolute(), zero) 

else: 

raise TypeError("primes must match") 

cdef mpz_t leftover 

cdef long long_val 

cdef Integer Integer_val 

if is_IntegerMod(a): 

mpz_init(leftover) 

long_val = mpz_remove(leftover, (<Integer>a.modulus()).value, pp.prime.value) 

if long_val > 0 and mpz_cmp_ui(leftover, 1) == 0: 

mpz_clear(leftover) 

Integer_val = PY_NEW(Integer) 

mpz_set_ui(Integer_val.value, long_val) 

# Since we're guaranteed to be in type 0, we don't care about computing the actual valuation 

return (zero, Integer_val, zero) 

else: 

mpz_clear(leftover) 

raise TypeError("modulus must be a positive power of the appropriate prime") 

cdef ZZ_c leftover_z 

if isinstance(a, ntl_ZZ_p): 

long_val = ZZ_remove(leftover_z, (<ntl_ZZ_p>a).c.p.x, pp.pow_ZZ_tmp(1)[0]) 

if long_val > 0 and ZZ_IsOne(leftover_z): 

Integer_val = PY_NEW(Integer) 

mpz_set_ui(Integer_val.value, long_val) 

# Since we're guaranteed to be in type 0, we don't care about computing the actual valuation 

return (zero, Integer_val, zero) 

else: 

print(long_val) 

py_tmp = ntl_ZZ.__new__(ntl_ZZ) 

py_tmp.x = (<ntl_ZZ_p>a).c.p.x 

print(py_tmp) 

py_tmp.x = leftover_z 

print(py_tmp) 

raise TypeError("modulus must be a positive power of the appropriate prime") 

raise TypeError("unsupported type for list element: %s" % type(a))