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

""" 

Logarithmic Functions 

 

AUTHORS: 

 

- Yoora Yi Tenen (2012-11-16): Add documentation for :meth:`log()` (:trac:`12113`) 

 

- Tomas Kalvoda (2015-04-01): Add :meth:`exp_polar()` (:trac:`18085`) 

 

""" 

from six.moves import range 

 

from sage.symbolic.function import GinacFunction, BuiltinFunction 

from sage.symbolic.constants import e as const_e 

from sage.symbolic.constants import pi as const_pi 

 

from sage.libs.mpmath import utils as mpmath_utils 

from sage.structure.all import parent as s_parent 

from sage.symbolic.expression import Expression 

from sage.rings.real_double import RDF 

from sage.rings.complex_double import CDF 

from sage.rings.integer import Integer 

from sage.rings.integer_ring import ZZ 

from sage.rings.rational_field import QQ 

from sage.rings.rational import Rational 

 

class Function_exp(GinacFunction): 

r""" 

The exponential function, `\exp(x) = e^x`. 

 

EXAMPLES:: 

 

sage: exp(-1) 

e^(-1) 

sage: exp(2) 

e^2 

sage: exp(2).n(100) 

7.3890560989306502272304274606 

sage: exp(x^2 + log(x)) 

e^(x^2 + log(x)) 

sage: exp(x^2 + log(x)).simplify() 

x*e^(x^2) 

sage: exp(2.5) 

12.1824939607035 

sage: exp(float(2.5)) 

12.182493960703473 

sage: exp(RDF('2.5')) 

12.182493960703473 

sage: exp(I*pi/12) 

(1/4*I + 1/4)*sqrt(6) - (1/4*I - 1/4)*sqrt(2) 

 

To prevent automatic evaluation, use the ``hold`` parameter:: 

 

sage: exp(I*pi,hold=True) 

e^(I*pi) 

sage: exp(0,hold=True) 

e^0 

 

To then evaluate again, we currently must use Maxima via 

:meth:`sage.symbolic.expression.Expression.simplify`:: 

 

sage: exp(0,hold=True).simplify() 

1 

 

:: 

 

sage: exp(pi*I/2) 

I 

sage: exp(pi*I) 

-1 

sage: exp(8*pi*I) 

1 

sage: exp(7*pi*I/2) 

-I 

 

For the sake of simplification, the argument is reduced modulo the 

period of the complex exponential function, `2\pi i`:: 

 

sage: k = var('k', domain='integer') 

sage: exp(2*k*pi*I) 

1 

sage: exp(log(2) + 2*k*pi*I) 

2 

 

The precision for the result is deduced from the precision of 

the input. Convert the input to a higher precision explicitly 

if a result with higher precision is desired:: 

 

sage: t = exp(RealField(100)(2)); t 

7.3890560989306502272304274606 

sage: t.prec() 

100 

sage: exp(2).n(100) 

7.3890560989306502272304274606 

 

TESTS:: 

 

sage: latex(exp(x)) 

e^{x} 

sage: latex(exp(sqrt(x))) 

e^{\sqrt{x}} 

sage: latex(exp) 

\exp 

sage: latex(exp(sqrt(x))^x) 

\left(e^{\sqrt{x}}\right)^{x} 

sage: latex(exp(sqrt(x)^x)) 

e^{\left(\sqrt{x}^{x}\right)} 

sage: exp(x)._sympy_() 

exp(x) 

 

Test conjugates:: 

 

sage: conjugate(exp(x)) 

e^conjugate(x) 

 

Test simplifications when taking powers of exp (:trac:`7264`):: 

 

sage: var('a,b,c,II') 

(a, b, c, II) 

sage: model_exp = exp(II)**a*(b) 

sage: sol1_l={b: 5.0, a: 1.1} 

sage: model_exp.subs(sol1_l) 

5.00000000000000*e^(1.10000000000000*II) 

 

:: 

 

sage: exp(3)^II*exp(x) 

e^(3*II + x) 

sage: exp(x)*exp(x) 

e^(2*x) 

sage: exp(x)*exp(a) 

e^(a + x) 

sage: exp(x)*exp(a)^2 

e^(2*a + x) 

 

Another instance of the same problem (:trac:`7394`):: 

 

sage: 2*sqrt(e) 

2*e^(1/2) 

 

Check that :trac:`19918` is fixed:: 

 

sage: exp(-x^2).subs(x=oo) 

0 

sage: exp(-x).subs(x=-oo) 

+Infinity 

""" 

def __init__(self): 

""" 

TESTS:: 

 

sage: loads(dumps(exp)) 

exp 

sage: maxima(exp(x))._sage_() 

e^x 

""" 

GinacFunction.__init__(self, "exp", latex_name=r"\exp", 

conversions=dict(maxima='exp', fricas='exp')) 

 

exp = Function_exp() 

 

class Function_log1(GinacFunction): 

