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

r""" 

Power series implemented using PARI 

  

EXAMPLES: 

  

This implementation can be selected for any base ring supported by 

PARI by passing the keyword ``implementation='pari'`` to the 

:func:`~sage.rings.power_series_ring.PowerSeriesRing` constructor:: 

  

sage: R.<q> = PowerSeriesRing(ZZ, implementation='pari'); R 

Power Series Ring in q over Integer Ring 

sage: S.<t> = PowerSeriesRing(CC, implementation='pari'); S 

Power Series Ring in t over Complex Field with 53 bits of precision 

  

Note that only the type of the elements depends on the implementation, 

not the type of the parents:: 

  

sage: type(R) 

<class 'sage.rings.power_series_ring.PowerSeriesRing_domain_with_category'> 

sage: type(q) 

<type 'sage.rings.power_series_pari.PowerSeries_pari'> 

sage: type(S) 

<class 'sage.rings.power_series_ring.PowerSeriesRing_over_field_with_category'> 

sage: type(t) 

<type 'sage.rings.power_series_pari.PowerSeries_pari'> 

  

If `k` is a finite field implemented using PARI, this is the default 

implementation for power series over `k`:: 

  

sage: k.<c> = GF(5^12) 

sage: type(c) 

<type 'sage.rings.finite_rings.element_pari_ffelt.FiniteFieldElement_pari_ffelt'> 

sage: A.<x> = k[[]] 

sage: type(x) 

<type 'sage.rings.power_series_pari.PowerSeries_pari'> 

  

.. WARNING:: 

  

Because this implementation uses the PARI interface, the PARI variable 

ordering must be respected in the sense that the variable name of the 

power series ring must have higher priority than any variable names 

occurring in the base ring:: 

  

sage: R.<y> = QQ[] 

sage: S.<x> = PowerSeriesRing(R, implementation='pari'); S 

Power Series Ring in x over Univariate Polynomial Ring in y over Rational Field 

  

Reversing the variable ordering leads to errors:: 

  

sage: R.<x> = QQ[] 

sage: S.<y> = PowerSeriesRing(R, implementation='pari') 

Traceback (most recent call last): 

... 

PariError: incorrect priority in gtopoly: variable x <= y 

  

AUTHORS: 

  

- Peter Bruin (December 2013): initial version 

  

""" 

  

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

# Copyright (C) 2013-2017 Peter Bruin <P.J.Bruin@math.leidenuniv.nl> 

# 

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

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

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

# (at your option) any later version. 

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

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

  

from __future__ import absolute_import, division, print_function 

  

from cypari2.gen cimport Gen as pari_gen 

from cypari2.pari_instance cimport get_var 

from cypari2.paridecl cimport gel, typ, lg, valp, varn, t_POL, t_SER, t_RFRAC, t_VEC 

from sage.libs.pari.all import pari 

  

from sage.misc.superseded import deprecated_function_alias 

  

from sage.rings.polynomial.polynomial_element cimport Polynomial 

from sage.rings.power_series_ring_element cimport PowerSeries 

from sage.structure.element cimport Element, RingElement 

from sage.structure.parent cimport Parent 

from sage.rings.infinity import infinity 

  

  

cdef PowerSeries_pari construct_from_pari(parent, pari_gen g): 

""" 

Fast construction of power series from PARI objects of suitable 

type (series, polynomials, scalars and rational functions). 

  

The resulting series has the same precision as `g`, unless `g` is 

a rational function, in which case the default precision of 

``parent`` is used. 

  

""" 

cdef long t = typ(g.g) 

v = parent.variable_name() 

if t == t_SER and varn(g.g) == get_var(v): 

prec = lg(g.g) - 2 + valp(g.g) 

elif t == t_RFRAC: 

prec = parent.default_prec() 

g = g.Ser(v, prec - g.valuation(v)) 

else: 

prec = infinity 

cdef PowerSeries_pari x = PowerSeries_pari.__new__(PowerSeries_pari) 

x._parent = parent 

x._prec = prec 

x.g = g 

return x 

  

  

