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
#![no_std]
use core::ops::{Add, Sub};
/// Low-level bindings to double-precision Cephes functions.
///
/// This provides access to the C function implemented in Cephes for maximum
/// flexibility.
#[allow(dead_code)]
pub mod unsafe_cephes_double {
extern "C" {
// Floating point numeric utilities
/// Round to nearest or event integer valued f64.
pub fn round(x: f64) -> f64;
/// Largest integer less than or equal to x.
pub fn floor(x: f64) -> f64;
/// Smallest integer greater than or equal to x.
pub fn ceil(x: f64) -> f64;
/// Return the significand between 0.5 and 1. Write exponent to expnt.
/// x = y * 2**expn
pub fn frexp(x: f64, expnt: &mut i32) -> f64;
/// Multiply x by 2**n.
pub fn ldexp(x: f64, n: i32) -> f64;
/// Absolute value.
pub fn fabs(x: f64) -> f64;
/// Return 1 if the sign bit of x is 1, else 0.
pub fn signbit(x: f64) -> i32;
/// Return 1 if x is NaN, else 0.
pub fn isnan(x: f64) -> i32;
/// Return 1 if x is finite, else 0.
pub fn isfinite(x: f64) -> i32;
// Roots
/// Cube root.
pub fn cbrt(x: f64) -> f64;
/// Square root.
pub fn sqrt(x: f64) -> f64;
/// Integer square root.
pub fn lsqrt(x: i64) -> i64;
// Exponential functions
/// Exponential function.
pub fn exp(x: f64) -> f64;
/// Base 10 exponential function.
pub fn exp10(x: f64) -> f64;
/// Base 2 exponential function.
pub fn exp2(x: f64) -> f64;
/// Compute accurately exponential of squared argument.
pub fn expm1(x: f64) -> f64;
/// Compute accurately exp(x) - 1 for x close to 0.
pub fn expx2(x: f64, sign: i32) -> f64;
/// Exponential integral.
pub fn ei(x: f64) -> f64;
/// Error function.
pub fn erf(x: f64) -> f64;
/// Complementary error function.
pub fn erfc(x: f64) -> f64;
/// Power function.
pub fn pow(x: f64, y: f64) -> f64;
/// Integer power function.
pub fn powi(x: f64, n: i32) -> f64;
// Logarithmic functions
/// Natural logarithm.
pub fn log(x: f64) -> f64;
/// Common logarithm.
pub fn log10(x: f64) -> f64;
/// Base 2 logarithm.
pub fn log2(x: f64) -> f64;
/// Compute accurately log(1 + x) for x close to 0.
pub fn log1p(x: f64) -> f64;
/// Dilogarithm (Spence's function).
pub fn spence(x: f64) -> f64;
// Trigonometric functions
/// Circular sine.
pub fn sin(x: f64) -> f64;
/// Circular cosine.
pub fn cos(x: f64) -> f64;
/// Circular tangent.
pub fn tan(x: f64) -> f64;
/// Inverse circular sine.
pub fn asin(x: f64) -> f64;
/// Inverse circular cosine.
pub fn acos(x: f64) -> f64;
/// Inverse circular tangent.
pub fn atan(x: f64) -> f64;
/// Quadrant-correct inverse circular tangent.
pub fn atan2(y: f64, x: f64) -> f64;
/// Compute accurately cos(x) - 1 for x close to 0.
pub fn cosm1(x: f64) -> f64;
/// Sine and cosine integrals.
pub fn sici(x: f64, si: &mut f64, ci: &mut f64) -> i32;
// Hyperbolic functions
/// Hyperbolic sine.
pub fn sinh(x: f64) -> f64;
/// Hyperbolic cosine.
pub fn cosh(x: f64) -> f64;
/// Hyperbolic tangent.
pub fn tanh(x: f64) -> f64;
/// Inverse hyperbolic sine.
pub fn asinh(x: f64) -> f64;
/// Inverse hyperbolic cosine.
pub fn acosh(x: f64) -> f64;
/// Inverse hyperbolic tangent.
pub fn atanh(x: f64) -> f64;
/// Hyperbolic sine and cosine integrals.
pub fn shichi(x: f64, chi: &mut f64, shi: &mut f64) -> i32;
// Beta functions
/// Beta function.
pub fn beta(a: f64, b: f64) -> f64;
/// Regularized incomplete beta function.
pub fn incbet(a: f64, b: f64, x: f64) -> f64;
/// Inverse of incomplete beta integral.
pub fn incbi(a: f64, b: f64, y: f64) -> f64;
// Gamma functions
/// Gamma function.
pub fn gamma(x: f64) -> f64;
/// Reciprocal gamma function.
pub fn rgamma(x: f64) -> f64;
/// Natural logarithm of gamma function.
pub fn lgam(x: f64) -> f64;
/// Regularized incomplete gamma integral.
pub fn igam(a: f64, x: f64) -> f64;
/// Complemented incomplete gamma integral.
pub fn igamc(a: f64, x: f64) -> f64;
/// Inverse of complemented incomplete gamma integral.
pub fn igami(a: f64, p: f64) -> f64;
/// Psi (digamma) function.
pub fn psi(x: f64) -> f64;
/// Factorial function.
pub fn fac(i: i32) -> f64;
// Bessel functions
/// Bessel function of order zero.
pub fn j0(x: f64) -> f64;
/// Bessel function of order one.
pub fn j1(x: f64) -> f64;
/// Bessel function of integer order.
pub fn jn(n: i32, x: f64) -> f64;
/// Bessel function of real order.
pub fn jv(n: f64, x: f64) -> f64;
/// Bessel function of the second kind, order zero.
pub fn y0(x: f64) -> f64;
/// Bessel function of the second kind, order one.
pub fn y1(x: f64) -> f64;
/// Bessel function of the second kind, integer order.
pub fn yn(n: i32, x: f64) -> f64;
/// Bessel function of the second kind, real order.
pub fn yv(v: f64, x: f64) -> f64;
/// Modified Bessel function of order zero.
pub fn i0(x: f64) -> f64;
/// Modified Bessel function of order zero, exponentially scaled.
pub fn i0e(x: f64) -> f64;
/// Modified Bessel function of order one.
pub fn i1(x: f64) -> f64;
/// Modified Bessel function of order one, exponentially scaled.
pub fn i1e(x: f64) -> f64;
/// Modified Bessel function of real order.
pub fn iv(v: f64, x: f64) -> f64;
/// Modified Bessel function of the third kind, order zero.
pub fn k0(x: f64) -> f64;
/// Modified Bessel function of the third kind, order zero,
/// exponentially scaled.
pub fn k0e(x: f64) -> f64;
/// Modified Bessel function of the third kind, order one.
pub fn k1(x: f64) -> f64;
/// Modified Bessel function of the third kind, order one,
/// exponentially scaled.
pub fn k1e(x: f64) -> f64;
/// Modified Bessel function of the third kind, integer order.
pub fn kn(n: i32, x: f64) -> f64;
// Elliptic functions
/// Incomplete elliptic integral of the first kind.
pub fn ellik(phi: f64, m: f64) -> f64;
/// Incomplete elliptic integral of the second kind.
pub fn ellie(phi: f64, m: f64) -> f64;
/// Complete elliptic integral of the first kind.
pub fn ellpk(m1: f64) -> f64;
/// Complete elliptic integral of the second kind.
pub fn ellpe(m1: f64) -> f64;
/// Jacobian elliptic function.
pub fn ellpj(u: f64, m: f64, sn: &mut f64, cn: &mut f64, dn: &mut f64, phi: &mut f64) -> i32;
// Hypergeometric functions
/// Confluent hypergeometric function 1F1.
pub fn hyperg(a: f64, b: f64, x: f64) -> f64;
/// Hypergeometric function 1F2.
pub fn onef2(a: f64, b: f64, c: f64, x: f64, err: &mut f64) -> f64;
/// Gauss hypergeometric function 2F1.
pub fn hyp2f1(a: f64, b: f64, c: f64, x: f64) -> f64;
/// Hypergeometric function 3F0.
pub fn threef0(a: f64, b: f64, c: f64, x: f64, err: &mut f64) -> f64;
// Distributions
/// Binomial distribution.
pub fn bdtr(k: i32, n: i32, p: f64) -> f64;
/// Complemented binomial distribution.
pub fn bdtrc(k: i32, n: i32, p: f64) -> f64;
/// Inverse of binomial distribution.
pub fn bdtri(k: i32, n: i32, y: f64) -> f64;
/// Negative binomial distribution.
pub fn nbdtr(k: i32, n: i32, p: f64) -> f64;
/// Complemented negative binomial distribution.
pub fn nbdtrc(k: i32, n: i32, p: f64) -> f64;
/// Inverse of negative binomial distribution.
pub fn nbdtri(k: i32, n: i32, p: f64) -> f64;
/// Beta distribution.
pub fn btdtr(a: f64, b: f64, x: f64) -> f64;
/// Chi-square distribution.
pub fn chdtr(df: f64, x: f64) -> f64;
/// Complemented chi-square distribution.
pub fn chdtrc(v: f64, x: f64) -> f64;
/// Inverse of complemented chi-square distribution.
pub fn chdtri(df: f64, y: f64) -> f64;
/// F distribution.
pub fn fdtr(df1: i32, df2: i32, x: f64) -> f64;
/// Complemented F distribution.
pub fn fdtrc(df1: i32, df2: i32, x: f64) -> f64;
/// Inverse of complemented F distribution.
pub fn fdtri(df1: i32, df2: i32, p: f64) -> f64;
/// Gamma distribution.
pub fn gdtr(a: f64, b: f64, x: f64) -> f64;
/// Complemented gamma distribution.
pub fn gdtrc(a: f64, b: f64, x: f64) -> f64;
/// Normal distribution.
pub fn ndtr(x: f64) -> f64;
/// Inverse of normal distribution.
pub fn ndtri(y: f64) -> f64;
/// Poisson distribution.
pub fn pdtr(k: i32, m: f64) -> f64;
/// Complemented Poisson distribution.
pub fn pdtrc(k: i32, m: f64) -> f64;
/// Inverse of Poisson distribution.
pub fn pdtri(k: i32, y: f64) -> f64;
/// Student's t distribution.
pub fn stdtr(k: i16, t: f64) -> f64;
/// Inverse of Student's t distribution.
pub fn stdtri(k: i32, p: f64) -> f64;
// Misc special functions
/// Airy function.
pub fn airy(x: f64, ai: &mut f64, aip: &mut f64, bi: &mut f64, bip: &mut f64) -> i32;
/// Dawson's integral.
pub fn dawsn(x: f64) -> f64;
/// Fresnel integral.
pub fn fresnl(x: f64, s: &mut f64, c: &mut f64);
/// Integral of Planck's black body radiation formula.
pub fn plancki(lambda: f64, temperature: f64) -> f64;
/// Struve function.
pub fn struve(v: f64, x: f64) -> f64;
/// Riemann zeta function.
pub fn zetac(x: f64) -> f64;
/// Riemann zeta function of two arguments.
pub fn zeta(x: f64, q: f64) -> f64;
}
}
/// High-level bindings to double-precision Cephes functions.
///
/// This provides safe access to a subset of the Cephes functions.
pub mod cephes_double {
use unsafe_cephes_double;
/// Function to compute the base-10 exponential of x
///
/// # Original Description from Stephen L. Moshier
/// Returns 10 raised to the x power.
///
/// Range reduction is accomplished by expressing the argument
/// as 10^x = 2^n 10^f, with |f| < 0.5 log10(2).
/// The Pade' form
///
/// 10^x = 1 + 2x P( x^2)/( Q( x^2 ) - P( x^2 ) )
///
/// is used to approximate 10^f.
///
/// ACCURACY (Relative error):
///
/// | arithmetic | domain | # trials | peak | rms |
/// | :--------: | :----: | :-------: | :---: | :--: |
/// | IEEE | -307,+307 | 30000 | 2.2e-16 | 5.5e-17 |
///
/// ERROR MESSAGES:
///
/// | message | condition | value returned |
/// | :-----: | :-------: | :------------: |
/// | exp10 underflow | x < -MAXL10 | 0.0 |
/// | exp10 overflow | x > MAXL10 | MAXNUM |
///
/// IEEE arithmetic: MAXL10 = 308.2547155599167.
///
///
/// # Arguments
/// * `x`: A double precision number
///
/// # Example
/// ```
/// use special_fun::cephes_double::exp10;
/// let x = 1.0f64;
/// exp10(x);
/// ```
pub fn exp10(x: f64) -> f64 {
unsafe { unsafe_cephes_double::exp10(x) }
}
/// Function to accurately compute `exp(x) - 1` for small x
///
/// # Arguments
/// * `x`: A double precision number
///
/// # Example
/// ```
/// use special_fun::cephes_double::expm1;
/// let x = 1.0f64;
/// expm1(x);
/// ```
pub fn expm1(x: f64) -> f64 {
unsafe { unsafe_cephes_double::expm1(x) }
}
/// Function to accurately compute the exponential of a squared argument
///
/// # Original Description from Stephen L. Moshier
///
/// Computes y = exp(x*x) while suppressing error amplification
/// that would ordinarily arise from the inexactness of the
/// exponential argument x*x.
///
/// If sign < 0, the result is inverted; i.e., y = exp(-x*x) .
///
/// ACCURACY (Relative error):
///
/// | arithmetic | domain | # trials | peak | rms |
/// | :--------: | :----: | :------: | :--: | :--:|
/// | IEEE | -26.6, 26.6 | 10^7 | 3.9e-16 | 8.9e-17 |
///
/// # Arguments
/// * `x`: A double precision number
/// * 'sign': An integer representing the sign of the exponent
///
/// # Example
/// ```
/// use special_fun::cephes_double::expx2;
/// let x = 1.0f64;
/// expx2(x, -1);
/// ```
pub fn expx2(x: f64, sign: i32) -> f64 {
unsafe { unsafe_cephes_double::expx2(x, sign) }
}
/// Function to accurately compute the exponential integral of x
///
/// The exponential integral is given by:
///
/// $Ei(x) = -\int^{\infty}_{-x} \frac{e^{-t}}{t} dt$
///
/// # Original Description from Stephen L. Moshier
///
/// ACCURACY (Relative error):
///
/// | arithmetic | domain | # trials | peak | rms |
/// | :--------: | :----: | :------: | :--: | :--:|
/// | IEEE | 0, 100 | 50000 | 8.6e-16 | 1.3e-16 |
///
/// # Arguments
/// * `x`: A double precision number
///
/// # Example
/// ```
/// use special_fun::cephes_double::ei;
/// let x = 1.0f64;
/// ei(x);
/// ```
pub fn ei(x: f64) -> f64 {
unsafe { unsafe_cephes_double::ei(x) }
}
/// Function to accurately compute the error function of x
///
/// The error function is given by:
///
/// $Erf(x) = \frac{2}{\sqrt{pi}} -\int^{x}_{0} e^{-t^2} dt$
///
/// # Arguments
/// * `x`: A double precision number
///
/// # Example
/// ```
/// use special_fun::cephes_double::erf;
/// let x = 1.0f64;
/// erf(x);
/// ```
pub fn erf(x: f64) -> f64 {
unsafe { unsafe_cephes_double::erf(x) }
}
/// Function to accurately compute the complementary error function of x
///
/// The error function is given by:
///
/// $Erf(x) = 1 + \frac{2}{\sqrt{pi}} -\int^{x}_{0} e^{-t^2} dt$
///
/// # Arguments
/// * `x`: A double precision number
///
/// # Example
/// ```
/// use special_fun::cephes_double::erfc;
/// let x = 1.0f64;
/// erfc(x);
/// ```
pub fn erfc(x: f64) -> f64 {
unsafe { unsafe_cephes_double::erfc(x) }
}
/// Function to accurately compute `log(1 + x)`
///
/// # Arguments
/// * `x`: A double precision number
///
/// # Example
/// ```
/// use special_fun::cephes_double::log1p;
/// let x = 0.1f64;
/// log1p(x);
/// ```
pub fn log1p(x: f64) -> f64 {
unsafe { unsafe_cephes_double::log1p(x) }
}
/// Function to accurately compute the dilogarithm function
///
/// Spence's function is a special case of the polylogarithm function, and
/// is defined as:
///
/// $ Li_2(x) = \int^x_1 \frac{ln(t)}{t - 1} du
///
/// Note, this implies that the domain has been shifted by 1 from the
/// standard definition (as per wikipedia) of:
///
/// $ Li_2(x) = - \int^x_0 \frac{ ln(1 - t) }{ t } dt
///
/// # Original Description from Stephen L. Moshier
/// Computes the integral for x >= 0. A rational approximation gives the
/// integral in the interval (0.5, 1.5). Transformation formulas for 1/x
/// and 1-x are employed outside the basic expansion range.
///
/// ACCURACY(Relative error):
///
/// | arithmetic | domain | # trials | peak | rms |
/// | :---: | :--: | :--: | :--: | :--: | :--: |
/// | IEEE | 0,4 | 30000 | 3.9e-15 | 5.4e-16 |
///
/// # Arguments
/// * `x`: A double precision number
///
/// # Example
/// ```
/// use special_fun::cephes_double::spence;
/// let x = 0.1f64;
/// spence(x);
/// ```
pub fn spence(x: f64) -> f64 {
unsafe { unsafe_cephes_double::spence(x) }
}
/// Function to accurately compute `cos(x) - 1` for small x
///
/// # Arguments
/// * `x`: A double precision number
///
/// # Example
/// ```
/// use special_fun::cephes_double::cosm1;
/// let x = 0.1f64;
/// cosm1(x);
/// ```
pub fn cosm1(x: f64) -> f64 {
unsafe { unsafe_cephes_double::cosm1(x) }
}
/// Function to accurately compute sine and cosine integrals
///
/// The sine integral is defined as:
///
/// $ Si(x) = \int_0^{\infty} \frac{ sin(t) }{t} dt $
///
/// and the cosine integral is defined as:
///
/// $ Ci(x) = -\int_x^{\infty} \frac{ cos(t) }{t} dt $
///
/// which reduces to:
///
/// $ Ci(x) = \gamma + ln(x) + \int_0^x \frac{cos(t) - 1}{t} dt $
///
/// where $\gamma$ is the euler constant.
///
/// # Original Description from Stephen L. Moshier
/// The integrals are approximated by rational functions.
/// For x > 8 auxiliary functions f(x) and g(x) are employed
/// such that
///
/// Ci(x) = f(x) sin(x) - g(x) cos(x)
/// Si(x) = pi/2 - f(x) cos(x) - g(x) sin(x)
///
/// ACCURACY(Absolute error, except relative when > 1):
///
/// | arithmetic | Function | domain | # trials | peak | rms |
/// | :---: | :--: | :--: | :--: | :--: | :--: | :--: |
/// | IEEE | Si | 0,50 | 30000 | 4.4e-16 | 7.3e-17 |
/// | IEEE | Ci | 0,50 | 30000 | 6.9e-16 | 5.1e-17 |
///
/// # Arguments
/// * `x`: A double precision number
/// * `Si`: A mutable double precision number that will return the sine
/// integral value
/// * `Ci`: A mutable double precision number that will return the cosine
/// integral value
///
/// # Example
/// ```
/// use special_fun::cephes_double::sici;
/// let x = 0.1f64;
/// let mut si = 0.0_f64;
/// let mut ci = 0.0_f64;
/// let mut code = 0.0_f64;
/// sici(0.5_f64, &mut si, &mut ci);
/// ```
pub fn sici(x: f64, si: &mut f64, ci: &mut f64) {
let code = unsafe { unsafe_cephes_double::sici(x, si, ci) };
assert_eq!(code, 0);
}
/// Function to accurately compute hyperbolic sine and cosine integrals
///
/// The hyperbolic sine integral is defined as:
///
/// $ Shi(x) = \int_0^{\infty} \frac{ sinh(t) }{t} dt $
///
/// and the hyperbolic cosine integral is defined as:
///
/// $ Chi(x) = -\int_x^{\infty} \frac{ cosh(t) }{t} dt $
///
/// which reduces to:
///
/// $ Chi(x) = \gamma + ln(x) + \int_0^x \frac{cosh(t) - 1}{t} dt $
///
/// where $\gamma$ is the euler constant.
///
/// # Original Description from Stephen L. Moshier
/// The integrals are approximated by rational functions.
/// For x > 8 auxiliary functions f(x) and g(x) are employed
/// such that
///
/// Ci(x) = f(x) sin(x) - g(x) cos(x)
/// Si(x) = pi/2 - f(x) cos(x) - g(x) sin(x)
///
/// ACCURACY(Relative error)):
///
/// | arithmetic | Function | domain | # trials | peak | rms |
/// | :---: | :--: | :--: | :--: | :--: | :--: | :--: |
/// | IEEE | Shi | 0,88 | 30000 | 6.9-16 | 1.6e-16 |
///
/// ACCURACY(Absolute error, except relative when |Chi| > 1):
///
/// | arithmetic | Function | domain | # trials | peak | rms |
/// | :---: | :--: | :--: | :--: | :--: | :--: | :--: |
/// | IEEE | Chi | 0,88 | 30000 | 8.4e-16 | 1.4e-16 |
///
/// # Arguments
/// * `x`: A double precision number
/// * `Shi`: A mutable double precision number that will return the
/// hyperbolic sine integral value
/// * `Chi`: A mutable double precision number that will return the
/// hyperbolic cosine integral value
///
/// # Example
/// ```
/// use special_fun::cephes_double::shichi;
/// let x = 0.1f64;
/// let mut shi = 0.0_f64;
/// let mut chi = 0.0_f64;
/// shichi(0.5_f64, &mut shi, &mut chi);
/// ```
pub fn shichi(x: f64, chi: &mut f64, shi: &mut f64){
let code = unsafe { unsafe_cephes_double::shichi(x, chi, shi) };
assert_eq!(code, 0);
}
/// Function to compute the beta function.
///
/// The beta function is given by:
///
/// $B(a, b) = \int_0^1 t^{a - 1} ( 1 - t )^{b - 1} dt $
///
/// which simplifies to
///
/// $ B(a, b) = \frac{ \Gamma(a) \Gamma(b)}{ \Gamma(a + b) } $
///
/// # Original Description from Stephen L. Moshier
/// For large arguments the logarithm of the function is
/// evaluated using lgam(), then exponentiated.
///
/// ACCURACY (Relative error):
///
/// | arithmetic | domain | # trials | peak | rms |
/// | :--------: | :----: | :------: | :--: | :-: |
/// | IEEE | 0,30 | 30000 | 8.1e-14 | 1.1e-14 |
///
/// ERROR MESSAGES:
///
/// | message | condition | value returned |
/// | :-------: | :--------: | :----------: |
/// | beta overflow | log(beta) > MAXLOG | 0.0 |
/// | beta overflow | a or b <0 integer | 0.0 |
///
/// # Arguments
/// * `a`: A double precision parameter, assumed to be greater than 0
/// * `b`: A double precision parameter, assumed to be greater than 0
///
/// # Example
/// ```
/// use special_fun::cephes_double::beta;
/// let a = 0.1f64;
/// let b = 0.2f64;
/// beta(a, b);
/// ```
pub fn beta(a: f64, b: f64) -> f64 {
unsafe { unsafe_cephes_double::beta(a, b) }
}
/// Function to compute the regularized incomplete beta function.
///
/// The regularized incomplete beta function is given by:
///
/// $I_x(a, b) = \frac{1}{B(a, b)} \int_0^x t^{a - 1} ( 1 - t )^{b - 1} dt $
///
/// and for $x = 1$, the regularized incomplete beta function evaluates to
/// exactly 1. Note, $ 1 - I_x(a, b) = I_{1 - x}(a, b)$.
///
/// # Original Description from Stephen L. Moshier
/// The integral is evaluated by a continued fraction expansion
/// or, when b*x is small, by a power series.
///
/// ACCURACY (Relative error):
///
/// | arithmetic | domain | # trials | peak | rms |
/// | :--------: | :----: | :------: | :--: | :-: |
/// | IEEE | 0,5 | 10000 | 6.9e-15 | 4.5e-16 |
/// | IEEE | 0,85 | 250000 | 2.2e-13 | 1.7e-14 |
/// | IEEE | 0,1000 | 30000 | 5.3e-12 | 6.3e-13 |
/// | IEEE | 0,10000 | 250000 | 9.3e-11 | 7.1e-12 |
/// | IEEE | 0,100000 | 10000 | 8.7e-10 | 4.8e-11 |
///
/// ERROR MESSAGES:
///
/// | message | condition | value returned |
/// | :-------: | :--------: | :----------: |
/// | incbet domain | x<0, x>1 | 0.0 |
/// | incbet underflow | - | 0.0 |
///
/// # Arguments
/// * `a`: A double precision parameter, assumed to be greater than 0
/// * `b`: A double precision parameter, assumed to be greater than 0
/// * `x`: A double precision parameter, assumed to be between 0 and 1
///
/// # Example
/// ```
/// use special_fun::cephes_double::incbet;
/// let a = 0.1f64;
/// let b = 0.2f64;
/// incbet(a, b, 0.5f64);
/// ```
pub fn incbet(a: f64, b: f64, x: f64) -> f64 {
unsafe { unsafe_cephes_double::incbet(a, b, x) }
}
/// Inverse of incomplete beta integral.
pub fn incbi(a: f64, b: f64, y: f64) -> f64 {
unsafe { unsafe_cephes_double::incbi(a, b, y) }
}
/// Gamma function.
pub fn gamma(x: f64) -> f64 {
unsafe { unsafe_cephes_double::gamma(x) }
}
/// Reciprocal gamma function.
pub fn rgamma(x: f64) -> f64 {
unsafe { unsafe_cephes_double::rgamma(x) }
}
/// Natural logarithm of gamma function.
pub fn lgam(x: f64) -> f64 {
unsafe { unsafe_cephes_double::lgam(x) }
}
/// Regularized incomplete gamma integral.
pub fn igam(a: f64, x: f64) -> f64 {
unsafe { unsafe_cephes_double::igam(a, x) }
}
/// Complemented incomplete gamma integral.
pub fn igamc(a: f64, x: f64) -> f64 {
unsafe { unsafe_cephes_double::igamc(a, x) }
}
/// Inverse of complemented incomplete gamma integral.
pub fn igami(a: f64, p: f64) -> f64 {
unsafe { unsafe_cephes_double::igami(a, p) }
}
/// Psi (digamma) function.
pub fn psi(x: f64) -> f64 {
unsafe { unsafe_cephes_double::psi(x) }
}
/// Factorial function.
pub fn fac(i: i32) -> f64 {
unsafe { unsafe_cephes_double::fac(i) }
}
}
/// Low-level bindings to single-precision Cephes functions.
///
/// This provides access to the C function implemented in Cephes for maximum
/// flexibility. Note that Cephes implements less functions for single than for
/// double precision.
#[allow(dead_code)]
pub mod unsafe_cephes_single {
extern "C" {
// Floating point numeric utilities
/// Round to nearest or event integer valued f32.
pub fn roundf(x: f32) -> f32;
/// Largest integer less than or equal to x.
pub fn floorf(x: f32) -> f32;
/// Smallest integer greater than or equal to x.
pub fn ceilf(x: f32) -> f32;
/// Return the significand between 0.5 and 1. Write exponent to expnt.
/// x = y * 2**expn
pub fn frexpf(x: f32, expnt: &mut i32) -> f32;
/// Multiply x by 2**n.
pub fn ldexpf(x: f32, n: i32) -> f32;
/// Absolute value.
pub fn fabsf(x: f32) -> f32;
/// Return 1 if the sign bit of x is 1, else 0.
pub fn signbitf(x: f32) -> i32;
/// Return 1 if x is NaN, else 0.
pub fn isnanf(x: f32) -> i32;
/// Return 1 if x is finite, else 0.
pub fn isfinitef(x: f32) -> i32;
// Roots
/// Cube root.
pub fn cbrtf(x: f32) -> f32;
/// Square root.
pub fn sqrtf(x: f32) -> f32;
/// Integer square root.
pub fn lsqrtf(x: i64) -> i64;
// Exponential functions
/// Exponential function.
pub fn expf(x: f32) -> f32;
/// Base 10 exponential function.
pub fn exp10f(x: f32) -> f32;
/// Base 2 exponential function.
pub fn exp2f(x: f32) -> f32;
/// Compute accurately exponential of squared argument.
pub fn expm1f(x: f32) -> f32;
/// Compute accurately exp(x) - 1 for x close to 0.
pub fn expx2f(x: f32, sign: i32) -> f32;
/// Error function.
pub fn erff(x: f32) -> f32;
/// Complementary error function.
pub fn erfcf(x: f32) -> f32;
/// Power function.
pub fn powf(x: f32, y: f32) -> f32;
/// Integer power function.
pub fn powif(x: f32, n: i32) -> f32;
// Logarithmic functions
/// Natural logarithm.
pub fn logf(x: f32) -> f32;
/// Common logarithm.
pub fn log10f(x: f32) -> f32;
/// Base 2 logarithm.
pub fn log2f(x: f32) -> f32;
/// Compute accurately log(1 + x) for x close to 0.
pub fn log1pf(x: f32) -> f32;
/// Dilogarithm (Spence's function).
pub fn spencef(x: f32) -> f32;
// Trigonometric functions
/// Circular sine.
pub fn sinf(x: f32) -> f32;
/// Circular cosine.
pub fn cosf(x: f32) -> f32;
/// Circular tangent.
pub fn tanf(x: f32) -> f32;
/// Inverse circular sine.
pub fn asinf(x: f32) -> f32;
/// Inverse circular cosine.
pub fn acosf(x: f32) -> f32;
/// Inverse circular tangent.
pub fn atanf(x: f32) -> f32;
/// Quadrant-correct inverse circular tangent.
pub fn atan2f(y: f32, x: f32) -> f32;
/// Sine and cosine integrals.
pub fn sicif(x: f32, si: &mut f32, ci: &mut f32) -> i32;
// Hyperbolic functions
/// Hyperbolic sine.
pub fn sinhf(x: f32) -> f32;
/// Hyperbolic cosine.
pub fn coshf(x: f32) -> f32;
/// Hyperbolic tangent.
pub fn tanhf(x: f32) -> f32;
/// Inverse hyperbolic sine.
pub fn asinhf(x: f32) -> f32;
/// Inverse hyperbolic cosine.
pub fn acoshf(x: f32) -> f32;
/// Inverse hyperbolic tangent.
pub fn atanhf(x: f32) -> f32;
/// Hyperbolic sine and cosine integrals.
pub fn shichif(x: f32, chi: &mut f32, shi: &mut f32) -> i32;
// Beta functions
/// Beta function.
pub fn betaf(a: f32, b: f32) -> f32;
/// Regularized incomplete beta function.
pub fn incbetf(a: f32, b: f32, x: f32) -> f32;
/// Inverse of incomplete beta integral.
pub fn incbif(a: f32, b: f32, y: f32) -> f32;
// Gamma functions
/// Gamma function.
pub fn gammaf(x: f32) -> f32;
/// Reciprocal gamma function.
pub fn rgammaf(x: f32) -> f32;
/// Natural logarithm of gamma function.
pub fn lgamf(x: f32) -> f32;
/// Regularized incomplete gamma integral.
pub fn igamf(a: f32, x: f32) -> f32;
/// Complemented incomplete gamma integral.
pub fn igamcf(a: f32, x: f32) -> f32;
/// Inverse of complemented incomplete gamma integral.
pub fn igamif(a: f32, p: f32) -> f32;
/// Psi (digamma) function.
pub fn psif(x: f32) -> f32;
/// Factorial function.
pub fn facf(i: i32) -> f32;
// Bessel functions
/// Bessel function of order zero.
pub fn j0f(x: f32) -> f32;
/// Bessel function of order one.
pub fn j1f(x: f32) -> f32;
/// Bessel function of integer order.
pub fn jnf(n: i32, x: f32) -> f32;
/// Bessel function of real order.
pub fn jvf(n: f32, x: f32) -> f32;
/// Bessel function of the second kind, order zero.
pub fn y0f(x: f32) -> f32;
/// Bessel function of the second kind, order one.
pub fn y1f(x: f32) -> f32;
/// Bessel function of the second kind, integer order.
pub fn ynf(n: i32, x: f32) -> f32;
/// Bessel function of the second kind, real order.
pub fn yvf(v: f32, x: f32) -> f32;
/// Modified Bessel function of order zero.
pub fn i0f(x: f32) -> f32;
/// Modified Bessel function of order zero, exponentially scaled.
pub fn i0ef(x: f32) -> f32;
/// Modified Bessel function of order one.
pub fn i1f(x: f32) -> f32;
/// Modified Bessel function of order one, exponentially scaled.
pub fn i1ef(x: f32) -> f32;
/// Modified Bessel function of real order.
pub fn ivf(v: f32, x: f32) -> f32;
/// Modified Bessel function of the third kind, order zero.
pub fn k0f(x: f32) -> f32;
/// Modified Bessel function of the third kind, order zero,
/// exponentially scaled.
pub fn k0ef(x: f32) -> f32;
/// Modified Bessel function of the third kind, order one.
pub fn k1f(x: f32) -> f32;
/// Modified Bessel function of the third kind, order one,
/// exponentially scaled.
pub fn k1ef(x: f32) -> f32;
/// Modified Bessel function of the third kind, integer order.
pub fn knf(n: i32, x: f32) -> f32;
// Elliptic functions
/// Incomplete elliptic integral of the first kind.
pub fn ellikf(phi: f32, m: f32) -> f32;
/// Incomplete elliptic integral of the second kind.
pub fn ellief(phi: f32, m: f32) -> f32;
/// Complete elliptic integral of the first kind.
pub fn ellpkf(m1: f32) -> f32;
/// Complete elliptic integral of the second kind.
pub fn ellpef(m1: f32) -> f32;
/// Jacobian elliptic function.
pub fn ellpjf(u: f32, m: f32, sn: &mut f32, cn: &mut f32, dn: &mut f32, phi: &mut f32) -> i32;
// Hypergeometric functions
/// Confluent hypergeometric function 1F1.
pub fn hypergf(a: f32, b: f32, x: f32) -> f32;
/// Hypergeometric function 1F2.
pub fn onef2f(a: f32, b: f32, c: f32, x: f32, err: &mut f32) -> f32;
/// Gauss hypergeometric function 2F1.
pub fn hyp2f1f(a: f32, b: f32, c: f32, x: f32) -> f32;
/// Hypergeometric function 3F0.
pub fn threef0f(a: f32, b: f32, c: f32, x: f32, err: &mut f32) -> f32;
// Distributions
/// Binomial distribution.
pub fn bdtrf(k: i32, n: i32, p: f32) -> f32;
/// Complemented binomial distribution.
pub fn bdtrcf(k: i32, n: i32, p: f32) -> f32;
/// Inverse of binomial distribution.
pub fn bdtrif(k: i32, n: i32, y: f32) -> f32;
/// Negative binomial distribution.
pub fn nbdtrf(k: i32, n: i32, p: f32) -> f32;
/// Complemented negative binomial distribution.
pub fn nbdtrcf(k: i32, n: i32, p: f32) -> f32;
/// Inverse of negative binomial distribution.
pub fn nbdtrif(k: i32, n: i32, p: f32) -> f32;
/// Beta distribution.
pub fn btdtrf(a: f32, b: f32, x: f32) -> f32;
/// Chi-square distribution.
pub fn chdtrf(df: f32, x: f32) -> f32;
/// Complemented chi-square distribution.
pub fn chdtrcf(v: f32, x: f32) -> f32;
/// Inverse of complemented chi-square distribution.
pub fn chdtrif(df: f32, y: f32) -> f32;
/// F distribution.
pub fn fdtrf(df1: i32, df2: i32, x: f32) -> f32;
/// Complemented F distribution.
pub fn fdtrcf(df1: i32, df2: i32, x: f32) -> f32;
/// Inverse of complemented F distribution.
pub fn fdtrif(df1: i32, df2: i32, p: f32) -> f32;
/// Gamma distribution.
pub fn gdtrf(a: f32, b: f32, x: f32) -> f32;
/// Complemented gamma distribution.
pub fn gdtrcf(a: f32, b: f32, x: f32) -> f32;
/// Normal distribution.
pub fn ndtrf(x: f32) -> f32;
/// Inverse of normal distribution.
pub fn ndtrif(y: f32) -> f32;
/// Poisson distribution.
pub fn pdtrf(k: i32, m: f32) -> f32;
/// Complemented Poisson distribution.
pub fn pdtrcf(k: i32, m: f32) -> f32;
/// Inverse of Poisson distribution.
pub fn pdtrif(k: i32, y: f32) -> f32;
/// Student's t distribution.
pub fn stdtrf(k: i16, t: f32) -> f32;
/// Inverse of Student's t distribution.
pub fn stdtrif(k: i32, p: f32) -> f32;
// Misc special functions
/// Airy function.
pub fn airyf(x: f32, ai: &mut f32, aip: &mut f32, bi: &mut f32, bip: &mut f32) -> i32;
/// Dawson's integral.
pub fn dawsnf(x: f32) -> f32;
/// Fresnel integral.
pub fn fresnlf(x: f32, s: &mut f32, c: &mut f32);
/// Integral of Planck's black body radiation formula.
pub fn planckif(lambda: f32, temperature: f32) -> f32;
/// Struve function.
pub fn struvef(v: f32, x: f32) -> f32;
/// Riemann zeta function.
pub fn zetacf(x: f32) -> f32;
/// Riemann zeta function of two arguments.
pub fn zetaf(x: f32, q: f32) -> f32;
}
}
/// High-level bindings to single-precision Cephes functions.
///
/// This provides safe access to a subset of the Cephes functions. Note that
/// Cephes implements less functions for single than for double precision.
pub mod cephes_single {
use unsafe_cephes_single;
/// Function to compute the base-10 exponential of x
///
/// # Original Description from Stephen L. Moshier
/// Returns 10 raised to the x power.
///
/// Range reduction is accomplished by expressing the argument
/// as 10^x = 2^n 10^f, with |f| < 0.5 log10(2).
/// The Pade' form
///
/// 10^x = 1 + 2x P( x^2)/( Q( x^2 ) - P( x^2 ) )
///
/// is used to approximate 10^f.
///
/// ACCURACY (Relative error):
///
/// | arithmetic | domain | # trials | peak | rms |
/// | :--------: | :----: | :-------: | :---: | :--: |
/// | IEEE | -38,+38 | 100000 | 9.8e-8 | 2.8e-8 |
///
/// ERROR MESSAGES:
///
/// | message | condition | value returned |
/// | :-----: | :-------: | :------------: |
/// | exp10 underflow | x < -MAXL10 | 0.0 |
/// | exp10 overflow | x > MAXL10 | MAXNUM |
///
/// IEEE single arithmetic: MAXL10 = 38.230809449325611792.
///
///
/// # Arguments
/// * `x`: A single precision number
///
/// # Example
/// ```
/// use special_fun::cephes_single::exp10;
/// let x = 1.0f32;
/// exp10(x);
/// ```
pub fn exp10(x: f32) -> f32 {
unsafe { unsafe_cephes_single::exp10f(x) }
}
/// Function to accurately compute `exp(x) - 1` for small x
///
/// # Arguments
/// * `x`: A single precision number
///
/// # Example
/// ```
/// use special_fun::cephes_single::expm1;
/// let x = 1.0f32;
/// expm1(x);
/// ```
pub fn expm1(x: f32) -> f32 {
unsafe { unsafe_cephes_single::expm1f(x) }
}
/// Function to accurately compute the exponential of a squared argument
///
/// # Original Description from Stephen L. Moshier
///
/// Computes y = exp(x*x) while suppressing error amplification
/// that would ordinarily arise from the inexactness of the
/// exponential argument x*x.
///
/// If sign < 0, the result is inverted; i.e., y = exp(-x*x) .
///
/// ACCURACY (Relative error):
///
/// | arithmetic | domain | # trials | peak | rms |
/// | :--------: | :----: | :------: | :--: | :--:|
/// | IEEE | -9.4, 9.4 | 10^7 | 1.7e-7 | 4.7e-8 |
///
/// # Arguments
/// * `x`: A single precision number
/// * `sign`: An integer representing the sign of the exponent
///
/// # Example
/// ```
/// use special_fun::cephes_single::expx2;
/// let x = 1.0f32;
/// expx2(x, -1);
/// ```
pub fn expx2(x: f32, sign: i32) -> f32 {
unsafe { unsafe_cephes_single::expx2f(x, sign) }
}
/// Function to accurately compute the error function of x
///
/// The error function is given by:
///
/// $Erf(x) = \frac{2}{\sqrt{pi}} -\int^{x}_{0} e^{-t^2} dt$
///
/// # Arguments
/// * `x`: A single precision number
///
/// # Example
/// ```
/// use special_fun::cephes_single::erf;
/// let x = 1.0f32;
/// erf(x);
/// ```
pub fn erf(x: f32) -> f32 {
unsafe { unsafe_cephes_single::erff(x) }
}
/// Function to accurately compute the complementary error function of x
///
/// The error function is given by:
///
/// $Erf(x) = 1 + \frac{2}{\sqrt{pi}} -\int^{x}_{0} e^{-t^2} dt$
///
/// # Arguments
/// * `x`: A single precision number
///
/// # Example
/// ```
/// use special_fun::cephes_single::erfc;
/// let x = 1.0f32;
/// erfc(x);
/// ```
pub fn erfc(x: f32) -> f32 {
unsafe { unsafe_cephes_single::erfcf(x) }
}
/// Function to accurately compute `log(1 + x)`
///
/// # Arguments
/// * `x`: A single precision number
///
/// # Example
/// ```
/// use special_fun::cephes_single::log1p;
/// let x = 0.1f32;
/// log1p(x);
/// ```
pub fn log1p(x: f32) -> f32 {
unsafe { unsafe_cephes_single::log1pf(x) }
}
/// Function to accurately compute the dilogarithm function
///
/// Spence's function is a special case of the polylogarithm function, and
/// is defined as:
///
/// $ Li_2(x) = \int^x_1 \frac{ln(t)}{t - 1} du
///
/// Note, this implies that the domain has been shifted by 1 from the
/// standard definition (as per wikipedia) of:
///
/// $ Li_2(x) = - \int^x_0 \frac{ ln(1 - t) }{ t } dt
///
/// # Original Description from Stephen L. Moshier
/// Computes the integral for x >= 0. A rational approximation gives the
/// integral in the interval (0.5, 1.5). Transformation formulas for 1/x
/// and 1-x are employed outside the basic expansion range.
///
/// ACCURACY(Relative error):
///
/// | arithmetic | domain | # trials | peak | rms |
/// | :---: | :--: | :--: | :--: | :--: | :--: |
/// | IEEE | 0,4 | 30000 | 4.4e-7 | 6.3e-8 |
///
/// # Arguments
/// * `x`: A single precision number
///
/// # Example
/// ```
/// use special_fun::cephes_single::spence;
/// let x = 0.1f32;
/// spence(x);
/// ```
pub fn spence(x: f32) -> f32 {
unsafe { unsafe_cephes_single::spencef(x) }
}
/// Function to accurately compute sine and cosine integrals
///
/// The sine integral is defined as:
///
/// $ Si(x) = \int_0^{\infty} \frac{ sin(t) }{t} dt $
///
/// and the cosine integral is defined as:
///
/// $ Ci(x) = -\int_x^{\infty} \frac{ cos(t) }{t} dt $
///
/// which reduces to:
///
/// $ Ci(x) = \gamma + ln(x) + \int_0^x \frac{cos(t) - 1}{t} dt $
///
/// where $\gamma$ is the euler constant.
///
/// # Original Description from Stephen L. Moshier
/// The integrals are approximated by rational functions.
/// For x > 8 auxiliary functions f(x) and g(x) are employed
/// such that
///
/// Ci(x) = f(x) sin(x) - g(x) cos(x)
/// Si(x) = pi/2 - f(x) cos(x) - g(x) sin(x)
///
/// ACCURACY(Absolute error, except relative when > 1):
///
/// | arithmetic | Function | domain | # trials | peak | rms |
/// | :---: | :--: | :--: | :--: | :--: | :--: | :--: |
/// | IEEE | Si | 0,50 | 30000 | 2.1e-7 | 4.3e-8 |
/// | IEEE | Ci | 0,50 | 30000 | 3.9e-7 | 2.2e-8 |
///
/// # Arguments
/// * `x`: A single precision number
/// * `Si`: A mutable single precision number that will return the sine
/// integral value
/// * `Ci`: A mutable single precision number that will return the cosine
/// integral value
///
/// # Example
/// ```
/// use special_fun::cephes_single::sici;
/// let x = 0.1f32;
/// let mut si = 0.0_f32;
/// let mut ci = 0.0_f32;
/// sici(0.5_f32, &mut si, &mut ci);
/// ```
pub fn sici(x: f32, si: &mut f32, ci: &mut f32) {
let code = unsafe { unsafe_cephes_single::sicif(x, si, ci) };
assert_eq!(code, 0);
}
/// Function to accurately compute hyperbolic sine and cosine integrals
///
/// The hyperbolic sine integral is defined as:
///
/// $ Shi(x) = \int_0^{\infty} \frac{ sinh(t) }{t} dt $
///
/// and the hyperbolic cosine integral is defined as:
///
/// $ Chi(x) = -\int_x^{\infty} \frac{ cosh(t) }{t} dt $
///
/// which reduces to:
///
/// $ Chi(x) = \gamma + ln(x) + \int_0^x \frac{cosh(t) - 1}{t} dt $
///
/// where $\gamma$ is the euler constant.
///
/// # Original Description from Stephen L. Moshier
/// The integrals are approximated by rational functions.
/// For x > 8 auxiliary functions f(x) and g(x) are employed
/// such that
///
/// Ci(x) = f(x) sin(x) - g(x) cos(x)
/// Si(x) = pi/2 - f(x) cos(x) - g(x) sin(x)
///
/// ACCURACY(Relative error)):
///
/// | arithmetic | Function | domain | # trials | peak | rms |
/// | :---: | :--: | :--: | :--: | :--: | :--: | :--: |
/// | IEEE | Shi | 0,88 | 30000 | 3.5e-7 | 7.0e-8 |
///
/// ACCURACY(Absolute error, except relative when |Chi| > 1):
///
/// | arithmetic | Function | domain | # trials | peak | rms |
/// | :---: | :--: | :--: | :--: | :--: | :--: | :--: |
/// | IEEE | Chi | 0,88 | 30000 | 3.8e-7 | 7.6e-8 |
///
/// # Arguments
/// * `x`: A single precision number
/// * `Shi`: A mutable single precision number that will return the
/// hyperbolic sine integral value
/// * `Chi`: A mutable single precision number that will return the
/// hyperbolic cosine integral value
///
/// # Example
/// ```
/// use special_fun::cephes_single::shichi;
/// let x = 0.1f32;
/// let mut shi = 0.0_f32;
/// let mut chi = 0.0_f32;
/// shichi(0.5_f32, &mut shi, &mut chi);
/// ```
pub fn shichi(x: f32, chi: &mut f32, shi: &mut f32){
let code = unsafe { unsafe_cephes_single::shichif(x, chi, shi) };
assert_eq!(code, 0);
}
/// Function to compute the beta function.
///
/// The beta function is given by:
///
/// $B(a, b) = \int_0^1 t^{a - 1} ( 1 - t )^{b - 1} dt $
///
/// which simplifies to
///
/// $ B(a, b) = \frac{ \Gamma(a) \Gamma(b)}{ \Gamma(a + b) } $
///
/// # Original Description from Stephen L. Moshier
/// For large arguments the logarithm of the function is
/// evaluated using lgam(), then exponentiated.
///
/// ACCURACY (Relative error):
///
/// | arithmetic | domain | # trials | peak | rms |
/// | :--------: | :----: | :------: | :--: | :-: |
/// | IEEE | 0,30 | 10000 | 4.0e-5 | 6.0e-6 |
/// | IEEE | -20,0 | 10000 | 4.9e-3 | 5.4e-5 |
///
/// ERROR MESSAGES:
///
/// | message | condition | value returned |
/// | :-------: | :--------: | :----------: |
/// | beta overflow | log(beta) > MAXLOG | 0.0 |
/// | beta overflow | a or b <0 integer | 0.0 |
///
/// # Arguments
/// * `a`: A single precision parameter, assumed to be greater than 0
/// * `b`: A single precision parameter, assumed to be greater than 0
///
/// # Example
/// ```
/// use special_fun::cephes_single::beta;
/// let a = 0.1f32;
/// let b = 0.2f32;
/// beta(a, b);
/// ```
pub fn beta(a: f32, b: f32) -> f32 {
unsafe { unsafe_cephes_single::betaf(a, b) }
}
/// Function to compute the regularized incomplete beta function.
///
/// The regularized incomplete beta function is given by:
///
/// $I_x(a, b) = \frac{1}{B(a, b)} \int_0^x t^{a - 1} ( 1 - t )^{b - 1} dt $
///
/// and for $x = 1$, the regularized incomplete beta function evaluates to
/// exactly 1. Note, $ 1 - I_x(a, b) = I_{1 - x}(a, b)$.
///
/// # Original Description from Stephen L. Moshier
/// The integral is evaluated by a continued fraction expansion
/// or, when b*x is small, by a power series.
///
/// ACCURACY (Relative error):
///
/// | arithmetic | domain | # trials | peak | rms |
/// | :--------: | :----: | :------: | :--: | :-: |
/// | IEEE | 0,30 | 10000 | 3.7e-5 | 5.1e-6 |
/// | IEEE | 0,100 | 10000 | 1.7e-4 | 2.5e-5 |
///
/// The useful domain for relative error is limited by underflow of the
/// single precision exponential function.
///
/// ACCURACY (Absolute error):
///
/// | arithmetic | domain | # trials | peak | rms |
/// | :--------: | :----: | :------: | :--: | :-: |
/// | IEEE | 0,30 | 100000 | 2.2e-5 | 5.1e-6 |
/// | IEEE | 0,100 | 10000 | 6.5e-5 | 3.7e-6 |
///
/// Larger errors may occur for extreme ratios of `a` and `b`.
///
/// ERROR MESSAGES:
///
/// | message | condition | value returned |
/// | :-------: | :--------: | :----------: |
/// | incbet domain | x<0, x>1 | 0.0 |
///
/// # Arguments
/// * `a`: A single precision parameter, assumed to be greater than 0
/// * `b`: A single precision parameter, assumed to be greater than 0
/// * `x`: A single precision parameter, assumed to be between 0 and 1
///
/// # Example
/// ```
/// use special_fun::cephes_single::incbet;
/// let a = 0.1f32;
/// let b = 0.2f32;
/// incbet(a, b, 0.5f32);
/// ```
pub fn incbet(a: f32, b: f32, x: f32) -> f32 {
unsafe { unsafe_cephes_single::incbetf(a, b, x) }
}
/// Inverse of incomplete beta integral.
pub fn incbi(a: f32, b: f32, y: f32) -> f32 {
unsafe { unsafe_cephes_single::incbif(a, b, y) }
}
/// Gamma function.
pub fn gamma(x: f32) -> f32 {
unsafe { unsafe_cephes_single::gammaf(x) }
}
/// Reciprocal gamma function.
pub fn rgamma(x: f32) -> f32 {
unsafe { unsafe_cephes_single::rgammaf(x) }
}
/// Natural logarithm of gamma function.
pub fn lgam(x: f32) -> f32 {
unsafe { unsafe_cephes_single::lgamf(x) }
}
/// Regularized incomplete gamma integral.
pub fn igam(a: f32, x: f32) -> f32 {
unsafe { unsafe_cephes_single::igamf(a, x) }
}
/// Complemented incomplete gamma integral.
pub fn igamc(a: f32, x: f32) -> f32 {
unsafe { unsafe_cephes_single::igamcf(a, x) }
}
/// Inverse of complemented incomplete gamma integral.
pub fn igami(a: f32, p: f32) -> f32 {
unsafe { unsafe_cephes_single::igamif(a, p) }
}
/// Psi (digamma) function.
pub fn psi(x: f32) -> f32 {
unsafe { unsafe_cephes_single::psif(x) }
}
/// Factorial function.
pub fn fac(i: i32) -> f32 {
unsafe { unsafe_cephes_single::facf(i) }
}
}
/// Special functions on primitive floating point numbers.
///
/// This provides safe access to a subset of the Cephes functions implemented
/// for double and single precision.
pub trait FloatSpecial: Copy + Add<Output=Self> + Sub<Output=Self> {
/// Beta function.
fn beta(self, b: Self) -> Self;
/// Logarithm of beta function.
fn logbeta(self, b: Self) -> Self {
self.loggamma() + b.loggamma() - (self + b).loggamma()
}
/// Regularized incomplete beta function.
fn betainc(self, a: Self, b: Self) -> Self;
/// Inverse of incomplete beta integral.
fn betainc_inv(self, a: Self, b: Self) -> Self;
/// Factorial.
fn factorial(self) -> Self;
/// Gamma function.
fn gamma(self) -> Self;
/// Reciprocal gamma function.
fn rgamma(self) -> Self;
/// Logarithm of gamma function.
fn loggamma(self) -> Self;
/// Regularized incomplete gamma integral.
fn gammainc(self, a: Self) -> Self;
/// Complemented incomplete gamma integral.
fn gammac(self, a: Self) -> Self;
/// Inverse of complemented incomplete gamma integral.
fn gammac_inv(self, a: Self) -> Self;
/// Digamma function.
fn digamma(self) -> Self;
/// Error function.
fn erf(self) -> Self;
/// Complementary error function.
fn erfc(self) -> Self;
/// Confluent hypergeometric function 1F1.
fn hyp1f1(self, a: Self, b: Self) -> Self;
/// Hypergeometric function 1F2.
fn hyp1f2(self, a: Self, b: Self, c: Self) -> Self;
/// Gauss hypergeometric function 2F1.
fn hyp2f1(self, a: Self, b: Self, c: Self) -> Self;
/// Hypergeometric function 3F0.
fn hyp3f0(self, a: Self, b: Self, c: Self) -> Self;
/// Normal distribution function.
fn norm(self) -> Self;
/// Inverse of Normal distribution function.
fn norm_inv(self) -> Self;
/// Bessel function of real order of the first kind.
fn besselj(self, v: Self) -> Self;
/// Bessel function of real order of the second kind.
fn bessely(self, v: Self) -> Self;
/// Modified bessel function of real order of the first kind.
fn besseli(self, v: Self) -> Self;
/// Modified bessel function of integer order of the second kind.
fn besselk(self, v: i32) -> Self;
/// Riemann zeta function.
fn riemann_zeta(self) -> Self;
/// Hurwitz zeta function.
fn hurwitz_zeta(self, q: Self) -> Self;
}
impl FloatSpecial for f64 {
fn beta(self, b: f64) -> f64 {
unsafe { unsafe_cephes_double::beta(self, b) }
}
fn betainc(self, a: f64, b: f64) -> f64 {
unsafe { unsafe_cephes_double::incbet(a, b, self) }
}
fn betainc_inv(self, a: f64, b: f64) -> f64 {
unsafe { unsafe_cephes_double::incbi(a, b, self) }
}
fn factorial(self) -> f64 {
unsafe { unsafe_cephes_double::gamma(self + 1.0) }
}
fn gamma(self) -> f64 {
unsafe { unsafe_cephes_double::gamma(self) }
}
fn rgamma(self) -> f64 {
unsafe { unsafe_cephes_double::rgamma(self) }
}
fn loggamma(self) -> f64 {
unsafe { unsafe_cephes_double::lgam(self) }
}
fn gammainc(self, a: f64) -> f64 {
unsafe { unsafe_cephes_double::igam(a, self) }
}
fn gammac(self, a: f64) -> f64 {
unsafe { unsafe_cephes_double::igamc(a, self) }
}
fn gammac_inv(self, a: f64) -> f64 {
unsafe { unsafe_cephes_double::igami(a, self) }
}
fn digamma(self) -> f64 {
unsafe { unsafe_cephes_double::psi(self) }
}
fn erf(self) -> f64 {
unsafe { unsafe_cephes_double::erf(self) }
}
fn erfc(self) -> f64 {
unsafe { unsafe_cephes_double::erfc(self) }
}
fn hyp1f1(self, a: f64, b: f64) -> f64 {
unsafe { unsafe_cephes_double::hyperg(a, b, self) }
}
fn hyp1f2(self, a: f64, b: f64, c: f64) -> f64 {
let mut err = 0.0;
unsafe { unsafe_cephes_double::onef2(a, b, c, self, &mut err) }
}
fn hyp2f1(self, a: f64, b: f64, c: f64) -> f64 {
unsafe { unsafe_cephes_double::hyp2f1(a, b, c, self) }
}
fn hyp3f0(self, a: f64, b: f64, c: f64) -> f64 {
let mut err = 0.0;
unsafe { unsafe_cephes_double::threef0(a, b, c, self, &mut err) }
}
fn norm(self) -> f64 {
unsafe { unsafe_cephes_double::ndtr(self) }
}
fn norm_inv(self) -> f64 {
unsafe { unsafe_cephes_double::ndtri(self) }
}
fn besselj(self, v: f64) -> f64 {
unsafe { unsafe_cephes_double::jv(v, self) }
}
fn bessely(self, v: f64) -> f64 {
unsafe { unsafe_cephes_double::yv(v, self) }
}
fn besseli(self, v: f64) -> f64 {
unsafe { unsafe_cephes_double::iv(v, self) }
}
fn besselk(self, n: i32) -> f64 {
unsafe { unsafe_cephes_double::kn(n, self) }
}
fn riemann_zeta(self) -> f64 {
unsafe { 1.0 + unsafe_cephes_double::zetac(self) }
}
fn hurwitz_zeta(self, q: f64) -> f64 {
unsafe { unsafe_cephes_double::zeta(self, q) }
}
}
impl FloatSpecial for f32 {
fn beta(self, b: f32) -> f32 {
unsafe { unsafe_cephes_single::betaf(self, b) }
}
fn betainc(self, a: f32, b: f32) -> f32 {
unsafe { unsafe_cephes_single::incbetf(a, b, self) }
}
fn betainc_inv(self, a: f32, b: f32) -> f32 {
unsafe { unsafe_cephes_single::incbif(a, b, self) }
}
fn factorial(self) -> f32 {
unsafe { unsafe_cephes_single::gammaf(self + 1.0) }
}
fn gamma(self) -> f32 {
unsafe { unsafe_cephes_single::gammaf(self) }
}
fn rgamma(self) -> f32 {
unsafe { unsafe_cephes_single::rgammaf(self) }
}
fn loggamma(self) -> f32 {
unsafe { unsafe_cephes_single::lgamf(self) }
}
fn gammainc(self, a: f32) -> f32 {
unsafe { unsafe_cephes_single::igamf(a, self) }
}
fn gammac(self, a: f32) -> f32 {
unsafe { unsafe_cephes_single::igamcf(a, self) }
}
fn gammac_inv(self, a: f32) -> f32 {
unsafe { unsafe_cephes_single::igamif(a, self) }
}
fn digamma(self) -> f32 {
unsafe { unsafe_cephes_single::psif(self) }
}
fn erf(self) -> f32 {
unsafe { unsafe_cephes_single::erff(self) }
}
fn erfc(self) -> f32 {
unsafe { unsafe_cephes_single::erfcf(self) }
}
fn hyp1f1(self, a: f32, b: f32) -> f32 {
unsafe { unsafe_cephes_single::hypergf(a, b, self) }
}
fn hyp1f2(self, a: f32, b: f32, c: f32) -> f32 {
let mut err = 0.0;
unsafe { unsafe_cephes_single::onef2f(a, b, c, self, &mut err) }
}
fn hyp2f1(self, a: f32, b: f32, c: f32) -> f32 {
unsafe { unsafe_cephes_single::hyp2f1f(a, b, c, self) }
}
fn hyp3f0(self, a: f32, b: f32, c: f32) -> f32 {
let mut err = 0.0;
unsafe { unsafe_cephes_single::threef0f(a, b, c, self, &mut err) }
}
fn norm(self) -> f32 {
unsafe { unsafe_cephes_single::ndtrf(self) }
}
fn norm_inv(self) -> f32 {
unsafe { unsafe_cephes_single::ndtrif(self) }
}
fn besselj(self, v: f32) -> f32 {
unsafe { unsafe_cephes_single::jvf(v, self) }
}
fn bessely(self, v: f32) -> f32 {
unsafe { unsafe_cephes_single::yvf(v, self) }
}
fn besseli(self, v: f32) -> f32 {
unsafe { unsafe_cephes_single::ivf(v, self) }
}
fn besselk(self, n: i32) -> f32 {
unsafe { unsafe_cephes_single::knf(n, self) }
}
fn riemann_zeta(self) -> f32 {
unsafe { 1.0 + unsafe_cephes_single::zetacf(self) }
}
fn hurwitz_zeta(self, q: f32) -> f32 {
unsafe { unsafe_cephes_single::zetaf(self, q) }
}
}