r""" 

The natural logarithm of ``x``. 

 

See :meth:`log()` for extensive documentation. 

 

EXAMPLES:: 

 

sage: ln(e^2) 

2 

sage: ln(2) 

log(2) 

sage: ln(10) 

log(10) 

 

TESTS:: 

 

sage: latex(x.log()) 

\log\left(x\right) 

sage: latex(log(1/4)) 

\log\left(\frac{1}{4}\right) 

sage: log(x)._sympy_() 

log(x) 

sage: loads(dumps(ln(x)+1)) 

log(x) + 1 

 

``conjugate(log(x))==log(conjugate(x))`` unless on the branch cut which 

runs along the negative real axis.:: 

 

sage: conjugate(log(x)) 

conjugate(log(x)) 

sage: var('y', domain='positive') 

y 

sage: conjugate(log(y)) 

log(y) 

sage: conjugate(log(y+I)) 

conjugate(log(y + I)) 

sage: conjugate(log(-1)) 

-I*pi 

sage: log(conjugate(-1)) 

I*pi 

 

Check if float arguments are handled properly.:: 

 

sage: from sage.functions.log import function_log as log 

sage: log(float(5)) 

1.6094379124341003 

sage: log(float(0)) 

-inf 

sage: log(float(-1)) 

3.141592653589793j 

sage: log(x).subs(x=float(-1)) 

3.141592653589793j 

 

:trac:`22142`:: 

 

sage: log(QQbar(sqrt(2))) 

log(1.414213562373095?) 

sage: log(QQbar(sqrt(2))*1.) 

0.346573590279973 

sage: polylog(QQbar(sqrt(2)),3) 

polylog(1.414213562373095?, 3) 

""" 

def __init__(self): 

""" 

TESTS:: 

 

sage: loads(dumps(ln)) 

log 

sage: maxima(ln(x))._sage_() 

log(x) 

""" 

GinacFunction.__init__(self, 'log', latex_name=r'\log', 

conversions=dict(maxima='log', fricas='log', 

mathematica='Log')) 

 

ln = function_log = Function_log1() 

 

class Function_log2(GinacFunction): 

""" 

Return the logarithm of x to the given base. 

 

See :meth:`log() <sage.functions.log.log>` for extensive documentation. 

 

EXAMPLES:: 

 

sage: from sage.functions.log import logb 

sage: logb(1000,10) 

3 

""" 

def __init__(self): 

""" 

TESTS:: 

 

sage: from sage.functions.log import logb 

sage: loads(dumps(logb)) 

log 

""" 

GinacFunction.__init__(self, 'log', ginac_name='logb', nargs=2, 

latex_name=r'\log', 

conversions=dict(maxima='log')) 

 

logb = Function_log2() 

 

def log(*args, **kwds): 

""" 

Return the logarithm of the first argument to the base of 

the second argument which if missing defaults to ``e``. 

 

It calls the ``log`` method of the first argument when computing 

the logarithm, thus allowing the use of logarithm on any object 

containing a ``log`` method. In other words, ``log`` works 

on more than just real numbers. 

 

EXAMPLES:: 

 

sage: log(e^2) 

2 

 

To change the base of the logarithm, add a second parameter:: 

 

sage: log(1000,10) 

3 

 

The synonym ``ln`` can only take one argument:: 

 

sage: ln(RDF(10)) 

2.302585092994046 

sage: ln(2.718) 

0.999896315728952 

sage: ln(2.0) 

0.693147180559945 

sage: ln(float(-1)) 

3.141592653589793j 

sage: ln(complex(-1)) 

3.141592653589793j 

 

You can use 

:class:`RDF<sage.rings.real_double.RealDoubleField_class>`, 

:class:`~sage.rings.real_mpfr.RealField` or ``n`` to get a 

numerical real approximation:: 

 

sage: log(1024, 2) 

10 

sage: RDF(log(1024, 2)) 

10.0 

sage: log(10, 4) 

1/2*log(10)/log(2) 

sage: RDF(log(10, 4)) 

1.6609640474436813 

sage: log(10, 2) 

log(10)/log(2) 

sage: n(log(10, 2)) 

3.32192809488736 

sage: log(10, e) 

log(10) 

sage: n(log(10, e)) 

2.30258509299405 

 

The log function works for negative numbers, complex 

numbers, and symbolic numbers too, picking the branch 

with angle between `-\\pi` and `\\pi`:: 

 

sage: log(-1+0*I) 

I*pi 

sage: log(CC(-1)) 

3.14159265358979*I 

sage: log(-1.0) 

3.14159265358979*I 

 

Small integer powers are factored out immediately:: 

 

sage: log(4) 

2*log(2) 

sage: log(1000000000) 

9*log(10) 

sage: log(8) - 3*log(2) 

0 

sage: bool(log(8) == 3*log(2)) 

True 

 

The ``hold`` parameter can be used to prevent automatic evaluation:: 

 

sage: log(-1,hold=True) 

log(-1) 

sage: log(-1) 

I*pi 

sage: I.log(hold=True) 

log(I) 

sage: I.log(hold=True).simplify() 

1/2*I*pi 

 

For input zero, the following behavior occurs:: 

 

sage: log(0) 

-Infinity 

sage: log(CC(0)) 

-infinity 

sage: log(0.0) 

-infinity 

 

The log function also works in finite fields as long as the 

argument lies in the multiplicative group generated by the base:: 

 

sage: F = GF(13); g = F.multiplicative_generator(); g 

2 

sage: a = F(8) 

sage: log(a,g); g^log(a,g) 

3 

8 

sage: log(a,3) 

Traceback (most recent call last): 

... 

ValueError: No discrete log of 8 found to base 3 modulo 13 

sage: log(F(9), 3) 

2 

 

The log function also works for p-adics (see documentation for 

p-adics for more information):: 

 

sage: R = Zp(5); R 

5-adic Ring with capped relative precision 20 

sage: a = R(16); a 

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

sage: log(a) 

3*5 + 3*5^2 + 3*5^4 + 3*5^5 + 3*5^6 + 4*5^7 + 2*5^8 + 5^9 + 

5^11 + 2*5^12 + 5^13 + 3*5^15 + 2*5^16 + 4*5^17 + 3*5^18 + 

3*5^19 + O(5^20) 

 

 

TESTS: 

 

Check if :trac:`10136` is fixed:: 

 

sage: ln(x).operator() is ln 

True 

sage: log(x).operator() is ln 

True 

 

sage: log(1000, 10) 

3 

sage: log(3,-1) 

-I*log(3)/pi 

sage: log(int(8),2) 

3 

sage: log(8,int(2)) # known bug, see #21518 

3 

sage: log(8,2) 

3 

sage: log(1/8,2) 

-3 

sage: log(1/8,1/2) 

3 

sage: log(8,1/2) # known bug, see #21517 

-3 

 

sage: log(1000, 10, base=5) 

Traceback (most recent call last): 

... 

TypeError: Symbolic function log takes at most 2 arguments (3 given) 

""" 

