Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

645

646

647

648

649

650

651

652

653

654

655

656

657

658

659

660

661

662

663

664

665

666

667

668

669

670

671

672

673

674

675

676

677

678

679

680

681

682

683

684

685

686

687

688

689

690

691

692

693

694

695

696

697

698

699

700

701

702

703

704

705

706

707

708

709

710

711

712

713

714

715

716

717

718

719

720

721

722

723

724

725

726

727

728

729

730

731

732

733

734

735

736

737

738

739

740

741

742

743

744

745

746

747

748

749

750

751

752

753

754

755

756

757

758

759

760

761

762

763

764

765

766

767

768

769

770

771

772

773

774

775

776

777

778

779

780

781

782

783

784

785

786

787

788

789

790

791

792

793

794

795

796

797

798

799

800

801

802

803

804

805

806

807

808

809

810

811

812

813

814

815

816

817

818

819

820

821

822

823

824

825

826

827

828

829

830

831

832

833

834

835

836

837

838

839

840

841

842

843

844

845

846

847

848

849

850

851

852

853

854

855

856

857

858

859

860

861

862

863

864

865

866

867

868

869

870

871

872

873

874

875

876

877

878

879

880

881

882

883

884

885

886

887

888

889

890

891

892

893

894

895

896

897

898

899

900

901

902

903

904

905

906

907

908

909

910

911

912

913

914

915

916

917

918

919

920

921

922

923

924

925

926

927

928

929

930

931

932

933

934

935

936

937

938

939

940

941

942

943

944

945

946

947

948

949

950

951

952

953

954

955

956

957

958

959

960

961

962

963

964

965

966

967

968

969

970

971

972

973

974

975

976

977

978

979

980

981

982

983

984

985

986

987

988

989

990

991

992

993

994

995

996

997

998

999

1000

1001

1002

1003

1004

1005

1006

1007

1008

1009

1010

1011

1012

1013

1014

1015

1016

1017

1018

1019

1020

1021

1022

1023

1024

1025

1026

1027

1028

1029

1030

1031

1032

1033

1034

1035

1036

1037

1038

1039

1040

1041

1042

1043

1044

1045

1046

1047

1048

1049

1050

1051

1052

1053

1054

1055

1056

1057

1058

1059

1060

1061

1062

1063

1064

1065

1066

1067

1068

1069

1070

1071

1072

1073

1074

1075

1076

1077

1078

1079

1080

1081

1082

1083

1084

1085

1086

1087

1088

1089

1090

1091

1092

1093

1094

1095

1096

1097

1098

1099

1100

1101

1102

1103

1104

1105

1106

1107

1108

1109

1110

1111

1112

1113

1114

1115

1116

1117

1118

1119

1120

1121

1122

1123

1124

1125

1126

1127

1128

1129

1130

1131

1132

1133

1134

1135

1136

1137

1138

1139

1140

1141

1142

1143

1144

1145

1146

1147

1148

1149

1150

1151

1152

1153

1154

1155

1156

1157

1158

1159

1160

1161

1162

1163

1164

1165

1166

1167

1168

1169

1170

1171

1172

1173

1174

1175

1176

1177

1178

1179

1180

1181

1182

1183

1184

1185

1186

1187

1188

1189

1190

1191

1192

1193

1194

1195

1196

1197

1198

1199

1200

1201

1202

1203

1204

1205

1206

1207

1208

1209

1210

1211

1212

1213

1214

1215

1216

1217

1218

1219

1220

1221

1222

1223

1224

1225

1226

1227

1228

1229

1230

1231

1232

1233

1234

1235

1236

1237

1238

1239

1240

1241

1242

1243

1244

1245

1246

1247

1248

1249

1250

1251

1252

1253

1254

1255

1256

1257

1258

1259

1260

1261

1262

1263

1264

1265

1266

1267

1268

1269

1270

1271

1272

1273

1274

1275

1276

1277

1278

1279

1280

1281

1282

1283

1284

1285

1286

1287

1288

1289

1290

1291

1292

1293

1294

1295

1296

1297

1298

1299

1300

1301

1302

1303

1304

1305

1306

1307

1308

1309

1310

1311

1312

1313

1314

1315

1316

1317

1318

1319

1320

1321

1322

1323

1324

1325

1326

1327

1328

1329

1330

1331

1332

1333

1334

1335

1336

1337

1338

1339

1340

1341

1342

1343

1344

1345

1346

1347

1348

1349

1350

1351

1352

1353

1354

1355

1356

1357

1358

1359

1360

1361

1362

1363

1364

1365

1366

1367

1368

1369

1370

1371

1372

1373

1374

1375

1376

1377

1378

1379

1380

1381

1382

1383

1384

1385

1386

1387

1388

1389

1390

1391

1392

1393

1394

1395

1396

1397

1398

1399

1400

1401

1402

1403

1404

1405

1406

1407

1408

1409

1410

1411

1412

1413

1414

1415

1416

1417

1418

1419

1420

1421

1422

1423

1424

1425

1426

1427

1428

1429

1430

1431

1432

1433

1434

1435

1436

1437

1438

1439

1440

1441

1442

1443

1444

1445

1446

1447

1448

1449

1450

1451

1452

1453

1454

1455

1456

1457

1458

1459

1460

1461

1462

1463

1464

1465

1466

1467

1468

1469

1470

1471

1472

1473

1474

1475

1476

1477

1478

1479

1480

1481

1482

1483

1484

1485

1486

1487

1488

1489

1490

1491

1492

1493

1494

1495

1496

1497

1498

1499

1500

1501

1502

1503

1504

1505

1506

1507

1508

1509

1510

1511

1512

1513

1514

1515

1516

1517

1518

1519

1520

1521

1522

1523

1524

1525

1526

1527

1528

1529

1530

1531

1532

1533

1534

1535

1536

1537

1538

1539

1540

1541

1542

1543

1544

1545

1546

1547

1548

1549

1550

1551

1552

1553

1554

1555

1556

1557

1558

1559

1560

1561

1562

1563

1564

1565

1566

1567

1568

1569

1570

1571

1572

1573

1574

1575

1576

1577

1578

1579

1580

1581

1582

1583

1584

1585

1586

1587

1588

1589

1590

1591

1592

1593

1594

1595

1596

1597

1598

1599

1600

1601

1602

1603

1604

1605

1606

1607

1608

1609

1610

1611

1612

1613

1614

1615

1616

1617

1618

1619

1620

1621

1622

1623

1624

1625

1626

1627

1628

1629

1630

1631

1632

1633

1634

1635

1636

1637

1638

1639

1640

1641

1642

1643

1644

1645

1646

1647

1648

1649

1650

1651

1652

1653

1654

1655

1656

1657

1658

1659

1660

1661

1662

1663

1664

1665

1666

1667

1668

1669

1670

1671

1672

1673

1674

1675

1676

1677

1678

1679

1680

1681

1682

1683

1684

1685

1686

1687

1688

1689

1690

1691

1692

1693

1694

1695

1696

1697

1698

1699

1700

1701

1702

1703

1704

1705

1706

1707

1708

1709

1710

1711

1712

1713

1714

1715

1716

1717

1718

1719

1720

1721

1722

1723

1724

1725

1726

1727

1728

1729

1730

1731

1732

1733

1734

1735

1736

1737

1738

1739

1740

1741

1742

1743

1744

1745

1746

1747

1748

1749

1750

1751

1752

1753

1754

1755

1756

1757

1758

1759

1760

1761

1762

1763

1764

1765

1766

1767

1768

1769

1770

1771

1772

1773

1774

1775

1776

1777

1778

1779

1780

1781

1782

1783

1784

1785

1786

1787

1788

1789

1790

1791

1792

1793

1794

1795

1796

1797

1798

1799

1800

1801

1802

1803

1804

1805

1806

1807

1808

1809

1810

1811

1812

1813

1814

1815

1816

1817

1818

1819

1820

1821

1822

1823

1824

1825

1826

1827

1828

1829

1830

1831

1832

1833

1834

1835

1836

1837

1838

1839

1840

1841

1842

1843

1844

1845

1846

1847

1848

1849

1850

1851

1852

1853

1854

1855

1856

1857

1858

1859

1860

1861

1862

1863

1864

1865

1866

1867

1868

1869

1870

1871

1872

1873

1874

1875

1876

1877

1878

1879

1880

1881

1882

1883

1884

1885

1886

1887

1888

1889

1890

1891

1892

1893

1894

1895

1896

1897

1898

1899

1900

1901

1902

1903

1904

1905

1906

1907

1908

1909

1910

1911

1912

1913

1914

1915

1916

1917

1918

1919

1920

1921

1922

1923

1924

1925

1926

1927

1928

1929

1930

1931

1932

1933

1934

1935

1936

1937

1938

1939

1940

1941

1942

1943

1944

1945

1946

1947

1948

1949

1950

1951

1952

1953

1954

1955

1956

1957

1958

1959

1960

1961

1962

1963

1964

1965

1966

1967

1968

1969

1970

1971

1972

1973

1974

1975

1976

1977

1978

1979

1980

1981

1982

1983

1984

1985

1986

1987

1988

1989

1990

1991

1992

1993

1994

1995

1996

1997

1998

1999

2000

2001

2002

2003

2004

2005

2006

2007

2008

2009

2010

2011

2012

2013

2014

2015

2016

2017

2018

2019

2020

2021

2022

2023

2024

2025

2026

2027

2028

2029

2030

2031

2032

2033

2034

2035

2036

2037

2038

2039

2040

2041

2042

2043

2044

2045

2046

2047

2048

2049

2050

2051

2052

2053

2054

2055

2056

2057

2058

2059

2060

2061

2062

2063

2064

2065

2066

2067

2068

2069