cdef class PowerSeries_pari(PowerSeries): 

r""" 

A power series implemented using PARI. 

  

INPUT: 

  

- ``parent`` -- the power series ring to use as the parent 

  

- ``f`` -- object from which to construct a power series 

  

- ``prec`` -- (default: infinity) precision of the element 

to be constructed 

  

- ``check`` -- ignored, but accepted for compatibility with 

:class:`~sage.rings.power_series_poly.PowerSeries_poly` 

  

""" 

def __init__(self, parent, f=0, prec=infinity, check=None): 

""" 

Initialize ``self``. 

  

TESTS:: 

  

sage: R.<q> = PowerSeriesRing(CC, implementation='pari') 

sage: TestSuite(q).run() 

sage: f = q - q^3 + O(q^10) 

sage: TestSuite(f).run() 

  

""" 

cdef Parent f_parent 

cdef pari_gen g 

cdef long t 

v = parent.variable_name() 

R = parent.base_ring() 

P = parent._poly_ring() 

  

if isinstance(f, PowerSeries): # not only PowerSeries_pari 

f_parent = (<PowerSeries>f)._parent 

if f_parent is parent: 

if prec is infinity: 

prec = (<PowerSeries>f)._prec 

g = f.__pari__() 

elif R.has_coerce_map_from(f_parent): 

g = R.coerce(f).__pari__() 

else: 

if prec is infinity: 

prec = f.prec() 

g = f.polynomial().change_ring(R).__pari__() 

elif isinstance(f, Polynomial): 

f_parent = (<Polynomial>f)._parent 

if f_parent is P: 

g = f.__pari__() 

elif R.has_coerce_map_from(f_parent): 

g = R.coerce(f).__pari__() 

else: 

g = P.coerce(f).__pari__() 

elif isinstance(f, pari_gen): 

g = f 

t = typ(g.g) 

if t == t_POL: 

g = P(g).__pari__() 

elif t == t_SER and varn(g.g) == get_var(v): 

if valp(g.g) < 0: 

raise ValueError('series has negative valuation') 

if prec is infinity: 

prec = lg(g.g) - 2 + valp(g.g) 

g = P(g.Pol(v)).__pari__() 

elif t == t_RFRAC: 

if prec is infinity: 

prec = parent.default_prec() 

g = P.fraction_field()(g).__pari__() 

g = g.Ser(v, prec - g.valuation(v)) 

elif t == t_VEC: 

g = P(g.Polrev(v)).__pari__() 

else: 

g = R(g).__pari__() 

elif isinstance(f, (list, tuple)): 

g = pari([R.coerce(x) for x in f]).Polrev(v) 

else: 

g = R.coerce(f).__pari__() 

  

if prec is infinity: 

self.g = g 

else: 

if not g: 

self.g = g.Ser(v, prec) 

else: 

self.g = g.Ser(v, prec - g.valuation(v)) 

  

PowerSeries.__init__(self, parent, prec) 

  

def __hash__(self): 

""" 

Return a hash of ``self``. 

  

TESTS:: 

  

sage: R.<t> = PowerSeriesRing(ZZ, implementation='pari') 

sage: hash(t^2 + 1) == hash(pari(t^2 + 1)) 

True 

  

""" 

return hash(self.g) 

  

def __reduce__(self): 

""" 

Used for pickling. 

  

EXAMPLES:: 

  

sage: A.<z> = PowerSeriesRing(RR, implementation='pari') 

sage: f = z - z^3 + O(z^10) 

sage: z == loads(dumps(z)) 

True 

sage: f == loads(dumps(f)) 

True 

  

""" 

return PowerSeries_pari, (self._parent, self.g, self._prec, False) 

  

def __pari__(self): 

""" 

Convert ``self`` to a PARI object. 

  

TESTS:: 

  

sage: R.<t> = PowerSeriesRing(GF(7), implementation='pari') 

sage: (3 - t^3 + O(t^5)).__pari__() 

Mod(3, 7) + Mod(6, 7)*t^3 + O(t^5) 

  

""" 

return self.g 

  

