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

r""" 

Jacobi Elliptic Functions 

 

This module implements the 12 Jacobi elliptic functions, along with their 

inverses and the Jacobi amplitude function. 

 

Jacobi elliptic functions can be thought of as generalizations 

of both ordinary and hyperbolic trig functions. There are twelve 

Jacobian elliptic functions. Each of the twelve corresponds to an 

arrow drawn from one corner of a rectangle to another. 

 

:: 

 

n ------------------- d 

| | 

| | 

| | 

s ------------------- c 

 

Each of the corners of the rectangle are labeled, by convention, ``s``, 

``c``, ``d``, and ``n``. The rectangle is understood to be lying on the complex 

plane, so that ``s`` is at the origin, ``c`` is on the real axis, and ``n`` is 

on the imaginary axis. The twelve Jacobian elliptic functions are 

then `\operatorname{pq}(x)`, where ``p`` and ``q`` are one of the letters 

``s``, ``c``, ``d``, ``n``. 

 

The Jacobian elliptic functions are then the unique 

doubly-periodic, meromorphic functions satisfying the following 

three properties: 

 

#. There is a simple zero at the corner ``p``, and a simple pole at the 

corner ``q``. 

#. The step from ``p`` to ``q`` is equal to half the period of the function 

`\operatorname{pq}(x)`; that is, the function `\operatorname{pq}(x)` is 

periodic in the direction ``pq``, with the period being twice the distance 

from ``p`` to ``q``. `\operatorname{pq}(x)` is periodic in the other two 

directions as well, with a period such that the distance from ``p`` to one 

of the other corners is a quarter period. 

#. If the function `\operatorname{pq}(x)` is expanded in terms of `x` at one of 

the corners, the leading term in the expansion has a coefficient of 1. 

In other words, the leading term of the expansion of `\operatorname{pq}(x)` 

at the corner ``p`` is `x`; the leading term of the expansion at the corner 

``q`` is `1/x`, and the leading term of an expansion at the other two 

corners is 1. 

 

We can write 

 

.. MATH:: 

 

\operatorname{pq}(x) = \frac{\operatorname{pr}(x)}{\operatorname{qr}(x)} 

 

where ``p``, ``q``, and ``r`` are any of the 

letters ``s``, ``c``, ``d``, ``n``, with 

the understanding that `\mathrm{ss} = \mathrm{cc} = \mathrm{dd} 

= \mathrm{nn} = 1`. 

 

Let 

 

.. MATH:: 

 

u = \int_0^{\phi} \frac{d\theta} {\sqrt {1-m \sin^2 \theta}}, 

 

then the *Jacobi elliptic function* `\operatorname{sn}(u)` is given by 

 

.. MATH:: 

 

\operatorname{sn}{u} = \sin{\phi} 

 

and `\operatorname{cn}(u)` is given by 

 

.. MATH:: 

 

\operatorname{cn}{u} = \cos{\phi} 

 

and 

 

.. MATH:: 

 

\operatorname{dn}{u} = \sqrt{1 - m\sin^2 \phi}. 

 

To emphasize the dependence on `m`, one can write 

`\operatorname{sn}(u|m)` for example (and similarly for `\mathrm{cn}` and 

`\mathrm{dn}`). This is the notation used below. 

 

For a given `k` with `0 < k < 1` they therefore are 

solutions to the following nonlinear ordinary differential 

equations: 

 

- `\operatorname{sn}\,(x;k)` solves the differential equations 

 

.. MATH:: 

 

\frac{d^2 y}{dx^2} + (1+k^2) y - 2 k^2 y^3 = 0 

\quad \text{ and } \quad 

\left(\frac{dy}{dx}\right)^2 = (1-y^2) (1-k^2 y^2). 

 

- `\operatorname{cn}(x;k)` solves the differential equations 

 

.. MATH:: 

 

\frac{d^2 y}{dx^2} + (1-2k^2) y + 2 k^2 y^3 = 0 

\quad \text{ and } \quad 

\left(\frac{dy}{dx}\right)^2 = (1-y^2)(1-k^2 + k^2 y^2). 

 

- `\operatorname{dn}(x;k)` solves the differential equations 

 

.. MATH:: 

 

\frac{d^2 y}{dx^2} - (2 - k^2) y + 2 y^3 = 0 

\quad \text{ and } \quad 

\left(\frac{dy}{dx}\right)^2 = y^2 (1 - k^2 - y^2). 

 

If `K(m)` denotes the complete elliptic integral of the 

first kind (named ``elliptic_kc`` in Sage), the elliptic functions 

`\operatorname{sn}(x|m)` and `\operatorname{cn}(x|m)` have real periods 

`4K(m)`, whereas `\operatorname{dn}(x|m)` has a period 

`2K(m)`. The limit `m \rightarrow 0` gives 

`K(0) = \pi/2` and trigonometric functions: 

`\operatorname{sn}(x|0) = \sin{x}`, `\operatorname{cn}(x|0) = \cos{x}`, 

`\operatorname{dn}(x|0) = 1`. The limit `m \rightarrow 1` gives 

`K(1) \rightarrow \infty` and hyperbolic functions: 

`\operatorname{sn}(x|1) = \tanh{x}`, 

`\operatorname{cn}(x|1) = \operatorname{sech}{x}`, 

`\operatorname{dn}(x|1) = \operatorname{sech}{x}`. 

 

REFERENCES: 

 

- :wikipedia:`Jacobi's_elliptic_functions` 

 

- [KS2002]_ 

 

AUTHORS: 

 

- David Joyner (2006): initial version 

 

- Eviatar Bach (2013): complete rewrite, new numerical evaluation, and 

addition of the Jacobi amplitude function 

""" 

 

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

# Copyright (C) 2006 David Joyner <wdj@usna.edu> 

# Copyright (C) 2013 Eviatar Bach <eviatarbach@gmail.com> 

# 

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

# as published by the Free Software Foundation; either version 2 of 

# the License, or (at your option) any later version. 

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

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

 

from sage.symbolic.function import BuiltinFunction 

from sage.functions.trig import (arctan, arcsin, arccos, arccot, arcsec, 

arccsc, csc, sec, sin, cos, tan, cot) 

from sage.functions.hyperbolic import (arctanh, arccosh, arcsinh, arcsech, 

arccsch, arccoth, cosh, coth, sech, 

csch, tanh, sinh) 

from sage.rings.rational_field import QQ 

from sage.rings.integer import Integer 

from sage.functions.special import elliptic_e, elliptic_kc 

from sage.libs.mpmath import utils 

from sage.misc.latex import latex 

 

HALF = QQ((1, 2)) 

 

 

class Jacobi(BuiltinFunction): 

""" 

Base class for the Jacobi elliptic functions. 

""" 

def __init__(self, kind): 

r""" 

Initialize ``self``. 

 

EXAMPLES:: 

 

sage: from sage.functions.jacobi import Jacobi 

sage: Jacobi('sn') 

jacobi_sn 

""" 

if kind not in ['nd', 'ns', 'nc', 'dn', 'ds', 'dc', 'sn', 'sd', 

'sc', 'cn', 'cd', 'cs']: 

raise ValueError("kind must be one of 'nd', 'ns', 'nc', 'dn', " 

"'ds', 'dc', 'sn', 'sd', 'sc', 'cn', 'cd', 'cs'.") 

self.kind = kind 

BuiltinFunction.__init__(self, 

name='jacobi_{}'.format(kind), 

nargs=2, evalf_params_first=False, 

conversions=dict(maple= 

('Jacobi{}' 

.format(kind.upper())), 

mathematica= 

('Jacobi{}' 

.format(kind.upper())), 

maxima= 

('jacobi_{}' 

.format(kind)))) 

 

def _eval_(self, x, m): 