base = kwds.pop('base', None) 

if base: 

args = args + (base,) 

if not args: 

raise TypeError("Symbolic function log takes at least 1 arguments (0 given)") 

if len(args) == 1: 

return ln(args[0], **kwds) 

if len(args) > 2: 

raise TypeError("Symbolic function log takes at most 2 arguments (%s given)"%(len(args)+1-(base is not None))) 

try: 

return args[0].log(args[1]) 

except ValueError as ex: 

if repr(ex)[12:27] == "No discrete log": 

raise 

return logb(args[0], args[1]) 

except (AttributeError, TypeError): 

return logb(args[0], args[1]) 

 

class Function_polylog(GinacFunction): 

def __init__(self): 

r""" 

The polylog function 

`\text{Li}_s(z) = \sum_{k=1}^{\infty} z^k / k^s`. 

 

This definition is valid for arbitrary complex order `s` and for 

all complex arguments `z` with `|z| < 1`; it can be extended to 

`|z| \ge 1` by the process of analytic continuation. So the 

function may have a discontinuity at `z=1` which can cause a 

`NaN` value returned for floating point arguments. 

 

EXAMPLES:: 

 

sage: polylog(2.7, 0) 

0.000000000000000 

sage: polylog(2, 1) 

1/6*pi^2 

sage: polylog(2, -1) 

-1/12*pi^2 

sage: polylog(3, -1) 

-3/4*zeta(3) 

sage: polylog(2, I) 

I*catalan - 1/48*pi^2 

sage: polylog(4, 1/2) 

polylog(4, 1/2) 

sage: polylog(4, 0.5) 

0.517479061673899 

 

sage: polylog(1, x) 

-log(-x + 1) 

sage: polylog(2,x^2+1) 

dilog(x^2 + 1) 

 

sage: f = polylog(4, 1); f 

1/90*pi^4 

sage: f.n() 

1.08232323371114 

 

sage: polylog(4, 2).n() 

2.42786280675470 - 0.174371300025453*I 

sage: complex(polylog(4,2)) 

(2.4278628067547032-0.17437130002545306j) 

sage: float(polylog(4,0.5)) 

0.5174790616738993 

 

sage: z = var('z') 

sage: polylog(2,z).series(z==0, 5) 

1*z + 1/4*z^2 + 1/9*z^3 + 1/16*z^4 + Order(z^5) 

 

sage: loads(dumps(polylog)) 

polylog 

 

sage: latex(polylog(5, x)) 

{\rm Li}_{5}(x) 

sage: polylog(x, x)._sympy_() 

polylog(x, x) 

 

TESTS: 

 

Check if :trac:`8459` is fixed:: 

 

sage: t = maxima(polylog(5,x)).sage(); t 

polylog(5, x) 

sage: t.operator() == polylog 

True 

sage: t.subs(x=.5).n() 

0.50840057924226... 

 

Check if :trac:`18386` is fixed:: 

 

sage: polylog(2.0, 1) 

1.64493406684823 

sage: polylog(2, 1.0) 

1.64493406684823 

sage: polylog(2.0, 1.0) 

1.64493406684823 

 

sage: BF = RealBallField(100) 

sage: polylog(2, BF(1/3)) 

[0.36621322997706348761674629766 +/- 4.51e-30] 

sage: polylog(2, BF(4/3)) 

nan 

sage: parent(_) 

Real ball field with 100 bits of precision 

sage: polylog(2, CBF(1/3)) 

[0.366213229977063 +/- 5.85e-16] 

sage: parent(_) 

Complex ball field with 53 bits of precision 

sage: polylog(2, CBF(1)) 

[1.644934066848226 +/- 6.59e-16] 

sage: parent(_) 

Complex ball field with 53 bits of precision 

""" 

GinacFunction.__init__(self, "polylog", nargs=2) 

 

def _maxima_init_evaled_(self, *args): 

""" 

EXAMPLES: 

 

These are indirect doctests for this function.:: 

 

sage: polylog(2, x)._maxima_() 

li[2](_SAGE_VAR_x) 

sage: polylog(4, x)._maxima_() 

polylog(4,_SAGE_VAR_x) 

""" 

args_maxima = [] 

for a in args: 

if isinstance(a, str): 

args_maxima.append(a) 

elif hasattr(a, '_maxima_init_'): 

args_maxima.append(a._maxima_init_()) 

else: 

args_maxima.append(str(a)) 

 

n, x = args_maxima 

if int(n) in [1,2,3]: 

return 'li[%s](%s)'%(n, x) 

else: 

return 'polylog(%s, %s)'%(n, x) 

 

 

polylog = Function_polylog() 

 

class Function_dilog(GinacFunction): 

def __init__(self): 