def polynomial(self): 

""" 

Convert ``self`` to a polynomial. 

  

EXAMPLES:: 

  

sage: R.<t> = PowerSeriesRing(GF(7), implementation='pari') 

sage: f = 3 - t^3 + O(t^5) 

sage: f.polynomial() 

6*t^3 + 3 

  

""" 

return self._parent._poly_ring()(self.list()) 

  

def valuation(self): 

""" 

Return the valuation of ``self``. 

  

EXAMPLES:: 

  

sage: R.<t> = PowerSeriesRing(QQ, implementation='pari') 

sage: (5 - t^8 + O(t^11)).valuation() 

0 

sage: (-t^8 + O(t^11)).valuation() 

8 

sage: O(t^7).valuation() 

7 

sage: R(0).valuation() 

+Infinity 

  

""" 

if not self.g: 

return self._prec 

return self.g.valuation(self._parent.variable_name()) 

  

def __bool__(self): 

""" 

Return ``True`` if ``self`` is nonzero, and ``False`` otherwise. 

  

EXAMPLES:: 

  

sage: R.<t> = PowerSeriesRing(GF(11), implementation='pari') 

sage: bool(1 + t + O(t^18)) 

True 

sage: bool(R(0)) 

False 

sage: bool(O(t^18)) 

False 

  

""" 

return bool(self.g) 

  

def __call__(self, *x, **kwds): 

""" 

Evaluate ``self`` at `x = a`. 

  

EXAMPLES:: 

  

sage: R.<t> = PowerSeriesRing(ZZ, implementation='pari') 

sage: f = t^2 + t^3 + O(t^6) 

sage: f(t^3) 

t^6 + t^9 + O(t^18) 

sage: f(t=t^3) 

t^6 + t^9 + O(t^18) 

sage: f(f) 

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

sage: f(f)(f) == f(f(f)) 

True 

  

The following demonstrates that the problems raised in 

:trac:`3979` and :trac:`5367` are solved:: 

  

sage: [f(t^2 + O(t^n)) for n in [9, 10, 11]] 

[t^4 + t^6 + O(t^11), t^4 + t^6 + O(t^12), t^4 + t^6 + O(t^12)] 

sage: f(t^2) 

t^4 + t^6 + O(t^12) 

  

It is possible to substitute a series for which only 

the precision is defined:: 

  

sage: f(O(t^5)) 

O(t^10) 

  

or to substitute a polynomial (the result belonging to the power 

series ring over the same base ring):: 

  

sage: P.<z> = ZZ[] 

sage: g = f(z + z^3); g 

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

sage: g.parent() 

Power Series Ring in z over Integer Ring 

  

A series defined over another ring can be substituted:: 

  

sage: S.<u> = PowerSeriesRing(GF(7), implementation='pari') 

sage: f(2*u + u^3 + O(u^5)) 

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

  

Substituting `p`-adic numbers:: 

  

sage: f(100 + O(5^7)) 

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

  

sage: ff = PowerSeriesRing(pAdicRing(5), 't', implementation='pari')(f) 

sage: ff 

(1 + O(5^20))*t^2 + (1 + O(5^20))*t^3 + O(t^6) 

  

sage: ff(100 + O(5^7)) 

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

  

sage: ff(100 + O(2^7)) 

Traceback (most recent call last): 

... 

TypeError: no common canonical parent for objects with parents: 

'5-adic Ring with capped relative precision 20' and 

'2-adic Ring with capped relative precision 20' 

  

The argument must have valuation at least 1, unless the series 

is actually a polynomial:: 

  

sage: f(0) 

0 

sage: f(1 + t) 

Traceback (most recent call last): 

... 

ValueError: can only substitute elements of positive valuation 

  

sage: f(t^-2) 

Traceback (most recent call last): 

... 

ValueError: can only substitute elements of positive valuation 

  

sage: f(2 + O(5^3)) 

Traceback (most recent call last): 

... 

ValueError: can only substitute elements of positive valuation 

  

sage: g = t^2 + t^3 

sage: g(1 + t + O(t^2)) 

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

sage: g(3) 

36 

  

Substitution of variables belonging to the base ring can be 

done using keywords:: 

  

sage: P.<a> = GF(5)[] 

sage: Q.<x> = PowerSeriesRing(P, implementation='pari') 

sage: h = (1 - a*x)^-1 + O(x^7); h 

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

sage: h(x^2, a=3) 

1 + 3*x^2 + 4*x^4 + 2*x^6 + x^8 + 3*x^10 + 4*x^12 + O(x^14) 

  

""" 

