rgsl/
trigonometric.rs

1//
2// A rust binding for the GSL library by Guillaume Gomez (guillaume1.gomez@gmail.com)
3//
4
5use crate::Value;
6use std::mem::MaybeUninit;
7use types;
8
9pub trait Trigonometric {
10    /// This routine computes the sine function \sin(x).
11    fn sin(&self) -> Self;
12    /// This routine computes the sine function \sin(x).
13    fn sin_e(&self) -> Result<types::Result, Value>;
14    /// This routine computes the cosine function \sin(x).
15    fn cos(&self) -> Self;
16    /// This routine computes the cosine function \sin(x).
17    fn cos_e(&self) -> Result<types::Result, Value>;
18    /// This routine computes the hypotenuse function \sqrt{x^2 + y^2} avoiding overflow and underflow.
19    fn sf_hypot(&self, y: f64) -> Self;
20    /// This routine computes the hypotenuse function \sqrt{x^2 + y^2} avoiding overflow and underflow.
21    fn sf_hypot_e(&self, y: f64) -> Result<types::Result, Value>;
22    /// This routine computes \sinc(x) = \sin(\pi x) / (\pi x) for any value of x.
23    fn sinc(&self) -> Self;
24    /// This routine computes \sinc(x) = \sin(\pi x) / (\pi x) for any value of x.
25    fn sinc_e(&self) -> Result<types::Result, Value>;
26    /// This function computes the complex sine, \sin(z_r + i z_i) storing the real and imaginary parts in szr, szi.
27    fn complex_sin_e(&self, zi: f64) -> Result<(types::Result, types::Result), Value>;
28    /// This function computes the complex cosine, \cos(z_r + i z_i) storing the real and imaginary parts in czr, czi.
29    fn complex_cos_e(&self, zi: f64) -> Result<(types::Result, types::Result), Value>;
30    /// This function computes the logarithm of the complex sine, \log(\sin(z_r + i z_i)) storing the real and imaginary parts in lszr, lszi.
31    fn complex_logsin_e(&self, zi: f64) -> Result<(types::Result, types::Result), Value>;
32    /// This routine computes \log(\sinh(x)) for x > 0.
33    fn lnsinh(&self) -> Self;
34    /// This routine computes \log(\sinh(x)) for x > 0.
35    fn lnsinh_e(&self) -> Result<types::Result, Value>;
36    /// This routine computes \log(\cosh(x)) for x > 0.
37    fn lncosh(&self) -> Self;
38    /// This routine computes \log(\cosh(x)) for x > 0.
39    fn lncosh_e(&self) -> Result<types::Result, Value>;
40    /// This function converts the polar coordinates (r,theta) to rectilinear coordinates (x,y), x = r\cos(\theta), y = r\sin(\theta).
41    fn polar_to_rect(&self, theta: f64) -> Result<(types::Result, types::Result), Value>;
42    /// This function converts the rectilinear coordinates (x,y) to polar coordinates (r,theta), such that x = r\cos(\theta), y = r\sin(\theta).
43    /// The argument theta lies in the range [-\pi, \pi].
44    fn rect_to_polar(&self, y: f64) -> Result<(types::Result, types::Result), Value>;
45    /// This routine forces the angle theta to lie in the range (-\pi,\pi].
46    ///
47    /// Note that the mathematical value of \pi is slightly greater than M_PI, so the machine numbers M_PI and -M_PI are included in the range.
48    fn angle_restrict_symm(&self) -> Self;
49    /// This routine forces the angle theta to lie in the range (-\pi,\pi].
50    ///
51    /// Note that the mathematical value of \pi is slightly greater than M_PI, so the machine numbers M_PI and -M_PI are included in the range.
52    fn angle_restrict_symm_e(&mut self) -> Result<(), Value>;
53    /// This routine forces the angle theta to lie in the range [0, 2\pi).
54    ///
55    /// Note that the mathematical value of 2\pi is slightly greater than 2*M_PI, so the machine number 2*M_PI is included in the range.
56    fn angle_restrict_pos(&self) -> Self;
57    /// This routine forces the angle theta to lie in the range [0, 2\pi).
58    ///
59    /// Note that the mathematical value of 2\pi is slightly greater than 2*M_PI, so the machine number 2*M_PI is included in the range.
60    fn angle_restrict_pos_e(&mut self) -> Result<(), Value>;
61    /// This routine computes the sine of an angle x with an associated absolute error dx, \sin(x \pm dx).
62    ///
63    /// Note that this function is provided in the error-handling form only since its purpose is to compute the propagated error.
64    fn sin_err_e(&self, dx: f64) -> Result<types::Result, Value>;
65    /// This routine computes the cosine of an angle x with an associated absolute error dx, \cos(x \pm dx).
66    ///
67    /// Note that this function is provided in the error-handling form only since its purpose is to compute the propagated error.
68    fn cos_err_e(&self, dx: f64) -> Result<types::Result, Value>;
69}
70
71impl Trigonometric for f64 {
72    #[doc(alias = "gsl_sf_sin")]
73    fn sin(&self) -> f64 {
74        unsafe { ::sys::gsl_sf_sin(*self) }
75    }
76
77    #[doc(alias = "gsl_sf_sin_e")]
78    fn sin_e(&self) -> Result<types::Result, Value> {
79        let mut result = MaybeUninit::<sys::gsl_sf_result>::uninit();
80        let ret = unsafe { ::sys::gsl_sf_sin_e(*self, result.as_mut_ptr()) };
81
82        result_handler!(ret, unsafe { result.assume_init() }.into())
83    }
84
85    #[doc(alias = "gsl_sf_cos")]
86    fn cos(&self) -> f64 {
87        unsafe { ::sys::gsl_sf_cos(*self) }
88    }
89
90    #[doc(alias = "gsl_sf_cos_e")]
91    fn cos_e(&self) -> Result<types::Result, Value> {
92        let mut result = MaybeUninit::<sys::gsl_sf_result>::uninit();
93        let ret = unsafe { ::sys::gsl_sf_cos_e(*self, result.as_mut_ptr()) };
94
95        result_handler!(ret, unsafe { result.assume_init() }.into())
96    }
97
98    #[doc(alias = "gsl_sf_hypot")]
99    fn sf_hypot(&self, y: f64) -> f64 {
100        unsafe { ::sys::gsl_sf_hypot(*self, y) }
101    }
102
103    #[doc(alias = "gsl_sf_hypot_e")]
104    fn sf_hypot_e(&self, y: f64) -> Result<types::Result, Value> {
105        let mut result = MaybeUninit::<sys::gsl_sf_result>::uninit();
106        let ret = unsafe { ::sys::gsl_sf_hypot_e(*self, y, result.as_mut_ptr()) };
107
108        result_handler!(ret, unsafe { result.assume_init() }.into())
109    }
110
111    #[doc(alias = "gsl_sf_sinc")]
112    fn sinc(&self) -> f64 {
113        unsafe { ::sys::gsl_sf_sinc(*self) }
114    }
115
116    #[doc(alias = "gsl_sf_sinc_e")]
117    fn sinc_e(&self) -> Result<types::Result, Value> {
118        let mut result = MaybeUninit::<sys::gsl_sf_result>::uninit();
119        let ret = unsafe { ::sys::gsl_sf_sinc_e(*self, result.as_mut_ptr()) };
120
121        result_handler!(ret, unsafe { result.assume_init() }.into())
122    }
123
124    #[doc(alias = "gsl_sf_complex_sin_e")]
125    fn complex_sin_e(&self, zi: f64) -> Result<(types::Result, types::Result), Value> {
126        let mut result = MaybeUninit::<sys::gsl_sf_result>::uninit();
127        let mut result2 = MaybeUninit::<sys::gsl_sf_result>::uninit();
128        let ret = unsafe {
129            ::sys::gsl_sf_complex_sin_e(*self, zi, result.as_mut_ptr(), result2.as_mut_ptr())
130        };
131
132        result_handler!(
133            ret,
134            (
135                unsafe { result.assume_init() }.into(),
136                unsafe { result2.assume_init() }.into(),
137            )
138        )
139    }
140
141    #[doc(alias = "gsl_sf_complex_cos_e")]
142    fn complex_cos_e(&self, zi: f64) -> Result<(types::Result, types::Result), Value> {
143        let mut result = MaybeUninit::<sys::gsl_sf_result>::uninit();
144        let mut result2 = MaybeUninit::<sys::gsl_sf_result>::uninit();
145        let ret = unsafe {
146            ::sys::gsl_sf_complex_cos_e(*self, zi, result.as_mut_ptr(), result2.as_mut_ptr())
147        };
148
149        result_handler!(
150            ret,
151            (
152                unsafe { result.assume_init() }.into(),
153                unsafe { result2.assume_init() }.into(),
154            )
155        )
156    }
157
158    #[doc(alias = "gsl_sf_complex_logsin_e")]
159    fn complex_logsin_e(&self, zi: f64) -> Result<(types::Result, types::Result), Value> {
160        let mut result = MaybeUninit::<sys::gsl_sf_result>::uninit();
161        let mut result2 = MaybeUninit::<sys::gsl_sf_result>::uninit();
162        let ret = unsafe {
163            ::sys::gsl_sf_complex_logsin_e(*self, zi, result.as_mut_ptr(), result2.as_mut_ptr())
164        };
165
166        result_handler!(
167            ret,
168            (
169                unsafe { result.assume_init() }.into(),
170                unsafe { result2.assume_init() }.into(),
171            )
172        )
173    }
174
175    #[doc(alias = "gsl_sf_lnsinh")]
176    fn lnsinh(&self) -> f64 {
177        unsafe { ::sys::gsl_sf_lnsinh(*self) }
178    }
179
180    #[doc(alias = "gsl_sf_lnsinh_e")]
181    fn lnsinh_e(&self) -> Result<types::Result, Value> {
182        let mut result = MaybeUninit::<sys::gsl_sf_result>::uninit();
183        let ret = unsafe { ::sys::gsl_sf_lnsinh_e(*self, result.as_mut_ptr()) };
184
185        result_handler!(ret, unsafe { result.assume_init() }.into())
186    }
187
188    #[doc(alias = "gsl_sf_lncosh")]
189    fn lncosh(&self) -> f64 {
190        unsafe { ::sys::gsl_sf_lncosh(*self) }
191    }
192
193    #[doc(alias = "gsl_sf_lncosh_e")]
194    fn lncosh_e(&self) -> Result<types::Result, Value> {
195        let mut result = MaybeUninit::<sys::gsl_sf_result>::uninit();
196        let ret = unsafe { ::sys::gsl_sf_lncosh_e(*self, result.as_mut_ptr()) };
197
198        result_handler!(ret, unsafe { result.assume_init() }.into())
199    }
200
201    #[doc(alias = "gsl_sf_polar_to_rect")]
202    fn polar_to_rect(&self, theta: f64) -> Result<(types::Result, types::Result), Value> {
203        let mut result = MaybeUninit::<sys::gsl_sf_result>::uninit();
204        let mut result2 = MaybeUninit::<sys::gsl_sf_result>::uninit();
205        let ret = unsafe {
206            ::sys::gsl_sf_polar_to_rect(*self, theta, result.as_mut_ptr(), result2.as_mut_ptr())
207        };
208
209        result_handler!(
210            ret,
211            (
212                unsafe { result.assume_init() }.into(),
213                unsafe { result2.assume_init() }.into(),
214            )
215        )
216    }
217
218    #[doc(alias = "gsl_sf_rect_to_polar")]
219    fn rect_to_polar(&self, y: f64) -> Result<(types::Result, types::Result), Value> {
220        let mut result = MaybeUninit::<sys::gsl_sf_result>::uninit();
221        let mut result2 = MaybeUninit::<sys::gsl_sf_result>::uninit();
222        let ret = unsafe {
223            ::sys::gsl_sf_rect_to_polar(*self, y, result.as_mut_ptr(), result2.as_mut_ptr())
224        };
225
226        result_handler!(
227            ret,
228            (
229                unsafe { result.assume_init() }.into(),
230                unsafe { result2.assume_init() }.into(),
231            )
232        )
233    }
234
235    #[doc(alias = "gsl_sf_angle_restrict_symm")]
236    fn angle_restrict_symm(&self) -> f64 {
237        unsafe { ::sys::gsl_sf_angle_restrict_symm(*self) }
238    }
239
240    #[doc(alias = "gsl_sf_angle_restrict_symm_e")]
241    fn angle_restrict_symm_e(&mut self) -> Result<(), Value> {
242        let ret = unsafe { ::sys::gsl_sf_angle_restrict_symm_e(self) };
243        result_handler!(ret, ())
244    }
245
246    #[doc(alias = "gsl_sf_angle_restrict_pos")]
247    fn angle_restrict_pos(&self) -> f64 {
248        unsafe { ::sys::gsl_sf_angle_restrict_pos(*self) }
249    }
250
251    #[doc(alias = "gsl_sf_angle_restrict_pos_e")]
252    fn angle_restrict_pos_e(&mut self) -> Result<(), Value> {
253        let ret = unsafe { ::sys::gsl_sf_angle_restrict_pos_e(self) };
254        result_handler!(ret, ())
255    }
256
257    #[doc(alias = "gsl_sf_sin_err_e")]
258    fn sin_err_e(&self, dx: f64) -> Result<types::Result, Value> {
259        let mut result = MaybeUninit::<sys::gsl_sf_result>::uninit();
260        let ret = unsafe { ::sys::gsl_sf_sin_err_e(*self, dx, result.as_mut_ptr()) };
261
262        result_handler!(ret, unsafe { result.assume_init() }.into())
263    }
264
265    #[doc(alias = "gsl_sf_cos_err_e")]
266    fn cos_err_e(&self, dx: f64) -> Result<types::Result, Value> {
267        let mut result = MaybeUninit::<sys::gsl_sf_result>::uninit();
268        let ret = unsafe { ::sys::gsl_sf_cos_err_e(*self, dx, result.as_mut_ptr()) };
269
270        result_handler!(ret, unsafe { result.assume_init() }.into())
271    }
272}