r""" 

TESTS: 

 

Check that the simplifications are correct:: 

 

sage: from mpmath import almosteq 

sage: almosteq(n(jacobi_nd(8, 0, hold=True)), n(jacobi_nd(8, 0))) 

True 

sage: almosteq(n(jacobi_nd(1, 1, hold=True)), n(jacobi_nd(1, 1))) 

True 

sage: almosteq(n(jacobi_nd(0, -5, hold=True)), n(jacobi_nd(0, -5))) 

True 

sage: almosteq(n(jacobi_ns(-4, 0, hold=True)), n(jacobi_ns(-4, 0))) 

True 

sage: almosteq(n(jacobi_ns(-2, 1, hold=True)), n(jacobi_ns(-2, 1))) 

True 

sage: almosteq(n(jacobi_nc(2, 0, hold=True)), n(jacobi_nc(2, 0))) 

True 

sage: almosteq(n(jacobi_nc(1, 1, hold=True)), n(jacobi_nc(1, 1))) 

True 

sage: almosteq(n(jacobi_nc(0, 0, hold=True)), n(jacobi_nc(0, 0))) 

True 

sage: almosteq(n(jacobi_dn(-10, 0, hold=True)), n(jacobi_dn(-10, 0))) 

True 

sage: almosteq(n(jacobi_dn(-1, 1, hold=True)), n(jacobi_dn(-1, 1))) 

True 

sage: almosteq(n(jacobi_dn(0, 3, hold=True)), n(jacobi_dn(0, 3))) 

True 

sage: almosteq(n(jacobi_ds(2, 0, hold=True)), n(jacobi_ds(2, 0))) 

True 

sage: almosteq(n(jacobi_dc(-1, 0, hold=True)), n(jacobi_dc(-1, 0))) 

True 

sage: almosteq(n(jacobi_dc(-8, 1, hold=True)), n(jacobi_dc(-8, 1))) 

True 

sage: almosteq(n(jacobi_dc(0, -10, hold=True)), n(jacobi_dc(0, -10))) 

True 

sage: almosteq(n(jacobi_sn(-7, 0, hold=True)), n(jacobi_sn(-7, 0))) 

True 

sage: almosteq(n(jacobi_sn(-3, 1, hold=True)), n(jacobi_sn(-3, 1))) 

True 

sage: almosteq(n(jacobi_sn(0, -6, hold=True)), n(jacobi_sn(0, -6))) 

True 

sage: almosteq(n(jacobi_sd(4, 0, hold=True)), n(jacobi_sd(4, 0))) 

True 

sage: almosteq(n(jacobi_sd(0, 1, hold=True)), n(jacobi_sd(0, 1))) 

True 

sage: almosteq(n(jacobi_sd(0, 3, hold=True)), n(jacobi_sd(0, 3))) 

True 

sage: almosteq(n(jacobi_sc(-9, 0, hold=True)), n(jacobi_sc(-9, 0))) 

True 

sage: almosteq(n(jacobi_sc(0, 1, hold=True)), n(jacobi_sc(0, 1))) 

True 

sage: almosteq(n(jacobi_sc(0, -10, hold=True)), n(jacobi_sc(0, -10))) 

True 

sage: almosteq(n(jacobi_cn(-2, 0, hold=True)), n(jacobi_cn(-2, 0))) 

True 

sage: almosteq(n(jacobi_cn(6, 1, hold=True)), n(jacobi_cn(6, 1))) 

True 

sage: almosteq(n(jacobi_cn(0, -10, hold=True)), n(jacobi_cn(0, -10))) 

True 

sage: almosteq(n(jacobi_cd(9, 0, hold=True)), n(jacobi_cd(9, 0))) 

True 

sage: almosteq(n(jacobi_cd(-8, 1, hold=True)), n(jacobi_cd(-8, 1))) 

True 

sage: almosteq(n(jacobi_cd(0, 1, hold=True)), n(jacobi_cd(0, 1))) 

True 

sage: almosteq(n(jacobi_cs(-9, 0, hold=True)), n(jacobi_cs(-9, 0))) 

True 

sage: almosteq(n(jacobi_cs(-6, 1, hold=True)), n(jacobi_cs(-6, 1))) 

True 

""" 

if self.kind == 'nd': 

if m == 0: 

return Integer(1) 

elif m == 1: 

return cosh(x) 

elif x == 0: 

return Integer(1) 

elif self.kind == 'ns': 

if m == 0: 

return csc(x) 

elif m == 1: 

return coth(x) 

elif self.kind == 'nc': 

if m == 0: 

return sec(x) 

elif m == 1: 

return cosh(x) 

elif x == 0: 

return Integer(1) 

elif self.kind == 'dn': 

if m == 0: 

return Integer(1) 

elif m == 1: 

return sech(x) 

elif x == 0: 

return Integer(1) 

elif self.kind == 'ds': 

if m == 0: 

return csc(x) 

elif self.kind == 'dc': 

if m == 0: 

return sec(x) 

elif m == 1: 

return Integer(1) 

elif x == 0: 

return Integer(1) 

elif self.kind == 'sn': 

if m == 0: 

return sin(x) 

elif m == 1: 

return tanh(x) 

elif x == 0: 

return Integer(0) 

elif self.kind == 'sd': 

if m == 0: 

return sin(x) 

elif m == 1: 

return sinh(x) 

elif x == 0: 

return Integer(0) 

elif self.kind == 'sc': 

if m == 0: 

return tan(x) 

elif m == 1: 

return sinh(x) 

elif x == 0: 

return Integer(0) 

elif self.kind == 'cn': 

if m == 0: 

return cos(x) 

elif m == 1: 

return sech(x) 

elif x == 0: 

return Integer(1) 

elif self.kind == 'cd': 

if m == 0: 

return cos(x) 

elif m == 1: 

return Integer(1) 

elif x == 0: 

return Integer(1) 

elif self.kind == 'cs': 

if m == 0: 

return cot(x) 

elif m == 1: 

return csch(x) 

return 

 

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

r""" 

TESTS:: 

 

sage: jacobi_sn(3, 4).n(100) 

-0.33260000892770027112809652714 + 1.7077912301715219199143891076e-33*I 

sage: jacobi_dn(I, I).n() 

0.874189950651018 + 0.667346865048825*I 

""" 

from mpmath import ellipfun 

return utils.call(ellipfun, self.kind, x, m, parent=parent) 

 

def _derivative_(self, x, m, diff_param): 

r""" 

TESTS: 

 

sn, cn, and dn are analytic for all real ``x``, so we can check 

that the derivatives are correct by computing the series:: 

 

sage: from mpmath import almosteq 

sage: a = 0.9327542442482303 

sage: b = 0.7402326293643771 

sage: almosteq(jacobi_sn(x, b).series(x, 10).subs(x=a), 

....: jacobi_sn(a, b), abs_eps=0.01) 

True 

sage: almosteq(jacobi_cn(x, b).series(x, 10).subs(x=a), 

....: jacobi_cn(a, b), abs_eps=0.01) 

True 

sage: almosteq(jacobi_dn(x, b).series(x, 10).subs(x=a), 

....: jacobi_dn(a, b), abs_eps=0.01) 

True 

""" 

if diff_param == 0: 

# From Wolfram Functions Site 

if self.kind == 'cd': 

return (m - Integer(1)) * jacobi_nd(x, m) * jacobi_sd(x, m) 

elif self.kind == 'cn': 

return -jacobi_sn(x, m) * jacobi_dn(x, m) 

elif self.kind == 'cs': 

return -jacobi_ds(x, m) * jacobi_ns(x, m) 

elif self.kind == 'dc': 

return (Integer(1) - m) * jacobi_nc(x, m) * jacobi_sc(x, m) 

elif self.kind == 'dn': 

return -m * jacobi_sn(x, m) * jacobi_cn(x, m) 

elif self.kind == 'ds': 

return -jacobi_cs(x, m) * jacobi_ns(x, m) 

elif self.kind == 'nc': 

return jacobi_dc(x, m) * jacobi_sc(x, m) 

elif self.kind == 'nd': 

return m * jacobi_cd(x, m) * jacobi_sd(x, m) 

elif self.kind == 'ns': 

return -jacobi_cs(x, m) * jacobi_ds(x, m) 

elif self.kind == 'sc': 

return jacobi_dc(x, m) * jacobi_nc(x, m) 

elif self.kind == 'sd': 

return jacobi_cd(x, m) * jacobi_nd(x, m) 

elif self.kind == 'sn': 

return jacobi_cn(x, m) * jacobi_dn(x, m) 

elif diff_param == 1: 

# From Maxima 

if self.kind == 'nd': 

return (HALF*((x + elliptic_e(arcsin(jacobi_sn(x, m)), m)/ 

(m - Integer(1)))*jacobi_sn(x, m)*jacobi_cn(x, m) - 

jacobi_dn(x, m)*jacobi_sn(x, m)**Integer(2)/(m - Integer(1)))/ 

jacobi_dn(x, m)**Integer(2)) 

elif self.kind == 'ns': 

return (HALF*(jacobi_sn(x, m)*jacobi_cn(x, m)**Integer(2)/(m - Integer(1)) - 

(x + elliptic_e(arcsin(jacobi_sn(x, m)), m)/ 

(m - Integer(1)))*jacobi_dn(x, m)*jacobi_cn(x, m)/m)/ 

jacobi_sn(x, m)**Integer(2)) 

elif self.kind == 'nc': 

return (-HALF*(jacobi_sn(x, m)**Integer(2)*jacobi_cn(x, m)/(m - Integer(1)) - 

(x + elliptic_e(arcsin(jacobi_sn(x, m)), m)/ 

(m - Integer(1)))*jacobi_dn(x, m)* 

jacobi_sn(x, m)/m)/jacobi_cn(x, m)**Integer(2)) 

elif self.kind == 'dn': 

return (-HALF*(x + elliptic_e(arcsin(jacobi_sn(x, m)), m)/ 

(m - Integer(1)))*jacobi_sn(x, m)*jacobi_cn(x, m) + 

HALF*jacobi_dn(x, m)*jacobi_sn(x, m)**Integer(2)/(m - Integer(1))) 

elif self.kind == 'ds': 

return (HALF*(jacobi_sn(x, m)*jacobi_cn(x, m)**Integer(2)/(m - Integer(1)) - 

(x + elliptic_e(arcsin(jacobi_sn(x, m)), m)/ 

(m - Integer(1)))*jacobi_dn(x, m)*jacobi_cn(x, m)/m)* 

jacobi_dn(x, m)/jacobi_sn(x, m)**Integer(2) - 

HALF*((x + elliptic_e(arcsin(jacobi_sn(x, m)), m)/ 

(m - Integer(1)))*jacobi_sn(x, m)*jacobi_cn(x, m) - 

jacobi_dn(x, m)*jacobi_sn(x, m)**Integer(2)/(m - Integer(1)))/ 

jacobi_sn(x, m)) 