r""" 

The dilogarithm function 

`\text{Li}_2(z) = \sum_{k=1}^{\infty} z^k / k^2`. 

 

This is simply an alias for polylog(2, z). 

 

EXAMPLES:: 

 

sage: dilog(1) 

1/6*pi^2 

sage: dilog(1/2) 

1/12*pi^2 - 1/2*log(2)^2 

sage: dilog(x^2+1) 

dilog(x^2 + 1) 

sage: dilog(-1) 

-1/12*pi^2 

sage: dilog(-1.0) 

-0.822467033424113 

sage: dilog(-1.1) 

-0.890838090262283 

sage: dilog(1/2) 

1/12*pi^2 - 1/2*log(2)^2 

sage: dilog(.5) 

0.582240526465012 

sage: dilog(1/2).n() 

0.582240526465012 

sage: var('z') 

z 

sage: dilog(z).diff(z, 2) 

log(-z + 1)/z^2 - 1/((z - 1)*z) 

sage: dilog(z).series(z==1/2, 3) 

(1/12*pi^2 - 1/2*log(2)^2) + (-2*log(1/2))*(z - 1/2) + (2*log(1/2) + 2)*(z - 1/2)^2 + Order(1/8*(2*z - 1)^3) 

 

sage: latex(dilog(z)) 

{\rm Li}_2\left(z\right) 

 

Dilog has a branch point at `1`. Sage's floating point libraries 

may handle this differently from the symbolic package:: 

 

sage: dilog(1) 

1/6*pi^2 

sage: dilog(1.) 

1.64493406684823  

sage: dilog(1).n() 

1.64493406684823 

sage: float(dilog(1)) 

1.6449340668482262 

 

TESTS: 

 

``conjugate(dilog(x))==dilog(conjugate(x))`` unless on the branch cuts 

which run along the positive real axis beginning at 1.:: 

 

sage: conjugate(dilog(x)) 

conjugate(dilog(x)) 

sage: var('y',domain='positive') 

y 

sage: conjugate(dilog(y)) 

conjugate(dilog(y)) 

sage: conjugate(dilog(1/19)) 

dilog(1/19) 

sage: conjugate(dilog(1/2*I)) 

dilog(-1/2*I) 

sage: dilog(conjugate(1/2*I)) 

dilog(-1/2*I) 

sage: conjugate(dilog(2)) 

conjugate(dilog(2)) 

 

Check that return type matches argument type where possible 

(:trac:`18386`):: 

 

sage: dilog(0.5) 

0.582240526465012 

sage: dilog(-1.0) 

-0.822467033424113 

sage: y = dilog(RealField(13)(0.5)) 

sage: parent(y) 

Real Field with 13 bits of precision 

sage: dilog(RealField(13)(1.1)) 

1.96 - 0.300*I 

sage: parent(_) 

Complex Field with 13 bits of precision 

""" 

GinacFunction.__init__(self, 'dilog', 

conversions=dict(maxima='li[2]')) 

 

dilog = Function_dilog() 

 

 

class Function_lambert_w(BuiltinFunction): 

r""" 

The integral branches of the Lambert W function `W_n(z)`. 

 

This function satisfies the equation 

 

.. MATH:: 

 

z = W_n(z) e^{W_n(z)} 

 

INPUT: 

 

- ``n`` - an integer. `n=0` corresponds to the principal branch. 

 

- ``z`` - a complex number 

 

If called with a single argument, that argument is ``z`` and the branch ``n`` is 

assumed to be 0 (the principal branch). 

 

ALGORITHM: 

 

Numerical evaluation is handled using the mpmath and SciPy libraries. 

 

REFERENCES: 

 

- :wikipedia:`Lambert_W_function` 

 

EXAMPLES: 

 

Evaluation of the principal branch:: 

 

sage: lambert_w(1.0) 

0.567143290409784 

sage: lambert_w(-1).n() 

-0.318131505204764 + 1.33723570143069*I 

sage: lambert_w(-1.5 + 5*I) 

1.17418016254171 + 1.10651494102011*I 

 

Evaluation of other branches:: 

 

sage: lambert_w(2, 1.0) 

-2.40158510486800 + 10.7762995161151*I 

 

Solutions to certain exponential equations are returned in terms of lambert_w:: 

 

sage: S = solve(e^(5*x)+x==0, x, to_poly_solve=True) 

sage: z = S[0].rhs(); z 

-1/5*lambert_w(5) 

sage: N(z) 

-0.265344933048440 

 

Check the defining equation numerically at `z=5`:: 

 

sage: N(lambert_w(5)*exp(lambert_w(5)) - 5) 

0.000000000000000 

 

There are several special values of the principal branch which 

are automatically simplified:: 

 

sage: lambert_w(0) 

0 

sage: lambert_w(e) 

1 

sage: lambert_w(-1/e) 

-1 

 

Integration (of the principal branch) is evaluated using Maxima:: 

 

sage: integrate(lambert_w(x), x) 

(lambert_w(x)^2 - lambert_w(x) + 1)*x/lambert_w(x) 

sage: integrate(lambert_w(x), x, 0, 1) 

(lambert_w(1)^2 - lambert_w(1) + 1)/lambert_w(1) - 1 

sage: integrate(lambert_w(x), x, 0, 1.0) 

0.3303661247616807 

 

Warning: The integral of a non-principal branch is not implemented, 

neither is numerical integration using GSL. The :meth:`numerical_integral` 

function does work if you pass a lambda function:: 

 

sage: numerical_integral(lambda x: lambert_w(x), 0, 1) 

(0.33036612476168054, 3.667800782666048e-15) 

""" 

 

def __init__(self): 

r""" 

See the docstring for :meth:`Function_lambert_w`. 

 

EXAMPLES:: 

 

sage: lambert_w(0, 1.0) 

0.567143290409784 

sage: lambert_w(x, x)._sympy_() 

LambertW(x, x) 

""" 

