Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

645

646

647

648

649

650

651

652

653

654

655

656

657

658

659

660

661

662

663

664

665

666

667

668

669

670

671

672

673

674

675

676

677

678

679

680

681

682

683

684

685

686

687

688

689

690

691

692

693

694

695

696

697

698

699

700

701

702

703

704

705

706

707

708

709

710

711

712

713

714

715

716

717

718

719

720

721

722

723

724

725

726

727

728

729

730

731

732

733

734

735

736

737

738

739

740

741

742

743

744

745

746

747

748

749

750

751

752

753

754

755

756

757

758

759

760

761

762

763

764

765

766

767

768

769

770

771

772

773

774

775

776

777

778

779

780

781

782

783

784

785

786

787

788

789

790

791

792

793

794

795

796

797

798

799

800

801

802

803

804

805

806

807

808

809

810

811

812

813

814

815

816

817

818

819

820

821

822

823

824

825

826

827

828

829

830

831

832

833

834

835

836

837

838

839

840

841

842

843

844

845

846

847

848

849

850

851

852

853

854

855

856

857

858

859

860

861

862

863

864

865

866

867

868

869

870

871

872

873

874

875

876

877

878

879

880

881

882

883

884

885

886

887

888

889

890

891

892

893

894

895

896

897

898

899

900

901

902

903

904

905

906

907

908

909

910

911

912

913

914

915

916

917

918

919

920

921

922

923

924

925

926

927

928

929

930

931

932

933

934

935

936

937

938

939

940

941

942

943

944

945

946

947

948

949

950

951

952

953

954

955

956

957

958

959

960

961

962

963

964

965

966

967

968

969

970

971

972

973

974

975

976

977

978

979

980

981

982

983

984

985

986

987

988

989

990

991

992

993

994

995

996

997

998

999

1000

1001

1002

1003

1004

1005

1006

1007

1008

1009

1010

1011

1012

1013

1014

1015

1016

1017

1018

1019

1020

1021

1022

1023

1024

1025

1026

1027

1028

1029

1030

1031

1032

1033

1034

1035

1036

1037

1038

1039

1040

1041

1042

1043

1044

1045

1046

1047

1048

1049

1050

1051

1052

1053

1054

1055

1056

1057

1058

1059

1060

1061

1062

1063

1064

1065

1066

1067

1068

1069

1070

1071

1072

1073

1074

1075

1076

1077

1078

1079

1080

1081

1082

1083

1084

1085

1086

1087

1088

1089

1090

1091

1092

1093

1094

1095

1096

1097

1098

1099

1100

1101

1102

1103

1104

1105

1106

1107

1108

1109

1110

1111

1112

1113

1114

1115

1116

1117

1118

1119

1120

1121

1122

1123

1124

1125

1126

1127

1128

1129

1130

1131

1132

1133

1134

1135

1136

1137

1138

1139

1140

1141

1142

1143

1144

1145

1146

1147

1148

1149

1150

1151

1152

1153

1154

1155

1156

1157

1158

1159

1160

1161

1162

1163

1164

1165

1166

1167

1168

1169

1170

1171

1172

1173

1174

1175

1176

1177

1178

1179

1180

1181

1182

1183

1184

1185

1186

1187

1188

1189

1190

1191

1192

1193

1194

1195

1196

1197

1198

1199

1200

1201

1202

1203

1204

1205

1206

1207

1208

1209

1210

1211

1212

1213

1214

1215

1216

1217

1218

1219

1220

1221

1222

1223

1224

1225

1226

1227

1228

1229

1230

1231

1232

1233

1234

1235

1236

1237

1238

1239

1240

1241

1242

1243

1244

1245

1246

1247

1248

1249

1250

1251

1252

1253

1254

1255

1256

1257

1258

1259

1260

1261

1262

1263

1264

1265

1266

1267

1268

1269

1270

1271

1272

1273

1274

1275

1276

1277

1278

1279

1280

1281

1282

1283

1284

1285

1286

1287

1288

1289

1290

1291

1292

1293

1294

1295

1296

1297

1298

1299

1300

1301

1302

1303

1304

1305

1306

1307

1308

1309

1310

1311

1312

1313

1314

1315

1316

1317

1318

1319

1320

1321

1322

1323

1324

1325

1326

1327

1328

1329

1330

1331

1332

1333

1334

1335

1336

1337

1338

1339

1340

1341

1342

1343

1344

1345

1346

1347

1348

1349

1350

1351

1352

1353

1354

1355

1356

1357

1358

1359

1360

1361

1362

1363

1364

1365

1366

1367

1368

1369

1370

1371

1372

1373

1374

1375

1376

1377

1378

1379

1380

1381

1382

1383

1384

1385

1386

1387

1388

1389

1390

1391

1392

1393

1394

1395

1396

1397

1398

1399

1400

1401

1402

1403

1404

1405

1406

1407

1408

1409

1410

1411

1412

1413

1414

1415

1416

1417

1418

1419

1420

1421

1422

1423

1424

1425

1426

1427

1428

1429

1430

1431

1432

1433

1434

1435

1436

1437

1438

1439

1440

1441

1442

1443

1444

1445

1446

1447

1448

1449

1450

1451

1452

1453

1454

1455

1456

1457

1458

1459

1460

1461

1462

1463

1464

1465

1466

1467

1468

1469

1470

1471

1472

1473

1474

1475

1476

1477

1478

1479

1480

1481

1482

1483

1484

1485

1486

1487

1488

1489

1490

1491

1492

1493

1494

1495

1496

1497

1498

1499

1500

1501

1502

1503

1504

1505

1506

1507

1508

1509

1510

1511

1512

1513

1514

1515

1516

1517

1518

1519

1520

1521

1522

1523

1524

1525

1526

1527

1528

1529

1530

1531

1532

1533

1534

1535

1536

1537

1538

1539

1540

1541

1542

1543

1544

1545

1546

1547

1548

1549

1550

1551

1552

1553

1554

1555

1556

1557

1558

1559

1560

1561

1562

1563

1564

1565

1566

1567

1568

1569

1570

1571

1572

1573

1574

1575

1576

1577

1578

1579

1580

1581

1582

1583

1584

1585

1586

1587

1588

1589

1590

1591

1592

1593

1594

1595

1596

1597

1598

1599

1600

1601

1602

1603

1604

1605

1606

1607

1608

1609

1610

1611

1612

1613

1614

1615

1616

1617

1618

1619

1620

1621

1622

1623

1624

1625

1626

1627

1628

1629

1630

1631

1632

1633

1634

1635

1636

1637

1638

1639

1640

1641

1642

1643

1644

1645

1646

1647

1648

1649

1650

1651

1652

1653

1654

1655

1656

1657

1658

1659

1660

1661

1662

1663

1664

1665

1666

1667

1668

1669

1670

1671

1672

1673

1674

1675

1676

1677

1678

1679

1680

1681

1682

1683

1684

1685

1686

1687

1688

1689

1690

1691

1692

1693

1694

1695

1696

1697

1698

1699

1700

1701

1702

1703

1704

1705

1706

1707

1708

1709

1710

1711

1712

1713

1714

1715

1716

1717

1718

1719

1720

1721

1722

1723

1724

1725

1726

1727

1728

1729

1730

1731

1732

1733

1734

1735

1736

1737

1738

1739

1740

1741

1742

1743

1744

1745

1746

1747

1748

1749

1750

1751

1752

1753

1754

1755

1756

1757

1758

1759

1760

1761

1762

1763

1764

1765

1766

1767

1768

1769

1770

1771

1772

1773

1774

1775

1776

1777

1778

1779

1780

1781

1782

1783

1784

1785

1786

1787

1788

1789

1790

1791

1792

1793

1794

1795

1796

1797

1798

1799

1800

1801

1802

1803

1804

1805

1806

1807

1808

1809

1810

1811

1812

1813

1814

1815

1816

1817

1818

1819

1820

1821

1822

1823

1824

1825

1826

1827

1828

1829

1830

1831

1832

1833

1834

1835

1836

1837

1838

1839

1840

1841

1842

1843

1844

1845

1846

1847

1848

1849

1850

1851

1852

1853

1854

1855

1856

1857

1858

1859

1860

1861

1862

1863

1864

1865

1866

1867

1868

1869

1870

1871

1872

1873

1874

1875

1876

1877

1878

1879

1880

1881

1882

1883

1884

1885

1886

1887

1888

1889

1890

1891

1892

1893

1894

1895

1896

1897

1898

1899

1900

1901

1902

1903

1904

1905

1906

1907

1908

1909

1910

1911

1912

1913

1914

1915

1916

1917

1918

1919

1920

1921

1922

1923

1924

1925

1926

1927

1928

1929

1930

1931

1932

1933

1934

1935

1936

1937

1938

1939

1940

1941

1942

1943

1944

1945

1946

1947

1948

1949

1950

1951

1952

1953

1954

1955

1956

1957

1958

1959

1960

1961

1962

1963

1964

1965

1966

1967

1968

1969

1970

1971

1972

1973

1974

1975

1976

1977

1978

1979

1980

1981

1982

1983

1984

1985

1986

1987

1988

1989

1990

1991

1992

1993

1994

1995

1996

1997

1998

1999

2000

2001

2002

2003

2004

2005

2006

2007

2008

2009

2010

2011

2012

2013

2014

2015

2016

2017

2018

2019

2020

2021

2022

2023

2024

2025

2026

2027

2028

2029

2030

2031

2032

2033

2034

2035

2036

2037

2038

2039

2040

2041

2042

2043

2044

2045

2046

2047

2048

2049

2050

2051

2052

2053

2054

2055

2056

2057

2058

2059

2060

2061

2062

2063

2064

2065

2066

2067

2068

2069

2070

2071

2072

2073

2074

2075

2076

2077

2078

2079

2080

2081

2082

2083

2084

2085

2086

2087

2088

2089

2090

2091

2092

2093

2094

2095

2096

2097

2098

2099

2100

2101

2102

2103

2104

2105

2106

2107

2108

2109

2110

2111

2112

2113

2114

2115

2116

2117

2118

2119

2120

2121

2122

2123

2124

2125

2126

2127

2128

2129

2130

2131

2132

2133

2134

2135

2136

2137

2138

2139

2140

2141

2142

2143

2144

2145

2146

2147

2148

2149

2150

2151

2152

2153

2154

2155

2156

2157

2158

2159

2160

2161

2162

2163

2164

2165

2166

2167

2168

2169

2170

2171

2172

2173

2174

2175

2176

2177

2178

2179

2180

2181

2182

2183

2184

2185

2186

2187

2188

2189

2190

2191

2192

2193

2194

2195

2196

2197

2198

2199

2200

2201

2202

2203

2204

2205

2206

2207

2208

2209

2210

2211

2212

2213

2214

2215

2216

2217

2218

2219

2220

2221

2222

2223

2224

2225

2226

2227

2228

2229

2230

2231

2232

2233

2234

2235

2236

2237

2238

2239

2240

2241

2242

2243

2244

2245

2246

2247

2248

2249

2250

2251

2252

2253

2254

2255

2256

2257

2258

2259

2260

2261

2262

2263

2264

2265

2266

2267

2268

2269

2270

2271

2272

2273

2274

2275

2276

2277

2278

2279

2280

2281

2282

2283

2284

2285

2286

2287

2288

2289

2290

2291

2292

2293

2294

2295

2296

2297

2298

2299

2300

2301

2302

2303

2304

2305

2306

2307

2308

2309

2310

2311

2312

2313

2314

2315

2316

2317

2318

2319

2320

2321

2322

2323

2324

2325

2326

2327

2328

2329

2330

2331

2332

2333

2334

2335

2336

2337

2338

2339

2340

2341

2342

2343

2344

2345

2346

2347

2348

2349

2350

2351

2352

2353

2354

2355

2356

2357

2358

2359

2360

2361

2362

2363

2364

2365

2366

2367

2368

2369

2370

2371

2372

2373

2374

2375

2376

2377

2378

2379

2380

2381

2382

2383

2384

2385

2386

2387

2388

2389

2390

2391

2392

2393

2394

2395

2396

2397

2398

2399

2400

2401

2402

2403

2404

2405

2406

2407

2408

2409

2410

2411

2412

2413

2414

2415

2416

2417

2418

2419

2420

2421

2422

2423

2424

2425

2426

2427

2428

2429

2430

2431

2432

2433

2434

2435

2436

2437

2438

2439

2440

2441

2442

2443

2444

2445

2446

2447

2448

2449

2450

2451

2452

2453

2454

2455

2456

2457

2458

2459

2460

2461

2462

2463

2464

2465

2466

2467

2468

2469

2470

2471

2472

2473

2474

2475

2476

2477

2478

2479

2480

2481

2482

2483

2484

2485

2486

2487

2488

2489

2490

2491

2492

2493

2494

2495

2496

2497

2498

2499

2500

2501

2502

2503

2504

2505

2506

2507

2508

2509

2510

2511

2512

2513

2514

2515

2516

2517

2518

2519

2520

2521

2522

2523

2524

2525

2526

2527

2528

2529

2530

2531