elif self.kind == 'dc': 

return (-HALF*(jacobi_sn(x, m)**Integer(2)*jacobi_cn(x, m)/(m - Integer(1)) - 

(x + elliptic_e(arcsin(jacobi_sn(x, m)), m)/ 

(m - Integer(1)))*jacobi_dn(x, m)* 

jacobi_sn(x, m)/m)*jacobi_dn(x, m)/ 

jacobi_cn(x, m)**Integer(2) - 

HALF*((x + elliptic_e(arcsin(jacobi_sn(x, m)), m)/ 

(m - Integer(1)))*jacobi_sn(x, m)*jacobi_cn(x, m) - 

jacobi_dn(x, m)*jacobi_sn(x, m)**Integer(2)/(m - Integer(1)))/ 

jacobi_cn(x, m)) 

elif self.kind == 'sn': 

return (-HALF*jacobi_sn(x, m)*jacobi_cn(x, m)**Integer(2)/(m - Integer(1)) + 

HALF*(x + elliptic_e(arcsin(jacobi_sn(x, m)), m)/ 

(m - Integer(1)))*jacobi_dn(x, m)*jacobi_cn(x, m)/m) 

elif self.kind == 'sd': 

return (-HALF*(jacobi_sn(x, m)*jacobi_cn(x, m)**Integer(2)/(m - Integer(1)) - 

(x + elliptic_e(arcsin(jacobi_sn(x, m)), m)/ 

(m - Integer(1)))*jacobi_dn(x, m)*jacobi_cn(x, m)/m)/ 

jacobi_dn(x, m) + HALF* 

((x + elliptic_e(arcsin(jacobi_sn(x, m)), m)/ 

(m - Integer(1)))*jacobi_sn(x, m)*jacobi_cn(x, m) - 

jacobi_dn(x, m)*jacobi_sn(x, m)**Integer(2)/(m - Integer(1)))* 

jacobi_sn(x, m)/jacobi_dn(x, m)**Integer(2)) 

elif self.kind == 'sc': 

return (-HALF*(jacobi_sn(x, m)*jacobi_cn(x, m)**Integer(2)/(m - Integer(1)) - 

(x + elliptic_e(arcsin(jacobi_sn(x, m)), m)/ 

(m - Integer(1)))*jacobi_dn(x, m)* 

jacobi_cn(x, m)/m)/jacobi_cn(x, m) - 

HALF*(jacobi_sn(x, m)**Integer(2)*jacobi_cn(x, m)/(m - Integer(1)) - 

(x + elliptic_e(arcsin(jacobi_sn(x, m)), m)/ 

(m - Integer(1)))*jacobi_dn(x, m)*jacobi_sn(x, m)/m)* 

jacobi_sn(x, m)/jacobi_cn(x, m)**Integer(2)) 

elif self.kind == 'cn': 

return (HALF*jacobi_sn(x, m)**Integer(2)*jacobi_cn(x, m)/(m - Integer(1)) - 

HALF*(x + elliptic_e(arcsin(jacobi_sn(x, m)), m)/ 

(m - Integer(1)))*jacobi_dn(x, m)*jacobi_sn(x, m)/m) 

elif self.kind == 'cd': 

return (HALF*(jacobi_sn(x, m)**Integer(2)*jacobi_cn(x, m)/(m - Integer(1)) - 

(x + elliptic_e(arcsin(jacobi_sn(x, m)), m)/ 

(m - Integer(1)))*jacobi_dn(x, m)*jacobi_sn(x, m)/m)/ 

jacobi_dn(x, m) + 

HALF*((x + elliptic_e(arcsin(jacobi_sn(x, m)), m)/ 

(m - Integer(1)))*jacobi_sn(x, m)*jacobi_cn(x, m) - 

jacobi_dn(x, m)*jacobi_sn(x, m)**Integer(2)/(m - Integer(1)))* 

jacobi_cn(x, m)/jacobi_dn(x, m)**Integer(2)) 

elif self.kind == 'cs': 

return (HALF*(jacobi_sn(x, m)*jacobi_cn(x, m)**Integer(2)/(m - Integer(1)) - 

(x + elliptic_e(arcsin(jacobi_sn(x, m)), m)/ 

(m - Integer(1)))*jacobi_dn(x, m)*jacobi_cn(x, m)/m)* 

jacobi_cn(x, m)/jacobi_sn(x, m)**Integer(2) + 

HALF*(jacobi_sn(x, m)**Integer(2)*jacobi_cn(x, m)/(m - Integer(1)) - 

(x + elliptic_e(arcsin(jacobi_sn(x, m)), m)/ 

(m - Integer(1)))*jacobi_dn(x, m)*jacobi_sn(x, m)/m)/ 

jacobi_sn(x, m)) 

 

def _latex_(self): 

r""" 

TESTS:: 

 

sage: latex(jacobi_sn) 

\operatorname{sn} 

""" 

return r"\operatorname{{{}}}".format(self.kind) 

 

def _print_latex_(self, x, m): 

r""" 

TESTS:: 

 

sage: latex(jacobi_sn(x, 3)) 

\operatorname{sn}\left(x\middle|3\right) 

""" 

return r"\operatorname{{{}}}\left({}\middle|{}\right)".format(self.kind, 

latex(x), 

latex(m)) 

 

jacobi_nd = Jacobi('nd') 

jacobi_ns = Jacobi('ns') 

jacobi_nc = Jacobi('nc') 

jacobi_dn = Jacobi('dn') 

jacobi_ds = Jacobi('ds') 

jacobi_dc = Jacobi('dc') 

jacobi_sn = Jacobi('sn') 

jacobi_sd = Jacobi('sd') 

jacobi_sc = Jacobi('sc') 

jacobi_cn = Jacobi('cn') 

jacobi_cd = Jacobi('cd') 

jacobi_cs = Jacobi('cs') 

 

 

class InverseJacobi(BuiltinFunction): 

r""" 

Base class for the inverse Jacobi elliptic functions. 

""" 

def __init__(self, kind): 

r""" 

Initialize ``self``. 

 

EXAMPLES:: 

 

sage: from sage.functions.jacobi import InverseJacobi 

sage: InverseJacobi('sn') 

inverse_jacobi_sn 

""" 

if kind not in ['nd', 'ns', 'nc', 'dn', 'ds', 'dc', 'sn', 'sd', 

'sc', 'cn', 'cd', 'cs']: 

raise ValueError("kind must be one of 'nd', 'ns', 'nc', 'dn', " 

"'ds', 'dc', 'sn', 'sd', 'sc', 'cn', 'cd', 'cs'.") 

self.kind = kind 

BuiltinFunction.__init__(self, 

name='inverse_jacobi_{}'.format(kind), 

nargs=2, evalf_params_first=False, 

conversions=dict(maple= 

('InverseJacobi{}' 

.format(kind.upper())), 

mathematica= 

('InverseJacobi{}' 

.format(kind.upper())), 

maxima= 

('inverse_jacobi_{}' 

.format(kind)))) 

 

def _eval_(self, x, m): 

r""" 

TESTS: 

 

Check that the simplifications are correct:: 

 

sage: from mpmath import almosteq 

sage: almosteq(n(inverse_jacobi_cd(1, -8, hold=True)), 

....: n(inverse_jacobi_cd(1, -8))) 

True 

sage: almosteq(n(inverse_jacobi_cn(0, -5, hold=True)), 

....: n(inverse_jacobi_cn(0, -5))) 

True 

sage: almosteq(n(inverse_jacobi_cn(1, -8, hold=True)), 

....: n(inverse_jacobi_cn(1, -8))) 

True 

sage: almosteq(n(inverse_jacobi_cs(7, 1, hold=True)), 

....: n(inverse_jacobi_cs(7, 1))) 

True 

sage: almosteq(n(inverse_jacobi_dc(3, 0, hold=True)), 

....: n(inverse_jacobi_dc(3, 0))) 

True 

sage: almosteq(n(inverse_jacobi_dc(1, 7, hold=True)), 

....: n(inverse_jacobi_dc(1, 7))) 

True 

sage: almosteq(n(inverse_jacobi_dn(1, -1, hold=True)), 

....: n(inverse_jacobi_dn(1, -1))) 

True 

sage: almosteq(n(inverse_jacobi_ds(7, 0, hold=True)), 

....: n(inverse_jacobi_ds(7, 0))) 

True 

sage: almosteq(n(inverse_jacobi_ds(5, 1, hold=True)), 

....: n(inverse_jacobi_ds(5, 1))) 

True 

sage: almosteq(n(inverse_jacobi_nc(-2, 0, hold=True)), 

....: n(inverse_jacobi_nc(-2, 0))) 

True 

sage: almosteq(n(inverse_jacobi_nc(-1, 1, hold=True)), 

....: n(inverse_jacobi_nc(-1, 1))) 

True 

sage: almosteq(n(inverse_jacobi_nc(1, 4, hold=True)), 

....: n(inverse_jacobi_nc(1, 4))) 

True 

sage: almosteq(n(inverse_jacobi_nd(9, 1, hold=True)), 

....: n(inverse_jacobi_nd(9, 1))) 

True 

sage: almosteq(n(inverse_jacobi_nd(1, -9, hold=True)), 

....: n(inverse_jacobi_nd(1, -9))) 

True 

sage: almosteq(n(inverse_jacobi_ns(-6, 0, hold=True)), 

....: n(inverse_jacobi_ns(-6, 0))) 

True 

sage: almosteq(n(inverse_jacobi_ns(6, 1, hold=True)), 

....: n(inverse_jacobi_ns(6, 1))) 

True 

sage: almosteq(n(inverse_jacobi_sc(9, 0, hold=True)), 

....: n(inverse_jacobi_sc(9, 0))) 

True 

sage: almosteq(n(inverse_jacobi_sc(8, 1, hold=True)), 

....: n(inverse_jacobi_sc(8, 1))) 

True 

sage: almosteq(n(inverse_jacobi_sc(0, -8, hold=True)), 

....: n(inverse_jacobi_sc(0, -8))) 

True 

sage: almosteq(n(inverse_jacobi_sd(-1, 0, hold=True)), 

....: n(inverse_jacobi_sd(-1, 0))) 

True 

sage: almosteq(n(inverse_jacobi_sd(-2, 1, hold=True)), 

....: n(inverse_jacobi_sd(-2, 1))) 

True 

sage: almosteq(n(inverse_jacobi_sd(0, -2, hold=True)), 

....: n(inverse_jacobi_sd(0, -2))) 

True 

sage: almosteq(n(inverse_jacobi_sn(0, 0, hold=True)), 

....: n(inverse_jacobi_sn(0, 0))) 

True 

sage: almosteq(n(inverse_jacobi_sn(0, 6, hold=True)), 

....: n(inverse_jacobi_sn(0, 6))) 

True 

""" 

