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

2755

2756

2757

2758

2759

2760

2761

2762

2763

2764

2765

2766

2767

2768

2769

2770

2771

2772

2773

2774

2775

2776

2777

2778

2779

2780

2781

2782

2783

2784

2785

2786

2787

2788

2789

2790

2791

2792

2793

2794

2795

2796

2797

2798

2799

2800

2801

2802

2803

2804

2805

2806

2807

2808

2809

2810

2811

2812

2813

2814

2815

2816

2817

2818

2819

2820

2821

2822

2823

2824

2825

2826

2827

2828

2829

2830

2831

2832

2833

2834

2835

2836

2837

2838

2839

2840

2841

2842

2843

2844

2845

2846

2847

2848

2849

2850

2851

2852

2853

2854

2855

2856

2857

2858

2859

2860

2861

2862

2863

2864

2865

2866

2867

2868

2869

2870

2871

2872

2873

2874

2875

2876

2877

2878

2879

2880

2881

2882

2883

2884

2885

2886

2887

2888

2889

2890

2891

2892

2893

2894

2895

2896

2897

2898

2899

2900

2901

2902

2903

2904

2905

2906

2907

2908

2909

2910

2911

2912

2913

2914

2915

2916

2917

2918

2919

2920

2921

2922

2923

2924

2925

2926

2927

2928

2929

2930

2931

2932

2933

2934

2935

2936

2937

2938

2939

2940

2941

2942

2943

2944

2945

2946

2947

2948

2949

2950

2951

2952

2953

2954

2955

2956

2957

2958

2959

2960

2961

2962

2963

2964

2965

2966

2967

2968

2969

2970

2971

2972

2973

2974

2975

2976

2977

2978

2979

2980

2981

2982

2983

2984

2985

2986

2987

2988

2989

2990

2991

2992

2993

2994

2995

2996

2997

2998

2999

3000

3001

3002

3003

3004

3005

3006

3007

3008

3009

3010

3011

3012

3013

3014

3015

3016

3017

3018

3019

3020

3021

3022

3023

3024

3025

3026

3027

3028

3029

3030

3031

3032

3033

3034

3035

3036

3037

3038

3039

3040

3041

3042

3043

3044

3045

3046

3047

3048

3049

3050

3051

3052

3053

3054

3055

3056

3057

3058

3059

3060

3061

3062

3063

3064

3065

3066

3067

3068

3069

3070

3071

3072

3073

3074

3075

3076

3077

3078

3079

3080

3081

3082

3083

3084

3085

3086

3087

3088

3089

3090

3091

3092

3093

3094

3095

3096

3097

3098

3099

3100

3101

3102

3103

3104

3105

3106

3107

3108

3109

3110

3111

3112

3113

3114

3115

3116

3117

3118

3119

3120

3121

3122

3123

3124

3125

3126

3127

3128

3129

3130

3131

3132

3133

3134

3135

3136

3137

3138

3139

3140

3141

3142

3143

3144

3145

3146

3147

3148

3149

3150

3151

3152

3153

3154

3155

3156

3157

3158

3159

3160

3161

3162

3163

3164

3165

3166

3167

3168

3169

3170

3171

3172

3173

3174

3175

3176

3177

3178

3179

3180

3181

3182

3183

3184

3185

3186

3187

3188

3189

3190

3191

3192

3193

3194

3195

3196

3197

3198

3199

3200

3201

3202

3203

3204

3205

3206

3207

3208

3209

3210

3211

3212

3213

3214

3215

3216

3217

3218

3219

3220

3221

3222

3223

3224

3225

3226

3227

3228

3229

3230

3231

3232

3233

3234

3235

3236

3237

3238

3239

3240

3241

3242

3243

3244

3245

3246

3247

3248

3249

3250

3251

3252

3253

3254

3255

3256

3257

3258

3259

3260

3261

3262

3263

3264

3265

3266

3267

3268

3269

3270

3271

3272

3273

3274

3275

3276

3277

3278

3279

3280

3281

3282

3283

3284

3285

3286

3287

3288

3289

3290

3291

3292

3293

3294

3295

3296

3297

3298

3299

3300

3301

3302

3303

3304

3305

3306

3307

3308

3309

3310

3311

3312

3313

3314

3315

3316

3317

3318

3319

3320

3321

3322

3323

3324

3325

3326

3327

3328

3329

3330

3331

3332

3333

3334

3335

3336

3337

3338

3339

3340

3341

3342

3343

3344

3345

3346

3347

3348

3349

3350

3351

3352

3353

3354

3355

3356

3357

3358

3359

3360

3361

3362

3363

3364

3365

3366

3367

3368

3369

3370

3371

3372

3373

3374

3375

3376

3377

3378

3379

3380

3381

3382

3383

3384

3385

3386

3387

3388

3389

3390

3391

3392

3393

3394

3395

3396

3397

3398

3399

3400

3401

3402

3403

3404

3405

3406

3407

3408

3409

3410

3411

3412

3413

3414

3415

3416

3417

3418

3419

3420

3421

3422

3423

3424

3425

3426

3427

3428

3429

3430

3431

3432

3433

3434

3435

3436

3437

3438

3439

3440

3441

3442

3443

3444

3445

3446

3447

3448

3449

3450

3451

3452

3453

3454

3455

3456

3457

3458

3459

3460

3461

3462

3463

3464

3465

3466

3467

3468

3469

3470

3471

3472

3473

3474

3475

3476

3477

3478

3479

3480

3481

3482

3483

3484

3485

3486

3487

3488

3489

3490

3491

3492

3493

3494

3495

3496

3497

3498

3499

3500

3501

3502

3503

3504

3505

3506

3507

3508

3509

3510

3511

3512

3513

3514

3515

3516

3517

3518

3519

3520

3521

3522

3523

3524

3525

3526

3527

3528

3529

3530

3531

3532

3533

3534

3535

3536

3537

3538

3539

3540

3541

3542

3543

3544

3545

3546

3547

3548

3549

3550

3551

3552

3553

3554

3555

3556

3557

3558

3559

3560

3561

3562

3563

3564

3565

3566

3567

3568

3569

3570

3571

3572

3573

3574

3575

3576

3577

3578

3579

3580

3581

3582

3583

3584

3585

3586

3587

3588

3589

3590

3591

3592

3593

3594

3595

3596

3597

3598

3599

3600

3601

3602

3603

3604

3605

3606

3607

3608

3609

3610

3611

3612

3613

3614

3615

3616

3617

3618

3619

3620

3621

3622

3623

3624

3625

3626

3627

3628

3629

3630

3631

3632

3633

3634

3635

3636

3637

3638

3639

3640

3641

3642

3643

3644

3645

3646

3647

3648

3649

3650

3651

3652

3653

3654

3655

3656

3657

3658

3659

3660

3661

3662

3663

3664

3665

3666

3667

3668

3669

3670

3671

3672

3673

3674

3675

3676

3677

3678

3679

3680

3681

3682

3683

3684

3685

3686

3687

3688

3689

3690

3691

3692

3693

3694

3695

3696

3697

3698

3699

3700

3701

3702

3703

3704

3705

3706

3707

3708

3709

3710

3711

3712

3713

3714

3715

3716

3717

3718

3719

3720

3721

3722

3723

3724

3725

3726

3727

3728

3729

3730

3731

3732

3733

3734

3735

3736

3737

3738

3739

3740

3741

3742

3743

3744

3745

3746

3747

3748

3749

3750

3751

3752

3753

3754

3755

3756

3757

3758

3759

3760

3761

3762

3763

3764

3765

3766

3767

3768

3769

3770

3771

3772

3773

3774

3775

3776

3777

3778

3779

3780

3781

3782

3783

3784

3785

3786

3787

3788

3789

3790

3791

3792

3793

3794

3795

3796

3797

3798

3799

3800

3801

3802

3803

3804

3805

3806

3807

3808

3809

3810

3811

3812

3813

3814

3815

3816

3817

3818

3819

3820

3821

3822

3823

3824

3825

3826

3827

3828

3829

3830

3831

3832

3833

3834

3835

3836

3837

3838

3839

3840

3841

3842

3843

3844

3845

3846

3847

3848

3849

3850

3851

3852

3853

3854

3855

3856

3857

3858

3859

3860

3861

3862

3863

3864

3865

3866

3867

3868

3869

3870

3871

3872

3873

3874

3875

3876

3877

3878

3879

3880

3881

3882

3883

3884

3885

3886

3887

3888

3889

3890

3891

3892

3893

3894

3895

3896

3897

3898

3899

3900

3901

3902

3903

3904

3905

3906

3907

3908

3909

3910

3911

3912

3913

3914

3915

3916

3917

3918

3919

3920

3921

3922

3923

3924

3925

3926

3927

3928

3929

3930

3931

3932

3933

3934

3935

3936

3937

3938

3939

3940

3941

3942

3943

3944

3945

3946

3947

3948

3949

3950

3951

3952

3953

3954

3955

3956

3957

3958

3959

3960

3961

3962

3963

3964

3965

3966

3967

3968

3969

3970

3971

3972

3973

3974

3975

3976

3977

3978

3979

3980

3981

3982

3983

3984

3985

3986

3987

3988

3989

3990

3991

3992

3993

3994

3995

3996

3997

3998

3999

4000

4001

4002

4003

4004

4005

4006

4007

4008

4009

4010

4011

4012

4013

4014

4015

4016

4017

4018

4019

4020

4021

4022

4023

4024

4025

4026

4027

4028

4029

4030

4031

4032

4033

4034

4035

4036

4037

4038

4039

4040

4041

4042

4043

4044

4045

4046

4047

4048

4049

4050

4051

4052

4053

4054

4055

4056

4057

4058

4059

4060

4061

4062

4063

4064

4065

4066

4067

4068

4069

4070

4071

4072

4073

4074

4075

4076

4077

4078

4079

4080

4081

4082

4083

4084

4085

4086

4087

4088

4089

4090

4091

4092

4093

4094

4095

4096

4097

4098

4099

4100

4101

4102

4103

4104

4105

4106

4107

4108

4109

4110

4111

4112

4113

4114

4115

4116

4117

4118

4119

4120

4121

4122

4123

4124

4125

4126

4127

4128

4129

4130

4131

4132

4133

4134

4135

4136

4137

4138

4139

4140

4141

4142

4143

4144

4145

4146

4147

4148

4149

4150

4151

4152

4153

4154

4155

4156

4157

4158

4159

4160

4161

4162

4163

4164

4165

4166

4167

4168

4169

4170

4171

4172

4173

4174

4175

4176

4177

4178

4179

4180

4181

4182

4183

4184

4185

4186

4187

4188

4189

4190

4191

4192

4193

4194

4195

4196

4197

4198

4199

4200

4201

4202

4203

4204

4205

4206

4207

4208

4209

4210

4211

4212

4213

4214

4215

4216

4217

4218

4219

4220

4221

4222

4223

4224

4225

4226

4227

4228

4229

4230

4231

4232

4233

4234

4235

4236

4237

4238

4239

4240

4241

4242

4243

4244

4245

4246

4247

4248

4249

4250

4251

4252

4253

4254

4255

4256

4257

4258

4259

4260

4261

4262

4263

4264

4265

4266

4267

4268

4269

4270

4271

4272

4273

4274

4275

4276

4277

4278

4279

4280

4281

4282

4283

4284

4285

4286

4287

4288

4289

4290

4291

4292

4293

4294

4295

4296

4297

4298

4299

4300

4301

4302

4303

4304

4305

4306

4307

4308

4309

4310

4311

4312

4313

4314

4315

4316

4317

4318

4319

4320

4321

4322

4323

4324

4325

4326

4327

4328

4329

4330

4331

4332

4333

4334

4335

4336

4337

4338

4339

4340

4341

4342

4343

4344

4345

4346

4347

4348

4349

4350

4351

4352

4353

4354

4355

4356

4357

4358

4359

4360

4361

4362

4363

4364

4365

4366

4367

4368

4369

4370

4371

4372

4373

4374

4375

4376

4377

4378

4379

4380

4381

4382

4383

4384

4385

4386

4387

4388

4389

4390

4391

4392

4393

4394

4395

4396

4397

4398

4399

4400

4401

4402

4403

4404

4405

4406

4407

4408

4409

4410

4411

4412

4413

4414

4415

4416

4417

4418

4419

4420

4421

4422

4423

4424

4425

4426

4427

4428

4429

4430

4431

4432

4433

4434

4435

4436

4437

4438

4439

4440

4441

4442

4443

4444

4445

4446

4447

4448

4449

4450

4451

4452

4453

4454

4455

4456

4457

4458

4459

4460

4461

4462

4463

4464

4465

4466

4467

4468

4469

4470

4471

4472

4473

4474

4475

4476

4477

4478

4479

4480

4481

4482

4483

4484

4485

4486

4487

4488

4489

4490

4491

4492

4493

4494

4495

4496

4497

4498

4499

4500

4501

4502

4503

4504

4505

4506

4507

4508

4509

4510

4511

4512

4513

4514

4515

4516

4517

4518

4519

4520

4521

4522

4523

4524

4525

4526

4527

4528

4529

4530

4531

4532

4533

4534

4535

4536

4537

4538

4539

4540

4541

4542

4543

4544

4545

4546

4547

4548

4549

4550

4551

4552

4553

4554

4555

4556

4557

4558

4559

4560

4561

4562

4563

4564

4565

4566

4567

4568

4569

4570

4571

4572

4573

4574

4575

4576

4577

4578

4579

4580

4581

4582

4583

4584

4585

4586

4587

4588

4589

4590

4591

4592

4593

4594

4595

4596

4597

4598

4599

4600

4601

4602

4603

4604

4605

4606

4607

4608

4609

4610

4611

4612

4613

4614

4615

4616

4617

4618

4619

4620

4621

4622

4623

4624

4625

4626

4627

4628

4629

4630

4631

4632

4633

4634

4635

4636

4637

4638

4639

4640

4641

4642

4643

4644

4645

4646

4647

4648

4649

4650

4651

4652

4653

4654

4655

4656

4657

4658

4659

4660

4661

4662

4663

4664

4665

4666

4667

4668

4669

4670

4671

4672

4673

4674

4675

4676

4677

4678

4679

4680

4681

4682

4683

4684

4685

4686

4687

4688

4689

4690

4691

4692

4693

4694

4695

4696

4697

4698

4699

4700

4701

4702

4703

4704

4705

4706

4707

4708

4709

4710

4711

4712

4713

4714

4715

4716

4717

4718

4719

4720

4721

4722

4723

4724

4725

4726

4727

4728

4729

4730

4731

4732

4733

4734

4735

4736

4737

4738

4739

4740

4741

4742

4743

4744

4745

4746

4747

4748

4749

4750

4751

4752

4753

4754

4755

4756

4757

4758

4759

4760

4761

4762

4763

4764

4765

4766

4767

4768

4769

4770

4771

4772

4773

4774

4775

4776

4777

4778

4779

4780

4781

4782

4783

4784

4785

4786

4787

4788

4789

4790

4791

4792

4793

4794

4795

4796

4797

4798

4799

4800

4801

4802

4803

4804

4805

4806

4807

4808

4809

4810

4811

4812

4813

4814

4815

4816

4817

4818

4819

4820

4821

4822

4823

4824

4825

4826

4827

4828

4829

4830

4831

4832

4833

4834

4835

4836

4837

4838

4839

4840

4841

4842

4843

4844

4845

4846

4847

4848

4849

4850

4851

4852

4853

4854

4855

4856

4857

4858

4859

4860

4861

4862

4863

4864

4865

4866

4867

4868

4869

4870

4871

4872

4873

4874

4875

4876

4877

4878

4879

4880

4881

4882

4883

4884

4885

4886

4887

4888

4889

4890

4891

4892

4893

4894

4895

4896

4897

4898

4899

4900

4901

4902

4903

4904

4905

4906

4907

4908

4909

4910

4911

4912

4913

4914

4915

4916

4917

4918

4919

4920

4921

4922

4923

4924

4925

4926

4927

4928

4929

4930

4931

4932

4933

4934

4935

4936

4937

4938

4939

4940

4941

4942

4943

4944

4945

4946

4947

4948

4949

4950

4951

4952

4953

4954

4955

4956

4957

4958

4959

4960

4961

4962

4963

4964

4965

4966

4967

4968

4969

4970

4971

4972

4973

4974

4975

4976

4977

4978

4979

4980

4981

4982

4983

4984

4985

4986

4987

4988

4989

4990

4991

4992

4993

4994

4995

4996

4997

4998

4999

5000

5001

5002

5003

5004

5005

5006

5007

5008

5009

5010

5011

5012

5013

5014

5015

5016

5017

5018

5019

5020

5021

5022

5023

5024

5025

5026

5027

5028

5029

5030

5031

5032

5033

5034

5035

5036

5037

5038

5039

5040

5041

5042

5043

5044

5045

5046

5047

5048

5049

5050

5051

5052

5053

5054

5055

5056

5057

5058

5059

5060

5061

5062

5063

5064

5065

5066

5067

5068

5069

5070

5071

5072

5073

5074

5075

5076

5077

5078

5079

5080

5081

5082

5083

5084

5085

5086

5087

5088

5089

5090

5091

5092

5093

5094

5095

5096

5097

5098

5099

5100

5101

5102

5103

5104

5105

5106

5107

5108

5109

5110

5111

5112

5113

5114

5115

5116

5117

5118

5119

5120

5121

5122

5123

5124

5125

5126

5127

5128

5129

5130

5131

5132

5133

5134

5135

5136

5137

5138

5139

5140

5141

5142

5143

5144

5145

5146

5147

5148

5149

5150

5151

5152

5153

5154

5155

5156

5157

5158

5159

5160

5161

5162

5163

5164

5165

5166

5167

5168

5169

5170

5171

5172

5173

5174

5175

5176

5177

5178

5179

5180

5181

5182

5183

5184

5185

5186

5187

5188

5189

5190

5191

5192

5193

5194

5195

5196

5197

5198

5199

5200

5201

5202

5203

5204

5205

5206

5207

5208

5209

5210

5211

5212

5213

5214

5215

5216

5217

5218

5219

5220

5221

5222

5223

5224

5225

5226

5227

5228

5229

5230

5231

5232

5233

5234

5235

5236

5237

5238

5239

5240

5241

5242

5243

5244

5245

5246

5247

5248

5249

5250

5251

5252

r""" 

Arbitrary Precision Real Intervals 

  

AUTHORS: 

  

- Carl Witty (2007-01-21): based on ``real_mpfr.pyx``; changed it to 

use mpfi rather than mpfr. 

  

- William Stein (2007-01-24): modifications and clean up and docs, etc. 

  

- Niles Johnson (2010-08): :trac:`3893`: ``random_element()`` should pass 

on ``*args`` and ``**kwds``. 

  

- Travis Scrimshaw (2012-10-20): Fixing scientific notation output 

to fix :trac:`13634`. 

  

- Travis Scrimshaw (2012-11-02): Added doctests for full coverage 

  

This is a straightforward binding to the MPFI library; it may be 

useful to refer to its documentation for more details. 

  

An interval is represented as a pair of floating-point numbers `a` 

and `b` (where `a \leq b`) and is printed as a standard floating-point 

number with a question mark (for instance, ``3.1416?``). The question 

mark indicates that the preceding digit may have an error of `\pm 1`. 

These floating-point numbers are implemented using MPFR (the same 

as the :class:`RealNumber` elements of 

:class:`~sage.rings.real_mpfr.RealField_class`). 

  

There is also an alternate method of printing, where the interval 

prints as ``[a .. b]`` (for instance, ``[3.1415 .. 3.1416]``). 

  

The interval represents the set `\{ x : a \leq x \leq b \}` (so if `a = b`, 

then the interval represents that particular floating-point number). The 

endpoints can include positive and negative infinity, with the 

obvious meaning. It is also possible to have a ``NaN`` (Not-a-Number) 

interval, which is represented by having either endpoint be ``NaN``. 

  

PRINTING: 

  

There are two styles for printing intervals: 'brackets' style and 

'question' style (the default). 

  

In question style, we print the "known correct" part of the number, 

followed by a question mark. The question mark indicates that the 

preceding digit is possibly wrong by `\pm 1`. 

  

:: 

  

sage: RIF(sqrt(2)) 

1.414213562373095? 

  

However, if the interval is precise (its lower bound is equal to 

its upper bound) and equal to a not-too-large integer, then we just 

print that integer. 

  

:: 

  

sage: RIF(0) 

0 

sage: RIF(654321) 

654321 

  

:: 

  

sage: RIF(123, 125) 

124.? 

sage: RIF(123, 126) 

1.3?e2 

  

As we see in the last example, question style can discard almost a 

whole digit's worth of precision. We can reduce this by allowing 

"error digits": an error following the question mark, that gives 

the maximum error of the digit(s) before the question mark. If the 

error is absent (which it always is in the default printing), then 

it is taken to be 1. 

  

:: 

  

sage: RIF(123, 126).str(error_digits=1) 

'125.?2' 

sage: RIF(123, 127).str(error_digits=1) 

'125.?2' 

sage: v = RIF(-e, pi); v 

0.?e1 

sage: v.str(error_digits=1) 

'1.?4' 

sage: v.str(error_digits=5) 

'0.2117?29300' 

  

Error digits also sometimes let us indicate that the interval is 

actually equal to a single floating-point number:: 

  

sage: RIF(54321/256) 

212.19140625000000? 

sage: RIF(54321/256).str(error_digits=1) 

'212.19140625000000?0' 

  

In brackets style, intervals are printed with the left value 

rounded down and the right rounded up, which is conservative, but 

in some ways unsatisfying. 

  

Consider a 3-bit interval containing exactly the floating-point 

number 1.25. In round-to-nearest or round-down, this prints as 1.2; 

in round-up, this prints as 1.3. The straightforward options, then, 

are to print this interval as ``[1.2 .. 1.2]`` (which does not even 

contain the true value, 1.25), or to print it as ``[1.2 .. 1.3]`` 

(which gives the impression that the upper and lower bounds are not 

equal, even though they really are). Neither of these is very 

satisfying, but we have chosen the latter. 

  

:: 

  

sage: R = RealIntervalField(3) 

sage: a = R(1.25) 

sage: a.str(style='brackets') 

'[1.2 .. 1.3]' 

sage: a == 1.25 

True 

sage: a == 2 

False 

  

COMPARISONS: 

  

Comparison operations (``==``, ``!=``, ``<``, ``<=``, ``>``, ``>=``) 

return ``True`` if every value in the first interval has the given relation 

to every value in the second interval. 

  

This convention for comparison operators has good and bad points. The 

good: 

  

- Expected transitivity properties hold (if ``a > b`` and ``b == c``, then 

``a > c``; etc.) 

  

- ``a == 0`` is true if the interval contains only the floating-point number 

0; similarly for ``a == 1`` 

  

- ``a > 0`` means something useful (that every value in the interval is 

greater than 0) 

  

The bad: 

  

- Trichotomy fails to hold: there are values ``(a,b)`` such that none of 

``a < b``, ``a == b``, or ``a > b`` are true 

  

- There are values ``a`` and ``b`` such that ``a <= b`` but neither ``a < b`` 

nor ``a == b`` hold. 

  

- There are values ``a`` and ``b`` such that neither ``a != b`` 

nor ``a == b`` hold. 

  

.. NOTE:: 

  

Intervals ``a`` and ``b`` overlap iff ``not(a != b)``. 

  

.. WARNING:: 

  

The ``cmp(a, b)`` function should not be used to compare real 

intervals. Note that ``cmp`` will disappear in Python3. 

  

EXAMPLES:: 

  

sage: 0 < RIF(1, 2) 

True 

sage: 0 == RIF(0) 

True 

sage: not(0 == RIF(0, 1)) 

True 

sage: not(0 != RIF(0, 1)) 

True 

sage: 0 <= RIF(0, 1) 

True 

sage: not(0 < RIF(0, 1)) 

True 

  

Comparison with infinity is defined through coercion to the infinity 

ring where semi-infinite intervals are sent to their central value 

(plus or minus infinity); This implements the above convention for 

inequalities:: 

  

sage: InfinityRing.has_coerce_map_from(RIF) 

True 

sage: -oo < RIF(-1,1) < oo 

True 

sage: -oo < RIF(0,oo) <= oo 

True 

sage: -oo <= RIF(-oo,-1) < oo 

True 

  

Comparison by equality shows what the semi-infinite intervals actually 

coerce to:: 

  

sage: RIF(1,oo) == oo 

True 

sage: RIF(-oo,-1) == -oo 

True 

  

For lack of a better value in the infinity ring, the doubly infinite 

interval coerces to plus infinity:: 

  

sage: RIF(-oo,oo) == oo 

True 

  

If you want to compare two intervals lexicographically, you can use the 

method ``lexico_cmp``. However, the behavior of this method is not 

specified if given a non-interval and an interval:: 

  

sage: RIF(0).lexico_cmp(RIF(0, 1)) 

-1 

sage: RIF(0, 1).lexico_cmp(RIF(0)) 

1 

sage: RIF(0, 1).lexico_cmp(RIF(1)) 

-1 

sage: RIF(0, 1).lexico_cmp(RIF(0, 1)) 

0 

  

TESTS: 

  

Comparisons with numpy types are right (see :trac:`17758` and :trac:`18076`):: 

  

sage: import numpy 

sage: RIF(0,1) < numpy.float('2') 

True 

sage: RIF(0,1) <= numpy.float('1') 

True 

sage: RIF(0,1) <= numpy.float('0.5') 

False 

sage: RIF(2) == numpy.int8('2') 

True 

sage: numpy.int8('2') == RIF(2) 

True 

""" 

  

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

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

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