2070

2071

2072

2073

2074

2075

2076

2077

2078

2079

2080

2081

2082

2083

2084

2085

2086

2087

2088

2089

2090

2091

2092

2093

2094

2095

2096

2097

2098

2099

2100

2101

2102

2103

2104

2105

2106

2107

2108

2109

2110

2111

2112

2113

2114

2115

2116

2117

2118

2119

2120

2121

2122

2123

2124

2125

2126

2127

2128

2129

2130

2131

2132

2133

2134

2135

2136

2137

2138

2139

2140

2141

2142

2143

2144

2145

2146

2147

2148

2149

2150

2151

2152

2153

2154

2155

2156

2157

2158

2159

2160

2161

2162

2163

2164

2165

2166

2167

2168

2169

2170

2171

2172

2173

2174

2175

2176

2177

2178

2179

2180

2181

2182

2183

2184

2185

2186

2187

2188

2189

2190

2191

2192

2193

2194

2195

2196

2197

2198

2199

2200

2201

2202

2203

2204

2205

2206

2207

2208

2209

2210

2211

2212

2213

2214

2215

2216

2217

2218

2219

2220

2221

2222

2223

2224

2225

2226

2227

2228

2229

2230

2231

2232

2233

2234

2235

2236

2237

2238

2239

2240

2241

2242

2243

2244

2245

2246

2247

2248

2249

2250

2251

2252

2253

2254

2255

2256

2257

2258

""" 

Power Series 

  

Sage provides an implementation of dense and sparse power series 

over any Sage base ring. This is the base class of the implementations 

of univariate and multivariate power series ring elements in Sage 

(see also :doc:`power_series_poly`, :doc:`multi_power_series_ring_element`). 

  

AUTHORS: 

  

- William Stein 

- David Harvey (2006-09-11): added solve_linear_de() method 

- Robert Bradshaw (2007-04): sqrt, rmul, lmul, shifting 

- Robert Bradshaw (2007-04): Cython version 

- Simon King (2012-08): use category and coercion framework, :trac:`13412` 

  

EXAMPLES:: 

  

sage: R.<x> = PowerSeriesRing(ZZ) 

sage: TestSuite(R).run() 

sage: R([1,2,3]) 

1 + 2*x + 3*x^2 

sage: R([1,2,3], 10) 

1 + 2*x + 3*x^2 + O(x^10) 

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

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

sage: f^10 

1 + 20*x + 180*x^2 + 930*x^3 + O(x^4) 

sage: g = 1/f; g 

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

sage: g * f 

1 + O(x^4) 

  

In Python (as opposed to Sage) create the power series ring and 

its generator as follows:: 

  

sage: R = PowerSeriesRing(ZZ, 'x') 

sage: x = R.gen() 

sage: parent(x) 

Power Series Ring in x over Integer Ring 

  

EXAMPLES: 

  

This example illustrates that coercion for power 

series rings is consistent with coercion for polynomial rings. 

  

:: 

  

sage: poly_ring1.<gen1> = PolynomialRing(QQ) 

sage: poly_ring2.<gen2> = PolynomialRing(QQ) 

sage: huge_ring.<x> = PolynomialRing(poly_ring1) 

  

The generator of the first ring gets coerced in as itself, since it 

is the base ring. 

  

:: 

  

sage: huge_ring(gen1) 

gen1 

  

The generator of the second ring gets mapped via the natural map 

sending one generator to the other. 

  

:: 

  

sage: huge_ring(gen2) 

x 

  

With power series the behavior is the same. 

  

:: 

  

sage: power_ring1.<gen1> = PowerSeriesRing(QQ) 

sage: power_ring2.<gen2> = PowerSeriesRing(QQ) 

sage: huge_power_ring.<x> = PowerSeriesRing(power_ring1) 

sage: huge_power_ring(gen1) 

gen1 

sage: huge_power_ring(gen2) 

x 

""" 

  

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

# Copyright (C) 2005 William Stein <wstein@gmail.com> 

# 2017 Vincent Delecroix <20100.delecroix@gmail.com> 

# 

# Distributed under the terms of the GNU General Public License (GPL) 

# 

# This code is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 

# General Public License for more details. 

# 

# The full text of the GPL is available at: 

# 

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

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

from __future__ import absolute_import 

  

import operator 

  

from .infinity import infinity, is_Infinite 

from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing 

import sage.rings.polynomial.polynomial_element 

import sage.misc.misc 

import sage.arith.all as arith 

import sage.misc.latex 

from . import rational_field 

from . import integer_ring 

from .integer import Integer 

from sage.rings.finite_rings.integer_mod_ring import IntegerModRing 

from sage.misc.superseded import deprecated_function_alias, deprecation 

from warnings import warn 

  

from sage.categories.fields import Fields 

_Fields = Fields() 

  

from sage.misc.derivative import multi_derivative 

  

Polynomial = sage.rings.polynomial.polynomial_element.Polynomial_generic_dense 

  

from sage.structure.element cimport AlgebraElement, RingElement, ModuleElement, Element 

from sage.structure.richcmp cimport richcmp 

  

  

def is_PowerSeries(x): 

""" 

Return True if ``x`` is an instance of a univariate 

or multivariate power series. 

 

EXAMPLES:: 

 

sage: R.<x> = PowerSeriesRing(ZZ) 

sage: from sage.rings.power_series_ring_element import is_PowerSeries 

sage: is_PowerSeries(1+x^2) 

True 

sage: is_PowerSeries(x-x) 

True 

sage: is_PowerSeries(0) 

False 

sage: var('x') 

x 

sage: is_PowerSeries(1+x^2) 

False 

""" 

return isinstance(x, PowerSeries) 

  

cdef class PowerSeries(AlgebraElement): 

""" 

A power series. Base class of univariate and 

multivariate power series. The following methods 

are available with both types of objects. 

""" 

def __init__(self, parent, prec, is_gen=False): 

""" 

Initialize a power series. Not for public use. 

It gets called by the ``PowerSeries_poly`` and 

``MPowerSeries`` constructors. 

 

EXAMPLES:: 

 

sage: PowerSeriesRing(CC, 'q') 

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

sage: T = PowerSeriesRing(GF(3),5,'t'); T 

Multivariate Power Series Ring in t0, t1, t2, t3, t4 over Finite 

Field of size 3 

""" 

AlgebraElement.__init__(self, parent) 

self.__is_gen = is_gen 

self._prec = prec 

  

def __hash__(self): 

""" 

Compute a hash of self. 

 

EXAMPLES:: 

 

sage: R.<x> = PowerSeriesRing(ZZ) 

sage: (1+x^2).__hash__() # random 

15360174650385709 

""" 

return hash(self.polynomial()) 

  

def __reduce__(self): 

""" 

EXAMPLES:: 

  

sage: K.<t> = PowerSeriesRing(QQ, 5) 

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

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

True 

""" 

return make_element_from_parent_v0, (self._parent, self.polynomial(), self.prec()) 

  

def is_sparse(self): 

""" 

EXAMPLES:: 

  

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

sage: t.is_sparse() 

False 

sage: R.<t> = PowerSeriesRing(ZZ, sparse=True) 

sage: t.is_sparse() 

True 

""" 

return self._parent.is_sparse() 

  

def is_dense(self): 

""" 

EXAMPLES:: 

  

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

sage: t.is_dense() 

True 

sage: R.<t> = PowerSeriesRing(ZZ, sparse=True) 

sage: t.is_dense() 

False 

""" 

return self._parent.is_dense() 

  

def is_gen(self): 

""" 

Return True if this is the generator (the variable) of the power 

series ring. 

  

EXAMPLES:: 

  

sage: R.<t> = QQ[[]] 

sage: t.is_gen() 

True 

sage: (1 + 2*t).is_gen() 

False 

  

Note that this only returns True on the actual generator, not on 

something that happens to be equal to it. 

  

:: 

  

sage: (1*t).is_gen() 

False 

sage: 1*t == t 

True 

""" 

return bool(self.__is_gen) 

  

def _im_gens_(self, codomain, im_gens): 

""" 

Return the image of this series under the map that sends the 

generators to ``im_gens``. This is used internally for computing 

homomorphisms. 

  

EXAMPLES:: 

  

sage: R.<t> = QQ[[]] 

sage: f = 1 + t + t^2 

sage: f._im_gens_(ZZ, [3]) 

13 

""" 

return codomain(self(im_gens[0])) 

  

cpdef base_extend(self, R): 

""" 

Return a copy of this power series but with coefficients in R. 

  

The following coercion uses ``base_extend`` implicitly:: 

  

sage: R.<t> = ZZ[['t']] 

sage: (t - t^2) * Mod(1, 3) 

t + 2*t^2 

""" 

S = self._parent.base_extend(R) 

return S(self) 

  

def change_ring(self, R): 

""" 

Change if possible the coefficients of self to lie in R. 

  

EXAMPLES:: 

  

sage: R.<T> = QQ[[]]; R 

Power Series Ring in T over Rational Field 

sage: f = 1 - 1/2*T + 1/3*T^2 + O(T^3) 

sage: f.base_extend(GF(5)) 

Traceback (most recent call last): 

... 

TypeError: no base extension defined 

sage: f.change_ring(GF(5)) 

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

sage: f.change_ring(GF(3)) 

Traceback (most recent call last): 

... 

ZeroDivisionError: Inverse does not exist. 

  

We can only change the ring if there is a ``__call__`` coercion 

defined. The following succeeds because ``ZZ(K(4))`` is defined. 

  

:: 

  

sage: K.<a> = NumberField(cyclotomic_polynomial(3), 'a') 

sage: R.<t> = K[['t']] 

sage: (4*t).change_ring(ZZ) 

4*t 

  

This does not succeed because ``ZZ(K(a+1))`` is not defined. 

  

:: 

  

sage: K.<a> = NumberField(cyclotomic_polynomial(3), 'a') 

sage: R.<t> = K[['t']] 

sage: ((a+1)*t).change_ring(ZZ) 

Traceback (most recent call last): 

... 

TypeError: Unable to coerce a + 1 to an integer 

""" 