if self.kind == 'cd': 

if m == 0: 

return arccos(x) 

elif x == 1: 

return Integer(0) 

elif self.kind == 'cn': 

if m == 0: 

return arccos(x) 

elif m == 1: 

return arcsech(x) 

elif x == 0: 

return elliptic_kc(m) 

elif x == 1: 

return Integer(0) 

elif self.kind == 'cs': 

if m == 0: 

return arccot(x) 

elif m == 1: 

return arccsch(x) 

elif self.kind == 'dc': 

if m == 0: 

return arcsec(x) 

elif x == 1: 

return Integer(0) 

elif self.kind == 'dn': 

if m == 1: 

return arcsech(x) 

elif x == 1: 

return Integer(0) 

elif self.kind == 'ds': 

if m == 0: 

return arccsc(x) 

elif m == 1: 

return arccsch(x) 

elif self.kind == 'nc': 

if m == 0: 

return arcsec(x) 

elif m == 1: 

return arccosh(x) 

elif x == 1: 

return Integer(0) 

elif self.kind == 'nd': 

if m == 1: 

return arccosh(x) 

elif x == 1: 

return Integer(0) 

elif self.kind == 'ns': 

if m == 0: 

return arccsc(x) 

elif m == 1: 

return arccoth(x) 

elif self.kind == 'sc': 

if m == 0: 

return arctan(x) 

elif m == 1: 

return arcsinh(x) 

elif x == 0: 

return Integer(0) 

elif self.kind == 'sd': 

if m == 0: 

return arcsin(x) 

elif m == 1: 

return arcsinh(x) 

elif x == 0: 

return Integer(0) 

elif self.kind == 'sn': 

if m == 0: 

return arcsin(x) 

elif m == 1: 

return arctanh(x) 

elif x == 0: 

return Integer(0) 

return 

 

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

r""" 

TESTS:: 

 

sage: inverse_jacobi_cn(2, 3).n() 

0.859663746362987*I 

sage: inverse_jacobi_cd(3, 4).n(100) 

-0.67214752201235862490069823239 + 2.1565156474996432354386749988*I 

""" 

return utils.call(inverse_jacobi_f, self.kind, x, m, parent=parent) 

 

def _derivative_(self, x, m, diff_param): 

r""" 

TESTS: 

 

Check that ``dy/dx * dx/dy == 1``, where ``y = jacobi_pq(x, m)`` and 

``x = inverse_jacobi_pq(y, m)``:: 

 

sage: from mpmath import almosteq 

sage: a = 0.130103220857094 

sage: b = 0.437176765041986 

sage: m = var('m') 

sage: almosteq(abs((diff(jacobi_cd(x, m), x) * 

....: diff(inverse_jacobi_cd(x, m), x).subs(x=jacobi_cd(x, m))).subs(x=a, m=b)), 

....: 1, abs_eps=1e-14) 

True 

sage: almosteq(abs((diff(jacobi_cn(x, m), x) * 

....: diff(inverse_jacobi_cn(x, m), x).subs(x=jacobi_cn(x, m))).subs(x=a, m=b)), 

....: 1, abs_eps=1e-14) 

True 

sage: almosteq(abs((diff(jacobi_cs(x, m), x) * 

....: diff(inverse_jacobi_cs(x, m), x).subs(x=jacobi_cs(x, m))).subs(x=a, m=b)), 

....: 1, abs_eps=1e-14) 

True 

sage: almosteq(abs((diff(jacobi_dc(x, m), x) * 

....: diff(inverse_jacobi_dc(x, m), x).subs(x=jacobi_dc(x, m))).subs(x=a, m=b)), 

....: 1, abs_eps=1e-14) 

True 

sage: almosteq(abs((diff(jacobi_dn(x, m), x) * 

....: diff(inverse_jacobi_dn(x, m), x).subs(x=jacobi_dn(x, m))).subs(x=a, m=b)), 

....: 1, abs_eps=1e-14) 

True 

sage: almosteq(abs((diff(jacobi_ds(x, m), x) * 

....: diff(inverse_jacobi_ds(x, m), x).subs(x=jacobi_ds(x, m))).subs(x=a, m=b)), 

....: 1, abs_eps=1e-14) 

True 

sage: almosteq(abs((diff(jacobi_nc(x, m), x) * 

....: diff(inverse_jacobi_nc(x, m), x).subs(x=jacobi_nc(x, m))).subs(x=a, m=b)), 

....: 1, abs_eps=1e-14) 

True 

sage: almosteq(abs((diff(jacobi_nd(x, m), x) * 

....: diff(inverse_jacobi_nd(x, m), x).subs(x=jacobi_nd(x, m))).subs(x=a, m=b)), 

....: 1, abs_eps=1e-14) 

True 

sage: almosteq(abs((diff(jacobi_ns(x, m), x) * 

....: diff(inverse_jacobi_ns(x, m), x).subs(x=jacobi_ns(x, m))).subs(x=a, m=b)), 

....: 1, abs_eps=1e-14) 

True 

sage: almosteq(abs((diff(jacobi_sc(x, m), x) * 

....: diff(inverse_jacobi_sc(x, m), x).subs(x=jacobi_sc(x, m))).subs(x=a, m=b)), 

....: 1, abs_eps=1e-14) 

True 

sage: almosteq(abs((diff(jacobi_sd(x, m), x) * 

....: diff(inverse_jacobi_sd(x, m), x).subs(x=jacobi_sd(x, m))).subs(x=a, m=b)), 

....: 1, abs_eps=1e-14) 

True 

sage: almosteq(abs((diff(jacobi_sn(x, m), x) * 

....: diff(inverse_jacobi_sn(x, m), x).subs(x=jacobi_sn(x, m))).subs(x=a, m=b)), 

....: 1, abs_eps=1e-14) 

True 

""" 

# From Wolfram Functions Site 

if diff_param == 0: 

if self.kind == 'cd': 

return (jacobi_sn(inverse_jacobi_cd(x, m), m) / 

(x ** Integer(2) - Integer(1))) 

elif self.kind == 'cn': 

return (jacobi_ds(inverse_jacobi_cn(x, m), m) / 

(m * x ** Integer(2) - m + Integer(1))) 

elif self.kind == 'cs': 

return (jacobi_nd(inverse_jacobi_cs(x, m), m) / 

(x ** Integer(2) + Integer(1))) 

elif self.kind == 'dc': 

return (jacobi_sn(inverse_jacobi_dc(x, m), m) / 

(x ** Integer(2) - Integer(1))) 

elif self.kind == 'dn': 

return -(jacobi_cs(inverse_jacobi_dn(x, m), m) / 

(x ** Integer(2) + m - Integer(1))) 

elif self.kind == 'ds': 

return (jacobi_nc(inverse_jacobi_ds(x, m), m) / 

(x ** Integer(2) + m)) 

elif self.kind == 'nc': 

return (jacobi_ds(inverse_jacobi_nc(x, m), m) / 

(-m * x ** Integer(2) + x ** Integer(2) + m)) 

elif self.kind == 'nd': 

return (jacobi_sc(inverse_jacobi_nd(x, m), m) / 

(x ** Integer(2) - Integer(1))) 

elif self.kind == 'ns': 

return Integer(1) / (jacobi_cs(inverse_jacobi_ns(x, m), m) * 

jacobi_ds(inverse_jacobi_ns(x, m), m)) 

elif self.kind == 'sc': 

return (jacobi_nd(inverse_jacobi_sc(x, m), m) / 

(x ** Integer(2) + Integer(1))) 

elif self.kind == 'sd': 

return (jacobi_cn(inverse_jacobi_sd(x, m), m) / 

((m - Integer(1)) * x ** Integer(2) + Integer(1))) 