# 

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

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

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

# (at your option) any later version. 

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

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

  

from __future__ import absolute_import, print_function 

  

from libc.string cimport strlen 

from cpython.mem cimport * 

from cpython.object cimport Py_EQ, Py_NE, Py_LT, Py_LE, Py_GT, Py_GE 

  

from cysignals.signals cimport sig_on, sig_off 

  

from sage.libs.gmp.mpz cimport * 

from sage.libs.mpfr cimport * 

from sage.libs.mpfi cimport * 

from sage.arith.constants cimport LOG_TEN_TWO_PLUS_EPSILON 

  

cimport sage.structure.element 

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

from sage.structure.parent cimport Parent 

from sage.structure.richcmp cimport richcmp 

  

from .convert.mpfi cimport mpfi_set_sage 

from .real_mpfr cimport RealField_class, RealNumber, RealField 

from .integer cimport Integer 

from .real_double cimport RealDoubleElement 

from .real_double import RDF 

from .integer_ring import ZZ 

from .rational_field import QQ 

from sage.categories.morphism cimport Map 

  

cimport sage.rings.real_mpfr as real_mpfr 

  

import math # for log 

import sys 

import operator 

  

from sage.cpython.string cimport char_to_str, bytes_to_str 

  

import sage.rings.complex_field 

import sage.rings.infinity 

  

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

# 

# Implementation 

# 

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

  

# Global settings 

printing_style = 'question' 

printing_error_digits = 0 

  

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

# 

# Real Interval Field 

# 

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

  

cdef dict RealIntervalField_cache = {} 

cpdef RealIntervalField_class RealIntervalField(prec=53, sci_not=False): 

r""" 

Construct a :class:`RealIntervalField_class`, with caching. 

  

INPUT: 

  

- ``prec`` -- (integer) precision; default = 53: 

The number of bits used to represent the mantissa of a 

floating-point number. The precision can be any integer between 

:func:`mpfr_prec_min()` and :func:`mpfr_prec_max()`. In the current 

implementation, :func:`mpfr_prec_min()` is equal to 2. 

  

- ``sci_not`` -- (default: ``False``) whether or not to display using 

scientific notation 

  

EXAMPLES:: 

  

sage: RealIntervalField() 

Real Interval Field with 53 bits of precision 

sage: RealIntervalField(200, sci_not=True) 

Real Interval Field with 200 bits of precision 

sage: RealIntervalField(53) is RIF 

True 

sage: RealIntervalField(200) is RIF 

False 

sage: RealIntervalField(200) is RealIntervalField(200) 

True 

  

See the documentation for :class:`RealIntervalField_class 

<sage.rings.real_mpfi.RealIntervalField_class>` for many more 

examples. 

""" 

try: 

return RealIntervalField_cache[prec, sci_not] 

except KeyError: 

RealIntervalField_cache[prec, sci_not] = R = RealIntervalField_class(prec, sci_not) 

return R 

  

  

cdef class RealIntervalField_class(Field): 

""" 

Class of the real interval field. 

  

INPUT: 

  

- ``prec`` -- (integer) precision; default = 53 ``prec`` is 

the number of bits used to represent the mantissa of a 

floating-point number. The precision can be any integer between 

:func:`~sage.rings.real_mpfr.mpfr_prec_min()` and 

:func:`~sage.rings.real_mpfr.mpfr_prec_max()`. In the current 

implementation, :func:`~sage.rings.real_mpfr.mpfr_prec_min()` 

is equal to 2. 

  

- ``sci_not`` -- (default: ``False``) whether or not to display using 

scientific notation 

  

EXAMPLES:: 

  

sage: RealIntervalField(10) 

Real Interval Field with 10 bits of precision 

sage: RealIntervalField() 

Real Interval Field with 53 bits of precision 

sage: RealIntervalField(100000) 

Real Interval Field with 100000 bits of precision 

  

.. NOTE:: 

  

The default precision is 53, since according to the GMP manual: 

'mpfr should be able to exactly reproduce all computations with 

double-precision machine floating-point numbers (double type in 

C), except the default exponent range is much wider and 

subnormal numbers are not implemented.' 

  

EXAMPLES: 

  

Creation of elements. 

  

First with default precision. First we coerce elements of various 

types, then we coerce intervals:: 

  

sage: RIF = RealIntervalField(); RIF 

Real Interval Field with 53 bits of precision 

sage: RIF(3) 

3 

sage: RIF(RIF(3)) 

3 

sage: RIF(pi) 

3.141592653589794? 

sage: RIF(RealField(53)('1.5')) 

1.5000000000000000? 

sage: RIF(-2/19) 

-0.1052631578947369? 

sage: RIF(-3939) 

-3939 

sage: RIF(-3939r) 

-3939 

sage: RIF('1.5') 

1.5000000000000000? 

sage: R200 = RealField(200) 

sage: RIF(R200.pi()) 

3.141592653589794? 

sage: RIF(10^100) 

1.000000000000000?e100 

  

The base must be explicitly specified as a named parameter:: 

  

sage: RIF('101101', base=2) 

45 

sage: RIF('+infinity') 

[+infinity .. +infinity] 

sage: RIF('[1..3]').str(style='brackets') 

'[1.0000000000000000 .. 3.0000000000000000]' 

  

All string-like types are accepted:: 

  

sage: RIF(b"100", u"100") 

100 

  

Next we coerce some 2-tuples, which define intervals:: 

  

sage: RIF((-1.5, -1.3)) 

-1.4? 

sage: RIF((RDF('-1.5'), RDF('-1.3'))) 

-1.4? 

sage: RIF((1/3,2/3)).str(style='brackets') 

'[0.33333333333333331 .. 0.66666666666666675]' 

  

The extra parentheses aren't needed:: 

  

sage: RIF(1/3,2/3).str(style='brackets') 

'[0.33333333333333331 .. 0.66666666666666675]' 

sage: RIF((1,2)).str(style='brackets') 

'[1.0000000000000000 .. 2.0000000000000000]' 

sage: RIF((1r,2r)).str(style='brackets') 

'[1.0000000000000000 .. 2.0000000000000000]' 

sage: RIF((pi, e)).str(style='brackets') 

'[2.7182818284590450 .. 3.1415926535897936]' 

  

Values which can be represented as an exact floating-point number 

(of the precision of this ``RealIntervalField``) result in a precise 

interval, where the lower bound is equal to the upper bound (even 

if they print differently). Other values typically result in an 

interval where the lower and upper bounds are adjacent 

floating-point numbers. 

  

:: 

  

sage: def check(x): 

....: return (x, x.lower() == x.upper()) 

sage: check(RIF(pi)) 

(3.141592653589794?, False) 

sage: check(RIF(RR(pi))) 

(3.1415926535897932?, True) 

sage: check(RIF(1.5)) 

(1.5000000000000000?, True) 

sage: check(RIF('1.5')) 

(1.5000000000000000?, True) 

sage: check(RIF(0.1)) 

(0.10000000000000001?, True) 

sage: check(RIF(1/10)) 

(0.10000000000000000?, False) 

sage: check(RIF('0.1')) 

(0.10000000000000000?, False) 

  

Similarly, when specifying both ends of an interval, the lower end 

is rounded down and the upper end is rounded up:: 

  

sage: outward = RIF(1/10, 7/10); outward.str(style='brackets') 

'[0.099999999999999991 .. 0.70000000000000007]' 

sage: nearest = RIF(RR(1/10), RR(7/10)); nearest.str(style='brackets') 

'[0.10000000000000000 .. 0.69999999999999996]' 

sage: nearest.lower() - outward.lower() 

1.38777878078144e-17 

sage: outward.upper() - nearest.upper() 

1.11022302462516e-16 

  

Some examples with a real interval field of higher precision:: 

  

sage: R = RealIntervalField(100) 

sage: R(3) 

3 

sage: R(R(3)) 

3 

sage: R(pi) 

3.14159265358979323846264338328? 

sage: R(-2/19) 

-0.1052631578947368421052631578948? 

sage: R(e,pi).str(style='brackets') 

'[2.7182818284590452353602874713512 .. 3.1415926535897932384626433832825]' 

  

TESTS:: 

  

sage: RIF(0, 10^200) 

1.?e200 

sage: RIF(10^100, 10^200) 

1.?e200 

sage: RIF.lower_field() is RealField(53, rnd='RNDD') 

True 

sage: RIF.upper_field() is RealField(53, rnd='RNDU') 

True 

sage: RIF.middle_field() is RR 

True 

sage: TestSuite(RIF).run() 

""" 

Element = RealIntervalFieldElement 

  

def __init__(self, mpfr_prec_t prec=53, int sci_not=0): 

""" 

Initialize ``self``. 

  

EXAMPLES:: 

  

sage: RealIntervalField() 

Real Interval Field with 53 bits of precision 

sage: RealIntervalField(200) 

Real Interval Field with 200 bits of precision 

""" 

if prec < MPFR_PREC_MIN or prec > MPFR_PREC_MAX: 

raise ValueError("prec (=%s) must be >= %s and <= %s." % ( 

prec, MPFR_PREC_MIN, MPFR_PREC_MAX)) 

self.__prec = prec 

self.sci_not = sci_not 

self.__lower_field = RealField(prec, sci_not, "RNDD") 

self.__middle_field = RealField(prec, sci_not, "RNDN") 

self.__upper_field = RealField(prec, sci_not, "RNDU") 

from sage.categories.fields import Fields 

Field.__init__(self, self, category=Fields().Infinite()) 

self._populate_coercion_lists_(convert_method_name="_real_mpfi_") 

  

def lower_field(self): 

""" 

Return the :class:`RealField_class` with rounding mode ``'RNDD'`` 

(rounding towards minus infinity). 

  

EXAMPLES:: 

  

sage: RIF.lower_field() 

Real Field with 53 bits of precision and rounding RNDD 

sage: RealIntervalField(200).lower_field() 

Real Field with 200 bits of precision and rounding RNDD 

""" 

return self.__lower_field 

  

def middle_field(self): 

""" 

Return the :class:`RealField_class` with rounding mode ``'RNDN'`` 

(rounding towards nearest). 

  

EXAMPLES:: 

  

sage: RIF.middle_field() 

Real Field with 53 bits of precision 

sage: RealIntervalField(200).middle_field() 

Real Field with 200 bits of precision 

""" 

return self.__middle_field 

  

def upper_field(self): 

""" 

Return the :class:`RealField_class` with rounding mode ``'RNDU'`` 

(rounding towards plus infinity). 

  

EXAMPLES:: 

  

sage: RIF.upper_field() 

Real Field with 53 bits of precision and rounding RNDU 

sage: RealIntervalField(200).upper_field() 

Real Field with 200 bits of precision and rounding RNDU 

""" 

return self.__upper_field 

  

def _real_field(self, rnd): 

""" 

Return the :class:`RealField_class` with rounding mode ``rnd``. 

  

EXAMPLES:: 

  

sage: RIF._real_field('RNDN') 

Real Field with 53 bits of precision 

sage: RIF._real_field('RNDZ') 

Real Field with 53 bits of precision and rounding RNDZ 

sage: RealIntervalField(200)._real_field('RNDD') 

Real Field with 200 bits of precision and rounding RNDD 

""" 

if rnd == "RNDD": 

return self.lower_field() 

elif rnd == "RNDN": 

return self.middle_field() 

elif rnd == "RNDU": 

return self.upper_field() 

else: 

return RealField(self.__prec, self.sci_not, rnd) 

  

def _repr_(self): 

""" 

Return a string representation of ``self``. 

  

EXAMPLES:: 

  

sage: RealIntervalField() # indirect doctest 

Real Interval Field with 53 bits of precision 

sage: RealIntervalField(200) # indirect doctest 

Real Interval Field with 200 bits of precision 

""" 

s = "Real Interval Field with %s bits of precision"%self.__prec 

return s 

  

def _latex_(self): 

r""" 

LaTeX representation for the real interval field. 

  

EXAMPLES:: 

  

sage: latex(RIF) # indirect doctest 

\Bold{I} \Bold{R} 

""" 

return "\\Bold{I} \\Bold{R}" 

  

def _sage_input_(self, sib, coerce): 

r""" 

Produce an expression which will reproduce this value when evaluated. 

  

EXAMPLES:: 

  

sage: sage_input(RIF, verify=True) 

# Verified 

RIF 

sage: sage_input(RealIntervalField(25), verify=True) 

# Verified 

RealIntervalField(25) 

sage: k = (RIF, RealIntervalField(37), RealIntervalField(1024)) 

sage: sage_input(k, verify=True) 

# Verified 

(RIF, RealIntervalField(37), RealIntervalField(1024)) 

sage: sage_input((k, k), verify=True) 

# Verified 

RIF37 = RealIntervalField(37) 

RIF1024 = RealIntervalField(1024) 

((RIF, RIF37, RIF1024), (RIF, RIF37, RIF1024)) 

sage: from sage.misc.sage_input import SageInputBuilder 

sage: RealIntervalField(2)._sage_input_(SageInputBuilder(), False) 

{call: {atomic:RealIntervalField}({atomic:2})} 

""" 

if self.prec() == 53: 

return sib.name('RIF') 

  

v = sib.name('RealIntervalField')(sib.int(self.prec())) 

name = 'RIF%d' % self.prec() 

sib.cache(self, v, name) 

return v 

  

cpdef bint is_exact(self) except -2: 

""" 

Returns whether or not this field is exact, which is always ``False``. 

  

EXAMPLES:: 

  

sage: RIF.is_exact() 

False 

""" 

return False 

  

def __call__(self, x=None, y=None, **kwds): 

""" 

Create an element in this real interval field. 

  

INPUT: 

  

- ``x`` -- a number, string, or 2-tuple 

  

- ``y`` -- (default: ``None``); if given ``x`` is set to ``(x,y)``; 

this is so you can write ``R(2,3)`` to make the interval from 2 to 3 

  

- ``base`` -- integer (default: 10) - only used if ``x`` is a string 

  

OUTPUT: an element of this real interval field. 

  

EXAMPLES:: 

  

sage: R = RealIntervalField(20) 

sage: R('1.234') 

1.23400? 

sage: R('2', base=2) 

Traceback (most recent call last): 

... 

TypeError: unable to convert '2' to real interval 

sage: a = R('1.1001', base=2); a 

1.5625000? 

sage: a.str(2) 

'1.1001000000000000000?' 

  

Type: RealIntervalField? for more information. 

""" 

# Note: we override Parent.__call__ because we want to support 

# RIF(a, b) and that is hard to do using coerce maps. 

if y is not None: 

return self.element_class(self, [x, y], **kwds) 

if kwds: 

return self.element_class(self, x, **kwds) 

return Parent.__call__(self, x) 

  

def algebraic_closure(self): 

""" 

Return the algebraic closure of this interval field, i.e., the 

complex interval field with the same precision. 

  

EXAMPLES:: 

  

sage: RIF.algebraic_closure() 

Complex Interval Field with 53 bits of precision 

sage: RIF.algebraic_closure() is CIF 

True 

sage: RealIntervalField(100).algebraic_closure() 

Complex Interval Field with 100 bits of precision 

""" 

return self.complex_field() 

  

def construction(self): 

r""" 

Returns the functorial construction of ``self``, namely, completion of 

the rational numbers with respect to the prime at `\infty`, 

and the note that this is an interval field. 

  

Also preserves other information that makes this field unique (e.g. 

precision, print mode). 

  

EXAMPLES:: 

  

sage: R = RealIntervalField(123) 

sage: c, S = R.construction(); S 

Rational Field 

sage: R == c(S) 

True 

""" 

from sage.categories.pushout import CompletionFunctor 

return (CompletionFunctor(sage.rings.infinity.Infinity, 

self.prec(), 

{'sci_not': self.scientific_notation(), 'type': 'Interval'}), 

sage.rings.rational_field.QQ) 

  

cpdef _coerce_map_from_(self, S): 

""" 

Canonical coercion from ``S`` to this real interval field. 

  

The rings that canonically coerce to this mpfi real field are: 

  

- this mpfi field itself 

  

- any mpfr real field with precision that is as large as this 

one 

  

- any other mpfi real field with precision that is as large as 

this one 

  

- anything that canonically coerces to the mpfr real field 

with same precision as ``self``. 

  

Values which can be exactly represented as a floating-point number 

are coerced to a precise interval, with upper and lower bounds 

equal; otherwise, the upper and lower bounds will typically be 

adjacent floating-point numbers that surround the given value. 

  

EXAMPLES:: 

  

sage: phi = RIF.coerce_map_from(ZZ); phi 

Coercion map: 

From: Integer Ring 

To: Real Interval Field with 53 bits of precision 

sage: phi(3^100) 

5.153775207320114?e47 

sage: phi = RIF.coerce_map_from(float); phi 

Coercion map: 

From: Set of Python objects of class 'float' 

To: Real Interval Field with 53 bits of precision 

sage: phi(math.pi) 

3.1415926535897932? 

  

Coercion can decrease precision, but not increase it:: 

  

sage: phi = RIF.coerce_map_from(RealIntervalField(100)); phi 

Coercion map: 

From: Real Interval Field with 100 bits of precision 

To: Real Interval Field with 53 bits of precision 

sage: phi = RIF.coerce_map_from(RealField(100)); phi 

Coercion map: 

From: Real Field with 100 bits of precision 

To: Real Interval Field with 53 bits of precision 

sage: print(RIF.coerce_map_from(RealIntervalField(20))) 

None 

sage: print(RIF.coerce_map_from(RealField(20))) 

None 

  

:: 

  

sage: phi = RIF.coerce_map_from(AA); phi 

Conversion via _real_mpfi_ method map: 

From: Algebraic Real Field 

To: Real Interval Field with 53 bits of precision 

""" 

prec = self.__prec 

  

# Direct and efficient conversions 

if S is ZZ or S is QQ: 

return True 

if S is float or S is int or S is long: 

return True 

if isinstance(S, RealIntervalField_class): 

return (<RealIntervalField_class>S).__prec >= prec 

if isinstance(S, RealField_class): 

return (<RealField_class>S).__prec >= prec 

if S is RDF: 

return 53 >= prec 

from .number_field.number_field import NumberField_quadratic 

if isinstance(S, NumberField_quadratic): 

return S.discriminant() > 0 

  

# If coercion to RR is possible and there is a _real_mpfi_ 

# method, assume that it defines a coercion to RIF 

if self.__middle_field.has_coerce_map_from(S): 

return self._convert_method_map(S, "_real_mpfi_") 

return None 

  

def __richcmp__(self, other, int op): 

""" 

Compare ``self`` to ``other``. 

  

EXAMPLES:: 

  

sage: RealIntervalField(10) == RealIntervalField(11) 

False 

sage: RealIntervalField(10) == RealIntervalField(10) 

True 

sage: RealIntervalField(10,sci_not=True) == RealIntervalField(10,sci_not=False) 

True 

sage: RealIntervalField(10) == IntegerRing() 

False 

""" 