BuiltinFunction.__init__(self, "lambert_w", nargs=2, 

conversions={'mathematica': 'ProductLog', 

'maple': 'LambertW', 

'matlab': 'lambertw', 

'maxima': 'generalized_lambert_w', 

'sympy': 'LambertW'}) 

 

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

r""" 

Custom call method allows the user to pass one argument or two. If 

one argument is passed, we assume it is ``z`` and that ``n=0``. 

 

EXAMPLES:: 

 

sage: lambert_w(1) 

lambert_w(1) 

sage: lambert_w(1, 2) 

lambert_w(1, 2) 

""" 

if len(args) == 2: 

return BuiltinFunction.__call__(self, *args, **kwds) 

elif len(args) == 1: 

return BuiltinFunction.__call__(self, 0, args[0], **kwds) 

else: 

raise TypeError("lambert_w takes either one or two arguments.") 

 

def _eval_(self, n, z): 

""" 

EXAMPLES:: 

 

sage: lambert_w(6.0) 

1.43240477589830 

sage: lambert_w(1) 

lambert_w(1) 

sage: lambert_w(x+1) 

lambert_w(x + 1) 

 

There are three special values which are automatically simplified:: 

 

sage: lambert_w(0) 

0 

sage: lambert_w(e) 

1 

sage: lambert_w(-1/e) 

-1 

sage: lambert_w(SR(0)) 

0 

 

The special values only hold on the principal branch:: 

 

sage: lambert_w(1,e) 

lambert_w(1, e) 

sage: lambert_w(1, e.n()) 

-0.532092121986380 + 4.59715801330257*I 

 

TESTS: 

 

When automatic simplification occurs, the parent of the output 

value should be either the same as the parent of the input, or 

a Sage type:: 

 

sage: parent(lambert_w(int(0))) 

<... 'int'> 

sage: parent(lambert_w(Integer(0))) 

Integer Ring 

sage: parent(lambert_w(e)) 

Symbolic Ring 

""" 

if not isinstance(z, Expression): 

if n == 0 and z == 0: 

return s_parent(z)(0) 

elif n == 0: 

if z.is_trivial_zero(): 

return s_parent(z)(Integer(0)) 

elif (z-const_e).is_trivial_zero(): 

return s_parent(z)(Integer(1)) 

elif (z+1/const_e).is_trivial_zero(): 

return s_parent(z)(Integer(-1)) 

 

def _evalf_(self, n, z, parent=None, algorithm=None): 

""" 

EXAMPLES:: 

 

sage: N(lambert_w(1)) 

0.567143290409784 

sage: lambert_w(RealField(100)(1)) 

0.56714329040978387299996866221 

 

SciPy is used to evaluate for float, RDF, and CDF inputs:: 

 

sage: lambert_w(RDF(1)) 

0.5671432904097838 

sage: lambert_w(float(1)) 

0.5671432904097838 

sage: lambert_w(CDF(1)) 

0.5671432904097838 

sage: lambert_w(complex(1)) 

(0.5671432904097838+0j) 

sage: lambert_w(RDF(-1)) # abs tol 2e-16 

-0.31813150520476413 + 1.3372357014306895*I 

sage: lambert_w(float(-1)) # abs tol 2e-16 

(-0.31813150520476413+1.3372357014306895j) 

""" 

R = parent or s_parent(z) 

if R is float or R is RDF: 

from scipy.special import lambertw 

res = lambertw(z, n) 

# SciPy always returns a complex value, make it real if possible 

if not res.imag: 

return R(res.real) 

elif R is float: 

return complex(res) 

else: 

return CDF(res) 

elif R is complex or R is CDF: 

from scipy.special import lambertw 

return R(lambertw(z, n)) 

else: 

import mpmath 

return mpmath_utils.call(mpmath.lambertw, z, n, parent=R) 

 

def _derivative_(self, n, z, diff_param=None): 

""" 

The derivative of `W_n(x)` is `W_n(x)/(x \cdot W_n(x) + x)`. 

 

EXAMPLES:: 

 

sage: x = var('x') 

sage: derivative(lambert_w(x), x) 

lambert_w(x)/(x*lambert_w(x) + x) 

 

sage: derivative(lambert_w(2, exp(x)), x) 

e^x*lambert_w(2, e^x)/(e^x*lambert_w(2, e^x) + e^x) 

 

TESTS: 

 

Differentiation in the first parameter raises an error :trac:`14788`:: 

 

sage: n = var('n') 

sage: lambert_w(n, x).diff(n) 

Traceback (most recent call last): 

... 

ValueError: cannot differentiate lambert_w in the first parameter 

""" 

if diff_param == 0: 

raise ValueError("cannot differentiate lambert_w in the first parameter") 

 

return lambert_w(n, z)/(z*lambert_w(n, z)+z) 

 

def _maxima_init_evaled_(self, n, z): 

""" 

EXAMPLES: 

 

These are indirect doctests for this function.:: 

 

sage: lambert_w(0, x)._maxima_() 

lambert_w(_SAGE_VAR_x) 

sage: lambert_w(1, x)._maxima_() 

generalized_lambert_w(1,_SAGE_VAR_x) 

 

TESTS:: 

 

sage: lambert_w(x)._maxima_()._sage_() 

lambert_w(x) 

sage: lambert_w(2, x)._maxima_()._sage_() 

lambert_w(2, x) 

""" 

if isinstance(z, str): 

maxima_z = z 

elif hasattr(z, '_maxima_init_'): 

maxima_z = z._maxima_init_() 

else: 

maxima_z = str(z) 

if n == 0: 

return "lambert_w(%s)" % maxima_z 

else: 

return "generalized_lambert_w(%s,%s)" % (n, maxima_z) 

 