elif self.kind == 'sn': 

return (jacobi_cd(inverse_jacobi_sn(x, m), m) / 

(Integer(1) - x ** Integer(2))) 

elif diff_param == 1: 

if self.kind == 'cd': 

return ((Integer(1) / (Integer(2) * (Integer(1) - m) * m)) * 

((m - Integer(1)) * inverse_jacobi_cd(x, m) + 

elliptic_e(jacobi_am(inverse_jacobi_cd(x, m), m), 

m))) 

elif self.kind == 'cn': 

return ((-(Integer(1) / (Integer(2) * (-Integer(1) + m) * m))) * 

(elliptic_e(jacobi_am(inverse_jacobi_cn(x, m), m), 

m) + (-Integer(1) + m) * 

inverse_jacobi_cn(x, m) - m * x * 

jacobi_sd(inverse_jacobi_cn(x, m), m))) 

elif self.kind == 'cs': 

return ((-(Integer(1) / (Integer(2) * (-Integer(1) + m) * m * (Integer(1) + x ** Integer(2))))) * 

((Integer(1) + x ** Integer(2)) * 

elliptic_e(jacobi_am(inverse_jacobi_cs(x, m), m), 

m) + (-Integer(1) + m) * (Integer(1) + x ** Integer(2)) * 

inverse_jacobi_cs(x, m) - m * x * 

jacobi_nd(inverse_jacobi_cs(x, m), m))) 

elif self.kind == 'dc': 

return ((Integer(1) / (Integer(2) * (Integer(1) - m) * m)) * 

(elliptic_e(jacobi_am(inverse_jacobi_dc(x, m), m), 

m) - (Integer(1) - m) * 

inverse_jacobi_dc(x, m))) 

elif self.kind == 'dn': 

return ((Integer(1) / (Integer(2) * (Integer(1) - m) * m)) * ((m - Integer(1)) * 

inverse_jacobi_dn(x, m) + 

elliptic_e(jacobi_am(inverse_jacobi_dn(x, m), m), m) - 

x * jacobi_sc(inverse_jacobi_dn(x, m), m))) 

elif self.kind == 'ds': 

return ((-(Integer(1) / (Integer(2) * (-Integer(1) + m) * m))) * 

(elliptic_e(jacobi_am(inverse_jacobi_ds(x, m), m), m) + 

(-Integer(1) + m) * inverse_jacobi_ds(x, m) - 

(m * x * jacobi_nc(inverse_jacobi_ds(x, m), m)) / 

(m + x ** Integer(2)))) 

elif self.kind == 'nc': 

return ((Integer(1) / (Integer(2) * (-Integer(1) + m) * m * x)) * ((-x) * 

(elliptic_e(jacobi_am(inverse_jacobi_nc(x, m), m), m) + 

(-Integer(1) + m) * inverse_jacobi_nc(x, m)) + m * 

jacobi_sd(inverse_jacobi_nc(x, m), m))) 

elif self.kind == 'nd': 

return ((Integer(1) / (Integer(2) * (m - Integer(1)) * m)) * 

((Integer(1) - m) * inverse_jacobi_nd(x, m) - 

elliptic_e(jacobi_am(inverse_jacobi_nd(x, m), m), m) + 

(Integer(1) / x) * jacobi_sc(inverse_jacobi_nd(x, m), m))) 

elif self.kind == 'ns': 

return ((Integer(1)/(Integer(2) * (m - Integer(1)) * m)) * 

((Integer(1) - m) * inverse_jacobi_ns(x, m) - 

elliptic_e(jacobi_am(inverse_jacobi_ns(x, m), m), m) + 

(m / x) * jacobi_cd(inverse_jacobi_ns(x, m), m))) 

elif self.kind == 'sc': 

return ((-(Integer(1) / (Integer(2) * (-Integer(1) + m) * m * (Integer(1) + x ** Integer(2))))) * 

((Integer(1) + x ** Integer(2)) * 

elliptic_e(jacobi_am(inverse_jacobi_sc(x, m), m), m) + 

(-Integer(1) + m) * (Integer(1) + x ** Integer(2)) * inverse_jacobi_sc(x, m) - 

m * x * jacobi_nd(inverse_jacobi_sc(x, m), m))) 

elif self.kind == 'sd': 

return ((-(Integer(1) / (Integer(2) * (-Integer(1) + m) * m))) * 

(elliptic_e(jacobi_am(inverse_jacobi_sd(x, m), m), m) + 

(-Integer(1) + m) * inverse_jacobi_sd(x, m) - 

(m * x * jacobi_nc(inverse_jacobi_sd(x, m), m)) / 

(Integer(1) + m * x ** Integer(2)))) 

elif self.kind == 'sn': 

return ((Integer(1) / (Integer(2) * (Integer(1) - m) * m)) * 

(elliptic_e(jacobi_am(inverse_jacobi_sn(x, m), m), m) + 

(-Integer(1) + m) * inverse_jacobi_sn(x, m) - m * x * 

jacobi_cd(inverse_jacobi_sn(x, m), m))) 

 

def _latex_(self): 

r""" 

TESTS:: 

 

sage: latex(inverse_jacobi_dn) 

\operatorname{arcdn} 

""" 

return r"\operatorname{{arc{}}}".format(self.kind) 

 

def _print_latex_(self, x, m): 

r""" 

TESTS:: 

 

sage: latex(inverse_jacobi_dn(x, 3)) 

\operatorname{arcdn}\left(x\middle|3\right) 

""" 

return r"\operatorname{{arc{}}}\left({}\middle|{}\right)".format(self.kind, 

latex(x), 

latex(m)) 

 

inverse_jacobi_nd = InverseJacobi('nd') 

inverse_jacobi_ns = InverseJacobi('ns') 

inverse_jacobi_nc = InverseJacobi('nc') 

inverse_jacobi_dn = InverseJacobi('dn') 

inverse_jacobi_ds = InverseJacobi('ds') 

inverse_jacobi_dc = InverseJacobi('dc') 

inverse_jacobi_sn = InverseJacobi('sn') 

inverse_jacobi_sd = InverseJacobi('sd') 

inverse_jacobi_sc = InverseJacobi('sc') 

inverse_jacobi_cn = InverseJacobi('cn') 

inverse_jacobi_cd = InverseJacobi('cd') 

inverse_jacobi_cs = InverseJacobi('cs') 

 

 

def jacobi(kind, z, m, **kwargs): 

r""" 

The 12 Jacobi elliptic functions. 

 

INPUT: 

 

- ``kind`` -- a string of the form ``'pq'``, where ``p``, ``q`` are in 

``c``, ``d``, ``n``, ``s`` 

- ``z`` -- a complex number 

- ``m`` -- a complex number; note that `m = k^2`, where `k` is 

the elliptic modulus 

 

EXAMPLES:: 

 

sage: jacobi('sn', 1, 1) 

tanh(1) 

sage: jacobi('cd', 1, 1/2) 

jacobi_cd(1, 1/2) 

sage: RDF(jacobi('cd', 1, 1/2)) 

0.7240097216593705 

sage: (RDF(jacobi('cn', 1, 1/2)), RDF(jacobi('dn', 1, 1/2)), 

....: RDF(jacobi('cn', 1, 1/2) / jacobi('dn', 1, 1/2))) 

(0.5959765676721407, 0.8231610016315962, 0.7240097216593705) 

sage: jsn = jacobi('sn', x, 1) 

sage: P = plot(jsn, 0, 1) 

""" 

if kind == 'nd': 

return jacobi_nd(z, m, **kwargs) 

elif kind == 'ns': 

return jacobi_ns(z, m, **kwargs) 

elif kind == 'nc': 

return jacobi_nc(z, m, **kwargs) 

elif kind == 'dn': 

return jacobi_dn(z, m, **kwargs) 

elif kind == 'ds': 

return jacobi_ds(z, m, **kwargs) 

elif kind == 'dc': 

return jacobi_dc(z, m, **kwargs) 

elif kind == 'sn': 

return jacobi_sn(z, m, **kwargs) 

elif kind == 'sd': 

return jacobi_sd(z, m, **kwargs) 

elif kind == 'sc': 

return jacobi_sc(z, m, **kwargs) 

elif kind == 'cn': 

return jacobi_cn(z, m, **kwargs) 

elif kind == 'cd': 

return jacobi_cd(z, m, **kwargs) 

elif kind == 'cs': 

return jacobi_cs(z, m, **kwargs) 

else: 

raise ValueError("kind must be one of 'nd', 'ns', 'nc', 'dn', " 

"'ds', 'dc', 'sn', 'sd', 'sc', 'cn', 'cd', 'cs'.") 

 

def inverse_jacobi(kind, x, m, **kwargs): 