if not isinstance(other, RealIntervalField_class): 

if op in [Py_EQ, Py_NE]: 

return (op == Py_NE) 

return NotImplemented 

  

cdef RealIntervalField_class left 

cdef RealIntervalField_class right 

left = self 

right = other # to access C structure 

return richcmp(left.__prec, right.__prec, op) 

  

def __reduce__(self): 

""" 

For pickling. 

  

EXAMPLES:: 

  

sage: R = RealIntervalField(sci_not=1, prec=200) 

sage: loads(dumps(R)) == R 

True 

""" 

return __create__RealIntervalField_version0, (self.__prec, self.sci_not) 

  

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

""" 

Return a random element of ``self``. Any arguments or keywords are 

passed onto the random element function in real field. 

  

By default, this is uniformly distributed in `[-1, 1]`. 

  

EXAMPLES:: 

  

sage: RIF.random_element() 

0.15363619378561300? 

sage: RIF.random_element() 

-0.50298737524751780? 

sage: RIF.random_element(-100, 100) 

60.958996432224126? 

  

Passes extra positional or keyword arguments through:: 

  

sage: RIF.random_element(min=0, max=100) 

2.5572702830891970? 

sage: RIF.random_element(min=-100, max=0) 

-1.5803457307118123? 

""" 

return self(self.middle_field().random_element(*args, **kwds)) 

  

def gen(self, i=0): 

""" 

Return the ``i``-th generator of ``self``. 

  

EXAMPLES:: 

  

sage: RIF.gen(0) 

1 

sage: RIF.gen(1) 

Traceback (most recent call last): 

... 

IndexError: self has only one generator 

""" 

if i == 0: 

return self(1) 

else: 

raise IndexError("self has only one generator") 

  

def complex_field(self): 

""" 

Return complex field of the same precision. 

  

EXAMPLES:: 

  

sage: RIF.complex_field() 

Complex Interval Field with 53 bits of precision 

""" 

return sage.rings.complex_interval_field.ComplexIntervalField(self.prec()) 

  

def ngens(self): 

""" 

Return the number of generators of ``self``, which is 1. 

  

EXAMPLES:: 

  

sage: RIF.ngens() 

1 

""" 

return 1 

  

def gens(self): 

""" 

Return a list of generators. 

  

EXAMPLES:: 

  

sage: RIF.gens() 

[1] 

""" 

return [self.gen()] 

  

def _is_valid_homomorphism_(self, codomain, im_gens): 

""" 

Return ``True`` if the map from ``self`` to ``codomain`` sending 

``self(1)`` to the unique element of ``im_gens`` is a valid field 

homomorphism. Otherwise, return ``False``. 

  

EXAMPLES:: 

  

sage: RIF._is_valid_homomorphism_(RDF,[RDF(1)]) 

False 

sage: RIF._is_valid_homomorphism_(CIF,[CIF(1)]) 

True 

sage: RIF._is_valid_homomorphism_(CIF,[CIF(-1)]) 

False 

sage: R=RealIntervalField(100) 

sage: RIF._is_valid_homomorphism_(R,[R(1)]) 

False 

sage: RIF._is_valid_homomorphism_(CC,[CC(1)]) 

False 

sage: RIF._is_valid_homomorphism_(GF(2),GF(2)(1)) 

False 

""" 

try: 

s = codomain._coerce_(self(1)) 

except TypeError: 

return False 

return s == im_gens[0] 

  

def _repr_option(self, key): 

""" 

Metadata about the :meth:`_repr_` output. 

  

See :meth:`sage.structure.parent._repr_option` for details. 

  

EXAMPLES:: 

  

sage: RealIntervalField(10)._repr_option('element_is_atomic') 

True 

""" 

if key == 'element_is_atomic': 

return True 

return super(RealIntervalField_class, self)._repr_option(key) 

  

def is_finite(self): 

""" 

Return ``False``, since the field of real numbers is not finite. 

  

EXAMPLES:: 

  

sage: RealIntervalField(10).is_finite() 

False 

""" 

return False 

  

def characteristic(self): 

""" 

Returns 0, since the field of real numbers has characteristic 0. 

  

EXAMPLES:: 

  

sage: RealIntervalField(10).characteristic() 

0 

""" 

return Integer(0) 

  

def name(self): 

""" 

Return the name of ``self``. 

  

EXAMPLES:: 

  

sage: RIF.name() 

'IntervalRealIntervalField53' 

sage: RealIntervalField(200).name() 

'IntervalRealIntervalField200' 

""" 

return "IntervalRealIntervalField%s"%(self.__prec) 

  

def __hash__(self): 

""" 

Return the hash value of ``self``. 

  

EXAMPLES:: 

  

sage: hash(RIF) == hash(RealIntervalField(53)) # indirect doctest 

True 

sage: hash(RealIntervalField(200)) == hash(RealIntervalField(200)) 

True 

""" 

return hash(self.name()) 

  

def precision(self): 

""" 

Return the precision of this field (in bits). 

  

EXAMPLES:: 

  

sage: RIF.precision() 

53 

sage: RealIntervalField(200).precision() 

200 

""" 

return self.__prec 

  

prec = precision 

  

def to_prec(self, prec): 

""" 

Returns a real interval field to the given precision. 

  

EXAMPLES:: 

  

sage: RIF.to_prec(200) 

Real Interval Field with 200 bits of precision 

sage: RIF.to_prec(20) 

Real Interval Field with 20 bits of precision 

sage: RIF.to_prec(53) is RIF 

True 

""" 

return RealIntervalField(prec) 

  

def _magma_init_(self, magma): 

r""" 

Return a string representation of ``self`` in the Magma language. 

  

EXAMPLES:: 

  

sage: magma(RealIntervalField(80)) # optional - magma # indirect doctest 

Real field of precision 24 

sage: floor(RR(log(2**80, 10))) 

24 

""" 

return "RealField(%s : Bits := true)" % self.prec() 

  

def pi(self): 

r""" 

Returns `\pi` to the precision of this field. 

  

EXAMPLES:: 

  

sage: R = RealIntervalField(100) 

sage: R.pi() 

3.14159265358979323846264338328? 

sage: R.pi().sqrt()/2 

0.88622692545275801364908374167? 

sage: R = RealIntervalField(150) 

sage: R.pi().sqrt()/2 

0.886226925452758013649083741670572591398774728? 

""" 

x = self._new() 

mpfi_const_pi(x.value) 

return x 

  

def euler_constant(self): 

""" 

Returns Euler's gamma constant to the precision of this field. 

  

EXAMPLES:: 

  

sage: RealIntervalField(100).euler_constant() 

0.577215664901532860606512090083? 

""" 

x = self._new() 

mpfi_const_euler(x.value) 

return x 

  

def log2(self): 

r""" 

Returns `\log(2)` to the precision of this field. 

  

EXAMPLES:: 

  

sage: R=RealIntervalField(100) 

sage: R.log2() 

0.693147180559945309417232121458? 

sage: R(2).log() 

0.693147180559945309417232121458? 

""" 

x = self._new() 

mpfi_const_log2(x.value) 

return x 

  

def scientific_notation(self, status=None): 

""" 

Set or return the scientific notation printing flag. 

  

If this flag is ``True`` then real numbers with this space as parent 

print using scientific notation. 

  

INPUT: 

  

- ``status`` -- boolean optional flag 

  

EXAMPLES:: 

  

sage: RIF(0.025) 

0.025000000000000002? 

sage: RIF.scientific_notation(True) 

sage: RIF(0.025) 

2.5000000000000002?e-2 

sage: RIF.scientific_notation(False) 

sage: RIF(0.025) 

0.025000000000000002? 

""" 

if status is None: 

return self.sci_not 

else: 

self.sci_not = status 

  

def zeta(self, n=2): 

""" 

Return an `n`-th root of unity in the real field, if one 

exists, or raise a ``ValueError`` otherwise. 

  

EXAMPLES:: 

  

sage: R = RealIntervalField() 

sage: R.zeta() 

-1 

sage: R.zeta(1) 

1 

sage: R.zeta(5) 

Traceback (most recent call last): 

... 

ValueError: No 5th root of unity in self 

""" 

if n == 1: 

return self(1) 

elif n == 2: 

return self(-1) 

raise ValueError("No %sth root of unity in self" % n) 

  

  

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

# 

# RealIntervalFieldElement -- element of Real Interval Field 

# 

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

cdef class RealIntervalFieldElement(RingElement): 

""" 

A real number interval. 

""" 

def __cinit__(self, parent, *args, **kwds): 

""" 

Initialize the parent of this element and allocate memory 

  

TESTS:: 

  

sage: from sage.rings.real_mpfi import RealIntervalFieldElement 

sage: RealIntervalFieldElement.__new__(RealIntervalFieldElement, None) 

Traceback (most recent call last): 

... 

TypeError: Cannot convert NoneType to sage.rings.real_mpfi.RealIntervalField_class 

sage: RealIntervalFieldElement.__new__(RealIntervalFieldElement, ZZ) 

Traceback (most recent call last): 

... 

TypeError: Cannot convert sage.rings.integer_ring.IntegerRing_class to sage.rings.real_mpfi.RealIntervalField_class 

sage: RealIntervalFieldElement.__new__(RealIntervalFieldElement, RIF) 

[.. NaN ..] 

  

Equality ``x == x`` does not hold unless ``x`` is exact:: 

  

sage: x = RIF(4.5, 4.6) 

sage: TestSuite(x).run(skip=["_test_eq", "_test_pickling"]) 

""" 

cdef RealIntervalField_class p = <RealIntervalField_class?>parent 

mpfi_init2(self.value, p.__prec) 

self._parent = p 

  

def __init__(self, parent, x, int base=10): 

""" 

Initialize a real interval element. Should be called by first 

creating a :class:`RealIntervalField`, as illustrated in the 

examples. 

  

INPUT: 

  

- ``x`` -- a number, string, or 2-tuple 

  

- ``base`` -- integer (default: 10) - only used if ``x`` is a string 

  

EXAMPLES:: 

  

sage: R = RealIntervalField() 

sage: R('1.2456') 

1.245600000000000? 

sage: R = RealIntervalField(3) 

sage: R('1.2456').str(style='brackets') 

'[1.0 .. 1.3]' 

  

:: 

  

sage: RIF = RealIntervalField(53) 

sage: RIF(RR.pi()) 

3.1415926535897932? 

sage: RIF(RDF.pi()) 

3.1415926535897932? 

sage: RIF(math.pi) 

3.1415926535897932? 

sage: RIF.pi() 

3.141592653589794? 

  

Rounding:: 

  

sage: w = RealIntervalField(3)(5/2) 

sage: RealIntervalField(2)(w).str(2, style='brackets') 

'[10. .. 11.]' 

  

TESTS:: 

  

sage: a = RealIntervalField(428)(factorial(100)/exp(2)); a 

1.26303298005073195998439505058085204028142920134742241494671502106333548593576383141666758300089860337889002385197008191910406895?e157 

sage: a.diameter() 

4.7046373946079775711568954992429894854882556641460240333441655212438503516287848720594584761250430179569094634219773739322602945e-129 

  

Type: ``RealIntervalField?`` for many more examples. 

""" 

if x is None: 

mpfi_set_ui(self.value, 0) 

else: 

mpfi_set_sage(self.value, NULL, x, parent, base) 

  

def __reduce__(self): 

""" 

Pickling support. 

  

EXAMPLES:: 

  

sage: a = RIF(5,5.5) 

sage: loads(dumps(a)).lexico_cmp(a) 

0 

sage: R = RealIntervalField(sci_not=1, prec=200) 

sage: b = R('393.39203845902384098234098230948209384028340') 

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

0 

sage: b = R(1)/R(0); b # R(0) has no particular sign, thus 1/R(0) covers the whole reals 

[-infinity .. +infinity] 

sage: c = loads(dumps(b)) 

sage: (c.lower(), c.upper()) == (b.lower(), b.upper()) 

True 

sage: b = R(-1)/R(0); b # same as above 

[-infinity .. +infinity] 

sage: c = loads(dumps(b)) 

sage: (c.lower(), c.upper()) == (b.lower(), b.upper()) 

True 

sage: b = R('[2 .. 3]'); b.str(error_digits=1) 

'2.5?5e0' 

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

0 

sage: R = RealIntervalField(4000) 

sage: s = 1/R(3) 

sage: t = loads(dumps(s)) 

sage: (t.upper(), t.lower()) == (s.upper(), s.lower()) 

True 

sage: loads(dumps(1/RIF(0,1))) 

[1.0000000000000000 .. +infinity] 

""" 

return (__create__RealIntervalFieldElement_version1, (self._parent, self.upper(), self.lower())) 

  

def __dealloc__(self): 

""" 

Deallocate ``self``. 

  

EXAMPLES:: 

  

sage: R = RealIntervalField() 

sage: del R # indirect doctest 

""" 

if self._parent is not None: 

mpfi_clear(self.value) 

  

def __repr__(self): 

""" 

Return a string representation of ``self``. 

  

EXAMPLES:: 

  

sage: R = RealIntervalField() 

sage: R(1.2456) # indirect doctest 

1.2456000000000001? 

""" 

return self.str(10) 

  

def _latex_(self): 

""" 

Return a latex representation of ``self``. 

  

EXAMPLES:: 

  

sage: latex(RIF(1.5)) # indirect doctest 

1.5000000000000000? 

sage: latex(RIF(3e200)) # indirect doctest 

3.0000000000000000? \times 10^{200} 

""" 

import re 

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

  

def _magma_init_(self, magma): 

r""" 

Return a string representation of ``self`` in the Magma language. 

  

EXAMPLES:: 

  

sage: t = RIF(10, 10.5); t 

11.? 

sage: magma(t) # optional - magma # indirect doctest 

10.2500000000000 

""" 

return "%s!%s" % (self.parent()._magma_init_(magma), self.center()) 

  

def _interface_init_(self, I=None): 

""" 

Raise a ``TypeError``. 

  

This function would return the string representation of ``self`` that 

makes sense as a default representation of a real interval in other 

computer algebra systems. But, most other computer algebra systems 

do not support interval arithmetic, so instead we just raise a 

``TypeError``. 

  

Define the appropriate ``_cas_init_`` function if there is a 

computer algebra system you would like to support. 

  

EXAMPLES:: 

  

sage: n = RIF(1.3939494594) 

sage: n._interface_init_() 

Traceback (most recent call last): 

... 

TypeError 

  

Here's a conversion to Maxima happens, which results in a type 

error:: 

  

sage: a = RealInterval('2.3') 

sage: maxima(a) 

Traceback (most recent call last): 

... 

TypeError 

""" 

raise TypeError 

  

  

def _sage_input_(self, sib, coerce): 

r""" 

Produce an expression which will reproduce this value when evaluated. 

  

EXAMPLES:: 

  

sage: sage_input(RIF(e, pi), verify=True) 

# Verified 

RIF(RR(2.7182818284590451), RR(3.1415926535897936)) 

sage: sage_input(RealIntervalField(64)(sqrt(2)), preparse=False, verify=True) 

# Verified 

RR64 = RealField(64) 

RealIntervalField(64)(RR64('1.41421356237309504876'), RR64('1.41421356237309504887')) 

sage: sage_input(RealIntervalField(2)(12), verify=True) 

# Verified 

RealIntervalField(2)(RealField(2)(12.)) 

sage: sage_input(RealIntervalField(2)(13), verify=True) 

# Verified 

RR2 = RealField(2) 

RealIntervalField(2)(RR2(12.), RR2(16.)) 

sage: from sage.misc.sage_input import SageInputBuilder 

sage: sib = SageInputBuilder() 

sage: RIF(-sqrt(3), -sqrt(2))._sage_input_(sib, False) 

{call: {atomic:RIF}({unop:- {call: {atomic:RR}({atomic:1.7320508075688774})}}, {unop:- {call: {atomic:RR}({atomic:1.4142135623730949})}})} 

""" 

# Interval printing could often be much prettier, 

# but I'm feeling lazy :) 

if self.is_exact(): 

return sib(self.parent())(sib(self.lower(rnd='RNDN'))) 

else: 

# The following line would also be correct, but even though it 

# uses coerced=2, that doesn't help because RealNumber doesn't 

# print pretty for directed-rounding fields. 

#return sib(self.parent())(sib(self.lower(), 2), sib(self.upper(), 2)) 

return sib(self.parent())(sib(self.lower(rnd='RNDN')), sib(self.upper(rnd='RNDN'))) 

  

def __hash__(self): 

""" 

Return a hash value of ``self``. 

  

EXAMPLES:: 

  

sage: hash(RIF(e)) == hash(RIF(e)) # indirect doctest 

True 

""" 

return hash(self.str(16)) 

  

def _im_gens_(self, codomain, im_gens): 

""" 

Return the image of ``self`` under the homomorphism from the rational 

field to ``codomain``. 

  

This always just returns ``self`` coerced into the ``codomain``. 

  

EXAMPLES:: 

  

sage: RIF(2.1)._im_gens_(CIF, [CIF(1)]) 

2.1000000000000001? 

sage: R = RealIntervalField(20) 

sage: RIF(2.1)._im_gens_(R, [R(1)]) 

2.10000? 

""" 

return codomain(self) # since 1 |--> 1 

  

def real(self): 

""" 

Return the real part of this real interval. 

  

(Since this interval is real, this simply returns itself.) 

  

.. SEEALSO:: 

  

:meth:`imag` 

  

EXAMPLES:: 

  

sage: RIF(1.2465).real() == RIF(1.2465) 

True 

""" 

return self 

  

def imag(self): 

r""" 

Return the imaginary part of this real interval. 

  

(Since this is interval is real, this simply returns the zero interval.) 

  

.. SEEALSO:: 

  

:meth:`real` 

  

EXAMPLES:: 

  

sage: RIF(2,3).imag() 

0 

""" 

return self._parent.zero() 

  

# MPFR had an elaborate "truncation" scheme to avoid printing 

# inaccurate-looking results; this has been removed for MPFI, 

# because I think it's less confusing to get such results than to 

# see [0.333 .. 0.333] for an interval with unequal left and right 

# sides. 

def str(self, int base=10, style=None, no_sci=None, e=None, error_digits=None): 

r""" 

Return a string representation of ``self``. 

  

INPUT: 

  

- ``base`` -- base for output 

  

- ``style`` -- The printing style; either ``'brackets'`` or 

``'question'`` (or ``None``, to use the current default). 

  

- ``no_sci`` -- if ``True`` do not print using scientific 

notation; if ``False`` print with scientific notation; if ``None`` 

(the default), print how the parent prints. 

  

- ``e`` -- symbol used in scientific notation 

  

- ``error_digits`` -- The number of digits of error to 

print, in ``'question'`` style. 

  

We support two different styles of printing; ``'question'`` style and 

``'brackets'`` style. In question style (the default), we print the 

"known correct" part of the number, followed by a question mark:: 

  

sage: RIF(pi).str() 

'3.141592653589794?' 

sage: RIF(pi, 22/7).str() 

'3.142?' 

sage: RIF(pi, 22/7).str(style='question') 

'3.142?' 

  

However, if the interval is precisely equal to some integer that's 

not too large, we just return that integer:: 

  

sage: RIF(-42).str() 

'-42' 

sage: RIF(0).str() 

'0' 

sage: RIF(12^5).str(base=3) 

'110122100000' 

  

Very large integers, however, revert to the normal question-style 

printing:: 

  

sage: RIF(3^7).str() 

'2187' 

sage: RIF(3^7 * 2^256).str() 

'2.5323729916201052?e80' 

  

In brackets style, we print the lower and upper bounds of the 

interval within brackets:: 

  

sage: RIF(237/16).str(style='brackets') 

'[14.812500000000000 .. 14.812500000000000]' 

  

Note that the lower bound is rounded down, and the upper bound is 

rounded up. So even if the lower and upper bounds are equal, they 

may print differently. (This is done so that the printed 

representation of the interval contains all the numbers in the 

internal binary interval.) 

  

For instance, we find the best 10-bit floating point representation 

of ``1/3``:: 

  

sage: RR10 = RealField(10) 

sage: RR(RR10(1/3)) 

0.333496093750000 

  

And we see that the point interval containing only this 

floating-point number prints as a wider decimal interval, that does 

contain the number:: 

  

sage: RIF10 = RealIntervalField(10) 

sage: RIF10(RR10(1/3)).str(style='brackets') 

'[0.33349 .. 0.33350]' 

  

We always use brackets style for ``NaN`` and infinities:: 

  

sage: RIF(pi, infinity) 

[3.1415926535897931 .. +infinity] 

sage: RIF(NaN) 

[.. NaN ..] 

  

Let's take a closer, formal look at the question style. In its full 

generality, a number printed in the question style looks like: 

  

MANTISSA ?ERROR eEXPONENT 

  

(without the spaces). The "eEXPONENT" part is optional; if it is 

missing, then the exponent is 0. (If the base is greater than 10, 

then the exponent separator is "@" instead of "e".) 

  

The "ERROR" is optional; if it is missing, then the error is 1. 

  

The mantissa is printed in base `b`, and always contains a 

decimal point (also known as a radix point, in bases other than 

10). (The error and exponent are always printed in base 10.) 

  

We define the "precision" of a floating-point printed 

representation to be the positional value of the last digit of the 

mantissa. For instance, in ``2.7?e5``, the precision is `10^4`; 

in ``8.?``, the precision is `10^0`; and in ``9.35?`` the precision 

is `10^{-2}`. This precision will always be `10^k` 

for some `k` (or, for an arbitrary base `b`, `b^k`). 

  

Then the interval is contained in the interval: 

  

.. MATH:: 

  

\text{mantissa} \cdot b^{\text{exponent}} - \text{error} \cdot b^k 

.. \text{mantissa} \cdot b^{\text{exponent}} + \text{error} \cdot 

b^k 

  

To control the printing, we can specify a maximum number of error 

digits. The default is 0, which means that we do not print an error 

at all (so that the error is always the default, 1). 

  

Now, consider the precisions needed to represent the endpoints 

(this is the precision that would be produced by 

``v.lower().str(no_sci=False)``). Our 

result is no more precise than the less precise endpoint, and is 

sufficiently imprecise that the error can be represented with the 

given number of decimal digits. Our result is the most precise 

possible result, given these restrictions. When there are two 

possible results of equal precision and with the same error width, 

then we pick the one which is farther from zero. (For instance, 

``RIF(0, 123)`` with two error digits could print as ``61.?62`` or 

``62.?62``. We prefer the latter because it makes it clear that the 

interval is known not to be negative.) 

  

EXAMPLES:: 

  

sage: a = RIF(59/27); a 

2.185185185185186? 

sage: a.str() 

'2.185185185185186?' 

sage: a.str(style='brackets') 

'[2.1851851851851851 .. 2.1851851851851856]' 

sage: a.str(16) 

'2.2f684bda12f69?' 

sage: a.str(no_sci=False) 

'2.185185185185186?e0' 

sage: pi_appr = RIF(pi, 22/7) 

sage: pi_appr.str(style='brackets') 

'[3.1415926535897931 .. 3.1428571428571433]' 

sage: pi_appr.str() 

'3.142?' 

sage: pi_appr.str(error_digits=1) 

'3.1422?7' 

sage: pi_appr.str(error_digits=2) 

'3.14223?64' 

sage: pi_appr.str(base=36) 

'3.6?' 

sage: RIF(NaN) 

[.. NaN ..] 

sage: RIF(pi, infinity) 

[3.1415926535897931 .. +infinity] 

sage: RIF(-infinity, pi) 

[-infinity .. 3.1415926535897936] 

sage: RealIntervalField(210)(3).sqrt() 

1.732050807568877293527446341505872366942805253810380628055806980? 

sage: RealIntervalField(210)(RIF(3).sqrt()) 

1.732050807568878? 

sage: RIF(3).sqrt() 

1.732050807568878? 

sage: RIF(0, 3^-150) 

1.?e-71 

  

TESTS: 

  

Check that :trac:`13634` is fixed:: 

  

sage: RIF(0.025) 

0.025000000000000002? 

sage: RIF.scientific_notation(True) 

sage: RIF(0.025) 

2.5000000000000002?e-2 

sage: RIF.scientific_notation(False) 

sage: RIF(0.025) 

0.025000000000000002? 

""" 