def _print_(self, n, z): 

""" 

Custom _print_ method to avoid printing the branch number if 

it is zero. 

 

EXAMPLES:: 

 

sage: lambert_w(1) 

lambert_w(1) 

sage: lambert_w(0,x) 

lambert_w(x) 

""" 

if n == 0: 

return "lambert_w(%s)" % z 

else: 

return "lambert_w(%s, %s)" % (n, z) 

 

def _print_latex_(self, n, z): 

""" 

Custom _print_latex_ method to avoid printing the branch 

number if it is zero. 

 

EXAMPLES:: 

 

sage: latex(lambert_w(1)) 

\operatorname{W}({1}) 

sage: latex(lambert_w(0,x)) 

\operatorname{W}({x}) 

sage: latex(lambert_w(1,x)) 

\operatorname{W_{1}}({x}) 

sage: latex(lambert_w(1,x+exp(x))) 

\operatorname{W_{1}}({x + e^{x}}) 

""" 

if n == 0: 

return r"\operatorname{W}({%s})" % z._latex_() 

else: 

return r"\operatorname{W_{%s}}({%s})" % (n, z._latex_()) 

 

lambert_w = Function_lambert_w() 

 

 

class Function_exp_polar(BuiltinFunction): 

def __init__(self): 

r""" 

Representation of a complex number in a polar form. 

 

INPUT: 

 

- ``z`` - a complex number `z = a + ib`. 

 

OUTPUT: 

 

A complex number with modulus `\exp(a)` and argument `b`. 

 

If `-\pi < b \leq \pi` then `\operatorname{exp\_polar}(z)=\exp(z)`. 

For other values of `b` the function is left unevaluated. 

 

EXAMPLES: 

 

The following expressions are evaluated using the exponential 

function:: 

 

sage: exp_polar(pi*I/2) 

I 

sage: x = var('x', domain='real') 

sage: exp_polar(-1/2*I*pi + x) 

e^(-1/2*I*pi + x) 

 

The function is left unevaluated when the imaginary part of the 

input `z` does not satisfy `-\pi < \Im(z) \leq \pi`:: 

 

sage: exp_polar(2*pi*I) 

exp_polar(2*I*pi) 

sage: exp_polar(-4*pi*I) 

exp_polar(-4*I*pi) 

 

This fixes :trac:`18085`:: 

 

sage: integrate(1/sqrt(1+x^3),x,algorithm='sympy') 

1/3*x*gamma(1/3)*hypergeometric((1/3, 1/2), (4/3,), -x^3)/gamma(4/3) 

 

 

.. SEEALSO:: 

 

`Examples in Sympy documentation <http://docs.sympy.org/latest/modules/functions/special.html?highlight=exp_polar>`_, 

`Sympy source code of exp_polar <http://docs.sympy.org/0.7.4/_modules/sympy/functions/elementary/exponential.html>`_ 

 

REFERENCES: 

 

:wikipedia:`Complex_number#Polar_form` 

""" 

BuiltinFunction.__init__(self, "exp_polar", 

latex_name=r"\operatorname{exp\_polar}", 

conversions=dict(sympy='exp_polar')) 

 

def _evalf_(self, z, parent=None, algorithm=None): 

r""" 

EXAMPLES: 

 

If the imaginary part of `z` obeys `-\pi < z \leq \pi`, then 

`\operatorname{exp\_polar}(z)` is evaluated as `\exp(z)`:: 

 

sage: exp_polar(1.0 + 2.0*I) 

-1.13120438375681 + 2.47172667200482*I 

 

If the imaginary part of `z` is outside of that interval the 

expression is left unevaluated:: 

 

sage: exp_polar(-5.0 + 8.0*I) 

exp_polar(-5.00000000000000 + 8.00000000000000*I) 

 

An attempt to numerically evaluate such an expression raises an error:: 

 

sage: exp_polar(-5.0 + 8.0*I).n() 

Traceback (most recent call last): 

... 

ValueError: invalid attempt to numerically evaluate exp_polar() 

 

""" 

from sage.functions.other import imag 

 

if (not isinstance(z, Expression) 

and bool(-const_pi < imag(z) <= const_pi)): 

return exp(z) 

else: 

raise ValueError("invalid attempt to numerically evaluate exp_polar()") 

 

def _eval_(self, z): 

""" 

EXAMPLES:: 

 

sage: exp_polar(3*I*pi) 

exp_polar(3*I*pi) 

sage: x = var('x', domain='real') 

sage: exp_polar(4*I*pi + x) 

exp_polar(4*I*pi + x) 

 

TESTS: 

 

Check that :trac:`24441` is fixed:: 

 

sage: exp_polar(arcsec(jacobi_sn(1.1*I*x, x))) # should be fast 

exp_polar(arcsec(jacobi_sn(1.10000000000000*I*x, x))) 

""" 

try: 

im = z.imag_part() 

if (len(im.variables()) == 0 

and bool(-const_pi < im <= const_pi)): 

return exp(z) 

except AttributeError: 

pass 

 

exp_polar = Function_exp_polar() 

 

 

class Function_harmonic_number_generalized(BuiltinFunction): 