r""" 

The inverses of the 12 Jacobi elliptic functions. They have the property 

that 

 

.. MATH:: 

 

\operatorname{pq}(\operatorname{arcpq}(x|m)|m) = 

\operatorname{pq}(\operatorname{pq}^{-1}(x|m)|m) = x. 

 

INPUT: 

 

- ``kind`` -- a string of the form ``'pq'``, where ``p``, ``q`` are in 

``c``, ``d``, ``n``, ``s`` 

- ``x`` -- a real number 

- ``m`` -- a real number; note that `m = k^2`, where `k` is the elliptic 

modulus 

 

EXAMPLES:: 

 

sage: jacobi('dn', inverse_jacobi('dn', 3, 0.4), 0.4) 

3.00000000000000 

sage: inverse_jacobi('dn', 10, 1/10).n(digits=50) 

2.4777736267904273296523691232988240759001423661683*I 

sage: inverse_jacobi_dn(x, 1) 

arcsech(x) 

sage: inverse_jacobi_dn(1, 3) 

0 

sage: m = var('m') 

sage: z = inverse_jacobi_dn(x, m).series(x, 4).subs(x=0.1, m=0.7) 

sage: jacobi_dn(z, 0.7) 

0.0999892750039819... 

sage: inverse_jacobi_nd(x, 1) 

arccosh(x) 

sage: inverse_jacobi_nd(1, 2) 

0 

sage: inverse_jacobi_ns(10^-5, 3).n() 

5.77350269202456e-6 + 1.17142008414677*I 

sage: jacobi('sn', 1/2, 1/2) 

jacobi_sn(1/2, 1/2) 

sage: jacobi('sn', 1/2, 1/2).n() 

0.470750473655657 

sage: inverse_jacobi('sn', 0.47, 1/2) 

0.499098231322220 

sage: inverse_jacobi('sn', 0.4707504, 0.5) 

0.499999911466555 

sage: P = plot(inverse_jacobi('sn', x, 0.5), 0, 1) 

""" 

if kind == 'nd': 

return inverse_jacobi_nd(x, m, **kwargs) 

elif kind == 'ns': 

return inverse_jacobi_ns(x, m, **kwargs) 

elif kind == 'nc': 

return inverse_jacobi_nc(x, m, **kwargs) 

elif kind == 'dn': 

return inverse_jacobi_dn(x, m, **kwargs) 

elif kind == 'ds': 

return inverse_jacobi_ds(x, m, **kwargs) 

elif kind == 'dc': 

return inverse_jacobi_dc(x, m, **kwargs) 

elif kind == 'sn': 

return inverse_jacobi_sn(x, m, **kwargs) 

elif kind == 'sd': 

return inverse_jacobi_sd(x, m, **kwargs) 

elif kind == 'sc': 

return inverse_jacobi_sc(x, m, **kwargs) 

elif kind == 'cn': 

return inverse_jacobi_cn(x, m, **kwargs) 

elif kind == 'cd': 

return inverse_jacobi_cd(x, m, **kwargs) 

elif kind == 'cs': 

return inverse_jacobi_cs(x, m, **kwargs) 

else: 

raise ValueError("kind must be one of 'nd', 'ns', 'nc', 'dn', " 

"'ds', 'dc', 'sn', 'sd', 'sc', 'cn', 'cd', 'cs'.") 

 

class JacobiAmplitude(BuiltinFunction): 

r""" 

The Jacobi amplitude function 

`\operatorname{am}(x|m) = \int_0^x \operatorname{dn}(t|m) dt` for 

`-K(m) \leq x \leq K(m)`, `F(\operatorname{am}(x|m)|m) = x`. 

""" 

def __init__(self): 

r""" 

TESTS:: 

 

sage: from sage.functions.jacobi import JacobiAmplitude 

sage: JacobiAmplitude() 

jacobi_am 

""" 

BuiltinFunction.__init__(self, name='jacobi_am', nargs=2, 

conversions=dict(maple='JacobiAM', 

mathematica= 

'JacobiAmplitude'), 

evalf_params_first=False) 

 

def _eval_(self, x, m): 

r""" 

TESTS:: 

 

sage: jacobi_am(x, 0) 

x 

sage: jacobi_am(0, x) 

0 

sage: jacobi_am(3, 4.) 

-0.339059208303591 

""" 

if m == 0: 

return x 

elif x == 0: 

return Integer(0) 

return 

 

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

r""" 

TESTS:: 

 

sage: jacobi_am(1, 2).n(100) 

0.73704379494724574105101929735 

""" 

return utils.call(jacobi_am_f, x, m, parent=parent) 

 

def _derivative_(self, x, m, diff_param): 

r""" 

TESTS:: 

 

sage: diff(jacobi_am(x, 3), x) 

jacobi_dn(x, 3) 

sage: diff(jacobi_am(3, x), x) 

-1/2*(x*jacobi_cn(3, x)*jacobi_sn(3, x) -... 

(3*x + elliptic_e(jacobi_am(3, x), x) - 3)*jacobi_dn(3, x))/((x - 1)*x) 

""" 

if diff_param == 0: 

return jacobi_dn(x, m) 

elif diff_param == 1: 

return (((Integer(-1) + m) * x + elliptic_e(jacobi_am(x, m), m)) * 

jacobi('dn', x, m) - m * jacobi('cn', x, m) * 

jacobi('sn', x, m)) / (Integer(2) * (Integer(-1) + m) * m) 

 

def _latex_(self): 

r""" 

TESTS:: 

 

sage: latex(jacobi_am) 

\operatorname{am} 

""" 

return r"\operatorname{am}" 

 

def _print_latex_(self, x, m): 

r""" 

TESTS:: 

 

sage: latex(jacobi_am(3,x)) 

\operatorname{am}\left(3\middle|x\right) 

""" 

return r"\operatorname{{am}}\left({}\middle|{}\right)".format(latex(x), 

latex(m)) 

 

jacobi_am = JacobiAmplitude() 

 

 

def inverse_jacobi_f(kind, x, m): 