S = self._parent.change_ring(R) 

return S(self) 

  

cpdef _richcmp_(self, right, int op): 

r""" 

Comparison of self and ``right``. 

  

We say two approximate power series are equal if they agree for 

all coefficients up to the *minimum* of the precisions of each. 

Thus, e.g., `f = 1 + q + O(q^2)` is equal to `g = 1 + O(q)`. 

  

This is how PARI defines equality of power series, but not how 

Magma defines equality. (Magma would declare `f` and `g` unequal.) 

The PARI/Sage convention is consistent with the idea that 

comparison should take place after coercing both elements into 

a common parent. Hence, in the above example `f` is truncated 

to `f + O(q)`, which is considered to be equal to `g`, even 

though the coefficients of `q` are unknown for both series in 

that comparison. 

  

Comparison is done in dictionary order from lowest degree to 

highest degree coefficients. This is different than polynomial 

comparison. 

  

EXAMPLES:: 

  

sage: R.<q> = ZZ[[ ]]; R 

Power Series Ring in q over Integer Ring 

sage: f=1+q+O(q^2); g = 1+O(q) 

sage: f == g 

True 

sage: 1 - 2*q + q^2 +O(q^3) == 1 - 2*q^2 + q^2 + O(q^4) 

False 

  

:: 

  

sage: R.<t> = ZZ[[]] 

sage: 1 + t^2 < 2 - t 

True 

sage: f = 1 + t + t^7 - 5*t^10 

sage: g = 1 + t + t^7 - 5*t^10 + O(t^15) 

sage: f == f 

True 

sage: f < g 

False 

sage: f == g 

True 

  

TESTS: 

  

:trac:`9457` is fixed:: 

  

sage: A.<t> = PowerSeriesRing(ZZ) 

sage: g = t + t^3 + t^5 + O(t^6); g 

t + t^3 + t^5 + O(t^6) 

sage: [g == g.add_bigoh(i) for i in range(7)] 

[True, True, True, True, True, True, True] 

sage: A(g.polynomial()) == g 

True 

  

sage: f = t + t^2 + O(t^10) 

sage: f == f.truncate() 

True 

""" 

prec = self.common_prec(right) 

x = self.list() 

y = right.list() 

if not (prec is infinity): 

x += [0]*(prec - len(x)) # self.list() does not include trailing zeroes 

x = x[:prec] # truncate x to common prec 

y += [0]*(prec - len(y)) 

y = y[:prec] 

return richcmp(x, y, op) 

  

def __call__(self, x): 

""" 

Implementations *MUST* override this in the derived class. 

 

EXAMPLES:: 

 

sage: R.<x> = PowerSeriesRing(ZZ) 

sage: PowerSeries.__call__(1+x^2,x) 

Traceback (most recent call last): 

... 

NotImplementedError  

""" 

raise NotImplementedError 

  

def coefficients(self): 

""" 

Return the nonzero coefficients of self. 

  

EXAMPLES:: 

  

sage: R.<t> = PowerSeriesRing(QQ) 

sage: f = t + t^2 - 10/3*t^3 

sage: f.coefficients() 

[1, 1, -10/3] 

""" 

zero = self.parent().base_ring().zero() 

return [c for c in self.list() if c != zero] 

  

def exponents(self): 

""" 

Return the exponents appearing in self with nonzero coefficients. 

  

EXAMPLES:: 

  

sage: R.<t> = PowerSeriesRing(QQ) 

sage: f = t + t^2 - 10/3*t^3 

sage: f.exponents() 

[1, 2, 3] 

""" 

zero = self.parent().base_ring().zero() 

l = self.list() 

return [i for i in range(len(l)) if l[i] != zero] 

  

def list(self): 

""" 

See this method in derived classes: 

 

- :meth:`sage.rings.power_series_poly.PowerSeries_poly.list`, 

 

- :meth:`sage.rings.multi_power_series_ring_element.MPowerSeries.list` 

 

Implementations *MUST* override this in the derived class. 

 

EXAMPLES:: 

  

sage: R.<x> = PowerSeriesRing(ZZ) 

sage: PowerSeries.list(1+x^2) 

Traceback (most recent call last): 

... 

NotImplementedError  

""" 

raise NotImplementedError 

  

def polynomial(self): 

""" 

See this method in derived classes: 

 

- :meth:`sage.rings.power_series_poly.PowerSeries_poly.polynomial`, 

 

- :meth:`sage.rings.multi_power_series_ring_element.MPowerSeries.polynomial` 

  

Implementations *MUST* override this in the derived class. 

 

EXAMPLES:: 

  

sage: R.<x> = PowerSeriesRing(ZZ) 

sage: PowerSeries.polynomial(1+x^2) 

Traceback (most recent call last): 

... 

NotImplementedError  

""" 

raise NotImplementedError 

  

def __copy__(self): 

""" 

Return this power series. Power series are immutable so copy can 

safely just return the same polynomial. 

  

EXAMPLES:: 

  

sage: R.<q> = ZZ[[ ]]; R 

Power Series Ring in q over Integer Ring 

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

sage: copy(f) is f # !!! ok since power series are immutable. 

True 

""" 

return self 

  

def base_ring(self): 

""" 

Return the base ring that this power series is defined over. 

  

EXAMPLES:: 

  

sage: R.<t> = GF(49,'alpha')[[]] 

sage: (t^2 + O(t^3)).base_ring() 

Finite Field in alpha of size 7^2 

""" 

return self._parent.base_ring() 

  

def padded_list(self, n=None): 

""" 

Return a list of coefficients of self up to (but not including) 

`q^n`. 

  

Includes 0's in the list on the right so that the list has length 

`n`. 

  

INPUT: 

  

  

- ``n`` - (optional) an integer that is at least 0. 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) 

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 n < 0: 

raise ValueError("n must be at least 0") 

v = self.list() 

if len(v) < n: 

z = self._parent.base_ring()(0) 

return v + [z]*(n - len(v)) 

else: 

return v[:int(n)] 

  

def prec(self): 

""" 

The precision of `...+O(x^r)` is by definition 

`r`. 

  

EXAMPLES:: 

  

sage: R.<t> = ZZ[[]] 

sage: (t^2 + O(t^3)).prec() 

3 

sage: (1 - t^2 + O(t^100)).prec() 

100 

""" 

return self._prec 

  

def precision_absolute(self): 

""" 

Return the absolute precision of this series. 

  

By definition, the absolute precision of 

`...+O(x^r)` is `r`. 

  

EXAMPLES:: 

  

sage: R.<t> = ZZ[[]] 

sage: (t^2 + O(t^3)).precision_absolute() 

3 

sage: (1 - t^2 + O(t^100)).precision_absolute() 

100 

""" 

return self.prec() 

  

def precision_relative(self): 

""" 

Return the relative precision of this series, that 

is the difference between its absolute precision 

and its valuation. 

  

By convention, the relative precision of `0` (or 

`O(x^r)` for any `r`) is `0`. 

  

EXAMPLES:: 

  

sage: R.<t> = ZZ[[]] 

sage: (t^2 + O(t^3)).precision_relative() 

1 

sage: (1 - t^2 + O(t^100)).precision_relative() 

100 

sage: O(t^4).precision_relative() 

0 

""" 

if self.is_zero(): 

return 0 

else: 

return self.prec() - self.valuation() 

  

def _repr_(self): 

""" 

Return the string representation of this power series. 

  

EXAMPLES:: 

  

sage: R.<t> = ZZ[[]] 

sage: (t^2 + O(t^3))._repr_() 

't^2 + O(t^3)' 

  

:: 

  

sage: R.<t> = QQ[[]] 

sage: 1 / (1+2*t +O(t^5)) 

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

  

:: 

  

sage: R.<t> = PowerSeriesRing(QQ, sparse=True) 

sage: 1 / (1+2*t +O(t^5)) 

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

sage: -13/2 * t^3 + 5*t^5 + O(t^10) 

-13/2*t^3 + 5*t^5 + O(t^10) 

""" 

if self.is_zero(): 

if self.prec() is infinity: 

return "0" 

else: 

return "O(%s^%s)"%(self._parent.variable_name(),self.prec()) 

  

atomic_repr = self._parent.base_ring()._repr_option('element_is_atomic') 

X = self._parent.variable_name() 

  

s = " " 

if self.is_sparse(): 

f = self.polynomial() 

m = f.degree() + 1 

d = f._dict_unsafe() 

coeffs = sorted(d.items()) 

for (n, x) in coeffs: 

x = repr(x) 

if x != '0': 

if s != ' ': 

s += " + " 

if not atomic_repr and n > 0 and (x.find("+") != -1 or x.find("-") != -1): 

x = "(%s)"%x 

if n > 1: 

var = "*%s^%s"%(X,n) 

elif n==1: 

var = "*%s"%X 

else: 

var = "" 

s += "%s%s"%(x,var) 

else: 

v = self.list() 

m = len(v) 

first = True 

for n in xrange(m): 

x = v[n] 

x = repr(x) 

if x != '0': 

if not first: 

s += " + " 

if not atomic_repr and n > 0 and (x[1:].find("+") != -1 or x[1:].find("-") != -1): 

x = "(%s)"%x 

if n > 1: 

var = "*%s^%s"%(X,n) 

elif n==1: 

var = "*%s"%X 

else: 

var = "" 

s += "%s%s"%(x,var) 

first = False 

# end 

  

s = s.replace(" + -", " - ") 

s = s.replace(" 1*"," ") 

s = s.replace(" -1*", " -") 

if not (self._prec is infinity): 

if self._prec == 0: 

bigoh = "O(1)" 

elif self._prec == 1: 

bigoh = "O(%s)"%self._parent.variable_name() 

else: 

bigoh = "O(%s^%s)"%(self._parent.variable_name(),self._prec) 

if s==" ": 

return bigoh 

s += " + %s"%bigoh 

return s[1:] 

  

def _latex_(self): 

r""" 