if len(kwds) >= 1: 

name = self._parent.variable_name() 

if name in kwds: # the series variable is specified by a keyword 

if len(x): 

raise ValueError("must not specify %s keyword and positional argument" % name) 

x = [kwds[name]] 

del kwds[name] 

  

if len(x) != 1: 

raise ValueError("must specify exactly one positional argument") 

  

a = x[0] 

  

s = self._prec 

if s is infinity: 

return self.polynomial()(a) 

  

# Determine the parent of the result. 

P = self._parent 

Q = a.parent() 

if not Q.has_coerce_map_from(P.base_ring()): 

from sage.structure.element import canonical_coercion 

a = canonical_coercion(P.base_ring()(0), a)[1] 

Q = a.parent() 

  

# The result is defined if the ring Q is complete with respect 

# to an ideal I, and the element a lies in I. Here we only 

# implement a few special cases. 

from sage.rings.padics.padic_generic import pAdicGeneric 

from sage.rings.polynomial.polynomial_ring import PolynomialRing_general 

from sage.rings.power_series_ring import PowerSeriesRing_generic 

from sage.rings.laurent_series_ring import LaurentSeriesRing 

if isinstance(Q, pAdicGeneric): 

# Substitution of p-adic numbers in power series is 

# currently not implemented in PARI (2.8.0-development). 

t = a.valuation() 

if t <= 0: 

raise ValueError("can only substitute elements of positive valuation") 

return Q(self.polynomial()(a)).add_bigoh(t * self._prec) 

elif isinstance(Q, (PowerSeriesRing_generic, LaurentSeriesRing)): 

# In Sage, we want an error to be raised when trying to 

# substitute a series of non-positive valuation, but PARI 

# (2.8.0-development) does not do this. For example, 

# subst(1 + O(x), x, 1/y) yields O(y^-1). 

if a.valuation() <= 0: 

raise ValueError("can only substitute elements of positive valuation") 

elif isinstance(Q, PolynomialRing_general): 

Q = Q.completion(Q.gen()) 

elif Q.is_exact() and not a: 

pass 

else: 

raise ValueError('cannot substitute %s in %s' % (a, self)) 

  

if not kwds: 

return Q(self.g(a)) 

else: 

kwds[P.variable_name()] = a 

return Q(self.g(**kwds)) 

  

def __getitem__(self, n): 

r""" 

Return the ``n``-th coefficient of ``self``. 

  

If ``n`` is a slice object, this returns a power series of the 

same precision, whose coefficients are the same as ``self`` 

for those indices in the slice, and 0 otherwise. 

  

Returns 0 for negative coefficients. Raises an ``IndexError`` 

if trying to access beyond known coefficients. 

  

EXAMPLES:: 

  

sage: R.<t> = PowerSeriesRing(QQ, implementation='pari') 

sage: f = 3/2 - 17/5*t^3 + O(t^5) 

sage: f[3] 

-17/5 

sage: f[-2] 

0 

sage: f[4] 

0 

sage: f[5] 

Traceback (most recent call last): 

... 

IndexError: index out of range 

  

sage: R.<t> = PowerSeriesRing(ZZ, implementation='pari') 

sage: f = (2-t)^5; f 

32 - 80*t + 80*t^2 - 40*t^3 + 10*t^4 - t^5 

sage: f[:4] 

32 - 80*t + 80*t^2 - 40*t^3 

  

sage: f = 1 + t^3 - 4*t^4 + O(t^7) ; f 

1 + t^3 - 4*t^4 + O(t^7) 

sage: f[:4] 

1 + t^3 + O(t^7) 

  

""" 