if base < 2 or base > 36: 

raise ValueError("the base (=%s) must be between 2 and 36" % base) 

  

# If self is a NaN, always use brackets style. 

if mpfi_nan_p(self.value): 

if base >= 24: 

return "[.. @NaN@ ..]" 

else: 

return "[.. NaN ..]" 

  

if e is None: 

if base > 10: 

e = '@' 

else: 

e = 'e' 

  

if style is None: 

style = printing_style 

if error_digits is None: 

error_digits = printing_error_digits 

  

if mpfr_inf_p(&self.value.left) or mpfr_inf_p(&self.value.right): 

style = 'brackets' 

  

if style == 'brackets': 

t1 = self.lower().str(base=base, no_sci=no_sci, e=e) 

t2 = self.upper().str(base=base, no_sci=no_sci, e=e) 

  

return "[%s .. %s]"%(t1, t2) 

  

elif style == 'question': 

if no_sci is None: 

prefer_sci = self.parent().scientific_notation() 

elif not no_sci: 

prefer_sci = True 

else: 

prefer_sci = False 

return self._str_question_style(base, error_digits, e, prefer_sci) 

  

else: 

raise ValueError('Illegal interval printing style %s' % printing_style) 

  

cpdef _str_question_style(self, int base, int error_digits, e, bint prefer_sci): 

r""" 

Compute the "question-style print representation" of this value, 

with the given base and error_digits. See the documentation for 

the str method for the definition of the question style. 

  

INPUT: 

  

- ``base`` - base for output 

  

- ``error_digits`` - maximum number of decimal digits for error 

  

- ``e`` - symbol for exponent (typically ``'e'`` for base 

less than or equal to 10, ``'@'`` for larger base) 

  

- ``prefer_sci`` - ``True`` to always print in scientific notation; 

``False`` to prefer non-scientific notation when 

possible 

  

EXAMPLES:: 

  

sage: v = RIF(e, pi) 

sage: v.str(2, style='brackets') 

'[10.101101111110000101010001011000101000101011101101001 .. 11.001001000011111101101010100010001000010110100011001]' 

sage: v._str_question_style(2, 0, 'e', False) 

'11.0?' 

sage: v._str_question_style(2, 3, '@', False) 

'10.111011100001?867' 

sage: v._str_question_style(2, 30, 'e', False) 

'10.111011100001000001011101111101011000100001001000001?476605618580184' 

sage: v.str(5, style='brackets') 

'[2.32434303404423034041024 .. 3.03232214303343241124132]' 

sage: v._str_question_style(5, 3, '@', False) 

'2.43111?662' 

sage: (Integer('232434', 5) + 2*662).str(5) 

'303233' 

sage: v.str(style='brackets') 

'[2.7182818284590450 .. 3.1415926535897936]' 

sage: v._str_question_style(10, 3, '@', False) 

'2.930?212' 

sage: v._str_question_style(10, 3, '@', True) 

'2.930?212@0' 

sage: v.str(16, style='brackets') 

'[2.b7e151628aed2 .. 3.243f6a8885a32]' 

sage: v._str_question_style(16, 3, '@', False) 

'2.ee1?867' 

sage: (Integer('2b7e', 16) + 2*867).str(16) 

'3244' 

sage: v.str(36, style='brackets') 

'[2.puw5nggjf8f .. 3.53i5ab8p5gz]' 

sage: v._str_question_style(36, 3, '@', False) 

'2.xh?275' 

sage: (Integer('2pu', 36) + 2*275).str(36) 

'354' 

  

TESTS:: 

  

sage: RIF(0, infinity)._str_question_style(10, 0, 'e', False) 

Traceback (most recent call last): 

... 

ValueError: _str_question_style on NaN or infinity 

sage: for i in range(1, 9): 

....: print(RIF(-10^i, 12345).str(style='brackets')) 

....: print(RIF(-10^i, 12345)._str_question_style(10, 0, 'e', False)) 

....: print(RIF(-10^i, 12345)._str_question_style(10, 3, 'e', False)) 

[-10.000000000000000 .. 12345.000000000000] 

0.?e5 

6.17?618e3 

[-100.00000000000000 .. 12345.000000000000] 

0.?e5 

6.13?623e3 

[-1000.0000000000000 .. 12345.000000000000] 

0.?e5 

5.68?668e3 

[-10000.000000000000 .. 12345.000000000000] 

0.?e5 

1.2?112e3 

[-100000.00000000000 .. 12345.000000000000] 

0.?e5 

-4.38?562e4 

[-1.0000000000000000e6 .. 12345.000000000000] 

0.?e6 

-4.94?507e5 

[-1.0000000000000000e7 .. 12345.000000000000] 

0.?e7 

-4.99?501e6 

[-1.0000000000000000e8 .. 12345.000000000000] 

0.?e8 

-5.00?501e7 

sage: RIF(10^-3, 12345)._str_question_style(10, 3, 'e', False) 

'6.18?618e3' 

sage: RIF(-golden_ratio, -10^-6)._str_question_style(10, 3, 'e', False) 

'-0.810?810' 

sage: RIF(-0.85, 0.85)._str_question_style(10, 0, 'e', False) 

'0.?' 

sage: RIF(-0.85, 0.85)._str_question_style(10, 1, 'e', False) 

'0.0?9' 

sage: RIF(-0.85, 0.85)._str_question_style(10, 2, 'e', False) 

'0.00?85' 

sage: RIF(-8.5, 8.5)._str_question_style(10, 0, 'e', False) 

'0.?e1' 

sage: RIF(-85, 85)._str_question_style(10, 0, 'e', False) 

'0.?e2' 

sage: for i in range(-6, 7): 

....: v = RIF(2).sqrt() * 10^i 

....: print(v._str_question_style(10, 0, 'e', False)) 

1.414213562373095?e-6 

0.00001414213562373095? 

0.0001414213562373095? 

0.001414213562373095? 

0.01414213562373095? 

0.1414213562373095? 

1.414213562373095? 

14.14213562373095? 

141.4213562373095? 

1414.213562373095? 

14142.13562373095? 

141421.3562373095? 

1.414213562373095?e6 

sage: RIF(3^33) 

5559060566555523 

sage: RIF(3^33 * 2) 

1.1118121133111046?e16 

sage: RIF(-pi^-512, 0) 

-1.?e-254 

sage: RealIntervalField(2)(3 * 2^18) 

786432 

sage: RealIntervalField(2)(3 * 2^19) 

1.6?e6 

sage: v = RIF(AA(2-sqrt(3))) 

sage: v 

0.2679491924311227? 

sage: v.str(error_digits=1) 

'0.26794919243112273?4' 

sage: v.str(style='brackets') 

'[0.26794919243112269 .. 0.26794919243112276]' 

sage: -v 

-0.2679491924311227? 

  

Check that :trac:`15166` is fixed:: 

  

sage: RIF(1.84e13).exp() 

[2.0985787164673874e323228496 .. +infinity] # 32-bit 

6.817557048799520?e7991018467019 # 64-bit 

sage: from sage.rings.real_mpfr import mpfr_get_exp_min, mpfr_get_exp_max 

sage: v = RIF(1.0 << (mpfr_get_exp_max() - 1)); v 

1.0492893582336939?e323228496 # 32-bit 

2.9378268945557938?e1388255822130839282 # 64-bit 

sage: -v 

-1.0492893582336939?e323228496 # 32-bit 

-2.9378268945557938?e1388255822130839282 # 64-bit 

sage: v = RIF(1.0 >> -mpfr_get_exp_min()+1); v 

2.3825649048879511?e-323228497 # 32-bit 

8.5096913117408362?e-1388255822130839284 # 64-bit 

  

""" 

if not(mpfr_number_p(&self.value.left) and mpfr_number_p(&self.value.right)): 

raise ValueError("_str_question_style on NaN or infinity") 

if base < 2 or base > 36: 

raise ValueError("the base (=%s) must be between 2 and 36" % base) 

if error_digits < 0 or error_digits > 1000: 

# The restriction to 1000 is not essential. The reason to have 

# a restriction is that this code is not efficient for 

# large error_digits values (for instance, we always construct 

# a number with that many digits); a very large error_digits 

# could run out of memory, etc. 

# I think that 1000 is a pretty "safe" limit. The whole 

# purpose of question_style is to be human-readable, and 

# the human-readability will go way down after about 6 

# error digits; 1000 error digits is just silly. 

raise ValueError("error_digits (=%s) must be between 0 and 1000" % error_digits) 

  

cdef mp_exp_t self_exp 

cdef mpz_t self_zz 

cdef mpfr_prec_t prec = (<RealIntervalField_class>self._parent).__prec 

cdef char *zz_str 

cdef size_t zz_str_maxlen 

  

if mpfr_equal_p(&self.value.left, &self.value.right) \ 

and mpfr_integer_p(&self.value.left): 

# This might be suitable for integer printing, but not if it's 

# too big. (We can represent 2^3000000 exactly in RIF, but we 

# don't want to print this 903090 digit number; we'd rather 

# just print 9.7049196389007116?e903089 .) 

  

# Represent self as m*2^k, where m is an integer with 

# self.prec() bits and k is an integer. (So RIF(1) would have 

# m = 2^52 and k=-52.) Then, as a simple heuristic, we print 

# as an integer if k<=0. (As a special dispensation for tiny 

# precisions, we also print as an integer if the number is 

# less than a million (actually, less than 2^20); this 

# will never affect "normal" uses, but it makes tiny examples 

# with RealIntervalField(2) prettier.) 

  

self_exp = mpfr_get_exp(&self.value.left) 

if mpfr_zero_p(&self.value.left) or self_exp <= prec or self_exp <= 20: 

mpz_init(self_zz) 

mpfr_get_z(self_zz, &self.value.left, MPFR_RNDN) 

zz_str_maxlen = mpz_sizeinbase(self_zz, base) + 2 

zz_str = <char *>PyMem_Malloc(zz_str_maxlen) 

if zz_str == NULL: 

mpz_clear(self_zz) 

raise MemoryError("Unable to allocate memory for integer representation of interval") 

sig_on() 

mpz_get_str(zz_str, base, self_zz) 

sig_off() 

v = char_to_str(zz_str) 

PyMem_Free(zz_str) 

return v 

  

# We want the endpoints represented as an integer mantissa 

# and an exponent, using the given base. MPFR will do that for 

# us in mpfr_get_str, so we end up converting from MPFR to strings 

# to MPZ to strings... ouch. We could avoid this overhead by 

# copying most of the guts of mpfr_get_str to give something like 

# mpfr_get_mpz_exp (to give the mantissa as an mpz and the exponent); 

# we could also write the body of this method using the string 

# values, so that the second and third conversions are always 

# on small values (of around the size of error_digits). 

# We don't do any of that. 

  

cdef char *lower_s 

cdef char *upper_s 

cdef mp_exp_t lower_expo 

cdef mp_exp_t upper_expo 

cdef mpz_t lower_mpz 

cdef mpz_t upper_mpz 

  

sig_on() 

lower_s = mpfr_get_str(<char*>0, &lower_expo, base, 0, 

&self.value.left, MPFR_RNDD) 

upper_s = mpfr_get_str(<char*>0, &upper_expo, base, 0, 

&self.value.right, MPFR_RNDU) 

sig_off() 

  

if lower_s == <char*> 0: 

raise RuntimeError("unable to convert interval lower bound to a string") 

if upper_s == <char*> 0: 

raise RuntimeError("unable to convert interval upper bound to a string") 

  

# MPFR returns an exponent assuming that the implicit radix point 

# is to the left of the first mantissa digit. We'll be doing 

# arithmetic on mantissas that might not preserve the number of 

# digits; we adjust the exponent so that the radix point is to 

# the right of the last mantissa digit (that is, so the number 

# is mantissa*base^exponent, if you interpret mantissa as an integer). 

  

cdef long digits 

digits = strlen(lower_s) 

if lower_s[0] == '-': 

digits -= 1 

lower_expo -= digits 

  

digits = strlen(upper_s) 

if upper_s[0] == '-': 

digits -= 1 

upper_expo -= digits 

  

sig_on() 

mpz_init_set_str(lower_mpz, lower_s, base) 

mpz_init_set_str(upper_mpz, upper_s, base) 

mpfr_free_str(lower_s) 

mpfr_free_str(upper_s) 

sig_off() 

  

cdef mpz_t tmp 

mpz_init(tmp) 

  

# At several places in the function, we divide by a power of 

# base. This could be sped up by shifting instead, when base 

# is a power of 2. (I'm not bothering right now because I 

# expect the not-base-10 case to be quite rare, especially 

# when combined with high precision and question style.) 

  

# First we normalize so that both mantissas are at the same 

# precision. This just involves dividing the more-precise 

# endpoint by the appropriate power of base. However, there's 

# one potentially very slow case we want to avoid: if the two 

# exponents are sufficiently different, the simple code would 

# involve computing a huge power of base, and then dividing 

# by it to get -1, 0, or 1 (depending on the rounding). 

  

# There's one complication first: if one of the endpoints is zero, 

# we want to treat it as infinitely precise (otherwise, it defaults 

# to a precision of 2^-self.prec(), so that RIF(0, 2^-1000) 

# would print as 1.?e-17). (If both endpoints are zero, then 

# we can't get here; we already returned '0' in the integer 

# case above.) 

  

if mpfr_zero_p(&self.value.left): 

lower_expo = upper_expo 

if mpfr_zero_p(&self.value.right): 

upper_expo = lower_expo 

  

cdef mp_exp_t expo_delta 

  

if lower_expo < upper_expo: 

expo_delta = upper_expo - lower_expo 

if mpz_sizeinbase(lower_mpz, base) < expo_delta: 

# abs(lower) < base^expo_delta, so 

# floor(lower/base^expo_delta) is either -1 or 0 

# (depending on the sign of lower). 

if mpz_sgn(lower_mpz) < 0: 

mpz_set_si(lower_mpz, -1) 

else: 

mpz_set_ui(lower_mpz, 0) 

else: 

mpz_ui_pow_ui(tmp, base, expo_delta) 

mpz_fdiv_q(lower_mpz, lower_mpz, tmp) 

lower_expo = upper_expo 

elif upper_expo < lower_expo: 

expo_delta = lower_expo - upper_expo 

if mpz_sizeinbase(upper_mpz, base) < expo_delta: 

# abs(upper) < base^expo_delta, so 

# ceiling(upper/base^expo_delta) is either 0 or 1 

# (depending on the sign of upper). 

if mpz_sgn(upper_mpz) > 0: 

mpz_set_ui(upper_mpz, 1) 

else: 

mpz_set_ui(upper_mpz, 0) 

else: 

mpz_ui_pow_ui(tmp, base, expo_delta) 

mpz_cdiv_q(upper_mpz, upper_mpz, tmp) 

upper_expo = lower_expo 

  

cdef mp_exp_t expo = lower_expo 

  

# Now the basic plan is to repeat 

# lower = floor(lower/base); upper = ceiling(upper/base) 

# until the error upper-lower is sufficiently small. However, 

# if this loop executes many times, that's pretty inefficient 

# (quadratic). We do a single pre-check to see approximately 

# how many times we would have to go through that loop, 

# and do most of them with a single division. 

  

cdef mpz_t cur_error 

mpz_init(cur_error) 

  

mpz_sub(cur_error, upper_mpz, lower_mpz) 

  

cdef mpz_t max_error 

mpz_init(max_error) 

if error_digits == 0: 

mpz_set_ui(max_error, 2) 

else: 

mpz_ui_pow_ui(max_error, 10, error_digits) 

mpz_sub_ui(max_error, max_error, 1) 

mpz_mul_2exp(max_error, max_error, 1) 

  

# Now we want to compute k as large as possible such that 

# ceiling(upper/base^(k-1)) - floor(lower/base^(k-1)) > max_error. 

# We start by noting that 

# ceiling(upper/base^(k-1)) - floor(lower/base^(k-1)) >= 

# cur_error/base^(k-1), 

# so it suffices if 

# cur_error/base^(k-1) > max_error, or 

# cur_error/max_error > base^(k-1). 

  

# We would like to take logarithms and subtract, but taking 

# logarithms is expensive. Instead we use mpz_sizeinbase 

# as an approximate logarithm. (This could probably be 

# improved, either by assuming undocumented knowledge of 

# the internals of mpz_sizeinbase, or by writing our own 

# approximate logarithm.) 

  

cdef long cur_error_digits = mpz_sizeinbase(cur_error, base) 

cdef long max_error_digits = mpz_sizeinbase(max_error, base) 

  

# The GMP documentation claims that mpz_sizeinbase will be either 

# the true number of digits, or one too high. So 

# cur_error might have as few as cur_error_digits-1 digits, 

# so it might be as small as base^(cur_error_digits-2). 

# max_error might have as many as max_error_digits digits, so it 

# might be almost (but not quite) as large as base^max_error_digits. 

# Then their quotient will be at least slightly larger than 

# base^(cur_error_digits-2-max_error_digits). So we can take 

# k-1 = cur_error_digits-2-max_error_digits, and 

# k = cur_error_digits-1-max_error_digits. 

  

cdef long k = cur_error_digits - 1 - max_error_digits 

  

if k > 0: 

mpz_ui_pow_ui(tmp, base, k) 

mpz_fdiv_q(lower_mpz, lower_mpz, tmp) 

mpz_cdiv_q(upper_mpz, upper_mpz, tmp) 

expo += k 

mpz_sub(cur_error, upper_mpz, lower_mpz) 

  

# OK, we've almost divided enough times to fit within max_error. 

# (In fact, maybe we already have.) Now we just loop a few more 

# times until we're done. 

  

while mpz_cmp(cur_error, max_error) > 0: 

mpz_fdiv_q_ui(lower_mpz, lower_mpz, base) 

mpz_cdiv_q_ui(upper_mpz, upper_mpz, base) 

expo += 1 

mpz_sub(cur_error, upper_mpz, lower_mpz) 

  

# Almost done. Now we need to print out a floating-point number 

# with a mantissa halfway between lower_mpz and upper_mpz, 

# an error of half of cur_error (rounded up), and an exponent 

# based on expo (shifted by the location of the decimal point 

# within the mantissa). 

  

# We briefly re-purpose lower_mpz to hold the final mantissa: 

mpz_add(lower_mpz, lower_mpz, upper_mpz) 

# According to our spec, we're supposed to divide lower_mpz 

# by 2, rounding away from 0. 

if mpz_sgn(lower_mpz) >= 0: 

mpz_cdiv_q_2exp(lower_mpz, lower_mpz, 1) 

else: 

mpz_fdiv_q_2exp(lower_mpz, lower_mpz, 1) 

  

# and cur_error to hold the error: 

mpz_cdiv_q_2exp(cur_error, cur_error, 1) 

  

cdef char *tmp_cstr 

  

tmp_cstr = <char *>PyMem_Malloc(mpz_sizeinbase(lower_mpz, base) + 2) 

if tmp_cstr == NULL: 

raise MemoryError("Unable to allocate memory for the mantissa of an interval") 

mpz_get_str(tmp_cstr, base, lower_mpz) 

digits = strlen(tmp_cstr) 

if tmp_cstr[0] == '-': 

digits -= 1 

mant_string = bytes_to_str(tmp_cstr+1) 

sign_string = bytes_to_str(b'-') 

else: 

mant_string = bytes_to_str(tmp_cstr) 

sign_string = bytes_to_str(b'') 

PyMem_Free(tmp_cstr) 

  

if error_digits == 0: 

error_string = bytes_to_str(b'') 

else: 

tmp_cstr = <char *>PyMem_Malloc(mpz_sizeinbase(cur_error, 10) + 2) 

if tmp_cstr == NULL: 

raise MemoryError("Unable to allocate memory for the error of an interval") 

mpz_get_str(tmp_cstr, 10, cur_error) 

error_string = char_to_str(tmp_cstr) 

PyMem_Free(tmp_cstr) 

  

mpz_clear(lower_mpz) 

mpz_clear(upper_mpz) 

mpz_clear(tmp) 

mpz_clear(cur_error) 