Return the latex representation of this power series. 

  

EXAMPLES:: 

  

sage: R.<t> = QQ[[]] 

sage: f = -1/2 * t + 2/3*t^2 - 9/7 * t^15 + O(t^20); f 

-1/2*t + 2/3*t^2 - 9/7*t^15 + O(t^20) 

sage: latex(f) 

-\frac{1}{2}t + \frac{2}{3}t^{2} - \frac{9}{7}t^{15} + O(t^{20}) 

""" 

if self.is_zero(): 

if self.prec() is infinity: 

return "0" 

else: 

return "0 + \\cdots" 

s = " " 

v = self.list() 

m = len(v) 

X = self._parent.latex_variable_names()[0] 

atomic_repr = self._parent.base_ring()._repr_option('element_is_atomic') 

first = True 

for n in xrange(m): 

x = v[n] 

x = sage.misc.latex.latex(x) 

if x != '0': 

if not first: 

s += " + " 

if not atomic_repr and n > 0 and (x[1:].find("+") != -1 or x[1:].find("-") != -1): 

x = "\\left(%s\\right)"%x 

if n > 1: 

var = "%s^{%s}"%(X,n) 

elif n==1: 

var = "%s"%X 

else: 

var = "" 

if n > 0: 

s += "%s|%s"%(x,var) 

else: 

s += repr(x) 

first = False 

  

s = s.replace(" + -", " - ") 

s = s.replace(" -1|", " -") 

s = s.replace(" 1|"," ") 

s = s.replace("|","") 

if not (self._prec is infinity): 

if self._prec == 0: 

bigoh = "O(1)" 

elif self._prec == 1: 

bigoh = "O(%s)"%(X,) 

else: 

bigoh = "O(%s^{%s})"%(X,self._prec) 

if s == " ": 

return bigoh 

s += " + %s"%bigoh 

return s[1:] 

  

  

def truncate(self, prec=infinity): 

""" 

The polynomial obtained from power series by truncation. 

  

EXAMPLES:: 

  

sage: R.<I> = GF(2)[[]] 

sage: f = 1/(1+I+O(I^8)); f 

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

sage: f.truncate(5) 

I^4 + I^3 + I^2 + I + 1 

""" 

if prec is infinity: 

prec = self._prec 

a = self.list() 

v = [a[i] for i in range(min(prec, len(a)))] 

return self._parent._poly_ring()(v) 

  

cdef _inplace_truncate(self, long prec): 

return self.truncate(prec) 

  

def add_bigoh(self, prec): 

r""" 

Return the power series of precision at most ``prec`` got by adding 

`O(q^\text{prec})` to `f`, where `q` is the variable. 

  

EXAMPLES:: 

  

sage: R.<A> = RDF[[]] 

sage: f = (1+A+O(A^5))^5; f 

1.0 + 5.0*A + 10.0*A^2 + 10.0*A^3 + 5.0*A^4 + O(A^5) 

sage: f.add_bigoh(3) 

1.0 + 5.0*A + 10.0*A^2 + O(A^3) 

sage: f.add_bigoh(5) 

1.0 + 5.0*A + 10.0*A^2 + 10.0*A^3 + 5.0*A^4 + O(A^5) 

""" 

if prec is infinity or prec > self.prec(): 

return self 

a = self.list() 

v = [a[i] for i in range(min(prec, len(a)))] 

return self._parent(v, prec) 

  

def __getitem__(self,n): 

r""" 

Return the coefficient of `t^n` in this power series, where 

`t` is the indeterminate of the power series ring. 

  

If `n` is negative return 0. If `n` is beyond the precision, raise an 

IndexError. 

  

EXAMPLES:: 

  

sage: R.<m> = CDF[[]] 

sage: f = CDF(pi)^2 + m^3 + CDF(e)*m^4 + O(m^10); f # abs tol 5e-16 

9.869604401089358 + 0.0*m + 0.0*m^2 + 1.0*m^3 + 2.718281828459045*m^4 + O(m^10) 

sage: f[-5] 

0.0 

sage: f[0] 

9.869604401089358 

sage: f[4] # abs tol 5e-16 

2.718281828459045 

sage: f[9] 

0.0 

sage: f[10] 

Traceback (most recent call last): 

... 

IndexError: coefficient not known 

sage: f[1000] 

Traceback (most recent call last): 

... 

IndexError: coefficient not known 

""" 

if n<0: 

return self.base_ring()(0) 

c = self.list() 

if n >= len(c): 

if self._prec > n: 

return self.base_ring()(0) 

else: 

raise IndexError("coefficient not known") 

return c[n] 

  

def common_prec(self, f): 

r""" 

Return minimum precision of `f` and ``self``. 

  

EXAMPLES:: 

  

sage: R.<t> = PowerSeriesRing(QQ) 

  

:: 

  

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

sage: g = t + t^3 + t^4 + O(t^4) 

sage: f.common_prec(g) 

3 

sage: g.common_prec(f) 

3 

  

:: 

  

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

sage: g = t^2 

sage: f.common_prec(g) 

3 

sage: g.common_prec(f) 

3 

  

:: 

  

sage: f = t + t^2 

sage: f = t^2 

sage: f.common_prec(g) 

+Infinity 

""" 

if self.prec() is infinity: 

return f.prec() 

elif f.prec() is infinity: 

return self.prec() 

return min(self.prec(), f.prec()) 

  

cdef common_prec_c(self, PowerSeries f): 

if self._prec is infinity: 

return f._prec 

elif f._prec is infinity: 

return self._prec 

elif self._prec < f._prec: 

return self._prec 

else: 

return f._prec 

  

def _mul_prec(self, RingElement right_r): 

cdef PowerSeries right = <PowerSeries>right_r 

sp = self._prec 

rp = right._prec 

if sp is infinity: 

if rp is infinity: 

prec = infinity 

else: 

prec = rp + self.valuation() 

else: # sp != infinity 

if rp is infinity: 

prec = sp + right.valuation() 

else: 

prec = min(rp + self.valuation(), sp + right.valuation()) 

# endif 

return prec 

  

def __nonzero__(self): 

""" 

Return True if this power series is not equal to 0. 

  

EXAMPLES:: 

  

sage: R.<q> = ZZ[[ ]]; R 

Power Series Ring in q over Integer Ring 

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

sage: f.is_zero() 

False 

sage: (0 + O(q^2)).is_zero() 

True 

sage: R(0).is_zero() 

True 

sage: (0 + O(q^1000)).is_zero() 

True 

""" 

return not not self.polynomial() 

  

def is_unit(self): 

""" 

Return True if this power series is invertible. 

 

A power series is invertible precisely when the 

constant term is invertible. 

  

EXAMPLES:: 

  

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

sage: (-1 + t - t^5).is_unit() 

True 

sage: (3 + t - t^5).is_unit() 

False 

  

AUTHORS: 

  

- David Harvey (2006-09-03) 

""" 

return self[0].is_unit() 

  

def inverse(self): 

""" 

Return the inverse of self, i.e., self^(-1). 

  

EXAMPLES:: 

  

sage: R.<t> = PowerSeriesRing(QQ, sparse=True) 

sage: t.inverse() 

t^-1 

sage: type(_) 

<type 'sage.rings.laurent_series_ring_element.LaurentSeries'> 

sage: (1-t).inverse() 

1 + t + t^2 + t^3 + t^4 + t^5 + t^6 + t^7 + t^8 + ... 

""" 

return ~self 

  

def valuation_zero_part(self): 

r""" 

Factor self as `q^n \cdot (a_0 + a_1 q + \cdots)` with 

`a_0` nonzero. Then this function returns 

`a_0 + a_1 q + \cdots` . 

  

.. NOTE:: 

  

This valuation zero part need not be a unit if, e.g., 

`a_0` is not invertible in the base ring. 

  

EXAMPLES:: 

  

sage: R.<t> = PowerSeriesRing(QQ) 

sage: ((1/3)*t^5*(17-2/3*t^3)).valuation_zero_part() 

17/3 - 2/9*t^3 

  

In this example the valuation 0 part is not a unit:: 

  

sage: R.<t> = PowerSeriesRing(ZZ, sparse=True) 

sage: u = (-2*t^5*(17-t^3)).valuation_zero_part(); u 

-34 + 2*t^3 

sage: u.is_unit() 

False 

sage: u.valuation() 

0 

""" 

if self.is_zero(): 

raise ValueError("power series has no valuation 0 part") 

n = self.valuation() 

if n == 0: 

return self 

elif self.is_dense(): 

v = self.list()[int(n):] 

else: 

n = int(n) 

v = {} 

for k, x in self.dict().iteritems(): 

if k >= n: 

v[k-n] = x 

return self._parent(v, self.prec()-n) 

  

cpdef _div_(self, denom_r): 

""" 

EXAMPLES:: 

  

sage: k.<t> = QQ[[]] 

sage: t/t 

1 

sage: (t/(t^3 + 1)) * (t^3 + 1) 

t + O(t^21) 

sage: (t^5/(t^2 - 2)) * (t^2 -2 ) 

t^5 + O(t^25) 