2532

2533

2534

2535

2536

2537

2538

2539

2540

2541

2542

2543

2544

2545

2546

2547

2548

2549

2550

2551

2552

2553

2554

2555

2556

2557

2558

2559

2560

2561

2562

2563

2564

2565

2566

2567

2568

2569

2570

2571

2572

2573

2574

2575

2576

2577

2578

2579

2580

2581

2582

2583

2584

2585

2586

2587

2588

2589

2590

2591

2592

2593

2594

2595

2596

2597

2598

2599

2600

2601

2602

2603

2604

2605

2606

2607

2608

2609

2610

2611

2612

2613

2614

2615

2616

2617

2618

2619

2620

2621

2622

2623

2624

2625

2626

2627

2628

2629

2630

2631

2632

2633

2634

2635

2636

2637

2638

2639

2640

2641

2642

2643

2644

2645

2646

2647

2648

2649

2650

2651

2652

2653

2654

2655

2656

2657

2658

2659

2660

2661

2662

2663

2664

2665

2666

2667

2668

2669

2670

2671

2672

2673

2674

2675

2676

2677

2678

2679

2680

2681

2682

2683

2684

2685

2686

2687

2688

2689

2690

2691

2692

2693

2694

2695

2696

2697

2698

2699

2700

2701

2702

2703

2704

2705

2706

2707

2708

2709

2710

2711

2712

2713

2714

2715

2716

2717

2718

2719

2720

2721

2722

2723

2724

2725

2726

2727

2728

2729

2730

2731

2732

2733

2734

2735

2736

2737

2738

2739

2740

2741

2742

2743

2744

2745

2746

2747

2748

2749

2750

2751

2752

2753

2754

""" 

Arbitrary Precision Complex Numbers 

  

AUTHORS: 

  

- William Stein (2006-01-26): complete rewrite 

  

- Joel B. Mohler (2006-12-16): naive rewrite into pyrex 

  

- William Stein(2007-01): rewrite of Mohler's rewrite 

  

- Vincent Delecroix (2010-01): plot function 

  

- Travis Scrimshaw (2012-10-18): Added documentation for full coverage 

  

- Vincent Klein (2017-11-14) : add __mpc__() to class ComplexNumber. 

ComplexNumber constructor support gmpy2.mpc parameter. 

""" 

  

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

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

# 

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

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

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

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

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

  

from __future__ import absolute_import, print_function 

  

import math 

import operator 

  

from sage.libs.mpfr cimport * 

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

from sage.categories.map cimport Map 

  

from .complex_double cimport ComplexDoubleElement 

from .real_mpfr cimport RealNumber 

  

import sage.misc.misc 

from sage.misc.superseded import deprecated_function_alias 

import sage.rings.integer as integer 

import sage.rings.infinity as infinity 

  

from sage.libs.mpmath.utils cimport mpfr_to_mpfval 

from sage.rings.integer_ring import ZZ 

  

IF HAVE_GMPY2: 

cimport gmpy2 

gmpy2.import_gmpy2() 

  

cdef object numpy_complex_interface = {'typestr': '=c16'} 

cdef object numpy_object_interface = {'typestr': '|O'} 

  

cdef mpfr_rnd_t rnd 

rnd = MPFR_RNDN 

  

cdef double LOG_TEN_TWO_PLUS_EPSILON = 3.321928094887363 # a small overestimate of log(10,2) 

  

def set_global_complex_round_mode(n): 

""" 

Set the global complex rounding mode. 

  

.. WARNING:: 

  

Do not call this function explicitly. The default rounding mode is 

``n = 0``. 

  

EXAMPLES:: 

  

sage: sage.rings.complex_number.set_global_complex_round_mode(0) 

""" 

global rnd 

rnd = n 

  

#from sage.databases.odlyzko import zeta_zeroes 

  

def is_ComplexNumber(x): 

r""" 

Returns ``True`` if ``x`` is a complex number. In particular, if ``x`` is 

of the :class:`ComplexNumber` type. 

  

EXAMPLES:: 

  

sage: from sage.rings.complex_number import is_ComplexNumber 

sage: a = ComplexNumber(1,2); a 

1.00000000000000 + 2.00000000000000*I 

sage: is_ComplexNumber(a) 

True 

sage: b = ComplexNumber(1); b 

1.00000000000000 

sage: is_ComplexNumber(b) 

True 

  

Note that the global element ``I`` is of type :class:`SymbolicConstant`. 

However, elements of the class :class:`ComplexField_class` are of type 

:class:`ComplexNumber`:: 

  

sage: c = 1 + 2*I 

sage: is_ComplexNumber(c) 

False 

sage: d = CC(1 + 2*I) 

sage: is_ComplexNumber(d) 

True 

""" 

return isinstance(x, ComplexNumber) 

  

cdef class ComplexNumber(sage.structure.element.FieldElement): 

""" 

A floating point approximation to a complex number using any 

specified precision. Answers derived from calculations with such 

approximations may differ from what they would be if those 

calculations were performed with true complex numbers. This is due 

to the rounding errors inherent to finite precision calculations. 

  

EXAMPLES:: 

  

sage: I = CC.0 

sage: b = 1.5 + 2.5*I 

sage: loads(b.dumps()) == b 

True 

""" 

  

cdef ComplexNumber _new(self): 

""" 

Quickly creates a new initialized complex number with the same 

parent as ``self``. 

""" 

cdef ComplexNumber x 

x = ComplexNumber.__new__(ComplexNumber) 

x._parent = self._parent 

x._prec = self._prec 

x._multiplicative_order = None 

mpfr_init2(x.__re, self._prec) 

mpfr_init2(x.__im, self._prec) 

return x 

  

def __cinit__(self, parent=None, real=None, imag=None): 

""" 

Cython initialize ``self``. 

  

EXAMPLES:: 

  

sage: ComplexNumber(2,1) # indirect doctest 

2.00000000000000 + 1.00000000000000*I 

""" 

self._prec = -1 

  

def __init__(self, parent, real, imag=None): 

r""" 

Initialize :class:`ComplexNumber` instance. 

  

EXAMPLES:: 

  

sage: a = ComplexNumber(2,1) 

sage: a.__init__(CC,2,1) 

sage: a 

2.00000000000000 + 1.00000000000000*I 

sage: parent(a) 

Complex Field with 53 bits of precision 

sage: real(a) 

2.00000000000000 

sage: imag(a) 

1.00000000000000 

  

Conversion from gmpy2 numbers:: 

  

sage: from gmpy2 import * # optional - gmpy2 

sage: c = mpc('2.0+1.0j') # optional - gmpy2 

sage: CC(c) # optional - gmpy2 

2.00000000000000 + 1.00000000000000*I 

""" 

cdef RealNumber rr, ii 

self._parent = parent 

self._prec = self._parent._prec 

self._multiplicative_order = None 

  

mpfr_init2(self.__re, self._prec) 

mpfr_init2(self.__im, self._prec) 

  

if imag is None: 

if real is None: return 

  

if isinstance(real, ComplexNumber): 

real, imag = (<ComplexNumber>real).real(), (<ComplexNumber>real).imag() 

elif isinstance(real, sage.libs.pari.all.pari_gen): 

real, imag = real.real(), real.imag() 

elif isinstance(real, list) or isinstance(real, tuple): 

re, imag = real 

real = re 

elif isinstance(real, complex): 

real, imag = real.real, real.imag 

elif HAVE_GMPY2 and type(real) is gmpy2.mpc: 

real, imag = (<gmpy2.mpc>real).real, (<gmpy2.mpc>real).imag 

else: 

imag = 0 

try: 

R = parent._real_field() 

rr = R(real) 

ii = R(imag) 

mpfr_set(self.__re, rr.value, rnd) 

mpfr_set(self.__im, ii.value, rnd) 

except TypeError: 

raise TypeError("unable to coerce to a ComplexNumber: %s" % type(real)) 

  

  

def __dealloc__(self): 

""" 

TESTS: 

  

Check that :trac:`12038` is resolved:: 

  

sage: from sage.rings.complex_number import ComplexNumber as CN 

sage: coerce(CN, 1+I) 

Traceback (most recent call last): 

... 

TypeError: __init__() takes at least 2 positional arguments (1 given) 

""" 

if self._prec != -1: 

mpfr_clear(self.__re) 

mpfr_clear(self.__im) 

  

def _interface_init_(self, I=None): 

""" 

Returns ``self`` formatted as a string, suitable as input to another 

computer algebra system. (This is the default function used for 

exporting to other computer algebra systems.) 

  

EXAMPLES:: 

  

sage: s1 = CC(exp(I)); s1 

0.540302305868140 + 0.841470984807897*I 

sage: s1._interface_init_() 

'0.54030230586813977 + 0.84147098480789650*I' 

sage: s1 == CC(gp(s1)) 

True 

""" 

return self.str() 

  

def _maxima_init_(self, I=None): 

""" 

Return a string representation of this complex number in the syntax of 

Maxima. That is, use ``%i`` to represent the complex unit. 

  

EXAMPLES:: 

  

sage: CC.0._maxima_init_() 

'1.0000000000000000*%i' 

sage: CC(.5 + I)._maxima_init_() 

'0.50000000000000000 + 1.0000000000000000*%i' 

""" 

return self.str(istr='%i') 

  

@property 

def __array_interface__(self): 

""" 

Used for NumPy conversion. 

  

EXAMPLES:: 

  

sage: import numpy 

sage: numpy.array([1.0, 2.5j]).dtype 

dtype('complex128') 

sage: numpy.array([1.000000000000000000000000000000000000j]).dtype 

dtype('O') 

""" 

if self._prec <= 53: 

return numpy_complex_interface 

else: 

return numpy_object_interface 

  

def _sage_input_(self, sib, coerced): 

r""" 

Produce an expression which will reproduce this value when evaluated. 

  

EXAMPLES:: 

  

sage: for prec in (2, 53, 200): 

....: fld = ComplexField(prec) 

....: var = polygen(fld) 

....: ins = [-20, 0, 1, -2^4000, 2^-4000] + [fld._real_field().random_element() for _ in range(3)] 

....: for v1 in ins: 

....: for v2 in ins: 

....: v = fld(v1, v2) 

....: _ = sage_input(fld(v), verify=True) 

....: _ = sage_input(fld(v) * var, verify=True) 

sage: x = polygen(CC) 

sage: for v1 in [-2, 0, 2]: 

....: for v2 in [-2, -1, 0, 1, 2]: 

....: print(str(sage_input(x + CC(v1, v2))).splitlines()[1]) 

x + CC(-2 - RR(2)*I) 

x + CC(-2 - RR(1)*I) 

x - 2 

x + CC(-2 + RR(1)*I) 

x + CC(-2 + RR(2)*I) 

x - CC(RR(2)*I) 

x - CC(RR(1)*I) 

x 

x + CC(RR(1)*I) 

x + CC(RR(2)*I) 

x + CC(2 - RR(2)*I) 

x + CC(2 - RR(1)*I) 

x + 2 

x + CC(2 + RR(1)*I) 

x + CC(2 + RR(2)*I) 

sage: from sage.misc.sage_input import SageInputBuilder 

sage: sib = SageInputBuilder() 

sage: sib_np = SageInputBuilder(preparse=False) 

sage: CC(-infinity)._sage_input_(sib, True) 

{unop:- {call: {atomic:RR}({atomic:Infinity})}} 

sage: CC(0, infinity)._sage_input_(sib, True) 

{call: {atomic:CC}({call: {atomic:RR}({atomic:0})}, {call: {atomic:RR}({atomic:Infinity})})} 

sage: CC(NaN, 5)._sage_input_(sib, True) 

{call: {atomic:CC}({call: {atomic:RR}({atomic:NaN})}, {call: {atomic:RR}({atomic:5})})} 

sage: CC(5, NaN)._sage_input_(sib, True) 

{call: {atomic:CC}({call: {atomic:RR}({atomic:5})}, {call: {atomic:RR}({atomic:NaN})})} 

sage: CC(12345)._sage_input_(sib, True) 

{atomic:12345} 

sage: CC(-12345)._sage_input_(sib, False) 

{call: {atomic:CC}({binop:+ {unop:- {atomic:12345}} {binop:* {call: {atomic:RR}({atomic:0})} {atomic:I}}})} 

sage: CC(0, 12345)._sage_input_(sib, True) 

{call: {atomic:CC}({binop:* {call: {atomic:RR}({atomic:12345})} {atomic:I}})} 

sage: CC(0, -12345)._sage_input_(sib, False) 

{unop:- {call: {atomic:CC}({binop:* {call: {atomic:RR}({atomic:12345})} {atomic:I}})}} 

sage: CC(1.579)._sage_input_(sib, True) 

{atomic:1.579} 

sage: CC(1.579)._sage_input_(sib_np, True) 

{atomic:1.579} 

sage: ComplexField(150).zeta(37)._sage_input_(sib, True) 

{call: {call: {atomic:ComplexField}({atomic:150})}({binop:+ {atomic:0.98561591034770846226477029397621845736859851519} {binop:* {call: {call: {atomic:RealField}({atomic:150})}({atomic:0.16900082032184907409303555538443060626072476297})} {atomic:I}}})} 

sage: ComplexField(150).zeta(37)._sage_input_(sib_np, True) 

{call: {call: {atomic:ComplexField}({atomic:150})}({binop:+ {call: {call: {atomic:RealField}({atomic:150})}({atomic:'0.98561591034770846226477029397621845736859851519'})} {binop:* {call: {call: {atomic:RealField}({atomic:150})}({atomic:'0.16900082032184907409303555538443060626072476297'})} {atomic:I}}})} 

""" 

