rgsl/
logarithm.rs

1//
2// A rust binding for the GSL library by Guillaume Gomez (guillaume1.gomez@gmail.com)
3//
4
5//! Information on the properties of the Logarithm function can be found in Abramowitz & Stegun, Chapter 4.
6
7use crate::{types, Value};
8use std::mem::MaybeUninit;
9
10/// This routine computes the logarithm of x, \log(x), for x > 0.
11#[doc(alias = "gsl_sf_log")]
12pub fn log(x: f64) -> f64 {
13    unsafe { sys::gsl_sf_log(x) }
14}
15
16/// This routine computes the logarithm of x, \log(x), for x > 0.
17#[doc(alias = "gsl_sf_log_e")]
18pub fn log_e(x: f64) -> Result<types::Result, Value> {
19    let mut result = MaybeUninit::<sys::gsl_sf_result>::uninit();
20    let ret = unsafe { sys::gsl_sf_log_e(x, result.as_mut_ptr()) };
21
22    result_handler!(ret, unsafe { result.assume_init() }.into())
23}
24
25/// This routine computes the logarithm of the magnitude of x, \log(|x|), for x \ne 0.
26#[doc(alias = "gsl_sf_log_abs")]
27pub fn log_abs(x: f64) -> f64 {
28    unsafe { sys::gsl_sf_log_abs(x) }
29}
30
31/// This routine computes the logarithm of the magnitude of x, \log(|x|), for x \ne 0.
32#[doc(alias = "gsl_sf_log_abs_e")]
33pub fn log_abs_e(x: f64) -> Result<types::Result, Value> {
34    let mut result = MaybeUninit::<sys::gsl_sf_result>::uninit();
35    let ret = unsafe { sys::gsl_sf_log_abs_e(x, result.as_mut_ptr()) };
36
37    result_handler!(ret, unsafe { result.assume_init() }.into())
38}
39
40/// This routine computes the complex logarithm of z = z_r + i z_i.
41/// The results are returned as lnr, theta such that \exp(lnr + i \theta) = z_r + i z_i, where \theta lies in the range [-\pi,\pi].
42#[doc(alias = "gsl_sf_complex_log_e")]
43pub fn complex_log_e(zr: f64, zi: f64) -> Result<(::types::Result, ::types::Result), Value> {
44    let mut lnr = MaybeUninit::<sys::gsl_sf_result>::uninit();
45    let mut theta = MaybeUninit::<sys::gsl_sf_result>::uninit();
46    let ret = unsafe { sys::gsl_sf_complex_log_e(zr, zi, lnr.as_mut_ptr(), theta.as_mut_ptr()) };
47
48    result_handler!(
49        ret,
50        (
51            unsafe { lnr.assume_init() }.into(),
52            unsafe { theta.assume_init() }.into(),
53        )
54    )
55}
56
57/// This routine computes \log(1 + x) for x > -1 using an algorithm that is accurate for small x.
58#[doc(alias = "gsl_sf_log_1plusx")]
59pub fn log_1plusx(x: f64) -> f64 {
60    unsafe { sys::gsl_sf_log_1plusx(x) }
61}
62
63/// This routine computes \log(1 + x) for x > -1 using an algorithm that is accurate for small x.
64#[doc(alias = "gsl_sf_log_1plusx_e")]
65pub fn log_1plusx_e(x: f64) -> Result<types::Result, Value> {
66    let mut result = MaybeUninit::<sys::gsl_sf_result>::uninit();
67    let ret = unsafe { sys::gsl_sf_log_1plusx_e(x, result.as_mut_ptr()) };
68
69    result_handler!(ret, unsafe { result.assume_init() }.into())
70}
71
72/// This routine computes \log(1 + x) - x for x > -1 using an algorithm that is accurate for small x.
73#[doc(alias = "gsl_sf_log_1plusx_mx")]
74pub fn log_1plusx_mx(x: f64) -> f64 {
75    unsafe { sys::gsl_sf_log_1plusx_mx(x) }
76}
77
78/// This routine computes \log(1 + x) - x for x > -1 using an algorithm that is accurate for small x.
79#[doc(alias = "gsl_sf_log_1plusx_mx_e")]
80pub fn log_1plusx_mx_e(x: f64) -> Result<types::Result, Value> {
81    let mut result = MaybeUninit::<sys::gsl_sf_result>::uninit();
82    let ret = unsafe { sys::gsl_sf_log_1plusx_mx_e(x, result.as_mut_ptr()) };
83
84    result_handler!(ret, unsafe { result.assume_init() }.into())
85}