r""" 

Internal function for numerical evaluation of a continous complex branch 

of each inverse Jacobi function, as described in [Tee1997]_. Only accepts 

real arguments. 

 

TESTS:: 

 

sage: from mpmath import ellipfun, chop 

sage: from sage.functions.jacobi import inverse_jacobi_f 

 

sage: chop(ellipfun('sn', inverse_jacobi_f('sn', 0.6, 0), 0)) 

mpf('0.59999999999999998') 

sage: chop(ellipfun('sn', inverse_jacobi_f('sn', 0.6, 1), 1)) 

mpf('0.59999999999999998') 

sage: chop(ellipfun('sn', inverse_jacobi_f('sn', 0, -3), -3)) 

mpf('0.0') 

sage: chop(ellipfun('sn', inverse_jacobi_f('sn', -1, 4), 4)) 

mpf('-1.0') 

sage: chop(ellipfun('sn', inverse_jacobi_f('sn', 0.3, 4), 4)) 

mpf('0.29999999999999999') 

sage: chop(ellipfun('sn', inverse_jacobi_f('sn', 0.8, 4), 4)) 

mpf('0.80000000000000004') 

 

sage: chop(ellipfun('ns', inverse_jacobi_f('ns', 0.8, 0), 0)) 

mpf('0.80000000000000004') 

sage: chop(ellipfun('ns', inverse_jacobi_f('ns', -0.7, 1), 1)) 

mpf('-0.69999999999999996') 

sage: chop(ellipfun('ns', inverse_jacobi_f('ns', 0.01, 2), 2)) 

mpf('0.01') 

sage: chop(ellipfun('ns', inverse_jacobi_f('ns', 0, 2), 2)) 

mpf('0.0') 

sage: chop(ellipfun('ns', inverse_jacobi_f('ns', -10, 6), 6)) 

mpf('-10.0') 

 

sage: chop(ellipfun('cn', inverse_jacobi_f('cn', -10, 0), 0)) 

mpf('-9.9999999999999982') 

sage: chop(ellipfun('cn', inverse_jacobi_f('cn', 50, 1), 1)) 

mpf('50.000000000000071') 

sage: chop(ellipfun('cn', inverse_jacobi_f('cn', 1, 5), 5)) 

mpf('1.0') 

sage: chop(ellipfun('cn', inverse_jacobi_f('cn', 0.5, -5), -5)) 

mpf('0.5') 

sage: chop(ellipfun('cn', inverse_jacobi_f('cn', -0.75, -15), -15)) 

mpf('-0.75000000000000022') 

sage: chop(ellipfun('cn', inverse_jacobi_f('cn', 10, 0.8), 0.8)) 

mpf('9.9999999999999982') 

sage: chop(ellipfun('cn', inverse_jacobi_f('cn', -2, 0.9), 0.9)) 

mpf('-2.0') 

 

sage: chop(ellipfun('nc', inverse_jacobi_f('nc', -4, 0), 0)) 

mpf('-3.9999999999999987') 

sage: chop(ellipfun('nc', inverse_jacobi_f('nc', 7, 1), 1)) 

mpf('7.0000000000000009') 

sage: chop(ellipfun('nc', inverse_jacobi_f('nc', 7, 3), 3)) 

mpf('7.0') 

sage: chop(ellipfun('nc', inverse_jacobi_f('nc', 0, 2), 2)) 

mpf('0.0') 

sage: chop(ellipfun('nc', inverse_jacobi_f('nc', -18, -4), -4)) 

mpf('-17.999999999999925') 

 

sage: chop(ellipfun('dn', inverse_jacobi_f('dn', -0.3, 1), 1)) 

mpf('-0.29999999999999999') 

sage: chop(ellipfun('dn', inverse_jacobi_f('dn', 1, -1), -1)) 

mpf('1.0') 

sage: chop(ellipfun('dn', inverse_jacobi_f('dn', 0.8, 0.5), 0.5)) 

mpf('0.80000000000000004') 

sage: chop(ellipfun('dn', inverse_jacobi_f('dn', 5, -4), -4)) 

mpf('5.0') 

sage: chop(ellipfun('dn', inverse_jacobi_f('dn', 0.4, 0.5), 0.5)) 

mpf('0.40000000000000002') 

sage: chop(ellipfun('dn', inverse_jacobi_f('dn', -0.4, 0.5), 0.5)) 

mpf('-0.40000000000000002') 

sage: chop(ellipfun('dn', inverse_jacobi_f('dn', -0.9, 0.5), 0.5)) 

mpf('-0.90000000000000002') 

sage: chop(ellipfun('dn', inverse_jacobi_f('dn', -1.9, 0.2), 0.2)) 

mpf('-1.8999999999999999') 

 

sage: chop(ellipfun('nd', inverse_jacobi_f('nd', -1.9, 1), 1)) 

mpf('-1.8999999999999999') 

sage: chop(ellipfun('nd', inverse_jacobi_f('nd', 1, -1), -1)) 

mpf('1.0') 

sage: chop(ellipfun('nd', inverse_jacobi_f('nd', 11, -6), -6)) 

mpf('11.0') 

sage: chop(ellipfun('nd', inverse_jacobi_f('nd', 0, 8), 8)) 

mpf('0.0') 

sage: chop(ellipfun('nd', inverse_jacobi_f('nd', -3, 0.8), 0.8)) 

mpf('-2.9999999999999996') 

 

sage: chop(ellipfun('sc', inverse_jacobi_f('sc', -3, 0), 0)) 

mpf('-3.0') 

sage: chop(ellipfun('sc', inverse_jacobi_f('sc', 2, 1), 1)) 

mpf('2.0') 

sage: chop(ellipfun('sc', inverse_jacobi_f('sc', 0, 9), 9)) 

mpf('0.0') 

sage: chop(ellipfun('sc', inverse_jacobi_f('sc', -7, 3), 3)) 

mpf('-7.0') 

 

sage: chop(ellipfun('cs', inverse_jacobi_f('cs', -7, 0), 0)) 

mpf('-6.9999999999999991') 

sage: chop(ellipfun('cs', inverse_jacobi_f('cs', 8, 1), 1)) 

mpf('8.0') 

sage: chop(ellipfun('cs', inverse_jacobi_f('cs', 2, 6), 6)) 

mpf('2.0') 

sage: chop(ellipfun('cs', inverse_jacobi_f('cs', 0, 4), 4)) 

mpf('0.0') 

sage: chop(ellipfun('cs', inverse_jacobi_f('cs', -6, 8), 8)) 

mpf('-6.0000000000000018') 

 

sage: chop(ellipfun('cd', inverse_jacobi_f('cd', -6, 0), 0)) 

mpf('-6.0000000000000009') 

sage: chop(ellipfun('cd', inverse_jacobi_f('cd', 1, 3), 3)) 

mpf('1.0') 

sage: chop(ellipfun('cd', inverse_jacobi_f('cd', 6, 8), 8)) 

mpf('6.0000000000000027') 

 

sage: chop(ellipfun('dc', inverse_jacobi_f('dc', 5, 0), 0)) 

mpf('5.0000000000000018') 

sage: chop(ellipfun('dc', inverse_jacobi_f('dc', -4, 2), 2)) 

mpf('-4.0000000000000018') 

 

sage: chop(ellipfun('sd', inverse_jacobi_f('sd', -4, 0), 0)) 

mpf('-3.9999999999999991') 

sage: chop(ellipfun('sd', inverse_jacobi_f('sd', 7, 1), 1)) 

mpf('7.0') 

sage: chop(ellipfun('sd', inverse_jacobi_f('sd', 0, 9), 9)) 

mpf('0.0') 

sage: chop(ellipfun('sd', inverse_jacobi_f('sd', 8, 0.8), 0.8)) 

mpf('7.9999999999999991') 

 

sage: chop(ellipfun('ds', inverse_jacobi_f('ds', 4, 0.25), 0.25)) 

mpf('4.0') 

""" 

from mpmath import mp 

 

ctx = mp 

prec = ctx.prec 

try: 

x = ctx.convert(x) 

m = ctx.convert(m) 

if not isinstance(x, ctx.mpf) or not isinstance(x, ctx.mpf): 

raise ValueError('arguments must be real') 

if kind == 'sn': 

if m == 0: 

return ctx.asin(x) 

elif m == 1: 

return ctx.atanh(x) 

elif x == 0: 

return ctx.zero 

sign = ctx.sign(x) # sn is odd in x, so operate with abs(x) and 

x = abs(x) # include the sign at the end 

if x <= 1: 

ctx.prec += 10 

phi = ctx.asin(x) 

return sign * ctx.ellipf(phi, m) 

elif 1 < x <= 1 / ctx.sqrt(m): 

K = ctx.ellipk(m) 

ctx.prec += 10 

xpn2 = x ** (-2) 

m1 = 1 - m 

ctx.prec += 10 

omxpn2 = 1 - xpn2 

ctx.prec += 10 

omxpn2dm1 = omxpn2 / m1 

ctx.prec += 10 

phi = ctx.asin(omxpn2dm1.sqrt()) 

return sign * ctx.mpc(K, ctx.ellipf(phi, m1)) 

else: 

ctx.prec += 10 

m1 = 1 - m 

K_prime = ctx.ellipk(m1) 

sqrtm = ctx.sqrt(m) 

ctx.prec += 10 

xsqrtm = x * sqrtm 

ctx.prec += 10 

phi = ctx.asin(1 / xsqrtm) 

ctx.prec += 10 

return sign * ctx.mpc(ctx.ellipf(phi, m), K_prime) 

if kind == 'ns': 

if m == 0: 

return ctx.acsc(x) 

elif m == 1: 

return ctx.acoth(x) 

elif x > 0: 

ctx.prec += 10 

return inverse_jacobi_f('sn', 1 / x, m) 

elif x == 0: 

ctx.prec += 10 

return ctx.j * ctx.ellipk(1 - m) 

else: 

ctx.prec += 10 

K_prime = ctx.ellipk(1 - m) 

odx = 1 / x 

ctx.prec += 10 

arcsnodx = inverse_jacobi_f('sn', odx, m) 

itK_prime = ctx.j * 2 * K_prime 

ctx.prec += 10 

return arcsnodx + itK_prime 

if kind == 'cn': 

if m == 0: 

return ctx.acos(x) 

elif m == 1: 

return ctx.asech(x) 

elif x == 1: 

return ctx.zero 

elif 0 <= x < 1: 

ctx.prec += 10 

x2 = x ** 2 

ctx.prec += 10 

osx2 = 1 - x2 

ctx.prec += 10 

return ctx.ellipf(ctx.asin(ctx.sqrt(osx2)), m) 

elif -1 <= x < 0: 

K = ctx.ellipk(m) 

ctx.prec += 10 

x2 = x ** 2 

ctx.prec += 10 

osx2 = 1 - x2 

ctx.prec += 10 

return (2 * K) - ctx.ellipf(ctx.asin(ctx.sqrt(osx2)), m) 

elif x > 1: 

ctx.prec += 10 

m1 = 1 - m 

xn2 = x ** (-2) 

ctx.prec += 10 

osx2 = 1 - xn2 

ctx.prec += 10 

return ctx.j * ctx.ellipf(ctx.asin(ctx.sqrt(osx2)), m1) 

elif x < -1: 

K = ctx.ellipk(m) 

ctx.prec += 10 

m1 = 1 - m 

xn2 = x ** (-2) 

tK = 2 * K 

ctx.prec += 10 

osx2 = 1 - xn2 

ctx.prec += 10 

phi = ctx.asin(ctx.sqrt(osx2)) 

ctx.prec += 10 

return tK - ctx.j * ctx.ellipf(phi, m1) 

if kind == 'nc': 

if m == 0: 

return ctx.asec(x) 

elif m == 1: 

return ctx.acosh(x) 

elif x == 1: 

return ctx.zero 

elif x > 0: 

ctx.prec += 10 

return inverse_jacobi_f('cn', 1 / x, m) 

elif x == 0: 

ctx.prec += 10 

return ctx.j * ctx.ellipk(1 - m) 

else: 

K = ctx.ellipk(m) 

ctx.prec += 10 

K_prime = ctx.ellipk(1 - m) 

odx = 1 / x 

ctx.prec += 10 

arccnodx = inverse_jacobi_f('cn', odx, m) 

tK = 2 * K 

ctx.prec += 10 

return arccnodx - tK + ctx.j * 2 * K_prime 

if kind == 'dn': 

if x == 1: 

return ctx.zero 

if not m <= 1: 

raise ValueError('m must be <= 1') 

if m == 1: 

return ctx.asech(x) 

ctx.prec += 10 

m1 = 1 - m 

sqrtm1 = ctx.sqrt(m1) 

if sqrtm1 <= x < 1: 

ctx.prec += 10 

x2 = x ** 2 

ctx.prec += 10 

osx2 = 1 - x2 

ctx.prec += 10 

osx2dm = osx2 / m 

ctx.prec += 10 

return ctx.ellipf(ctx.asin(ctx.sqrt(osx2dm)), m) 

elif x > 1: 