if coerced and self.imag() == 0: 

return sib(self.real(), True) 

  

# The body will be coerced first to symbolics and then to CC. 

# This works fine if we produce integer or float literals, but 

# not for infinity or NaN. 

if not (mpfr_number_p(self.__re) and mpfr_number_p(self.__im)): 

return sib(self.parent())(self.real(), self.imag()) 

  

# The following uses of .sum() and .prod() will simplify 

# 3+0*I to 3, 0+1*I to I, etc. 

real_part = sib(self.real(), 2) 

imag_part = sib.prod([sib(self.imag()), sib.name('I')], 

simplify=True) 

sum = sib.sum([real_part, imag_part], simplify=True) 

if sum._sie_is_negation(): 

return -sib(self.parent())(sum._sie_operand) 

else: 

return sib(self.parent())(sum) 

  

# The following is an (untested) implementation that produces 

# CC_I = CC.gen() 

# 2 + 3*CC_I 

# instead of CC(2 + 3*I) 

# cdef int prec 

  

# if self.real().is_zero() and self.imag() == 1: 

# v = sib(self.parent()).gen() 

# prec = self.prec() 

# if prec == 53: 

# gen_name = 'CC_I' 

# else: 

# gen_name = 'CC%d_I' % prec 

# sib.cache(self, v, gen_name) 

  

# real_part = sib(self.real()) 

# imag_part = sib.prod([self.imag(), self.parent().gen()], simplify=True) 

# return sib.sum([real_part, imag_part], simplify=True) 

  

def _repr_(self): 

r""" 

Returns ``self`` formatted as a string. 

  

EXAMPLES:: 

  

sage: a = ComplexNumber(2,1); a 

2.00000000000000 + 1.00000000000000*I 

sage: a._repr_() 

'2.00000000000000 + 1.00000000000000*I' 

""" 

return self.str(truncate=True) 

  

def __hash__(self): 

""" 

Returns the hash of ``self``, which coincides with the python complex 

and float (and often int) types. 

  

This has the drawback that two very close high precision numbers 

will have the same hash, but allows them to play nicely with other 

real types. 

  

EXAMPLES:: 

  

sage: hash(CC(1.2, 33)) == hash(complex(1.2, 33)) 

True 

""" 

return hash(complex(self)) 

  

def __getitem__(self, i): 

r""" 

Returns either the real or imaginary component of self depending on 

the choice of i: real (i=0), imaginary (i=1) 

  

INPUT: 

  

- ``i`` - 0 or 1 

- ``0`` -- will return the real component of ``self`` 

- ``1`` -- will return the imaginary component of ``self`` 

  

EXAMPLES:: 

  

sage: a = ComplexNumber(2,1) 

sage: a.__getitem__(0) 

2.00000000000000 

sage: a.__getitem__(1) 

1.00000000000000 

  

:: 

  

sage: b = CC(42,0) 

sage: b 

42.0000000000000 

sage: b.__getitem__(1) 

0.000000000000000 

""" 

if i == 0: 

return self.real() 

elif i == 1: 

return self.imag() 

raise IndexError("i must be between 0 and 1.") 

  

def __reduce__( self ): 

""" 

Pickling support 

  

EXAMPLES:: 

  

sage: a = CC(1 + I) 

sage: loads(dumps(a)) == a 

True 

""" 

# TODO: This is potentially slow -- make a 1 version that 

# is native and much faster -- doesn't use .real()/.imag() 

return (make_ComplexNumber0, (self._parent, self._multiplicative_order, self.real(), self.imag())) 

  

def _set_multiplicative_order(self, n): 

r""" 

Function for setting the attribute :meth:`multiplicative_order` of 

``self``. 

  

.. WARNING:: 

  

It is not advisable to explicitly call 

``_set_multiplicative_order()`` for explicitly declared complex 

numbers. 

  

INPUT: 

  

- ``n`` -- an integer which will define the multiplicative order 

  

EXAMPLES:: 

  

sage: a = ComplexNumber(2,1) 

sage: a.multiplicative_order() 

+Infinity 

sage: a._set_multiplicative_order(5) 

sage: a.multiplicative_order() 

5 

sage: a^5 

-38.0000000000000 + 41.0000000000000*I 

""" 

self._multiplicative_order = integer.Integer(n) 

  

def str(self, base=10, istr='I', **kwds): 

r""" 

Return a string representation of ``self``. 

  

INPUT: 

  

- ``base`` -- (default: 10) base for output 

  

- ``istr`` -- (default: ``I``) String representation of the complex unit 

  

- ``**kwds`` -- other arguments to pass to the ``str()`` 

method of the real numbers in the real and imaginary parts. 

  

EXAMPLES:: 

  

sage: a = CC(pi + I*e) 

sage: a 

3.14159265358979 + 2.71828182845905*I 

sage: a.str(truncate=True) 

'3.14159265358979 + 2.71828182845905*I' 

sage: a.str() 

'3.1415926535897931 + 2.7182818284590451*I' 

sage: a.str(base=2) 

'11.001001000011111101101010100010001000010110100011000 + 10.101101111110000101010001011000101000101011101101001*I' 

sage: CC(0.5 + 0.625*I).str(base=2) 

'0.10000000000000000000000000000000000000000000000000000 + 0.10100000000000000000000000000000000000000000000000000*I' 

sage: a.str(base=16) 

'3.243f6a8885a30 + 2.b7e151628aed2*I' 

sage: a.str(base=36) 

'3.53i5ab8p5fc + 2.puw5nggjf8f*I' 

sage: CC(0) 

0.000000000000000 

sage: CC.0.str(istr='%i') 

'1.0000000000000000*%i' 

""" 

s = "" 

if self.real(): 

s = self.real().str(base, **kwds) 

if self.imag(): 

y = self.imag() 

if s: 

if y < 0: 

s += " - " 

y = -y 

else: 

s += " + " 

ystr = y.str(base, **kwds) 

s += ystr + "*" + istr 

if not s: 

s = self.real().str(base, **kwds) 

return s 

  

def _latex_(self): 

r""" 

Method for converting ``self`` to a string with latex formatting. 

Called by the global function ``latex``. 

  

EXAMPLES:: 

  

sage: a = ComplexNumber(2,1) 

sage: a 

2.00000000000000 + 1.00000000000000*I 

sage: latex(a) 

2.00000000000000 + 1.00000000000000i 

sage: a._latex_() 

'2.00000000000000 + 1.00000000000000i' 

  

:: 

  

sage: b = ComplexNumber(7,4,min_prec=16) 

sage: b 

7.000 + 4.000*I 

sage: latex(b) 

7.000 + 4.000i 

sage: b._latex_() 

'7.000 + 4.000i' 

  

:: 

  

sage: ComplexNumber(0).log()._latex_() 

'-\\infty' 

""" 

import re 

s = repr(self).replace('*I', 'i').replace('infinity','\\infty') 

return re.sub(r"e(-?\d+)", r" \\times 10^{\1}", s) 

  

def __pari__(self): 

r""" 

Coerces ``self`` into a PARI ``t_COMPLEX`` object, 

or a ``t_REAL`` if ``self`` is real. 

  

EXAMPLES: 

  

Coerce the object using the ``pari`` function:: 

  

sage: a = ComplexNumber(2,1) 

sage: pari(a) 

2.00000000000000 + 1.00000000000000*I 

sage: pari(a).type() 

't_COMPLEX' 

sage: type(pari(a)) 

<type 'cypari2.gen.Gen'> 

sage: a.__pari__() 

2.00000000000000 + 1.00000000000000*I 

sage: type(a.__pari__()) 

<type 'cypari2.gen.Gen'> 

sage: a = CC(pi) 

sage: pari(a) 

3.14159265358979 

sage: pari(a).type() 

't_REAL' 

sage: a = CC(-2).sqrt() 

sage: pari(a) 

1.41421356237310*I 

""" 

if self.is_real(): 

return self.real().__pari__() 

return sage.libs.pari.all.pari.complex(self.real() or 0, self.imag()) 

  

def __mpc__(self): 

""" 

Convert Sage ``ComplexNumber`` to gmpy2 ``mpc``. 

  

EXAMPLES:: 

  

sage: c = ComplexNumber(2,1) 

sage: c.__mpc__() # optional - gmpy2 

mpc('2.0+1.0j') 

sage: from gmpy2 import mpc # optional - gmpy2 

sage: mpc(c) # optional - gmpy2 

mpc('2.0+1.0j') 

sage: CF = ComplexField(134)  

sage: mpc(CF.pi()).precision # optional - gmpy2 

(134, 134) 

sage: CF = ComplexField(45)  

sage: mpc(CF.zeta(5)).precision # optional - gmpy2 

(45, 45) 

sage: CF = ComplexField(255) 

sage: x = CF(5, 8) 

sage: y = mpc(x) # optional - gmpy2 

sage: y.precision # optional - gmpy2 

(255, 255) 

sage: CF(y) == x # optional - gmpy2 

True 

sage: x = mpc('1.324+4e50j', precision=(70,70)) # optional - gmpy2 

sage: CF = ComplexField(70) 

sage: y = CF(x) # optional - gmpy2 

sage: x == mpc(y) # optional - gmpy2 

True 

  

TESTS:: 

  

sage: c.__mpc__(); raise NotImplementedError("gmpy2 is not installed") 

Traceback (most recent call last): 

... 

NotImplementedError: gmpy2 is not installed 

""" 

IF HAVE_GMPY2: 

return gmpy2.GMPy_MPC_From_mpfr(self.__re, self.__im) 

ELSE: 

raise NotImplementedError("gmpy2 is not installed") 

  

  

def _mpmath_(self, prec=None, rounding=None): 

""" 

Returns an mpmath version of ``self``. 

  

.. NOTE:: 

  

Currently, the rounding mode is ignored. 

  

EXAMPLES:: 

  

sage: CC(1,2)._mpmath_() 

mpc(real='1.0', imag='2.0') 

""" 

if prec is not None: 

from .complex_field import ComplexField 

return ComplexField(prec)(self)._mpmath_() 

from sage.libs.mpmath.all import make_mpc 

re = mpfr_to_mpfval(self.__re) 

im = mpfr_to_mpfval(self.__im) 

return make_mpc((re, im)) 

  

cpdef _add_(self, right): 

""" 

Add ``self`` to ``right``. 

  

EXAMPLES:: 

  

sage: CC(2, 1)._add_(CC(1, -2)) 

3.00000000000000 - 1.00000000000000*I 

""" 

cdef ComplexNumber x 

x = self._new() 

mpfr_add(x.__re, self.__re, (<ComplexNumber>right).__re, rnd) 

mpfr_add(x.__im, self.__im, (<ComplexNumber>right).__im, rnd) 

return x 

  

cpdef _sub_(self, right): 

""" 

Subtract ``right`` from ``self``. 

  

EXAMPLES:: 

  

sage: CC(2, 1)._sub_(CC(1, -2)) 

1.00000000000000 + 3.00000000000000*I 

""" 

cdef ComplexNumber x 

x = self._new() 

mpfr_sub(x.__re, self.__re, (<ComplexNumber>right).__re, rnd) 

mpfr_sub(x.__im, self.__im, (<ComplexNumber>right).__im, rnd) 

return x 

  

cpdef _mul_(self, right): 

""" 

Multiply ``self`` by ``right``. 

  

EXAMPLES:: 

  

sage: CC(2, 1)._mul_(CC(1, -2)) 

4.00000000000000 - 3.00000000000000*I 

""" 

cdef ComplexNumber x 

x = self._new() 

cdef mpfr_t t0, t1 

mpfr_init2(t0, self._prec) 

mpfr_init2(t1, self._prec) 

mpfr_mul(t0, self.__re, (<ComplexNumber>right).__re, rnd) 

mpfr_mul(t1, self.__im, (<ComplexNumber>right).__im, rnd) 

mpfr_sub(x.__re, t0, t1, rnd) 

mpfr_mul(t0, self.__re, (<ComplexNumber>right).__im, rnd) 

mpfr_mul(t1, self.__im, (<ComplexNumber>right).__re, rnd) 

mpfr_add(x.__im, t0, t1, rnd) 

mpfr_clear(t0) 

mpfr_clear(t1) 

return x 

  

def norm(self): 