mpz_clear(max_error) 

  

cdef bint scientific = prefer_sci 

  

# If the exponent is >0, we must use scientific notation. For 

# instance, RIF(10, 30) gives 2.?e1; we couldn't write that 

# number in the question syntax (with no error digits) without 

# scientific notation. 

if expo > 0: 

scientific = True 

  

# If we use scientific notation, we put the radix point to the 

# right of the first digit; that would give us an exponent of: 

cdef mp_exp_t sci_expo = expo + digits - 1 

if abs(sci_expo) >= 6: 

scientific = True 

  

if scientific: 

return '%s%s.%s?%s%s%s'%(sign_string, 

mant_string[0], mant_string[1:], 

error_string, e, sci_expo) 

  

if expo + digits <= 0: 

return '%s0.%s%s?%s'%(sign_string, 

'0' * -(expo + digits), mant_string, 

error_string) 

  

return '%s%s.%s?%s'%(sign_string, 

mant_string[:expo+digits], 

mant_string[expo+digits:], 

error_string) 

  

def __copy__(self): 

""" 

Return copy of ``self`` - since ``self`` is immutable, we just return 

``self`` again. 

  

EXAMPLES:: 

  

sage: a = RIF(3.5) 

sage: copy(a) is a 

True 

""" 

return self 

  

# Interval-specific functions 

def lower(self, rnd=None): 

""" 

Return the lower bound of this interval 

  

INPUT: 

  

- ``rnd`` -- the rounding mode (default: towards minus infinity, 

see :class:`sage.rings.real_mpfr.RealField` for possible values) 

  

The rounding mode does not affect the value returned as a 

floating-point number, but it does control which variety of 

``RealField`` the returned number is in, which affects printing and 

subsequent operations. 

  

EXAMPLES:: 

  

sage: R = RealIntervalField(13) 

sage: R.pi().lower().str() 

'3.1411' 

  

:: 

  

sage: x = R(1.2,1.3); x.str(style='brackets') 

'[1.1999 .. 1.3001]' 

sage: x.lower() 

1.19 

sage: x.lower('RNDU') 

1.20 

sage: x.lower('RNDN') 

1.20 

sage: x.lower('RNDZ') 

1.19 

sage: x.lower('RNDA') 

1.20 

sage: x.lower().parent() 

Real Field with 13 bits of precision and rounding RNDD 

sage: x.lower('RNDU').parent() 

Real Field with 13 bits of precision and rounding RNDU 

sage: x.lower('RNDA').parent() 

Real Field with 13 bits of precision and rounding RNDA 

sage: x.lower() == x.lower('RNDU') 

True 

""" 

cdef RealNumber x 

if rnd is None: 

x = (<RealIntervalField_class>self._parent).__lower_field._new() 

else: 

x = (<RealField_class>(self._parent._real_field(rnd)))._new() 

mpfi_get_left(x.value, self.value) 

return x 

  

def upper(self, rnd=None): 

""" 

Return the upper bound of ``self`` 

  

INPUT: 

  

- ``rnd`` -- the rounding mode (default: towards plus infinity, 

see :class:`sage.rings.real_mpfr.RealField` for possible values) 

  

The rounding mode does not affect the value returned as a 

floating-point number, but it does control which variety of 

``RealField`` the returned number is in, which affects printing and 

subsequent operations. 

  

EXAMPLES:: 

  

sage: R = RealIntervalField(13) 

sage: R.pi().upper().str() 

'3.1417' 

  

:: 

  

sage: R = RealIntervalField(13) 

sage: x = R(1.2,1.3); x.str(style='brackets') 

'[1.1999 .. 1.3001]' 

sage: x.upper() 

1.31 

sage: x.upper('RNDU') 

1.31 

sage: x.upper('RNDN') 

1.30 

sage: x.upper('RNDD') 

1.30 

sage: x.upper('RNDZ') 

1.30 

sage: x.upper('RNDA') 

1.31 

sage: x.upper().parent() 

Real Field with 13 bits of precision and rounding RNDU 

sage: x.upper('RNDD').parent() 

Real Field with 13 bits of precision and rounding RNDD 

sage: x.upper() == x.upper('RNDD') 

True 

""" 

cdef RealNumber x 

if rnd is None: 

x = (<RealIntervalField_class>self._parent).__upper_field._new() 

else: 

x = ((<RealField_class>self._parent._real_field(rnd)))._new() 

mpfi_get_right(x.value, self.value) 

return x 

  

def endpoints(self, rnd=None): 

""" 

Return the lower and upper endpoints of this interval. 

  

OUTPUT: a 2-tuple of real numbers 

(lower endpoint, upper endpoint) 

  

.. SEEALSO:: 

  

:meth:`edges` which returns the endpoints as exact 

intervals instead of real numbers. 

  

EXAMPLES:: 

  

sage: RIF(1,2).endpoints() 

(1.00000000000000, 2.00000000000000) 

sage: RIF(pi).endpoints() 

(3.14159265358979, 3.14159265358980) 

sage: a = CIF(RIF(1,2), RIF(3,4)) 

sage: a.real().endpoints() 

(1.00000000000000, 2.00000000000000) 

  

As with ``lower()`` and ``upper()``, a rounding mode is accepted:: 

  

sage: RIF(1,2).endpoints('RNDD')[0].parent() 

Real Field with 53 bits of precision and rounding RNDD 

""" 

return self.lower(rnd), self.upper(rnd) 

  

def edges(self): 

""" 

Return the lower and upper endpoints of this interval as 

intervals. 

  

OUTPUT: a 2-tuple of real intervals 

(lower endpoint, upper endpoint) 

each containing just one point. 

  

.. SEEALSO:: 

  

:meth:`endpoints` which returns the endpoints as real 

numbers instead of intervals. 

  

EXAMPLES:: 

  

sage: RIF(1,2).edges() 

(1, 2) 

sage: RIF(pi).edges() 

(3.1415926535897932?, 3.1415926535897936?) 

""" 

left = self._new() 

right = self._new() 

cdef mpfr_t x 

mpfr_init2(x, self.prec()) 

mpfi_get_left(x, self.value) 

mpfi_set_fr(left.value, x) 

mpfi_get_right(x, self.value) 

mpfi_set_fr(right.value, x) 

mpfr_clear(x) 

return (left, right) 

  

def absolute_diameter(self): 

""" 

The diameter of this interval (for `[a .. b]`, this is `b-a`), rounded 

upward, as a :class:`RealNumber`. 

  

EXAMPLES:: 

  

sage: RIF(1, pi).absolute_diameter() 

2.14159265358979 

""" 

cdef RealNumber x 

x = (<RealIntervalField_class>self._parent).__middle_field._new() 

mpfi_diam_abs(x.value, self.value) 

return x 

  

def relative_diameter(self): 

""" 

The relative diameter of this interval (for `[a .. b]`, this is 

`(b-a)/((a+b)/2)`), rounded upward, as a :class:`RealNumber`. 

  

EXAMPLES:: 

  

sage: RIF(1, pi).relative_diameter() 

1.03418797197910 

""" 

cdef RealNumber x 

x = (<RealIntervalField_class>self._parent).__middle_field._new() 

mpfi_diam_rel(x.value, self.value) 

return x 

  

def diameter(self): 

""" 

If 0 is in ``self``, then return :meth:`absolute_diameter()`, 

otherwise return :meth:`relative_diameter()`. 

  

EXAMPLES:: 

  

sage: RIF(1, 2).diameter() 

0.666666666666667 

sage: RIF(1, 2).absolute_diameter() 

1.00000000000000 

sage: RIF(1, 2).relative_diameter() 

0.666666666666667 

sage: RIF(pi).diameter() 

1.41357985842823e-16 

sage: RIF(pi).absolute_diameter() 

4.44089209850063e-16 

sage: RIF(pi).relative_diameter() 

1.41357985842823e-16 

sage: (RIF(pi) - RIF(3, 22/7)).diameter() 

0.142857142857144 

sage: (RIF(pi) - RIF(3, 22/7)).absolute_diameter() 

0.142857142857144 

sage: (RIF(pi) - RIF(3, 22/7)).relative_diameter() 

2.03604377705518 

""" 

cdef RealNumber x 

x = (<RealIntervalField_class>self._parent).__middle_field._new() 

mpfi_diam(x.value, self.value) 

return x 

  

def fp_rank_diameter(self): 

r""" 

Computes the diameter of this interval in terms of the 

"floating-point rank". 

  

The floating-point rank is the number of floating-point numbers (of 

the current precision) contained in the given interval, minus one. An 

``fp_rank_diameter`` of 0 means that the interval is exact; an 

``fp_rank_diameter`` of 1 means that the interval is 

as tight as possible, unless the number you're trying to represent 

is actually exactly representable as a floating-point number. 

  

EXAMPLES:: 

  

sage: RIF(pi).fp_rank_diameter() 

1 

sage: RIF(12345).fp_rank_diameter() 

0 

sage: RIF(-sqrt(2)).fp_rank_diameter() 

1 

sage: RIF(5/8).fp_rank_diameter() 

0 

sage: RIF(5/7).fp_rank_diameter() 

1 

sage: a = RIF(pi)^12345; a 

2.06622879260?e6137 

sage: a.fp_rank_diameter() 

30524 

sage: (RIF(sqrt(2)) - RIF(sqrt(2))).fp_rank_diameter() 

9671406088542672151117826 # 32-bit 

41538374868278620559869609387229186 # 64-bit 

  

Just because we have the best possible interval, doesn't mean the 

interval is actually small:: 

  

sage: a = RIF(pi)^12345678901234567890; a 

[2.0985787164673874e323228496 .. +infinity] # 32-bit 

[5.8756537891115869e1388255822130839282 .. +infinity] # 64-bit 

sage: a.fp_rank_diameter() 

1 

""" 

return self.lower().fp_rank_delta(self.upper()) 

  

def is_exact(self): 

""" 

Return whether this real interval is exact (i.e. contains exactly 

one real value). 

  

EXAMPLES:: 

  

sage: RIF(3).is_exact() 

True 

sage: RIF(2*pi).is_exact() 

False 

""" 

return mpfr_equal_p(&self.value.left, &self.value.right) 

  

def magnitude(self): 

""" 

The largest absolute value of the elements of the interval. 

  

OUTPUT: a real number with rounding mode ``RNDU`` 

  

EXAMPLES:: 

  

sage: RIF(-2, 1).magnitude() 

2.00000000000000 

sage: RIF(-1, 2).magnitude() 

2.00000000000000 

sage: parent(RIF(1).magnitude()) 

Real Field with 53 bits of precision and rounding RNDU 

""" 

cdef RealNumber x 

x = (<RealIntervalField_class>self._parent).__upper_field._new() 

mpfi_mag(x.value, self.value) 

return x 

  

def mignitude(self): 

""" 

The smallest absolute value of the elements of the interval. 

  

OUTPUT: a real number with rounding mode ``RNDD`` 

  

EXAMPLES:: 

  

sage: RIF(-2, 1).mignitude() 

0.000000000000000 

sage: RIF(-2, -1).mignitude() 

1.00000000000000 

sage: RIF(3, 4).mignitude() 

3.00000000000000 

sage: parent(RIF(1).mignitude()) 

Real Field with 53 bits of precision and rounding RNDD 

""" 

cdef RealNumber x 

x = (<RealIntervalField_class>self._parent).__lower_field._new() 

mpfi_mig(x.value, self.value) 

return x 

  

def center(self): 

""" 

Compute the center of the interval `[a .. b]` which is `(a+b) / 2`. 

  

EXAMPLES:: 

  

sage: RIF(1, 2).center() 

1.50000000000000 

""" 

cdef RealNumber x 

x = (<RealIntervalField_class>self._parent).__middle_field._new() 

mpfi_mid(x.value, self.value) 

return x 

  

def bisection(self): 

""" 

Returns the bisection of ``self`` into two intervals of half the size 

whose union is ``self`` and intersection is :meth:`center()`. 

  

EXAMPLES:: 

  

sage: a, b = RIF(1,2).bisection() 

sage: a.lower(), a.upper() 

(1.00000000000000, 1.50000000000000) 

sage: b.lower(), b.upper() 

(1.50000000000000, 2.00000000000000) 

  

sage: I = RIF(e, pi) 

sage: a, b = I.bisection() 

sage: a.intersection(b) == I.center() 

True 

sage: a.union(b).endpoints() == I.endpoints() 

True 

""" 

left = self._new() 

right = self._new() 

mpfr_set(&left.value.left, &self.value.left, MPFR_RNDN) 

mpfi_mid(&left.value.right, self.value) 

mpfi_interv_fr(right.value, &left.value.right, &self.value.right) 

return left, right 

  

def alea(self): 

""" 

Return a floating-point number picked at random from the interval. 

  

EXAMPLES:: 

  

sage: RIF(1, 2).alea() # random 

1.34696133696137 

""" 

cdef RealNumber x 

x = (<RealIntervalField_class>self._parent).__middle_field._new() 

mpfi_alea(x.value, self.value) 

return x 

  

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

# Basic Arithmetic 

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

  

cpdef _add_(self, other): 

""" 

Add two real intervals with the same parent. 

  

EXAMPLES:: 

  

sage: R = RealIntervalField() 

sage: R(-1.5) + R(2.5) # indirect doctest 

1 

sage: R('-1.3') + R('2.3') 

1.000000000000000? 

sage: (R(1, 2) + R(3, 4)).str(style='brackets') 

'[4.0000000000000000 .. 6.0000000000000000]' 

""" 

x = self._new() 

mpfi_add(x.value, self.value, (<RealIntervalFieldElement>other).value) 

return x 

  

def __invert__(self): 

""" 

Return the multiplicative "inverse" of this interval. (Technically, 

non-precise intervals don't have multiplicative inverses.) 

  

EXAMPLES:: 

  

sage: v = RIF(2); v 

2 

sage: ~v 

0.50000000000000000? 

sage: v * ~v 

1 

sage: v = RIF(1.5, 2.5); v.str(style='brackets') 

'[1.5000000000000000 .. 2.5000000000000000]' 

sage: (~v).str(style='brackets') 

'[0.39999999999999996 .. 0.66666666666666675]' 

sage: (v * ~v).str(style='brackets') 

'[0.59999999999999986 .. 1.6666666666666670]' 

sage: ~RIF(-1, 1) 

[-infinity .. +infinity] 

""" 

x = self._new() 

mpfi_inv(x.value, self.value) 

return x 

  

cpdef _sub_(self, right): 

""" 

Subtract two real intervals with the same parent. 

  

EXAMPLES:: 

  

sage: R = RealIntervalField() 

sage: R(-1.5) - R(2.5) # indirect doctest 

-4 

sage: R('-1.3') - R('2.7') 

-4.000000000000000? 

sage: (R(1, 2) - R(3, 4)).str(style='brackets') 

'[-3.0000000000000000 .. -1.0000000000000000]' 

""" 

x = self._new() 

mpfi_sub(x.value, self.value, (<RealIntervalFieldElement>right).value) 

return x 

  

cpdef _mul_(self, right): 

""" 

Multiply two real intervals with the same parent. 

  

EXAMPLES:: 

  

sage: R = RealIntervalField() 

sage: R(-1.5) * R(2.5) # indirect doctest 

-3.7500000000000000? 

sage: R('-1.3') * R('2.3') 

-2.990000000000000? 

sage: (R(1, 2) * R(3, 4)).str(style='brackets') 

'[3.0000000000000000 .. 8.0000000000000000]' 

  

If two elements have different precision, arithmetic operations are 

performed after coercing to the lower precision:: 

  

sage: R20 = RealIntervalField(20) 

sage: R100 = RealIntervalField(100) 

sage: a = R20('393.3902834028345') 

sage: b = R100('393.3902834028345') 

sage: a 

393.391? 

sage: b 

393.390283402834500000000000000? 

sage: a*b 

154756.? 

sage: b*a 

154756.? 

sage: parent(b*a) 

Real Interval Field with 20 bits of precision 

""" 

x = self._new() 

mpfi_mul(x.value, self.value, (<RealIntervalFieldElement>right).value) 

return x 

  

  

cpdef _div_(self, right): 

""" 

Divide ``self`` by ``right``, where both are real intervals with the 

same parent. 

  

EXAMPLES:: 

  

sage: R = RealIntervalField() 

sage: R(1)/R(3) # indirect doctest 

0.3333333333333334? 

sage: R(1)/R(0) # since R(0) has no sign, gives the whole reals 

[-infinity .. +infinity] 

sage: R(1)/R(-1, 1) 

[-infinity .. +infinity] 

  

:: 

  

sage: R(-1.5) / R(2.5) 

-0.6000000000000000? 

sage: (R(1, 2) / R(3, 4)).str(style='brackets') 

'[0.25000000000000000 .. 0.66666666666666675]' 

""" 

x = self._new() 

mpfi_div(x.value, self.value, 

(<RealIntervalFieldElement>right).value) 

return x 

  

cpdef _neg_(self): 

""" 

Return the additive "inverse" of this interval. (Technically, 

non-precise intervals don't have additive inverses.) 

  

EXAMPLES:: 

  

sage: v = RIF(2); v 

2 

sage: -v # indirect doctest 

-2 

sage: v + -v 

0 

sage: v = RIF(1.5, 2.5); v.str(error_digits=3) 

'2.000?500' 

sage: (-v).str(style='brackets') 

'[-2.5000000000000000 .. -1.5000000000000000]' 

sage: (v + -v).str(style='brackets') 

'[-1.0000000000000000 .. 1.0000000000000000]' 

""" 

x = self._new() 

mpfi_neg(x.value, self.value) 

return x 

  

def __abs__(self): 

""" 

Return the absolute value of ``self``. 

  

EXAMPLES:: 

  

sage: RIF(2).__abs__() 

2 

sage: RIF(2.1).__abs__() 

2.1000000000000001? 

sage: RIF(-2.1).__abs__() 

2.1000000000000001? 

""" 

return self.abs() 

  

cdef RealIntervalFieldElement abs(self): 

""" 

Return the absolute value of ``self``. 

  

EXAMPLES:: 

  

sage: RIF(2).abs() 

2 

sage: RIF(2.1).abs() 

2.1000000000000001? 

sage: RIF(-2.1).abs() 

2.1000000000000001? 

""" 

x = self._new() 

mpfi_abs(x.value, self.value) 

return x 

  

def square(self): 

""" 

Return the square of ``self``. 

  

.. NOTE:: 

  

Squaring an interval is different than multiplying it by itself, 

because the square can never be negative. 

  

EXAMPLES:: 

  

sage: RIF(1, 2).square().str(style='brackets') 

'[1.0000000000000000 .. 4.0000000000000000]' 

sage: RIF(-1, 1).square().str(style='brackets') 

'[0.00000000000000000 .. 1.0000000000000000]' 

sage: (RIF(-1, 1) * RIF(-1, 1)).str(style='brackets') 

'[-1.0000000000000000 .. 1.0000000000000000]' 

""" 

x = self._new() 

mpfi_sqr(x.value, self.value) 

return x 

  

# Bit shifting 

def _lshift_(self, n): 

""" 

Return ``self*(2^n)`` for an integer ``n``. 

  

EXAMPLES:: 

  

sage: RIF(1.0)._lshift_(32) 

4294967296 

sage: RIF(1.5)._lshift_(2) 

6 

""" 

if n > sys.maxsize: 

raise OverflowError("n (=%s) must be <= %s" % (n, sys.maxsize)) 

x = self._new() 

mpfi_mul_2exp(x.value, self.value, n) 

return x 

  

def __lshift__(x, y): 

""" 

Returns `x * 2^y`, for `y` an integer. Much faster 

than an ordinary multiplication. 

  

EXAMPLES:: 

  

sage: RIF(1.0) << 32 

4294967296 

""" 

if isinstance(x, RealIntervalFieldElement) and isinstance(y, (int,long, Integer)): 

return x._lshift_(y) 

return sage.structure.element.bin_op(x, y, operator.lshift) 

  

def _rshift_(self, n): 

""" 

Return ``self/(2^n)`` for an integer ``n``. 

  

EXAMPLES:: 

  

sage: RIF(1.0)._rshift_(32) 

2.3283064365386963?e-10 

sage: RIF(1.5)._rshift_(2) 

0.37500000000000000? 

""" 

if n > sys.maxsize: 

raise OverflowError("n (=%s) must be <= %s" % (n, sys.maxsize)) 

x = self._new() 

mpfi_div_2exp(x.value, self.value, n) 

return x 

  

def __rshift__(x, y): 

""" 

Returns `x / 2^y`, for `y` an integer. Much faster 

than an ordinary division. 

  

EXAMPLES:: 

  

sage: RIF(1024.0) >> 14 

0.062500000000000000? 

""" 

if isinstance(x, RealIntervalFieldElement) and \ 

isinstance(y, (int,long,Integer)): 

return x._rshift_(y) 

return sage.structure.element.bin_op(x, y, operator.rshift) 

  

def multiplicative_order(self): 

r""" 

Return `n` such that ``self^n == 1``. 

  

Only `\pm 1` have finite multiplicative order. 

  

EXAMPLES:: 

  

sage: RIF(1).multiplicative_order() 

1 

sage: RIF(-1).multiplicative_order() 

2 

sage: RIF(3).multiplicative_order() 

+Infinity 

""" 

if self == 1: 

return 1 

elif self == -1: 

return 2 

return sage.rings.infinity.infinity 

  

def precision(self): 

""" 

Returns the precision of ``self``. 

  

EXAMPLES:: 

  

sage: RIF(2.1).precision() 

53 

sage: RealIntervalField(200)(2.1).precision() 

200 

""" 

return (<RealIntervalField_class>self._parent).__prec 

  

prec = precision 

  

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

# Rounding etc 

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

  

def floor(self): 