""" 

denom = <PowerSeries>denom_r 

if denom.is_zero(): 

raise ZeroDivisionError("Can't divide by something indistinguishable from 0") 

u = denom.valuation_zero_part() 

inv = ~u # inverse 

  

v = denom.valuation() 

if v > self.valuation(): 

R = self._parent.laurent_series_ring() 

return R(self)/R(denom) 

  

# Algorithm: Cancel common factors of q from top and bottom, 

# then invert the denominator. We do the cancellation first 

# because we can only invert a unit (and remain in the ring 

# of power series). 

  

if v > 0: 

num = self >> v 

else: 

num = self 

return num*inv 

  

def __mod__(self, other): 

""" 

EXAMPLES:: 

  

sage: R.<T> = Qp(7)[[]] 

sage: f = (48*67 + 46*67^2)*T + (1 + 42*67 + 5*67^3)*T^2 + O(T^3) 

sage: f % 67 

T^2 + O(T^3) 

""" 

from sage.rings.power_series_ring import PowerSeriesRing 

if isinstance(other, (int, Integer, long)): 

return PowerSeriesRing(IntegerModRing(other), self.variable())(self) 

raise NotImplementedError("Mod on power series ring elements not defined except modulo an integer.") 

  

def shift(self, n): 

r""" 

Return this power series multiplied by the power `t^n`. If 

`n` is negative, terms below `t^n` will be 

discarded. Does not change this power series. 

  

.. NOTE:: 

  

Despite the fact that higher order terms are printed to the 

right in a power series, right shifting decreases the 

powers of `t`, while left shifting increases 

them. This is to be consistent with polynomials, integers, 

etc. 

  

EXAMPLES:: 

  

sage: R.<t> = PowerSeriesRing(QQ['y'], 't', 5) 

sage: f = ~(1+t); f 

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

sage: f.shift(3) 

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

sage: f >> 2 

1 - t + t^2 + O(t^3) 

sage: f << 10 

t^10 - t^11 + t^12 - t^13 + t^14 + O(t^15) 

sage: t << 29 

t^30 

  

AUTHORS: 

  

- Robert Bradshaw (2007-04-18) 

""" 

return self._parent(self.polynomial().shift(n), self._prec + n) 

  

def __lshift__(self, n): 

return self.parent()(self.polynomial() << n, self.prec()) 

  

def __rshift__(self, n): 

return self.parent()(self.polynomial() >> n, self.prec()) 

  

def is_monomial(self): 

""" 

Return True if this element is a monomial. That is, if self is 

`x^n` for some non-negative integer `n`. 

  

EXAMPLES:: 

  

sage: k.<z> = PowerSeriesRing(QQ, 'z') 

sage: z.is_monomial() 

True 

sage: k(1).is_monomial() 

True 

sage: (z+1).is_monomial() 

False 

sage: (z^2909).is_monomial() 

True 

sage: (3*z^2909).is_monomial() 

False 

""" 

  

return self.polynomial().is_monomial() 

  

def is_square(self): 

""" 

Return True if this function has a square root in this ring, e.g., 

there is an element `y` in ``self.parent()`` 

such that `y^2` equals ``self``. 

  

ALGORITHM: If the base ring is a field, this is true whenever the 

power series has even valuation and the leading coefficient is a 

perfect square. 

  

For an integral domain, it attempts the square root in the 

fraction field and tests whether or not the result lies in the 

original ring. 

  

EXAMPLES:: 

  

sage: K.<t> = PowerSeriesRing(QQ, 't', 5) 

sage: (1+t).is_square() 

True 

sage: (2+t).is_square() 

False 

sage: (2+t.change_ring(RR)).is_square() 

True 

sage: t.is_square() 

False 

sage: K.<t> = PowerSeriesRing(ZZ, 't', 5) 

sage: (1+t).is_square() 

False 

sage: f = (1+t)^100 

sage: f.is_square() 

True 

""" 

val = self.valuation() 

if val is not infinity and val % 2 == 1: 

return False 

elif not self[val].is_square(): 

return False 

elif self.base_ring() in _Fields: 

return True 

else: 

try: 

self.parent()(self.sqrt()) 

return True 

except TypeError: 

return False 

  

def sqrt(self, prec=None, extend=False, all=False, name=None): 

r""" 

Return a square root of self. 

  

INPUT: 

  

- ``prec`` - integer (default: None): if not None and the series 

has infinite precision, truncates series at precision 

prec. 

  

- ``extend`` - bool (default: False); if True, return a square 

root in an extension ring, if necessary. Otherwise, raise 

a ValueError if the square root is not in the base power series 

ring. For example, if ``extend`` is True the square root of a 

power series with odd degree leading coefficient is 

defined as an element of a formal extension ring. 

  

- ``name`` - string; if ``extend`` is True, you must also specify the print 

name of the formal square root. 

  

- ``all`` - bool (default: False); if True, return all square 

roots of self, instead of just one. 

  

ALGORITHM: Newton's method 

  

.. MATH:: 

  

x_{i+1} = \frac{1}{2}( x_i + \mathrm{self}/x_i ) 

  

EXAMPLES:: 

  

sage: K.<t> = PowerSeriesRing(QQ, 't', 5) 

sage: sqrt(t^2) 

t 

sage: sqrt(1+t) 

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

sage: sqrt(4+t) 

2 + 1/4*t - 1/64*t^2 + 1/512*t^3 - 5/16384*t^4 + O(t^5) 

sage: u = sqrt(2+t, prec=2, extend=True, name = 'alpha'); u 

alpha 

sage: u^2 

2 + t 

sage: u.parent() 

Univariate Quotient Polynomial Ring in alpha over Power Series Ring in t over Rational Field with modulus x^2 - 2 - t 

sage: K.<t> = PowerSeriesRing(QQ, 't', 50) 

sage: sqrt(1+2*t+t^2) 

1 + t 

sage: sqrt(t^2 +2*t^4 + t^6) 

t + t^3 

sage: sqrt(1 + t + t^2 + 7*t^3)^2 

1 + t + t^2 + 7*t^3 + O(t^50) 

sage: sqrt(K(0)) 

0 

sage: sqrt(t^2) 

t 

  

:: 

  

sage: K.<t> = PowerSeriesRing(CDF, 5) 

sage: v = sqrt(-1 + t + t^3, all=True); v 

[1.0*I - 0.5*I*t - 0.125*I*t^2 - 0.5625*I*t^3 - 0.2890625*I*t^4 + O(t^5), 

-1.0*I + 0.5*I*t + 0.125*I*t^2 + 0.5625*I*t^3 + 0.2890625*I*t^4 + O(t^5)] 

sage: [a^2 for a in v] 

[-1.0 + 1.0*t + 0.0*t^2 + 1.0*t^3 + O(t^5), -1.0 + 1.0*t + 0.0*t^2 + 1.0*t^3 + O(t^5)] 

  

A formal square root:: 

  

sage: K.<t> = PowerSeriesRing(QQ, 5) 

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

sage: s = f.sqrt(extend=True, name='sqrtf'); s 

sqrtf 

sage: s^2 

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

sage: parent(s) 

Univariate Quotient Polynomial Ring in sqrtf over Power Series Ring in t over Rational Field with modulus x^2 - 2*t - t^3 + O(t^4) 

  

TESTS:: 

  

sage: R.<x> = QQ[[]] 

sage: (x^10/2).sqrt() 

Traceback (most recent call last): 

... 

ValueError: unable to take the square root of 1/2 

  

AUTHORS: 

  

- Robert Bradshaw 

  

- William Stein 

""" 

if self.is_zero(): 

ans = self._parent(0).O(self.prec()/2) 

if all: 

return [ans] 

else: 

return ans 

  

if all and not self.base_ring().is_integral_domain(): 

raise NotImplementedError('all roots not implemented over a non-integral domain') 

  

formal_sqrt = False 

u = self.valuation_zero_part() 

# TODO, fix underlying element sqrt() 

try: 

try: 

s = u[0].sqrt(extend=False) 

except TypeError: 

s = u[0].sqrt() 

except ValueError: 

formal_sqrt = True 

if self.degree() == 0: 

if not formal_sqrt: 

a = self.parent()([s], self.prec()) 

if all: 

return [a, -a] 

else: 

return a 

  

val = self.valuation() 

  

if formal_sqrt or val % 2 == 1: 

if extend: 

if name is None: 

raise ValueError("the square root generates an extension, so you must specify the name of the square root") 

R = self._parent['x'] 

S = R.quotient(R([-self,0,1]), names=name) 

a = S.gen() 

if all: 

if not self.base_ring().is_integral_domain(): 

raise NotImplementedError('all roots not implemented over a non-integral domain') 

return [a, -a] 

else: 

return a 

elif formal_sqrt: 

raise ValueError("unable to take the square root of %s" % u[0]) 

else: 

raise ValueError("power series does not have a square root since it has odd valuation.") 

  

  

pr = self.prec() 

if pr == infinity: 

test_exact = True 

if prec is None: 

pr = self._parent.default_prec() 

else: 

pr = prec 

else: 

test_exact = False 

prec = pr 

  

R = s.parent() 

a = self.valuation_zero_part() 

P = self._parent 

if not R is P.base_ring(): 

a = a.change_ring(R) 

half = ~R(2) 

  

s = a.parent()([s]) 

for cur_prec in sage.misc.misc.newton_method_sizes(prec)[1:]: 

(<PowerSeries>s)._prec = cur_prec 

s = half * (s + a/s) 

  

ans = s 

if val != 0: 

ans *= P.gen(0) ** (val // 2) 

if test_exact and ans.degree() < prec/2: 

if ans*ans == self: 

(<PowerSeries>ans)._prec = infinity 

  

if all: 

return [ans, -ans] # since over an integral domain 

else: 

return ans 

  

def square_root(self): 

""" 

