rgsl/
interpolation.rs

1//
2// A rust binding for the GSL library by Guillaume Gomez (guillaume1.gomez@gmail.com)
3//
4
5use crate::Value;
6use ffi::FFI;
7
8/// This function returns the index i of the array x_array such that `x_array[i] <= x < x_array[i+1]`.
9/// The index is searched for in the range `[index_lo,index_hi]`.
10#[doc(alias = "gsl_interp_bsearch")]
11pub fn bsearch(x_array: &[f64], x: f64, index_lo: usize, index_hi: usize) -> usize {
12    unsafe { sys::gsl_interp_bsearch(x_array.as_ptr(), x, index_lo, index_hi) }
13}
14
15/// This function returns the interpolated value of y for a given point x, using the interpolation
16/// object interp, data arrays xa and ya and the accelerator acc. When x is outside the range of xa,
17/// the error code ::Dom is returned with a value of rgsl::NAN for y.
18#[doc(alias = "gsl_interp_eval")]
19pub fn eval(interp: &::Interp, xa: &[f64], ya: &[f64], x: f64, acc: &mut ::InterpAccel) -> f64 {
20    unsafe {
21        sys::gsl_interp_eval(
22            interp.unwrap_shared(),
23            xa.as_ptr(),
24            ya.as_ptr(),
25            x,
26            &mut acc.0,
27        )
28    }
29}
30
31/// This function returns the interpolated value of y for a given point x, using the interpolation
32/// object interp, data arrays xa and ya and the accelerator acc. When x is outside the range of xa,
33/// the error code ::Dom is returned with a value of rgsl::NAN for y.
34///
35/// Returns `y`.
36#[doc(alias = "gsl_interp_eval_e")]
37pub fn eval_e(
38    interp: &::Interp,
39    xa: &[f64],
40    ya: &[f64],
41    x: f64,
42    acc: &mut ::InterpAccel,
43) -> Result<f64, Value> {
44    let mut y = 0.;
45    let ret = unsafe {
46        sys::gsl_interp_eval_e(
47            interp.unwrap_shared(),
48            xa.as_ptr(),
49            ya.as_ptr(),
50            x,
51            &mut acc.0,
52            &mut y,
53        )
54    };
55    result_handler!(ret, y)
56}
57
58/// This function returns the derivative d of an interpolated function for a given point x, using
59/// the interpolation object interp, data arrays xa and ya and the accelerator acc.
60#[doc(alias = "gsl_interp_eval_deriv")]
61pub fn eval_deriv(
62    interp: &::Interp,
63    xa: &[f64],
64    ya: &[f64],
65    x: f64,
66    acc: &mut ::InterpAccel,
67) -> f64 {
68    unsafe {
69        sys::gsl_interp_eval_deriv(
70            interp.unwrap_shared(),
71            xa.as_ptr(),
72            ya.as_ptr(),
73            x,
74            &mut acc.0,
75        )
76    }
77}
78
79/// This function returns the derivative d of an interpolated function for a given point x, using
80/// the interpolation object interp, data arrays xa and ya and the accelerator acc.
81///
82/// Returns `(Value, d)`.
83#[doc(alias = "gsl_interp_eval_deriv_e")]
84pub fn eval_deriv_e(
85    interp: &::Interp,
86    xa: &[f64],
87    ya: &[f64],
88    x: f64,
89    acc: &mut ::InterpAccel,
90) -> Result<f64, Value> {
91    let mut d = 0.;
92    let ret = unsafe {
93        sys::gsl_interp_eval_deriv_e(
94            interp.unwrap_shared(),
95            xa.as_ptr(),
96            ya.as_ptr(),
97            x,
98            &mut acc.0,
99            &mut d,
100        )
101    };
102    result_handler!(ret, d)
103}
104
105/// This function returns the second derivative d2 of an interpolated function for a given point x,
106/// using the interpolation object interp, data arrays xa and ya and the accelerator acc.
107#[doc(alias = "gsl_interp_eval_deriv2")]
108pub fn eval_deriv2(
109    interp: &::Interp,
110    xa: &[f64],
111    ya: &[f64],
112    x: f64,
113    acc: &mut ::InterpAccel,
114) -> f64 {
115    unsafe {
116        sys::gsl_interp_eval_deriv2(
117            interp.unwrap_shared(),
118            xa.as_ptr(),
119            ya.as_ptr(),
120            x,
121            &mut acc.0,
122        )
123    }
124}
125
126/// This function returns the second derivative d2 of an interpolated function for a given point x,
127/// using the interpolation object interp, data arrays xa and ya and the accelerator acc.
128///
129/// Returns `(Value, d2)`.
130#[doc(alias = "gsl_interp_eval_deriv2_e")]
131pub fn eval_deriv2_e(
132    interp: &::Interp,
133    xa: &[f64],
134    ya: &[f64],
135    x: f64,
136    acc: &mut ::InterpAccel,
137) -> Result<f64, Value> {
138    let mut d2 = 0.;
139    let ret = unsafe {
140        sys::gsl_interp_eval_deriv2_e(
141            interp.unwrap_shared(),
142            xa.as_ptr(),
143            ya.as_ptr(),
144            x,
145            &mut acc.0,
146            &mut d2,
147        )
148    };
149    result_handler!(ret, d2)
150}
151
152/// This function returns the numerical integral result of an interpolated function over the range
153/// [a, b], using the interpolation object interp, data arrays xa and ya and the accelerator acc.
154#[doc(alias = "gsl_interp_eval_integ")]
155pub fn eval_integ(
156    interp: &::Interp,
157    xa: &[f64],
158    ya: &[f64],
159    a: f64,
160    b: f64,
161    acc: &mut ::InterpAccel,
162) -> f64 {
163    unsafe {
164        sys::gsl_interp_eval_integ(
165            interp.unwrap_shared(),
166            xa.as_ptr(),
167            ya.as_ptr(),
168            a,
169            b,
170            &mut acc.0,
171        )
172    }
173}
174
175/// This function returns the numerical integral result of an interpolated function over the range
176/// [a, b], using the interpolation object interp, data arrays xa and ya and the accelerator acc.
177///
178/// Returns `(Value, result)`.
179#[doc(alias = "gsl_interp_eval_integ_e")]
180pub fn eval_integ_e(
181    interp: &::Interp,
182    xa: &[f64],
183    ya: &[f64],
184    a: f64,
185    b: f64,
186    acc: &mut ::InterpAccel,
187) -> Result<f64, Value> {
188    let mut result = 0.;
189    let ret = unsafe {
190        sys::gsl_interp_eval_integ_e(
191            interp.unwrap_shared(),
192            xa.as_ptr(),
193            ya.as_ptr(),
194            a,
195            b,
196            &mut acc.0,
197            &mut result,
198        )
199    };
200    result_handler!(ret, result)
201}