cdef long t 

if isinstance(n, slice): 

return PowerSeries_pari(self._parent, self.polynomial()[n], 

prec=self._prec) 

if n < 0: 

return self.base_ring().zero() 

  

t = typ(self.g.g) 

if t == t_POL or t == t_SER: 

h = self.g[n] 

else: 

h = self.g 

return self.base_ring()(h) 

  

def __invert__(self): 

""" 

Return the multiplicative inverse of ``self``. 

  

TESTS:: 

  

sage: R.<t> = PowerSeriesRing(QQ, default_prec=6, implementation='pari') 

sage: ~(R(1-t)) 

1 + t + t^2 + t^3 + t^4 + t^5 + O(t^6) 

  

""" 

h = ~self.g 

if h.valuation(self._parent.variable_name()) < 0: 

return self._parent.laurent_series_ring()(h) 

return construct_from_pari(self._parent, h) 

  

def __neg__(self): 

""" 

Return the negative of ``self``. 

  

TESTS:: 

  

sage: R.<t> = PowerSeriesRing(QQ, implementation='pari') 

sage: f = t + 17/5*t^3 + 2*t^4 + O(t^5) 

sage: -f 

-t - 17/5*t^3 - 2*t^4 + O(t^5) 

  

""" 

return construct_from_pari(self._parent, -self.g) 

  

def __pow__(PowerSeries_pari self, n, m): 

""" 

Exponentiation of power series. 

  

TESTS:: 

  

sage: R.<t> = PowerSeriesRing(QQ, implementation='pari') 

sage: f = 3 - t^3 + O(t^5) 

sage: a = f^3; a 

27 - 27*t^3 + O(t^5) 

sage: b = f^-3; b 

1/27 + 1/27*t^3 + O(t^5) 

  

""" 

h = self.g ** n 

if h.valuation(self._parent.variable_name()) < 0: 

return self._parent.laurent_series_ring()(h) 

return construct_from_pari(self._parent, h) 

  

cpdef _add_(self, right): 

""" 

Addition of power series. 

  

TESTS:: 

  

sage: R.<x> = PowerSeriesRing(ZZ, implementation='pari') 

sage: f = x^4 + O(x^5); f 

x^4 + O(x^5) 

sage: g = x^2 + O(x^3); g 

x^2 + O(x^3) 

sage: f+g 

x^2 + O(x^3) 

  

""" 

return construct_from_pari(self._parent, self.g + (<PowerSeries_pari>right).g) 

  

cpdef _sub_(self, right): 

""" 

Subtraction of power series. 

  

TESTS:: 

  

sage: k.<w> = ZZ[] 

sage: R.<t> = PowerSeriesRing(k, implementation='pari') 

sage: w*t^2 -w*t +13 - (w*t^2 + w*t) 

13 - 2*w*t 

  

""" 

return construct_from_pari(self._parent, self.g - (<PowerSeries_pari>right).g) 

  

cpdef _mul_(self, right): 

""" 

Multiplication of power series. 

  

TESTS:: 

  

sage: k.<w> = PowerSeriesRing(ZZ, implementation='pari') 

sage: (1+17*w+15*w^3+O(w^5))*(19*w^10+O(w^12)) 

19*w^10 + 323*w^11 + O(w^12) 

  

""" 

return construct_from_pari(self._parent, self.g * (<PowerSeries_pari>right).g) 

  

cpdef _rmul_(self, Element c): 

""" 

Right multiplication by a scalar. 

  

TESTS:: 

  

sage: R.<t> = PowerSeriesRing(GF(7), implementation='pari') 

sage: f = t + 3*t^4 + O(t^11) 

sage: f * GF(7)(3) 

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

  

""" 

return construct_from_pari(self._parent, self.g * c) 

  

cpdef _lmul_(self, Element c): 

""" 

Left multiplication by a scalar. 

  

TESTS:: 

  

sage: R.<t> = PowerSeriesRing(GF(11), implementation='pari') 

sage: f = 1 + 3*t^4 + O(t^120) 

sage: 2 * f 

2 + 6*t^4 + O(t^120) 

  

""" 