Return the square root of self in this ring. If this cannot be done 

then an error will be raised. 

  

This function succeeds if and only if 

``self``. :meth:`.is_square` 

  

EXAMPLES:: 

  

sage: K.<t> = PowerSeriesRing(QQ, 't', 5) 

sage: (1+t).square_root() 

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

sage: (2+t).square_root() 

Traceback (most recent call last): 

... 

ValueError: Square root does not live in this ring. 

sage: (2+t.change_ring(RR)).square_root() 

1.41421356237309 + 0.353553390593274*t - 0.0441941738241592*t^2 + 0.0110485434560398*t^3 - 0.00345266983001244*t^4 + O(t^5) 

sage: t.square_root() 

Traceback (most recent call last): 

... 

ValueError: Square root not defined for power series of odd valuation. 

sage: K.<t> = PowerSeriesRing(ZZ, 't', 5) 

sage: f = (1+t)^20 

sage: f.square_root() 

1 + 10*t + 45*t^2 + 120*t^3 + 210*t^4 + O(t^5) 

sage: f = 1+t 

sage: f.square_root() 

Traceback (most recent call last): 

... 

ValueError: Square root does not live in this ring. 

  

AUTHORS: 

  

- Robert Bradshaw 

""" 

val = self.valuation() 

if val is not infinity and val % 2 == 1: 

raise ValueError("Square root not defined for power series of odd valuation.") 

elif not self[val].is_square(): 

raise ValueError("Square root does not live in this ring.") 

elif self.base_ring() in _Fields: 

return self.sqrt() 

else: 

try: 

return self.parent()(self.sqrt()) 

except TypeError: 

raise ValueError("Square root does not live in this ring.") 

  

def nth_root(self, n, prec=None): 

r""" 

Return the ``n``-th root of this power series. 

  

INPUT: 

  

- ``n`` -- integer 

  

- ``prec`` -- integer (optional) - precision of the result. Though, if 

this series has finite precision, then the result can not have larger 

precision. 

  

EXAMPLES:: 

  

sage: R.<x> = QQ[[]] 

sage: (1+x).nth_root(5) 

1 + 1/5*x - 2/25*x^2 + ... + 12039376311816/2384185791015625*x^19 + O(x^20) 

  

sage: (1 + x + O(x^5)).nth_root(5) 

1 + 1/5*x - 2/25*x^2 + 6/125*x^3 - 21/625*x^4 + O(x^5) 

  

Check that the results are consistent with taking log and exponential:: 

  

sage: R.<x> = PowerSeriesRing(QQ, default_prec=100) 

sage: p = (1 + 2*x - x^4)**200 

sage: p1 = p.nth_root(1000, prec=100) 

sage: p2 = (p.log()/1000).exp() 

sage: p1.prec() == p2.prec() == 100 

True 

sage: p1.polynomial() == p2.polynomial() 

True 

  

Positive characteristic:: 

  

sage: R.<u> = GF(3)[[]] 

sage: p = 1 + 2 * u^2 

sage: p.nth_root(4) 

1 + 2*u^2 + u^6 + 2*u^8 + u^12 + 2*u^14 + O(u^20) 

sage: p.nth_root(4)**4 

1 + 2*u^2 + O(u^20) 

""" 

if prec is None: 

prec = self.prec() 

if prec == infinity: 

prec = self.parent().default_prec() 

else: 

prec = min(self.prec(), prec) 

  

p = self.polynomial() 

q = p._nth_root_series(n, prec) 

return self.parent()(q) + self.parent()(0).O(prec) 

  

def cos(self, prec=infinity): 

r""" 

Apply cos to the formal power series. 

  

INPUT: 

  

- ``prec`` -- Integer or ``infinity``. The degree to truncate 

the result to. 

  

OUTPUT: 

  

A new power series. 

  

EXAMPLES: 

  

For one variable:: 

  

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

sage: f = (t + t**2).O(4) 

sage: cos(f) 

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

  

For several variables:: 

  

sage: T.<a,b> = PowerSeriesRing(ZZ,2) 

sage: f = a + b + a*b + T.O(3) 

sage: cos(f) 

1 - 1/2*a^2 - a*b - 1/2*b^2 + O(a, b)^3 

sage: f.cos() 

1 - 1/2*a^2 - a*b - 1/2*b^2 + O(a, b)^3 

sage: f.cos(prec=2) 

1 + O(a, b)^2 

  

If the power series has a non-zero constant coefficient `c`, 

one raises an error:: 

  

sage: g = 2+f 

sage: cos(g) 

Traceback (most recent call last): 

... 

ValueError: Can only apply cos to formal power series with zero constant term. 

  

If no precision is specified, the default precision is used:: 

  

sage: T.default_prec() 

12 

sage: cos(a) 

1 - 1/2*a^2 + 1/24*a^4 - 1/720*a^6 + 1/40320*a^8 - 1/3628800*a^10 + O(a, b)^12 

sage: a.cos(prec=5) 

1 - 1/2*a^2 + 1/24*a^4 + O(a, b)^5 

sage: cos(a + T.O(5)) 

1 - 1/2*a^2 + 1/24*a^4 + O(a, b)^5 

  

TESTS:: 

  

sage: cos(a^2 + T.O(5)) 

1 - 1/2*a^4 + O(a, b)^5 

""" 

R = self.parent() 

  

c = self[0] 

if not c.is_zero(): 

raise ValueError('Can only apply cos to formal power ' 

'series with zero constant term.') 

x = self 

val = x.valuation() 

assert(val >= 1) 

  

prec = min(prec, self.prec()) 

if is_Infinite(prec): 

prec = R.default_prec() 

n_inv_factorial = R.base_ring().one() 

x_pow_n = R.one() 

x2 = x ** 2 

cos_x = R.one().add_bigoh(prec) 

for n in range(2, prec // val + 1, 2): 

x_pow_n = (x_pow_n * x2).add_bigoh(prec) 

n_inv_factorial /= - n * (n - 1) 

cos_x += x_pow_n * n_inv_factorial 

result_bg = cos_x 

  

if result_bg.base_ring() is not self.base_ring(): 

R = R.change_ring(self.base_ring().fraction_field()) 

return R(result_bg, prec=prec) 

  

def sin(self, prec=infinity): 

r""" 

Apply sin to the formal power series. 

  

INPUT: 

  

- ``prec`` -- Integer or ``infinity``. The degree to truncate 

the result to. 

  

OUTPUT: 

  

A new power series. 

  

EXAMPLES: 

  

For one variable:: 

  

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

sage: f = (t + t**2).O(4) 

sage: sin(f) 

t + t^2 - 1/6*t^3 + O(t^4) 

  

For several variables:: 

  

sage: T.<a,b> = PowerSeriesRing(ZZ,2) 

sage: f = a + b + a*b + T.O(3) 

sage: sin(f) 

a + b + a*b + O(a, b)^3 

sage: f.sin() 

a + b + a*b + O(a, b)^3 

sage: f.sin(prec=2) 

a + b + O(a, b)^2 

  

If the power series has a non-zero constant coefficient `c`, 

one raises an error:: 

  

sage: g = 2+f 

sage: sin(g) 

Traceback (most recent call last): 

... 

ValueError: Can only apply sin to formal power series with zero constant term. 

  

If no precision is specified, the default precision is used:: 

  

sage: T.default_prec() 

12 

sage: sin(a) 

a - 1/6*a^3 + 1/120*a^5 - 1/5040*a^7 + 1/362880*a^9 - 1/39916800*a^11 + O(a, b)^12 

sage: a.sin(prec=5) 

a - 1/6*a^3 + O(a, b)^5 

sage: sin(a + T.O(5)) 

a - 1/6*a^3 + O(a, b)^5 

  

TESTS:: 

  

sage: sin(a^2 + T.O(5)) 

a^2 + O(a, b)^5 

""" 

R = self.parent() 

  

c = self[0] 

if not c.is_zero(): 

raise ValueError('Can only apply sin to formal power ' 

'series with zero constant term.') 

val = self.valuation() 

assert(val >= 1) 

  

x = self 

 

prec = min(prec, self.prec()) 

if is_Infinite(prec): 

prec = R.default_prec() 

n_inv_factorial = R.base_ring().one() 

x_pow_n = x 

x2 = x ** 2 

sin_x = x.add_bigoh(prec) 

for n in range(3, prec // val + 1, 2): 

x_pow_n = (x_pow_n * x2).add_bigoh(prec) 

n_inv_factorial /= - n * (n - 1) 

sin_x += x_pow_n * n_inv_factorial 

result_bg = sin_x 

  

if result_bg.base_ring() is not self.base_ring(): 

R = R.change_ring(self.base_ring().fraction_field()) 

return R(result_bg, prec=prec) 

  

def O(self, prec): 

r""" 

Return this series plus `O(x^\text{prec})`. Does not change 

self. 

 

EXAMPLES:: 

 

sage: R.<x> = PowerSeriesRing(ZZ) 

sage: p = 1 + x^2 + x^10; p 

1 + x^2 + x^10 

sage: p.O(15) 

1 + x^2 + x^10 + O(x^15) 

sage: p.O(5) 

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

sage: p.O(-5) 

Traceback (most recent call last): 

... 

ValueError: prec (= -5) must be non-negative 

""" 

if prec is infinity or prec >= self.prec(): 

return self 

coeffs = self[:prec] 

return self._parent(coeffs, prec) 

  

  

def solve_linear_de(self, prec = infinity, b = None, f0 = None): 

r""" 

Obtain a power series solution to an inhomogeneous linear 

differential equation of the form: 

  

.. MATH:: 

  

f'(t) = a(t) f(t) + b(t). 

  

  

  

INPUT: 

  

  