r""" 

Returns the norm of this complex number. 

  

If `c = a + bi` is a complex number, then the norm of `c` is defined as 

the product of `c` and its complex conjugate: 

  

.. MATH:: 

  

\text{norm}(c) 

= 

\text{norm}(a + bi) 

= 

c \cdot \overline{c} 

= 

a^2 + b^2. 

  

The norm of a complex number is different from its absolute value. 

The absolute value of a complex number is defined to be the square 

root of its norm. A typical use of the complex norm is in the 

integral domain `\ZZ[i]` of Gaussian integers, where the norm of 

each Gaussian integer `c = a + bi` is defined as its complex norm. 

  

.. SEEALSO:: 

  

- :func:`sage.misc.functional.norm` 

  

- :meth:`sage.rings.complex_double.ComplexDoubleElement.norm` 

  

EXAMPLES: 

  

This indeed acts as the square function when the 

imaginary component of ``self`` is equal to zero:: 

  

sage: a = ComplexNumber(2,1) 

sage: a.norm() 

5.00000000000000 

sage: b = ComplexNumber(4.2,0) 

sage: b.norm() 

17.6400000000000 

sage: b^2 

17.6400000000000 

""" 

return self.norm_c() 

  

cdef RealNumber norm_c(ComplexNumber self): 

cdef RealNumber x 

x = RealNumber(self._parent._real_field(), None) 

  

cdef mpfr_t t0, t1 

mpfr_init2(t0, self._prec) 

mpfr_init2(t1, self._prec) 

  

mpfr_mul(t0, self.__re, self.__re, rnd) 

mpfr_mul(t1, self.__im, self.__im, rnd) 

  

mpfr_add(x.value, t0, t1, rnd) 

  

mpfr_clear(t0) 

mpfr_clear(t1) 

return x 

  

cdef RealNumber abs_c(ComplexNumber self): 

cdef RealNumber x 

x = RealNumber(self._parent._real_field(), None) 

  

cdef mpfr_t t0, t1 

mpfr_init2(t0, self._prec) 

mpfr_init2(t1, self._prec) 

  

mpfr_mul(t0, self.__re, self.__re, rnd) 

mpfr_mul(t1, self.__im, self.__im, rnd) 

  

mpfr_add(x.value, t0, t1, rnd) 

mpfr_sqrt(x.value, x.value, rnd) 

  

mpfr_clear(t0) 

mpfr_clear(t1) 

return x 

  

cpdef _div_(self, right): 

""" 

Divide ``self`` by ``right``. 

  

EXAMPLES:: 

  

sage: CC(2, 1)._div_(CC(1, -2)) 

1.00000000000000*I 

""" 

cdef ComplexNumber x 

x = self._new() 

cdef mpfr_t a, b, t0, t1, right_nm 

mpfr_init2(t0, self._prec) 

mpfr_init2(t1, self._prec) 

mpfr_init2(a, self._prec) 

mpfr_init2(b, self._prec) 

mpfr_init2(right_nm, self._prec) 

  

mpfr_mul(t0, (<ComplexNumber>right).__re, (<ComplexNumber>right).__re, rnd) 

mpfr_mul(t1, (<ComplexNumber>right).__im, (<ComplexNumber>right).__im, rnd) 

mpfr_add(right_nm, t0, t1, rnd) 

  

mpfr_div(a, (<ComplexNumber>right).__re, right_nm, rnd) 

mpfr_div(b, (<ComplexNumber>right).__im, right_nm, rnd) 

  

## Do this: x.__re = a * self.__re + b * self.__im 

mpfr_mul(t0, a, self.__re, rnd) 

mpfr_mul(t1, b, self.__im, rnd) 

mpfr_add(x.__re, t0, t1, rnd) 

  

## Do this: x.__im = a * self.__im - b * self.__re 

mpfr_mul(t0, a, self.__im, rnd) 

mpfr_mul(t1, b, self.__re, rnd) 

mpfr_sub(x.__im, t0, t1, rnd) 

mpfr_clear(t0) 

mpfr_clear(t1) 

mpfr_clear(a) 

mpfr_clear(b) 

mpfr_clear(right_nm) 

return x 

  

def __rdiv__(self, left): 

r""" 

Returns the quotient of left with ``self``, that is: 

  

``left/self`` 

  

as a complex number. 

  

INPUT: 

  

- ``left`` - a complex number to divide by ``self`` 

  

EXAMPLES:: 

  

sage: a = ComplexNumber(2,0) 

sage: a.__rdiv__(CC(1)) 

0.500000000000000 

sage: CC(1)/a 

0.500000000000000 

""" 

return ComplexNumber(self._parent, left)/self 

  

def __pow__(self, right, modulus): 

r""" 

Raise ``self`` to the ``right`` exponent. 

  

This takes `a^b` and compues `\exp(b \log(a))`. 

  

EXAMPLES:: 

  

sage: C.<i> = ComplexField(20) 

sage: a = i^2; a 

-1.0000 

sage: a.parent() 

Complex Field with 20 bits of precision 

sage: a = (1+i)^i; a 

0.42883 + 0.15487*I 

sage: (1+i)^(1+i) 

0.27396 + 0.58370*I 

sage: a.parent() 

Complex Field with 20 bits of precision 

sage: i^i 

0.20788 

sage: (2+i)^(0.5) 

1.4553 + 0.34356*I 

""" 

if isinstance(right, (int, long, integer.Integer)): 

return RingElement.__pow__(self, right) 

  

try: 

return (self.log()*right).exp() 

except TypeError: 

pass 

  

try: 

self = right.parent()(self) 

return self**right 

except AttributeError: 

raise TypeError 

  

def _magma_init_(self, magma): 

r""" 

EXAMPLES:: 

  

sage: magma(CC([1, 2])) # indirect doctest, optional - magma 

1.00000000000000 + 2.00000000000000*$.1 

sage: v = magma(CC([1, 2])).sage(); v # indirect, optional - magma 

1.00000000000000 + 2.00000000000000*I 

sage: v.parent() # optional - magma 

Complex Field with 53 bits of precision 

  

sage: i = ComplexField(200).gen() 

sage: sqrt(i) 

0.70710678118654752440084436210484903928483593768847403658834 + 0.70710678118654752440084436210484903928483593768847403658834*I 

sage: magma(sqrt(i)) # indirect, optional - magma 

0.707106781186547524400844362104849039284835937688474036588340 + 0.707106781186547524400844362104849039284835937688474036588340*$.1 

sage: magma(i).Sqrt() # indirect, optional - magma 

0.707106781186547524400844362104849039284835937688474036588340 + 0.707106781186547524400844362104849039284835937688474036588340*$.1 

  

sage: magma(ComplexField(200)(1/3)) # indirect, optional - magma 

0.333333333333333333333333333333333333333333333333333333333333 

""" 

real_string = self.real().str() 

imag_string = self.imag().str() 

digit_precision_bound = len(real_string) 

return "%s![%sp%s, %sp%s]" % (self.parent()._magma_init_(magma), 

real_string, digit_precision_bound, 

imag_string, digit_precision_bound) 

  

def __nonzero__(self): 

""" 

Return ``True`` if ``self`` is not zero. This is an internal function; 

use :meth:`is_zero()` instead. 

  

EXAMPLES:: 

  

sage: z = 1 + CC(I) 

sage: z.is_zero() 

False 

""" 

return not (mpfr_zero_p(self.__re) and mpfr_zero_p(self.__im)) 

  

def prec(self): 

""" 

Return precision of this complex number. 

  

EXAMPLES:: 

  

sage: i = ComplexField(2000).0 

sage: i.prec() 

2000 

""" 

return self._parent.prec() 

  

def real(self): 

""" 

Return real part of ``self``. 

  

EXAMPLES:: 

  

sage: i = ComplexField(100).0 

sage: z = 2 + 3*i 

sage: x = z.real(); x 

2.0000000000000000000000000000 

sage: x.parent() 

Real Field with 100 bits of precision 

sage: z.real_part() 

2.0000000000000000000000000000 

""" 

cdef RealNumber x 

x = RealNumber(self._parent._real_field(), None) 

mpfr_set(x.value, self.__re, rnd) 

return x 

  

real_part = real 

  

def imag(self): 

""" 

Return imaginary part of ``self``. 

  

EXAMPLES:: 

  

sage: i = ComplexField(100).0 

sage: z = 2 + 3*i 

sage: x = z.imag(); x 

3.0000000000000000000000000000 

sage: x.parent() 

Real Field with 100 bits of precision 

sage: z.imag_part() 

3.0000000000000000000000000000 

""" 

cdef RealNumber x 

x = RealNumber(self._parent._real_field(), None) 

mpfr_set(x.value, self.__im, rnd) 

return x 

  

imag_part = imag 

  

def __neg__(self): 

r""" 

Method for computing the negative of ``self``. 

  

.. MATH:: 

  

-(a + bi) = -a - bi 

  

EXAMPLES:: 

  

sage: a = ComplexNumber(2,1) 

sage: -a 

-2.00000000000000 - 1.00000000000000*I 

sage: a.__neg__() 

-2.00000000000000 - 1.00000000000000*I 

""" 

cdef ComplexNumber x 

x = self._new() 

mpfr_neg(x.__re, self.__re, rnd) 

mpfr_neg(x.__im, self.__im, rnd) 

return x 

  

def __pos__(self): 

r""" 

Method for computing the "positive" of ``self``. 

  

EXAMPLES:: 

  

sage: a = ComplexNumber(2,1) 

sage: +a 

2.00000000000000 + 1.00000000000000*I 

sage: a.__pos__() 

2.00000000000000 + 1.00000000000000*I 

""" 

return self 

  

def __abs__(self): 

r""" 

Method for computing the absolute value or modulus of ``self`` 

  

.. MATH:: 

  

`|a + bi| = sqrt(a^2 + b^2)` 

  

EXAMPLES: 

  

Note that the absolute value of a complex number with imaginary 

component equal to zero is the absolute value of the real component. 

  

:: 

  

sage: a = ComplexNumber(2,1) 

sage: abs(a) 

2.23606797749979 

sage: a.__abs__() 

2.23606797749979 

sage: float(sqrt(2^2 + 1^1)) 

2.23606797749979 

  

:: 

  

sage: b = ComplexNumber(42,0) 

sage: abs(b) 

42.0000000000000 

sage: b.__abs__() 

42.0000000000000 

sage: b 

42.0000000000000 

""" 

return self.abs_c() 

  

def __invert__(self): 

""" 

Return the multiplicative inverse. 

  

EXAMPLES:: 

  

sage: I = CC.0 

sage: a = ~(5+I) 

sage: a * (5+I) 

1.00000000000000 

""" 

cdef ComplexNumber x 

x = self._new() 

  

cdef mpfr_t t0, t1 

mpfr_init2(t0, self._prec) 

mpfr_init2(t1, self._prec) 

  

mpfr_mul(t0, self.__re, self.__re, rnd) 

mpfr_mul(t1, self.__im, self.__im, rnd) 

  

mpfr_add(t0, t0, t1, rnd) # now t0 is the norm 

mpfr_div(x.__re, self.__re, t0, rnd) # x.__re = self.__re/norm 

  

mpfr_neg(t1, self.__im, rnd) 

mpfr_div(x.__im, t1, t0, rnd) # x.__im = -self.__im/norm 

  

mpfr_clear(t0) 

mpfr_clear(t1) 

  

return x 

  

def __int__(self): 

r""" 

Method for converting ``self`` to type ``int``. 

  

Called by the ``int`` function. Note that calling this method returns 

an error since, in general, complex numbers cannot be coerced into 

integers. 

  

EXAMPLES:: 

  

sage: a = ComplexNumber(2,1) 

sage: int(a) 

Traceback (most recent call last): 

... 

TypeError: can't convert complex to int; use int(abs(z)) 

sage: a.__int__() 

Traceback (most recent call last): 

... 

TypeError: can't convert complex to int; use int(abs(z)) 

""" 

raise TypeError("can't convert complex to int; use int(abs(z))") 

  

def __long__(self): 

r""" 

Method for converting ``self`` to type ``long``. 

  

Called by the ``long`` function. Note that calling this method 

returns an error since, in general, complex numbers cannot be 

coerced into integers. 

  

EXAMPLES:: 

  

sage: a = ComplexNumber(2,1) 

sage: long(a) 

Traceback (most recent call last): 

... 

TypeError: can't convert complex to long; use long(abs(z)) 

sage: a.__long__() 

Traceback (most recent call last): 

... 

TypeError: can't convert complex to long; use long(abs(z)) 

""" 

raise TypeError("can't convert complex to long; use long(abs(z))") 

  

def __float__(self): 

r""" 

Method for converting ``self`` to type ``float``. 

  

Called by the ``float`` function. This conversion will throw an error 

if the number has a nonzero imaginary part. 

  

EXAMPLES:: 

  

sage: a = ComplexNumber(1, 0) 

sage: float(a) 

1.0 

sage: a = ComplexNumber(2,1) 

sage: float(a) 

Traceback (most recent call last): 

... 

TypeError: unable to convert 2.00000000000000 + 1.00000000000000*I to float; use abs() or real_part() as desired 

sage: a.__float__() 

Traceback (most recent call last): 

... 

TypeError: unable to convert 2.00000000000000 + 1.00000000000000*I to float; use abs() or real_part() as desired 

sage: float(abs(ComplexNumber(1,1))) 

1.4142135623730951 

""" 