return construct_from_pari(self._parent, c * self.g) 

  

cpdef _div_(self, right): 

""" 

Division of power series. 

  

TESTS:: 

  

sage: R.<t> = PowerSeriesRing(GF(11), default_prec=8, implementation='pari') 

sage: f = t/(1 - t); f 

t + t^2 + t^3 + t^4 + t^5 + t^6 + t^7 + O(t^8) 

sage: f.parent() 

Power Series Ring in t over Finite Field of size 11 

sage: g = (1 - t)/t; g 

t^-1 + 10 

sage: g.parent() 

Laurent Series Ring in t over Finite Field of size 11 

  

""" 

h = self.g / (<PowerSeries_pari>right).g 

if h.valuation(self._parent.variable_name()) < 0: 

return self._parent.laurent_series_ring()(h) 

return construct_from_pari(self._parent, h) 

  

def list(self): 

""" 

Return the list of known coefficients for ``self``. 

  

This is just the list of coefficients of the underlying 

polynomial; it need not have length equal to ``self.prec()``. 

  

EXAMPLES:: 

  

sage: R.<t> = PowerSeriesRing(ZZ, implementation='pari') 

sage: f = 1 - 5*t^3 + t^5 + O(t^7) 

sage: f.list() 

[1, 0, 0, -5, 0, 1] 

  

sage: S.<u> = PowerSeriesRing(pAdicRing(5), implementation='pari') 

sage: (2 + u).list() 

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

  

""" 

cdef pari_gen g = self.g 

cdef long vn = get_var(self._parent.variable_name()) 

R = self.base_ring() 

if typ(g.g) == t_SER and varn(g.g) == vn: 

g = g.truncate() 

if typ(g.g) == t_POL and varn(g.g) == vn: 

# t_POL has 2 codewords. Use new_ref instead of g[i] for speed. 

return [R(g.new_ref(gel(g.g, i))) for i in range(2, lg(g.g))] 

else: 

return [R(g)] 

  

def padded_list(self, n=None): 

""" 

Return a list of coefficients of ``self`` up to (but not 

including) `q^n`. 

  

The list is padded with zeroes on the right so that it has 

length `n`. 

  

INPUT: 

  

- ``n`` -- a non-negative integer (optional); if `n` is not 

given, it will be taken to be the precision of ``self`, 

unless this is ``+Infinity``, in which case we just 

return ``self.list()`` 

  

EXAMPLES:: 

  

sage: R.<q> = PowerSeriesRing(QQ, implementation='pari') 

sage: f = 1 - 17*q + 13*q^2 + 10*q^4 + O(q^7) 

sage: f.list() 

[1, -17, 13, 0, 10] 

sage: f.padded_list(7) 

[1, -17, 13, 0, 10, 0, 0] 

sage: f.padded_list(10) 

[1, -17, 13, 0, 10, 0, 0, 0, 0, 0] 

sage: f.padded_list(3) 

[1, -17, 13] 

sage: f.padded_list() 

[1, -17, 13, 0, 10, 0, 0] 

sage: g = 1 - 17*q + 13*q^2 + 10*q^4 

sage: g.list() 

[1, -17, 13, 0, 10] 

sage: g.padded_list() 

[1, -17, 13, 0, 10] 

sage: g.padded_list(10) 

[1, -17, 13, 0, 10, 0, 0, 0, 0, 0] 

  

""" 

if n is None: 

if self._prec is infinity: 

return self.list() 

else: 

n = self._prec 

if not n: 

return [] 

  

cdef pari_gen g = self.g 

cdef long l, m 

  

R = self.base_ring() 

if typ(g.g) == t_POL and varn(g.g) == get_var(self._parent.variable_name()): 

l = lg(g.g) - 2 # t_POL has 2 codewords 

if n <= l: 

return [R(g.new_ref(gel(g.g, i + 2))) for i in range(n)] 

else: 

return ([R(g.new_ref(gel(g.g, i + 2))) for i in range(l)] 

+ [R.zero()] * (n - l)) 