- ``self`` - the power series `a(t)` 

  

- ``b`` - the power series `b(t)` (default is 

zero) 

  

- ``f0`` - the constant term of `f` ("initial 

condition") (default is 1) 

  

- ``prec`` - desired precision of result (this will be 

reduced if either a or b have less precision available) 

  

  

OUTPUT: the power series `f`, to indicated precision 

  

ALGORITHM: A divide-and-conquer strategy; see the source code. 

Running time is approximately `M(n) \log n`, where 

`M(n)` is the time required for a polynomial multiplication 

of length `n` over the coefficient ring. (If you're working 

over something like `\QQ`, running time analysis can be a 

little complicated because the coefficients tend to explode.) 

  

.. NOTE:: 

  

- If the coefficient ring is a field of characteristic 

zero, then the solution will exist and is unique. 

  

- For other coefficient rings, things are more 

complicated. A solution may not exist, and if it does it 

may not be unique. Generally, by the time the nth term 

has been computed, the algorithm will have attempted 

divisions by `n!` in the coefficient ring. So if 

your coefficient ring has enough 'precision', and if your 

coefficient ring can perform divisions even when the 

answer is not unique, and if you know in advance that a 

solution exists, then this function will find a solution 

(otherwise it will probably crash). 

  

AUTHORS: 

  

- David Harvey (2006-09-11): factored functionality out from 

exp() function, cleaned up precision tests a bit 

  

EXAMPLES:: 

  

sage: R.<t> = PowerSeriesRing(QQ, default_prec=10) 

  

:: 

  

sage: a = 2 - 3*t + 4*t^2 + O(t^10) 

sage: b = 3 - 4*t^2 + O(t^7) 

sage: f = a.solve_linear_de(prec=5, b=b, f0=3/5) 

sage: f 

3/5 + 21/5*t + 33/10*t^2 - 38/15*t^3 + 11/24*t^4 + O(t^5) 

sage: f.derivative() - a*f - b 

O(t^4) 

  

:: 

  

sage: a = 2 - 3*t + 4*t^2 

sage: b = b = 3 - 4*t^2 

sage: f = a.solve_linear_de(b=b, f0=3/5) 

Traceback (most recent call last): 

... 

ValueError: cannot solve differential equation to infinite precision 

  

:: 

  

sage: a.solve_linear_de(prec=5, b=b, f0=3/5) 

3/5 + 21/5*t + 33/10*t^2 - 38/15*t^3 + 11/24*t^4 + O(t^5) 

""" 

if b is None: 

b = self._parent(0) 

  

if f0 is None: 

f0 = 1 

f0 = self.base_ring()(f0) 

  

# reduce precision to whatever is available from a and b 

prec = min(prec, self.prec() + 1, b.prec() + 1) 

if prec is infinity: 

raise ValueError("cannot solve differential equation to infinite precision") 

prec = int(prec) 

if prec == 0: 

return self._parent(0, 0) 

if prec < 0: 

raise ValueError("prec (=%s) must be a non-negative integer" % prec) 

  

base_ring = self._parent.base_ring() 

R = PolynomialRing(base_ring, self._parent.variable_name()) 

  

a_list = self.list() 

b_list = [base_ring(0)] 

b_list.extend(b.list()) 

while len(b_list) < prec: 

b_list.append(base_ring(0)) 

f = _solve_linear_de(R, 0, prec, a_list, b_list, f0) 

return self._parent(f, prec) 

  

def exp(self, prec=None): 

r""" 

Return exp of this power series to the indicated precision. 

  

INPUT: 

  

  

- ``prec`` - integer; default is 

``self.parent().default_prec`` 

  

  

ALGORITHM: See :meth:`.solve_linear_de`. 

  

.. NOTE:: 

  

- Screwy things can happen if the coefficient ring is not a 

field of characteristic zero. See :meth:`.solve_linear_de`. 

  

AUTHORS: 

  

- David Harvey (2006-09-08): rewrote to use simplest possible "lazy" algorithm. 

  

- David Harvey (2006-09-10): rewrote to use divide-and-conquer 

strategy. 

  

- David Harvey (2006-09-11): factored functionality out to 

solve_linear_de(). 

  

- Sourav Sen Gupta, David Harvey (2008-11): handle constant 

term 

  

EXAMPLES:: 

  

sage: R.<t> = PowerSeriesRing(QQ, default_prec=10) 

  

Check that `\exp(t)` is, well, `\exp(t)`:: 

  

sage: (t + O(t^10)).exp() 

1 + t + 1/2*t^2 + 1/6*t^3 + 1/24*t^4 + 1/120*t^5 + 1/720*t^6 + 1/5040*t^7 + 1/40320*t^8 + 1/362880*t^9 + O(t^10) 

  

Check that `\exp(\log(1+t))` is `1+t`:: 

  

sage: (sum([-(-t)^n/n for n in range(1, 10)]) + O(t^10)).exp() 

1 + t + O(t^10) 

  

Check that `\exp(2t + t^2 - t^5)` is whatever it is:: 

  

sage: (2*t + t^2 - t^5 + O(t^10)).exp() 

1 + 2*t + 3*t^2 + 10/3*t^3 + 19/6*t^4 + 8/5*t^5 - 7/90*t^6 - 538/315*t^7 - 425/168*t^8 - 30629/11340*t^9 + O(t^10) 

  

Check requesting lower precision:: 

  

sage: (t + t^2 - t^5 + O(t^10)).exp(5) 

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

  

Can't get more precision than the input:: 

  

sage: (t + t^2 + O(t^3)).exp(10) 

1 + t + 3/2*t^2 + O(t^3) 

  

Check some boundary cases:: 

  

sage: (t + O(t^2)).exp(1) 

1 + O(t) 

sage: (t + O(t^2)).exp(0) 

O(t^0) 

  

Handle nonzero constant term (fixes :trac:`4477`):: 

  

sage: R.<x> = PowerSeriesRing(RR) 

sage: (1 + x + x^2 + O(x^3)).exp() 

2.71828182845905 + 2.71828182845905*x + 4.07742274268857*x^2 + O(x^3) 

  

:: 

  

sage: R.<x> = PowerSeriesRing(ZZ) 

sage: (1 + x + O(x^2)).exp() 

Traceback (most recent call last): 

... 

ArithmeticError: exponential of constant term does not belong to coefficient ring (consider working in a larger ring) 

  

:: 

  

sage: R.<x> = PowerSeriesRing(GF(5)) 

sage: (1 + x + O(x^2)).exp() 

Traceback (most recent call last): 

... 

ArithmeticError: constant term of power series does not support exponentiation 

""" 

if prec is None: 

prec = self._parent.default_prec() 

  

t = self.derivative().solve_linear_de(prec) 

  

if not self[0].is_zero(): 

try: 

C = self[0].exp() 

except AttributeError: 

raise ArithmeticError("constant term of power series does not support exponentiation") 

  

if C.parent() is not self.base_ring(): 

raise ArithmeticError("exponential of constant term does not belong to coefficient ring (consider working in a larger ring)") 

  

t = C * t 

  

return t 

  

def log(self, prec=None): 

r""" 

Return log of this power series to the indicated precision. 

  

This works only if the constant term of the power series is 1 

or the base ring can take the logarithm of the constant coefficient. 

  

INPUT: 

  

- ``prec`` -- integer; default is ``self.parent().default_prec()`` 

  

ALGORITHM: See :meth:`solve_linear_de()`. 

  

.. WARNING:: 

  

Screwy things can happen if the coefficient ring is not a 

field of characteristic zero. See :meth:`solve_linear_de()`. 

  

EXAMPLES:: 

  

sage: R.<t> = PowerSeriesRing(QQ, default_prec=10) 

sage: (1 + t + O(t^10)).log() 

t - 1/2*t^2 + 1/3*t^3 - 1/4*t^4 + 1/5*t^5 - 1/6*t^6 + 1/7*t^7 - 1/8*t^8 + 1/9*t^9 + O(t^10) 

  

sage: t.exp().log() 

t + O(t^10) 

  

sage: (1+t).log().exp() 

1 + t + O(t^10) 

  

sage: (-1 + t + O(t^10)).log() 

Traceback (most recent call last): 

... 

ArithmeticError: constant term of power series is not 1 

  

sage: R.<t> = PowerSeriesRing(RR) 

sage: (2+t).log().exp() 

2.00000000000000 + 1.00000000000000*t + O(t^20) 

""" 

if prec is None: 

prec = self._parent.default_prec() 

  

zero = self.parent().zero() 

const_off = zero 

  

if not self[0].is_one(): 

if self.base_ring() in _Fields and self[0] > 0: 

const_off = self[0].log() 

else: 

raise ArithmeticError("constant term of power series is not 1") 

  

t = zero.solve_linear_de(prec,b=self.derivative()/self,f0=0) 

return t + const_off 

  

def V(self, n): 

r""" 

If `f = \sum a_m x^m`, then this function returns 

`\sum a_m x^{nm}`. 

 

EXAMPLES:: 

 

sage: R.<x> = PowerSeriesRing(ZZ) 

sage: p = 1 + x^2 + x^10; p 

1 + x^2 + x^10 

sage: p.V(3) 

1 + x^6 + x^30 

sage: (p+O(x^20)).V(3) 

1 + x^6 + x^30 + O(x^60) 

""" 

v = self.list() 

m = 0 

w = [] 

zero = self.base_ring()(0) 

for i in range(len(v)*n): 

if i%n != 0: 

w.append(zero) 

else: 

w.append(v[m]) 

m += 1 

return self._parent(w, self.prec()*n) 

  

def valuation(self): 

""" 

Return the valuation of this power series. 

  

This is equal to the valuation of the underlying polynomial. 

  

EXAMPLES: 

  

Sparse examples:: 

  

sage: R.<t> = PowerSeriesRing(QQ, sparse=True) 

sage: f = t^100000 + O(t^10000000) 

sage: f.valuation() 

100000 

sage: R(0).valuation() 

+Infinity 

  

Dense examples:: 

  

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

sage: f = 17*t^100 +O(t^110) 

sage: f.valuation() 

100 

sage: t.valuation() 

1 

""" 

return self.polynomial().valuation() 

  

def variable(self): 

""" 

Return a string with the name of the variable 

of this power series. 

 

EXAMPLES:: 

  

sage: R.<x> = PowerSeriesRing(Rationals()) 

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

sage: f.variable() 

'x' 

  

AUTHORS: 

  

- David Harvey (2006-08-08) 

""" 

return self._parent.variable_name() 

  

def degree(self): 

""" 

Return the degree of this power series, which is by definition the 

degree of the underlying polynomial. 

  

EXAMPLES:: 

  

sage: R.<t> = PowerSeriesRing(QQ, sparse=True) 

sage: f = t^100000 + O(t^10000000) 

sage: f.degree() 

100000 

""" 

return self.polynomial().degree() 

  

def derivative(self, *args): 

r""" 

The formal derivative of this power series, with respect to 

variables supplied in args. 

  

Multiple variables and iteration counts may be supplied; see 

documentation for the global derivative() function for more 

details. 

  

.. SEEALSO:: 

  

:meth:`_derivative` 

  

EXAMPLES:: 

  

sage: R.<x> = PowerSeriesRing(QQ) 

sage: g = -x + x^2/2 - x^4 + O(x^6) 

sage: g.derivative() 

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

sage: g.derivative(x) 

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

sage: g.derivative(x, x) 

1 - 12*x^2 + O(x^4) 

sage: g.derivative(x, 2) 

1 - 12*x^2 + O(x^4) 

""" 

return multi_derivative(self, args) 

  

  

def __setitem__(self, n, value): 

""" 

Called when an attempt is made to change a power series. 

 

EXAMPLES:: 

  

sage: R.<t> = ZZ[[]] 

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

sage: f[1] = 5 

Traceback (most recent call last): 

... 

IndexError: power series are immutable 

""" 

raise IndexError("power series are immutable") 

  

def laurent_series(self): 

""" 

Return the Laurent series associated to this power series, i.e., 

this series considered as a Laurent series. 

  

EXAMPLES:: 

  

sage: k.<w> = QQ[[]] 

sage: f = 1+17*w+15*w^3+O(w^5) 

sage: parent(f) 

Power Series Ring in w over Rational Field 

sage: g = f.laurent_series(); g 

1 + 17*w + 15*w^3 + O(w^5) 

""" 

return self._parent.laurent_series_ring()(self) 

  

def egf_to_ogf(self): 

r""" 

Returns the ordinary generating function power series, 

assuming self is an exponential generating function power series. 

  

This function is known as ``serlaplace`` in PARI/GP. 

  

EXAMPLES:: 

  

sage: R.<t> = PowerSeriesRing(QQ) 

sage: f = t + t^2/factorial(2) + 2*t^3/factorial(3) 

sage: f.egf_to_ogf() 

t + t^2 + 2*t^3 

""" 

return self.parent()([self[i] * arith.factorial(i) for i in range(self.degree()+1)]) 

  

def ogf_to_egf(self): 

r""" 

Returns the exponential generating function power series, 

assuming self is an ordinary generating function power series. 

  

This can also be computed as ``serconvol(f,exp(t))`` in PARI/GP. 

  

EXAMPLES:: 

  

sage: R.<t> = PowerSeriesRing(QQ) 

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

sage: f.ogf_to_egf() 

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

""" 

return self.parent()([self[i] / arith.factorial(i) for i in range(self.degree()+1)]) 

  

ogf = deprecated_function_alias(15705, egf_to_ogf) 

egf = deprecated_function_alias(15705, ogf_to_egf) 

  

def __pari__(self): 

""" 

Return a PARI representation of this series. 

  

There are currently limits to the possible base rings over which this 

function works. See the documentation for 

``sage.rings.polynomial.polynomial_element.Polynomial.__pari__`` 

  

EXAMPLES:: 

  

sage: k.<w> = QQ[[]] 

sage: f = 1+17*w+15*w^3+O(w^5) 

sage: pari(f) # indirect doctest 

1 + 17*w + 15*w^3 + O(w^5) 

sage: pari(1 - 19*w + w^5) # indirect doctest 

w^5 - 19*w + 1 

sage: R.<x> = Zmod(6)[[]] 

sage: pari(1 + x + 8*x^3 + O(x^8)) # indirect doctest 

Mod(1, 6) + Mod(1, 6)*x + Mod(2, 6)*x^3 + O(x^8) 

  

TESTS:: 

  

sage: pari(1 + O(x^1)) 

Mod(1, 6) + O(x) 

sage: pari(O(x^1)) 

O(x) 

sage: pari(O(x^0)) 

O(x^0) 

""" 

n = self.prec() 

s = self.truncate().__pari__() # PARI polynomial 

if n is not infinity: 

v = s.variable() 

s = s.Ser(v, n - s.valuation(v) if s else n) 

return s 

  

def _solve_linear_de(R, N, L, a, b, f0): 

r""" 

Internal function used by PowerSeries.solve_linear_de(). 

  

INPUT: 

  

  

- ``R`` - a PolynomialRing 

  

- ``N`` - integer = 0 

  

- ``L`` - integer = 1 

  

- ``a`` - list of coefficients of `a`, any 

length, all coefficients should belong to base ring of R. 

  

- ``b`` - list of coefficients of `b`, length 

at least `L` (only first `L` coefficients are 

used), all coefficients should belong to base ring of R. 

  

- ``f0`` - constant term of `f` (only used if 

`N == 0`), should belong to base ring of R. 

  

  

OUTPUT: List of coefficients of `f` (length exactly 

`L`), where `f` is a solution to the linear 

inhomogeneous differential equation: 

  

.. MATH:: 

  

(t^N f)' = a t^N f + t^{N-1} b + O(t^{N+L-1}). 

  

  

The constant term of `f` is taken to be f0 if 

`N == 0`, and otherwise is determined by 

`N f_0 = b_0`. 

  

ALGORITHM: The algorithm implemented here is inspired by the "fast 

lazy multiplication algorithm" described in the paper "Lazy 

multiplication of formal power series" by J van der Hoeven (1997 

ISSAC conference). 

  

