1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
use ffi;
use enums;
#[repr(C)]
#[derive(Clone, Copy)]
pub struct InterpAccel {
pub cache: usize,
pub miss_count: usize,
pub hit_count: usize
}
impl InterpAccel {
pub fn new() -> InterpAccel {
InterpAccel {
cache: 0usize,
miss_count: 0usize,
hit_count: 0usize
}
}
pub fn reset(&mut self) {
self.cache = 0usize;
self.miss_count = 0usize;
self.hit_count = 0usize;
}
pub fn find(&mut self, x_array: &[f64], x: f64) -> usize {
unsafe { ffi::gsl_interp_accel_find(self, x_array.as_ptr(), x_array.len() as usize, x) }
}
}
pub struct Interp {
interp: *mut ffi::gsl_interp
}
impl Interp {
pub fn new(t: &InterpType, size: usize) -> Option<Interp> {
let tmp = unsafe { ffi::gsl_interp_alloc(t.t, size) };
if tmp.is_null() {
None
} else {
Some(Interp {
interp: tmp
})
}
}
pub fn init(&self, xa: &[f64], ya: &[f64]) -> enums::Value {
unsafe { ffi::gsl_interp_init(self.interp, xa.as_ptr(), ya.as_ptr(), xa.len() as usize) }
}
pub fn name(&self) -> String {
let tmp = unsafe { ffi::gsl_interp_name(self.interp) };
if tmp.is_null() {
String::new()
} else {
unsafe { String::from_utf8_lossy(::std::ffi::CStr::from_ptr(tmp).to_bytes()).to_string() }
}
}
pub fn min_size(&self) -> u32 {
unsafe { ffi::gsl_interp_min_size(self.interp) }
}
}
impl Drop for Interp {
fn drop(&mut self) {
unsafe { ffi::gsl_interp_free(self.interp) };
self.interp = ::std::ptr::null_mut();
}
}
impl ffi::FFI<ffi::gsl_interp> for Interp {
fn wrap(interp: *mut ffi::gsl_interp) -> Interp {
Interp {
interp: interp
}
}
fn unwrap(interp: &Interp) -> *mut ffi::gsl_interp {
interp.interp
}
}
#[derive(Clone, Copy)]
pub struct InterpType {
t: *const ffi::gsl_interp_type
}
impl InterpType {
pub fn min_size(&self) -> u32 {
unsafe { ffi::gsl_interp_type_min_size(self.t) }
}
pub fn linear() -> InterpType {
ffi::FFI::wrap(ffi::gsl_interp_linear as *mut ffi::gsl_interp_type)
}
pub fn polynomial() -> InterpType {
ffi::FFI::wrap(ffi::gsl_interp_polynomial as *mut ffi::gsl_interp_type)
}
pub fn cspline() -> InterpType {
ffi::FFI::wrap(ffi::gsl_interp_cspline as *mut ffi::gsl_interp_type)
}
pub fn cspline_periodic() -> InterpType {
ffi::FFI::wrap(ffi::gsl_interp_cspline_periodic as *mut ffi::gsl_interp_type)
}
pub fn akima() -> InterpType {
ffi::FFI::wrap(ffi::gsl_interp_akima as *mut ffi::gsl_interp_type)
}
pub fn akima_periodic() -> InterpType {
ffi::FFI::wrap(ffi::gsl_interp_akima_periodic as *mut ffi::gsl_interp_type)
}
}
impl ffi::FFI<ffi::gsl_interp_type> for InterpType {
fn wrap(t: *mut ffi::gsl_interp_type) -> InterpType {
InterpType {
t: t
}
}
fn unwrap(t: &InterpType) -> *mut ffi::gsl_interp_type {
t.t as *mut ffi::gsl_interp_type
}
}
pub struct Spline {
spline: *mut ffi::gsl_spline
}
impl Spline {
pub fn new(t: &InterpType, size: usize) -> Option<Spline> {
let tmp = unsafe { ffi::gsl_spline_alloc(t.t, size) };
if tmp.is_null() {
None
} else {
Some(Spline {
spline: tmp
})
}
}
pub fn init(&self, xa: &[f64], ya: &[f64]) -> enums::Value {
unsafe { ffi::gsl_spline_init(self.spline, xa.as_ptr(), ya.as_ptr(), xa.len() as usize) }
}
pub fn name(&self) -> String {
let tmp = unsafe { ffi::gsl_spline_name(self.spline) };
if tmp.is_null() {
String::new()
} else {
unsafe { String::from_utf8_lossy(::std::ffi::CStr::from_ptr(tmp).to_bytes()).to_string() }
}
}
pub fn min_size(&self) -> u32 {
unsafe { ffi::gsl_spline_min_size(self.spline) }
}
pub fn eval(&self, x: f64, acc: &mut InterpAccel) -> f64 {
unsafe { ffi::gsl_spline_eval(self.spline, x, acc) }
}
pub fn eval_e(&self, x: f64, acc: &mut InterpAccel, y: &mut f64) -> enums::Value {
unsafe { ffi::gsl_spline_eval_e(self.spline, x, acc, y) }
}
pub fn eval_deriv(&self, x: f64, acc: &mut InterpAccel) -> f64 {
unsafe { ffi::gsl_spline_eval_deriv(self.spline, x, acc) }
}
pub fn eval_deriv_e(&self, x: f64, acc: &mut InterpAccel, d: &mut f64) -> enums::Value {
unsafe { ffi::gsl_spline_eval_deriv_e(self.spline, x, acc, d) }
}
pub fn eval_deriv2(&self, x: f64, acc: &mut InterpAccel) -> f64 {
unsafe { ffi::gsl_spline_eval_deriv2(self.spline, x, acc) }
}
pub fn eval_deriv2_e(&self, x: f64, acc: &mut InterpAccel, d2: &mut f64) -> enums::Value {
unsafe { ffi::gsl_spline_eval_deriv2_e(self.spline, x, acc, d2) }
}
pub fn eval_integ(&self, a: f64, b: f64, acc: &mut InterpAccel) -> f64 {
unsafe { ffi::gsl_spline_eval_integ(self.spline, a, b, acc) }
}
pub fn eval_integ_e(&self, a: f64, b: f64, acc: &mut InterpAccel, result: &mut f64) -> enums::Value {
unsafe { ffi::gsl_spline_eval_integ_e(self.spline, a, b, acc, result) }
}
}
impl Drop for Spline {
fn drop(&mut self) {
unsafe { ffi::gsl_spline_free(self.spline) };
self.spline = ::std::ptr::null_mut();
}
}
impl ffi::FFI<ffi::gsl_spline> for Spline {
fn wrap(spline: *mut ffi::gsl_spline) -> Spline {
Spline {
spline: spline
}
}
fn unwrap(spline: &Spline) -> *mut ffi::gsl_spline {
spline.spline
}
}