r""" 

Harmonic and generalized harmonic number functions, 

defined by: 

 

.. MATH:: 

 

H_{n}=H_{n,1}=\sum_{k=1}^n\frac{1}{k} 

 

H_{n,m}=\sum_{k=1}^n\frac{1}{k^m} 

 

They are also well-defined for complex argument, through: 

 

.. MATH:: 

 

H_{s}=\int_0^1\frac{1-x^s}{1-x} 

 

H_{s,m}=\zeta(m)-\zeta(m,s-1) 

 

If called with a single argument, that argument is ``s`` and ``m`` is 

assumed to be 1 (the normal harmonic numbers ``H_s``). 

 

ALGORITHM: 

 

Numerical evaluation is handled using the mpmath and FLINT libraries. 

 

REFERENCES: 

 

- :wikipedia:`Harmonic_number` 

 

EXAMPLES: 

 

Evaluation of integer, rational, or complex argument:: 

 

sage: harmonic_number(5) 

137/60 

sage: harmonic_number(3,3) 

251/216 

sage: harmonic_number(5/2) 

-2*log(2) + 46/15 

sage: harmonic_number(3.,3) 

zeta(3) - 0.0400198661225573 

sage: harmonic_number(3.,3.) 

1.16203703703704 

sage: harmonic_number(3,3).n(200) 

1.16203703703703703703703... 

sage: harmonic_number(1+I,5) 

harmonic_number(I + 1, 5) 

sage: harmonic_number(5,1.+I) 

1.57436810798989 - 1.06194728851357*I 

 

Solutions to certain sums are returned in terms of harmonic numbers:: 

 

sage: k=var('k') 

sage: sum(1/k^7,k,1,x) 

harmonic_number(x, 7) 

 

Check the defining integral at a random integer:: 

 

sage: n=randint(10,100) 

sage: bool(SR(integrate((1-x^n)/(1-x),x,0,1)) == harmonic_number(n)) 

True 

 

There are several special values which are automatically simplified:: 

 

sage: harmonic_number(0) 

0 

sage: harmonic_number(1) 

1 

sage: harmonic_number(x,1) 

harmonic_number(x) 

 

Arguments are swapped with respect to the same functions in 

Maxima:: 

 

sage: maxima(harmonic_number(x,2)) # maxima expect interface 

gen_harmonic_number(2,_SAGE_VAR_x) 

sage: from sage.calculus.calculus import symbolic_expression_from_maxima_string as sefms 

sage: sefms('gen_harmonic_number(3,x)') 

harmonic_number(x, 3) 

sage: from sage.interfaces.maxima_lib import maxima_lib, max_to_sr 

sage: c=maxima_lib(harmonic_number(x,2)); c 

gen_harmonic_number(2,_SAGE_VAR_x) 

sage: max_to_sr(c.ecl()) 

harmonic_number(x, 2) 

""" 

 

def __init__(self): 

r""" 

EXAMPLES:: 

 

sage: loads(dumps(harmonic_number(x,5))) 

harmonic_number(x, 5) 

sage: harmonic_number(x, x)._sympy_() 

harmonic(x, x) 

""" 

BuiltinFunction.__init__(self, "harmonic_number", nargs=2, 

conversions={'sympy':'harmonic'}) 

 

def __call__(self, z, m=1, **kwds): 

r""" 

Custom call method allows the user to pass one argument or two. If 

one argument is passed, we assume it is ``z`` and that ``m=1``. 

 

EXAMPLES:: 

 

sage: harmonic_number(x) 

harmonic_number(x) 

sage: harmonic_number(x,1) 

harmonic_number(x) 

sage: harmonic_number(x,2) 

harmonic_number(x, 2) 

""" 

return BuiltinFunction.__call__(self, z, m, **kwds) 

 

def _eval_(self, z, m): 

""" 

EXAMPLES:: 

 

sage: harmonic_number(x,0) 

x 

sage: harmonic_number(x,1) 

harmonic_number(x) 

sage: harmonic_number(5) 

137/60 

sage: harmonic_number(3,3) 

251/216 

sage: harmonic_number(3,3).n() # this goes from rational to float 

1.16203703703704 

sage: harmonic_number(3,3.) # the following uses zeta functions 

1.16203703703704 

sage: harmonic_number(3.,3) 

zeta(3) - 0.0400198661225573 

sage: harmonic_number(0.1,5) 

zeta(5) - 0.650300133161038 

sage: harmonic_number(0.1,5).n() 

0.386627621982332 

sage: harmonic_number(3,5/2) 

1/27*sqrt(3) + 1/8*sqrt(2) + 1 

 

TESTS:: 

 

sage: harmonic_number(int(3), int(3)) 

1.162037037037037 

""" 

if m == 0: 

return z 

elif m == 1: 

return harmonic_m1._eval_(z) 

 

if z in ZZ and z >= 0: 

return sum(ZZ(k) ** (-m) for k in range(1, z + 1)) 

 

def _evalf_(self, z, m, parent=None, algorithm=None): 

""" 

EXAMPLES:: 

 

sage: harmonic_number(3.,3) 

zeta(3) - 0.0400198661225573 

sage: harmonic_number(3.,3.) 

1.16203703703704 

sage: harmonic_number(3,3).n(200) 

1.16203703703703703703703... 

sage: harmonic_number(5,I).n() 

2.36889632899995 - 3.51181956521611*I 

""" 

if m == 0: 

if parent is None: 

return z 

return parent(z) 

elif m == 1: 

return harmonic_m1._evalf_(z, parent, algorithm) 

 

from sage.functions.transcendental import zeta, hurwitz_zeta 

return zeta(m) - hurwitz_zeta(m,z+1) 

 

def _maxima_init_evaled_(self, n, z): 

""" 

EXAMPLES: 

 

sage: maxima_calculus(harmonic_number(x,2)) 

gen_harmonic_number(2,_SAGE_VAR_x) 

sage: maxima_calculus(harmonic_number(3,harmonic_number(x,3),hold=True)) 

1/3^gen_harmonic_number(3,_SAGE_VAR_x)+1/2^gen_harmonic_number(3,_SAGE_VAR_x)+1 

""" 