""" 

Return the floor of this interval as an interval 

  

The floor of a real number `x` is the largest integer smaller than or 

equal to `x`. 

  

.. SEEALSO:: 

  

- :meth:`unique_floor` -- method which returns the floor as an integer 

if it is unique or raises a ``ValueError`` otherwise. 

- :meth:`ceil` -- truncation towards plus infinity 

- :meth:`round` -- rounding 

- :meth:`trunc` -- truncation towards zero 

  

EXAMPLES:: 

  

sage: R = RealIntervalField() 

sage: (2.99).floor() 

2 

sage: (2.00).floor() 

2 

sage: floor(RR(-5/2)) 

-3 

sage: R = RealIntervalField(100) 

sage: a = R(9.5, 11.3); a.str(style='brackets') 

'[9.5000000000000000000000000000000 .. 11.300000000000000710542735760101]' 

sage: floor(a).str(style='brackets') 

'[9.0000000000000000000000000000000 .. 11.000000000000000000000000000000]' 

sage: a.floor() 

10.? 

sage: ceil(a) 

11.? 

sage: a.ceil().str(style='brackets') 

'[10.000000000000000000000000000000 .. 12.000000000000000000000000000000]' 

""" 

return self.parent()(self.lower().floor(), self.upper().floor()) 

  

def ceil(self): 

""" 

Return the celing of this interval as an interval 

  

The ceiling of a real number `x` is the smallest integer larger than or 

equal to `x`. 

  

.. SEEALSO:: 

  

- :meth:`unique_ceil` -- return the ceil as an integer if it is 

unique and raises a ``ValueError`` otherwise 

- :meth:`floor` -- truncation towards minus infinity 

- :meth:`trunc` -- truncation towards zero 

- :meth:`round` -- rounding 

  

EXAMPLES:: 

  

sage: (2.99).ceil() 

3 

sage: (2.00).ceil() 

2 

sage: (2.01).ceil() 

3 

sage: R = RealIntervalField(30) 

sage: a = R(-9.5, -11.3); a.str(style='brackets') 

'[-11.300000012 .. -9.5000000000]' 

sage: a.floor().str(style='brackets') 

'[-12.000000000 .. -10.000000000]' 

sage: a.ceil() 

-10.? 

sage: ceil(a).str(style='brackets') 

'[-11.000000000 .. -9.0000000000]' 

""" 

return self.parent()(self.lower().ceil(), self.upper().ceil()) 

  

ceiling = ceil 

  

def round(self): 

r""" 

Return the nearest integer of this interval as an interval 

  

.. SEEALSO:: 

  

- :meth:`unique_round` -- return the round as an integer if it is 

unique and raises a ``ValueError`` otherwise 

- :meth:`floor` -- truncation towards `-\infty` 

- :meth:`ceil` -- truncation towards `+\infty` 

- :meth:`trunc` -- truncation towards `0` 

  

EXAMPLES:: 

  

sage: RIF(7.2, 7.3).round() 

7 

sage: RIF(-3.2, -3.1).round() 

-3 

  

Be careful that the answer is not an integer but an interval:: 

  

sage: RIF(2.2, 2.3).round().parent() 

Real Interval Field with 53 bits of precision 

  

And in some cases, the lower and upper bounds of this interval do not 

agree:: 

  

sage: r = RIF(2.5, 3.5).round() 

sage: r 

4.? 

sage: r.lower() 

3.00000000000000 

sage: r.upper() 

4.00000000000000 

""" 

return self.parent()(self.lower().round(), self.upper().round()) 

  

def trunc(self): 

r""" 

Return the truncation of this interval as an interval 

  

The truncation of `x` is the floor of `x` if `x` is non-negative or the 

ceil of `x` if `x` is negative. 

  

.. SEEALSO:: 

  

- :meth:`unique_trunc` -- return the trunc as an integer if it is 

unique and raises a ``ValueError`` otherwise 

- :meth:`floor` -- truncation towards `-\infty` 

- :meth:`ceil` -- truncation towards `+\infty` 

- :meth:`round` -- rounding 

  

EXAMPLES:: 

  

sage: RIF(2.3, 2.7).trunc() 

2 

sage: parent(_) 

Real Interval Field with 53 bits of precision 

  

sage: RIF(-0.9, 0.9).trunc() 

0 

sage: RIF(-7.5, -7.3).trunc() 

-7 

  

In the above example, the obtained interval contains only one element. 

But on the following it is not the case anymore:: 

  

sage: r = RIF(2.99, 3.01).trunc() 

sage: r.upper() 

3.00000000000000 

sage: r.lower() 

2.00000000000000 

""" 

return self.parent()(self.lower().trunc(), self.upper().trunc()) 

  

def frac(self): 

r""" 

Return the fractional part of this interval as an interval. 

  

The fractional part `y` of a real number `x` is the unique element in the 

interval `(-1,1)` that has the same sign as `x` and such that `x-y` is 

an integer. The integer `x-y` can be obtained through the method 

:meth:`trunc`. 

  

The output of this function is the smallest interval that contains all 

possible values of `frac(x)` for `x` in this interval. Note that if it 

contains an integer then the answer might not be very meaningful. More 

precisely, if the endpoints are `a` and `b` then: 

  

- if `floor(b) > \max(a,0)` then the interval obtained contains `[0,1]`, 

- if `ceil(a) < \min(b,0)` then the interval obtained contains `[-1,0]`. 

  

.. SEEALSO:: 

  

:meth:`trunc` -- return the integer part complement to this 

fractional part 

  

EXAMPLES:: 

  

sage: RIF(2.37123, 2.372).frac() 

0.372? 

sage: RIF(-23.12, -23.13).frac() 

-0.13? 

  

sage: RIF(.5, 1).frac().endpoints() 

(0.000000000000000, 1.00000000000000) 

sage: RIF(1, 1.5).frac().endpoints() 

(0.000000000000000, 0.500000000000000) 

  

sage: r = RIF(-22.47, -22.468) 

sage: r in (r.frac() + r.trunc()) 

True 

  

sage: r = RIF(18.222, 18.223) 

sage: r in (r.frac() + r.trunc()) 

True 

  

sage: RIF(1.99, 2.025).frac().endpoints() 

(0.000000000000000, 1.00000000000000) 

sage: RIF(1.99, 2.00).frac().endpoints() 

(0.000000000000000, 1.00000000000000) 

sage: RIF(2.00, 2.025).frac().endpoints() 

(0.000000000000000, 0.0250000000000000) 

  

sage: RIF(-2.1,-0.9).frac().endpoints() 

(-1.00000000000000, -0.000000000000000) 

sage: RIF(-0.5,0.5).frac().endpoints() 

(-0.500000000000000, 0.500000000000000) 

""" 

a = self.lower() 

b = self.upper() 

P = self.parent() 

r = P(a.frac(), b.frac()) 

if b.floor() > max(a,0): 

r = r.union(P(0, 1)) 

if a.ceil() < min(b,0): 

r = r.union(P(-1, 0)) 

return r 

  

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

# Conversions 

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

  

def _mpfr_(self, RealField_class field): 

""" 

Convert to a real field, honoring the rounding mode of the 

real field. 

  

EXAMPLES:: 

  

sage: a = RealIntervalField(30)("1.2") 

sage: RR(a) 

1.20000000018626 

sage: b = RIF(-1, 3) 

sage: RR(b) 

1.00000000000000 

  

With different rounding modes:: 

  

sage: RealField(53, rnd="RNDU")(a) 

1.20000000111759 

sage: RealField(53, rnd="RNDD")(a) 

1.19999999925494 

sage: RealField(53, rnd="RNDZ")(a) 

1.19999999925494 

sage: RealField(53, rnd="RNDU")(b) 

3.00000000000000 

sage: RealField(53, rnd="RNDD")(b) 

-1.00000000000000 

sage: RealField(53, rnd="RNDZ")(b) 

0.000000000000000 

""" 

cdef RealNumber x = field._new() 

if field.rnd == MPFR_RNDN: 

mpfi_mid(x.value, self.value) 

elif field.rnd == MPFR_RNDD: 

mpfi_get_left(x.value, self.value) 

elif field.rnd == MPFR_RNDU: 

mpfi_get_right(x.value, self.value) 

elif field.rnd == MPFR_RNDZ: 

if mpfi_is_strictly_pos(self.value): # interval is > 0 

mpfi_get_left(x.value, self.value) 

elif mpfi_is_strictly_neg(self.value): # interval is < 0 

mpfi_get_right(x.value, self.value) 

else: 

mpfr_set_zero(x.value, 1) # interval contains 0 

elif field.rnd == MPFR_RNDA: 

# return the endpoint which is furthest from 0 

lo, hi = self.endpoints() 

if hi.abs() >= lo.abs(): 

mpfi_get_right(x.value, self.value) 

else: 

mpfi_get_left(x.value, self.value) 

else: 

raise AssertionError("%s has unknown rounding mode"%field) 

return x 

  

def __float__(self): 

""" 

Convert ``self`` to a ``float``. 

  

EXAMPLES:: 

  

sage: float(RIF(1)) 

1.0 

""" 

return float(self.n(self.prec())) 

  

def __complex__(self): 

""" 

Convert ``self`` to a ``complex``. 

  

EXAMPLES:: 

  

sage: complex(RIF(1)) 

(1+0j) 

""" 

return complex(self.n(self.prec())) 

  

def unique_sign(self): 

r""" 

Return the sign of this element if it is well defined. 

  

This method returns `+1` if all elements in this interval are positive, 

`-1` if all of them are negative and `0` if it contains only zero. 

Otherwise it raises a ``ValueError``. 

  

EXAMPLES:: 

  

sage: RIF(1.2,5.7).unique_sign() 

1 

sage: RIF(-3,-2).unique_sign() 

-1 

sage: RIF(0).unique_sign() 

0 

sage: RIF(0,1).unique_sign() 

Traceback (most recent call last): 

... 

ValueError: interval does not have a unique sign 

sage: RIF(-1,0).unique_sign() 

Traceback (most recent call last): 

... 

ValueError: interval does not have a unique sign 

sage: RIF(-0.1, 0.1).unique_sign() 

Traceback (most recent call last): 

... 

ValueError: interval does not have a unique sign 

""" 

if mpfi_is_zero(self.value): 

return 0 

elif mpfi_is_strictly_pos(self.value): 

return 1 

elif mpfi_is_strictly_neg(self.value): 

return -1 

else: 

raise ValueError("interval does not have a unique sign") 

  

def argument(self): 

r""" 

The argument of this interval, if it is well-defined, in the 

complex sense. Otherwise raises a ``ValueError``. 

  

OUTPUT: 

  

- an element of the parent of this interval (0 or pi) 

  

EXAMPLES:: 

  

sage: RIF(1).argument() 

0 

sage: RIF(-1).argument() 

3.141592653589794? 

sage: RIF(0,1).argument() 

0 

sage: RIF(-1,0).argument() 

3.141592653589794? 

sage: RIF(0).argument() 

Traceback (most recent call last): 

... 

ValueError: Can't take the argument of an exact zero 

sage: RIF(-1,1).argument() 

Traceback (most recent call last): 

... 

ValueError: Can't take the argument of interval strictly containing zero 

  

""" 

k=self.parent() 

if mpfi_is_zero(self.value): 

raise ValueError("Can't take the argument of an exact zero") 

if mpfi_is_nonneg(self.value): 

return k.zero() 

elif mpfi_is_nonpos(self.value): 

return k.pi() 

else: 

raise ValueError("Can't take the argument of interval strictly containing zero") 

  

def unique_floor(self): 

""" 

Returns the unique floor of this interval, if it is well defined, 

otherwise raises a ``ValueError``. 

  

OUTPUT: 

  

- an integer. 

  

.. SEEALSO:: 

  

:meth:`floor` -- return the floor as an interval (and never raise 

error) 

  

EXAMPLES:: 

  

sage: RIF(pi).unique_floor() 

3 

sage: RIF(100*pi).unique_floor() 

314 

sage: RIF(100, 200).unique_floor() 

Traceback (most recent call last): 

... 

ValueError: interval does not have a unique floor 

""" 

a, b = self.lower().floor(), self.upper().floor() 

if a == b: 

return a 

else: 

raise ValueError("interval does not have a unique floor") 

  

def unique_ceil(self): 

""" 

Returns the unique ceiling of this interval, if it is well defined, 

otherwise raises a ``ValueError``. 

  

OUTPUT: 

  

- an integer. 

  

.. SEEALSO:: 

  

:meth:`ceil` -- return the ceil as an interval (and never raise 

error) 

  

EXAMPLES:: 

  

sage: RIF(pi).unique_ceil() 

4 

sage: RIF(100*pi).unique_ceil() 

315 

sage: RIF(100, 200).unique_ceil() 

Traceback (most recent call last): 

... 

ValueError: interval does not have a unique ceil 

""" 

a, b = self.lower().ceil(), self.upper().ceil() 

if a == b: 

return a 

else: 

raise ValueError("interval does not have a unique ceil") 

  

def unique_round(self): 

""" 

Returns the unique round (nearest integer) of this interval, 

if it is well defined, otherwise raises a ``ValueError``. 

  

OUTPUT: 

  

- an integer. 

  

.. SEEALSO:: 

  

:meth:`round` -- return the round as an interval (and never raise 

error) 

  

EXAMPLES:: 

  

sage: RIF(pi).unique_round() 

3 

sage: RIF(1000*pi).unique_round() 

3142 

sage: RIF(100, 200).unique_round() 

Traceback (most recent call last): 

... 

ValueError: interval does not have a unique round (nearest integer) 

sage: RIF(1.2, 1.7).unique_round() 

Traceback (most recent call last): 

... 

ValueError: interval does not have a unique round (nearest integer) 

sage: RIF(0.7, 1.2).unique_round() 

1 

sage: RIF(-pi).unique_round() 

-3 

sage: (RIF(4.5).unique_round(), RIF(-4.5).unique_round()) 

(5, -5) 

  

TESTS:: 

  

sage: RIF(-1/2, -1/3).unique_round() 

Traceback (most recent call last): 

... 

ValueError: interval does not have a unique round (nearest integer) 

sage: RIF(-1/2, 1/3).unique_round() 

Traceback (most recent call last): 

... 

ValueError: interval does not have a unique round (nearest integer) 

sage: RIF(-1/3, 1/3).unique_round() 

0 

sage: RIF(-1/2, 0).unique_round() 

Traceback (most recent call last): 

... 

ValueError: interval does not have a unique round (nearest integer) 

sage: RIF(1/2).unique_round() 

1 

sage: RIF(-1/2).unique_round() 

-1 

sage: RIF(0).unique_round() 

0 

""" 

a, b = self.lower().round(), self.upper().round() 

if a == b: 

return a 

else: 

raise ValueError("interval does not have a unique round (nearest integer)") 

  

def unique_trunc(self): 

r""" 

Return the nearest integer toward zero if it is unique, otherwise raise 

a ``ValueError``. 

  

.. SEEALSO:: 

  

:meth:`trunc` -- return the truncation as an interval (and never 

raise error) 

  

EXAMPLES:: 

  

sage: RIF(1.3,1.4).unique_trunc() 

1 

sage: RIF(-3.3, -3.2).unique_trunc() 

-3 

sage: RIF(2.9,3.2).unique_trunc() 

Traceback (most recent call last): 

... 

ValueError: interval does not have a unique trunc (nearest integer toward zero) 

sage: RIF(-3.1,-2.9).unique_trunc() 

Traceback (most recent call last): 

... 

ValueError: interval does not have a unique trunc (nearest integer toward zero) 

""" 

a = self.lower().trunc() 

b = self.upper().trunc() 

if a == b: 

return a 

else: 

raise ValueError("interval does not have a unique trunc (nearest integer toward zero)") 

  

def unique_integer(self): 

""" 

Return the unique integer in this interval, if there is exactly one, 

otherwise raises a ``ValueError``. 

  

EXAMPLES:: 

  

sage: RIF(pi).unique_integer() 

Traceback (most recent call last): 

... 

ValueError: interval contains no integer 

sage: RIF(pi, pi+1).unique_integer() 

4 

sage: RIF(pi, pi+2).unique_integer() 

Traceback (most recent call last): 

... 

ValueError: interval contains more than one integer 

sage: RIF(100).unique_integer() 

100 

""" 

a, b = self.lower().ceil(), self.upper().floor() 

if a == b: 

return a 

elif a < b: 

raise ValueError("interval contains more than one integer") 

else: 

raise ValueError("interval contains no integer") 

  

def simplest_rational(self, low_open=False, high_open=False): 

""" 

Return the simplest rational in this interval. Given rationals 

`a / b` and `c / d` (both in lowest terms), the former is simpler if 

`b<d` or if `b = d` and `|a| < |c|`. 

  

If optional parameters ``low_open`` or ``high_open`` are ``True``, 

then treat this as an open interval on that end. 

  

EXAMPLES:: 

  

sage: RealIntervalField(10)(pi).simplest_rational() 

22/7 

sage: RealIntervalField(20)(pi).simplest_rational() 

355/113 

sage: RIF(0.123, 0.567).simplest_rational() 

1/2 

sage: RIF(RR(1/3).nextabove(), RR(3/7)).simplest_rational() 

2/5 

sage: RIF(1234/567).simplest_rational() 

1234/567 

sage: RIF(-8765/432).simplest_rational() 

-8765/432 

sage: RIF(-1.234, 0.003).simplest_rational() 

0 

sage: RIF(RR(1/3)).simplest_rational() 

6004799503160661/18014398509481984 

sage: RIF(RR(1/3)).simplest_rational(high_open=True) 

Traceback (most recent call last): 

... 

ValueError: simplest_rational() on open, empty interval 

sage: RIF(1/3, 1/2).simplest_rational() 

1/2 

sage: RIF(1/3, 1/2).simplest_rational(high_open=True) 

1/3 

sage: phi = ((RealIntervalField(500)(5).sqrt() + 1)/2) 

sage: phi.simplest_rational() == fibonacci(362)/fibonacci(361) 

True 

""" 

if mpfr_equal_p(&self.value.left, &self.value.right): 

if low_open or high_open: 

raise ValueError('simplest_rational() on open, empty interval') 

return self.lower().exact_rational() 

  

if mpfi_has_zero(self.value): 

return Rational(0) 

  

if mpfi_is_neg(self.value): 

return -(self._neg_().simplest_rational(low_open=high_open, high_open=low_open)) 

  

low = self.lower() 

high = self.upper() 

  

# First, we try using approximate arithmetic of slightly higher 

# precision. 

cdef RealIntervalFieldElement highprec 

highprec = RealIntervalField(int(self.prec() * 1.2))(self) 

  

cdef Rational try1 = highprec._simplest_rational_helper() 

  

# Note that to compute "try1 >= low", Sage converts try1 to a 

# floating-point number rounding down, and "try1 <= high" 

# rounds up (since "low" and "high" are in downward-rounding 

# and upward-rounding fields, respectively). 

if try1 >= low and try1 <= high: 

ok = True 

if low_open and (try1 == low.exact_rational()): 

ok = False 

if high_open and (try1 == high.exact_rational()): 

ok = False 

if ok: 

return try1 

  

# We could try again with higher precision; instead, we 

# go directly to using exact arithmetic. 

return _simplest_rational_exact(low.exact_rational(), 

high.exact_rational(), 

low_open, 

high_open) 

  

cdef Rational _simplest_rational_helper(self): 

""" 

Returns the simplest rational in an interval which is either equal 

to or slightly larger than ``self``. We assume that both endpoints of 

``self`` are nonnegative. 

""" 

  

low = self.lower() 

  

cdef RealIntervalFieldElement new_elt 

  

if low <= 1: 

if low == 0: 

return Rational(0) 

if self.upper() >= 1: 

return Rational(1) 

new_elt = ~self 

return ~(new_elt._simplest_rational_helper()) 

  

fl = low.floor() 

new_elt = self - fl 

return fl + new_elt._simplest_rational_helper() 

  

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

# Comparisons: ==, !=, <, <=, >, >= 

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

  

def is_NaN(self): 

""" 

Check to see if ``self`` is Not-a-Number ``NaN``. 

  

EXAMPLES:: 

  

sage: a = RIF(0) / RIF(0.0,0.00); a 

[.. NaN ..] 

sage: a.is_NaN() 

True 

""" 

return mpfi_nan_p(self.value) 

  

cpdef _richcmp_(left, right, int op): 

""" 

Implement comparisons between intervals. 

  

See the file header comment for more information on interval 

comparison. 

  

EXAMPLES:: 

  

sage: RIF(0) < RIF(2) 

True 

sage: RIF(0, 1) < RIF(2, 3) 

True 

  

Because these are possible ranges, they are only equal if they 

are exact and follow inequality if the intervals are disjoint:: 

  

sage: RIF(2) == RIF(2) 

True 

sage: RIF(0, 1) == RIF(0, 1) 

False 

sage: RIF(0, 2) < RIF(2, 3) 

False 

sage: RIF(0, 2) > RIF(2, 3) 

False 

  

EXAMPLES:: 

  

sage: 0 < RIF(1, 3) 

True 

sage: 1 < RIF(1, 3) 

False 

sage: 2 < RIF(1, 3) 

False 

sage: 4 < RIF(1, 3) 

False 

sage: RIF(0, 1/2) < RIF(1, 3) 

True 

sage: RIF(0, 1) < RIF(1, 3) 

False 

sage: RIF(0, 2) < RIF(1, 3) 

False 

sage: RIF(1, 2) < RIF(1, 3) 

False 

sage: RIF(1, 3) < 4 

True 

sage: RIF(1, 3) < 3 

False 

sage: RIF(1, 3) < 2 

False 

sage: RIF(1, 3) < 0 

False 

sage: 0 <= RIF(1, 3) 

True 

sage: 1 <= RIF(1, 3) 

True 

sage: 2 <= RIF(1, 3) 

False 

sage: 4 <= RIF(1, 3) 

False 

sage: RIF(0, 1/2) <= RIF(1, 3) 

True 

sage: RIF(0, 1) <= RIF(1, 3) 

True 

sage: RIF(0, 2) <= RIF(1, 3) 

False 

sage: RIF(1, 2) <= RIF(1, 3) 

False 

sage: RIF(1, 3) <= 4 

True 

sage: RIF(1, 3) <= 3 

True 

sage: RIF(1, 3) <= 2 

False 

sage: RIF(1, 3) <= 0 

False 

sage: RIF(1, 3) > 0 

True 

sage: RIF(1, 3) > 1 

False 

sage: RIF(1, 3) > 2 

False 

sage: RIF(1, 3) > 4 

False 

sage: RIF(1, 3) > RIF(0, 1/2) 

True 

sage: RIF(1, 3) > RIF(0, 1) 

False 

sage: RIF(1, 3) > RIF(0, 2) 

False 

sage: RIF(1, 3) > RIF(1, 2) 

False 

sage: 4 > RIF(1, 3) 

True 

sage: 3 > RIF(1, 3) 

False 

sage: 2 > RIF(1, 3) 

False 

sage: 0 > RIF(1, 3) 

False 

sage: RIF(1, 3) >= 0 

True 

sage: RIF(1, 3) >= 1 

True 

sage: RIF(1, 3) >= 2 

False 

sage: RIF(1, 3) >= 4 

False 

sage: RIF(1, 3) >= RIF(0, 1/2) 

True 

sage: RIF(1, 3) >= RIF(0, 1) 

True 

sage: RIF(1, 3) >= RIF(0, 2) 

False 

sage: RIF(1, 3) >= RIF(1, 2) 

False 

sage: 4 >= RIF(1, 3) 

True 

sage: 3 >= RIF(1, 3) 

True 

sage: 2 >= RIF(1, 3) 

False 

sage: 0 >= RIF(1, 3) 

False 

sage: 0 == RIF(0) 

True 

sage: 0 == RIF(1) 

False 

sage: 1 == RIF(0) 

False 

sage: 0 == RIF(0, 1) 

False 

sage: 1 == RIF(0, 1) 

False 

sage: RIF(0, 1) == RIF(0, 1) 

False 

sage: RIF(1) == 0 

False 

sage: RIF(1) == 1 

True 

sage: RIF(0) == RIF(0) 

True 

sage: RIF(pi) == RIF(pi) 

False 

sage: RIF(0, 1) == RIF(1, 2) 

False 

sage: RIF(1, 2) == RIF(0, 1) 

False 

sage: 0 != RIF(0) 

False 

sage: 0 != RIF(1) 

True 

sage: 1 != RIF(0) 

True 

sage: 0 != RIF(0, 1) 

False 

sage: 1 != RIF(0, 1) 

False 

sage: RIF(0, 1) != RIF(0, 1) 

False 

sage: RIF(1) != 0 

True 

sage: RIF(1) != 1 

False 

sage: RIF(0) != RIF(0) 

False 

sage: RIF(pi) != RIF(pi) 

False 

sage: RIF(0, 1) != RIF(1, 2) 

False 

sage: RIF(1, 2) != RIF(0, 1) 

False 

""" 