Our situation is a bit simpler than the one described there, 

because only one of the series being multiplied needs the lazy 

treatment; the other one is known fully in advance. I have 

reformulated the algorithm as an explicit divide-and-conquer 

recursion. Possibly it is slower than the one described by van der 

Hoeven, by a constant factor, but it seems easier to implement. 

Also, because we repeatedly split in half starting at the top 

level, instead of repeatedly doubling in size from the bottom 

level, it's easier to avoid problems with "overshooting" in the 

last iteration. 

  

The idea is to split the problem into two instances with 

`L` about half the size. Take `L'` to be the 

ceiling of `(L/2)`. First recursively find `g` 

modulo `t^{L'}` such that 

  

.. MATH:: 

  

(t^N g)' = a t^N g + t^{N-1} b + O(t^{N+L'-1}). 

  

  

  

Next we want to find `h` modulo `t^{L-L'}` such 

that `f = g + t^{L'} h` is a solution of the original 

equation. We can find a suitable `h` by recursively solving 

another differential equation of the same form, namely 

  

.. MATH:: 

  

(t^{N+L'} h)' = a t^{N+L'} h + c t^{N+L'-1} + O(t^{N+L'-1}), 

  

  

where `c` is determined by 

  

.. MATH:: 

  

(t^N g)' - a t^N g - t^{N-1} b = -c t^{N+L'-1} + O(t^{N+L-1}). 

  

  

Once `g` is known, `c` can be recovered from this 

relation by computing the coefficients of `t^j` of the 

product `a g` for `j` in the range 

`L'-1 \leq j < L-1`. 

  

At the bottom of the recursion we have `L = 1`, in which 

case the solution is simply given by `f_0 = b_0/N` (or by 

the supplied initial condition `f_0` when 

`N == 0`). 

  

The algorithm has to do one multiplication of length `N`, 

two of length `N/2`, four of length `N/4`, etc, 

hence giving runtime `O(M(N) \log N)`. 

  

AUTHORS: 

  

- David Harvey (2006-09-11): factored out of PowerSeries.exp(). 

""" 

if L == 1: 

# base case 

if N == 0: 

return [f0] 

else: 

return [b[0] / N] 

  

L2 = (L + 1) >> 1 # ceil(L/2) 

  

g = _solve_linear_de(R, N, L2, a, b, f0) 

  

term1 = R(g) # we must not have check=False, since otherwise [..., 0, 0] is not stripped 

term2 = R(a[:L]) #, check=False) 

product = (term1 * term2).list() 

  

# todo: perhaps next loop could be made more efficient 

c = b[L2 : L] 

for j in range(L2 - 1, min(L-1, len(product))): 

c[j - (L2-1)] = c[j - (L2-1)] + product[j] 

  

h = _solve_linear_de(R, N + L2, L - L2, a, c, f0) 

  

g.extend(h) 

return g 

  

  

def make_powerseries_poly_v0(parent, f, prec, is_gen): 

# This is only used to unpickle old pickles. The new pickling 

# works differently! 

from . import power_series_poly 

return power_series_poly.PowerSeries_poly(parent, f, prec, 0, is_gen) 

  

  

def make_element_from_parent_v0(parent, *args): 

# This is only used to unpickle old pickles. The new pickling 

# works differently! 

return parent(*args)