if mpfr_zero_p(self.__im) or mpfr_nan_p(self.__re): 

return mpfr_get_d(self.__re, rnd) 

else: 

raise TypeError("unable to convert {!r} to float; use abs() or real_part() as desired".format(self)) 

  

def __complex__(self): 

r""" 

Method for converting ``self`` to type ``complex``. 

  

Called by the ``complex`` function. 

  

EXAMPLES:: 

  

sage: a = ComplexNumber(2,1) 

sage: complex(a) 

(2+1j) 

sage: type(complex(a)) 

<... 'complex'> 

sage: a.__complex__() 

(2+1j) 

""" 

return complex(mpfr_get_d(self.__re, rnd), 

mpfr_get_d(self.__im, rnd)) 

  

cpdef int _cmp_(left, right) except -2: 

""" 

Compare ``left`` and ``right``. 

  

EXAMPLES:: 

  

sage: CC(2, 1) > CC(-1, 2) 

True 

sage: CC(2, 1) == CC(2, 1) 

True 

""" 

cdef int a, b 

a = mpfr_nan_p(left.__re) 

b = mpfr_nan_p((<ComplexNumber>right).__re) 

if a != b: 

return -1 

  

cdef int i 

i = mpfr_cmp(left.__re, (<ComplexNumber>right).__re) 

if i < 0: 

return -1 

elif i > 0: 

return 1 

i = mpfr_cmp(left.__im, (<ComplexNumber>right).__im) 

if i < 0: 

return -1 

elif i > 0: 

return 1 

return 0 

  

def multiplicative_order(self): 

""" 

Return the multiplicative order of this complex number, if known, 

or raise a ``NotImplementedError``. 

  

EXAMPLES:: 

  

sage: C.<i> = ComplexField() 

sage: i.multiplicative_order() 

4 

sage: C(1).multiplicative_order() 

1 

sage: C(-1).multiplicative_order() 

2 

sage: C(i^2).multiplicative_order() 

2 

sage: C(-i).multiplicative_order() 

4 

sage: C(2).multiplicative_order() 

+Infinity 

sage: w = (1+sqrt(-3.0))/2; w 

0.500000000000000 + 0.866025403784439*I 

sage: abs(w) 

1.00000000000000 

sage: w.multiplicative_order() 

Traceback (most recent call last): 

... 

NotImplementedError: order of element not known 

""" 

if self == 1: 

return integer.Integer(1) 

elif self == -1: 

return integer.Integer(2) 

elif self == self._parent.gen(): 

return integer.Integer(4) 

elif self == -self._parent.gen(): 

return integer.Integer(4) 

elif not self._multiplicative_order is None: 

return integer.Integer(self._multiplicative_order) 

elif abs(abs(self) - 1) > 0.1: # clearly not a root of unity 

return infinity.infinity 

raise NotImplementedError("order of element not known") 

  

  

######################################################################## 

# Plotting 

######################################################################## 

  

def plot(self, **kargs): 

""" 

Plots this complex number as a point in the plane 

  

The accepted options are the ones of :meth:`~sage.plot.point.point2d`. 

Type ``point2d.options`` to see all options. 

  

.. NOTE:: 

  

Just wraps the sage.plot.point.point2d method 

  

EXAMPLES: 

  

You can either use the indirect:: 

  

sage: z = CC(0,1) 

sage: plot(z) 

Graphics object consisting of 1 graphics primitive 

  

or the more direct:: 

  

sage: z = CC(0,1) 

sage: z.plot() 

Graphics object consisting of 1 graphics primitive 

""" 

return sage.plot.point.point2d((self.real(), self.imag()), **kargs) 

  

######################################################################## 

# Transcendental (and other) functions 

######################################################################## 

  

# Trig functions 

def arccos(self): 

""" 

Return the arccosine of ``self``. 

  

EXAMPLES:: 

  

sage: (1+CC(I)).arccos() 

0.904556894302381 - 1.06127506190504*I 

""" 

return self._parent(self.__pari__().acos()) 

  

def arccosh(self): 

""" 

Return the hyperbolic arccosine of ``self``. 

  

EXAMPLES:: 

  

sage: (1+CC(I)).arccosh() 

1.06127506190504 + 0.904556894302381*I 

""" 

return self._parent(self.__pari__().acosh()) 

  

def arcsin(self): 

""" 

Return the arcsine of ``self``. 

  

EXAMPLES:: 

  

sage: (1+CC(I)).arcsin() 

0.666239432492515 + 1.06127506190504*I 

""" 

return self._parent(self.__pari__().asin()) 

  

def arcsinh(self): 

""" 

Return the hyperbolic arcsine of ``self``. 

  

EXAMPLES:: 

  

sage: (1+CC(I)).arcsinh() 

1.06127506190504 + 0.666239432492515*I 

""" 

return self._parent(self.__pari__().asinh()) 

  

def arctan(self): 

""" 

Return the arctangent of ``self``. 

  

EXAMPLES:: 

  

sage: (1+CC(I)).arctan() 

1.01722196789785 + 0.402359478108525*I 

""" 

return self._parent(self.__pari__().atan()) 

  

def arctanh(self): 

""" 

Return the hyperbolic arctangent of ``self``. 

  

EXAMPLES:: 

  

sage: (1+CC(I)).arctanh() 

0.402359478108525 + 1.01722196789785*I 

""" 

return self._parent(self.__pari__().atanh()) 

  

def coth(self): 

""" 

Return the hyperbolic cotangent of ``self``. 

  

EXAMPLES:: 

  

sage: ComplexField(100)(1,1).coth() 

0.86801414289592494863584920892 - 0.21762156185440268136513424361*I 

""" 

return ~(self.tanh()) 

  

def arccoth(self): 

""" 

Return the hyperbolic arccotangent of ``self``. 

  

EXAMPLES:: 

  

sage: ComplexField(100)(1,1).arccoth() 

0.40235947810852509365018983331 - 0.55357435889704525150853273009*I 

""" 

return (~self).arctanh() 

  

def csc(self): 

""" 

Return the cosecant of ``self``. 

  

EXAMPLES:: 

  

sage: ComplexField(100)(1,1).csc() 

0.62151801717042842123490780586 - 0.30393100162842645033448560451*I 

""" 

return ~(self.sin()) 

  

def csch(self): 

""" 

Return the hyperbolic cosecant of ``self``. 

  

EXAMPLES:: 

  

sage: ComplexField(100)(1,1).csch() 

0.30393100162842645033448560451 - 0.62151801717042842123490780586*I 

""" 

return ~(self.sinh()) 

  

def arccsch(self): 

""" 

Return the hyperbolic arccosecant of ``self``. 

  

EXAMPLES:: 

  

sage: ComplexField(100)(1,1).arccsch() 

0.53063753095251782601650945811 - 0.45227844715119068206365839783*I 

""" 

return (~self).arcsinh() 

  

def sec(self): 

""" 

Return the secant of ``self``. 

  

EXAMPLES:: 

  

sage: ComplexField(100)(1,1).sec() 

0.49833703055518678521380589177 + 0.59108384172104504805039169297*I 

""" 

return ~(self.cos()) 

  

def sech(self): 

""" 

Return the hyperbolic secant of ``self``. 

  

EXAMPLES:: 

  

sage: ComplexField(100)(1,1).sech() 

0.49833703055518678521380589177 - 0.59108384172104504805039169297*I 

""" 

return ~(self.cosh()) 

  

def arcsech(self): 

""" 

Return the hyperbolic arcsecant of ``self``. 

  

EXAMPLES:: 

  

sage: ComplexField(100)(1,1).arcsech() 

0.53063753095251782601650945811 - 1.1185178796437059371676632938*I 

""" 

return (~self).arccosh() 

  

def cotan(self): 

""" 

Return the cotangent of ``self``. 

  

EXAMPLES:: 

  

sage: (1+CC(I)).cotan() 

0.217621561854403 - 0.868014142895925*I 

sage: i = ComplexField(200).0 

sage: (1+i).cotan() 

0.21762156185440268136513424360523807352075436916785404091068 - 0.86801414289592494863584920891627388827343874994609327121115*I 

sage: i = ComplexField(220).0 

sage: (1+i).cotan() 

0.21762156185440268136513424360523807352075436916785404091068124239 - 0.86801414289592494863584920891627388827343874994609327121115071646*I 

""" 

return ~(self.tan()) 

  

def cos(self): 

""" 

Return the cosine of ``self``. 

  

EXAMPLES:: 

  

sage: (1+CC(I)).cos() 

0.833730025131149 - 0.988897705762865*I 

""" 

# write self = a + i*b, then 

# cos(self) = cosh(b)*cos(a) - i*sinh(b)*sin(a) 

cdef ComplexNumber z 

z = self._new() 

cdef mpfr_t ch, sh 

mpfr_init2(sh, self._prec) 

mpfr_sinh(sh, self.__im, rnd) 

mpfr_init2(ch, self._prec) 

mpfr_sqr(ch, sh, rnd) 

mpfr_add_ui(ch, ch, 1, rnd) 

mpfr_sqrt(ch, ch, rnd) 

mpfr_neg(sh, sh, rnd) 

mpfr_sin_cos(z.__im, z.__re, self.__re, rnd) 

mpfr_mul(z.__re, z.__re, ch, rnd) 

mpfr_mul(z.__im, z.__im, sh, rnd) 

mpfr_clear(sh) 

mpfr_clear(ch) 

return z 

  

def cosh(self): 

""" 

Return the hyperbolic cosine of ``self``. 

  

EXAMPLES:: 

  

sage: (1+CC(I)).cosh() 

0.833730025131149 + 0.988897705762865*I 

""" 

# write self = a + i*b, then 

# cosh(self) = cosh(a)*cos(b) + i*sinh(a)*sin(b) 

cdef ComplexNumber z 

z = self._new() 

cdef mpfr_t ch, sh 

mpfr_init2(sh, self._prec) 

mpfr_sinh(sh, self.__re, rnd) 

mpfr_init2(ch, self._prec) 

mpfr_sqr(ch, sh, rnd) 

mpfr_add_ui(ch, ch, 1, rnd) 

mpfr_sqrt(ch, ch, rnd) 

mpfr_sin_cos(z.__im, z.__re, self.__im, rnd) 

mpfr_mul(z.__re, z.__re, ch, rnd) 

mpfr_mul(z.__im, z.__im, sh, rnd) 

mpfr_clear(sh) 

mpfr_clear(ch) 

return z 

  

  

  

def eta(self, omit_frac=False): 

r""" 

Return the value of the Dedekind `\eta` function on ``self``, 

intelligently computed using `\mathbb{SL}(2,\ZZ)` 

transformations. 

  

The `\eta` function is 

  

.. MATH:: 

  

\eta(z) = e^{\pi i z / 12} \prod_{n=1}^{\infty}(1-e^{2\pi inz}) 

  

INPUT: 

  

- ``self`` -- element of the upper half plane (if not, 

raises a ``ValueError``). 

  

- ``omit_frac`` -- (bool, default: ``False``), if ``True``, 

omit the `e^{\pi i z / 12}` factor. 

  

OUTPUT: a complex number 

  

ALGORITHM: Uses the PARI C library. 

  

EXAMPLES: 

  

First we compute `\eta(1+i)`:: 

  

sage: i = CC.0 

sage: z = 1+i; z.eta() 

0.742048775836565 + 0.198831370229911*I 

  

We compute eta to low precision directly from the definition:: 

  

sage: z = 1 + i; z.eta() 

0.742048775836565 + 0.198831370229911*I 

sage: pi = CC(pi) # otherwise we will get a symbolic result. 

sage: exp(pi * i * z / 12) * prod([1-exp(2*pi*i*n*z) for n in range(1,10)]) 

0.742048775836565 + 0.198831370229911*I 

  

The optional argument allows us to omit the fractional part:: 

  

sage: z = 1 + i 

sage: z.eta(omit_frac=True) 

0.998129069925959 

sage: prod([1-exp(2*pi*i*n*z) for n in range(1,10)]) 

0.998129069925958 + 4.59099857829247e-19*I 

  

We illustrate what happens when `z` is not in the upper 

half plane:: 

  

sage: z = CC(1) 

sage: z.eta() 

Traceback (most recent call last): 

... 

ValueError: value must be in the upper half plane 

  

You can also use functional notation:: 

  

sage: eta(1+CC(I)) 

0.742048775836565 + 0.198831370229911*I 

""" 

try: 

return self._parent(self.__pari__().eta(not omit_frac)) 

except sage.libs.pari.all.PariError: 

raise ValueError("value must be in the upper half plane") 

  

  

def sin(self): 

""" 

Return the sine of ``self``. 

  

EXAMPLES:: 

  

sage: (1+CC(I)).sin() 

1.29845758141598 + 0.634963914784736*I 

""" 

# write self = a + i*b, then 

# sin(self) = cosh(b)*sin(a) + i*sinh(b)*cos(a) 

cdef ComplexNumber z 

z = self._new() 

cdef mpfr_t ch, sh 