cdef RealIntervalFieldElement lt, rt 

  

lt = left 

rt = right 

  

if op == Py_LT: # < 

return mpfr_less_p(&lt.value.right, &rt.value.left) 

elif op == Py_EQ: # == 

# a == b iff a <= b and b <= a 

# (this gives a result with two comparisons, where the 

# obvious approach would use three) 

return mpfr_lessequal_p(&lt.value.right, &rt.value.left) \ 

and mpfr_lessequal_p(&rt.value.right, &lt.value.left) 

elif op == Py_GT: # > 

return mpfr_less_p(&rt.value.right, &lt.value.left) 

elif op == Py_LE: # <= 

return mpfr_lessequal_p(&lt.value.right, &rt.value.left) 

elif op == Py_NE: # != 

return mpfr_less_p(&lt.value.right, &rt.value.left) \ 

or mpfr_less_p(&rt.value.right, &lt.value.left) 

elif op == Py_GE: # >= 

return mpfr_lessequal_p(&rt.value.right, &lt.value.left) 

  

def __nonzero__(self): 

""" 

Return ``True`` if ``self`` is not known to be exactly zero. 

  

EXAMPLES:: 

  

sage: bool(RIF(0)) 

False 

sage: bool(RIF(1)) 

True 

sage: bool(RIF(1, 2)) 

True 

sage: bool(RIF(0, 1)) 

True 

sage: bool(RIF(-1, 1)) 

True 

""" 

return not (mpfr_zero_p(&self.value.left) and mpfr_zero_p(&self.value.right)) 

  

def lexico_cmp(left, right): 

""" 

Compare two intervals lexicographically. 

  

This means that the left bounds are compared first and then 

the right bounds are compared if the left bounds coincide. 

  

Return 0 if they are the same interval, -1 if the second is larger, 

or 1 if the first is larger. 

  

EXAMPLES:: 

  

sage: RIF(0).lexico_cmp(RIF(1)) 

-1 

sage: RIF(0, 1).lexico_cmp(RIF(1)) 

-1 

sage: RIF(0, 1).lexico_cmp(RIF(1, 2)) 

-1 

sage: RIF(0, 0.99999).lexico_cmp(RIF(1, 2)) 

-1 

sage: RIF(1, 2).lexico_cmp(RIF(0, 1)) 

1 

sage: RIF(1, 2).lexico_cmp(RIF(0)) 

1 

sage: RIF(0, 1).lexico_cmp(RIF(0, 2)) 

-1 

sage: RIF(0, 1).lexico_cmp(RIF(0, 1)) 

0 

sage: RIF(0, 1).lexico_cmp(RIF(0, 1/2)) 

1 

""" 

cdef RealIntervalFieldElement lt, rt 

  

lt = left 

rt = right 

  

cdef int i 

i = mpfr_cmp(&lt.value.left, &rt.value.left) 

if i < 0: 

return -1 

elif i > 0: 

return 1 

i = mpfr_cmp(&lt.value.right, &rt.value.right) 

if i < 0: 

return -1 

elif i > 0: 

return 1 

else: 

return 0 

  

cpdef int _cmp_(self, other) except -2: 

""" 

Deprecated method (:trac:`22907`) 

  

EXAMPLES:: 

  

sage: a = RIF(1) 

sage: a._cmp_(a) 

doctest:...: DeprecationWarning: for RIF elements, do not use cmp 

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

0 

""" 

from sage.misc.superseded import deprecation 

deprecation(22907, 'for RIF elements, do not use cmp') 

return self.lexico_cmp(other) 

  

def __contains__(self, other): 

""" 

Test whether one interval (or real number) is totally contained in 

another. 

  

EXAMPLES:: 

  

sage: RIF(0, 2) in RIF(1, 3) 

False 

sage: RIF(0, 2) in RIF(0, 2) 

True 

sage: RIF(1, 2) in RIF(0, 3) 

True 

sage: 1.0 in RIF(0, 2) 

True 

sage: pi in RIF(3.1415, 3.1416) 

True 

sage: 22/7 in RIF(3.1415, 3.1416) 

False 

""" 

  

cdef RealIntervalFieldElement other_intv 

cdef RealNumber other_rn 

if isinstance(other, RealIntervalFieldElement): 

other_intv = other 

return mpfi_is_inside(other_intv.value, self.value) 

elif isinstance(other, RealNumber): 

other_rn = other 

return mpfi_is_inside_fr(other_rn.value, self.value) 

try: 

other_intv = self._parent(other) 

return mpfi_is_inside(other_intv.value, self.value) 

except TypeError as msg: 

return False 

  

def contains_zero(self): 

""" 

Return ``True`` if ``self`` is an interval containing zero. 

  

EXAMPLES:: 

  

sage: RIF(0).contains_zero() 

True 

sage: RIF(1, 2).contains_zero() 

False 

sage: RIF(-1, 1).contains_zero() 

True 

sage: RIF(-1, 0).contains_zero() 

True 

""" 

return mpfi_has_zero(self.value) 

  

def overlaps(self, RealIntervalFieldElement other): 

""" 

Return ``True`` if ``self`` and other are intervals with at least one 

value in common. For intervals ``a`` and ``b``, we have 

``a.overlaps(b)`` iff ``not(a!=b)``. 

  

EXAMPLES:: 

  

sage: RIF(0, 1).overlaps(RIF(1, 2)) 

True 

sage: RIF(1, 2).overlaps(RIF(0, 1)) 

True 

sage: RIF(0, 1).overlaps(RIF(2, 3)) 

False 

sage: RIF(2, 3).overlaps(RIF(0, 1)) 

False 

sage: RIF(0, 3).overlaps(RIF(1, 2)) 

True 

sage: RIF(0, 2).overlaps(RIF(1, 3)) 

True 

""" 

return mpfr_greaterequal_p(&self.value.right, &other.value.left) \ 

and mpfr_greaterequal_p(&other.value.right, &self.value.left) 

  

def intersection(self, other): 

""" 

Return the intersection of two intervals. If the intervals do not 

overlap, raises a ``ValueError``. 

  

EXAMPLES:: 

  

sage: RIF(1, 2).intersection(RIF(1.5, 3)).str(style='brackets') 

'[1.5000000000000000 .. 2.0000000000000000]' 

sage: RIF(1, 2).intersection(RIF(4/3, 5/3)).str(style='brackets') 

'[1.3333333333333332 .. 1.6666666666666668]' 

sage: RIF(1, 2).intersection(RIF(3, 4)) 

Traceback (most recent call last): 

... 

ValueError: intersection of non-overlapping intervals 

""" 

x = self._new() 

cdef RealIntervalFieldElement other_intv 

if isinstance(other, RealIntervalFieldElement): 

other_intv = other 

else: 

# Let type errors from _coerce_ propagate... 

other_intv = self._parent(other) 

  

mpfi_intersect(x.value, self.value, other_intv.value) 

if mpfr_less_p(&x.value.right, &x.value.left): 

raise ValueError("intersection of non-overlapping intervals") 

return x 

  

def union(self, other): 

""" 

Return the union of two intervals, or of an interval and a real 

number (more precisely, the convex hull). 

  

EXAMPLES:: 

  

sage: RIF(1, 2).union(RIF(pi, 22/7)).str(style='brackets') 

'[1.0000000000000000 .. 3.1428571428571433]' 

sage: RIF(1, 2).union(pi).str(style='brackets') 

'[1.0000000000000000 .. 3.1415926535897936]' 

sage: RIF(1).union(RIF(0, 2)).str(style='brackets') 

'[0.00000000000000000 .. 2.0000000000000000]' 

sage: RIF(1).union(RIF(-1)).str(style='brackets') 

'[-1.0000000000000000 .. 1.0000000000000000]' 

""" 

x = self._new() 

cdef RealIntervalFieldElement other_intv 

cdef RealNumber other_rn 

if isinstance(other, RealIntervalFieldElement): 

other_intv = other 

mpfi_union(x.value, self.value, other_intv.value) 

elif isinstance(other, RealNumber): 

other_rn = other 

mpfi_set(x.value, self.value) 

mpfi_put_fr(x.value, other_rn.value) 

else: 

# Let type errors from _coerce_ propagate... 

other_intv = self._parent(other) 

mpfi_union(x.value, self.value, other_intv.value) 

return x 

  

def min(self, *_others): 

""" 

Return an interval containing the minimum of ``self`` and the 

arguments. 

  

EXAMPLES:: 

  

sage: a = RIF(-1, 1).min(0).endpoints() 

sage: a[0] == -1.0 and a[1].abs() == 0.0 # in MPFI, the sign of 0.0 is not specified 

True 

sage: RIF(-1, 1).min(pi).endpoints() 

(-1.00000000000000, 1.00000000000000) 

sage: RIF(-1, 1).min(RIF(-100, 100)).endpoints() 

(-100.000000000000, 1.00000000000000) 

sage: RIF(-1, 1).min(RIF(-100, 0)).endpoints() 

(-100.000000000000, 0.000000000000000) 

sage: RIF(-1, 1).min(RIF(-100, 2), RIF(-200, -3)).endpoints() 

(-200.000000000000, -3.00000000000000) 

  

Note that if the minimum is one of the given elements, 

that element will be returned. :: 

  

sage: a = RIF(-1, 1) 

sage: b = RIF(2, 3) 

sage: c = RIF(3, 4) 

sage: c.min(a, b) is a 

True 

sage: b.min(a, c) is a 

True 

sage: a.min(b, c) is a 

True 

  

It might also be convenient to call the method as a function:: 

  

sage: from sage.rings.real_mpfi import RealIntervalFieldElement 

sage: RealIntervalFieldElement.min(a, b, c) is a 

True 

sage: elements = [a, b, c] 

sage: RealIntervalFieldElement.min(*elements) is a 

True 

  

The generic min does not always do the right thing:: 

  

sage: min(0, RIF(-1, 1)) 

0 

sage: min(RIF(-1, 1), RIF(-100, 100)).endpoints() 

(-1.00000000000000, 1.00000000000000) 

  

Note that calls involving NaNs try to return a number when possible. 

This is consistent with IEEE-754-2008 but may be surprising. :: 

  

sage: RIF('nan').min(2, 1) 

1 

sage: RIF(-1/3).min(RIF('nan')) 

-0.3333333333333334? 

sage: RIF('nan').min(RIF('nan')) 

[.. NaN ..] 

  

.. SEEALSO:: 

  

:meth:`~sage.rings.real_mpfi.RealIntervalFieldElement.max` 

  

TESTS:: 

  

sage: a.min('x') 

Traceback (most recent call last): 

... 

TypeError: unable to convert 'x' to real interval 

""" 

cdef RealIntervalFieldElement constructed 

cdef RealIntervalFieldElement result 

cdef RealIntervalFieldElement other 

cdef bint initialized 

  

initialized = False 

result = self 

  

for _other in _others: 

if isinstance(_other, RealIntervalFieldElement): 

other = <RealIntervalFieldElement>_other 

else: 

other = self._parent(_other) 

  

if result.is_NaN(): 

result = other 

elif other.is_NaN(): 

pass 

elif mpfr_cmp(&result.value.right, &other.value.left) <= 0: 

pass 

elif mpfr_cmp(&other.value.right, &result.value.left) <= 0: 

result = other 

else: 

if not initialized: 

constructed = self._new() 

initialized = True 

mpfr_min(&constructed.value.left, 

&result.value.left, 

&other.value.left, 

MPFR_RNDD) 

mpfr_min(&constructed.value.right, 

&result.value.right, 

&other.value.right, 

MPFR_RNDU) 

result = constructed 

  

return result 

  

def max(self, *_others): 

""" 

Return an interval containing the maximum of ``self`` and the 

arguments. 

  

EXAMPLES:: 

  

sage: RIF(-1, 1).max(0).endpoints() 

(0.000000000000000, 1.00000000000000) 

sage: RIF(-1, 1).max(RIF(2, 3)).endpoints() 

(2.00000000000000, 3.00000000000000) 

sage: RIF(-1, 1).max(RIF(-100, 100)).endpoints() 

(-1.00000000000000, 100.000000000000) 

sage: RIF(-1, 1).max(RIF(-100, 100), RIF(5, 10)).endpoints() 

(5.00000000000000, 100.000000000000) 

  

Note that if the maximum is one of the given elements, 

that element will be returned. :: 

  

sage: a = RIF(-1, 1) 

sage: b = RIF(2, 3) 

sage: c = RIF(3, 4) 

sage: c.max(a, b) is c 

True 

sage: b.max(a, c) is c 

True 

sage: a.max(b, c) is c 

True 

  

It might also be convenient to call the method as a function:: 

  

sage: from sage.rings.real_mpfi import RealIntervalFieldElement 

sage: RealIntervalFieldElement.max(a, b, c) is c 

True 

sage: elements = [a, b, c] 

sage: RealIntervalFieldElement.max(*elements) is c 

True 

  

The generic max does not always do the right thing:: 

  

sage: max(0, RIF(-1, 1)) 

0 

sage: max(RIF(-1, 1), RIF(-100, 100)).endpoints() 

(-1.00000000000000, 1.00000000000000) 

  

Note that calls involving NaNs try to return a number when possible. 

This is consistent with IEEE-754-2008 but may be surprising. :: 

  

sage: RIF('nan').max(1, 2) 

2 

sage: RIF(-1/3).max(RIF('nan')) 

-0.3333333333333334? 

sage: RIF('nan').max(RIF('nan')) 

[.. NaN ..] 

  

.. SEEALSO:: 

  

:meth:`~sage.rings.real_mpfi.RealIntervalFieldElement.min` 

  

TESTS:: 

  

sage: a.max('x') 

Traceback (most recent call last): 

... 

TypeError: unable to convert 'x' to real interval 

""" 

cdef RealIntervalFieldElement constructed 

cdef RealIntervalFieldElement result 

cdef RealIntervalFieldElement other 

cdef bint initialized 

  

initialized = False 

result = self 

  

for _other in _others: 

if isinstance(_other, RealIntervalFieldElement): 

other = <RealIntervalFieldElement>_other 

else: 

other = self._parent(_other) 

  

if result.is_NaN(): 

result = other 

elif other.is_NaN(): 

pass 

elif mpfr_cmp(&result.value.right, &other.value.left) <= 0: 

result = other 

elif mpfr_cmp(&other.value.right, &result.value.left) <= 0: 

pass 

else: 

if not initialized: 

constructed = self._new() 

initialized = True 

  

mpfr_max(&constructed.value.left, 

&result.value.left, 

&other.value.left, 

MPFR_RNDD) 

mpfr_max(&constructed.value.right, 

&result.value.right, 

&other.value.right, 

MPFR_RNDU) 

result = constructed 

  

return result 

  

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

# Special Functions 

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

  

  

def sqrt(self): 

""" 

Return a square root of ``self``. Raises an error if ``self`` is 

nonpositive. 

  

If you use :meth:`square_root()` then an interval will always be 

returned (though it will be ``NaN`` if self is nonpositive). 

  

EXAMPLES:: 

  

sage: r = RIF(4.0) 

sage: r.sqrt() 

2 

sage: r.sqrt()^2 == r 

True 

  

:: 

  

sage: r = RIF(4344) 

sage: r.sqrt() 

65.90902821313633? 

sage: r.sqrt()^2 == r 

False 

sage: r in r.sqrt()^2 

True 

sage: r.sqrt()^2 - r 

0.?e-11 

sage: (r.sqrt()^2 - r).str(style='brackets') 

'[-9.0949470177292824e-13 .. 1.8189894035458565e-12]' 

  

:: 

  

sage: r = RIF(-2.0) 

sage: r.sqrt() 

Traceback (most recent call last): 

... 

ValueError: self (=-2) is not >= 0 

  

:: 

  

sage: r = RIF(-2, 2) 

sage: r.sqrt() 

Traceback (most recent call last): 

... 

ValueError: self (=0.?e1) is not >= 0 

""" 

if self.lower() < 0: 

raise ValueError("self (=%s) is not >= 0" % self) 

return self.square_root() 

  

  

def square_root(self): 

""" 

Return a square root of ``self``. An interval will always be returned 

(though it will be ``NaN`` if self is nonpositive). 

  

EXAMPLES:: 

  

sage: r = RIF(-2.0) 

sage: r.square_root() 

[.. NaN ..] 

sage: r.sqrt() 

Traceback (most recent call last): 

... 

ValueError: self (=-2) is not >= 0 

""" 

x = self._new() 

sig_on() 

mpfi_sqrt(x.value, self.value) 

sig_off() 

return x 

  

def __pow__(self, exponent, modulus): 

""" 

Raise ``self`` to ``exponent``. 

  

EXAMPLES:: 

  

sage: R = RealIntervalField(17) 

sage: x = R((-e,pi)) 

sage: x2 = x^2; x2.lower(), x2.upper() 

(0.0000, 9.870) 

sage: x3 = x^3; x3.lower(), x3.upper() 

(-26.83, 31.01) 

""" 

if exponent == 2: 

return self.square() 

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

q, r = divmod (exponent, 2) 

if r == 0: # x^(2q) = (x^q)^2 

xq = RingElement.__pow__(self, q) 

return xq.abs().square() 

else: 

return RingElement.__pow__(self, exponent) 

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

  

def log(self, base='e'): 

""" 

Return the logarithm of ``self`` to the given ``base``. 

  

EXAMPLES:: 

  

sage: R = RealIntervalField() 

sage: r = R(2); r.log() 

0.6931471805599453? 

sage: r = R(-2); r.log() 

0.6931471805599453? + 3.141592653589794?*I 

""" 

if self < 0: 

return self.parent().complex_field()(self).log(base) 

if base == 'e': 

x = self._new() 

sig_on() 

mpfi_log(x.value, self.value) 

sig_off() 

return x 

elif base == 10: 

return self.log10() 

elif base == 2: 

return self.log2() 

else: 

return self.log() / (self.parent()(base)).log() 

  

def log2(self): 

""" 

Return ``log`` to the base 2 of ``self``. 

  

EXAMPLES:: 

  

sage: r = RIF(16.0) 

sage: r.log2() 

4 

  

:: 

  

sage: r = RIF(31.9); r.log2() 

4.995484518877507? 

  

:: 

  

sage: r = RIF(0.0, 2.0) 

sage: r.log2() 

[-infinity .. 1.0000000000000000] 

""" 

if self < 0: 

return self.parent().complex_field()(self).log(2) 

x = self._new() 

sig_on() 

mpfi_log2(x.value, self.value) 

sig_off() 

return x 

  

def log10(self): 

""" 

Return log to the base 10 of ``self``. 

  

EXAMPLES:: 

  

sage: r = RIF(16.0); r.log10() 

1.204119982655925? 

sage: r.log() / log(10.0) 

1.204119982655925? 

  

:: 

  

sage: r = RIF(39.9); r.log10() 

1.600972895686749? 

  

:: 

  

sage: r = RIF(0.0) 

sage: r.log10() 

[-infinity .. -infinity] 

  

:: 

  

sage: r = RIF(-1.0) 

sage: r.log10() 

1.364376353841841?*I 

""" 

if self < 0: 

return self.parent().complex_field()(self).log(10) 

x = self._new() 

sig_on() 

mpfi_log10(x.value, self.value) 

sig_off() 

return x 

  

def exp(self): 

r""" 

Returns `e^\mathtt{self}` 

  

EXAMPLES:: 

  

sage: r = RIF(0.0) 

sage: r.exp() 

1 

  

:: 

  

sage: r = RIF(32.3) 

sage: a = r.exp(); a 

1.065888472748645?e14 

sage: a.log() 

32.30000000000000? 

  

:: 

  

sage: r = RIF(-32.3) 

sage: r.exp() 

9.38184458849869?e-15 

""" 

x = self._new() 

sig_on() 

mpfi_exp(x.value, self.value) 

sig_off() 

return x 

  

def exp2(self): 

