use crate::Value;
use ffi::FFI;
#[derive(Clone)]
pub struct InterpAccel(pub sys::gsl_interp_accel);
impl InterpAccel {
#[allow(clippy::new_without_default)]
pub fn new() -> InterpAccel {
InterpAccel(sys::gsl_interp_accel {
cache: 0,
miss_count: 0,
hit_count: 0,
})
}
pub fn reset(&mut self) {
self.0.cache = 0;
self.0.miss_count = 0;
self.0.hit_count = 0;
}
#[doc(alias = "gsl_interp_accel_find")]
pub fn find(&mut self, x_array: &[f64], x: f64) -> usize {
unsafe { sys::gsl_interp_accel_find(&mut self.0, x_array.as_ptr(), x_array.len() as _, x) }
}
}
ffi_wrapper!(Interp, *mut sys::gsl_interp, gsl_interp_free);
impl Interp {
#[doc(alias = "gsl_interp_alloc")]
pub fn new(t: InterpType, size: usize) -> Option<Interp> {
let tmp = unsafe { sys::gsl_interp_alloc(t.unwrap_shared(), size) };
if tmp.is_null() {
None
} else {
Some(Self::wrap(tmp))
}
}
#[doc(alias = "gsl_interp_init")]
pub fn init(&mut self, xa: &[f64], ya: &[f64]) -> Result<(), Value> {
assert!(ya.len() >= xa.len());
let ret = unsafe {
sys::gsl_interp_init(
self.unwrap_unique(),
xa.as_ptr(),
ya.as_ptr(),
xa.len() as _,
)
};
result_handler!(ret, ())
}
#[doc(alias = "gsl_interp_name")]
pub fn name(&self) -> String {
let tmp = unsafe { sys::gsl_interp_name(self.unwrap_shared()) };
if tmp.is_null() {
String::new()
} else {
unsafe {
String::from_utf8_lossy(::std::ffi::CStr::from_ptr(tmp).to_bytes()).to_string()
}
}
}
#[doc(alias = "gsl_interp_min_size")]
pub fn min_size(&self) -> u32 {
unsafe { sys::gsl_interp_min_size(self.unwrap_shared()) }
}
}
ffi_wrapper!(InterpType, *const sys::gsl_interp_type);
impl InterpType {
#[doc(alias = "gsl_interp_type_min_size")]
pub fn min_size(&self) -> u32 {
unsafe { sys::gsl_interp_type_min_size(self.unwrap_shared()) }
}
#[doc(alias = "gsl_interp_linear")]
pub fn linear() -> InterpType {
ffi_wrap!(gsl_interp_linear)
}
#[doc(alias = "gsl_interp_polynomial")]
pub fn polynomial() -> InterpType {
ffi_wrap!(gsl_interp_polynomial)
}
#[doc(alias = "gsl_interp_cspline")]
pub fn cspline() -> InterpType {
ffi_wrap!(gsl_interp_cspline)
}
#[doc(alias = "gsl_interp_cspline_periodic")]
pub fn cspline_periodic() -> InterpType {
ffi_wrap!(gsl_interp_cspline_periodic)
}
#[doc(alias = "gsl_interp_akima")]
pub fn akima() -> InterpType {
ffi_wrap!(gsl_interp_akima)
}
#[doc(alias = "gsl_interp_akima_periodic")]
pub fn akima_periodic() -> InterpType {
ffi_wrap!(gsl_interp_akima_periodic)
}
}
ffi_wrapper!(
Spline,
*mut sys::gsl_spline,
gsl_spline_free,
"General interpolation object."
);
impl Spline {
#[doc(alias = "gsl_spline_alloc")]
pub fn new(t: InterpType, size: usize) -> Option<Spline> {
let tmp = unsafe { sys::gsl_spline_alloc(t.unwrap_shared(), size) };
if tmp.is_null() {
None
} else {
Some(Self::wrap(tmp))
}
}
#[doc(alias = "gsl_spline_init")]
pub fn init(&mut self, xa: &[f64], ya: &[f64]) -> Result<(), Value> {
let ret = unsafe {
sys::gsl_spline_init(
self.unwrap_unique(),
xa.as_ptr(),
ya.as_ptr(),
xa.len() as _,
)
};
result_handler!(ret, ())
}
#[doc(alias = "gsl_spline_name")]
pub fn name(&self) -> String {
let tmp = unsafe { sys::gsl_spline_name(self.unwrap_shared()) };
if tmp.is_null() {
String::new()
} else {
unsafe {
String::from_utf8_lossy(::std::ffi::CStr::from_ptr(tmp).to_bytes()).to_string()
}
}
}
#[doc(alias = "gsl_spline_min_size")]
pub fn min_size(&self) -> u32 {
unsafe { sys::gsl_spline_min_size(self.unwrap_shared()) }
}
#[doc(alias = "gsl_spline_eval")]
pub fn eval(&self, x: f64, acc: &mut InterpAccel) -> f64 {
unsafe { sys::gsl_spline_eval(self.unwrap_shared(), x, &mut acc.0) }
}
#[doc(alias = "gsl_spline_eval_e")]
pub fn eval_e(&self, x: f64, acc: &mut InterpAccel) -> Result<f64, Value> {
let mut y = 0.;
let ret = unsafe { sys::gsl_spline_eval_e(self.unwrap_shared(), x, &mut acc.0, &mut y) };
result_handler!(ret, y)
}
#[doc(alias = "gsl_spline_eval_deriv")]
pub fn eval_deriv(&self, x: f64, acc: &mut InterpAccel) -> f64 {
unsafe { sys::gsl_spline_eval_deriv(self.unwrap_shared(), x, &mut acc.0) }
}
#[doc(alias = "gsl_spline_eval_deriv_e")]
pub fn eval_deriv_e(&self, x: f64, acc: &mut InterpAccel) -> Result<f64, Value> {
let mut d = 0.;
let ret =
unsafe { sys::gsl_spline_eval_deriv_e(self.unwrap_shared(), x, &mut acc.0, &mut d) };
result_handler!(ret, d)
}
#[doc(alias = "gsl_spline_eval_deriv2")]
pub fn eval_deriv2(&self, x: f64, acc: &mut InterpAccel) -> f64 {
unsafe { sys::gsl_spline_eval_deriv2(self.unwrap_shared(), x, &mut acc.0) }
}
#[doc(alias = "gsl_spline_eval_deriv2_e")]
pub fn eval_deriv2_e(&self, x: f64, acc: &mut InterpAccel) -> Result<f64, Value> {
let mut d2 = 0.;
let ret =
unsafe { sys::gsl_spline_eval_deriv2_e(self.unwrap_shared(), x, &mut acc.0, &mut d2) };
result_handler!(ret, d2)
}
#[doc(alias = "gsl_spline_eval_integ")]
pub fn eval_integ(&self, a: f64, b: f64, acc: &mut InterpAccel) -> f64 {
unsafe { sys::gsl_spline_eval_integ(self.unwrap_shared(), a, b, &mut acc.0) }
}
#[doc(alias = "gsl_spline_eval_integ_e")]
pub fn eval_integ_e(&self, a: f64, b: f64, acc: &mut InterpAccel) -> Result<f64, Value> {
let mut result = 0.;
let ret = unsafe {
sys::gsl_spline_eval_integ_e(self.unwrap_shared(), a, b, &mut acc.0, &mut result)
};
result_handler!(ret, result)
}
}