mpfr_init2(sh, self._prec) 

mpfr_sinh(sh, self.__im, rnd) 

mpfr_init2(ch, self._prec) 

mpfr_sqr(ch, sh, rnd) 

mpfr_add_ui(ch, ch, 1, rnd) 

mpfr_sqrt(ch, ch, rnd) 

mpfr_sin_cos(z.__re, z.__im, self.__re, rnd) 

mpfr_mul(z.__re, z.__re, ch, rnd) 

mpfr_mul(z.__im, z.__im, sh, rnd) 

mpfr_clear(sh) 

mpfr_clear(ch) 

return z 

  

def sinh(self): 

""" 

Return the hyperbolic sine of ``self``. 

  

EXAMPLES:: 

  

sage: (1+CC(I)).sinh() 

0.634963914784736 + 1.29845758141598*I 

""" 

# write self = a + i*b, then 

# sinh(self) = sinh(a)*cos(b) + i*cosh(a)*sin(b) 

cdef ComplexNumber z 

z = self._new() 

cdef mpfr_t ch, sh 

mpfr_init2(sh, self._prec) 

mpfr_sinh(sh, self.__re, rnd) 

mpfr_init2(ch, self._prec) 

mpfr_sqr(ch, sh, rnd) 

mpfr_add_ui(ch, ch, 1, rnd) 

mpfr_sqrt(ch, ch, rnd) 

mpfr_sin_cos(z.__im, z.__re, self.__im, rnd) 

mpfr_mul(z.__re, z.__re, sh, rnd) 

mpfr_mul(z.__im, z.__im, ch, rnd) 

mpfr_clear(sh) 

mpfr_clear(ch) 

return z 

  

def tan(self): 

""" 

Return the tangent of ``self``. 

  

EXAMPLES:: 

  

sage: (1+CC(I)).tan() 

0.271752585319512 + 1.08392332733869*I 

""" 

# write self = a + i*b, then 

# tan(self) = [cos(a)*sin(a) + i*cosh(b)*sinh(b)]/[sinh^2(b)+cos^2(a)] 

cdef ComplexNumber z 

z = self._new() 

cdef mpfr_t ch, sh, c, s, a, b 

mpfr_init2(sh, self._prec) 

mpfr_sinh(sh, self.__im, rnd) 

mpfr_init2(ch, self._prec) 

mpfr_init2(a, self._prec) 

mpfr_sqr(a, sh, rnd) 

mpfr_add_ui(ch, a, 1, rnd) 

mpfr_sqrt(ch, ch, rnd) 

mpfr_init2(c, self._prec) 

mpfr_init2(s, self._prec) 

mpfr_sin_cos(s, c, self.__re, rnd) 

mpfr_init2(b, self._prec) 

mpfr_sqr(b, c, rnd) 

mpfr_add(a, a, b, rnd) 

mpfr_mul(z.__re, c, s, rnd) 

mpfr_div(z.__re, z.__re, a, rnd) 

mpfr_mul(z.__im, ch, sh, rnd) 

mpfr_div(z.__im, z.__im, a, rnd) 

mpfr_clear(sh) 

mpfr_clear(ch) 

mpfr_clear(c) 

mpfr_clear(s) 

mpfr_clear(b) 

mpfr_clear(a) 

return z 

  

  

def tanh(self): 

""" 

Return the hyperbolic tangent of ``self``. 

  

EXAMPLES:: 

  

sage: (1+CC(I)).tanh() 

1.08392332733869 + 0.271752585319512*I 

""" 

# write self = a + i*b, then 

# tanh(self) = [cosh(a)*sinh(a) + i*cos(b)*sin(b)]/[sinh^2(a)+cos^2(b)] 

cdef ComplexNumber z 

z = self._new() 

cdef mpfr_t ch, sh, c, s, a, b 

mpfr_init2(sh, self._prec) 

mpfr_sinh(sh, self.__re, rnd) 

mpfr_init2(ch, self._prec) 

mpfr_init2(a, self._prec) 

mpfr_sqr(a, sh, rnd) 

mpfr_add_ui(ch, a, 1, rnd) 

mpfr_sqrt(ch, ch, rnd) 

mpfr_init2(c, self._prec) 

mpfr_init2(s, self._prec) 

mpfr_sin_cos(s, c, self.__im, rnd) 

mpfr_init2(b, self._prec) 

mpfr_sqr(b, c, rnd) 

mpfr_add(a, a, b, rnd) 

mpfr_mul(z.__im, c, s, rnd) 

mpfr_div(z.__im, z.__im, a, rnd) 

mpfr_mul(z.__re, ch, sh, rnd) 

mpfr_div(z.__re, z.__re, a, rnd) 

mpfr_clear(sh) 

mpfr_clear(ch) 

mpfr_clear(c) 

mpfr_clear(s) 

mpfr_clear(b) 

mpfr_clear(a) 

return z 

  

# Other special functions 

def agm(self, right, algorithm="optimal"): 

""" 

Return the Arithmetic-Geometric Mean (AGM) of ``self`` and ``right``. 

  

INPUT: 

  

- ``right`` (complex) -- another complex number 

  

- ``algorithm`` (string, default "optimal") -- the algorithm to use 

(see below). 

  

OUTPUT: 

  

(complex) A value of the AGM of ``self`` and ``right``. Note that 

this is a multi-valued function, and the algorithm used 

affects the value returned, as follows: 

  

- "pari": Call the sgm function from the pari library. 

  

- "optimal": Use the AGM sequence such that at each stage 

`(a,b)` is replaced by `(a_1,b_1)=((a+b)/2,\pm\sqrt{ab})` 

where the sign is chosen so that `|a_1-b_1|\le|a_1+b_1|`, or 

equivalently `\Re(b_1/a_1)\ge 0`. The resulting limit is 

maximal among all possible values. 

  

- "principal": Use the AGM sequence such that at each stage 

`(a,b)` is replaced by `(a_1,b_1)=((a+b)/2,\pm\sqrt{ab})` 

where the sign is chosen so that `\Re(b_1)\ge 0` (the 

so-called principal branch of the square root). 

  

The values `AGM(a,0)`, `AGM(0,a)`, and `AGM(a,-a)` are all taken to be 0. 

  

EXAMPLES:: 

  

sage: a = CC(1,1) 

sage: b = CC(2,-1) 

sage: a.agm(b) 

1.62780548487271 + 0.136827548397369*I 

sage: a.agm(b, algorithm="optimal") 

1.62780548487271 + 0.136827548397369*I 

sage: a.agm(b, algorithm="principal") 

1.62780548487271 + 0.136827548397369*I 

sage: a.agm(b, algorithm="pari") 

1.62780548487271 + 0.136827548397369*I 

  

An example to show that the returned value depends on the algorithm 

parameter:: 

  

sage: a = CC(-0.95,-0.65) 

sage: b = CC(0.683,0.747) 

sage: a.agm(b, algorithm="optimal") 

-0.371591652351761 + 0.319894660206830*I 

sage: a.agm(b, algorithm="principal") 

0.338175462986180 - 0.0135326969565405*I 

sage: a.agm(b, algorithm="pari") 

-0.371591652351761 + 0.319894660206830*I 

sage: a.agm(b, algorithm="optimal").abs() 

0.490319232466314 

sage: a.agm(b, algorithm="principal").abs() 

0.338446122230459 

sage: a.agm(b, algorithm="pari").abs() 

0.490319232466314 

  

TESTS: 

  

An example which came up in testing:: 

  

sage: I = CC(I) 

sage: a = 0.501648970493109 + 1.11877240294744*I 

sage: b = 1.05946309435930 + 1.05946309435930*I 

sage: a.agm(b) 

0.774901870587681 + 1.10254945079875*I 

  

sage: a = CC(-0.32599972608379413, 0.60395514542928641) 

sage: b = CC( 0.6062314525690593, 0.1425693337776659) 

sage: a.agm(b) 

0.199246281325876 + 0.478401702759654*I 

sage: a.agm(-a) 

0.000000000000000 

sage: a.agm(0) 

0.000000000000000 

sage: CC(0).agm(a) 

0.000000000000000 

  

Consistency:: 

  

sage: a = 1 + 0.5*I 

sage: b = 2 - 0.25*I 

sage: a.agm(b) - ComplexField(100)(a).agm(b) 

0.000000000000000 

sage: ComplexField(200)(a).agm(b) - ComplexField(500)(a).agm(b) 

0.00000000000000000000000000000000000000000000000000000000000 

sage: ComplexField(500)(a).agm(b) - ComplexField(1000)(a).agm(b) 

0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 

""" 

if algorithm=="pari": 

t = self._parent(right).__pari__() 

return self._parent(self.__pari__().agm(t)) 

  

cdef ComplexNumber a, b, a1, b1, d, e, res 

cdef mp_exp_t rel_prec 

cdef bint optimal = algorithm == "optimal" 

  

if optimal or algorithm == "principal": 

  

if not isinstance(right, ComplexNumber) or (<ComplexNumber>right)._parent is not self._parent: 

right = self._parent(right) 

  

res = self._new() 

  

if mpfr_zero_p(self.__re) and mpfr_zero_p(self.__im): 

return self 

elif mpfr_zero_p((<ComplexNumber>right).__re) and mpfr_zero_p((<ComplexNumber>right).__im): 

return right 

elif (mpfr_cmpabs(self.__re, (<ComplexNumber>right).__re) == 0 and 

mpfr_cmpabs(self.__im, (<ComplexNumber>right).__im) == 0 and 

mpfr_cmp(self.__re, (<ComplexNumber>right).__re) != 0 and 

mpfr_cmp(self.__im, (<ComplexNumber>right).__im) != 0): 

# self = -right 

mpfr_set_ui(res.__re, 0, rnd) 

mpfr_set_ui(res.__im, 0, rnd) 

return res 

  

# Do the computations to a bit higher precision so rounding error 

# won't obscure the termination condition. 

a = ComplexNumber(self._parent.to_prec(self._prec+5), None) 

b = a._new() 

a1 = a._new() 

b1 = a._new() 

  

d = a._new() 

if optimal: 

e = a._new() 

  

# Make copies so we don't mutate self or right. 

mpfr_set(a.__re, self.__re, rnd) 

mpfr_set(a.__im, self.__im, rnd) 

mpfr_set(b.__re, (<ComplexNumber>right).__re, rnd) 

mpfr_set(b.__im, (<ComplexNumber>right).__im, rnd) 

  

if optimal: 

mpfr_add(e.__re, a.__re, b.__re, rnd) 

mpfr_add(e.__im, a.__im, b.__im, rnd) 

  

while True: 

  

# a1 = (a+b)/2 

if optimal: 

mpfr_swap(a1.__re, e.__re) 

mpfr_swap(a1.__im, e.__im) 

else: 

mpfr_add(a1.__re, a.__re, b.__re, rnd) 

mpfr_add(a1.__im, a.__im, b.__im, rnd) 

mpfr_mul_2si(a1.__re, a1.__re, -1, rnd) 

mpfr_mul_2si(a1.__im, a1.__im, -1, rnd) 

  

# b1 = sqrt(a*b) 

mpfr_mul(d.__re, a.__re, b.__re, rnd) 

mpfr_mul(d.__im, a.__im, b.__im, rnd) 

mpfr_sub(b1.__re, d.__re, d.__im, rnd) 

mpfr_mul(d.__re, a.__re, b.__im, rnd) 

mpfr_mul(d.__im, a.__im, b.__re, rnd) 

mpfr_add(b1.__im, d.__re, d.__im, rnd) 

b1 = b1.sqrt() # this would be a *lot* of code duplication 

  

# d = a1 - b1 

mpfr_sub(d.__re, a1.__re, b1.__re, rnd) 

mpfr_sub(d.__im, a1.__im, b1.__im, rnd) 

if mpfr_zero_p(d.__re) and mpfr_zero_p(d.__im): 

mpfr_set(res.__re, a1.__re, rnd) 

mpfr_set(res.__im, a1.__im, rnd) 

return res 

  

if optimal: 

# e = a1+b1 

mpfr_add(e.__re, a1.__re, b1.__re, rnd) 

mpfr_add(e.__im, a1.__im, b1.__im, rnd) 

if mpfr_zero_p(e.__re) and mpfr_zero_p(e.__im): 

mpfr_set(res.__re, a1.__re, rnd) 

mpfr_set(res.__im, a1.__im, rnd) 

return res 

  

# |e| < |d| 

if cmp_abs(e, d) < 0: 

mpfr_swap(d.__re, e.__re) 

mpfr_swap(d.__im, e.__im) 

mpfr_neg(b1.__re, b1.__re, rnd) 

mpfr_neg(b1.__im, b1.__im, rnd) 

  

rel_prec = min_exp_t(max_exp(a1), max_exp(b1)) - max_exp(d) 

if rel_prec > self._prec: 

mpfr_set(res.__re, a1.__re, rnd) 

mpfr_set(res.__im, a1.__im, rnd) 

return res 

  

# a, b = a1, b1 

mpfr_swap(a.__re, a1.__re) 

mpfr_swap(a.__im, a1.__im) 