""" 

Returns `2^\mathtt{self}` 

  

EXAMPLES:: 

  

sage: r = RIF(0.0) 

sage: r.exp2() 

1 

  

:: 

  

sage: r = RIF(32.0) 

sage: r.exp2() 

4294967296 

  

:: 

  

sage: r = RIF(-32.3) 

sage: r.exp2() 

1.891172482530207?e-10 

""" 

x = self._new() 

sig_on() 

mpfi_exp2(x.value, self.value) 

sig_off() 

return x 

  

def is_int(self): 

r""" 

Checks to see whether this interval includes exactly one integer. 

  

OUTPUT: 

  

If this contains exactly one integer, it returns the tuple 

``(True, n)``, where ``n`` is that integer; otherwise, this returns 

``(False, None)``. 

  

EXAMPLES:: 

  

sage: a = RIF(0.8,1.5) 

sage: a.is_int() 

(True, 1) 

sage: a = RIF(1.1,1.5) 

sage: a.is_int() 

(False, None) 

sage: a = RIF(1,2) 

sage: a.is_int() 

(False, None) 

sage: a = RIF(-1.1, -0.9) 

sage: a.is_int() 

(True, -1) 

sage: a = RIF(0.1, 1.9) 

sage: a.is_int() 

(True, 1) 

sage: RIF(+infinity,+infinity).is_int() 

(False, None) 

""" 

a = self.lower() 

if a.is_NaN() or a.is_infinity(): 

return False, None 

a = a.ceil() 

b = self.upper() 

if b.is_NaN() or b.is_infinity(): 

return False, None 

b = b.floor() 

if a == b: 

return True, a 

else: 

return False, None 

  

def cos(self): 

""" 

Return the cosine of ``self``. 

  

EXAMPLES:: 

  

sage: t=RIF(pi)/2 

sage: t.cos() 

0.?e-15 

sage: t.cos().str(style='brackets') 

'[-1.6081226496766367e-16 .. 6.1232339957367661e-17]' 

sage: t.cos().cos() 

0.9999999999999999? 

  

TESTS: 

  

This looped forever with an earlier version of MPFI, but now 

it works:: 

  

sage: RIF(-1, 1).cos().str(style='brackets') 

'[0.54030230586813965 .. 1.0000000000000000]' 

""" 

x = self._new() 

sig_on() 

mpfi_cos(x.value, self.value) 

sig_off() 

return x 

  

def sin(self): 

""" 

Return the sine of ``self``. 

  

EXAMPLES:: 

  

sage: R = RealIntervalField(100) 

sage: R(2).sin() 

0.909297426825681695396019865912? 

""" 

x = self._new() 

sig_on() 

mpfi_sin(x.value, self.value) 

sig_off() 

return x 

  

def tan(self): 

""" 

Return the tangent of ``self``. 

  

EXAMPLES:: 

  

sage: q = RIF.pi()/3 

sage: q.tan() 

1.732050807568877? 

sage: q = RIF.pi()/6 

sage: q.tan() 

0.577350269189626? 

""" 

x = self._new() 

sig_on() 

mpfi_tan(x.value, self.value) 

sig_off() 

return x 

  

def arccos(self): 

""" 

Return the inverse cosine of ``self``. 

  

EXAMPLES:: 

  

sage: q = RIF.pi()/3; q 

1.047197551196598? 

sage: i = q.cos(); i 

0.500000000000000? 

sage: q2 = i.arccos(); q2 

1.047197551196598? 

sage: q == q2 

False 

sage: q != q2 

False 

sage: q2.lower() == q.lower() 

False 

sage: q - q2 

0.?e-15 

sage: q in q2 

True 

""" 

x = self._new() 

sig_on() 

mpfi_acos(x.value, self.value) 

sig_off() 

return x 

  

def arcsin(self): 

""" 

Return the inverse sine of ``self``. 

  

EXAMPLES:: 

  

sage: q = RIF.pi()/5; q 

0.6283185307179587? 

sage: i = q.sin(); i 

0.587785252292474? 

sage: q2 = i.arcsin(); q2 

0.628318530717959? 

sage: q == q2 

False 

sage: q != q2 

False 

sage: q2.lower() == q.lower() 

False 

sage: q - q2 

0.?e-15 

sage: q in q2 

True 

""" 

x = self._new() 

sig_on() 

mpfi_asin(x.value, self.value) 

sig_off() 

return x 

  

def arctan(self): 

""" 

Return the inverse tangent of ``self``. 

  

EXAMPLES:: 

  

sage: q = RIF.pi()/5; q 

0.6283185307179587? 

sage: i = q.tan(); i 

0.726542528005361? 

sage: q2 = i.arctan(); q2 

0.628318530717959? 

sage: q == q2 

False 

sage: q != q2 

False 

sage: q2.lower() == q.lower() 

False 

sage: q - q2 

0.?e-15 

sage: q in q2 

True 

""" 

x = self._new() 

sig_on() 

mpfi_atan(x.value, self.value) 

sig_off() 

return x 

  

def cosh(self): 

""" 

Return the hyperbolic cosine of ``self``. 

  

EXAMPLES:: 

  

sage: q = RIF.pi()/12 

sage: q.cosh() 

1.034465640095511? 

""" 

x = self._new() 

sig_on() 

mpfi_cosh(x.value, self.value) 

sig_off() 

return x 

  

def sinh(self): 

""" 

Return the hyperbolic sine of ``self``. 

  

EXAMPLES:: 

  

sage: q = RIF.pi()/12 

sage: q.sinh() 

0.2648002276022707? 

""" 

x = self._new() 

sig_on() 

mpfi_sinh(x.value, self.value) 

sig_off() 

return x 

  

def tanh(self): 

""" 

Return the hyperbolic tangent of ``self``. 

  

EXAMPLES:: 

  

sage: q = RIF.pi()/11 

sage: q.tanh() 

0.2780794292958503? 

""" 

x = self._new() 

sig_on() 

mpfi_tanh(x.value, self.value) 

sig_off() 

return x 

  

def arccosh(self): 

""" 

Return the hyperbolic inverse cosine of ``self``. 

  

EXAMPLES:: 

  

sage: q = RIF.pi()/2 

sage: i = q.arccosh() ; i 

1.023227478547551? 

""" 

x = self._new() 

sig_on() 

mpfi_acosh(x.value, self.value) 

sig_off() 

return x 

  

def arcsinh(self): 

""" 

Return the hyperbolic inverse sine of ``self``. 

  

EXAMPLES:: 

  

sage: q = RIF.pi()/7 

sage: i = q.sinh() ; i 

0.464017630492991? 

sage: i.arcsinh() - q 

0.?e-15 

""" 

x = self._new() 

sig_on() 

mpfi_asinh(x.value, self.value) 

sig_off() 

return x 

  

def arctanh(self): 

""" 

Return the hyperbolic inverse tangent of ``self``. 

  

EXAMPLES:: 

  

sage: q = RIF.pi()/7 

sage: i = q.tanh() ; i 

0.420911241048535? 

sage: i.arctanh() - q 

0.?e-15 

""" 

x = self._new() 

sig_on() 

mpfi_atanh(x.value, self.value) 

sig_off() 

return x 

  

# We implement some inverses in the obvious way (so they will 

# usually not be perfectly rounded). This gets us closer to the 

# API of RealField. 

  

def sec(self): 

r""" 

Return the secant of this number. 

  

EXAMPLES:: 

  

sage: RealIntervalField(100)(2).sec() 

-2.40299796172238098975460040142? 

""" 

return ~self.cos() 

  

def csc(self): 

r""" 

Return the cosecant of ``self``. 

  

EXAMPLES:: 

  

sage: RealIntervalField(100)(2).csc() 

1.099750170294616466756697397026? 

""" 

return ~self.sin() 

  

def cot(self): 

r""" 

Return the cotangent of ``self``. 

  

EXAMPLES:: 

  

sage: RealIntervalField(100)(2).cot() 

-0.457657554360285763750277410432? 

""" 

return ~self.tan() 

  

def sech(self): 

r""" 

Return the hyperbolic secant of ``self``. 

  

EXAMPLES:: 

  

sage: RealIntervalField(100)(2).sech() 

0.265802228834079692120862739820? 

""" 

return ~self.cosh() 

  

def csch(self): 

r""" 

Return the hyperbolic cosecant of ``self``. 

  

EXAMPLES:: 

  

sage: RealIntervalField(100)(2).csch() 

0.275720564771783207758351482163? 

""" 

return ~self.sinh() 

  

def coth(self): 

r""" 

Return the hyperbolic cotangent of ``self``. 

  

EXAMPLES:: 

  

sage: RealIntervalField(100)(2).coth() 

1.03731472072754809587780976477? 

""" 

return ~self.tanh() 

  

def arcsech(self): 

r""" 

Return the inverse hyperbolic secant of ``self``. 

  

EXAMPLES:: 

  

sage: RealIntervalField(100)(0.5).arcsech() 

1.316957896924816708625046347308? 

sage: (0.5).arcsech() 

1.31695789692482 

""" 

return (~self).arccosh() 

  

def arccsch(self): 

r""" 

Return the inverse hyperbolic cosecant of ``self``. 

  

EXAMPLES:: 

  

sage: RealIntervalField(100)(2).arccsch() 

0.481211825059603447497758913425? 

sage: (2.0).arccsch() 

0.481211825059603 

""" 

return (~self).arcsinh() 

  

def arccoth(self): 

r""" 

Return the inverse hyperbolic cotangent of ``self``. 

  

EXAMPLES:: 

  

sage: RealIntervalField(100)(2).arccoth() 

0.549306144334054845697622618462? 

sage: (2.0).arccoth() 

0.549306144334055 

""" 

return (~self).arctanh() 

  

def algdep(self, n): 

r""" 

Returns a polynomial of degree at most `n` which is 

approximately satisfied by ``self``. 

  

.. NOTE:: 

  

The returned polynomial need not be irreducible, and indeed usually 

won't be if ``self`` is a good approximation to an algebraic number 

of degree less than `n`. 

  

Pari needs to know the number of "known good bits" in the number; 

we automatically get that from the interval width. 

  

ALGORITHM: 

  

Uses the PARI C-library ``algdep`` command. 

  

EXAMPLES:: 

  

sage: r = sqrt(RIF(2)); r 

1.414213562373095? 

sage: r.algdep(5) 

x^2 - 2 

  

If we compute a wrong, but precise, interval, we get a wrong 

answer:: 

  

sage: r = sqrt(RealIntervalField(200)(2)) + (1/2)^40; r 

1.414213562374004543503461652447613117632171875376948073176680? 

sage: r.algdep(5) 

7266488*x^5 + 22441629*x^4 - 90470501*x^3 + 23297703*x^2 + 45778664*x + 13681026 

  

But if we compute an interval that includes the number we mean, 

we're much more likely to get the right answer, even if the 

interval is very imprecise:: 

  

sage: r = r.union(sqrt(2.0)) 

sage: r.algdep(5) 

x^2 - 2 

  

Even on this extremely imprecise interval we get an answer which is 

technically correct:: 

  

sage: RIF(-1, 1).algdep(5) 

x 

""" 

  

# If 0 is in the interval, then we have no known bits! But 

# fortunately, there's a perfectly valid answer we can 

# return anyway. 

if 0 in self: 

#import sage.rings.polynomial.polynomial_ring 

return sage.rings.polynomial.polynomial_ring.polygen( 

sage.rings.integer_ring.IntegerRing()) 

  

known_bits = -self.relative_diameter().log2() 

  

return sage.arith.all.algdep(self.center(), n, known_bits=known_bits) 

  

def factorial(self): 

""" 

Return the factorial evaluated on ``self``. 

  

EXAMPLES:: 

  

sage: RIF(5).factorial() 

120 

sage: RIF(2.3,5.7).factorial() 

1.?e3 

sage: RIF(2.3).factorial() 

2.683437381955768? 

  

Recover the factorial as integer:: 

  

sage: f = RealIntervalField(200)(50).factorial() 

sage: f 

3.0414093201713378043612608166064768844377641568960512000000000?e64 

sage: f.unique_integer() 

30414093201713378043612608166064768844377641568960512000000000000 

sage: 50.factorial() 

30414093201713378043612608166064768844377641568960512000000000000 

""" 

return (self+1).gamma() 

  

def gamma(self): 

""" 

Return the gamma function evaluated on ``self``. 

  

EXAMPLES:: 

  

sage: RIF(1).gamma() 

1 

sage: RIF(5).gamma() 

24 

sage: a = RIF(3,4).gamma(); a 

1.?e1 

sage: a.lower(), a.upper() 

(2.00000000000000, 6.00000000000000) 

sage: RIF(-1/2).gamma() 

-3.54490770181104? 

sage: gamma(-1/2).n(100) in RIF(-1/2).gamma() 

True 

sage: 0 in (RealField(2000)(-19/3).gamma() - RealIntervalField(1000)(-19/3).gamma()) 

True 

sage: gamma(RIF(100)) 

9.33262154439442?e155 

sage: gamma(RIF(-10000/3)) 

1.31280781451?e-10297 

  

Verify the result contains the local minima:: 

  

sage: 0.88560319441088 in RIF(1, 2).gamma() 

True 

sage: 0.88560319441088 in RIF(0.25, 4).gamma() 

True 

sage: 0.88560319441088 in RIF(1.4616, 1.46164).gamma() 

True 

  

sage: (-0.99).gamma() 

-100.436954665809 

sage: (-0.01).gamma() 

-100.587197964411 

sage: RIF(-0.99, -0.01).gamma().upper() 

-1.60118039970055 

  

Correctly detects poles:: 

  

sage: gamma(RIF(-3/2,-1/2)) 

[-infinity .. +infinity] 

""" 

x = self._new() 

if self > 1.462: 

# increasing 

mpfr_gamma(&x.value.left, &self.value.left, MPFR_RNDD) 

mpfr_gamma(&x.value.right, &self.value.right, MPFR_RNDU) 

return x 

elif self < 0: 

# Gamma(s) Gamma(1-s) = pi/sin(pi s) 

pi = self._parent.pi() 

return pi / ((self*pi).sin() * (1-self).gamma()) 

elif self.contains_zero(): 

# [-infinity, infinity] 

return ~self 

elif self < 1.461: 

# 0 < self as well, so decreasing 

mpfr_gamma(&x.value.left, &self.value.right, MPFR_RNDD) 

mpfr_gamma(&x.value.right, &self.value.left, MPFR_RNDU) 

return x 

else: 

# Worst case, this will recurse twice, as self is positive. 

return (1+self).gamma() / self 

  

def psi(self): 

""" 

Return the digamma function evaluated on self. 

  

INPUT: 

  

None. 

  

OUTPUT: 

  

A :class:`RealIntervalFieldElement`. 

  

EXAMPLES:: 

  

sage: psi_1 = RIF(1).psi() 

sage: psi_1 

-0.577215664901533? 

sage: psi_1.overlaps(-RIF.euler_constant()) 

True 

""" 

from sage.rings.real_arb import RealBallField 

return RealBallField(self.precision())(self).psi()._real_mpfi_(self._parent) 

  

def zeta(self, a=None): 

""" 

Return the image of this interval by the Hurwitz zeta function. 

  

For ``a = 1`` (or ``a = None``), this computes the Riemann zeta function. 

  

EXAMPLES:: 

  

sage: zeta(RIF(3)) 

1.202056903159594? 

sage: _.parent() 

Real Interval Field with 53 bits of precision 

sage: RIF(3).zeta(1/2) 

8.41439832211716? 

""" 

from sage.rings.real_arb import RealBallField 

return RealBallField(self.precision())(self).zeta(a).\ 

_real_mpfi_(self._parent) 

  

def _simplest_rational_test_helper(low, high, low_open=False, high_open=False): 

""" 

Call ``_simplest_rational_exact()``. Only used to allow doctests on 

that function. 

  

EXAMPLES:: 

  

sage: test = sage.rings.real_mpfi._simplest_rational_test_helper 

sage: test(1/4, 1/3, 0, 0) 

1/3 

""" 

return _simplest_rational_exact(low, high, low_open, high_open) 

  

cdef _simplest_rational_exact(Rational low, Rational high, int low_open, int high_open): 

""" 

Return the simplest rational between ``low`` and ``high``. May return 

``low`` or ``high`` unless ``low_open`` or ``high_open`` (respectively) are 

``True`` (non-zero). We assume that ``low`` and ``high`` are both 

nonnegative, and that ``high > low``. 

  

This is a helper function for 

:meth:`simplest_rational() <RealIntervalFieldElement.simplest_rational>` 

on :class:`RealIntervalField_class`, and should not be called directly. 

  

EXAMPLES:: 

  

sage: test = sage.rings.real_mpfi._simplest_rational_test_helper 

sage: test(1/4, 1/3, 0, 0) 

1/3 

sage: test(1/4, 1/3, 0, 1) 

1/4 

sage: test(1/4, 1/3, 1, 1) 

2/7 

sage: test(QQ(0), QQ(2), 0, 0) 

0 

sage: test(QQ(0), QQ(2), 1, 0) 

1 

sage: test(QQ(0), QQ(1), 1, 0) 

1 

sage: test(QQ(0), QQ(1), 1, 1) 

1/2 

sage: test(1233/1234, QQ(1), 0, 0) 

1 

sage: test(1233/1234, QQ(1), 0, 1) 

1233/1234 

sage: test(10000/32007, 10001/32007, 0, 0) 

289/925 

sage: test(QQ(0), 1/3, 1, 0) 

1/3 

sage: test(QQ(0), 1/3, 1, 1) 

1/4 

sage: test(QQ(0), 2/5, 1, 0) 

1/3 

sage: test(QQ(0), 2/5, 1, 1) 

1/3 

""" 

cdef Rational r 

  

if low < 1: 

if low == 0: 

if low_open: 

if high > 1: 

return Rational(1) 

inv_high = ~high 

if high_open: 

return ~Rational(inv_high.floor() + 1) 

else: 

return ~Rational(inv_high.ceil()) 

else: 

return Rational(0) 

  

if high > 1: 

return Rational(1) 

  

r = _simplest_rational_exact(~high, ~low, high_open, low_open) 

return ~r 

  

fl = low.floor() 

return fl + _simplest_rational_exact(low - fl, high - fl, low_open, high_open) 

  

  

def RealInterval(s, upper=None, int base=10, int pad=0, min_prec=53): 

r""" 

Return the real number defined by the string s as an element of 

``RealIntervalField(prec=n)``, where ``n`` potentially has 

slightly more (controlled by pad) bits than given by ``s``. 

  

INPUT: 

  

- ``s`` -- a string that defines a real number (or 

something whose string representation defines a number) 

  

- ``upper`` -- (default: ``None``) - upper endpoint of 

interval if given, in which case ``s`` is the lower endpoint 

  

- ``base`` -- an integer between 2 and 36 

  

- ``pad`` -- (default: 0) an integer 

  

- ``min_prec`` -- number will have at least this many 

bits of precision, no matter what 

  

  

EXAMPLES:: 

  

sage: RealInterval('2.3') 

2.300000000000000? 

sage: RealInterval(10) 

10 

sage: RealInterval('1.0000000000000000000000000000000000') 

1 

sage: RealInterval('1.2345678901234567890123456789012345') 

1.23456789012345678901234567890123450? 

sage: RealInterval(29308290382930840239842390482, 3^20).str(style='brackets') 

'[3.48678440100000000000000000000e9 .. 2.93082903829308402398423904820e28]' 

  

TESTS: 

  

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

sufficient precision (:trac:`10164`). This is a little tricky 

because at the time of writing, we don't support intervals long 

enough to trip the error. However, at least we can make sure 

that we either do it correctly or fail noisily:: 

  

sage: ks = 5*10**5, 10**6 

sage: for k in ks: 

....: try: 

....: z = RealInterval("1." + "1"*k) 

....: assert len(str(z))-4 >= k 

....: except TypeError: 

....: pass 

  

""" 

if not isinstance(s, str): 

s = str(s) 

if base == 10: 

# hard-code the common case 

bits = int(LOG_TEN_TWO_PLUS_EPSILON*len(s)) 

else: 

bits = int(math.log(base,2)*1.00001*len(s)) 

R = RealIntervalField(prec=max(bits+pad, min_prec)) 

if upper is not None: 

s = (s, upper) 

return RealIntervalFieldElement(R, s, base) 

  

  

# The default real interval field, with precision 53 bits 

RIF = RealIntervalField() 

  

def is_RealIntervalField(x): 

""" 

Check if ``x`` is a :class:`RealIntervalField_class`. 

  

EXAMPLES:: 

  

sage: sage.rings.real_mpfi.is_RealIntervalField(RIF) 

True 

sage: sage.rings.real_mpfi.is_RealIntervalField(RealIntervalField(200)) 

True 

""" 

return isinstance(x, RealIntervalField_class) 

  

def is_RealIntervalFieldElement(x): 

""" 

Check if ``x`` is a :class:`RealIntervalFieldElement`. 

  

EXAMPLES:: 

  

sage: sage.rings.real_mpfi.is_RealIntervalFieldElement(RIF(2.2)) 

True 

sage: sage.rings.real_mpfi.is_RealIntervalFieldElement(RealIntervalField(200)(2.2)) 

True 

""" 

return isinstance(x, RealIntervalFieldElement) 

  

  

#### pickle functions 

def __create__RealIntervalField_version0(prec, sci_not): 

""" 

For pickling. 

  

EXAMPLES:: 

  

sage: sage.rings.real_mpfi.__create__RealIntervalField_version0(53, False) 

Real Interval Field with 53 bits of precision 

""" 

return RealIntervalField(prec, sci_not) 

  

## Keep all old versions!!! 

def __create__RealIntervalFieldElement_version0(parent, x, base=10): 

""" 

For pickling. 

  

EXAMPLES:: 

  

sage: sage.rings.real_mpfi.__create__RealIntervalFieldElement_version0(RIF, 2.2) 

2.2000000000000002? 

""" 

return RealIntervalFieldElement(parent, x, base=base) 

  

def __create__RealIntervalFieldElement_version1(parent, lower, upper): 

""" 

For pickling. 

  

EXAMPLES:: 

  

sage: sage.rings.real_mpfi.__create__RealIntervalFieldElement_version1(RIF, 2.225, 2.227) 

2.226? 

""" 

return parent([lower, upper])