if isinstance(n,str): 

maxima_n=n 

elif hasattr(n,'_maxima_init_'): 

maxima_n=n._maxima_init_() 

else: 

maxima_n=str(n) 

if isinstance(z,str): 

maxima_z=z 

elif hasattr(z,'_maxima_init_'): 

maxima_z=z._maxima_init_() 

else: 

maxima_z=str(z) 

return "gen_harmonic_number(%s,%s)" % (maxima_z, maxima_n) # swap arguments 

 

def _derivative_(self, n, m, diff_param=None): 

""" 

The derivative of `H_{n,m}`. 

 

EXAMPLES:: 

 

sage: k,m,n = var('k,m,n') 

sage: sum(1/k, k, 1, x).diff(x) 

1/6*pi^2 - harmonic_number(x, 2) 

sage: harmonic_number(x, 1).diff(x) 

1/6*pi^2 - harmonic_number(x, 2) 

sage: harmonic_number(n, m).diff(n) 

-m*(harmonic_number(n, m + 1) - zeta(m + 1)) 

sage: harmonic_number(n, m).diff(m) 

Traceback (most recent call last): 

... 

ValueError: cannot differentiate harmonic_number in the second parameter 

""" 

from sage.functions.transcendental import zeta 

if diff_param == 1: 

raise ValueError("cannot differentiate harmonic_number in the second parameter") 

if m==1: 

return harmonic_m1(n).diff() 

else: 

return m*(zeta(m+1) - harmonic_number(n, m+1)) 

 

def _print_(self, z, m): 

""" 

EXAMPLES:: 

 

sage: harmonic_number(x) 

harmonic_number(x) 

sage: harmonic_number(x,2) 

harmonic_number(x, 2) 

""" 

if m == 1: 

return "harmonic_number(%s)" % z 

else: 

return "harmonic_number(%s, %s)" % (z, m) 

 

def _print_latex_(self, z, m): 

""" 

EXAMPLES:: 

 

sage: latex(harmonic_number(x)) 

H_{x} 

sage: latex(harmonic_number(x,2)) 

H_{{x},{2}} 

""" 

if m == 1: 

return r"H_{%s}" % z 

else: 

return r"H_{{%s},{%s}}" % (z, m) 

 

harmonic_number = Function_harmonic_number_generalized() 

 

def _swap_harmonic(a,b): return harmonic_number(b,a) 

 

from sage.libs.pynac.pynac import register_symbol 

 

register_symbol(_swap_harmonic,{'maxima':'gen_harmonic_number'}) 

register_symbol(_swap_harmonic,{'maple':'harmonic'}) 

 

class Function_harmonic_number(BuiltinFunction): 

r""" 

Harmonic number function, defined by: 

 

.. MATH:: 

 

H_{n}=H_{n,1}=\sum_{k=1}^n\frac1k 

 

H_{s}=\int_0^1\frac{1-x^s}{1-x} 

 

See the docstring for :meth:`Function_harmonic_number_generalized`. 

 

This class exists as callback for ``harmonic_number`` returned by Maxima. 

""" 

 

def __init__(self): 

r""" 

EXAMPLES:: 

 

sage: k=var('k') 

sage: loads(dumps(sum(1/k,k,1,x))) 

harmonic_number(x) 

sage: harmonic_number(x)._sympy_() 

harmonic(x) 

""" 

BuiltinFunction.__init__(self, "harmonic_number", nargs=1, 

conversions={'mathematica':'HarmonicNumber', 

'maple':'harmonic', 

'maxima':'harmonic_number', 

'sympy':'harmonic'}) 

 

def _eval_(self, z, **kwds): 

""" 

EXAMPLES:: 

 

sage: harmonic_number(0) 

0 

sage: harmonic_number(1) 

1 

sage: harmonic_number(20) 

55835135/15519504 

sage: harmonic_number(5/2) 

-2*log(2) + 46/15 

sage: harmonic_number(2*x) 

harmonic_number(2*x) 

""" 

if z in ZZ: 

if z == 0: 

return Integer(0) 

elif z == 1: 

return Integer(1) 

elif z > 1: 

import sage.libs.flint.arith as flint_arith 

return flint_arith.harmonic_number(z) 

elif z in QQ: 

from .gamma import psi1 

return psi1(z+1) - psi1(1) 

 

def _evalf_(self, z, parent=None, algorithm='mpmath'): 

""" 

EXAMPLES:: 

 

sage: harmonic_number(20).n() # this goes from rational to float 

3.59773965714368 

sage: harmonic_number(20).n(200) 

3.59773965714368191148376906... 

sage: harmonic_number(20.) # this computes the integral with mpmath 

3.59773965714368 

sage: harmonic_number(1.0*I) 

0.671865985524010 + 1.07667404746858*I 

""" 

from sage.libs.mpmath import utils as mpmath_utils 

import mpmath 

return mpmath_utils.call(mpmath.harmonic, z, parent=parent) 

 

def _derivative_(self, z, diff_param=None): 

""" 

The derivative of `H_x`. 

 

EXAMPLES:: 

 

sage: k=var('k') 

sage: sum(1/k,k,1,x).diff(x) 

1/6*pi^2 - harmonic_number(x, 2) 

""" 

from sage.functions.transcendental import zeta 

return zeta(2)-harmonic_number(z,2) 

 

def _print_latex_(self, z): 

""" 

EXAMPLES:: 

 

sage: k=var('k') 

sage: latex(sum(1/k,k,1,x)) 

H_{x} 

""" 

return r"H_{%s}" % z 

 

harmonic_m1 = Function_harmonic_number()