mpfr_swap(b.__re, b1.__re) 

mpfr_swap(b.__im, b1.__im) 

  

raise ValueError("agm algorithm must be one of 'pari', 'optimal', 'principal'") 

  

def argument(self): 

r""" 

The argument (angle) of the complex number, normalized so that 

`-\pi < \theta \leq \pi`. 

  

EXAMPLES:: 

  

sage: i = CC.0 

sage: (i^2).argument() 

3.14159265358979 

sage: (1+i).argument() 

0.785398163397448 

sage: i.argument() 

1.57079632679490 

sage: (-i).argument() 

-1.57079632679490 

sage: (RR('-0.001') - i).argument() 

-1.57179632646156 

""" 

cdef RealNumber x 

x = RealNumber(self._parent._real_field(), None) 

mpfr_atan2(x.value, self.__im, self.__re, rnd) 

return x 

  

  

def arg(self): 

""" 

See :meth:`argument()`. 

  

EXAMPLES:: 

  

sage: i = CC.0 

sage: (i^2).arg() 

3.14159265358979 

""" 

return self.argument() 

  

def conjugate(self): 

""" 

Return the complex conjugate of this complex number. 

  

EXAMPLES:: 

  

sage: i = CC.0 

sage: (1+i).conjugate() 

1.00000000000000 - 1.00000000000000*I 

""" 

cdef ComplexNumber x 

x = self._new() 

  

cdef mpfr_t i 

mpfr_init2(i, self._prec) 

mpfr_neg(i, self.__im, rnd) 

mpfr_set(x.__re, self.__re, rnd) 

mpfr_set(x.__im, i, rnd) 

mpfr_clear(i) 

return x 

  

def dilog(self): 

r""" 

Returns the complex dilogarithm of ``self``. 

  

The complex dilogarithm, or Spence's function, is defined by 

  

.. MATH:: 

  

Li_2(z) = - \int_0^z \frac{\log|1-\zeta|}{\zeta} d(\zeta) 

= \sum_{k=1}^\infty \frac{z^k}{k} 

  

Note that the series definition can only be used for `|z| < 1`. 

  

EXAMPLES:: 

  

sage: a = ComplexNumber(1,0) 

sage: a.dilog() 

1.64493406684823 

sage: float(pi^2/6) 

1.6449340668482262 

  

:: 

  

sage: b = ComplexNumber(0,1) 

sage: b.dilog() 

-0.205616758356028 + 0.915965594177219*I 

  

:: 

  

sage: c = ComplexNumber(0,0) 

sage: c.dilog() 

0.000000000000000 

""" 

return self._parent(self.__pari__().dilog()) 

  

def exp(ComplexNumber self): 

r""" 

Compute `e^z` or `\exp(z)`. 

  

EXAMPLES:: 

  

sage: i = ComplexField(300).0 

sage: z = 1 + i 

sage: z.exp() 

1.46869393991588515713896759732660426132695673662900872279767567631093696585951213872272450 + 2.28735528717884239120817190670050180895558625666835568093865811410364716018934540926734485*I 

""" 

# write self = a + i*b, then 

# exp(self) = exp(a)*(cos(b) + i*sin(b)) 

cdef ComplexNumber z 

z = self._new() 

cdef mpfr_t r 

mpfr_init2(r, self._prec) 

mpfr_exp(r, self.__re, rnd) 

mpfr_sin_cos(z.__im, z.__re, self.__im, rnd) 

mpfr_mul(z.__re, z.__re, r, rnd) 

mpfr_mul(z.__im, z.__im, r, rnd) 

mpfr_clear(r) 

return z 

  

def gamma(self): 

""" 

Return the Gamma function evaluated at this complex number. 

  

EXAMPLES:: 

  

sage: i = ComplexField(30).0 

sage: (1+i).gamma() 

0.49801567 - 0.15494983*I 

  

TESTS:: 

  

sage: CC(0).gamma() 

Infinity 

  

:: 

  

sage: CC(-1).gamma() 

Infinity 

""" 

try: 

return self._parent(self.__pari__().gamma()) 

except sage.libs.pari.all.PariError: 

from sage.rings.infinity import UnsignedInfinityRing 

return UnsignedInfinityRing.gen() 

  

def gamma_inc(self, t): 

""" 

Return the incomplete Gamma function evaluated at this complex 

number. 

  

EXAMPLES:: 

  

sage: C, i = ComplexField(30).objgen() 

sage: (1+i).gamma_inc(2 + 3*i) # abs tol 2e-10 

0.0020969149 - 0.059981914*I 

sage: (1+i).gamma_inc(5) 

-0.0013781309 + 0.0065198200*I 

sage: C(2).gamma_inc(1 + i) 

0.70709210 - 0.42035364*I 

sage: CC(2).gamma_inc(5) 

0.0404276819945128 

  

TESTS: 

  

Check that :trac:`7099` is fixed:: 

  

sage: C = ComplexField(400) 

sage: C(2 + I).gamma_inc(C(3 + I)) # abs tol 1e-120 

0.121515644664508695525971545977439666159749344176962379708992904126499444842886620664991650378432544392118359044438541515 + 0.101533909079826033296475736021224621546966200987295663190553587086145836461236284668967411665020429964946098113930918850*I 

  

""" 

return self._parent(self.__pari__().incgam(t, precision=self.prec())) 

  

def log(self,base=None): 

r""" 

Complex logarithm of `z` with branch chosen as follows: Write 

`z = \rho e^{i \theta}` with `-\pi < \theta <= pi`. Then 

`\mathrm{log}(z) = \mathrm{log}(\rho) + i \theta`. 

  

.. WARNING:: 

  

Currently the real log is computed using floats, so there 

is potential precision loss. 

  

EXAMPLES:: 

  

sage: a = ComplexNumber(2,1) 

sage: a.log() 

0.804718956217050 + 0.463647609000806*I 

sage: log(a.abs()) 

0.804718956217050 

sage: a.argument() 

0.463647609000806 

  

:: 

  

sage: b = ComplexNumber(float(exp(42)),0) 

sage: b.log() 

41.99999999999971 

  

:: 

  

sage: c = ComplexNumber(-1,0) 

sage: c.log() 

3.14159265358979*I 

  

The option of a base is included for compatibility with other logs:: 

  

sage: c = ComplexNumber(-1,0) 

sage: c.log(2) 

4.53236014182719*I 

  

If either component (real or imaginary) of the complex number 

is NaN (not a number), log will return the complex NaN:: 

  

sage: c = ComplexNumber(NaN,2) 

sage: c.log() 

NaN + NaN*I 

""" 

if mpfr_nan_p(self.__re): 

return ComplexNumber(self._parent,self.real(),self.real()) 

if mpfr_nan_p(self.__im): 

return ComplexNumber(self._parent,self.imag(),self.imag()) 

theta = self.argument() 

rho = abs(self) 

if base is None: 

return ComplexNumber(self._parent, rho.log(), theta) 

else: 

from .real_mpfr import RealField 

return ComplexNumber(self._parent, rho.log()/RealNumber(RealField(self.prec()),base).log(), theta/RealNumber(RealField(self.prec()),base).log()) 

  

def additive_order(self): 

""" 

Return the additive order of ``self``. 

  

EXAMPLES:: 

  

sage: CC(0).additive_order() 

1 

sage: CC.gen().additive_order() 

+Infinity 

""" 

if self == 0: 

return 1 

else: 

return infinity.infinity 

  

def sqrt(self, all=False): 

""" 

The square root function, taking the branch cut to be the negative 

real axis. 

  

INPUT: 

  

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

list of all square roots. 

  

EXAMPLES:: 

  

sage: C.<i> = ComplexField(30) 

sage: i.sqrt() 

0.70710678 + 0.70710678*I 

sage: (1+i).sqrt() 

1.0986841 + 0.45508986*I 

sage: (C(-1)).sqrt() 

1.0000000*I 

sage: (1 + 1e-100*i).sqrt()^2 

1.0000000 + 1.0000000e-100*I 

sage: i = ComplexField(200).0 

sage: i.sqrt() 

0.70710678118654752440084436210484903928483593768847403658834 + 0.70710678118654752440084436210484903928483593768847403658834*I 

""" 

cdef ComplexNumber z = self._new() 

if mpfr_zero_p(self.__im): 

if mpfr_sgn(self.__re) >= 0: 

mpfr_set_ui(z.__im, 0, rnd) 

mpfr_sqrt(z.__re, self.__re, rnd) 

else: 

mpfr_set_ui(z.__re, 0, rnd) 

mpfr_neg(z.__im, self.__re, rnd) 

mpfr_sqrt(z.__im, z.__im, rnd) 

if all: 

return [z, -z] if z else [z] 

else: 

return z 

# self = x + yi = (a+bi)^2 

# expand, substitute, solve 

# a^2 = (x + sqrt(x^2+y^2))/2 

cdef bint avoid_branch = mpfr_sgn(self.__re) < 0 and mpfr_cmpabs(self.__im, self.__re) < 0 

cdef mpfr_t a2 

mpfr_init2(a2, self._prec) 

mpfr_hypot(a2, self.__re, self.__im, rnd) 

if avoid_branch: 

# x + sqrt(x^2+y^2) numerically unstable for x near negative real axis 

# so we compute sqrt of (-z) and shift by i at the end 

mpfr_sub(a2, a2, self.__re, rnd) 

else: 

mpfr_add(a2, a2, self.__re, rnd) 

mpfr_mul_2si(a2, a2, -1, rnd) 

# a = sqrt(a2) 

mpfr_sqrt(z.__re, a2, rnd) 

# b = y/(2a) 

mpfr_div(z.__im, self.__im, z.__re, rnd) 

mpfr_mul_2si(z.__im, z.__im, -1, rnd) 

mpfr_clear(a2) 

if avoid_branch: 

mpfr_swap(z.__re, z.__im) 

# Note that y (hence b) was never negated, so we have z=i*sqrt(self). 

# if we were below the branch cut, we want the other branch 

if mpfr_sgn(self.__im) < 0: 

mpfr_neg(z.__re, z.__re, rnd) 

mpfr_neg(z.__im, z.__im, rnd) 

if all: 

return [z, -z] 

else: 

return z 

  

def nth_root(self, n, all=False): 

""" 

The `n`-th root function. 

  

INPUT: 

  

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

list of all `n`-th roots. 

  

EXAMPLES:: 

  

sage: a = CC(27) 

sage: a.nth_root(3) 

3.00000000000000 

sage: a.nth_root(3, all=True) 

[3.00000000000000, -1.50000000000000 + 2.59807621135332*I, -1.50000000000000 - 2.59807621135332*I] 

sage: a = ComplexField(20)(2,1) 

sage: [r^7 for r in a.nth_root(7, all=True)] 

[2.0000 + 1.0000*I, 2.0000 + 1.0000*I, 2.0000 + 1.0000*I, 2.0000 + 1.0000*I, 2.0000 + 1.0000*I, 2.0000 + 1.0001*I, 2.0000 + 1.0001*I] 

""" 

if self.is_zero(): 

return [self] if all else self 

  

cdef ComplexNumber z 

z = self._new() 

  

cdef RealNumber arg, rho 

cdef mpfr_t r 

rho = abs(self) 

arg = self.argument() / n 

mpfr_init2(r, self._prec) 

mpfr_rootn_ui(r, rho.value, n, rnd) 

  

mpfr_sin_cos(z.__im, z.__re, arg.value, rnd) 

mpfr_mul(z.__re, z.__re, r, rnd) 

mpfr_mul(z.__im, z.__im, r, rnd) 

  

if not all: 

mpfr_clear(r) 

return z 

  

R = self._parent._real_field() 

cdef RealNumber theta 

theta = R.pi()*2/n 

zlist = [z] 

for k in range(1, n): 

z = self._new() 

arg += theta 

mpfr_sin_cos(z.__im, z.__re, arg.value, rnd) 

mpfr_mul(z.__re, z.__re, r, rnd) 

mpfr_mul(z.__im, z.__im, r, rnd) 

zlist.append(z) 

  

mpfr_clear(r) 

return zlist 

  

  

def is_square(self): 

r""" 

This function always returns true as `\CC` is algebraically closed. 

  

EXAMPLES:: 

  

sage: a = ComplexNumber(2,1) 

sage: a.is_square() 

True 

  

`\CC` is algebraically closed, hence every element 

is a square:: 

  

sage: b = ComplexNumber(5) 

sage: b.is_square() 

True 

""" 

return True 

  

def is_real(self): 

""" 

Return ``True`` if ``self`` is real, i.e. has imaginary part zero. 

  

EXAMPLES:: 

  

sage: CC(1.23).is_real() 

True 

sage: CC(1+i).is_real() 

False 

""" 

return (mpfr_zero_p(self.__im) != 0) 

  

def is_imaginary(self): 

""" 

Return ``True`` if ``self`` is imaginary, i.e. has real part zero. 

  

EXAMPLES:: 

  

sage: CC(1.23*i).is_imaginary() 

True 

sage: CC(1+i).is_imaginary() 

False 

""" 

return (mpfr_zero_p(self.__re) != 0) 

  