elif typ(g.g) == t_SER and varn(g.g) == get_var(self._parent.variable_name()): 

l = lg(g.g) - 2 # t_SER has 2 codewords 

m = valp(g.g) 

if n <= m: 

return [R.zero()] * n 

elif n <= l + m: 

return ([R.zero()] * m 

+ [R(g.new_ref(gel(g.g, i + 2))) for i in range(n - m)]) 

else: 

return ([R.zero()] * m 

+ [R(g.new_ref(gel(g.g, i + 2))) for i in range(l)] 

+ [R.zero()] * (n - l - m)) 

else: 

return [R(g)] + [R.zero()] * (n - 1) 

  

def dict(self): 

""" 

Return a dictionary of coefficients for ``self``. 

  

This is simply a dict for the underlying polynomial; it need 

not have keys corresponding to every number smaller than 

``self.prec()``. 

  

EXAMPLES:: 

  

sage: R.<t> = PowerSeriesRing(ZZ, implementation='pari') 

sage: f = 1 + t^10 + O(t^12) 

sage: f.dict() 

{0: 1, 10: 1} 

  

""" 

return self.polynomial().dict() 

  

def _derivative(self, var=None): 

""" 

Return the derivative of ``self`` with respect to the 

variable ``var``. 

  

If ``var`` is ``None``, the variable of the power series ring 

is used. 

  

.. SEEALSO:: 

  

:meth:`derivative()` 

  

EXAMPLES:: 

  

sage: R.<w> = PowerSeriesRing(QQ, implementation='pari') 

sage: f = 2 + 3*w^2 + w^10 + O(w^100); f 

2 + 3*w^2 + w^10 + O(w^100) 

sage: f._derivative() 

6*w + 10*w^9 + O(w^99) 

sage: f._derivative(w) 

6*w + 10*w^9 + O(w^99) 

  

sage: R.<t> = PolynomialRing(ZZ) 

sage: S.<x> = PowerSeriesRing(R, implementation='pari') 

sage: f = t^3*x^4 + O(x^5) 

sage: f._derivative() 

4*t^3*x^3 + O(x^4) 

sage: f._derivative(x) 

4*t^3*x^3 + O(x^4) 

sage: f._derivative(t) 

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

  

""" 

if var is None: 

var = self._parent.variable_name() 

return construct_from_pari(self._parent, self.g.deriv(var)) 

  

def integral(self, var=None): 

""" 

Return the formal integral of ``self``. 

  

By default, the integration variable is the variable of the 

power series. Otherwise, the integration variable is the 

optional parameter ``var``. 

  

.. NOTE:: 

  

The integral is always chosen so the constant term is 0. 

  

EXAMPLES:: 

  

sage: k.<w> = PowerSeriesRing(QQ, implementation='pari') 

sage: (1+17*w+15*w^3+O(w^5)).integral() 

w + 17/2*w^2 + 15/4*w^4 + O(w^6) 

sage: (w^3 + 4*w^4 + O(w^7)).integral() 

1/4*w^4 + 4/5*w^5 + O(w^8) 

sage: (3*w^2).integral() 

w^3 

  

TESTS:: 

  

sage: t = PowerSeriesRing(QQ, 't', implementation='pari').gen() 

sage: f = t + 5*t^2 + 21*t^3 

sage: g = f.integral() ; g 

1/2*t^2 + 5/3*t^3 + 21/4*t^4 

sage: g.parent() 

Power Series Ring in t over Rational Field 

  

sage: R.<a> = QQ[] 

sage: t = PowerSeriesRing(R, 't', implementation='pari').gen() 

sage: f = a*t +5*t^2 

sage: f.integral() 

1/2*a*t^2 + 5/3*t^3 

sage: f.integral(a) 

1/2*a^2*t + 5*a*t^2 

  

""" 

if var is None: 

var = self._parent.variable_name() 

return construct_from_pari(self._parent, self.g.intformal(var)) 

  

def reverse(self, precision=None): 

