Skip to content

Commit a5bf496

Browse files
authored
Merge pull request #18 from jorenham/erf_inv
2 parents 712d6cf + 000ddd9 commit a5bf496

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

src/ffi.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ unsafe extern "C" {
3131
// boost/math/special_functions/erf.hpp
3232
pub fn math_erf(x: f64) -> f64;
3333
pub fn math_erfc(x: f64) -> f64;
34+
pub fn math_erf_inv(p: f64) -> f64;
35+
pub fn math_erfc_inv(q: f64) -> f64;
3436

3537
// boost/math/special_functions/factorials.hpp
3638
pub fn math_factorial(i: c_uint) -> f64;

src/math/special_functions/erf.rs

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,62 @@
22
33
use crate::ffi;
44

5-
/// Error function *erf(x)*
5+
/// Error function
66
///
7+
/// Corresponds to `boost::math::erf(x)` in C++.
78
/// <https://boost.org/doc/libs/latest/libs/math/doc/html/math_toolkit/sf_erf/error_function.html>
89
pub fn erf(x: f64) -> f64 {
910
unsafe { ffi::math_erf(x) }
1011
}
1112

12-
/// Complementary error function *erfc(x)*
13+
/// Complement of the error function
1314
///
15+
/// Corresponds to `boost::math::erfc(x)` in C++.
1416
/// <https://boost.org/doc/libs/latest/libs/math/doc/html/math_toolkit/sf_erf/error_function.html>
1517
pub fn erfc(x: f64) -> f64 {
1618
unsafe { ffi::math_erfc(x) }
1719
}
1820

21+
/// Inverse of [`erf`]
22+
///
23+
/// Corresponds to `boost::math::erf_inv(x)` in C++.
24+
/// <https://boost.org/doc/libs/latest/libs/math/doc/html/math_toolkit/sf_erf/error_inv.html>
25+
pub fn erf_inv(p: f64) -> f64 {
26+
unsafe { ffi::math_erf_inv(p) }
27+
}
28+
29+
/// Inverse of [`erfc`]
30+
///
31+
/// Corresponds to `boost::math::erfc_inv(q)` in C++.
32+
/// <https://boost.org/doc/libs/latest/libs/math/doc/html/math_toolkit/sf_erf/error_inv.html>
33+
pub fn erfc_inv(q: f64) -> f64 {
34+
unsafe { ffi::math_erfc_inv(q) }
35+
}
36+
1937
#[cfg(test)]
20-
mod tests {
38+
mod smoketests {
2139
use super::*;
2240

2341
#[test]
2442
fn test_erf() {
25-
assert_abs_diff_eq!(erf(0.0), 0.0, epsilon = f64::EPSILON);
26-
assert_abs_diff_eq!(erf(-1.0) + erf(1.0), 0.0, epsilon = f64::EPSILON);
43+
assert!(erf(1e-9).is_finite());
44+
assert!(erf(1.0 - 1e-9).is_finite());
2745
}
2846

2947
#[test]
3048
fn test_erfc() {
31-
assert_abs_diff_eq!(erfc(0.0), 1.0, epsilon = f64::EPSILON);
49+
assert!(erfc(1e-9).is_finite());
50+
assert!(erfc(1.0 - 1e-9).is_finite());
51+
}
52+
#[test]
53+
fn test_erf_inv() {
54+
assert!(erf_inv(1e-9).is_finite());
55+
assert!(erf_inv(1.0 - 1e-9).is_finite());
3256
}
3357

3458
#[test]
35-
fn test_erf_erfc_relation() {
36-
assert_abs_diff_eq!(erf(1.0) + erfc(1.0), 1.0, epsilon = f64::EPSILON);
59+
fn test_erfc_inv() {
60+
assert!(erfc_inv(1e-9).is_finite());
61+
assert!(erfc_inv(1.0 - 1e-9).is_finite());
3762
}
3863
}

wrapper.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ double math_digamma(double x) { return digamma(x); }
5555
// boost/math/special_functions/erf.hpp
5656
double math_erf(double x) { return erf(x); }
5757
double math_erfc(double x) { return erfc(x); }
58+
double math_erf_inv(double p) { return erf_inv(p); }
59+
double math_erfc_inv(double q) { return erfc_inv(q); }
5860

5961
// boost/math/special_functions/factorials.hpp
6062
double math_factorial(unsigned i) { return factorial<double>(i); }

0 commit comments

Comments
 (0)