def is_integer(self): 

""" 

Return ``True`` if ``self`` is a integer 

  

EXAMPLES:: 

  

sage: CC(3).is_integer() 

True 

sage: CC(1,2).is_integer() 

False 

""" 

return self.is_real() and self.real() in ZZ 

  

def is_positive_infinity(self): 

r""" 

Check if ``self`` is `+\infty`. 

  

EXAMPLES:: 

  

sage: CC(1, 2).is_positive_infinity() 

False 

sage: CC(oo, 0).is_positive_infinity() 

True 

sage: CC(0, oo).is_positive_infinity() 

False 

""" 

return self.real().is_positive_infinity() and self.imag().is_zero() 

  

def is_negative_infinity(self): 

r""" 

Check if ``self`` is `-\infty`. 

  

EXAMPLES:: 

  

sage: CC(1, 2).is_negative_infinity() 

False 

sage: CC(-oo, 0).is_negative_infinity() 

True 

sage: CC(0, -oo).is_negative_infinity() 

False 

""" 

return self.real().is_negative_infinity() and self.imag().is_zero() 

  

def is_infinity(self): 

r""" 

Check if ``self`` is `\infty`. 

  

EXAMPLES:: 

  

sage: CC(1, 2).is_infinity() 

False 

sage: CC(0, oo).is_infinity() 

True 

""" 

return self.real().is_infinity() or self.imag().is_infinity() 

  

def is_NaN(self): 

r""" 

Check if ``self`` is not-a-number. 

  

EXAMPLES:: 

  

sage: CC(1, 2).is_NaN() 

False 

sage: CC(NaN).is_NaN() 

True 

sage: CC(NaN,2).log().is_NaN() 

True 

""" 

return mpfr_nan_p(self.__re) or mpfr_nan_p(self.__im) 

  

def zeta(self): 

""" 

Return the Riemann zeta function evaluated at this complex number. 

  

EXAMPLES:: 

  

sage: i = ComplexField(30).gen() 

sage: z = 1 + i 

sage: z.zeta() 

0.58215806 - 0.92684856*I 

sage: zeta(z) 

0.58215806 - 0.92684856*I 

  

sage: CC(1).zeta() 

Infinity 

""" 

if mpfr_zero_p(self.__im) and mpfr_cmp_ui(self.__re, 1) == 0: 

return infinity.unsigned_infinity 

return self._parent(self.__pari__().zeta()) 

  

def algebraic_dependency(self, n, **kwds): 

""" 

Return an irreducible polynomial of degree at most `n` which is 

approximately satisfied by this complex number. 

  

ALGORITHM: Uses the PARI C-library algdep command. 

  

INPUT: Type algdep? at the top level prompt. All additional 

parameters are passed onto the top-level algdep command. 

  

EXAMPLES:: 

  

sage: C = ComplexField() 

sage: z = (1/2)*(1 + sqrt(3.0) *C.0); z 

0.500000000000000 + 0.866025403784439*I 

sage: p = z.algdep(5); p 

x^2 - x + 1 

sage: p(z) 

1.11022302462516e-16 

  

TESTS:: 

  

sage: z.algdep(2) 

x^2 - x + 1 

sage: z.algebraic_dependancy(2) 

doctest:...: DeprecationWarning: algebraic_dependancy is deprecated. Please use algebraic_dependency instead. 

See http://trac.sagemath.org/22714 for details. 

x^2 - x + 1 

""" 

from sage.arith.all import algdep 

return algdep(self, n, **kwds) 

  

# Alias 

algdep = algebraic_dependency 

  

# Former misspelling 

algebraic_dependancy = deprecated_function_alias(22714, algebraic_dependency) 

  

  

def make_ComplexNumber0( fld, mult_order, re, im ): 

""" 

Create a complex number for pickling. 

  

EXAMPLES:: 

  

sage: a = CC(1 + I) 

sage: loads(dumps(a)) == a # indirect doctest 

True 

""" 

x = ComplexNumber( fld, re, im ) 

x._set_multiplicative_order( mult_order ) 

return x 

  

  

  

def create_ComplexNumber(s_real, s_imag=None, int pad=0, min_prec=53): 

r""" 

Return the complex number defined by the strings ``s_real`` and 

``s_imag`` as an element of ``ComplexField(prec=n)``, 

where `n` potentially has slightly more (controlled by pad) bits than 

given by `s`. 

  

INPUT: 

  

- ``s_real`` -- a string that defines a real number 

(or something whose string representation defines a number) 

  

- ``s_imag`` -- a string that defines a real number 

(or something whose string representation defines a number) 

  

- ``pad`` -- an integer at least 0. 

  

- ``min_prec`` -- number will have at least this many bits of precision, 

no matter what. 

  

EXAMPLES:: 

  

sage: ComplexNumber('2.3') 

2.30000000000000 

sage: ComplexNumber('2.3','1.1') 

2.30000000000000 + 1.10000000000000*I 

sage: ComplexNumber(10) 

10.0000000000000 

sage: ComplexNumber(10,10) 

10.0000000000000 + 10.0000000000000*I 

sage: ComplexNumber(1.000000000000000000000000000,2) 

1.00000000000000000000000000 + 2.00000000000000000000000000*I 

sage: ComplexNumber(1,2.000000000000000000000) 

1.00000000000000000000 + 2.00000000000000000000*I 

  

:: 

  

sage: sage.rings.complex_number.create_ComplexNumber(s_real=2,s_imag=1) 

2.00000000000000 + 1.00000000000000*I 

  

TESTS: 

  

Make sure we've rounded up ``log(10,2)`` enough to guarantee 

sufficient precision (:trac:`10164`):: 

  

sage: s = "1." + "0"*10**6 + "1" 

sage: sage.rings.complex_number.create_ComplexNumber(s,0).real()-1 == 0 

False 

sage: sage.rings.complex_number.create_ComplexNumber(0,s).imag()-1 == 0 

False 

  

""" 

if s_imag is None: 

s_imag = 0 

  

if not isinstance(s_real, str): 

s_real = str(s_real).strip() 

if not isinstance(s_imag, str): 

s_imag = str(s_imag).strip() 

#if base == 10: 

bits = max(int(LOG_TEN_TWO_PLUS_EPSILON*len(s_real)), 

int(LOG_TEN_TWO_PLUS_EPSILON*len(s_imag))) 

#else: 

# bits = max(int(math.log(base,2)*len(s_imag)),int(math.log(base,2)*len(s_imag))) 

from .complex_field import ComplexField 

C = ComplexField(prec=max(bits+pad, min_prec)) 

  

return ComplexNumber(C, s_real, s_imag) 

  

  

cdef class RRtoCC(Map): 

  

cdef ComplexNumber _zero 

  

def __init__(self, RR, CC): 

""" 

EXAMPLES:: 

  

sage: from sage.rings.complex_number import RRtoCC 

sage: RRtoCC(RR, CC) 

Natural map: 

From: Real Field with 53 bits of precision 

To: Complex Field with 53 bits of precision 

""" 

Map.__init__(self, RR, CC) 

self._zero = ComplexNumber(CC, 0) 

self._repr_type_str = "Natural" 

  

cdef dict _extra_slots(self): 

""" 

A helper for pickling and copying. 

  

INPUT: 

  

``_slots`` -- a dictionary 

  

OUTPUT: 

  

The given dictionary, with zero added. 

  

EXAMPLES:: 

  

sage: from sage.rings.complex_number import RRtoCC 

sage: f = RRtoCC(RR, CC) 

sage: g = copy(f) # indirect doctest 

sage: g 

Natural map: 

From: Real Field with 53 bits of precision 

To: Complex Field with 53 bits of precision 

""" 

slots = Map._extra_slots(self) 

slots['_zero'] = self._zero 

return slots 

  

cdef _update_slots(self, dict _slots): 

""" 

A helper for unpickling and copying. 

  

INPUT: 

  

``_slots`` -- a dictionary providing values for the c(p)def slots of self. 

  

EXAMPLES:: 

  

sage: from sage.rings.complex_number import RRtoCC 

sage: RRtoCC(RR, CC) 

Natural map: 

From: Real Field with 53 bits of precision 

To: Complex Field with 53 bits of precision 

""" 

Map._update_slots(self, _slots) 

self._zero = _slots['_zero'] 

  

cpdef Element _call_(self, x): 

""" 

EXAMPLES:: 

  

sage: from sage.rings.complex_number import RRtoCC 

sage: f = RRtoCC(RealField(100), ComplexField(10)) # indirect doctest 

sage: f(1/3) 

0.33 

""" 

cdef ComplexNumber z = self._zero._new() 

mpfr_set(z.__re, (<RealNumber>x).value, rnd) 

mpfr_set_ui(z.__im, 0, rnd) 

return z 

  

  

cdef class CCtoCDF(Map): 

  

cpdef Element _call_(self, x): 

""" 

EXAMPLES:: 

  

sage: from sage.rings.complex_number import CCtoCDF 

sage: f = CCtoCDF(CC, CDF) # indirect doctest 

sage: f(CC.0) 

1.0*I 

sage: f(exp(pi*CC.0/4)) 

0.7071067811865476 + 0.7071067811865475*I 

""" 

cdef ComplexDoubleElement z = <ComplexDoubleElement>ComplexDoubleElement.__new__(ComplexDoubleElement) 

z._complex.dat[0] = mpfr_get_d((<ComplexNumber>x).__re, MPFR_RNDN) 

z._complex.dat[1] = mpfr_get_d((<ComplexNumber>x).__im, MPFR_RNDN) 

return z 

  

  

cdef inline mp_exp_t min_exp_t(mp_exp_t a, mp_exp_t b): 

return a if a < b else b 

  

cdef inline mp_exp_t max_exp_t(mp_exp_t a, mp_exp_t b): 

return a if a > b else b 

  

cdef inline mp_exp_t max_exp(ComplexNumber z): 

""" 

Quickly return the maximum exponent of the real and complex parts of z, 

which is useful for estimating its magnitude. 

""" 

if mpfr_zero_p(z.__im): 

return mpfr_get_exp(z.__re) 

elif mpfr_zero_p(z.__re): 

return mpfr_get_exp(z.__im) 

return max_exp_t(mpfr_get_exp(z.__re), mpfr_get_exp(z.__im)) 

  

cpdef int cmp_abs(ComplexNumber a, ComplexNumber b): 

""" 

Returns -1, 0, or 1 according to whether `|a|` is less than, equal to, or 

greater than `|b|`. 

  

Optimized for non-close numbers, where the ordering can be determined by 

examining exponents. 

  

EXAMPLES:: 

  

sage: from sage.rings.complex_number import cmp_abs 

sage: cmp_abs(CC(5), CC(1)) 

1 

sage: cmp_abs(CC(5), CC(4)) 

1 

sage: cmp_abs(CC(5), CC(5)) 

0 

sage: cmp_abs(CC(5), CC(6)) 

-1 

sage: cmp_abs(CC(5), CC(100)) 

-1 

sage: cmp_abs(CC(-100), CC(1)) 

1 

sage: cmp_abs(CC(-100), CC(100)) 

0 

sage: cmp_abs(CC(-100), CC(1000)) 

-1 

sage: cmp_abs(CC(1,1), CC(1)) 

1 

sage: cmp_abs(CC(1,1), CC(2)) 

-1 

sage: cmp_abs(CC(1,1), CC(1,0.99999)) 

1 

sage: cmp_abs(CC(1,1), CC(1,-1)) 

0 

sage: cmp_abs(CC(0), CC(1)) 

-1 

sage: cmp_abs(CC(1), CC(0)) 

1 

sage: cmp_abs(CC(0), CC(0)) 

0 

sage: cmp_abs(CC(2,1), CC(1,2)) 

0 

""" 

if mpfr_zero_p(b.__re) and mpfr_zero_p(b.__im): 

return not ((mpfr_zero_p(a.__re) and mpfr_zero_p(a.__im))) 

elif (mpfr_zero_p(a.__re) and mpfr_zero_p(a.__im)): 

return -1 

cdef mp_exp_t exp_diff = max_exp(a) - max_exp(b) 

if exp_diff <= -2: 

return -1 

elif exp_diff >= 2: 

return 1 

  

cdef int res 

cdef mpfr_t abs_a, abs_b, tmp 

mpfr_init2(abs_a, mpfr_get_prec(a.__re)) 

mpfr_init2(abs_b, mpfr_get_prec(b.__re)) 

mpfr_init2(tmp, mpfr_get_prec(a.__re)) 

  

mpfr_sqr(abs_a, a.__re, rnd) 

mpfr_sqr(tmp, a.__im, rnd) 

mpfr_add(abs_a, abs_a, tmp, rnd) 

  

mpfr_sqr(abs_b, b.__re, rnd) 

mpfr_sqr(tmp, b.__im, rnd) 

mpfr_add(abs_b, abs_b, tmp, rnd) 

  

res = mpfr_cmpabs(abs_a, abs_b) 

  

mpfr_clear(abs_a) 

mpfr_clear(abs_b) 

mpfr_clear(tmp) 

  

return res