r""" 

Return the reverse of ``self``. 

  

The reverse of a power series `f` is the power series `g` such 

that `g(f(x)) = x`. This exists if and only if the valuation 

of ``self`` is exactly 1 and the coefficient of `x` is a unit. 

  

If the optional argument ``precision`` is given, the reverse 

is returned with this precision. If ``f`` has infinite 

precision and the argument ``precision`` is not given, then 

the reverse is returned with the default precision of 

``f.parent()``. 

  

EXAMPLES:: 

  

sage: R.<x> = PowerSeriesRing(QQ, implementation='pari') 

sage: f = 2*x + 3*x^2 - x^4 + O(x^5) 

sage: g = f.reverse() 

sage: g 

1/2*x - 3/8*x^2 + 9/16*x^3 - 131/128*x^4 + O(x^5) 

sage: f(g) 

x + O(x^5) 

sage: g(f) 

x + O(x^5) 

  

sage: A.<t> = PowerSeriesRing(ZZ, implementation='pari') 

sage: a = t - t^2 - 2*t^4 + t^5 + O(t^6) 

sage: b = a.reverse(); b 

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

sage: a(b) 

t + O(t^6) 

sage: b(a) 

t + O(t^6) 

  

sage: B.<b,c> = PolynomialRing(ZZ) 

sage: A.<t> = PowerSeriesRing(B, implementation='pari') 

sage: f = t + b*t^2 + c*t^3 + O(t^4) 

sage: g = f.reverse(); g 

t - b*t^2 + (2*b^2 - c)*t^3 + O(t^4) 

sage: f(g) 

t + O(t^4) 

sage: g(f) 

t + O(t^4) 

  

sage: A.<t> = PowerSeriesRing(ZZ, implementation='pari') 

sage: B.<x> = PowerSeriesRing(A, implementation='pari') 

sage: f = (1 - 3*t + 4*t^3 + O(t^4))*x + (2 + t + t^2 + O(t^3))*x^2 + O(x^3) 

sage: g = f.reverse(); g 

(1 + 3*t + 9*t^2 + 23*t^3 + O(t^4))*x + (-2 - 19*t - 118*t^2 + O(t^3))*x^2 + O(x^3) 

  

The optional argument ``precision`` sets the precision of the output:: 

  

sage: R.<x> = PowerSeriesRing(QQ, implementation='pari') 

sage: f = 2*x + 3*x^2 - 7*x^3 + x^4 + O(x^5) 

sage: g = f.reverse(precision=3); g 

1/2*x - 3/8*x^2 + O(x^3) 

sage: f(g) 

x + O(x^3) 

sage: g(f) 

x + O(x^3) 

  

If the input series has infinite precision, the precision of the 

output is automatically set to the default precision of the parent 

ring:: 

  

sage: R.<x> = PowerSeriesRing(QQ, default_prec=20, implementation='pari') 

sage: (x - x^2).reverse() # get some Catalan numbers 

x + x^2 + 2*x^3 + 5*x^4 + 14*x^5 + 42*x^6 + 132*x^7 + 429*x^8 

+ 1430*x^9 + 4862*x^10 + 16796*x^11 + 58786*x^12 + 208012*x^13 

+ 742900*x^14 + 2674440*x^15 + 9694845*x^16 + 35357670*x^17 

+ 129644790*x^18 + 477638700*x^19 + O(x^20) 

sage: (x - x^2).reverse(precision=3) 

x + x^2 + O(x^3) 

  

TESTS:: 

  

sage: R.<x> = PowerSeriesRing(QQ, implementation='pari') 

sage: f = 1 + 2*x + 3*x^2 - x^4 + O(x^5) 

sage: f.reverse() 

Traceback (most recent call last): 

... 

PariError: domain error in serreverse: valuation != 1 

  

""" 

cdef PowerSeries_pari f 

if self._prec is infinity: 

if precision is None: 

precision = self._parent.default_prec() 

f = self.add_bigoh(precision) 

else: 

if precision is None: 

precision = self._prec 

f = self 

return PowerSeries_pari(self._parent, f.g.serreverse(), precision)