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
use ffi;
use enums;
use std::f64::consts::PI;
use c_vec::CSlice;
pub struct ChebSeries {
c: *mut ffi::gsl_cheb_series,
data: CSlice<f64>
}
impl ChebSeries {
pub fn new(n: usize) -> Option<ChebSeries> {
let tmp = unsafe { ffi::gsl_cheb_alloc(n) };
if tmp.is_null() {
None
} else {
unsafe {
Some(ChebSeries {
c: tmp,
data: CSlice::new((*tmp).c, (*tmp).order as usize + 1)
})
}
}
}
pub fn init<T>(&mut self, func: ::function<T>, a: f64, b: f64, param: &mut T) -> enums::Value {
if a >= b {
rgsl_error!("null function interval [a,b]", ::Value::Domain);
::Value::Failure
} else {
unsafe {
(*self.c).a = a;
(*self.c).b = b;
let bma = 0.5 * (b - a);
let bpa = 0.5 * (b + a);
let fac = 2.0 / ((*self.c).order as f64 + 1.0);
let mut tmp_vec = CSlice::new((*self.c).f, (*self.c).order_sp as usize + 1);
for k in 0..((*self.c).order + 1) {
let y = (PI * (k as f64 + 0.5) / ((*self.c).order as f64 + 1f64)).cos();
tmp_vec.as_mut()[k as usize] = func(y * bma + bpa, param);
}
for j in 0..((*self.c).order + 1) {
let mut sum = 0f64;
for k in 0..((*self.c).order + 1) {
sum += tmp_vec.as_ref()[k as usize] * (PI * j as f64 * (k as f64 + 0.5) / ((*self.c).order as f64 + 1f64)).cos();
}
self.data.as_mut()[j as usize] = fac * sum;
}
}
::Value::Success
}
}
pub fn order(&self) -> usize {
unsafe { ffi::gsl_cheb_order(self.c) }
}
pub fn size(&self) -> usize {
unsafe { ffi::gsl_cheb_size(self.c) }
}
pub fn as_slice<'r>(&'r self) -> &'r [f64] {
self.data.as_ref()
}
pub fn as_mut_slice<'r>(&'r mut self) -> &'r mut [f64] {
self.data.as_mut()
}
pub fn eval(&self, x: f64) -> f64 {
unsafe { ffi::gsl_cheb_eval(self.c, x) }
}
pub fn eval_err(&self, x: f64, result: &mut f64, abs_err: &mut f64) -> enums::Value {
unsafe { ffi::gsl_cheb_eval_err(self.c, x, result, abs_err) }
}
pub fn eval_n(&self, order: usize, x: f64) -> f64 {
unsafe { ffi::gsl_cheb_eval_n(self.c, order, x) }
}
pub fn eval_n_err(&self, order: usize, x: f64, result: &mut f64, abs_err: &mut f64) -> enums::Value {
unsafe { ffi::gsl_cheb_eval_n_err(self.c, order, x, result, abs_err) }
}
pub fn calc_deriv(&self, deriv: &ChebSeries) -> enums::Value {
unsafe { ffi::gsl_cheb_calc_deriv(deriv.c, self.c) }
}
pub fn calc_integ(&self, integ: &ChebSeries) -> enums::Value {
unsafe { ffi::gsl_cheb_calc_integ(integ.c, self.c) }
}
}
impl Drop for ChebSeries {
fn drop(&mut self) {
unsafe { ffi::gsl_cheb_free(self.c) };
self.c = ::std::ptr::null_mut();
}
}
impl ffi::FFI<ffi::gsl_cheb_series> for ChebSeries {
fn wrap(c: *mut ffi::gsl_cheb_series) -> ChebSeries {
unsafe {
ChebSeries {
c: c,
data: CSlice::new((*c).c, (*c).order as usize + 1)
}
}
}
fn unwrap(c: &ChebSeries) -> *mut ffi::gsl_cheb_series {
c.c
}
}