ctx.prec += 10 

xn2 = x ** (-2) 

ctx.prec += 10 

osxn2 = 1 - xn2 

m1xn2 = m1 * xn2 

ctx.prec += 10 

osm1xn2 = 1 - m1xn2 

ctx.prec += 10 

sqrtosxn2dosm1xn2 = ctx.sqrt(osxn2 / osm1xn2) 

ctx.prec += 10 

return ctx.j * ctx.ellipf(ctx.asin(sqrtosxn2dosm1xn2), m1) 

elif 0 <= x < sqrtm1: 

K = ctx.ellipk(m) 

ctx.prec += 10 

x2 = x ** 2 

ctx.prec += 10 

x2dm1 = x2 / m1 

osx2 = 1 - x2 

ctx.prec += 10 

osx2dm1 = 1 - x2dm1 

ctx.prec += 10 

osx2dm1dosx2 = osx2dm1 / osx2 

ctx.prec += 10 

sqrtall = ctx.sqrt(osx2dm1dosx2) 

ctx.prec += 10 

phi = ctx.asin(sqrtall) 

ctx.prec += 10 

return K + ctx.j * ctx.ellipf(phi, m1) 

elif -sqrtm1 <= x < 0: 

K = ctx.ellipk(m) 

K_prime = ctx.ellipk(m1) 

ctx.prec += 10 

tK_prime = 2 * K_prime 

x2 = x ** 2 

ctx.prec += 10 

x2dm1 = x2 / m1 

osx2 = 1 - x2 

ctx.prec += 10 

osx2dm1 = 1 - x2dm1 

ctx.prec += 10 

osx2dm1dosx2 = osx2dm1 / osx2 

ctx.prec += 10 

sqrtall = ctx.sqrt(osx2dm1dosx2) 

ctx.prec += 10 

phi = ctx.asin(sqrtall) 

ctx.prec += 10 

return K + ctx.j * (tK_prime - ctx.ellipf(phi, m1)) 

elif -1 <= x < -sqrtm1: 

K = ctx.ellipk(m) 

K_prime = ctx.ellipk(m1) 

ctx.prec += 10 

x2 = x ** 2 

tK = 2 * K 

# Note that the factor of 2 is missing in the reference 

# (formula (81)), probably mistakenly so 

tK_prime = 2 * K_prime 

ctx.prec += 10 

osx2 = 1 - x2 

ctx.prec += 10 

osx2dm = osx2 / m 

sqrtall = ctx.sqrt(osx2dm) 

ctx.prec += 10 

phi = ctx.asin(sqrtall) 

ctx.prec += 10 

return (tK - ctx.ellipf(phi, m)) + (ctx.j * tK_prime) 

elif x < -1: 

K = ctx.ellipk(m) 

K_prime = ctx.ellipk(m1) 

ctx.prec += 10 

tK = 2 * K 

tK_prime = 2 * K_prime 

xn2 = x ** (-2) 

ctx.prec += 10 

osxn2 = 1 - xn2 

m1xn2 = m1 * xn2 

ctx.prec += 10 

osm1xn2 = 1 - m1xn2 

ctx.prec += 10 

sqrtosxn2dosm1xn2 = ctx.sqrt(osxn2 / osm1xn2) 

ctx.prec += 10 

phi = ctx.asin(sqrtosxn2dosm1xn2) 

ctx.prec += 10 

return tK + ctx.j * (tK_prime - ctx.ellipf(phi, m1)) 

if kind == 'nd': 

if m == 1: 

return ctx.acosh(x) 

elif x == 1: 

return ctx.zero 

elif x > 0: 

ctx.prec += 10 

return inverse_jacobi_f('dn', 1 / x, m) 

elif x == 0: 

ctx.prec += 10 

return ctx.j * ctx.ellipk(1 - m) 

else: 

K = ctx.ellipk(m) 

ctx.prec += 10 

tK = 2 * K 

ctx.prec += 10 

return inverse_jacobi_f('dn', 1 / x, m) - tK 

if kind == 'sc': 

if m == 0: 

return ctx.atan(x) 

elif m == 1: 

return ctx.asinh(x) 

elif x == 0: 

return ctx.zero 

else: 

ctx.prec += 10 

atanx = ctx.atan(x) 

return ctx.ellipf(atanx, m) 

if kind == 'cs': 

if m == 0: 

return ctx.acot(x) 

elif m == 1: 

return ctx.acsch(x) 

elif x > 0: 

ctx.prec += 10 

odx = 1 / x 

ctx.prec += 10 

return ctx.ellipf(ctx.atan(odx), m) 

elif x == 0: 

return ctx.ellipk(m) 

else: 

K = ctx.ellipk(m) 

ctx.prec += 10 

odx = 1 / x 

ctx.prec += 10 

phi = ctx.atan(odx) 

ctx.prec += 10 

return ctx.ellipf(phi, m) + (2 * K) 

if kind == 'cd': 

if m == 0: 

return ctx.acos(x) 

elif x == 1: 

return ctx.zero 

else: 

K = ctx.ellipk(m) 

ctx.prec += 10 

return inverse_jacobi_f('sn', x, m) - K 

if kind == 'dc': 

if m == 0: 

return ctx.asec(x) 

K = ctx.ellipk(m) 

ctx.prec += 10 

return inverse_jacobi_f('ns', x, m) - K 

if kind == 'sd': 

if m == 0: 

return ctx.asin(x) 

elif m == 1: 

return ctx.asinh(x) 

elif x == 0: 

return ctx.zero 

else: 

if m > 1: 

raise ValueError('m must be <= 1') 

K = ctx.ellipk(m) 

ctx.prec += 10 

m1 = 1 - m 

ctx.prec += 10 

sqrtm1 = ctx.sqrt(m1) 

ctx.prec += 10 

xsqrtm1 = x * sqrtm1 

ctx.prec += 10 

return inverse_jacobi_f('cn', xsqrtm1, m) + K 

if kind == 'ds': 

if m == 0: 

return ctx.acsc(x) 

elif m == 1: 

return ctx.acsch(x) 

else: 

if m > 1: 

raise ValueError('m must be <= 1') 

K = ctx.ellipk(m) 

ctx.prec += 10 

m1 = 1 - m 

ctx.prec += 10 

sqrtm1 = ctx.sqrt(m1) 

ctx.prec += 10 

xdsqrtm1 = x / sqrtm1 

ctx.prec += 10 

return inverse_jacobi_f('nc', xdsqrtm1, m) + K 

finally: 

ctx.prec = prec 

 

 

def jacobi_am_f(x, m): 

r""" 

Internal function for numeric evaluation of the Jacobi amplitude function 

for real arguments. Procedure described in [Eh2013]_. 

 

TESTS:: 

 

sage: from mpmath import ellipf 

sage: from sage.functions.jacobi import jacobi_am_f 

sage: ellipf(jacobi_am_f(0.5, 1), 1) 

mpf('0.5') 

sage: ellipf(jacobi_am(3, 0.3), 0.3) 

mpf('3.0') 

sage: ellipf(jacobi_am_f(2, -0.5), -0.5) 

mpf('2.0') 

sage: jacobi_am_f(2, -0.5) 

mpf('2.2680930777934176') 

sage: jacobi_am_f(-2, -0.5) 

mpf('-2.2680930777934176') 

sage: jacobi_am_f(-3, 2) 

mpf('0.36067407399586108') 

""" 

from mpmath import mp 

 

ctx = mp 

prec = ctx.prec 

try: 

x = ctx.convert(x) 

m = ctx.convert(m) 

if not isinstance(x, ctx.mpf) or not isinstance(m, ctx.mpf): 

raise ValueError('arguments must be real') 

if abs(m) == 1: 

# gd(x) 

ctx.prec += 10 

tanhx = ctx.tanh(x) 

ctx.prec += 10 

return ctx.asin(tanhx) 

elif abs(m) > 1: 

ctx.prec += 10 

# Real values needed for atan2; as per "Handbook of Elliptic 

# Integrals for Engineers and Scientists" 121.02, sn is real for 

# real x. The imaginary components can thus be safely discarded. 

snx = ctx.ellipfun('sn', x, m).real 

cnx = ctx.ellipfun('cn', x, m).real 

ctx.prec += 10 

return ctx.atan2(snx, cnx) 

else: 

ctx.prec += 10 

K = ctx.ellipk(m) 

if abs(x) <= K: 

snx = ctx.ellipfun('sn', x, m).real 

cnx = ctx.ellipfun('cn', x, m).real 

ctx.prec += 10 

return ctx.atan2(snx, cnx) 

else: 

# Do argument reduction on x to end up with z = x - 2nK, with 

# abs(z) <= K 

ctx.prec += 10 

tK = 2 * K 

ctx.prec += 10 

n = ctx.floor(x / tK) 

ctx.prec += 10 

tnK = n * tK 

npi = n * ctx.pi() 

ctx.prec += 10 

z = x - tnK 

ctx.prec += 10 

# z (and therefore sn(z, m) and cn(z, m)) is real because K(m) 

# is real for abs(m) <= 1. 

snz = ctx.ellipfun('sn', z, m).real 

cnz = ctx.ellipfun('cn', z, m).real 

ctx.prec += 10 

return ctx.atan2(snz, cnz) + npi 

finally: 

ctx.prec = prec